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 686781 : VectorPostprocessor::validParams() 19 : { 20 686781 : InputParameters params = UserObject::validParams(); 21 686781 : params += OutputInterface::validParams(); 22 2060343 : params.addParam<bool>("contains_complete_history", 23 1373562 : 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 686781 : 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 686781 : MooseEnum parallel_type("DISTRIBUTED REPLICATED", "REPLICATED"); 36 686781 : 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 686781 : params.addParamNamesToGroup("outputs", "Advanced"); 47 686781 : params.registerBase("VectorPostprocessor"); 48 1373562 : return params; 49 686781 : } 50 : 51 5856 : VectorPostprocessor::VectorPostprocessor(const MooseObject * moose_object) 52 : : OutputInterface(moose_object->parameters()), 53 5856 : _vpp_name(MooseUtils::shortName(moose_object->parameters().get<std::string>("_object_name"))), 54 5856 : _vpp_fe_problem( 55 5856 : *moose_object->parameters().getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")), 56 5856 : _parallel_type(moose_object->parameters().get<MooseEnum>("parallel_type")), 57 5856 : _vpp_moose_object(*moose_object), 58 11712 : _vpp_tid(moose_object->parameters().isParamValid("_tid") 59 5856 : ? moose_object->parameters().get<THREAD_ID>("_tid") 60 : : 0), 61 5856 : _contains_complete_history(moose_object->parameters().get<bool>("contains_complete_history")), 62 5856 : _is_distributed(_parallel_type == "DISTRIBUTED"), 63 11712 : _is_broadcast(_is_distributed || !moose_object->parameters().get<bool>("_auto_broadcast")) 64 : { 65 5856 : } 66 : 67 : VectorPostprocessorValue & 68 20547 : VectorPostprocessor::declareVector(const std::string & vector_name) 69 : { 70 20547 : _vector_names.insert(vector_name); 71 : 72 20547 : 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 20074 : ReporterMode mode = REPORTER_MODE_ROOT; 77 20074 : if (_is_broadcast) 78 10435 : mode = REPORTER_MODE_REPLICATED; 79 20074 : if (_is_distributed) 80 90 : mode = REPORTER_MODE_DISTRIBUTED; 81 : 82 20074 : return _vpp_fe_problem.getReporterData(ReporterData::WriteKey()) 83 : .declareReporterValue<VectorPostprocessorValue, 84 20074 : VectorPostprocessorContext<VectorPostprocessorValue>>( 85 40148 : VectorPostprocessorReporterName(_vpp_name, vector_name), mode, _vpp_moose_object); 86 20074 : } 87 : 88 : const std::set<std::string> & 89 26 : VectorPostprocessor::getVectorNames() const 90 : { 91 26 : return _vector_names; 92 : } 93 : 94 : const ReporterMode REPORTER_MODE_VPP_SCATTER("VPP_SCATTER"); 95 : 96 : template <typename T> 97 20074 : VectorPostprocessorContext<T>::VectorPostprocessorContext(const libMesh::ParallelObject & other, 98 : const MooseObject & producer, 99 : ReporterState<T> & state) 100 20074 : : ReporterGeneralContext<T>(other, producer, state) 101 : { 102 20074 : } 103 : 104 : template <typename T> 105 : void 106 61447 : VectorPostprocessorContext<T>::finalize() 107 : { 108 61447 : ReporterGeneralContext<T>::finalize(); 109 : 110 61447 : const auto & consumer_modes = this->state().getConsumers(); 111 8796 : auto func = [](const std::pair<ReporterMode, const MooseObject *> & mode_pair) 112 8796 : { return mode_pair.first == REPORTER_MODE_VPP_SCATTER; }; 113 61447 : if (std::find_if(consumer_modes.begin(), consumer_modes.end(), func) != consumer_modes.end()) 114 : { 115 84 : const T & value = this->state().value(); 116 84 : 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 84 : this->comm().scatter(value, _scatter_value); 124 : } 125 61447 : } 126 : 127 : template <typename T> 128 : void 129 38482 : VectorPostprocessorContext<T>::copyValuesBack() 130 : { 131 38482 : ReporterGeneralContext<T>::copyValuesBack(); 132 38482 : _scatter_value_old = _scatter_value; 133 38482 : } 134 : 135 : template <typename T> 136 : const ScatterVectorPostprocessorValue & 137 104 : VectorPostprocessorContext<T>::getScatterValue() const 138 : { 139 104 : 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>;