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 : #include "ParsedVectorRealReductionReporter.h" 11 : 12 : registerMooseObject("OptimizationApp", ParsedVectorRealReductionReporter); 13 : 14 : InputParameters 15 292 : ParsedVectorRealReductionReporter::validParams() 16 : { 17 292 : InputParameters params = ParsedReporterBase::validParams(); 18 292 : params.addClassDescription( 19 : "Use a parsed function to iterate through a vector and reduce it to a scalar."); 20 584 : params.addRequiredParam<ReporterName>("reporter_name", "Reporter name with vector to reduce."); 21 584 : params.addRequiredParam<Real>("initial_value", "Value to intialize the reduction with."); 22 : 23 : // reporter_symbols are the two symbols for reduction value and current value for the reduction 24 : // operation, these symbols are enforced in the constructor with a mooseError 25 1168 : params.set<std::vector<std::string>>("reporter_symbols") = {"reduction_value", "indexed_value"}; 26 292 : params.suppressParameter<std::vector<std::string>>("reporter_symbols"); 27 : 28 : // This reporter is for postprocessing optimization results and should be exectuted at the end of 29 : // execution 30 292 : params.set<ExecFlagEnum>("execute_on") = EXEC_TIMESTEP_END; 31 292 : return params; 32 584 : } 33 : 34 146 : ParsedVectorRealReductionReporter::ParsedVectorRealReductionReporter( 35 146 : const InputParameters & parameters) 36 : : ParsedReporterBase(parameters), 37 146 : _initial_value(getParam<Real>("initial_value")), 38 146 : _reporter_data( 39 292 : getReporterValueByName<std::vector<Real>>(getParam<ReporterName>("reporter_name"))), 40 584 : _output_reporter(declareValueByName<Real>(getParam<std::string>("name"), REPORTER_MODE_ROOT)) 41 : { 42 : // parse function 43 292 : std::string function = getParam<std::string>("expression"); 44 : // make sure the expression has the two required variables, vi and vplus 45 292 : if (function.find("reduction_value") == std::string::npos || 46 146 : function.find("indexed_value") == std::string::npos) 47 0 : paramError( 48 : "expression", 49 : "Parsed function must contain the two symbols 'reduction_value' and 'indexed_value'."); 50 146 : } 51 : 52 : void 53 722 : ParsedVectorRealReductionReporter::finalize() 54 : { 55 722 : Real reduction = _initial_value; 56 3250 : for (const auto i : index_range(_reporter_data)) 57 : { 58 2528 : _func_params[0] = reduction; 59 2528 : _func_params[1] = _reporter_data[i]; 60 : 61 2528 : if (_use_t) 62 0 : _func_params[2] = _t; 63 : 64 5056 : reduction = evaluate(_func_F); 65 : } 66 722 : _output_reporter = reduction; 67 722 : }