https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
DirectPerturbationReporterContext< DataType > Class Template Reference

Reporter context for computing direct perturbation-based sensitivity coefficients. More...

#include <DirectPerturbationReporter.h>

Inheritance diagram for DirectPerturbationReporterContext< DataType >:
[legend]

Public Types

enum  AutoOperation
 

Public Member Functions

 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.
 
virtual void finalize () override
 
virtual std::string type () const override
 
virtual void declareClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const override
 
virtual void declareVectorClone (ReporterData &r_data, const ReporterName &r_name, const ReporterMode &mode, const MooseObject &producer) const override
 
virtual void resize (dof_id_type local_size) final
 
virtual void clear () final
 
virtual void vectorSum () final
 
virtual std::string contextType () const override
 
const ReporterNamename () const override final
 
const ReporterState< T > & state () const
 
virtual void transfer (ReporterData &r_data, const ReporterName &r_name, unsigned int time_index=0) const override
 
virtual void transferToVector (ReporterData &r_data, const ReporterName &r_name, dof_id_type index, unsigned int time_index=0) const override
 
virtual void transferFromVector (ReporterData &r_data, const ReporterName &r_name, dof_id_type index, unsigned int time_index=0) const override
 
void init (const ReporterMode &mode)
 
const MooseObjectgetProducer () const
 
const ReporterProducerEnumgetProducerModeEnum () const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Protected Member Functions

void broadcast ()
 
virtual void storeInfo (nlohmann::json &json) const override
 
virtual void store (nlohmann::json &json) const override
 
virtual void copyValuesBack () override
 
virtual bool restoreState () override
 
void requiresConsumerModes (const ReporterStateBase &state, const std::set< ReporterMode > &modes) const
 

Protected Attributes

ReporterState< T > & _state
 
const MooseObject_producer
 
ReporterProducerEnum _producer_enum
 
const Parallel::Communicator & _communicator
 

Private Member Functions

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 operators on vectors and scalars.
 
DataType initializeDataType (const DataType &example_output) const
 Initialize the sensitivity container, split into a separate function due to the different constructors for scalars and vectors.
 

Private Attributes

DirectPerturbationSampler_sampler
 Reference to the direct perturbation sampler.
 
const std::vector< DataType > & _data
 Data used for the statistic calculation.
 
const bool _relative_sensitivity
 If relative sensitivities should be computed.
 

Detailed Description

template<typename DataType>
class DirectPerturbationReporterContext< DataType >

Reporter context for computing direct perturbation-based sensitivity coefficients.

Definition at line 58 of file DirectPerturbationReporter.h.

Constructor & Destructor Documentation

◆ DirectPerturbationReporterContext()

template<typename DataType >
DirectPerturbationReporterContext< DataType >::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.

Parameters
otherA parallel object, usually the MooseApp
producerThe producer object for the reporter
stateA reporter state (a vector of some types in this case)
samplerThe sampler holding information on the direct perturbation paraemters
dataThe data coming back from the executed models

Definition at line 95 of file DirectPerturbationReporter.C.

103 _sampler(sampler),
104 _data(data),
105 _relative_sensitivity(relative_sensitivity)
106{
107 this->_state.value().resize(_sampler.getNumberOfCols());
108}
DirectPerturbationSampler & _sampler
Reference to the direct perturbation sampler.
const bool _relative_sensitivity
If relative sensitivities should be computed.
const std::vector< DataType > & _data
Data used for the statistic calculation.
const ReporterState< T > & state() const
T & value(const std::size_t time_index=0)
dof_id_type getNumberOfCols() const

Member Function Documentation

◆ addSensitivityContribution()

template<typename DataType >
void DirectPerturbationReporterContext< DataType >::addSensitivityContribution ( DataType &  add_to,
const DataType &  to_add,
const DataType &  reference_value,
const Real  interval 
) const
private

Compute direct perturbation sensitivity, split into a separate function due to the different operators on vectors and scalars.

Parameters
add_toThe data structure which will be extended
to_addThe data structure which will be added to the other one
reference_valueThe reference values in case we are computing relative sensitivities
intervalThe interval scaling coefficient vector

Definition at line 178 of file DirectPerturbationReporter.C.

183{
184 // DataType is a numeric type that we can sum (excluding bool)
185 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
186 {
187 add_to += to_add / (_relative_sensitivity ? interval * reference_value : interval);
188 return;
189 }
190 // DataType is a vector type
191 else if constexpr (is_std_vector<DataType>::value)
192 {
193 // It can still be anything in the vector elements, so we will check this later
194 using VectorValueType = typename DataType::value_type;
195
196 mooseAssert(add_to.size() == to_add.size(), "The vectors for summation have different sizes!");
197
198 // Check if the vector elements are of a numeric type
199 if constexpr (std::is_arithmetic<VectorValueType>::value &&
200 !std::is_same<VectorValueType, bool>::value)
201 {
202 for (const auto index : index_range(add_to))
203 add_to[index] +=
204 to_add[index] / (_relative_sensitivity ? interval * reference_value[index] : interval);
205 return;
206 }
207 // Check if the vector elements are also vectors
208 else if constexpr (is_std_vector<VectorValueType>::value)
209 {
210 // This is as deep as we will go for now
211 using InnerValueType = typename VectorValueType::value_type;
212
213 // Check if the inner vector elements are of a numeric type
214 if constexpr (std::is_arithmetic<InnerValueType>::value &&
215 !std::is_same<InnerValueType, bool>::value)
216 {
217 // Iterate over each inner vector in the outer vector
218 for (auto & inner_index : index_range(add_to))
219 {
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);
226 }
227 return;
228 }
229 else
230 static_assert(Moose::always_false<DataType>,
231 "Sensitivity coefficient computation is not implemented for the given type!");
232 }
233 }
234 else
235 static_assert(Moose::always_false<DataType>,
236 "Sensitivity coefficient computation is not implemented for the given type!");
237}
auto index_range(const T &sizable)

