https://mooseframework.inl.gov
Loading...
Searching...
No Matches
JsonIO.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "MooseError.h"
13#include "TwoVector.h"
14
15#include "nlohmann/json.h"
16
17#include <variant>
18
19#include "libmesh/libmesh_common.h"
20#include "Eigen/Core"
21#include "metaphysicl/dualsemidynamicsparsenumberarray.h"
22
23#include <memory>
24
25class MooseApp;
26
31typedef std::variant<int,
32 unsigned int,
33 std::string,
34 Real,
35 bool,
36 std::vector<int>,
37 std::vector<unsigned int>,
38 std::vector<std::string>,
39 std::vector<Real>,
40 std::vector<bool>,
41 std::vector<std::vector<std::string>>>
43
44namespace libMesh
45{
46class BoundingBox;
47class Point;
48template <typename T>
49class DenseVector;
50template <typename T>
51class DenseMatrix;
52template <typename T>
53class NumericVector;
54}
55
56// Overloads for to_json, which _must_ be overloaded in the namespace
57// in which the object is found in order to enable argument-dependent lookup.
58// See https://en.cppreference.com/w/cpp/language/adl for more information
59void to_json(nlohmann::json & json, const MooseApp & app); // MooseDocs:to_json
60
61namespace libMesh
62{
63void to_json(nlohmann::json & json, const BoundingBox & p);
64void to_json(nlohmann::json & json, const Point & p);
65void to_json(nlohmann::json & json, const DenseVector<Real> & vector);
66void to_json(nlohmann::json & json, const DenseMatrix<Real> & matrix);
67void to_json(nlohmann::json & json, const std::unique_ptr<NumericVector<Number>> & vector);
68}
69
70namespace Eigen
71{
72template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
73void to_json(nlohmann::json & json,
74 const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix);
75}
76
77namespace nlohmann
78{
79// Serializer for AttributeVariant
80template <>
81struct adl_serializer<AttributeVariant>
82{
83 static void to_json(json & j, const AttributeVariant & data)
84 {
85 std::visit([&j](const auto & value) { j = value; }, data);
86 }
87};
88
89template <typename T>
90struct adl_serializer<std::unique_ptr<T>>
91{
96 static void to_json(json & j, const std::unique_ptr<T> & v)
97 {
98 if constexpr (std::is_constructible_v<nlohmann::json, T>)
99 {
100 if (v)
101 j = *v;
102 else
103 j = nullptr;
104 }
105 else
106 mooseAssert(false, "Should not get to this");
107 }
108};
109}
110
111namespace MetaPhysicL
112{
113template <template <typename, size_t> class Array, typename T, size_t N>
114void
115to_json(nlohmann::json & json, const DynamicArrayWrapper<Array, T, N> & array)
116{
117 for (const auto element : array)
118 nlohmann::to_json(json, element);
119}
120
121template <typename T, typename I, typename N, typename ArrayWrapper>
122void
123to_json(nlohmann::json & json,
124 const SemiDynamicSparseNumberArrayGeneric<T, I, N, ArrayWrapper> & sdsna)
125{
126 to_json(json, sdsna.nude_indices());
127 to_json(json, sdsna.nude_data());
128}
129
130template <typename T, typename D, bool asd>
131void
132to_json(nlohmann::json & json, const DualNumber<T, D, asd> & dn)
133{
134 nlohmann::to_json(json, dn.value());
135 to_json(json, dn.derivatives());
136}
137}
138
139namespace Eigen
140{
141template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
142void
143to_json(nlohmann::json & json, const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix)
144{
145 if constexpr (Rows == 1 || Cols == 1)
146 {
147 std::vector<Scalar> values(matrix.data(), matrix.data() + matrix.rows() * matrix.cols());
148 nlohmann::to_json(json, values);
149 }
150 else
151 {
152 const auto nrows = matrix.rows();
153 const auto ncols = matrix.cols();
154 std::vector<std::vector<Scalar>> values(nrows, std::vector<Scalar>(ncols));
155 for (const auto i : make_range(nrows))
156 for (const auto j : make_range(ncols))
157 values[i][j] = matrix(i, j);
158 nlohmann::to_json(json, values);
159 }
160}
161}
162
163template <typename T>
164void
165to_json(nlohmann::json & json, const GenericTwoVector<T> & two_vector)
166{
167 to_json(json, libMesh::cast_ref<const Eigen::Matrix<T, 2, 1> &>(two_vector));
168}
void to_json(nlohmann::json &json, const MooseApp &app)
Definition JsonIO.C:23
std::variant< int, unsigned int, std::string, Real, bool, std::vector< int >, std::vector< unsigned int >, std::vector< std::string >, std::vector< Real >, std::vector< bool >, std::vector< std::vector< std::string > > > AttributeVariant
Type definition for a variant that can hold all the supported types for lattice and CSG engineering u...
Definition JsonIO.h:42
std::array< Real, 2 > values
Definition MortarUtils.C:52
void ErrorVector unsigned int
A two-component zero initialized vector used for tangential quantities.
Definition TwoVector.h:20
Base class for MOOSE-based applications.
Definition MooseApp.h:110
Definition JsonIO.h:71
void to_json(nlohmann::json &json, const Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &matrix)
Definition JsonIO.h:143
We need to instantiate the following CompareTypes to tell the compiler that ADReal is a subtype of Ch...
void to_json(nlohmann::json &json, const DynamicArrayWrapper< Array, T, N > &array)
Definition JsonIO.h:115
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
void to_json(nlohmann::json &json, const BoundingBox &p)
Definition JsonIO.C:48
Tnew cast_ref(Told &oldvar)
template class LIBMESH_EXPORT DenseMatrix< Real >
template class LIBMESH_EXPORT DenseVector< Real >
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
template class LIBMESH_EXPORT NumericVector< Number >
static void to_json(json &j, const AttributeVariant &data)
Definition JsonIO.h:83
static void to_json(json &j, const std::unique_ptr< T > &v)
Serializer that will output a unique ptr if it exists.
Definition JsonIO.h:96