https://mooseframework.inl.gov
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 
12 registerMooseObject("StochasticToolsApp", ConditionalSampleReporter);
13 
14 template <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 
40 template <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 
93 template <typename T>
94 bool
95 ConditionalSampleReporterTempl<T>::needSample(const std::vector<Real> & row,
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)
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...
void addParam(const std::string &name, const std::initializer_list< typename T::value_type > &value, const std::string &doc_string)
char ** vars
processor_id_type rank() const
const Parallel::Communicator & _communicator
void addRequiredParam(const std::string &name, const std::string &doc_string)
This object is mainly meant for demonstration for eventual active learning algorithms, but could prove useful.
ConditionalSampleReporterTempl(const InputParameters &parameters)
void paramError(const std::string &param, Args... args) const
SymFunctionPtr _func_F
Parsed function pointer.
registerMooseObject("StochasticToolsApp", ConditionalSampleReporter)
const std::string & _function
User-inputted function string.
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, otherwise it replaces the "transferred" quantity with a default value.
void evaluate(const std::vector< Real > &q_vector, const DenseMatrix< Real > &data, std::vector< Real > &out)
static InputParameters validParams()
std::vector< GenericReal< is_ad > > _func_params
void addRequiredCustomTypeParam(const std::string &name, const std::string &custom_type, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
static InputParameters validParams()
void ErrorVector unsigned int
auto index_range(const T &sizable)
This is a base class for performing active learning routines, meant to be used in conjunction with Sa...
void setParserFeatureFlags(SymFunctionPtr &) const
uint8_t dof_id_type
const bool _use_time
Whether or not the function string includes a time variable.