[26740] | 1 | Index: ../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp
|
---|
| 2 | ===================================================================
|
---|
| 3 | --- ../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp (revision 26611)
|
---|
| 4 | +++ ../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp (revision 26612)
|
---|
| 5 | @@ -8,7 +8,7 @@
|
---|
| 6 | #include "../modules/modules.h"
|
---|
| 7 | #include "../analyses/analyses.h"
|
---|
| 8 | #include "../shared/Random/randomgenerator.h"
|
---|
| 9 | -#include <random>
|
---|
| 10 | +//#include <random>
|
---|
| 11 |
|
---|
| 12 | void GaussianVector(Vector<IssmDouble>* ppf,int seed){/*{{{*/
|
---|
| 13 |
|
---|
| 14 | @@ -17,19 +17,19 @@
|
---|
| 15 | double rdnumber;
|
---|
| 16 |
|
---|
| 17 | /*Define seed*/
|
---|
| 18 | - rnd::normal_distribution distribution;
|
---|
| 19 | - if(seed<0){
|
---|
| 20 | - std::random_device rd;
|
---|
| 21 | - seed = rd();
|
---|
| 22 | - }
|
---|
| 23 | - else
|
---|
| 24 | + rnd::linear_congruential_engine random_engine;
|
---|
| 25 | + if(seed>=0)
|
---|
| 26 | {
|
---|
| 27 | int my_rank;
|
---|
| 28 | ISSM_MPI_Comm_rank(ISSM_MPI_COMM_WORLD,&my_rank);
|
---|
| 29 | seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number)
|
---|
| 30 | }
|
---|
| 31 | - distribution.seed(seed);
|
---|
| 32 | + random_engine.seed(seed);
|
---|
| 33 |
|
---|
| 34 | + /* Define univariate distribution */
|
---|
| 35 | +
|
---|
| 36 | + rnd::normal_distribution distribution(0.0,1.0);
|
---|
| 37 | +
|
---|
| 38 | int *local_indices = NULL;
|
---|
| 39 | IssmDouble *local_vector = NULL;
|
---|
| 40 | ppf->GetLocalVector(&local_vector,&local_indices);
|
---|
| 41 | @@ -37,7 +37,7 @@
|
---|
| 42 | int M;
|
---|
| 43 | ppf->GetLocalSize(&M);
|
---|
| 44 | for(int i=0;i<M;i++){
|
---|
| 45 | - rdnumber = distribution.generator();
|
---|
| 46 | + rdnumber = distribution.generator(random_engine);
|
---|
| 47 | ppf->SetValue(local_indices[i],rdnumber,INS_VAL);
|
---|
| 48 | }
|
---|
| 49 | ppf->Assemble();
|
---|
| 50 | Index: ../trunk-jpl/src/c/shared/Random/randomgenerator.cpp
|
---|
| 51 | ===================================================================
|
---|
| 52 | --- ../trunk-jpl/src/c/shared/Random/randomgenerator.cpp (revision 26611)
|
---|
| 53 | +++ ../trunk-jpl/src/c/shared/Random/randomgenerator.cpp (revision 26612)
|
---|
| 54 | @@ -12,68 +12,94 @@
|
---|
| 55 |
|
---|
| 56 | namespace rnd{
|
---|
| 57 |
|
---|
| 58 | - uniform_distribution::uniform_distribution(){/*{{{*/
|
---|
| 59 | + /* Linear congruential engine */
|
---|
| 60 |
|
---|
| 61 | - a = 1103515245; // BSD Formula
|
---|
| 62 | - c = 12345; // BSD Formula
|
---|
| 63 | - m = 2147483648; // BSD Formula
|
---|
| 64 | - _seed = 0;
|
---|
| 65 | - lbound = 0.0;
|
---|
| 66 | - ubound = 1.0;
|
---|
| 67 | + linear_congruential_engine::linear_congruential_engine(){/*{{{*/
|
---|
| 68 | +
|
---|
| 69 | + pseed = new unsigned int;
|
---|
| 70 | + *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1);
|
---|
| 71 | + a = 1103515245; // BSD Formula
|
---|
| 72 | + c = 12345; // BSD Formula
|
---|
| 73 | + m = 2147483648; // BSD Formula
|
---|
| 74 | return;
|
---|
| 75 | + }/*}}}*/
|
---|
| 76 | +
|
---|
| 77 | + linear_congruential_engine::linear_congruential_engine(unsigned int _a, unsigned int _b, unsigned int _m){/*{{{*/
|
---|
| 78 | +
|
---|
| 79 | + pseed = new unsigned int;
|
---|
| 80 | + *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1);
|
---|
| 81 | + a = _a;
|
---|
| 82 | + c = _b;
|
---|
| 83 | + m = _m;
|
---|
| 84 | + return;
|
---|
| 85 | + }/*}}}*/
|
---|
| 86 | +
|
---|
| 87 | + linear_congruential_engine::~linear_congruential_engine(){}
|
---|
| 88 | +
|
---|
| 89 | + unsigned int linear_congruential_engine::get_m() { return m; }
|
---|
| 90 | +
|
---|
| 91 | + void linear_congruential_engine::seed( int s ) {
|
---|
| 92 | + if(s<0)
|
---|
| 93 | + *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1);
|
---|
| 94 | + else
|
---|
| 95 | + *pseed = (unsigned) s;
|
---|
| 96 | + }
|
---|
| 97 | +
|
---|
| 98 | + unsigned int linear_congruential_engine::generator(){
|
---|
| 99 | + *pseed = ( a * *pseed + c ) % m ;
|
---|
| 100 | + return *pseed;
|
---|
| 101 | + }
|
---|
| 102 | +
|
---|
| 103 | + /* Uniform distribution */
|
---|
| 104 | +
|
---|
| 105 | + uniform_distribution::uniform_distribution(){/*{{{*/
|
---|
| 106 | + a = 0.0;
|
---|
| 107 | + b = 1.0;
|
---|
| 108 | + return;
|
---|
| 109 | }
|
---|
| 110 | /*}}}*/
|
---|
| 111 | - uniform_distribution::uniform_distribution(double lower,double upper){/*{{{*/
|
---|
| 112 |
|
---|
| 113 | - a = 1103515245; // BSD Formula
|
---|
| 114 | - c = 12345; // BSD Formula
|
---|
| 115 | - m = 2147483648; // BSD Formula
|
---|
| 116 | - _seed = 0;
|
---|
| 117 | - lbound = lower;
|
---|
| 118 | - ubound = upper;
|
---|
| 119 | + uniform_distribution::uniform_distribution(double _a,double _b){/*{{{*/
|
---|
| 120 | + a = _a;
|
---|
| 121 | + b = _b;
|
---|
| 122 | return;
|
---|
| 123 | }
|
---|
| 124 | /*}}}*/
|
---|
| 125 | +
|
---|
| 126 | uniform_distribution::~uniform_distribution(){}
|
---|
| 127 | - void uniform_distribution::seed( unsigned int s ) { _seed = s; }
|
---|
| 128 | - unsigned int uniform_distribution::get_seed() { return _seed; }
|
---|
| 129 | - double uniform_distribution::generator() {
|
---|
| 130 | - _seed = ( a * _seed + c ) % m ;
|
---|
| 131 | - return (ubound-lbound)*(double) _seed/ m + lbound;
|
---|
| 132 | +
|
---|
| 133 | + double uniform_distribution::generator(rnd::linear_congruential_engine random_engine) {
|
---|
| 134 | + return (b-a)*(double) random_engine.generator()/ random_engine.get_m() + a;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | + /* Normal distribution */
|
---|
| 138 |
|
---|
| 139 | normal_distribution::normal_distribution(){/*{{{*/
|
---|
| 140 | -
|
---|
| 141 | - _seed = 0;
|
---|
| 142 | mean = 0;
|
---|
| 143 | sdev = 1.0;
|
---|
| 144 | return;
|
---|
| 145 | }
|
---|
| 146 | /*}}}*/
|
---|
| 147 | +
|
---|
| 148 | normal_distribution::normal_distribution(double m,double s){/*{{{*/
|
---|
| 149 | -
|
---|
| 150 | - _seed = 0;
|
---|
| 151 | mean = m;
|
---|
| 152 | sdev = s;
|
---|
| 153 | return;
|
---|
| 154 | }
|
---|
| 155 | - /*}}}*/
|
---|
| 156 | + /*}}}*/
|
---|
| 157 | +
|
---|
| 158 | normal_distribution::~normal_distribution(){}
|
---|
| 159 | - void normal_distribution::seed( unsigned int s ) { _seed = s; }
|
---|
| 160 | - double normal_distribution::generator(){/*{{{*/
|
---|
| 161 |
|
---|
| 162 | + double normal_distribution::generator(rnd::linear_congruential_engine random_engine){/*{{{*/
|
---|
| 163 | +
|
---|
| 164 | rnd::uniform_distribution unifdistri;
|
---|
| 165 | - unifdistri.seed(_seed);
|
---|
| 166 |
|
---|
| 167 | - double u1 = unifdistri.generator();
|
---|
| 168 | - double u2 = unifdistri.generator();
|
---|
| 169 | + double u1 = unifdistri.generator(random_engine);
|
---|
| 170 | + double u2 = unifdistri.generator(random_engine);
|
---|
| 171 |
|
---|
| 172 | double R = sqrt(-2*log(u1));
|
---|
| 173 | double theta = 2*M_PI*u2;
|
---|
| 174 |
|
---|
| 175 | - seed(unifdistri.get_seed());
|
---|
| 176 | -
|
---|
| 177 | return mean + sdev * (R*cos(theta));
|
---|
| 178 |
|
---|
| 179 | }
|
---|
| 180 | Index: ../trunk-jpl/src/c/shared/Random/randomgenerator.h
|
---|
| 181 | ===================================================================
|
---|
| 182 | --- ../trunk-jpl/src/c/shared/Random/randomgenerator.h (revision 26611)
|
---|
| 183 | +++ ../trunk-jpl/src/c/shared/Random/randomgenerator.h (revision 26612)
|
---|
| 184 | @@ -10,27 +10,42 @@
|
---|
| 185 |
|
---|
| 186 | namespace rnd{
|
---|
| 187 |
|
---|
| 188 | + class linear_congruential_engine
|
---|
| 189 | + {
|
---|
| 190 | + private:
|
---|
| 191 | + unsigned int a;
|
---|
| 192 | + unsigned int c;
|
---|
| 193 | + unsigned int m;
|
---|
| 194 | + unsigned int *pseed;
|
---|
| 195 | +
|
---|
| 196 | + public:
|
---|
| 197 | +
|
---|
| 198 | + /*constructors, destructors: */
|
---|
| 199 | + linear_congruential_engine();
|
---|
| 200 | + linear_congruential_engine(unsigned int _a, unsigned int _b, unsigned int _m);
|
---|
| 201 | + ~linear_congruential_engine();
|
---|
| 202 | +
|
---|
| 203 | + unsigned int get_m();
|
---|
| 204 | + void seed( int s );
|
---|
| 205 | + unsigned int generator();
|
---|
| 206 | +
|
---|
| 207 | + };
|
---|
| 208 | +
|
---|
| 209 | class uniform_distribution
|
---|
| 210 | {
|
---|
| 211 |
|
---|
| 212 | private:
|
---|
| 213 | - int a;
|
---|
| 214 | - int c;
|
---|
| 215 | - unsigned int m;
|
---|
| 216 | - unsigned _seed;
|
---|
| 217 | - double lbound;
|
---|
| 218 | - double ubound;
|
---|
| 219 | + double a; // lower bound of range
|
---|
| 220 | + double b; // upper bound of range
|
---|
| 221 |
|
---|
| 222 | public:
|
---|
| 223 |
|
---|
| 224 | /*constructors, destructors: */
|
---|
| 225 | uniform_distribution();
|
---|
| 226 | - uniform_distribution(double a_1,double a_2);
|
---|
| 227 | + uniform_distribution(double _a,double _b);
|
---|
| 228 | ~uniform_distribution();
|
---|
| 229 |
|
---|
| 230 | - void seed( unsigned int s );
|
---|
| 231 | - unsigned int get_seed();
|
---|
| 232 | - double generator();
|
---|
| 233 | + double generator(rnd::linear_congruential_engine random_engine);
|
---|
| 234 |
|
---|
| 235 | };
|
---|
| 236 |
|
---|
| 237 | @@ -38,7 +53,6 @@
|
---|
| 238 | {
|
---|
| 239 |
|
---|
| 240 | private:
|
---|
| 241 | - unsigned _seed;
|
---|
| 242 | double mean;
|
---|
| 243 | double sdev;
|
---|
| 244 |
|
---|
| 245 | @@ -49,11 +63,11 @@
|
---|
| 246 | normal_distribution(double m,double s);
|
---|
| 247 | ~normal_distribution();
|
---|
| 248 |
|
---|
| 249 | - void seed( unsigned int s );
|
---|
| 250 | - double generator();
|
---|
| 251 | + double generator(rnd::linear_congruential_engine random_engine);
|
---|
| 252 |
|
---|
| 253 | };
|
---|
| 254 |
|
---|
| 255 | +
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | -#endif //ifndef _RANDOMGENERATOR_H_
|
---|
| 259 | +#endif //* _RANDOMGENERATOR_H_ */
|
---|