LCOV - code coverage report
Current view: top level - src/transfers - SamplerReporterTransfer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 67 70 95.7 %
Date: 2026-05-29 20:40:35 Functions: 9 9 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             : // StochasticTools includes
      11             : #include "SamplerReporterTransfer.h"
      12             : #include "SamplerFullSolveMultiApp.h"
      13             : #include "SamplerTransientMultiApp.h"
      14             : #include "SamplerReceiver.h"
      15             : #include "StochasticResults.h"
      16             : #include "Sampler.h"
      17             : #include "StochasticReporter.h"
      18             : #include "Executioner.h"
      19             : 
      20             : registerMooseObject("StochasticToolsApp", SamplerReporterTransfer);
      21             : 
      22             : InputParameters
      23        2544 : SamplerReporterTransfer::validParams()
      24             : {
      25        2544 :   InputParameters params = StochasticToolsTransfer::validParams();
      26        2544 :   params.addClassDescription("Transfers data from Reporters on the sub-application to a "
      27             :                              "StochasticReporter on the main application.");
      28             : 
      29        5088 :   params.addRequiredParam<std::vector<ReporterName>>(
      30             :       "from_reporter", "The name(s) of the Reporter(s) on the sub-app to transfer from.");
      31        5088 :   params.addRequiredParam<std::string>(
      32             :       "stochastic_reporter", "The name of the StochasticReporter object to transfer values to.");
      33             : 
      34        5088 :   params.addParam<std::string>("prefix",
      35             :                                "Use the supplied string as the prefix for reporter "
      36             :                                "name rather than the transfer name.");
      37             : 
      38        2544 :   params.suppressParameter<MultiMooseEnum>("direction");
      39        2544 :   params.suppressParameter<MultiAppName>("multi_app");
      40        2544 :   return params;
      41           0 : }
      42             : 
      43        1264 : SamplerReporterTransfer::SamplerReporterTransfer(const InputParameters & parameters)
      44             :   : StochasticToolsTransfer(parameters),
      45             :     ReporterTransferInterface(this),
      46        2528 :     _sub_reporter_names(getParam<std::vector<ReporterName>>("from_reporter"))
      47             : {
      48        1264 :   if (hasToMultiApp())
      49           0 :     paramError("to_multi_app", "To and between multiapp directions are not implemented");
      50        1264 : }
      51             : 
      52             : void
      53        1262 : SamplerReporterTransfer::initialSetup()
      54             : {
      55             :   // Get the StochasticResults VPP object to populate
      56        2524 :   auto & uo = _fe_problem.getUserObject<UserObject>(getParam<std::string>("stochastic_reporter"));
      57        1262 :   _results = dynamic_cast<StochasticReporter *>(&uo);
      58        1262 :   if (!_results)
      59           0 :     paramError("stochastic_reporter", "This object must be a 'StochasticReporter' object.");
      60             : 
      61        1262 :   intitializeStochasticReporters();
      62        1258 : }
      63             : 
      64             : void
      65        3976 : SamplerReporterTransfer::initializeFromMultiapp()
      66             : {
      67             :   // Initialize vectors so they are the proper size before transfer
      68        3976 :   if (_results->getExecuteOnEnum().contains(EXEC_TRANSFER))
      69         205 :     _results->initialize();
      70        3976 : }
      71             : 
      72             : void
      73        9864 : SamplerReporterTransfer::executeFromMultiapp()
      74             : {
      75       19728 :   if (getFromMultiApp()->isRootProcessor())
      76             :   {
      77        9702 :     const dof_id_type n = getFromMultiApp()->numGlobalApps();
      78      488118 :     for (MooseIndex(n) i = 0; i < n; i++)
      79      478416 :       transferStochasticReporters(_global_index, i);
      80             :   }
      81        9864 : }
      82             : 
      83             : void
      84        3976 : SamplerReporterTransfer::finalizeFromMultiapp()
      85             : {
      86             :   // Execute reporter so that things are gathered after transfer properly
      87        3976 :   _fe_problem.computeUserObjectByName(EXEC_TRANSFER, Moose::PRE_AUX, _results->name());
      88        3976 :   _fe_problem.computeUserObjectByName(EXEC_TRANSFER, Moose::POST_AUX, _results->name());
      89        3976 : }
      90             : 
      91             : void
      92        2131 : SamplerReporterTransfer::execute()
      93             : {
      94        2131 :   initializeFromMultiapp();
      95             : 
      96        9095 :   for (dof_id_type i = _sampler_ptr->getLocalRowBegin(); i < _sampler_ptr->getLocalRowEnd(); ++i)
      97        6964 :     transferStochasticReporters(i, i);
      98             : 
      99        2131 :   finalizeFromMultiapp();
     100        2131 : }
     101             : 
     102             : void
     103        1262 : SamplerReporterTransfer::intitializeStochasticReporters()
     104             : {
     105        1262 :   const dof_id_type n = getFromMultiApp()->numGlobalApps();
     106             : 
     107        3339 :   for (const auto & sub_rname : _sub_reporter_names)
     108       38199 :     for (MooseIndex(n) i = 0; i < n; i++)
     109       72244 :       if (getFromMultiApp()->hasLocalApp(i))
     110        9007 :         addReporterTransferMode(
     111       18014 :             sub_rname, REPORTER_MODE_ROOT, getFromMultiApp()->appProblemBase(i));
     112             : 
     113        3987 :   const std::string prefix = isParamValid("prefix") ? getParam<std::string>("prefix") : name();
     114        3335 :   for (const auto & sub_rname : _sub_reporter_names)
     115        4772 :     for (MooseIndex(n) i = 0; i < n; i++)
     116        9544 :       if (getFromMultiApp()->hasLocalApp(i))
     117             :       {
     118        2077 :         const ReporterData & rdata = getFromMultiApp()->appProblemBase(i).getReporterData();
     119             :         ReporterName rname =
     120        4152 :             _results->declareStochasticReporterClone(*_sampler_ptr, rdata, sub_rname, prefix);
     121             :         if (rname.empty())
     122           2 :           paramError("from_reporter",
     123             :                      "Reporter value ",
     124             :                      sub_rname,
     125             :                      " is of unsupported type ",
     126           2 :                      rdata.getReporterContextBase(sub_rname).type(),
     127             :                      ". Contact MOOSE developers on how to transfer this type of reporter value.");
     128        2073 :         _reporter_names.push_back(rname);
     129             :         break;
     130             :       }
     131             : 
     132        1258 :   _converged = &_results->declareStochasticReporter<bool>(
     133        3573 :       prefix + (prefix.empty() ? "" : ":") + "converged", *_sampler_ptr);
     134        1258 : }
     135             : 
     136             : void
     137      485380 : SamplerReporterTransfer::transferStochasticReporters(dof_id_type global_index,
     138             :                                                      dof_id_type app_index)
     139             : {
     140      970760 :   if (getFromMultiApp()->hasLocalApp(app_index))
     141             :   {
     142       16666 :     const dof_id_type local_index = global_index - _sampler_ptr->getLocalRowBegin();
     143       46237 :     for (unsigned int r = 0; r < _sub_reporter_names.size(); ++r)
     144       29571 :       transferToVectorReporter(_sub_reporter_names[r],
     145             :                                _reporter_names[r],
     146       59142 :                                getFromMultiApp()->appProblemBase(app_index),
     147       59142 :                                getFromMultiApp()->problemBase(),
     148             :                                local_index);
     149             : 
     150       33332 :     (*_converged)[local_index] = getFromMultiApp()->getExecutioner(app_index)->lastSolveConverged();
     151             :   }
     152      485380 : }

Generated by: LCOV version 1.14