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 ReporterName & name () 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
 
virtual void broadcast (processor_id_type root=0) override
 
void init (const ReporterMode &mode)
 
const MooseObject & getProducer () const
 
const ReporterProducerEnum & getProducerModeEnum () const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Protected Member Functions

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 179 of file DirectPerturbationReporter.C.

184{
185 // DataType is a numeric type that we can sum (excluding bool)
186 if constexpr (std::is_arithmetic<DataType>::value && !std::is_same<DataType, bool>::value)
187 {
188 add_to += to_add / (_relative_sensitivity ? interval * reference_value : interval);
189 return;
190 }
191 // DataType is a vector type
192 else if constexpr (is_std_vector<DataType>::value)
193 {
194 // It can still be anything in the vector elements, so we will check this later
195 using VectorValueType = typename DataType::value_type;
196
197 mooseAssert(add_to.size() == to_add.size(), "The vectors for summation have different sizes!");
198
199 // Check if the vector elements are of a numeric type
200 if constexpr (std::is_arithmetic<VectorValueType>::value &&
201 !std::is_same<VectorValueType, bool>::value)
202 {
203 for (const auto index : index_range(add_to))
204 add_to[index] +=
205 to_add[index] / (_relative_sensitivity ? interval * reference_value[index] : interval);
206 return;
207 }
208 // Check if the vector elements are also vectors
209 else if constexpr (is_std_vector<VectorValueType>::value)
210 {
211 // This is as deep as we will go for now
212 using InnerValueType = typename VectorValueType::value_type;
213
214 // Check if the inner vector elements are of a numeric type
215 if constexpr (std::is_arithmetic<InnerValueType>::value &&
216 !std::is_same<InnerValueType, bool>::value)
217 {
218 // Iterate over each inner vector in the outer vector
219 for (auto & inner_index : index_range(add_to))
220 {
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);
227 }
228 return;
229 }
230 else
231 static_assert(Moose::always_false<DataType>,
232 "Sensitivity coefficient computation is not implemented for the given type!");
233 }
234 }
235 else
236 static_assert(Moose::always_false<DataType>,
237 "Sensitivity coefficient computation is not implemented for the given type!");
238}
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 // _data can be empty on a rank with zero local rows.
122 auto reference_value = _data.empty() ? DataType() : initializeDataType(_data[0]);
124 {
125 // The reference point is sampler row 0, which is always local to rank 0.
126 if (this->processor_id() == 0)
127 reference_value = _data[0];
128 this->comm().broadcast(reference_value);
129 }
130
131 for (const auto param_i : make_range(num_columns))
132 {
133 // If we need relative coefficients we need to normalize the difference
134 const auto interval = _relative_sensitivity ? _sampler.getRelativeInterval(param_i)
135 : _sampler.getAbsoluteInterval(param_i);
136
137 dof_id_type left_i;
138 dof_id_type right_i;
139 // Depending on which method we use, the indices will change
140 if (_sampler.perturbationMethod() == "central_difference")
141 {
142 left_i = 2 * param_i + 1;
143 right_i = 2 * param_i + 2;
144 }
145 else
146 {
147 left_i = param_i + 1;
148 right_i = 0;
149 }
150
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();
155
156 if (left_i_in_owned_range || right_i_in_owned_range)
157 {
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]);
160 }
161
162 // We add the contribution from one side
163 if (left_i_in_owned_range)
165 this->_state.value()[param_i], _data[left_i - offset], reference_value, interval);
166
167 // We add the contribution from the other side
168 if (right_i_in_owned_range)
170 this->_state.value()[param_i], _data[right_i - offset], reference_value, -interval);
171 }
172
173 // We gather the contributions across all processors
174 this->vectorSum();
175}
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
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
processor_id_type processor_id() 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 242 of file DirectPerturbationReporter.C.

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