19 params.
addClassDescription(
"Compute local sensitivities using the direct perturbation method.");
22 "Direct Perturbation sampler used to generate samples.");
23 params.
addParam<std::vector<VectorPostprocessorName>>(
24 "vectorpostprocessors",
26 "List of VectorPostprocessor(s) to utilize for sensitivity computations.");
27 params.
addParam<std::vector<ReporterName>>(
28 "reporters", {},
"List of Reporter values to utilize for sensitivity computations.");
30 params.
addParam<
bool>(
"relative_sensitivity",
32 "If the reporter should return relative or absolute sensitivities.");
40 _relative_sensitivity(getParam<bool>(
"relative_sensitivity")),
43 if (
getParam<std::vector<ReporterName>>(
"reporters").empty() &&
44 getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors").empty())
46 "The 'vectorpostprocessors' and/or 'reporters' parameters must be defined and non-empty.");
57 std::vector<std::string> unsupported_types;
58 for (
const auto & r_name :
getParam<std::vector<ReporterName>>(
"reporters"))
61 declareValueHelper<Real>(r_name);
65 unsupported_types.push_back(r_name.getCombinedName());
68 if (!unsupported_types.empty())
70 "The following reporter value(s) do not have a type supported by the " 71 "DirectPerturbationReporter:\n",
75 for (
const auto & vpp_name :
76 getParam<std::vector<VectorPostprocessorName>>(
"vectorpostprocessors"))
77 for (
const auto & vec_name :
79 declareValueHelper<Real>(
ReporterName(vpp_name, vec_name));
83 template <
typename DataType>
88 const auto & data = getReporterValueByName<std::vector<DataType>>(r_name);
94 template <
typename DataType>
100 const std::vector<DataType> & data,
101 const bool relative_sensitivity)
105 _relative_sensitivity(relative_sensitivity)
110 template <
typename DataType>
114 const dof_id_type offset = _sampler.getLocalRowBegin();
115 const dof_id_type num_columns = _sampler.getNumberOfCols();
121 auto reference_value = initializeDataType(_data[0]);
122 if (_relative_sensitivity)
124 if (_sampler.getLocalRowBegin() == 0)
125 reference_value = _data[0];
127 this->comm().sum(reference_value);
130 for (
const auto param_i :
make_range(num_columns))
133 const auto interval = _relative_sensitivity ? _sampler.getRelativeInterval(param_i)
134 : _sampler.getAbsoluteInterval(param_i);
139 if (_sampler.perturbationMethod() ==
"central_difference")
141 left_i = 2 * param_i + 1;
142 right_i = 2 * param_i + 2;
146 left_i = param_i + 1;
150 const bool left_i_in_owned_range =
151 _sampler.getLocalRowBegin() <= left_i && left_i < _sampler.getLocalRowEnd();
152 const bool right_i_in_owned_range =
153 _sampler.getLocalRowBegin() <= right_i && right_i < _sampler.getLocalRowEnd();
155 if (left_i_in_owned_range || right_i_in_owned_range)
157 const dof_id_type copy_i = left_i_in_owned_range ? left_i - offset : right_i - offset;
158 this->_state.value()[param_i] = initializeDataType(_data[copy_i]);
162 if (left_i_in_owned_range)
163 addSensitivityContribution(
164 this->_state.value()[param_i], _data[left_i - offset], reference_value, interval);
167 if (right_i_in_owned_range)
168 addSensitivityContribution(
169 this->_state.value()[param_i], _data[right_i - offset], reference_value, -interval);
176 template <
typename DataType>
180 const DataType & to_add,
181 const DataType & reference_value,
182 const Real interval)
const 185 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
187 add_to += to_add / (_relative_sensitivity ? interval * reference_value : interval);
194 using VectorValueType =
typename DataType::value_type;
196 mooseAssert(add_to.size() == to_add.size(),
"The vectors for summation have different sizes!");
199 if constexpr (std::is_arithmetic<VectorValueType>::value &&
200 !std::is_same<VectorValueType, bool>::value)
204 to_add[index] / (_relative_sensitivity ? interval * reference_value[index] : interval);
211 using InnerValueType =
typename VectorValueType::value_type;
214 if constexpr (std::is_arithmetic<InnerValueType>::value &&
215 !std::is_same<InnerValueType, bool>::value)
220 mooseAssert(add_to[inner_index].size() == to_add[inner_index].size(),
221 "The vectors for summation have different sizes!");
222 for (
const auto index :
index_range(add_to[inner_index]))
223 add_to[inner_index][index] +=
224 to_add[inner_index][index] /
225 (_relative_sensitivity ? interval * reference_value[inner_index][index] : interval);
230 static_assert(Moose::always_false<DataType>,
231 "Sensitivity coefficient computation is not implemented for the given type!");
235 static_assert(Moose::always_false<DataType>,
236 "Sensitivity coefficient computation is not implemented for the given type!");
239 template <
typename DataType>
242 const DataType & example_output)
const 245 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
251 using VectorValueType =
typename DataType::value_type;
254 if constexpr (std::is_arithmetic<VectorValueType>::value &&
255 !std::is_same<VectorValueType, bool>::value)
256 return std::vector<VectorValueType>(example_output.size(), 0);
261 using InnerValueType =
typename VectorValueType::value_type;
264 if constexpr (std::is_arithmetic<InnerValueType>::value &&
265 !std::is_same<InnerValueType, bool>::value)
267 auto return_vector = std::vector<VectorValueType>(example_output.size());
269 for (
auto & inner_index :
index_range(example_output))
270 return_vector[inner_index].resize(example_output.size(), 0.0);
271 return return_vector;
275 static_assert(Moose::always_false<DataType>,
276 "Sensitivity coefficient computation is not implemented for the given type!");
279 static_assert(Moose::always_false<DataType>,
280 "Sensitivity coefficient initialization is not implemented for the given type!");
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
virtual void finalize() override
A class used to generate samples for a direct perturbation analysis.
registerMooseObject("StochasticToolsApp", DirectPerturbationReporter)
const ReporterMode REPORTER_MODE_ROOT
Reporter class for computing and displaying local sensitivity coefficients around a nominal value usi...
ReporterState< std::vector< DataType > > & _state
static InputParameters validParams()
void declareValueHelper(const ReporterName &r_name)
Helper for adding direct perturbation-based reporter values.
static InputParameters validParams()
const bool _relative_sensitivity
If relative sensitivity should be computed.
DirectPerturbationReporterContext(const libMesh::ParallelObject &other, const MooseObject &producer, ReporterState< std::vector< DataType >> &state, DirectPerturbationSampler &sampler, const std::vector< DataType > &data, const bool relative_sensitivity)
Constructor.
DataType initializeDataType(const DataType &example_output) const
Initialize the sensitivity container, split into a separate function due to the different constructor...
const T & getParam(const std::string &name) const
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
void paramError(const std::string ¶m, Args... args) const
const std::string & getObjectName() const
Reporter context for computing direct perturbation-based sensitivity coefficients.
DirectPerturbationSampler & _sampler
Reference to the direct perturbation sampler.
void addSensitivityContribution(DataType &add_to, const DataType &to_add, const DataType &reference_value, const Real interval) const
Compute direct perturbation sensitivity, split into a separate function due to the different operator...
FEProblemBase & _fe_problem
IntRange< T > make_range(T beg, T end)
void mooseError(Args &&... args) const
T & value(const std::size_t time_index=0)
DirectPerturbationReporter(const InputParameters ¶meters)
const std::string & getValueName() const
DirectPerturbationSampler & _sampler
Direct perturbation sampler.
virtual void initialize() override final
const std::set< std::string > & getVectorNames() const
auto index_range(const T &sizable)
dof_id_type getNumberOfCols() const
bool hasReporterValueByName(const ReporterName &reporter_name) const
bool _initialized
Whether or not initialize() has been called for reporter value declaration.