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 "Sampler.h" 13 : #include "TransientInterface.h" 14 : #include "Distribution.h" 15 : 16 : /** 17 : * A base class used to perform Parallel Markov Chain Monte Carlo (MCMC) sampling 18 : */ 19 : class PMCMCBase : public Sampler, public TransientInterface 20 : { 21 : public: 22 : static InputParameters validParams(); 23 : 24 : PMCMCBase(const InputParameters & parameters); 25 : 26 : /** 27 : * Return the number of configuration parameters. 28 : */ 29 : dof_id_type getNumberOfConfigValues() const { return _confg_values[0].size(); } 30 : 31 : /** 32 : * Return the number of configuration parameters. 33 : */ 34 : dof_id_type getNumberOfConfigParams() const { return _confg_values.size(); } 35 : 36 : /** 37 : * Return the number of parallel proposals. 38 : */ 39 124 : dof_id_type getNumParallelProposals() const { return _num_parallel_proposals; } 40 : 41 : /** 42 : * Return the random numbers to facilitate decision making in reporters 43 : */ 44 : const std::vector<Real> & getRandomNumbers() const; 45 : 46 : /** 47 : * Return the proposed variance samples to facilitate decision making in reporters 48 : */ 49 : const std::vector<Real> & getVarSamples() const; 50 : 51 : /** 52 : * Return the proposed samples to facilitate decision making in reporters. 53 : * In MCMC schemes, there is a decision-making step after evaluating the 54 : * computational model on whether or not to accept the proposed samples. 55 : * To facilitate this decision-making, which happens in the Reporter, we 56 : * have to provide it the proposed samples. 57 : */ 58 : const std::vector<std::vector<Real>> & getSamples() const; 59 : 60 : /** 61 : * Return the priors to facilitate decision making in reporters 62 : */ 63 : const std::vector<const Distribution *> getPriors() const; 64 : 65 : /** 66 : * Return the prior over variance to facilitate decision making in reporters 67 : */ 68 : const Distribution * getVarPrior() const; 69 : 70 : /** 71 : * Return the step after which decision making can begin 72 : */ 73 200 : virtual int decisionStep() const { return 1; } 74 : 75 : protected: 76 : /** 77 : * Fill in the _new_samples vector of vectors (happens within sampleSetUp) 78 : * @param seed_value The seed for the random number generator 79 : */ 80 : virtual void proposeSamples(); 81 : 82 : // See Sampler.h for description 83 : virtual void executeSetUp() override; 84 : 85 : // See Sampler.h for description 86 : virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override; 87 : 88 : /** 89 : * Sample a random number between 0 and 1 90 : * @param upper_bound The upper bound provided 91 : * @return The required index 92 : */ 93 : Real random(); 94 : 95 : /** 96 : * Sample a random index excluding a specified index 97 : * @param upper_bound The upper bound provided 98 : * @return The required index 99 : */ 100 : unsigned int randomIndex(const unsigned int & upper_bound, const unsigned int & exclude); 101 : 102 : /** 103 : * Sample two random indices without repitition excluding a specified index 104 : * @param upper_bound The upper bound provided 105 : * @param exclude The index to be excluded from sampling 106 : * @return Pair of required indices 107 : */ 108 : std::pair<unsigned int, unsigned int> randomIndexPair(const unsigned int & upper_bound, 109 : const unsigned int & exclude); 110 : 111 : /// Number of parallel proposals to be made and subApps to be executed 112 : const unsigned int _num_parallel_proposals; 113 : 114 : /// Storage for prior distribution objects to be utilized 115 : std::vector<const Distribution *> _priors; 116 : 117 : /// Storage for prior distribution object of the variance to be utilized 118 : const Distribution * _var_prior; 119 : 120 : /// Lower bounds for making the next proposal 121 : const std::vector<Real> * _lower_bound; 122 : 123 : /// Upper bounds for making the next proposal 124 : const std::vector<Real> * _upper_bound; 125 : 126 : /// Upper bound for variance for making the next proposal 127 : const Real & _variance_bound; 128 : 129 : /// Initial values of the input params to get the MCMC scheme started 130 : const std::vector<Real> & _initial_values; 131 : 132 : /// Vectors of new proposed samples 133 : std::vector<std::vector<Real>> _new_samples; 134 : 135 : /// Vector of new proposed variance samples 136 : std::vector<Real> _new_var_samples; 137 : 138 : /// Vector of random numbers for decision making 139 : std::vector<Real> _rnd_vec; 140 : 141 : private: 142 : /** 143 : * Generates combinations of the new samples with the experimental configurations 144 : */ 145 : void combineWithExperimentalConfig(); 146 : 147 : /// Initialize a certain number of random seeds. Change from the default only if you have to. 148 : const unsigned int _num_random_seeds; 149 : 150 : /// Generator index when requesting random numbers 151 : unsigned int _seed_index; 152 : 153 : /// Running index for the random number generators 154 : std::size_t _rand_index; 155 : 156 : /// Configuration values 157 : std::vector<std::vector<Real>> _confg_values; 158 : 159 : /// Vectors of new proposed samples combined with the experimental configuration values 160 : std::vector<std::vector<Real>> _new_samples_confg; 161 : };