criptic v1
Cosmic Ray Interstellar Propagation Tool using Itô Calculus
Loading...
Searching...
No Matches
RngThread.H
Go to the documentation of this file.
1
13#ifndef RNGTHREAD_H_
14#define RNGTHREAD_H_
15
16#include <random>
17#include <sstream>
18#include <vector>
19#include "ThreadVec.H"
20#include "Types.H"
21#include "../pcg-cpp/pcg_random.hpp"
22#include "../IO/ParmParser.H"
23#ifdef _OPENMP
24# include <omp.h>
25#endif
26
27namespace criptic {
28
39 class RngThread {
40
41 public:
42
47 RngThread(const ParmParser &pp);
48
53 RngThread(std::string& buf);
54
58 ~RngThread();
59
66 Real uniform(const Real a = 0.0, const Real b = 1.0) const {
67 std::uniform_real_distribution<Real> dist(a,b);
68 pcg64& rth = *r();
69 return dist(rth);
70 }
71
78 IdxType uniformInt(const IdxType a = 0, const IdxType b = maxIdx) const {
79 std::uniform_int_distribution<IdxType> dist(a,b);
80 pcg64& rth = *r();
81 return dist(rth);
82 }
83
90 Real normal(const Real mean = 0.0, const Real stddev = 1.0) const {
91 std::normal_distribution<Real> dist(mean, stddev);
92 pcg64& rth = *r();
93 return dist(rth);
94 }
95
101 IdxType poisson(const Real ex) const {
102 std::poisson_distribution<IdxType> dist(ex);
103 pcg64& rth = *r();
104 return dist(rth);
105 }
106
119 template <typename T>
120 T& choose(std::vector<T>& elem,
121 const std::vector<Real>& w = std::vector<Real>()) const {
122 if (elem.size() == 1) return elem[0];
123 else if (w.size() == 0) return elem[uniformInt(0, elem.size()-1)];
124 else {
125 std::vector<Real> wCum(w.size());
126 wCum[0] = w[0];
127 for (IdxType i = 1; i < w.size(); i++) wCum[i] = wCum[i-1] + w[i];
128 Real x = uniform(0, wCum.back());
129 if (x < wCum[0]) return elem[0];
130 else {
131 IdxType l = 0, r = wCum.size()-1;
132 while (r - l > 1) {
133 IdxType c = (l + r) / 2;
134 if (x < wCum[c]) r = c;
135 else l = c;
136 }
137 return elem[r];
138 }
139 }
140 }
141
154 template <typename T>
155 const T& choose(const std::vector<T>& elem,
156 const std::vector<Real>& w = std::vector<Real>()) const {
157 if (w.size() == 0) return elem[uniformInt(0, elem.size()-1)];
158 else {
159 std::vector<Real> wCum(w.size());
160 wCum[0] = w[0];
161 for (IdxType i = 0; i < w.size(); i++) wCum[i] = wCum[i-1] + w[i];
162 Real x = uniform(0, wCum.back());
163 if (x < wCum[0]) return elem[0];
164 else {
165 IdxType l = 0, r = wCum.size()-1;
166 while (r - l > 1) {
167 IdxType c = (l + r) / 2;
168 if (x < wCum[c]) r = c;
169 else l = c;
170 }
171 return elem[r];
172 }
173 }
174 }
175
188 const std::string getState() const;
189
201 void setState(const std::string& buf);
202
210 static IdxType stateSize();
211
212 private:
213
214 // Internal data
217 };
218
219}
220
221#endif
222// _RNGTHREAD_H_
A class to hold objects where one copy is required per thread.
Basic integer and real types.
Class to parse the criptic input deck.
Definition ParmParser.H:37
Thread-safe random number generator.
Definition RngThread.H:39
ThreadVec< pcg64 * > r
Definition RngThread.H:215
T & choose(std::vector< T > &elem, const std::vector< Real > &w=std::vector< Real >()) const
Randomly choose an element from a vector.
Definition RngThread.H:120
const std::string getState() const
Get a serialized buffer holding the RNG state.
Definition RngThread.cpp:53
static IdxType stateSize()
Return the number of bytes in the RNG state buffer.
Definition RngThread.cpp:83
~RngThread()
Free RngThread object memory.
Definition RngThread.cpp:44
Real uniform(const Real a=0.0, const Real b=1.0) const
Return a random number uniformly-distributed in (a,b)
Definition RngThread.H:66
const T & choose(const std::vector< T > &elem, const std::vector< Real > &w=std::vector< Real >()) const
Randomly choose an element from a const vector.
Definition RngThread.H:155
Real normal(const Real mean=0.0, const Real stddev=1.0) const
Return a normally-distributed random number.
Definition RngThread.H:90
void setState(const std::string &buf)
Restore the RNG state from a buffer.
Definition RngThread.cpp:71
IdxType poisson(const Real ex) const
Return an integer drawn from a Poisson distribution.
Definition RngThread.H:101
IdxType uniformInt(const IdxType a=0, const IdxType b=maxIdx) const
Return a random integer uniformly-distributed in (a,b)
Definition RngThread.H:78
A class to automate private thread copies of objects.
Definition ThreadVec.H:35
IdxType size() const
Return the number of distinct objects stored.
Definition ThreadVec.H:59
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
constexpr IdxType maxIdx
Definition Types.H:55
std::vector< Real >::size_type IdxType
Definition Types.H:45
double Real
Definition Types.H:38