https://mooseframework.inl.gov
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 
14 #include "nlohmann/json.h"
15 
16 #include "libmesh/libmesh_common.h"
17 
18 #include "Eigen/Core"
19 
20 #include <memory>
21 
22 class MooseApp;
23 
24 namespace libMesh
25 {
26 class Point;
27 template <typename T>
28 class DenseVector;
29 template <typename T>
30 class DenseMatrix;
31 template <typename T>
32 class NumericVector;
33 }
34 
35 // Overloads for to_json, which _must_ be overloaded in the namespace
36 // in which the object is found in order to enable argument-dependent lookup.
37 // See https://en.cppreference.com/w/cpp/language/adl for more information
38 void to_json(nlohmann::json & json, const MooseApp & app); // MooseDocs:to_json
39 
40 namespace libMesh
41 {
42 void to_json(nlohmann::json & json, const Point & p);
43 void to_json(nlohmann::json & json, const DenseVector<Real> & vector);
44 void to_json(nlohmann::json & json, const DenseMatrix<Real> & matrix);
45 void to_json(nlohmann::json & json, const std::unique_ptr<NumericVector<Number>> & vector);
46 }
47 
48 namespace Eigen
49 {
50 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
51 void to_json(nlohmann::json & json,
52  const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix);
53 }
54 
55 namespace nlohmann
56 {
57 template <typename T>
58 struct adl_serializer<std::unique_ptr<T>>
59 {
64  static void to_json(json & j, const std::unique_ptr<T> & v)
65  {
66  if constexpr (std::is_constructible_v<nlohmann::json, T>)
67  {
68  if (v)
69  j = *v;
70  else
71  j = nullptr;
72  }
73  else
74  mooseAssert(false, "Should not get to this");
75  }
76 };
77 }
78 
79 namespace Eigen
80 {
81 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
82 void
83 to_json(nlohmann::json & json, const Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> & matrix)
84 {
85  if constexpr (Rows == 1 || Cols == 1)
86  {
87  std::vector<Scalar> values(matrix.data(), matrix.data() + matrix.rows() * matrix.cols());
88  nlohmann::to_json(json, values);
89  }
90  else
91  {
92  const auto nrows = matrix.rows();
93  const auto ncols = matrix.cols();
94  std::vector<std::vector<Scalar>> values(nrows, std::vector<Scalar>(ncols));
95  for (const auto i : make_range(nrows))
96  for (const auto j : make_range(ncols))
97  values[i][j] = matrix(i, j);
98  nlohmann::to_json(json, values);
99  }
100 }
101 }
template class LIBMESH_EXPORT DenseVector< Real >
Definition: JsonIO.h:48
Base class for MOOSE-based applications.
Definition: MooseApp.h:96
void to_json(nlohmann::json &json, const Point &p)
Definition: JsonIO.C:47
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
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:64
IntRange< T > make_range(T beg, T end)
void to_json(nlohmann::json &json, const MooseApp &app)
Definition: JsonIO.C:22
void to_json(nlohmann::json &json, const Matrix< Scalar, Rows, Cols, Options, MaxRows, MaxCols > &matrix)
Definition: JsonIO.h:83