LCOV - code coverage report
Current view: top level - src/transfers - SamplerReporterTransfer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #33187 (5aa0b2) with base d7c4bd Lines: 67 70 95.7 %
Date: 2026-06-30 12:24:43 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        2558 : SamplerReporterTransfer::validParams()
      24             : {
      25        2558 :   InputParameters params = StochasticToolsTransfer::validParams();
      26        2558 :   params.addClassDescription("Transfers data from Reporters on the sub-application to a "
      27             :                              "StochasticReporter on the main application.");
      28             : 
      29        5116 :   params.addRequiredParam<std::vector<ReporterName>>(
      30             :       "from_reporter", "The name(s) of the Reporter(s) on the sub-app to transfer from.");
      31        5116 :   params.addRequiredParam<std::string>(
      32             :       "stochastic_reporter", "The name of the StochasticReporter object to transfer values to.");
      33             : 
      34        5116 :   params.addParam<std::string>("prefix",
      35             :                                "Use the supplied string as the prefix for reporter "
      36             :                                "name rather than the transfer name.");
      37             : 
      38        2558 :   params.suppressParameter<MultiMooseEnum>("direction");
      39        2558 :   params.suppressParameter<MultiAppName>("multi_app");
      40        2558 :   return params;
      41           0 : }
      42             : 
      43        1271 : SamplerReporterTransfer::SamplerReporterTransfer(const InputParameters & parameters)
      44             :   : StochasticToolsTransfer(parameters),
      45             :     ReporterTransferInterface(this),
      46        2542 :     _sub_reporter_names(getParam<std::vector<ReporterName>>("from_reporter"))
      47             : {
      48        1271 :   if (hasToMultiApp())
      49           0 :     paramError("to_multi_app", "To and between multiapp directions are not implemented");
      50        1271 : }
      51             : 
      52             : void
      53        1269 : SamplerReporterTransfer::initialSetup()
      54             : {
      55             :   // Get the StochasticResults VPP object to populate
      56        2538 :   auto & uo = _fe_problem.getUserObject<UserObject>(getParam<std::string>("stochastic_reporter"));
      57        1269 :   _results = dynamic_cast<StochasticReporter *>(&uo);
      58        1269 :   if (!_results)
      59           0 :     paramError("stochastic_reporter", "This object must be a 'StochasticReporter' object.");
      60             : 
      61        1269 :   intitializeStochasticReporters();
      62        1265 : }
      63             : 
      64             : void
      65        4009 : SamplerReporterTransfer::initializeFromMultiapp()
      66             : {
      67             :   // Initialize vectors so they are the proper size before transfer
      68        4009 :   if (_results->getExecuteOnEnum().contains(EXEC_TRANSFER))
      69         196 :     _results->initialize();
      70        4009 : }
      71             : 
      72             : void
      73        9753 : SamplerReporterTransfer::executeFromMultiapp()
      74             : {
      75       19506 :   if (getFromMultiApp()->isRootProcessor())
      76             :   {
      77        9592 :     const dof_id_type n = getFromMultiApp()->numGlobalApps();
      78      487264 :     for (MooseIndex(n) i = 0; i < n; i++)
      79      477672 :       transferStochasticReporters(_global_index, i);
      80             :   }
      81        9753 : }
      82             : 
      83             : void
      84        4009 : SamplerReporterTransfer::finalizeFromMultiapp()
      85             : {
      86             :   // Execute reporter so that things are gathered after transfer properly
      87        4009 :   _fe_problem.computeUserObjectByName(EXEC_TRANSFER, Moose::PRE_AUX, _results->name());
      88        4009 :   _fe_problem.computeUserObjectByName(EXEC_TRANSFER, Moose::POST_AUX, _results->name());
      89        4009 : }
      90             : 
      91             : void
      92        2186 : SamplerReporterTransfer::execute()
      93             : {
      94        2186 :   initializeFromMultiapp();
      95             : 
      96        9273 :   for (dof_id_type i = _sampler_ptr->getLocalRowBegin(); i < _sampler_ptr->getLocalRowEnd(); ++i)
      97        7087 :     transferStochasticReporters(i, i);
      98             : 
      99        2186 :   finalizeFromMultiapp();
     100        2186 : }
     101             : 
     102             : void
     103        1269 : SamplerReporterTransfer::intitializeStochasticReporters()
     104             : {
     105        1269 :   const dof_id_type n = getFromMultiApp()->numGlobalApps();
     106             : 
     107        3353 :   for (const auto & sub_rname : _sub_reporter_names)
     108       38220 :     for (MooseIndex(n) i = 0; i < n; i++)
     109       72272 :       if (getFromMultiApp()->hasLocalApp(i))
     110        9017 :         addReporterTransferMode(
     111       18034 :             sub_rname, REPORTER_MODE_ROOT, getFromMultiApp()->appProblemBase(i));
     112             : 
     113        4008 :   const std::string prefix = isParamValid("prefix") ? getParam<std::string>("prefix") : name();
     114        3349 :   for (const auto & sub_rname : _sub_reporter_names)
     115        4781 :     for (MooseIndex(n) i = 0; i < n; i++)
     116        9562 :       if (getFromMultiApp()->hasLocalApp(i))
     117             :       {
     118        2084 :         const ReporterData & rdata = getFromMultiApp()->appProblemBase(i).getReporterData();
     119             :         ReporterName rname =
     120        4166 :             _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        2080 :         _reporter_names.push_back(rname);
     129             :         break;
     130             :       }
     131             : 
     132        1265 :   _converged = &_results->declareStochasticReporter<bool>(
     133        3594 :       prefix + (prefix.empty() ? "" : ":") + "converged", *_sampler_ptr);
     134        1265 : }
     135             : 
     136             : void
     137      484759 : SamplerReporterTransfer::transferStochasticReporters(dof_id_type global_index,
     138             :                                                      dof_id_type app_index)
     139             : {
     140      969518 :   if (getFromMultiApp()->hasLocalApp(app_index))
     141             :   {
     142       16679 :     const dof_id_type local_index = global_index - _sampler_ptr->getLocalRowBegin();
     143       46238 :     for (unsigned int r = 0; r < _sub_reporter_names.size(); ++r)
     144       29559 :       transferToVectorReporter(_sub_reporter_names[r],
     145             :                                _reporter_names[r],
     146       59118 :                                getFromMultiApp()->appProblemBase(app_index),
     147       59118 :                                getFromMultiApp()->problemBase(),
     148             :                                local_index);
     149             : 
     150       33358 :     (*_converged)[local_index] = getFromMultiApp()->getExecutioner(app_index)->lastSolveConverged();
     151             :   }
     152      484759 : }

Generated by: LCOV version 1.14