SST  14.0.0
StructuralSimulationToolkit
gaussian.h
1 // Copyright 2009-2024 NTESS. Under the terms
2 // of Contract DE-NA0003525 with NTESS, the U.S.
3 // Government retains certain rights in this software.
4 //
5 // Copyright (c) 2009-2024, NTESS
6 // All rights reserved.
7 //
8 // This file is part of the SST software package. For license
9 // information, see the LICENSE file in the top level directory of the
10 // distribution.
11 
12 #ifndef SST_CORE_RNG_GAUSSIAN_H
13 #define SST_CORE_RNG_GAUSSIAN_H
14 
15 #include "distrib.h"
16 #include "math.h"
17 #include "mersenne.h"
18 #include "rng.h"
19 
20 using namespace SST::RNG;
21 
22 namespace SST {
23 namespace RNG {
24 
25 /**
26  \class GaussianDistribution gaussian.h "sst/core/rng/gaussian.h"
27 
28  Creates a Gaussian (normal) distribution for which to sample
29 */
31 {
32 
33 public:
34  /**
35  Creates a new distribution with a predefined random number generator with a specified mean and standard
36  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
37  distribution
38  */
39  GaussianDistribution(double mn, double sd) : RandomDistribution()
40  {
41 
42  mean = mn;
43  stddev = sd;
44 
45  baseDistrib = new MersenneRNG();
46  unusedPair = 0;
47  usePair = false;
48  deleteDistrib = true;
49  }
50 
51  /**
52  Creates a new distribution with a predefined random number generator with a specified mean and standard
53  deviation. \param mn The mean of the Gaussian distribution \param sd The standard deviation of the Gaussian
54  distribution \param baseRNG The random number generator as the base of the distribution
55  */
56  GaussianDistribution(double mn, double sd, SST::RNG::Random* baseRNG) : RandomDistribution()
57  {
58 
59  mean = mn;
60  stddev = sd;
61 
62  baseDistrib = baseRNG;
63  unusedPair = 0;
64  usePair = false;
65  deleteDistrib = false;
66  }
67 
68  /**
69  Destroys the Gaussian distribution.
70  */
72  {
73  if ( deleteDistrib ) { delete baseDistrib; }
74  }
75 
76  /**
77  Gets the next double value in the distribution
78  \return The next double value of the distribution (in this case a Gaussian distribution)
79  */
80  double getNextDouble() override
81  {
82  if ( usePair ) {
83  usePair = false;
84  return unusedPair;
85  }
86  else {
87  double gauss_u, gauss_v, sq_sum;
88 
89  do {
90  gauss_u = baseDistrib->nextUniform();
91  gauss_v = baseDistrib->nextUniform();
92  sq_sum = (gauss_u * gauss_u) + (gauss_v * gauss_v);
93  } while ( sq_sum >= 1 || sq_sum == 0 );
94 
95  if ( baseDistrib->nextUniform() < 0.5 ) { gauss_u *= -1.0; }
96 
97  if ( baseDistrib->nextUniform() < 0.5 ) { gauss_v *= -1.0; }
98 
99  double multiplier = sqrt(-2.0 * log(sq_sum) / sq_sum);
100  unusedPair = mean + stddev * gauss_v * multiplier;
101  usePair = true;
102 
103  return mean + stddev * gauss_u * multiplier;
104  }
105  }
106 
107  /**
108  Gets the mean of the distribution
109  \return The mean of the Guassian distribution
110  */
111  double getMean() { return mean; }
112 
113  /**
114  Gets the standard deviation of the distribution
115  \return The standard deviation of the Gaussian distribution
116  */
117  double getStandardDev() { return stddev; }
118 
119  /**
120  Default constructor. FOR SERIALIZATION ONLY.
121  */
123 
124  /**
125  Serialization function for checkpoint
126  */
128  {
129  ser& mean;
130  ser& stddev;
131  ser& baseDistrib;
132  ser& unusedPair;
133  ser& usePair;
134  ser& deleteDistrib;
135  }
136 
137  /**
138  Serialization macro
139  */
140  ImplementSerializable(SST::RNG::GaussianDistribution)
141 
142 protected:
143  /**
144  The mean of the Gaussian distribution
145  */
146  double mean;
147  /**
148  The standard deviation of the Gaussian distribution
149  */
150  double stddev;
151  /**
152  The base random number generator for the distribution
153  */
155  /**
156  Random numbers for the distribution are read in pairs, this stores the second of the pair
157  */
158  double unusedPair;
159  /**
160  Random numbers for the distribution are read in pairs, this tells the code to use the second of the pair
161  */
162  bool usePair;
163 
164  /**
165  Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created the
166  distribution)
167  */
169 };
170 
172 
173 } // namespace RNG
174 } // namespace SST
175 
176 #endif // SST_CORE_RNG_GAUSSIAN_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:29
Definition: constant.h:21
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
bool usePair
Random numbers for the distribution are read in pairs, this tells the code to use the second of the p...
Definition: gaussian.h:162
bool deleteDistrib
Controls whether the destructor deletes the distribution (we need to ensure we do this IF we created ...
Definition: gaussian.h:168
Definition: action.cc:18
ImplementSerializable(SST::RNG::GaussianDistribution) protected double stddev
Serialization macro.
Definition: gaussian.h:140
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
~GaussianDistribution()
Destroys the Gaussian distribution.
Definition: gaussian.h:71
SST::RNG::Random * baseDistrib
The base random number generator for the distribution.
Definition: gaussian.h:154
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition: gaussian.h:127
double getNextDouble() override
Gets the next double value in the distribution.
Definition: gaussian.h:80
GaussianDistribution()
Default constructor.
Definition: gaussian.h:122
Creates a Gaussian (normal) distribution for which to sample.
Definition: gaussian.h:30
GaussianDistribution(double mn, double sd, SST::RNG::Random *baseRNG)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:56
double getMean()
Gets the mean of the distribution.
Definition: gaussian.h:111
double getStandardDev()
Gets the standard deviation of the distribution.
Definition: gaussian.h:117
double unusedPair
Random numbers for the distribution are read in pairs, this stores the second of the pair...
Definition: gaussian.h:158
Base class of statistical distributions in SST.
Definition: distrib.h:24
GaussianDistribution(double mn, double sd)
Creates a new distribution with a predefined random number generator with a specified mean and standa...
Definition: gaussian.h:39