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

Generated by: LCOV version 1.14