LCOV - code coverage report
Current view: top level - src/vectorpostprocessors - VectorPostprocessor.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 58 63 92.1 %
Date: 2025-07-17 01:28:37 Functions: 9 10 90.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             : // MOOSE includes
      11             : #include "VectorPostprocessor.h"
      12             : #include "SubProblem.h"
      13             : #include "Conversion.h"
      14             : #include "UserObject.h"
      15             : #include "FEProblem.h"
      16             : 
      17             : InputParameters
      18      685983 : VectorPostprocessor::validParams()
      19             : {
      20      685983 :   InputParameters params = UserObject::validParams();
      21      685983 :   params += OutputInterface::validParams();
      22     2057949 :   params.addParam<bool>("contains_complete_history",
      23     1371966 :                         false,
      24             :                         "Set this flag to indicate that the values in all vectors declared by this "
      25             :                         "VPP represent a time history (e.g. with each invocation, new values are "
      26             :                         "added and old values are never removed). This changes the output so that "
      27             :                         "only a single file is output and updated with each invocation");
      28             : 
      29             :   // VPPs can set this to true if their resulting vectors are naturally replicated in parallel
      30             :   // setting this to false will keep MOOSE from unnecessarily broadcasting those vectors
      31      685983 :   params.addPrivateParam<bool>("_auto_broadcast", true);
      32             : 
      33             :   // VPPs can operate in "distributed" mode, which disables the automatic broadcasting
      34             :   // and results in an individual file per processor if CSV output is enabled
      35      685983 :   MooseEnum parallel_type("DISTRIBUTED REPLICATED", "REPLICATED");
      36      685983 :   params.addParam<MooseEnum>(
      37             :       "parallel_type",
      38             :       parallel_type,
      39             :       "Set how the data is represented within the VectorPostprocessor (VPP); 'distributed' "
      40             :       "indicates that data within the VPP is distributed and no auto communication is performed, "
      41             :       "this setting will result in parallel output within the CSV output; 'replicated' indicates "
      42             :       "that the data within the VPP is correct on processor 0, the data will automatically be "
      43             :       "broadcast to all processors unless the '_auto_broadcast' param is set to false within the "
      44             :       "validParams function.");
      45             : 
      46      685983 :   params.addParamNamesToGroup("outputs", "Advanced");
      47      685983 :   params.registerBase("VectorPostprocessor");
      48     1371966 :   return params;
      49      685983 : }
      50             : 
      51        5458 : VectorPostprocessor::VectorPostprocessor(const MooseObject * moose_object)
      52             :   : OutputInterface(moose_object->parameters()),
      53        5458 :     _vpp_name(MooseUtils::shortName(moose_object->parameters().get<std::string>("_object_name"))),
      54        5458 :     _vpp_fe_problem(
      55        5458 :         *moose_object->parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
      56        5458 :     _parallel_type(moose_object->parameters().get<MooseEnum>("parallel_type")),
      57        5458 :     _vpp_moose_object(*moose_object),
      58       10916 :     _vpp_tid(moose_object->parameters().isParamValid("_tid")
      59        5458 :                  ? moose_object->parameters().get<THREAD_ID>("_tid")
      60             :                  : 0),
      61        5458 :     _contains_complete_history(moose_object->parameters().get<bool>("contains_complete_history")),
      62        5458 :     _is_distributed(_parallel_type == "DISTRIBUTED"),
      63       10916 :     _is_broadcast(_is_distributed || !moose_object->parameters().get<bool>("_auto_broadcast"))
      64             : {
      65        5458 : }
      66             : 
      67             : VectorPostprocessorValue &
      68       19078 : VectorPostprocessor::declareVector(const std::string & vector_name)
      69             : {
      70       19078 :   _vector_names.insert(vector_name);
      71             : 
      72       19078 :   if (_vpp_tid)
      73         473 :     return _thread_local_vectors.emplace(vector_name, VectorPostprocessorValue()).first->second;
      74             : 
      75             :   // _is_broadcast = true (_auto_broadcast = false) then data is produced in a replicated manner
      76       18605 :   ReporterMode mode = REPORTER_MODE_ROOT;
      77       18605 :   if (_is_broadcast)
      78        9631 :     mode = REPORTER_MODE_REPLICATED;
      79       18605 :   if (_is_distributed)
      80          81 :     mode = REPORTER_MODE_DISTRIBUTED;
      81             : 
      82       18605 :   return _vpp_fe_problem.getReporterData(ReporterData::WriteKey())
      83             :       .declareReporterValue<VectorPostprocessorValue,
      84       18605 :                             VectorPostprocessorContext<VectorPostprocessorValue>>(
      85       37210 :           VectorPostprocessorReporterName(_vpp_name, vector_name), mode, _vpp_moose_object);
      86       18605 : }
      87             : 
      88             : const std::set<std::string> &
      89          24 : VectorPostprocessor::getVectorNames() const
      90             : {
      91          24 :   return _vector_names;
      92             : }
      93             : 
      94             : const ReporterMode REPORTER_MODE_VPP_SCATTER("VPP_SCATTER");
      95             : 
      96             : template <typename T>
      97       18605 : VectorPostprocessorContext<T>::VectorPostprocessorContext(const libMesh::ParallelObject & other,
      98             :                                                           const MooseObject & producer,
      99             :                                                           ReporterState<T> & state)
     100       18605 :   : ReporterGeneralContext<T>(other, producer, state)
     101             : {
     102       18605 : }
     103             : 
     104             : template <typename T>
     105             : void
     106       56111 : VectorPostprocessorContext<T>::finalize()
     107             : {
     108       56111 :   ReporterGeneralContext<T>::finalize();
     109             : 
     110       56111 :   const auto & consumer_modes = this->state().getConsumers();
     111        8117 :   auto func = [](const std::pair<ReporterMode, const MooseObject *> & mode_pair)
     112        8117 :   { return mode_pair.first == REPORTER_MODE_VPP_SCATTER; };
     113       56111 :   if (std::find_if(consumer_modes.begin(), consumer_modes.end(), func) != consumer_modes.end())
     114             :   {
     115          74 :     const T & value = this->state().value();
     116          74 :     if (this->processor_id() == 0 && value.size() != this->n_processors())
     117           0 :       mooseError("The VectorPostprocessor value to be scatter has a length of ",
     118           0 :                  value.size(),
     119             :                  "; it must be the same length as the number of processors (",
     120           0 :                  this->n_processors(),
     121             :                  ").");
     122             : 
     123          74 :     this->comm().scatter(value, _scatter_value);
     124             :   }
     125       56111 : }
     126             : 
     127             : template <typename T>
     128             : void
     129       35483 : VectorPostprocessorContext<T>::copyValuesBack()
     130             : {
     131       35483 :   ReporterGeneralContext<T>::copyValuesBack();
     132       35483 :   _scatter_value_old = _scatter_value;
     133       35483 : }
     134             : 
     135             : template <typename T>
     136             : const ScatterVectorPostprocessorValue &
     137          94 : VectorPostprocessorContext<T>::getScatterValue() const
     138             : {
     139          94 :   return _scatter_value;
     140             : }
     141             : 
     142             : template <typename T>
     143             : const ScatterVectorPostprocessorValue &
     144           0 : VectorPostprocessorContext<T>::getScatterValueOld() const
     145             : {
     146           0 :   return _scatter_value_old;
     147             : }
     148             : 
     149             : // Explicit instantiation
     150             : template class VectorPostprocessorContext<VectorPostprocessorValue>;

Generated by: LCOV version 1.14