Changeset 26576
- Timestamp:
- 11/09/21 11:50:43 (3 years ago)
- Location:
- issm/trunk-jpl
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Random/randomgenerator.cpp
r26575 r26576 6 6 #include "./randomgenerator.h" 7 7 8 #undef M_PI 9 #define M_PI 3.141592653589793238462643 8 class uniform_distribution 9 { 10 10 11 }; 11 12 12 uniform_distribution_rnd::uniform_distribution_rnd(){/*{{{*/ 13 class normal_distribution 14 { 13 15 14 a = 1103515245; // BSD Formula 15 c = 12345; // BSD Formula 16 m = 2147483648; // BSD Formula 17 _seed = 0; 18 lbound = 0.0; 19 ubound = 1.0; 20 return; 21 } 22 /*}}}*/ 23 uniform_distribution_rnd::uniform_distribution_rnd(double lower,double upper){/*{{{*/ 24 25 a = 1103515245; // BSD Formula 26 c = 12345; // BSD Formula 27 m = 2147483648; // BSD Formula 28 _seed = 0; 29 lbound = lower; 30 ubound = upper; 31 return; 32 } 33 /*}}}*/ 34 uniform_distribution_rnd::~uniform_distribution_rnd(){} 35 void uniform_distribution_rnd::seed( unsigned int s ) { _seed = s; } 36 unsigned int uniform_distribution_rnd::get_seed() { return _seed; } 37 double uniform_distribution_rnd::generator() { 38 _seed = ( a * _seed + c ) % m ; 39 return (ubound-lbound)*(double) _seed/ m + lbound; 40 } 41 42 43 normal_distribution_rnd::normal_distribution_rnd(){/*{{{*/ 44 45 _seed = 0; 46 mean = 0; 47 sdev = 1.0; 48 return; 49 } 50 /*}}}*/ 51 normal_distribution_rnd::normal_distribution_rnd(double m,double s){/*{{{*/ 52 53 _seed = 0; 54 mean = m; 55 sdev = s; 56 return; 57 } 58 /*}}}*/ 59 normal_distribution_rnd::~normal_distribution_rnd(){} 60 void normal_distribution_rnd::seed( unsigned int s ) { _seed = s; } 61 double normal_distribution_rnd::generator(){/*{{{*/ 62 63 uniform_distribution_rnd unifdistri; 64 unifdistri.seed(_seed); 65 66 double u1 = unifdistri.generator(); 67 double u2 = unifdistri.generator(); 68 69 double R = sqrt(-2*log(u1)); 70 double theta = 2*M_PI*u2; 71 72 seed(unifdistri.get_seed()); 73 74 return mean + sdev * (R*cos(theta)); 75 76 } 77 /*}}}*/ 16 }; -
issm/trunk-jpl/src/c/shared/Random/randomgenerator.h
r26575 r26576 6 6 #define _RANDOMGENERATOR_H_ 7 7 8 #undef M_PI 9 #define M_PI 3.141592653589793238462643 10 8 11 class uniform_distribution_rnd 9 12 { 10 13 11 14 private: 12 15 13 unsigned int a; //multiplier of the linear congruential generator14 unsigned int c; //increment of the linear congruential generator15 unsigned int m; // modulo of the linear congruential generator16 unsigned int _seed; // seed value17 double lbound; // lower bound of uniform distribution18 double ubound; // upper bound of uniform distribution16 unsigned int a; 17 unsigned int c; 18 unsigned int m; 19 unsigned _seed; 20 double a1; 21 double a2; 19 22 20 public:23 int drnd() { return( _seed = ( a * _seed + c ) % m ); } 21 24 22 /*constructors, destructors: */ 23 uniform_distribution_rnd(); 24 uniform_distribution_rnd(double a_1, double a_2); 25 ~uniform_distribution_rnd(); 25 public: 26 26 27 void seed( unsigned int s ); 28 unsigned int get_seed(); 29 double generator(); 27 /*constructors, destructors: */ 28 uniform_distribution_rnd(); 29 uniform_distribution_rnd(double a_1, double a_2); 30 ~uniform_distribution_rnd(); 31 32 void seed( unsigned int s ) { _seed = s; } 33 unsigned int get_seed() { return _seed; } 34 double rnd() { return (a2-a1)*(double) drnd()/ m + a1; } 30 35 31 36 }; … … 34 39 { 35 40 36 37 unsigned int _seed; // seed value38 double mean; // mean value39 double sdev; // standard deviation41 private: 42 unsigned _seed; 43 double mean; 44 double sdev; 40 45 41 public: 42 43 /*constructors, destructors: */ 44 normal_distribution_rnd(); 45 normal_distribution_rnd(double m,double s); 46 ~normal_distribution_rnd(); 47 48 void seed( unsigned int s ); 49 double generator(); 46 public: 47 normal_distribution_rnd() : _seed( 0 ), mean( 0), sdev(1.0) {} 48 normal_distribution_rnd(double m,double s) : _seed( 0 ), mean( m ), sdev(s) {} 49 void seed( unsigned int s ) { _seed = s; } 50 double rnd(); 50 51 51 52 }; -
issm/trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp
r26575 r26576 18 18 19 19 /*Define seed*/ 20 normal_distribution_rnd distribution;20 //normal_distribution_rnd distribution; 21 21 if(seed<0){ 22 22 std::random_device rd; … … 29 29 seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number) 30 30 } 31 distribution.seed(seed);31 //distribution.seed(seed); 32 32 33 33 int *local_indices = NULL; … … 38 38 ppf->GetLocalSize(&M); 39 39 for(int i=0;i<M;i++){ 40 rdnumber = distribution.generator();40 rdnumber = 1.0;//distribution.rnd(); 41 41 ppf->SetValue(local_indices[i],rdnumber,INS_VAL); 42 42 }
Note:
See TracChangeset
for help on using the changeset viewer.