www.mooseframework.org
Sampler.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 // STL includes
16 #include <iterator>
17 
18 // MOOSE includes
19 #include "Sampler.h"
20 #include "MooseRandom.h"
21 #include "Distribution.h"
22 
23 template <>
26 {
28  params += validParams<SetupInterface>();
30 
31  params.addClassDescription("A base class for distribution sampling.");
32  params.addRequiredParam<std::vector<DistributionName>>(
33  "distributions", "The names of distributions that you want to sample.");
34  params.addParam<unsigned int>("seed", 0, "Random number generator initial seed");
35  params.registerBase("Sampler");
36  return params;
37 }
38 
39 Sampler::Sampler(const InputParameters & parameters)
40  : MooseObject(parameters),
41  SetupInterface(this),
43  _distribution_names(getParam<std::vector<DistributionName>>("distributions")),
44  _seed(getParam<unsigned int>("seed")),
45  _total_rows(0)
46 {
47  for (const DistributionName & name : _distribution_names)
50 }
51 
52 void
54 {
55  // Get the samples then save the state so that subsequent calls to getSamples returns the same
56  // random numbers until this execute command is called again.
57  std::vector<DenseMatrix<Real>> data = getSamples();
59  reinit(data);
60 }
61 
62 void
63 Sampler::reinit(const std::vector<DenseMatrix<Real>> & data)
64 {
65  // Update offsets and total number of rows
66  _total_rows = 0;
67  _offsets.clear();
68  _offsets.reserve(data.size() + 1);
69  _offsets.push_back(_total_rows);
70  for (const DenseMatrix<Real> & mat : data)
71  {
72  _total_rows += mat.m();
73  _offsets.push_back(_total_rows);
74  }
75 }
76 
77 std::vector<DenseMatrix<Real>>
79 {
81  sampleSetUp();
82  std::vector<DenseMatrix<Real>> output = sample();
84 
85  if (_sample_names.empty())
86  {
87  _sample_names.resize(output.size());
88  for (auto i = beginIndex(output); i < output.size(); ++i)
89  _sample_names[i] = "sample_" + std::to_string(i);
90  }
91  mooseAssert(output.size() == _sample_names.size(),
92  "The number of sample names must match the number of samples returned.");
93 
94  mooseAssert(output.size() > 0,
95  "It is not acceptable to return an empty vector of sample matrices.");
96 
97  return output;
98 }
99 
100 double
101 Sampler::rand(const unsigned int index)
102 {
103  mooseAssert(index < _generator.size(), "The seed number index does not exists.");
104  return _generator.rand(index);
105 }
106 
107 void
108 Sampler::setNumberOfRequiedRandomSeeds(const std::size_t & number)
109 {
110  if (number == 0)
111  mooseError("The number of seeds must be larger than zero.");
112 
113  // Seed the "master" seed generator
115 
116  // See the "slave" generator that will be used for the random number generation
117  for (std::size_t i = 0; i < number; ++i)
119 
121 }
122 
123 void
124 Sampler::setSampleNames(const std::vector<std::string> & names)
125 {
126  _sample_names = names;
127 
128  // Use assert because to check the size a getSamples call is required, which you don't
129  // want to do if you don't need it.
130  mooseAssert(getSamples().size() == _sample_names.size(),
131  "The number of sample names must match the number of samples returned.");
132 }
133 
135 Sampler::getLocation(unsigned int global_index)
136 {
137  if (_offsets.empty())
138  reinit(getSamples());
139 
140  mooseAssert(_offsets.size() > 1,
141  "The getSamples method returned an empty vector, if you are seeing this you have "
142  "done something to bypass another assert in the 'getSamples' method that should "
143  "prevent this message.");
144 
145  // The lower_bound method returns the first value "which does not compare less than" the value and
146  // upper_bound performs "which compares greater than." The upper_bound -1 method is used here
147  // because lower_bound will provide the wrong index, but the method here will provide the correct
148  // index, set the Sampler.GetLocation test in moose/unit/src/Sampler.C for an example.
149  std::vector<unsigned int>::iterator iter =
150  std::upper_bound(_offsets.begin(), _offsets.end(), global_index) - 1;
151  return Sampler::Location(std::distance(_offsets.begin(), iter), global_index - *iter);
152 }
153 
154 unsigned int
156 {
157  if (_total_rows == 0)
158  reinit(getSamples());
159  return _total_rows;
160 }
const std::string & name() const
Get the name of the object.
Definition: MooseObject.h:47
static uint32_t randl()
This method returns the next random number (long format) from the generator.
Definition: MooseRandom.h:76
double rand(unsigned int index=0)
Get the next random number from the generator.
Definition: Sampler.C:101
static double rand()
This method returns the next random number (double format) from the generator.
Definition: MooseRandom.h:55
MooseRandom _generator
Random number generator, don&#39;t give users access we want to control it via the interface from this cl...
Definition: Sampler.h:173
void setSampleNames(const std::vector< std::string > &names)
Set the sample names.
Definition: Sampler.C:124
unsigned int _total_rows
Total number of rows.
Definition: Sampler.h:185
virtual void sampleSetUp()
Setup method called prior and after looping through distributions.
Definition: Sampler.h:85
unsigned int getTotalNumberOfRows()
Return the number of samples.
Definition: Sampler.C:155
void saveState()
This method saves the current state of all generators which can be restored at a later time (i...
Definition: MooseRandom.h:131
std::vector< unsigned int > _offsets
Data offsets for computing location based on global index.
Definition: Sampler.h:182
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
InputParameters validParams< SetupInterface >()
Sampler(const InputParameters &parameters)
Definition: Sampler.C:39
std::vector< Distribution * > _distributions
Map used to store the perturbed parameters and their corresponding distributions. ...
Definition: Sampler.h:162
Simple object for storing the sampler location (see SamplerMultiApp).
Definition: Sampler.h:56
virtual std::vector< DenseMatrix< Real > > sample()=0
Base class must override this method to supply the sample distribution data.
Distribution & getDistributionByName(const DistributionName &name)
Get a distribution with a given name.
void setNumberOfRequiedRandomSeeds(const std::size_t &number)
Set the number of seeds required by the sampler.
Definition: Sampler.C:108
unsigned int size()
Return the number of states.
Definition: MooseRandom.h:157
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:36
Sampler::Location getLocation(unsigned int global_index)
Return the Sample::Location for the given multi app index.
Definition: Sampler.C:135
Interface for objects that need to use distributions.
const unsigned int & _seed
Initial random number seed.
Definition: Sampler.h:179
std::vector< std::string > _sample_names
Sample names.
Definition: Sampler.h:168
virtual void sampleTearDown()
Definition: Sampler.h:86
void reinit(const std::vector< DenseMatrix< Real >> &data)
Reinitialize the offsets and row counts.
Definition: Sampler.C:63
MooseRandom _seed_generator
Seed generator.
Definition: Sampler.h:176
void restoreState()
This method restores the last saved generator state.
Definition: MooseRandom.h:144
InputParameters validParams< MooseObject >()
Definition: MooseObject.C:22
InputParameters validParams< DistributionInterface >()
static void seed(unsigned int seed)
The method seeds the random number generator.
Definition: MooseRandom.h:49
std::vector< DenseMatrix< Real > > getSamples()
Return the sampled distribution data.
Definition: Sampler.C:78
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
void mooseError(Args &&...args) const
Definition: MooseObject.h:80
const std::vector< DistributionName > & _distribution_names
Distribution names.
Definition: Sampler.h:165
void execute()
Store the state of the MooseRandom generator so that new calls to getSamples will create new numbers...
Definition: Sampler.C:53
InputParameters validParams< Sampler >()
Definition: Sampler.C:25