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",
72 MooseUtils::join(unsupported_types,
", "));
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));
83template <
typename DataType>
88 const auto & data = getReporterValueByName<std::vector<DataType>>(r_name);
94template <
typename DataType>
100 const std::vector<DataType> & data,
101 const bool relative_sensitivity)
105 _relative_sensitivity(relative_sensitivity)
110template <
typename DataType>
114 const dof_id_type offset = _sampler.getLocalRowBegin();
115 const dof_id_type num_columns = _sampler.getNumberOfCols();
122 auto reference_value = _data.empty() ? DataType() : initializeDataType(_data[0]);
123 if (_relative_sensitivity)
126 if (this->processor_id() == 0)
127 reference_value = _data[0];
128 this->comm().broadcast(reference_value);
131 for (
const auto param_i : make_range(num_columns))
134 const auto interval = _relative_sensitivity ? _sampler.getRelativeInterval(param_i)
135 : _sampler.getAbsoluteInterval(param_i);
140 if (_sampler.perturbationMethod() ==
"central_difference")
142 left_i = 2 * param_i + 1;
143 right_i = 2 * param_i + 2;
147 left_i = param_i + 1;
151 const bool left_i_in_owned_range =
152 _sampler.getLocalRowBegin() <= left_i && left_i < _sampler.getLocalRowEnd();
153 const bool right_i_in_owned_range =
154 _sampler.getLocalRowBegin() <= right_i && right_i < _sampler.getLocalRowEnd();
156 if (left_i_in_owned_range || right_i_in_owned_range)
158 const dof_id_type copy_i = left_i_in_owned_range ? left_i - offset : right_i - offset;
159 this->_state.value()[param_i] = initializeDataType(_data[copy_i]);
163 if (left_i_in_owned_range)
164 addSensitivityContribution(
165 this->_state.value()[param_i], _data[left_i - offset], reference_value, interval);
168 if (right_i_in_owned_range)
169 addSensitivityContribution(
170 this->_state.value()[param_i], _data[right_i - offset], reference_value, -interval);
177template <
typename DataType>
181 const DataType & to_add,
182 const DataType & reference_value,
183 const Real interval)
const
186 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
188 add_to += to_add / (_relative_sensitivity ? interval * reference_value : interval);
195 using VectorValueType =
typename DataType::value_type;
197 mooseAssert(add_to.size() == to_add.size(),
"The vectors for summation have different sizes!");
200 if constexpr (std::is_arithmetic<VectorValueType>::value &&
201 !std::is_same<VectorValueType, bool>::value)
203 for (
const auto index : index_range(add_to))
205 to_add[index] / (_relative_sensitivity ? interval * reference_value[index] : interval);
212 using InnerValueType =
typename VectorValueType::value_type;
215 if constexpr (std::is_arithmetic<InnerValueType>::value &&
216 !std::is_same<InnerValueType, bool>::value)
219 for (
auto & inner_index : index_range(add_to))
221 mooseAssert(add_to[inner_index].size() == to_add[inner_index].size(),
222 "The vectors for summation have different sizes!");
223 for (
const auto index : index_range(add_to[inner_index]))
224 add_to[inner_index][index] +=
225 to_add[inner_index][index] /
226 (_relative_sensitivity ? interval * reference_value[inner_index][index] : interval);
231 static_assert(Moose::always_false<DataType>,
232 "Sensitivity coefficient computation is not implemented for the given type!");
236 static_assert(Moose::always_false<DataType>,
237 "Sensitivity coefficient computation is not implemented for the given type!");
240template <
typename DataType>
243 const DataType & example_output)
const
246 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
252 using VectorValueType =
typename DataType::value_type;
255 if constexpr (std::is_arithmetic<VectorValueType>::value &&
256 !std::is_same<VectorValueType, bool>::value)
257 return std::vector<VectorValueType>(example_output.size(), 0);
262 using InnerValueType =
typename VectorValueType::value_type;
265 if constexpr (std::is_arithmetic<InnerValueType>::value &&
266 !std::is_same<InnerValueType, bool>::value)
268 auto return_vector = std::vector<VectorValueType>(example_output.size());
270 for (
auto & inner_index : index_range(example_output))
271 return_vector[inner_index].resize(example_output.size(), 0.0);
272 return return_vector;
276 static_assert(Moose::always_false<DataType>,
277 "Sensitivity coefficient computation is not implemented for the given type!");
280 static_assert(Moose::always_false<DataType>,
281 "Sensitivity coefficient initialization is not implemented for the given type!");
registerMooseObject("StochasticToolsApp", DirectPerturbationReporter)
const ReporterMode REPORTER_MODE_ROOT
Reporter context for computing direct perturbation-based sensitivity coefficients.
virtual void finalize() override
DirectPerturbationSampler & _sampler
Reference to the direct perturbation sampler.
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.
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...
DataType initializeDataType(const DataType &example_output) const
Initialize the sensitivity container, split into a separate function due to the different constructor...
Reporter class for computing and displaying local sensitivity coefficients around a nominal value usi...
DirectPerturbationSampler & _sampler
Direct perturbation sampler.
const bool _relative_sensitivity
If relative sensitivity should be computed.
DirectPerturbationReporter(const InputParameters ¶meters)
static InputParameters validParams()
void declareValueHelper(const ReporterName &r_name)
Helper for adding direct perturbation-based reporter values.
bool _initialized
Whether or not initialize() has been called for reporter value declaration.
virtual void initialize() override final
A class used to generate samples for a direct perturbation analysis.
const VectorPostprocessor & getVectorPostprocessorObjectByName(const std::string &object_name, const THREAD_ID tid=0) const
static InputParameters validParams()
void paramError(const std::string ¶m, Args... args) const
void mooseError(Args &&... args) const
const T & getParam(const std::string &name) const
ReporterState< T > & _state
bool hasReporterValueByName(const ReporterName &reporter_name) const
const std::string & getObjectName() const
const std::string & getValueName() const
T & value(const std::size_t time_index=0)
dof_id_type getNumberOfCols() const
FEProblemBase & _fe_problem
const std::set< std::string > & getVectorNames() const