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
30typedef std::variant<int,
31 unsigned int,
32 std::string,
33 Real,
34 bool,
35 std::vector<int>,
36 std::vector<unsigned int>,
37 std::vector<std::string>,
38 std::vector<Real>,
39 std::vector<bool>>
41
42namespace libMesh
43{
44class BoundingBox;
45class Point;
46template <typename T>
47class DenseVector;
48template <typename T>
49class DenseMatrix;
50template <typename T>
51class NumericVector;
52}
53
54// Overloads for to_json, which _must_ be overloaded in the namespace
55// in which the object is found in order to enable argument-dependent lookup.
56// See https://en.cppreference.com/w/cpp/language/adl for more information
57void to_json(nlohmann::json & json, const MooseApp & app); // MooseDocs:to_json
58
59namespace libMesh
60{
61void to_json(nlohmann::json & json, const BoundingBox & p);
62void to_json(nlohmann::json & json, const Point & p);
63void to_json(nlohmann::json & json, const DenseVector<Real> & vector);
64void to_json(nlohmann::json & json, const DenseMatrix<Real> & matrix);
65void to_json(nlohmann::json & json, const std::unique_ptr<NumericVector<Number>> & vector);
66}
67
68namespace Eigen
69{
70template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
71void to_json(nlohmann::json & json,
72 const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix);
73}
74
75namespace nlohmann
76{
77// Serializer for AttributeVariant
78template <>
79struct adl_serializer<AttributeVariant>
80{
81 static void to_json(json & j, const AttributeVariant & data)
82 {
83 std::visit([&j](const auto & value) { j = value; }, data);
84 }
85};
86
87template <typename T>
88struct adl_serializer<std::unique_ptr<T>>
89{
94 static void to_json(json & j, const std::unique_ptr<T> & v)
95 {
96 if constexpr (std::is_constructible_v<nlohmann::json, T>)
97 {
98 if (v)
99 j = *v;
100 else
101 j = nullptr;
102 }
103 else
104 mooseAssert(false, "Should not get to this");
105 }
106};
107}
108
109namespace MetaPhysicL
110{
111template <template <typename, size_t> class Array, typename T, size_t N>
112void
113to_json(nlohmann::json & json, const DynamicArrayWrapper<Array, T, N> & array)
114{
115 for (const auto element : array)
116 nlohmann::to_json(json, element);
117}
118
119template <typename T, typename I, typename N, typename ArrayWrapper>
120void
121to_json(nlohmann::json & json,
122 const SemiDynamicSparseNumberArrayGeneric<T, I, N, ArrayWrapper> & sdsna)
123{
124 to_json(json, sdsna.nude_indices());
125 to_json(json, sdsna.nude_data());
126}
127
128template <typename T, typename D, bool asd>
129void
130to_json(nlohmann::json & json, const DualNumber<T, D, asd> & dn)
131{
132 nlohmann::to_json(json, dn.value());
133 to_json(json, dn.derivatives());
134}
135}
136
137namespace Eigen
138{
139template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
140void
141to_json(nlohmann::json & json, const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix)
142{
143 if constexpr (Rows == 1 || Cols == 1)
144 {
145 std::vector<Scalar> values(matrix.data(), matrix.data() + matrix.rows() * matrix.cols());
146 nlohmann::to_json(json, values);
147 }
148 else
149 {
150 const auto nrows = matrix.rows();
151 const auto ncols = matrix.cols();
152 std::vector<std::vector<Scalar>> values(nrows, std::vector<Scalar>(ncols));
153 for (const auto i : make_range(nrows))
154 for (const auto j : make_range(ncols))
155 values[i][j] = matrix(i, j);
156 nlohmann::to_json(json, values);
157 }
158}
159}
160
161template <typename T>
162void
163to_json(nlohmann::json & json, const GenericTwoVector<T> & two_vector)
164{
165 to_json(json, static_cast<const Eigen::Matrix<T, 2, 1> &>(two_vector));
166}
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 > > AttributeVariant
Type definition for a variant that can hold all the supported types for lattice attributes.
Definition JsonIO.h:40
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:69
void to_json(nlohmann::json &json, const Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &matrix)
Definition JsonIO.h:141
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:113
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
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:81
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:94