https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ConditionalSampleReporter.C
Go to the documentation of this file.
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
11
13
14template <typename T>
17{
20 params.addClassDescription("Evaluates parsed function to determine if sample needs to be "
21 "evaluated, otherwise data is set to a default value.");
22
23 params.addRequiredCustomTypeParam<std::string>(
24 "function",
25 "FunctionExpression",
26 "function expression that should evaluate to 0 if sample should NOT be evaluated.");
27 params.addRequiredParam<T>("default_value",
28 "Value to replace in data when sample is not evaluated.");
29 params.addParam<std::vector<std::string>>(
30 "sampler_vars", std::vector<std::string>(), "Vector of variables defined by sampler values.");
31 params.addParam<std::vector<unsigned int>>("sampler_var_indices",
32 std::vector<unsigned int>(),
33 "Vector of indices defining the sampler column index "
34 "associated with the variables in 'sampler_vars'.");
35 params.addParam<bool>(
36 "use_time", true, "Make time (t) variable available in the function expression.");
37 return params;
38}
39
40template <typename T>
42 const InputParameters & parameters)
43 : ActiveLearningReporterTempl<T>(parameters),
44 FunctionParserUtils<false>(parameters),
45 _function(this->template getParam<std::string>("function")),
46 _default_value(this->template getParam<T>("default_value")),
47 _sampler_vars(
48 this->template getParam<std::string, unsigned int>("sampler_vars", "sampler_var_indices")),
49 _use_time(this->template getParam<bool>("use_time"))
50{
51 // Gather variables and see if any of the indexes are out-of-bounds
52 std::stringstream vars;
53 std::string sep = "";
54 for (const auto & it : _sampler_vars)
55 {
56 vars << sep << it.first;
57 if (it.second >= this->sampler().getNumberOfCols())
58 this->paramError("sampler_var_indices",
59 "Provided index ",
60 it.second,
61 " must be smaller than the sampler number of columns (",
62 this->sampler().getNumberOfCols(),
63 ").");
64 sep = ",";
65 }
66 if (_use_time)
67 vars << sep << "t";
68
69 // Parse the inputted function
70 _func_F = std::make_shared<SymFunction>();
72 if (_func_F->Parse(_function, vars.str()) >= 0)
73 this->paramError("function", "Invalid function:\n", _function, "\n", _func_F->ErrorMsg());
75 _func_F->Optimize();
76
77 if (_enable_jit)
78 {
79 // let rank 0 do the JIT compilation first
80 if (this->_communicator.rank() != 0)
81 this->_communicator.barrier();
82
83 _func_F->JITCompile();
84
85 // wait for ranks > 0 to catch up
86 if (this->_communicator.rank() == 0)
87 this->_communicator.barrier();
88 }
89
90 _func_params.resize(_sampler_vars.size() + (_use_time ? 1 : 0));
91}
92
93template <typename T>
94bool
96 dof_id_type,
97 dof_id_type,
98 T & val)
99{
100 // Input sampler values
101 for (const auto & p : index_range(_sampler_vars))
102 _func_params[p] = row[_sampler_vars[p].second];
103 // Input time
104 if (_use_time)
105 _func_params[_sampler_vars.size()] = this->_t;
106
107 // If the function is not zero, then a sample is needed
108 if (evaluate(_func_F))
109 return true;
110 // Otherwise we will set the quantitiy of interest to the default value
111 else
112 {
113 val = _default_value;
114 return false;
115 }
116}
117
118// Explicit instantiation (more types can easily be added)
registerMooseObject("StochasticToolsApp", ConditionalSampleReporter)
const Real p
const double T
T evaluate(Real, const Point &)
char ** vars
void ErrorVector unsigned int
This is a base class for performing active learning routines, meant to be used in conjunction with Sa...
This object is mainly meant for demonstration for eventual active learning algorithms,...
SymFunctionPtr _func_F
Parsed function pointer.
virtual bool needSample(const std::vector< Real > &row, dof_id_type local_ind, dof_id_type global_ind, T &val) override
This evaluates the inputted function to determine whether a multiapp solve is necessary/allowed,...
const std::string & _function
User-inputted function string.
const std::vector< std::pair< std::string, unsigned int > > _sampler_vars
Pairs between the variables in the function string and their associated sampler column index.
const bool _use_time
Whether or not the function string includes a time variable.
ConditionalSampleReporterTempl(const InputParameters &parameters)
std::vector< GenericReal< is_ad > > _func_params
void setParserFeatureFlags(SymFunctionPtr &) const
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
void addRequiredCustomTypeParam(const std::string &name, const std::string &custom_type, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
void paramError(const std::string &param, Args... args) const
processor_id_type rank() const
const Parallel::Communicator & _communicator