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 "SobolCalculators.h" 14 : #include "VectorOfVectorCalculators.h" 15 : 16 : class SobolSampler; 17 : template <typename InType, typename OutType> 18 : class SobolReporterContext; 19 : 20 : /** 21 : * Computes Sobol sensitivity indices, see SobolCalculators 22 : */ 23 : class SobolReporter : public GeneralReporter 24 : { 25 : public: 26 : static InputParameters validParams(); 27 : SobolReporter(const InputParameters & parameters); 28 : 29 64 : virtual void initialSetup() override {} 30 : virtual void initialize() override; 31 64 : virtual void execute() override {} 32 64 : virtual void finalize() override {} 33 : 34 : virtual void store(nlohmann::json & json) const override; 35 : 36 : private: 37 : /** 38 : * Helper for adding Sobol reporter values 39 : * 40 : * @param r_name ReporterName of the data from which the statistics will be computed 41 : */ 42 : template <typename InType> 43 : void declareValueHelper(const ReporterName & r_name); 44 : 45 : /// The sampler that generated the samples that produced results for the _results_vectors 46 : const SobolSampler & _sobol_sampler; 47 : 48 : /// CI levels to be computed 49 : const std::vector<Real> & _ci_levels; 50 : 51 : /// Number of CI replicates to use in Bootstrap methods 52 : const unsigned int & _ci_replicates; 53 : 54 : /// Random seed for producing CI replicates 55 : const unsigned int & _ci_seed; 56 : 57 : /// Whether or not initialize() has been called for reporter value declaration 58 : bool _initialized; 59 : }; 60 : 61 : template <typename OutType> 62 : using SobolState = std::pair<std::vector<OutType>, std::vector<std::vector<OutType>>>; 63 : 64 : template <typename InType, typename OutType> 65 : class SobolReporterContext : public ReporterGeneralContext<SobolState<OutType>> 66 : { 67 : public: 68 : SobolReporterContext(const libMesh::ParallelObject & other, 69 : const MooseObject & producer, 70 : ReporterState<SobolState<OutType>> & state, 71 : const InType & data, 72 : const ReporterProducerEnum & mode, 73 : const SobolSampler & sampler); 74 : 75 : SobolReporterContext(const libMesh::ParallelObject & other, 76 : const MooseObject & producer, 77 : ReporterState<SobolState<OutType>> & state, 78 : const InType & data, 79 : const ReporterProducerEnum & mode, 80 : const SobolSampler & sampler, 81 : const MooseEnum & ci_method, 82 : const std::vector<Real> & ci_levels, 83 : unsigned int ci_replicates, 84 : unsigned int ci_seed); 85 : 86 : virtual void finalize() override; 87 70 : virtual std::string type() const override 88 : { 89 140 : return "SobolIndices<" + MooseUtils::prettyCppType<OutType>() + ">"; 90 : } 91 : static void 92 : storeSobol(nlohmann::json & json, const SobolState<OutType> & val, unsigned int nparam); 93 : 94 : protected: 95 : virtual void store(nlohmann::json & json) const override; 96 : 97 : private: 98 : /// Data used for the statistic calculation 99 : const InType & _data; 100 : 101 : /// Mode in which the above data was produced 102 : const ReporterProducerEnum & _data_mode; 103 : 104 : /// Sobol sampler to get info on number of matrices and whatnot 105 : const SobolSampler & _sampler; 106 : 107 : /// Storage for the SobolCalculator object, this is created in constructor 108 : StochasticTools::SobolCalculator<InType, OutType> _calc; 109 : 110 : /// Storage for the BootstrapCalculator for the desired confidence interval calculations (optional) 111 : std::unique_ptr<StochasticTools::BootstrapCalculator<std::vector<InType>, std::vector<OutType>>> 112 : _ci_calc_ptr = nullptr; 113 : };