LCOV - code coverage report
Current view: top level - src/transfers - SamplerPostprocessorTransfer.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: f45d79 Lines: 74 78 94.9 %
Date: 2025-07-25 05:00:46 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        1572 : getVectorNamesHelper(const std::string & prefix, const std::vector<PostprocessorName> & pp_names)
      22             : {
      23             :   std::vector<VectorPostprocessorName> vec_names;
      24        1572 :   vec_names.reserve(pp_names.size());
      25        3240 :   for (const auto & pp_name : pp_names)
      26             :   {
      27        1668 :     if (!prefix.empty())
      28        3304 :       vec_names.push_back(prefix + ":" + pp_name);
      29             :     else
      30          32 :       vec_names.push_back(pp_name);
      31             :   }
      32        1572 :   return vec_names;
      33           0 : }
      34             : 
      35             : InputParameters
      36        3164 : SamplerPostprocessorTransfer::validParams()
      37             : {
      38        3164 :   InputParameters params = StochasticToolsTransfer::validParams();
      39        3164 :   params.addClassDescription("Transfers data from Postprocessors on the sub-application to a "
      40             :                              "VectorPostprocessor on the master application.");
      41             : 
      42        6328 :   params.addParam<std::vector<PostprocessorName>>(
      43             :       "from_postprocessor", "The name(s) of the Postprocessor(s) on the sub-app to transfer from.");
      44        6328 :   params.addParam<VectorPostprocessorName>("to_vector_postprocessor",
      45             :                                            "The name of the VectorPostprocessor in "
      46             :                                            "the MultiApp to transfer values "
      47             :                                            "to.");
      48             : 
      49        6328 :   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        6328 :   params.addParam<bool>("keep_solve_fail_value",
      54        6328 :                         false,
      55             :                         "If true, whatever the value the sub app has upon exitting is used. "
      56             :                         "If false, NaN will be transferred.");
      57             : 
      58        3164 :   params.suppressParameter<MultiMooseEnum>("direction");
      59        3164 :   params.suppressParameter<MultiAppName>("multi_app");
      60        3164 :   return params;
      61           0 : }
      62             : 
      63        1584 : SamplerPostprocessorTransfer::SamplerPostprocessorTransfer(const InputParameters & parameters)
      64             :   : StochasticToolsTransfer(parameters),
      65        1572 :     _sub_pp_names(getParam<std::vector<PostprocessorName>>("from_postprocessor")),
      66        3144 :     _master_vpp_name(getParam<VectorPostprocessorName>("to_vector_postprocessor")),
      67        1604 :     _vpp_names(isParamValid("prefix")
      68        3144 :                    ? getVectorNamesHelper(getParam<std::string>("prefix"), _sub_pp_names)
      69        1540 :                    : getVectorNamesHelper(_name, _sub_pp_names)),
      70        4728 :     _keep_diverge(getParam<bool>("keep_solve_fail_value"))
      71             : {
      72        1572 :   if (hasToMultiApp())
      73           0 :     paramError("to_multi_app", "To and between multiapp directions are not implemented");
      74        1572 : }
      75             : 
      76             : const std::vector<VectorPostprocessorName> &
      77        1572 : SamplerPostprocessorTransfer::vectorNames() const
      78             : {
      79        1572 :   return _vpp_names;
      80             : }
      81             : 
      82             : void
      83        1488 : SamplerPostprocessorTransfer::initialSetup()
      84             : {
      85             :   // Get the StochasticResults VPP object to populate
      86        1488 :   auto & uo = _fe_problem.getUserObject<UserObject>(_master_vpp_name);
      87        1488 :   _results = dynamic_cast<StochasticResults *>(&uo);
      88        1488 :   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        1488 :   const dof_id_type n = getFromMultiApp()->numGlobalApps();
      93       10782 :   for (MooseIndex(n) i = 0; i < n; i++)
      94             :   {
      95       18596 :     if (getFromMultiApp()->hasLocalApp(i))
      96             :     {
      97        3356 :       FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
      98        6852 :       for (const auto & sub_pp_name : _sub_pp_names)
      99        3500 :         if (!app_problem.hasPostprocessorValueByName(sub_pp_name))
     100           4 :           mooseError("Unknown postprocesssor name '",
     101             :                      sub_pp_name,
     102             :                      "' on sub-application '",
     103           4 :                      getFromMultiApp()->name(),
     104             :                      "'");
     105             :     }
     106             :   }
     107             : 
     108             :   // Initialize storage for accumulating VPP data
     109        1484 :   _current_data.resize(_sub_pp_names.size());
     110        1484 : }
     111             : 
     112             : void
     113        1248 : SamplerPostprocessorTransfer::initializeFromMultiapp()
     114             : {
     115        2560 :   for (VectorPostprocessorValue & current : _current_data)
     116             :   {
     117             :     current.clear();
     118        1312 :     current.reserve(_sampler_ptr->getNumberOfLocalRows());
     119             :   }
     120        1248 : }
     121             : 
     122             : void
     123        3520 : SamplerPostprocessorTransfer::executeFromMultiapp()
     124             : {
     125        7040 :   if (getFromMultiApp()->isRootProcessor())
     126             :   {
     127        3220 :     const dof_id_type n = getFromMultiApp()->numGlobalApps();
     128       26620 :     for (MooseIndex(n) i = 0; i < n; i++)
     129             :     {
     130       46800 :       if (getFromMultiApp()->hasLocalApp(i))
     131             :       {
     132        3220 :         FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
     133        3220 :         if (app_problem.converged(/*nl_sys_num=*/0) || _keep_diverge)
     134        6440 :           for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     135        3320 :             _current_data[j].emplace_back(
     136        3320 :                 app_problem.getPostprocessorValueByName(_sub_pp_names[j]));
     137             :         else
     138         200 :           for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     139         100 :             _current_data[j].emplace_back(std::numeric_limits<double>::quiet_NaN());
     140             :       }
     141             :     }
     142             :   }
     143        3520 : }
     144             : 
     145             : void
     146        1248 : SamplerPostprocessorTransfer::finalizeFromMultiapp()
     147             : {
     148        2560 :   for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     149             :   {
     150        1312 :     _results->setCurrentLocalVectorPostprocessorValue(_vpp_names[j], std::move(_current_data[j]));
     151             :     _current_data[j].clear();
     152             :   }
     153        1248 : }
     154             : 
     155             : void
     156        1240 : SamplerPostprocessorTransfer::execute()
     157             : {
     158        2512 :   for (std::size_t j = 0; j < _sub_pp_names.size(); ++j)
     159             :   {
     160             :     VectorPostprocessorValue current;
     161        1272 :     current.reserve(_sampler_ptr->getNumberOfLocalRows());
     162        6804 :     for (dof_id_type i = _sampler_ptr->getLocalRowBegin(); i < _sampler_ptr->getLocalRowEnd(); ++i)
     163             :     {
     164        4260 :       FEProblemBase & app_problem = getFromMultiApp()->appProblemBase(i);
     165        4260 :       if (app_problem.converged(/*nl_sys_num=*/0) || _keep_diverge)
     166        4080 :         current.emplace_back(app_problem.getPostprocessorValueByName(_sub_pp_names[j]));
     167             :       else
     168         180 :         current.emplace_back(std::numeric_limits<double>::quiet_NaN());
     169             :     }
     170        1272 :     _results->setCurrentLocalVectorPostprocessorValue(_vpp_names[j], std::move(current));
     171             :   }
     172        1240 : }

Generated by: LCOV version 1.14