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 240 : 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 priors to facilitate decision making in reporters 53 : */ 54 : const std::vector<const Distribution *> getPriors() const; 55 : 56 : /** 57 : * Return the prior over variance to facilitate decision making in reporters 58 : */ 59 : const Distribution * getVarPrior() const; 60 : 61 : /** 62 : * Return the step after which decision making can begin 63 : */ 64 400 : virtual int decisionStep() const { return 1; } 65 : 66 : protected: 67 : /** 68 : * Fill in the _new_samples vector of vectors (happens within sampleSetUp) 69 : * @param seed_value The seed for the random number generator 70 : */ 71 : virtual void proposeSamples(const unsigned int seed_value); 72 : 73 : // See Sampler.h for description 74 : virtual void sampleSetUp(const Sampler::SampleMode mode) override; 75 : 76 : // See Sampler.h for description 77 : virtual Real computeSample(dof_id_type row_index, dof_id_type col_index) override; 78 : 79 : /** 80 : * Sample a random index excluding a specified index 81 : * @param upper_bound The upper bound provided 82 : * @param exclude The index to be excluded from sampling 83 : * @param seed The seed of the random number generator 84 : * @param req_index The required index to be filled 85 : */ 86 : void randomIndex(const unsigned int & upper_bound, 87 : const unsigned int & exclude, 88 : const unsigned int & seed, 89 : unsigned int & req_index); 90 : 91 : /** 92 : * Sample two random indices without repitition excluding a specified index 93 : * @param upper_bound The upper bound provided 94 : * @param exclude The index to be excluded from sampling 95 : * @param seed The seed of the random number generator 96 : * @param req_index1 The required index 1 to be filled 97 : * @param req_index2 The required index 2 to be filled 98 : */ 99 : void randomIndexPair(const unsigned int & upper_bound, 100 : const unsigned int & exclude, 101 : const unsigned int & seed, 102 : unsigned int & req_index1, 103 : unsigned int & req_index2); 104 : 105 : /// Number of parallel proposals to be made and subApps to be executed 106 : const unsigned int _num_parallel_proposals; 107 : 108 : /// Storage for prior distribution objects to be utilized 109 : std::vector<const Distribution *> _priors; 110 : 111 : /// Storage for prior distribution object of the variance to be utilized 112 : const Distribution * _var_prior; 113 : 114 : /// Lower bounds for making the next proposal 115 : const std::vector<Real> * _lower_bound; 116 : 117 : /// Upper bounds for making the next proposal 118 : const std::vector<Real> * _upper_bound; 119 : 120 : /// Ensure that the MCMC algorithm proceeds in a sequential fashion 121 : int _check_step; 122 : 123 : /// Initial values of the input params to get the MCMC scheme started 124 : const std::vector<Real> & _initial_values; 125 : 126 : /// Vectors of new proposed samples 127 : std::vector<std::vector<Real>> _new_samples; 128 : 129 : /// Vector of new proposed variance samples 130 : std::vector<Real> _new_var_samples; 131 : 132 : /// Vector of random numbers for decision making 133 : std::vector<Real> _rnd_vec; 134 : 135 : private: 136 : /** 137 : * Generates combinations of the new samples with the experimental configurations 138 : */ 139 : void combineWithExperimentalConfig(); 140 : 141 : /// Initialize a certain number of random seeds. Change from the default only if you have to. 142 : const unsigned int _num_random_seeds; 143 : 144 : /// Configuration values 145 : std::vector<std::vector<Real>> _confg_values; 146 : 147 : /// Vectors of new proposed samples combined with the experimental configuration values 148 : std::vector<std::vector<Real>> _new_samples_confg; 149 : };