Line data Source code
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 : 10 : #pragma once 11 : 12 : #include "GeneralReporter.h" 13 : #include "VectorCalculators.h" 14 : 15 : #include "nlohmann/json.h" 16 : 17 : // Forward declaration 18 : class DirectPerturbationSampler; 19 : 20 : /** 21 : * Reporter class for computing and displaying local sensitivity 22 : * coefficients around a nominal value using a direct perturbation 23 : * method. 24 : */ 25 : class DirectPerturbationReporter : public GeneralReporter 26 : { 27 : public: 28 : static InputParameters validParams(); 29 : DirectPerturbationReporter(const InputParameters & parameters); 30 : 31 : virtual void initialize() override final; 32 : 33 80 : virtual void execute() override final {} 34 80 : virtual void finalize() override final {} 35 : 36 : private: 37 : /** 38 : * Helper for adding direct perturbation-based reporter values 39 : */ 40 : template <typename DataType> 41 : void declareValueHelper(const ReporterName & r_name); 42 : 43 : /// Direct perturbation sampler 44 : DirectPerturbationSampler & _sampler; 45 : 46 : /// If relative sensitivity should be computed 47 : const bool _relative_sensitivity; 48 : 49 : /// Whether or not initialize() has been called for reporter value declaration 50 : bool _initialized; 51 : }; 52 : 53 : /** 54 : * Reporter context for computing direct perturbation-based sensitivity 55 : * coefficients 56 : */ 57 : template <typename DataType> 58 : class DirectPerturbationReporterContext : public ReporterGeneralContext<std::vector<DataType>> 59 : { 60 : public: 61 : /** 62 : * Constructor 63 : * @param other A parallel object, usually the MooseApp 64 : * @param producer The producer object for the reporter 65 : * @param state A reporter state (a vector of some types in this case) 66 : * @param sampler The sampler holding information on the direct perturbation paraemters 67 : * @param data The data coming back from the executed models 68 : */ 69 : DirectPerturbationReporterContext(const libMesh::ParallelObject & other, 70 : const MooseObject & producer, 71 : ReporterState<std::vector<DataType>> & state, 72 : DirectPerturbationSampler & sampler, 73 : const std::vector<DataType> & data, 74 : const bool relative_sensitivity); 75 : 76 : virtual void finalize() override; 77 200 : virtual std::string type() const override 78 : { 79 400 : return "DirectPerturbationSensitivity<" + MooseUtils::prettyCppType<DataType>() + ">"; 80 : } 81 : 82 : private: 83 : /// Compute direct perturbation sensitivity, split into a separate function due to 84 : /// the different operators on vectors and scalars 85 : /// @param add_to The data structure which will be extended 86 : /// @param to_add The data structure which will be added to the other one 87 : /// @param reference_value The reference values in case we are computing relative sensitivities 88 : /// @param interval The interval scaling coefficient vector 89 : void addSensitivityContribution(DataType & add_to, 90 : const DataType & to_add, 91 : const DataType & reference_value, 92 : const Real interval) const; 93 : 94 : /// Initialize the sensitivity container, split into a separate function due to 95 : /// the different constructors for scalars and vectors 96 : /// @param example_output A structure which supplies the dimensions for the allocation 97 : DataType initializeDataType(const DataType & example_output) const; 98 : 99 : /// Reference to the direct perturbation sampler 100 : DirectPerturbationSampler & _sampler; 101 : 102 : /// Data used for the statistic calculation 103 : const std::vector<DataType> & _data; 104 : 105 : /// If relative sensitivities should be computed 106 : const bool _relative_sensitivity; 107 : };