LCOV - code coverage report
Current view: top level - src/reporters - ParallelSolutionStorage.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 40 50 80.0 %
Date: 2025-07-25 05:00:46 Functions: 8 10 80.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             : #include "ParallelSolutionStorage.h"
      11             : #include "NonlinearSystemBase.h"
      12             : 
      13             : registerMooseObject("StochasticToolsApp", ParallelSolutionStorage);
      14             : 
      15             : InputParameters
      16         700 : ParallelSolutionStorage::validParams()
      17             : {
      18         700 :   InputParameters params = GeneralReporter::validParams();
      19         700 :   params.addClassDescription("Parallel container to store serialized solution fields from "
      20             :                              "simulations on sub-applications.");
      21        1400 :   params.addRequiredParam<std::vector<VariableName>>(
      22             :       "variables",
      23             :       "The names of the variables whose serialized solution this object is supposed to receive.");
      24             : 
      25             :   // Making this required to make sure nobody prints full solution vectors on accident
      26         700 :   params.makeParamRequired<std::vector<OutputName>>("outputs");
      27         700 :   return params;
      28           0 : }
      29             : 
      30         350 : ParallelSolutionStorage::ParallelSolutionStorage(const InputParameters & parameters)
      31             :   : GeneralReporter(parameters),
      32         350 :     _distributed_solutions(
      33             :         declareValueByName<
      34             :             std::map<VariableName,
      35         700 :                      std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>>>(
      36             :             "parallel_solution_storage", REPORTER_MODE_DISTRIBUTED)),
      37        1050 :     _variable_names(getParam<std::vector<VariableName>>("variables"))
      38             : {
      39         900 :   for (const auto & vname : _variable_names)
      40         550 :     _distributed_solutions.emplace(
      41        1100 :         vname, std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>());
      42         350 : }
      43             : 
      44             : void
      45         350 : ParallelSolutionStorage::initialSetup()
      46             : {
      47         900 :   for (const auto & vname : _variable_names)
      48         550 :     _distributed_solutions[vname].clear();
      49         350 : }
      50             : 
      51             : void
      52         920 : ParallelSolutionStorage::addEntry(const VariableName & vname,
      53             :                                   unsigned int global_i,
      54             :                                   const DenseVector<Real> & solution)
      55             : {
      56             :   mooseAssert(std::find(_variable_names.begin(), _variable_names.end(), vname) !=
      57             :                   _variable_names.end(),
      58             :               "We are trying to add a variable that we cannot receive!");
      59             : 
      60             :   auto sample_insert_pair =
      61        1840 :       _distributed_solutions[vname].emplace(global_i, std::vector<DenseVector<Real>>());
      62             : 
      63         920 :   sample_insert_pair.first->second.push_back(std::move(solution));
      64         920 : }
      65             : 
      66             : unsigned int
      67           0 : ParallelSolutionStorage::totalNumberOfStoredSolutions(const VariableName & vname) const
      68             : {
      69           0 :   const auto & samples = libmesh_map_find(_distributed_solutions, vname);
      70             : 
      71           0 :   return std::accumulate(
      72             :       samples.begin(),
      73             :       samples.end(),
      74             :       0,
      75             :       [](unsigned int count, const std::pair<unsigned int, std::vector<DenseVector<Real>>> & sample)
      76           0 :       { return std::move(count) + sample.second.size(); });
      77             : }
      78             : 
      79             : bool
      80         640 : ParallelSolutionStorage::hasGlobalSample(unsigned int global_sample_i,
      81             :                                          const VariableName & variable) const
      82             : {
      83         640 :   if (_distributed_solutions.find(variable) == _distributed_solutions.end())
      84             :     return false;
      85             : 
      86         640 :   auto & variable_storage = libmesh_map_find(_distributed_solutions, variable);
      87             : 
      88             :   return (variable_storage.find(global_sample_i) != variable_storage.end());
      89             : }
      90             : 
      91             : const std::vector<DenseVector<Real>> &
      92         640 : ParallelSolutionStorage::getGlobalSample(unsigned int global_sample_i,
      93             :                                          const VariableName & variable) const
      94             : {
      95             :   mooseAssert(_distributed_solutions.find(variable) != _distributed_solutions.end(),
      96             :               "We don't have the requested variable!");
      97         640 :   const auto & variable_storage = libmesh_map_find(_distributed_solutions, variable);
      98             :   mooseAssert(variable_storage.find(global_sample_i) != variable_storage.end(),
      99             :               "We don't have the requested global sample index! ");
     100             : 
     101         640 :   return libmesh_map_find(variable_storage, global_sample_i);
     102             : }
     103             : 
     104             : std::unordered_map<unsigned int, std::vector<DenseVector<Real>>> &
     105           0 : ParallelSolutionStorage::getStorage(const VariableName & variable)
     106             : {
     107           0 :   if (_distributed_solutions.find(variable) == _distributed_solutions.end())
     108           0 :     mooseError(
     109             :         "We are trying to access container for variable '", variable, "' but we don't have it!");
     110             : 
     111           0 :   return libmesh_map_find(_distributed_solutions, variable);
     112             : }
     113             : 
     114             : const std::unordered_map<unsigned int, std::vector<DenseVector<Real>>> &
     115         250 : ParallelSolutionStorage::getStorage(const VariableName & variable) const
     116             : {
     117         250 :   if (_distributed_solutions.find(variable) == _distributed_solutions.end())
     118           0 :     mooseError(
     119             :         "We are trying to access container for variable '", variable, "' but we don't have it!");
     120             : 
     121         250 :   return libmesh_map_find(_distributed_solutions, variable);
     122             : }
     123             : 
     124             : void
     125         260 : to_json(
     126             :     nlohmann::json & json,
     127             :     const std::map<VariableName, std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>> &
     128             :         solution_storage)
     129             : {
     130         680 :   for (const auto & vname_pair : solution_storage)
     131             :   {
     132         420 :     auto & variable_storage = json[vname_pair.first];
     133         900 :     for (const auto & sample_pair : vname_pair.second)
     134             :     {
     135         960 :       auto & sample_storage = variable_storage[std::to_string(sample_pair.first)];
     136         960 :       sample_storage = std::vector<std::vector<Real>>();
     137         960 :       for (const auto & sample : sample_pair.second)
     138             :       {
     139         960 :         sample_storage.push_back(sample.get_values());
     140             :       }
     141             :     }
     142             :   }
     143         260 : }

Generated by: LCOV version 1.14