LCOV - code coverage report
Current view: top level - src/actions - LoadCovarianceDataAction.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 48 49 98.0 %
Date: 2025-07-25 05:00:46 Functions: 4 4 100.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 "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       39276 : LoadCovarianceDataAction::validParams()
      19             : {
      20       39276 :   InputParameters params = Action::validParams();
      21       39276 :   params.addClassDescription("Calls load method on SurrogateModel objects contained within the "
      22             :                              "`[Surrogates]` input block, if a filename is given.");
      23       39276 :   return params;
      24           0 : }
      25             : 
      26       39276 : LoadCovarianceDataAction::LoadCovarianceDataAction(const InputParameters & params) : Action(params)
      27             : {
      28       39276 : }
      29             : 
      30             : void
      31       39192 : LoadCovarianceDataAction::act()
      32             : {
      33             :   std::vector<SurrogateModel *> objects;
      34       78384 :   _app.theWarehouse().query().condition<AttribSystem>("SurrogateModel").queryInto(objects);
      35       40630 :   for (auto model_ptr : objects)
      36             :   {
      37        1438 :     auto * gp_gen = dynamic_cast<GaussianProcessSurrogate *>(model_ptr);
      38        2178 :     if (gp_gen && model_ptr->isParamValid("filename"))
      39          32 :       load(*gp_gen);
      40             :   }
      41       39192 : }
      42             : 
      43             : void
      44          32 : 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          32 :   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          48 :   for (const auto & it : dep_covar_types)
      66             :   {
      67             :     const auto & name = it.first;
      68          16 :     const auto & type = it.second;
      69          16 :     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          16 :     const auto param_list = covar_params.getParametersList();
      75         368 :     for (const auto & param : param_list)
      76         352 :       if (covar_params.isParamRequired(param))
      77             :       {
      78          64 :         const std::string expected_name = name + ":" + param;
      79          96 :         for (const auto & it_map : map)
      80             :         {
      81          64 :           const auto pos = it_map.first.find(expected_name);
      82          64 :           if (pos != std::string::npos)
      83          16 :             covar_params.set<Real>(param) = it_map.second;
      84             :         }
      85         128 :         for (const auto & it_map : vec_map)
      86             :         {
      87          96 :           const auto pos = it_map.first.find(expected_name);
      88          96 :           if (pos != std::string::npos)
      89          16 :             covar_params.set<std::vector<Real>>(param) = it_map.second;
      90             :         }
      91             :       }
      92             : 
      93          32 :     _problem->addObject<CovarianceFunctionBase>(type, name, covar_params, /*threaded=*/false);
      94          16 :   }
      95             : 
      96          32 :   InputParameters covar_params = _factory.getValidParams(covar_type);
      97          32 :   covar_params.set<unsigned int>("num_outputs") = num_outputs;
      98          32 :   covar_params.set<std::vector<UserObjectName>>("covariance_functions") = dep_covar_names;
      99             : 
     100          32 :   const auto param_list = covar_params.getParametersList();
     101         704 :   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         672 :     if (covar_params.isParamRequired(param))
     106             :     {
     107         128 :       const std::string expected_name = covar_name + ":" + param;
     108             : 
     109             :       const auto & map_it = map.find(expected_name);
     110          64 :       if (map_it != map.end())
     111          16 :         covar_params.set<Real>(param) = map_it->second;
     112             : 
     113             :       const auto & vec_map_it = vec_map.find(expected_name);
     114          64 :       if (vec_map_it != vec_map.end())
     115          16 :         covar_params.set<std::vector<Real>>(param) = vec_map_it->second;
     116             :     }
     117             : 
     118          32 :   auto covar_object = _problem->addObject<CovarianceFunctionBase>(
     119          64 :       covar_type, covar_name, covar_params, /* threaded = */ false);
     120          32 :   covar_object[0]->loadHyperParamMap(map, vec_map);
     121             : 
     122          32 :   model.setupCovariance(covar_name);
     123          64 : }

Generated by: LCOV version 1.14