SST  14.0.0
StructuralSimulationToolkit
poisson.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_POISSON_H
13 #define SST_CORE_RNG_POISSON_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 PoissonDistribution poisson.h "sst/core/rng/poisson.h"
27 
28  Creates an Poisson distribution for use within SST. This distribution is the same across
29  platforms and compilers.
30 */
32 {
33 
34 public:
35  /**
36  Creates an Poisson distribution with a specific lambda
37  \param mn The lambda of the Poisson distribution
38  */
39  PoissonDistribution(const double mn) : RandomDistribution(), lambda(mn)
40  {
41 
42  baseDistrib = new MersenneRNG();
43  deleteDistrib = true;
44  }
45 
46  /**
47  Creates an Poisson distribution with a specific lambda and a base random number generator
48  \param lambda The lambda of the Poisson distribution
49  \param baseDist The base random number generator to take the distribution from.
50  */
51  PoissonDistribution(const double mn, SST::RNG::Random* baseDist) : RandomDistribution(), lambda(mn)
52  {
53 
54  baseDistrib = baseDist;
55  deleteDistrib = false;
56  }
57 
58  /**
59  Destroys the Poisson distribution
60  */
62  {
63  if ( deleteDistrib ) { delete baseDistrib; }
64  }
65 
66  /**
67  Gets the next (random) double value in the distribution
68  \return The next random double from the distribution
69  */
70  double getNextDouble() override
71  {
72  const double L = exp(-lambda);
73  double p = 1.0;
74  int k = 0;
75 
76  do {
77  k++;
78  p *= baseDistrib->nextUniform();
79  } while ( p > L );
80 
81  return k - 1;
82  }
83 
84  /**
85  Gets the lambda with which the distribution was created
86  \return The lambda which the user created the distribution with
87  */
88  double getLambda() { return lambda; }
89 
90  /**
91  Default constructor. FOR SERIALIZATION ONLY.
92  */
94 
95  /**
96  Serialization function for checkpoint
97  */
99  {
100  ser& const_cast<double&>(lambda);
101  ser& baseDistrib;
102  ser& deleteDistrib;
103  }
104 
105  /**
106  Serialization macro
107  */
108  ImplementSerializable(PoissonDistribution)
109 
110 protected:
111  /**
112  Sets the lambda of the Poisson distribution.
113  */
114  const double lambda;
115  /**
116  Sets the base random number generator for the distribution.
117  */
119 
120  /**
121  Controls whether the base distribution should be deleted when this class is destructed.
122  */
124 };
125 
127 
128 } // namespace RNG
129 } // namespace SST
130 
131 #endif // SST_CORE_RNG_POISSON_H
Implements the base class for random number generators for the SST core.
Definition: rng.h:29
ImplementSerializable(PoissonDistribution) protected SST::RNG::Random * baseDistrib
Serialization macro.
Definition: poisson.h:108
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
PoissonDistribution()
Default constructor.
Definition: poisson.h:93
bool deleteDistrib
Controls whether the base distribution should be deleted when this class is destructed.
Definition: poisson.h:123
Definition: action.cc:18
~PoissonDistribution()
Destroys the Poisson distribution.
Definition: poisson.h:61
Implements a Mersenne-based RNG for use in the SST core or components.
Definition: mersenne.h:34
PoissonDistribution(const double mn, SST::RNG::Random *baseDist)
Creates an Poisson distribution with a specific lambda and a base random number generator.
Definition: poisson.h:51
double getNextDouble() override
Gets the next (random) double value in the distribution.
Definition: poisson.h:70
double getLambda()
Gets the lambda with which the distribution was created.
Definition: poisson.h:88
PoissonDistribution(const double mn)
Creates an Poisson distribution with a specific lambda.
Definition: poisson.h:39
Creates an Poisson distribution for use within SST.
Definition: poisson.h:31
Base class of statistical distributions in SST.
Definition: distrib.h:24
void serialize_order(SST::Core::Serialization::serializer &ser) override
Serialization function for checkpoint.
Definition: poisson.h:98