◆ finalize()

template<typename DataType >
void DirectPerturbationReporterContext< DataType >::finalize ( )
overridevirtual

Reimplemented from ReporterGeneralContext< std::vector< DataType > >.

Definition at line 112 of file DirectPerturbationReporter.C.

113{
114 const dof_id_type offset = _sampler.getLocalRowBegin();
115 const dof_id_type num_columns = _sampler.getNumberOfCols();
116
117 // If we are computing the relative sensitivity, we will need
118 // the reference value. So the process that has that will communicate it
119 // to everybody. We reuse the initialization function for the reference
120 // value as well
121 auto reference_value = initializeDataType(_data[0]);
123 {
124 if (_sampler.getLocalRowBegin() == 0)
125 reference_value = _data[0];
126
127 this->comm().sum(reference_value);
128 }
129
130 for (const auto param_i : make_range(num_columns))
131 {
132 // If we need relative coefficients we need to normalize the difference
133 const auto interval = _relative_sensitivity ? _sampler.getRelativeInterval(param_i)
134 : _sampler.getAbsoluteInterval(param_i);
135
136 dof_id_type left_i;
137 dof_id_type right_i;
138 // Depending on which method we use, the indices will change
139 if (_sampler.perturbationMethod() == "central_difference")
140 {
141 left_i = 2 * param_i + 1;
142 right_i = 2 * param_i + 2;
143 }
144 else
145 {
146 left_i = param_i + 1;
147 right_i = 0;
148 }
149
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();
154
155 if (left_i_in_owned_range || right_i_in_owned_range)
156 {
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]);
159 }
160
161 // We add the contribution from one side
162 if (left_i_in_owned_range)
164 this->_state.value()[param_i], _data[left_i - offset], reference_value, interval);
165
166 // We add the contribution from the other side
167 if (right_i_in_owned_range)
169 this->_state.value()[param_i], _data[right_i - offset], reference_value, -interval);
170 }
171
172 // We gather the contributions across all processors
173 this->vectorSum();
174}
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...
Real getRelativeInterval(const Real param_index) const
Return the relative perturbation interval for a given index.
const MooseEnum & perturbationMethod() const
Return the requested perturbation method.
Real getAbsoluteInterval(const Real param_index) const
Return the absolute perturbation interval for a given index.
dof_id_type getLocalRowEnd() const
dof_id_type getLocalRowBegin() const
const Parallel::Communicator & comm() const
uint8_t dof_id_type
IntRange< T > make_range(T beg, T end)

◆ initializeDataType()

template<typename DataType >
DataType DirectPerturbationReporterContext< DataType >::initializeDataType ( const DataType &  example_output) const
private

Initialize the sensitivity container, split into a separate function due to the different constructors for scalars and vectors.

Parameters
example_outputA structure which supplies the dimensions for the allocation

Definition at line 241 of file DirectPerturbationReporter.C.

243{
244 // DataType is a numeric type so we just return 0
245 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
246 return 0.0;
247 // DataType is a vector type
248 else if constexpr (is_std_vector<DataType>::value)
249 {
250 // It can still be anything in the vector elements, so we will check this later
251 using VectorValueType = typename DataType::value_type;
252
253 // Check if the vector elements are of a numeric 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);
257 // Check if the vector elements are also vectors
258 else if constexpr (is_std_vector<VectorValueType>::value)
259 {
260 // This is as deep as we will go for now
261 using InnerValueType = typename VectorValueType::value_type;
262
263 // Check if the inner vector elements are of a numeric type
264 if constexpr (std::is_arithmetic<InnerValueType>::value &&
265 !std::is_same<InnerValueType, bool>::value)
266 {
267 auto return_vector = std::vector<VectorValueType>(example_output.size());
268 // Iterate over each inner vector in the outer vector
269 for (auto & inner_index : index_range(example_output))
270 return_vector[inner_index].resize(example_output.size(), 0.0);
271 return return_vector;
272 }
273 }
274 else
275 static_assert(Moose::always_false<DataType>,
276 "Sensitivity coefficient computation is not implemented for the given type!");
277 }
278 else
279 static_assert(Moose::always_false<DataType>,
280 "Sensitivity coefficient initialization is not implemented for the given type!");
281}
virtual void resize(dof_id_type local_size) final

◆ type()

template<typename DataType >
virtual std::string DirectPerturbationReporterContext< DataType >::type ( ) const
inlineoverridevirtual

Reimplemented from ReporterGeneralContext< std::vector< DataType > >.

Definition at line 77 of file DirectPerturbationReporter.h.

78 {
79 return "DirectPerturbationSensitivity<" + MooseUtils::prettyCppType<DataType>() + ">";
80 }

Member Data Documentation

◆ _data

template<typename DataType >
const std::vector<DataType>& DirectPerturbationReporterContext< DataType >::_data
private

Data used for the statistic calculation.

Definition at line 103 of file DirectPerturbationReporter.h.

◆ _relative_sensitivity

template<typename DataType >
const bool DirectPerturbationReporterContext< DataType >::_relative_sensitivity
private

If relative sensitivities should be computed.

Definition at line 106 of file DirectPerturbationReporter.h.

◆ _sampler

template<typename DataType >
DirectPerturbationSampler& DirectPerturbationReporterContext< DataType >::_sampler
private

Reference to the direct perturbation sampler.

Definition at line 100 of file DirectPerturbationReporter.h.

Referenced by DirectPerturbationReporterContext< DataType >::DirectPerturbationReporterContext().


The documentation for this class was generated from the following files: