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 "CovarianceFunctionBase.h" 11 : 12 : InputParameters 13 934 : CovarianceFunctionBase::validParams() 14 : { 15 934 : InputParameters params = MooseObject::validParams(); 16 1868 : params.addParam<std::vector<UserObjectName>>( 17 : "covariance_functions", {}, "Covariance functions that this covariance function depends on."); 18 1868 : params.addParam<unsigned int>( 19 1868 : "num_outputs", 1, "The number of outputs expected for this covariance function."); 20 934 : params.addClassDescription("Base class for covariance functions"); 21 934 : params.registerBase("CovarianceFunctionBase"); 22 934 : params.registerSystemAttributeName("CovarianceFunction"); 23 934 : return params; 24 0 : } 25 : 26 467 : CovarianceFunctionBase::CovarianceFunctionBase(const InputParameters & parameters) 27 : : MooseObject(parameters), 28 : CovarianceInterface(parameters), 29 934 : _num_outputs(getParam<unsigned int>("num_outputs")), 30 1868 : _dependent_covariance_names(getParam<std::vector<UserObjectName>>("covariance_functions")) 31 : 32 : { 33 : // Fetch the dependent covariance functions 34 518 : for (const auto & name : _dependent_covariance_names) 35 : { 36 51 : _covariance_functions.push_back(getCovarianceFunctionByName(name)); 37 51 : _dependent_covariance_types.push_back(_covariance_functions.back()->type()); 38 : } 39 467 : } 40 : 41 : bool 42 0 : CovarianceFunctionBase::computedKdhyper(RealEigenMatrix & /*dKdhp*/, 43 : const RealEigenMatrix & /*x*/, 44 : const std::string & /*hyper_param_name*/, 45 : unsigned int /*ind*/) const 46 : { 47 0 : mooseError("Hyperparameter tuning not set up for this covariance function. Please define " 48 : "computedKdhyper() to compute gradient."); 49 : } 50 : 51 : const Real & 52 917 : CovarianceFunctionBase::addRealHyperParameter(const std::string & name, 53 : const Real value, 54 : const bool is_tunable) 55 : { 56 917 : const auto prefixed_name = _name + ":" + name; 57 917 : if (is_tunable) 58 : _tunable_hp.insert(prefixed_name); 59 917 : return _hp_map_real.emplace(prefixed_name, value).first->second; 60 : } 61 : 62 : const std::vector<Real> & 63 518 : CovarianceFunctionBase::addVectorRealHyperParameter(const std::string & name, 64 : const std::vector<Real> value, 65 : const bool is_tunable) 66 : { 67 518 : const auto prefixed_name = _name + ":" + name; 68 518 : if (is_tunable) 69 : _tunable_hp.insert(prefixed_name); 70 518 : return _hp_map_vector_real.emplace(prefixed_name, value).first->second; 71 : } 72 : 73 : bool 74 586 : CovarianceFunctionBase::isTunable(const std::string & name) const 75 : { 76 : // First, we check if the dependent covariances have the parameter 77 620 : for (const auto dependent_covar : _covariance_functions) 78 68 : if (dependent_covar->isTunable(name)) 79 : return true; 80 : 81 552 : if (_tunable_hp.find(name) != _tunable_hp.end()) 82 : return true; 83 68 : else if (_hp_map_real.find(name) != _hp_map_real.end() || 84 : _hp_map_vector_real.find(name) != _hp_map_vector_real.end()) 85 0 : mooseError("We found hyperparameter ", name, " but it was not declared tunable!"); 86 : 87 : return false; 88 : } 89 : 90 : void 91 1095051 : CovarianceFunctionBase::loadHyperParamMap( 92 : const std::unordered_map<std::string, Real> & map, 93 : const std::unordered_map<std::string, std::vector<Real>> & vec_map) 94 : { 95 : // First, load the hyperparameters of the dependent covariance functions 96 1112068 : for (const auto dependent_covar : _covariance_functions) 97 17017 : dependent_covar->loadHyperParamMap(map, vec_map); 98 : 99 : // Then we load the hyperparameters of this object 100 3302119 : for (auto & iter : _hp_map_real) 101 : { 102 2207068 : const auto & map_iter = map.find(iter.first); 103 2207068 : if (map_iter != map.end()) 104 2207068 : iter.second = map_iter->second; 105 : } 106 2207119 : for (auto & iter : _hp_map_vector_real) 107 : { 108 1112068 : const auto & map_iter = vec_map.find(iter.first); 109 1112068 : if (map_iter != vec_map.end()) 110 1112068 : iter.second = map_iter->second; 111 : } 112 1095051 : } 113 : 114 : void 115 1143 : CovarianceFunctionBase::buildHyperParamMap( 116 : std::unordered_map<std::string, Real> & map, 117 : std::unordered_map<std::string, std::vector<Real>> & vec_map) const 118 : { 119 : // First, add the hyperparameters of the dependent covariance functions 120 1194 : for (const auto dependent_covar : _covariance_functions) 121 51 : dependent_covar->buildHyperParamMap(map, vec_map); 122 : 123 : // At the end we just append the hyperparameters this object owns 124 3463 : for (const auto & iter : _hp_map_real) 125 2320 : map[iter.first] = iter.second; 126 2337 : for (const auto & iter : _hp_map_vector_real) 127 1194 : vec_map[iter.first] = iter.second; 128 1143 : } 129 : 130 : bool 131 586 : CovarianceFunctionBase::getTuningData(const std::string & name, 132 : unsigned int & size, 133 : Real & min, 134 : Real & max) const 135 : { 136 : // First, check the dependent covariances 137 620 : for (const auto dependent_covar : _covariance_functions) 138 68 : if (dependent_covar->getTuningData(name, size, min, max)) 139 : return true; 140 : 141 552 : min = 1e-9; 142 552 : max = 1e9; 143 : 144 552 : if (_hp_map_real.find(name) != _hp_map_real.end()) 145 : { 146 242 : size = 1; 147 242 : return true; 148 : } 149 310 : else if (_hp_map_vector_real.find(name) != _hp_map_vector_real.end()) 150 : { 151 : const auto & vector_value = _hp_map_vector_real.find(name); 152 276 : size = vector_value->second.size(); 153 : return true; 154 : } 155 : else 156 : { 157 34 : size = 0; 158 34 : return false; 159 : } 160 : } 161 : 162 : void 163 463 : CovarianceFunctionBase::dependentCovarianceTypes( 164 : std::map<UserObjectName, std::string> & name_type_map) const 165 : { 166 514 : for (const auto dependent_covar : _covariance_functions) 167 : { 168 51 : dependent_covar->dependentCovarianceTypes(name_type_map); 169 51 : name_type_map.insert(std::make_pair(dependent_covar->name(), dependent_covar->type())); 170 : } 171 463 : }