source:
issm/oecreview/Archive/25834-26739/ISSM-26611-26612.diff
Last change on this file was 26740, checked in by , 3 years ago | |
---|---|
File size: 6.9 KB |
-
../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp
8 8 #include "../modules/modules.h" 9 9 #include "../analyses/analyses.h" 10 10 #include "../shared/Random/randomgenerator.h" 11 #include <random>11 //#include <random> 12 12 13 13 void GaussianVector(Vector<IssmDouble>* ppf,int seed){/*{{{*/ 14 14 … … 17 17 double rdnumber; 18 18 19 19 /*Define seed*/ 20 rnd::normal_distribution distribution; 21 if(seed<0){ 22 std::random_device rd; 23 seed = rd(); 24 } 25 else 20 rnd::linear_congruential_engine random_engine; 21 if(seed>=0) 26 22 { 27 23 int my_rank; 28 24 ISSM_MPI_Comm_rank(ISSM_MPI_COMM_WORLD,&my_rank); 29 25 seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number) 30 26 } 31 distribution.seed(seed);27 random_engine.seed(seed); 32 28 29 /* Define univariate distribution */ 30 31 rnd::normal_distribution distribution(0.0,1.0); 32 33 33 int *local_indices = NULL; 34 34 IssmDouble *local_vector = NULL; 35 35 ppf->GetLocalVector(&local_vector,&local_indices); … … 37 37 int M; 38 38 ppf->GetLocalSize(&M); 39 39 for(int i=0;i<M;i++){ 40 rdnumber = distribution.generator( );40 rdnumber = distribution.generator(random_engine); 41 41 ppf->SetValue(local_indices[i],rdnumber,INS_VAL); 42 42 } 43 43 ppf->Assemble(); -
../trunk-jpl/src/c/shared/Random/randomgenerator.cpp
12 12 13 13 namespace rnd{ 14 14 15 uniform_distribution::uniform_distribution(){/*{{{*/15 /* Linear congruential engine */ 16 16 17 a = 1103515245; // BSD Formula 18 c = 12345; // BSD Formula 19 m = 2147483648; // BSD Formula 20 _seed = 0; 21 lbound = 0.0; 22 ubound = 1.0; 17 linear_congruential_engine::linear_congruential_engine(){/*{{{*/ 18 19 pseed = new unsigned int; 20 *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1); 21 a = 1103515245; // BSD Formula 22 c = 12345; // BSD Formula 23 m = 2147483648; // BSD Formula 23 24 return; 25 }/*}}}*/ 26 27 linear_congruential_engine::linear_congruential_engine(unsigned int _a, unsigned int _b, unsigned int _m){/*{{{*/ 28 29 pseed = new unsigned int; 30 *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1); 31 a = _a; 32 c = _b; 33 m = _m; 34 return; 35 }/*}}}*/ 36 37 linear_congruential_engine::~linear_congruential_engine(){} 38 39 unsigned int linear_congruential_engine::get_m() { return m; } 40 41 void linear_congruential_engine::seed( int s ) { 42 if(s<0) 43 *pseed = std::chrono::steady_clock::now().time_since_epoch()/std::chrono::milliseconds(1); 44 else 45 *pseed = (unsigned) s; 46 } 47 48 unsigned int linear_congruential_engine::generator(){ 49 *pseed = ( a * *pseed + c ) % m ; 50 return *pseed; 51 } 52 53 /* Uniform distribution */ 54 55 uniform_distribution::uniform_distribution(){/*{{{*/ 56 a = 0.0; 57 b = 1.0; 58 return; 24 59 } 25 60 /*}}}*/ 26 uniform_distribution::uniform_distribution(double lower,double upper){/*{{{*/27 61 28 a = 1103515245; // BSD Formula 29 c = 12345; // BSD Formula 30 m = 2147483648; // BSD Formula 31 _seed = 0; 32 lbound = lower; 33 ubound = upper; 62 uniform_distribution::uniform_distribution(double _a,double _b){/*{{{*/ 63 a = _a; 64 b = _b; 34 65 return; 35 66 } 36 67 /*}}}*/ 68 37 69 uniform_distribution::~uniform_distribution(){} 38 void uniform_distribution::seed( unsigned int s ) { _seed = s; } 39 unsigned int uniform_distribution::get_seed() { return _seed; } 40 double uniform_distribution::generator() { 41 _seed = ( a * _seed + c ) % m ; 42 return (ubound-lbound)*(double) _seed/ m + lbound; 70 71 double uniform_distribution::generator(rnd::linear_congruential_engine random_engine) { 72 return (b-a)*(double) random_engine.generator()/ random_engine.get_m() + a; 43 73 } 44 74 75 /* Normal distribution */ 45 76 46 77 normal_distribution::normal_distribution(){/*{{{*/ 47 48 _seed = 0;49 78 mean = 0; 50 79 sdev = 1.0; 51 80 return; 52 81 } 53 82 /*}}}*/ 83 54 84 normal_distribution::normal_distribution(double m,double s){/*{{{*/ 55 56 _seed = 0;57 85 mean = m; 58 86 sdev = s; 59 87 return; 60 88 } 61 /*}}}*/ 89 /*}}}*/ 90 62 91 normal_distribution::~normal_distribution(){} 63 void normal_distribution::seed( unsigned int s ) { _seed = s; }64 double normal_distribution::generator(){/*{{{*/65 92 93 double normal_distribution::generator(rnd::linear_congruential_engine random_engine){/*{{{*/ 94 66 95 rnd::uniform_distribution unifdistri; 67 unifdistri.seed(_seed);68 96 69 double u1 = unifdistri.generator( );70 double u2 = unifdistri.generator( );97 double u1 = unifdistri.generator(random_engine); 98 double u2 = unifdistri.generator(random_engine); 71 99 72 100 double R = sqrt(-2*log(u1)); 73 101 double theta = 2*M_PI*u2; 74 102 75 seed(unifdistri.get_seed());76 77 103 return mean + sdev * (R*cos(theta)); 78 104 79 105 } -
../trunk-jpl/src/c/shared/Random/randomgenerator.h
10 10 11 11 namespace rnd{ 12 12 13 class linear_congruential_engine 14 { 15 private: 16 unsigned int a; 17 unsigned int c; 18 unsigned int m; 19 unsigned int *pseed; 20 21 public: 22 23 /*constructors, destructors: */ 24 linear_congruential_engine(); 25 linear_congruential_engine(unsigned int _a, unsigned int _b, unsigned int _m); 26 ~linear_congruential_engine(); 27 28 unsigned int get_m(); 29 void seed( int s ); 30 unsigned int generator(); 31 32 }; 33 13 34 class uniform_distribution 14 35 { 15 36 16 37 private: 17 int a; 18 int c; 19 unsigned int m; 20 unsigned _seed; 21 double lbound; 22 double ubound; 38 double a; // lower bound of range 39 double b; // upper bound of range 23 40 24 41 public: 25 42 26 43 /*constructors, destructors: */ 27 44 uniform_distribution(); 28 uniform_distribution(double a_1,double a_2);45 uniform_distribution(double _a,double _b); 29 46 ~uniform_distribution(); 30 47 31 void seed( unsigned int s ); 32 unsigned int get_seed(); 33 double generator(); 48 double generator(rnd::linear_congruential_engine random_engine); 34 49 35 50 }; 36 51 … … 38 53 { 39 54 40 55 private: 41 unsigned _seed;42 56 double mean; 43 57 double sdev; 44 58 … … 49 63 normal_distribution(double m,double s); 50 64 ~normal_distribution(); 51 65 52 void seed( unsigned int s ); 53 double generator(); 66 double generator(rnd::linear_congruential_engine random_engine); 54 67 55 68 }; 56 69 70 57 71 } 58 72 59 #endif // ifndef _RANDOMGENERATOR_H_73 #endif //* _RANDOMGENERATOR_H_ */
Note:
See TracBrowser
for help on using the repository browser.