https://mooseframework.inl.gov
LoadCovarianceDataAction.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 
12 #include "FEProblem.h"
13 #include "StochasticToolsApp.h"
14 
15 registerMooseAction("StochasticToolsApp", LoadCovarianceDataAction, "load_covariance_data");
16 
19 {
21  params.addClassDescription("Calls load method on SurrogateModel objects contained within the "
22  "`[Surrogates]` input block, if a filename is given.");
23  return params;
24 }
25 
27 {
28 }
29 
30 void
32 {
33  std::vector<SurrogateModel *> objects;
34  _app.theWarehouse().query().condition<AttribSystem>("SurrogateModel").queryInto(objects);
35  for (auto model_ptr : objects)
36  {
37  auto * gp_gen = dynamic_cast<GaussianProcessSurrogate *>(model_ptr);
38  if (gp_gen && model_ptr->isParamValid("filename"))
39  load(*gp_gen);
40  }
41 }
42 
43 void
45 {
46  // We grab all the necessary information that is needed to reconstruct the
47  // covariance structure for the GP
48  const std::string & covar_type = model.getGP().getCovarType();
49  const std::string & covar_name = model.getGP().getCovarName();
50  const std::map<UserObjectName, std::string> & dep_covar_types =
51  model.getGP().getDependentCovarTypes();
52  const std::vector<UserObjectName> & dep_covar_names = model.getGP().getDependentCovarNames();
53 
54  // This is for the covariance on the very top, the lower-level covariances are
55  // all assumed to have num_outputs=1.
56  const unsigned int num_outputs = model.getGP().getCovarNumOutputs();
57  const std::unordered_map<std::string, Real> & map = model.getGP().getHyperParamMap();
58  const std::unordered_map<std::string, std::vector<Real>> & vec_map =
59  model.getGP().getHyperParamVectorMap();
60 
61  // We start by creating and loading the lower-level covariances if they need
62  // to be present. Right now we can only load a complex covariance which has
63  // a one-level dependency depth.
64  // TODO: Extend this to arbitrary dependency depths. Maybe we could use a graph.
65  for (const auto & it : dep_covar_types)
66  {
67  const auto & name = it.first;
68  const auto & type = it.second;
70 
71  // We make sure that every required parameter is added so that the object
72  // can be constructed. The non-required hyperparameters (if present in the
73  // parameter maps) will be inserted later.
74  const auto param_list = covar_params.getParametersList();
75  for (const auto & param : param_list)
76  if (covar_params.isParamRequired(param))
77  {
78  const std::string expected_name = name + ":" + param;
79  for (const auto & it_map : map)
80  {
81  const auto pos = it_map.first.find(expected_name);
82  if (pos != std::string::npos)
83  covar_params.set<Real>(param) = it_map.second;
84  }
85  for (const auto & it_map : vec_map)
86  {
87  const auto pos = it_map.first.find(expected_name);
88  if (pos != std::string::npos)
89  covar_params.set<std::vector<Real>>(param) = it_map.second;
90  }
91  }
92 
93  _problem->addObject<CovarianceFunctionBase>(type, name, covar_params, /*threaded=*/false);
94  }
95 
96  InputParameters covar_params = _factory.getValidParams(covar_type);
97  covar_params.set<unsigned int>("num_outputs") = num_outputs;
98  covar_params.set<std::vector<UserObjectName>>("covariance_functions") = dep_covar_names;
99 
100  const auto param_list = covar_params.getParametersList();
101  for (const auto & param : param_list)
102  // We make sure that every required parameter is added so that the object
103  // can be constructed. The non-required hyperparameters (if present in the
104  // parameter maps) will be inserted later.
105  if (covar_params.isParamRequired(param))
106  {
107  const std::string expected_name = covar_name + ":" + param;
108 
109  const auto & map_it = map.find(expected_name);
110  if (map_it != map.end())
111  covar_params.set<Real>(param) = map_it->second;
112 
113  const auto & vec_map_it = vec_map.find(expected_name);
114  if (vec_map_it != vec_map.end())
115  covar_params.set<std::vector<Real>>(param) = vec_map_it->second;
116  }
117 
118  auto covar_object = _problem->addObject<CovarianceFunctionBase>(
119  covar_type, covar_name, covar_params, /* threaded = */ false);
120  covar_object[0]->loadHyperParamMap(map, vec_map);
121 
122  model.setupCovariance(covar_name);
123 }
virtual void act() override
void loadHyperParamMap(const std::unordered_map< std::string, Real > &map, const std::unordered_map< std::string, std::vector< Real >> &vec_map)
Load some hyperparameters into the local maps contained in this object.
std::set< std::string > getParametersList() const
MooseApp & _app
T & set(const std::string &name, bool quiet_mode=false)
InputParameters getValidParams(const std::string &name) const
Base class for covariance functions that are used in Gaussian Processes.
void load(GaussianProcessSurrogate &model)
virtual const std::string & name() const
Factory & _factory
static InputParameters validParams()
static InputParameters validParams()
LoadCovarianceDataAction(const InputParameters &params)
const std::string & type() const
const PertinentGeochemicalSystem model(database, {"H2O", "H+", "HCO3-", "O2(aq)", "Ca++", ">(s)FeOH", "radius_neg1", "radius_neg1.5"}, {"Calcite"}, {}, {"Calcite_asdf"}, {"CH4(aq)"}, {">(s)FeOCa+"}, "O2(aq)", "e-")
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Query query()
bool isParamRequired(const std::string &name) const
void addClassDescription(const std::string &doc_string)
std::shared_ptr< FEProblemBase > & _problem
TheWarehouse & theWarehouse()
registerMooseAction("StochasticToolsApp", LoadCovarianceDataAction, "load_covariance_data")