LCOV - code coverage report
Current view: top level - src/transfers - SamplerPostprocessorTransfer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 76 80 95.0 %
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 "SamplerPostprocessorTransfer.h"
      12             : #include "SamplerFullSolveMultiApp.h"
      13             : #include "SamplerTransientMultiApp.h"
      14             : #include "SamplerReceiver.h"
      15             : #include "StochasticResults.h"
      16             : #include "Sampler.h"
      17             : 
      18             : registerMooseObject("StochasticToolsApp", SamplerPostprocessorTransfer);
      19             : 
      20             : std::vector<VectorPostprocessorName>
      21         718 : getVectorNamesHelper(const std::string & prefix, const std::vector<PostprocessorName> & pp_names)
      22             : {
      23             :   std::vector<VectorPostprocessorName> vec_names;
      24         718 :   vec_names.reserve(pp_names.size());
      25        1478 :   for (const auto & pp_name : pp_names)
      26             :   {
      27         760 :     if (!prefix.empty())
      28        1506 :       vec_names.push_back(prefix + ":" + pp_name);
      29             :     else
      30          14 :       vec_names.push_back(pp_name);
      31             :   }
      32         718 :   return vec_names;
      33           0 : }
      34             : 
      35             : InputParameters
      36        1446 : SamplerPostprocessorTransfer::validParams()
      37             : {
      38        1446 :   InputParameters params = StochasticToolsTransfer::validParams();
      39        1446 :   params.addClassDescription("Transfers data from Postprocessors on the sub-application to a "
      40             :                              "VectorPostprocessor on the master application.");
      41             : 
      42        2892 :   params.addParam<std::vector<PostprocessorName>>(
      43             :       "from_postprocessor", "The name(s) of the Postprocessor(s) on the sub-app to transfer from.");
      44        2892 :   params.addParam<VectorPostprocessorName>("to_vector_postprocessor",
      45             :                                            "The name of the VectorPostprocessor in "
      46             :                                            "the MultiApp to transfer values "
      47             :                                            "to.");
      48             : 
      49        2892 :   params.addParam<std::string>("prefix",
      50             :                                "Use the supplied string as the prefix for vector postprocessor "
      51             :                                "name rather than the transfer name.");
      52             : 
      53        2892 :   params.addParam<bool>("keep_solve_fail_value",
      54        2892 :                         false,
      55             :                         "If true, whatever the value the sub app has upon exitting is used. "
      56             :                         "If false, NaN will be transferred.");
      57             : 
      58        1446 :   params.suppressParameter<MultiMooseEnum>("direction");
      59        1446 :   params.suppressParameter<MultiAppName>("multi_app");
      60        1446 :   return params;
      61           0 : }
      62             : 
      63         724 : SamplerPostprocessorTransfer::SamplerPostprocessorTransfer(const InputParameters & parameters)
      64             :   : StochasticToolsTransfer(parameters),
      65         718 :     _sub_pp_names(getParam<std::vector<PostprocessorName>>("from_postprocessor")),
      66        1436 :     _master_vpp_name(getParam<VectorPostprocessorName>("to_vector_postprocessor")),
      67        2154 :     _vpp_names(isParamValid("prefix")
      68        1436 :                    ? getVectorNamesHelper(getParam<std::string>("prefix"), _sub_pp_names)
      69             :                    : getVectorNamesHelper(_name, _sub_pp_names)),
      70        2160 :     _keep_diverge(getParam<bool>("keep_solve_fail_value"))
      71             : {
      72         718 :   if (hasToMultiApp())
      73           0 :     paramError("to_multi_app", "To and between multiapp directions are not implemented");
      74         718 : }
      75             : 
      76             : const std::vector<VectorPostprocessorName> &
      77         718 : SamplerPostprocessorTransfer::vectorNames() const
      78             : {
      79         718 :   return _vpp_names;
      80             : }
      81             : 
      82             : void
      83         678 : SamplerPostprocessorTransfer::initialSetup()
      84             : {
      85             :   // Get the StochasticResults VPP object to populate
      86         678 :   auto & uo = _fe_problem.getUserObject<UserObject>(_master_vpp_name);
      87         678 :   _results = dynamic_cast<StochasticResults *>(&uo);
      88         678 :   if (!_results)
      89           0 :     mooseError("The 'results' object must be a 'StochasticResults' object.");
      90             : 
      91             :   // Check that postprocessor on sub-application exists and create vectors on results VPP
      92         678 :   const dof_id_type n = getFromMultiApp()->numGlobalApps();
      93        4892 :   for (MooseIndex(n) i = 0; i < n; i++)
      94             :   {
      95        8432 :     if (getFromMultiApp()->hasLocalApp(i))
      96             :     {
      97        1604 :       FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
      98        3274 :       for (const auto & sub_pp_name : _sub_pp_names)
      99        1672 :         if (!app_problem.hasPostprocessorValueByName(sub_pp_name))
     100           2 :           mooseError("Unknown postprocesssor name '",
     101             :                      sub_pp_name,
     102             :                      "' on sub-application '",
     103           2 :                      getFromMultiApp()->name(),
     104             :                      "'");
     105             :     }
     106             :   }
     107             : 
     108             :   // Initialize storage for accumulating VPP data
     109         676 :   _current_data.resize(_sub_pp_names.size());
     110         676 : }
     111             : 
     112             : void
     113         572 : SamplerPostprocessorTransfer::initializeFromMultiapp()
     114             : {
     115        1172 :   for (VectorPostprocessorValue & current : _current_data)
     116             :   {
     117         600 :     current.clear();
     118         600 :     current.reserve(_sampler_ptr->getNumberOfLocalRows());
     119             :   }
     120         572 : }
     121             : 
     122             : void
     123        1694 : SamplerPostprocessorTransfer::executeFromMultiapp()
     124             : {
     125        3388 :   if (getFromMultiApp()->isRootProcessor())
     126             :   {
     127        1544 :     const dof_id_type n = getFromMultiApp()->numGlobalApps();
     128       12512 :     for (MooseIndex(n) i = 0; i < n; i++)
     129             :     {
     130       21936 :       if (getFromMultiApp()->hasLocalApp(i))
     131             :       {
     132        1544 :         FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
     133        1544 :         if (app_problem.converged(/*nl_sys_num=*/0) || _keep_diverge)
     134        3088 :           for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     135        1594 :             _current_data[j].emplace_back(
     136        1594 :                 app_problem.getPostprocessorValueByName(_sub_pp_names[j]));
     137             :         else
     138         100 :           for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     139          50 :             _current_data[j].emplace_back(std::numeric_limits<double>::quiet_NaN());
     140             :       }
     141             :     }
     142             :   }
     143        1694 : }
     144             : 
     145             : void
     146         572 : SamplerPostprocessorTransfer::finalizeFromMultiapp()
     147             : {
     148        1172 :   for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     149             :   {
     150         600 :     _results->setCurrentLocalVectorPostprocessorValue(_vpp_names[j], std::move(_current_data[j]));
     151         600 :     _current_data[j].clear();
     152             :   }
     153         572 : }
     154             : 
     155             : void
     156         568 : SamplerPostprocessorTransfer::execute()
     157             : {
     158        1150 :   for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     159             :   {
     160             :     VectorPostprocessorValue current;
     161         582 :     current.reserve(_sampler_ptr->getNumberOfLocalRows());
     162        3256 :     for (dof_id_type i = _sampler_ptr->getLocalRowBegin(); i < _sampler_ptr->getLocalRowEnd(); ++i)
     163             :     {
     164        2092 :       FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
     165        2092 :       if (app_problem.converged(/*nl_sys_num=*/0) || _keep_diverge)
     166        2002 :         current.emplace_back(app_problem.getPostprocessorValueByName(_sub_pp_names[j]));
     167             :       else
     168          90 :         current.emplace_back(std::numeric_limits<double>::quiet_NaN());
     169             :     }
     170         582 :     _results->setCurrentLocalVectorPostprocessorValue(_vpp_names[j], std::move(current));
     171         582 :   }
     172         568 : }

Generated by: LCOV version 1.14