LCOV - code coverage report
Current view: top level - include/outputs - JsonIO.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31405 (292dce) with base fef103 Lines: 18 18 100.0 %
Date: 2025-09-04 07:52:05 Functions: 3 5 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : {
      60             :   /// Serializer that will output a unique ptr if it exists. We wrap this
      61             :   /// with is_constructible_v so that we don't specialize types that
      62             :   /// don't already have a specialization. This is required for some earlier
      63             :   /// compilers, even though we're not using it at the moment
      64           2 :   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           2 :       if (v)
      69           1 :         j = *v;
      70             :       else
      71           1 :         j = nullptr;
      72             :     }
      73             :     else
      74             :       mooseAssert(false, "Should not get to this");
      75           2 :   }
      76             : };
      77             : }
      78             : 
      79             : namespace Eigen
      80             : {
      81             : template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
      82             : void
      83          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           9 :     std::vector<Scalar> values(matrix.data(), matrix.data() + matrix.rows() * matrix.cols());
      88           9 :     nlohmann::to_json(json, values);
      89           9 :   }
      90             :   else
      91             :   {
      92          74 :     const auto nrows = matrix.rows();
      93          74 :     const auto ncols = matrix.cols();
      94         148 :     std::vector<std::vector<Scalar>> values(nrows, std::vector<Scalar>(ncols));
      95         222 :     for (const auto i : make_range(nrows))
      96         550 :       for (const auto j : make_range(ncols))
      97         402 :         values[i][j] = matrix(i, j);
      98          74 :     nlohmann::to_json(json, values);
      99          74 :   }
     100          83 : }
     101             : }

Generated by: LCOV version 1.14