https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ParallelSolutionStorage.C
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
11#include "NonlinearSystemBase.h"
12
14
17{
19 params.addClassDescription("Parallel container to store serialized solution fields from "
20 "simulations on sub-applications.");
21 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 params.makeParamRequired<std::vector<OutputName>>("outputs");
27 return params;
28}
29
31 : GeneralReporter(parameters),
32 _distributed_solutions(
33 declareValueByName<
34 std::map<VariableName,
35 std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>>>(
36 "parallel_solution_storage", REPORTER_MODE_DISTRIBUTED)),
37 _variable_names(getParam<std::vector<VariableName>>("variables"))
38{
39 for (const auto & vname : _variable_names)
41 vname, std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>());
42}
43
44void
46{
47 for (const auto & vname : _variable_names)
49}
50
51void
52ParallelSolutionStorage::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 _distributed_solutions[vname].emplace(global_i, std::vector<DenseVector<Real>>());
62
63 sample_insert_pair.first->second.push_back(std::move(solution));
64}
65
66unsigned int
68{
69 const auto & samples = libmesh_map_find(_distributed_solutions, vname);
70
71 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 { return std::move(count) + sample.second.size(); });
77}
78
79bool
80ParallelSolutionStorage::hasGlobalSample(unsigned int global_sample_i,
81 const VariableName & variable) const
82{
83 if (_distributed_solutions.find(variable) == _distributed_solutions.end())
84 return false;
85
86 auto & variable_storage = libmesh_map_find(_distributed_solutions, variable);
87
88 return (variable_storage.find(global_sample_i) != variable_storage.end());
89}
90
91const std::vector<DenseVector<Real>> &
92ParallelSolutionStorage::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 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 return libmesh_map_find(variable_storage, global_sample_i);
102}
103
104std::unordered_map<unsigned int, std::vector<DenseVector<Real>>> &
105ParallelSolutionStorage::getStorage(const VariableName & variable)
106{
107 if (_distributed_solutions.find(variable) == _distributed_solutions.end())
109 "We are trying to access container for variable '", variable, "' but we don't have it!");
110
111 return libmesh_map_find(_distributed_solutions, variable);
112}
113
114const std::unordered_map<unsigned int, std::vector<DenseVector<Real>>> &
115ParallelSolutionStorage::getStorage(const VariableName & variable) const
116{
117 if (_distributed_solutions.find(variable) == _distributed_solutions.end())
119 "We are trying to access container for variable '", variable, "' but we don't have it!");
120
121 return libmesh_map_find(_distributed_solutions, variable);
122}
123
124void
126 nlohmann::json & json,
127 const std::map<VariableName, std::unordered_map<unsigned int, std::vector<DenseVector<Real>>>> &
128 solution_storage)
129{
130 for (const auto & vname_pair : solution_storage)
131 {
132 auto & variable_storage = json[vname_pair.first];
133 for (const auto & sample_pair : vname_pair.second)
134 {
135 auto & sample_storage = variable_storage[std::to_string(sample_pair.first)];
136 sample_storage = std::vector<std::vector<Real>>();
137 for (const auto & sample : sample_pair.second)
138 {
139 sample_storage.push_back(sample.get_values());
140 }
141 }
142 }
143}
unsigned int count
void to_json(nlohmann::json &json, const std::map< VariableName, std::unordered_map< unsigned int, std::vector< DenseVector< Real > > > > &solution_storage)
registerMooseObject("StochasticToolsApp", ParallelSolutionStorage)
sideset clear()
const ReporterMode REPORTER_MODE_DISTRIBUTED
void ErrorVector unsigned int
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void makeParamRequired(const std::string &name)
void mooseError(Args &&... args) const
A Reporter which stores serialized solution fields for given variables in a distributed fashion.
void addEntry(const VariableName &vname, unsigned int global_i, const DenseVector< Real > &solution)
Add a new solution entry to the container.
bool hasGlobalSample(unsigned int global_sample_i, const VariableName &variable) const
Determine if we have the solution vector with a given global sample index for a given variable.
std::map< VariableName, std::unordered_map< unsigned int, std::vector< DenseVector< Real > > > > & getStorage() const
Get the whole solution container.
std::map< VariableName, std::unordered_map< unsigned int, std::vector< DenseVector< Real > > > > & _distributed_solutions
The container of the solutions.
const std::vector< DenseVector< Real > > & getGlobalSample(unsigned int global_sample_i, const VariableName &variable) const
Get the serialized solution field which is associated with a given global sample index and variable.
virtual void initialSetup() override
const std::vector< VariableName > & _variable_names
The names of the variables whose serialized solution this object is supposed to receive.
ParallelSolutionStorage(const InputParameters &parameters)
unsigned int totalNumberOfStoredSolutions(const VariableName &vname) const
Return the number of total stored solutions for a given variable.
static InputParameters validParams()