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 "ConditionalSampleReporter.h" 11 : 12 : registerMooseObject("StochasticToolsApp", ConditionalSampleReporter); 13 : 14 : template <typename T> 15 : InputParameters 16 36 : ConditionalSampleReporterTempl<T>::validParams() 17 : { 18 36 : InputParameters params = ActiveLearningReporterTempl<T>::validParams(); 19 36 : params += FunctionParserUtils<false>::validParams(); 20 36 : params.addClassDescription("Evaluates parsed function to determine if sample needs to be " 21 : "evaluated, otherwise data is set to a default value."); 22 : 23 72 : params.addRequiredCustomTypeParam<std::string>( 24 : "function", 25 : "FunctionExpression", 26 : "function expression that should evaluate to 0 if sample should NOT be evaluated."); 27 72 : params.addRequiredParam<T>("default_value", 28 : "Value to replace in data when sample is not evaluated."); 29 36 : params.addParam<std::vector<std::string>>( 30 36 : "sampler_vars", std::vector<std::string>(), "Vector of variables defined by sampler values."); 31 72 : params.addParam<std::vector<unsigned int>>("sampler_var_indices", 32 36 : std::vector<unsigned int>(), 33 : "Vector of indices defining the sampler column index " 34 : "associated with the variables in 'sampler_vars'."); 35 72 : params.addParam<bool>( 36 72 : "use_time", true, "Make time (t) variable available in the function expression."); 37 36 : return params; 38 0 : } 39 : 40 : template <typename T> 41 16 : ConditionalSampleReporterTempl<T>::ConditionalSampleReporterTempl( 42 : const InputParameters & parameters) 43 : : ActiveLearningReporterTempl<T>(parameters), 44 : FunctionParserUtils<false>(parameters), 45 16 : _function(this->template getParam<std::string>("function")), 46 32 : _default_value(this->template getParam<T>("default_value")), 47 32 : _sampler_vars( 48 : this->template getParam<std::string, unsigned int>("sampler_vars", "sampler_var_indices")), 49 48 : _use_time(this->template getParam<bool>("use_time")) 50 : { 51 : // Gather variables and see if any of the indexes are out-of-bounds 52 16 : std::stringstream vars; 53 16 : std::string sep = ""; 54 48 : for (const auto & it : _sampler_vars) 55 : { 56 : vars << sep << it.first; 57 64 : if (it.second >= this->sampler().getNumberOfCols()) 58 0 : this->paramError("sampler_var_indices", 59 : "Provided index ", 60 0 : it.second, 61 : " must be smaller than the sampler number of columns (", 62 : this->sampler().getNumberOfCols(), 63 : ")."); 64 : sep = ","; 65 : } 66 16 : if (_use_time) 67 16 : vars << sep << "t"; 68 : 69 : // Parse the inputted function 70 16 : _func_F = std::make_shared<SymFunction>(); 71 16 : setParserFeatureFlags(_func_F); 72 32 : if (_func_F->Parse(_function, vars.str()) >= 0) 73 0 : this->paramError("function", "Invalid function:\n", _function, "\n", _func_F->ErrorMsg()); 74 16 : if (!_disable_fpoptimizer) 75 16 : _func_F->Optimize(); 76 : 77 16 : if (_enable_jit) 78 : { 79 : // let rank 0 do the JIT compilation first 80 16 : if (this->_communicator.rank() != 0) 81 6 : this->_communicator.barrier(); 82 : 83 16 : _func_F->JITCompile(); 84 : 85 : // wait for ranks > 0 to catch up 86 16 : if (this->_communicator.rank() == 0) 87 10 : this->_communicator.barrier(); 88 : } 89 : 90 16 : _func_params.resize(_sampler_vars.size() + (_use_time ? 1 : 0)); 91 16 : } 92 : 93 : template <typename T> 94 : bool 95 450 : ConditionalSampleReporterTempl<T>::needSample(const std::vector<Real> & row, 96 : dof_id_type, 97 : dof_id_type, 98 : T & val) 99 : { 100 : // Input sampler values 101 1350 : for (const auto & p : index_range(_sampler_vars)) 102 900 : _func_params[p] = row[_sampler_vars[p].second]; 103 : // Input time 104 450 : if (_use_time) 105 450 : _func_params[_sampler_vars.size()] = this->_t; 106 : 107 : // If the function is not zero, then a sample is needed 108 900 : if (evaluate(_func_F)) 109 : return true; 110 : // Otherwise we will set the quantitiy of interest to the default value 111 : else 112 : { 113 90 : val = _default_value; 114 90 : return false; 115 : } 116 : } 117 : 118 : // Explicit instantiation (more types can easily be added) 119 : template class ConditionalSampleReporterTempl<Real>;