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