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 "LoadCovarianceDataAction.h" 11 : #include "GaussianProcessSurrogate.h" 12 : #include "FEProblem.h" 13 : #include "StochasticToolsApp.h" 14 : 15 : registerMooseAction("StochasticToolsApp", LoadCovarianceDataAction, "load_covariance_data"); 16 : 17 : InputParameters 18 42798 : LoadCovarianceDataAction::validParams() 19 : { 20 42798 : InputParameters params = Action::validParams(); 21 42798 : params.addClassDescription("Calls load method on SurrogateModel objects contained within the " 22 : "`[Surrogates]` input block, if a filename is given."); 23 42798 : return params; 24 0 : } 25 : 26 42798 : LoadCovarianceDataAction::LoadCovarianceDataAction(const InputParameters & params) : Action(params) 27 : { 28 42798 : } 29 : 30 : void 31 42714 : LoadCovarianceDataAction::act() 32 : { 33 : std::vector<SurrogateModel *> objects; 34 85428 : _app.theWarehouse().query().condition<AttribSystem>("SurrogateModel").queryInto(objects); 35 44245 : for (auto model_ptr : objects) 36 : { 37 1531 : auto * gp_gen = dynamic_cast<GaussianProcessSurrogate *>(model_ptr); 38 2321 : if (gp_gen && model_ptr->isParamValid("filename")) 39 34 : load(*gp_gen); 40 : } 41 42714 : } 42 : 43 : void 44 34 : LoadCovarianceDataAction::load(GaussianProcessSurrogate & model) 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 34 : 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 51 : for (const auto & it : dep_covar_types) 66 : { 67 : const auto & name = it.first; 68 17 : const auto & type = it.second; 69 17 : InputParameters covar_params = _factory.getValidParams(type); 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 17 : const auto param_list = covar_params.getParametersList(); 75 391 : for (const auto & param : param_list) 76 374 : if (covar_params.isParamRequired(param)) 77 : { 78 68 : const std::string expected_name = name + ":" + param; 79 102 : for (const auto & it_map : map) 80 : { 81 68 : const auto pos = it_map.first.find(expected_name); 82 68 : if (pos != std::string::npos) 83 17 : covar_params.set<Real>(param) = it_map.second; 84 : } 85 136 : for (const auto & it_map : vec_map) 86 : { 87 102 : const auto pos = it_map.first.find(expected_name); 88 102 : if (pos != std::string::npos) 89 17 : covar_params.set<std::vector<Real>>(param) = it_map.second; 90 : } 91 : } 92 : 93 34 : _problem->addObject<CovarianceFunctionBase>(type, name, covar_params, /*threaded=*/false); 94 17 : } 95 : 96 34 : InputParameters covar_params = _factory.getValidParams(covar_type); 97 34 : covar_params.set<unsigned int>("num_outputs") = num_outputs; 98 34 : covar_params.set<std::vector<UserObjectName>>("covariance_functions") = dep_covar_names; 99 : 100 34 : const auto param_list = covar_params.getParametersList(); 101 748 : 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 714 : if (covar_params.isParamRequired(param)) 106 : { 107 136 : const std::string expected_name = covar_name + ":" + param; 108 : 109 : const auto & map_it = map.find(expected_name); 110 68 : if (map_it != map.end()) 111 17 : covar_params.set<Real>(param) = map_it->second; 112 : 113 : const auto & vec_map_it = vec_map.find(expected_name); 114 68 : if (vec_map_it != vec_map.end()) 115 17 : covar_params.set<std::vector<Real>>(param) = vec_map_it->second; 116 : } 117 : 118 34 : auto covar_object = _problem->addObject<CovarianceFunctionBase>( 119 68 : covar_type, covar_name, covar_params, /* threaded = */ false); 120 34 : covar_object[0]->loadHyperParamMap(map, vec_map); 121 : 122 34 : model.setupCovariance(covar_name); 123 68 : }