source:
issm/oecreview/Archive/25834-26739/ISSM-26577-26578.diff
Last change on this file was 26740, checked in by , 3 years ago | |
---|---|
File size: 7.6 KB |
-
../trunk-jpl/src/c/shared/Random/randomgenerator.cpp
Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
8 8 #undef M_PI 9 9 #define M_PI 3.141592653589793238462643 10 10 11 namespace rdngen{ 12 uniform_distribution_rnd::uniform_distribution_rnd(){/*{{{*/ 11 13 12 uniform_distribution_rnd::uniform_distribution_rnd(){/*{{{*/ 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){/*{{{*/ 13 24 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){/*{{{*/ 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 } 24 41 25 a = 1103515245; // BSD Formula26 c = 12345; // BSD Formula27 m = 2147483648; // BSD Formula28 _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(){/*{{{*/ 42 44 43 normal_distribution_rnd::normal_distribution_rnd(){/*{{{*/ 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){/*{{{*/ 44 52 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){/*{{{*/ 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(){/*{{{*/ 52 62 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(){/*{{{*/ 63 uniform_distribution_rnd unifdistri; 64 unifdistri.seed(_seed); 62 65 63 uniform_distribution_rnd unifdistri;64 unifdistri.seed(_seed);66 double u1 = unifdistri.generator(); 67 double u2 = unifdistri.generator(); 65 68 66 double u1 = unifdistri.generator();67 double u2 = unifdistri.generator();69 double R = sqrt(-2*log(u1)); 70 double theta = 2*M_PI*u2; 68 71 69 double R = sqrt(-2*log(u1)); 70 double theta = 2*M_PI*u2; 72 seed(unifdistri.get_seed()); 71 73 72 seed(unifdistri.get_seed());74 return mean + sdev * (R*cos(theta)); 73 75 74 return mean + sdev * (R*cos(theta)); 75 76 } 77 /*}}}*/ 76 } 77 /*}}}*/ 78 } -
../trunk-jpl/src/c/shared/Random/randomgenerator.h
8 8 #undef M_PI 9 9 #define M_PI 3.141592653589793238462643 10 10 11 class uniform_distribution_rnd 12 { 11 namespace rdngen{ 12 class uniform_distribution_rnd 13 { 13 14 14 private:15 private: 15 16 16 unsigned int a; //multiplier of the linear congruential generator17 unsigned int c; //increment of the linear congruential generator18 unsigned int m; // modulo of the linear congruential generator19 unsigned int _seed; // seed value20 double lbound; // lower bound of uniform distribution21 double ubound; // upper bound of uniform distribution17 unsigned int a; //multiplier of the linear congruential generator 18 unsigned int c; //increment of the linear congruential generator 19 unsigned int m; // modulo of the linear congruential generator 20 unsigned int _seed; // seed value 21 double lbound; // lower bound of uniform distribution 22 double ubound; // upper bound of uniform distribution 22 23 23 public:24 public: 24 25 25 /*constructors, destructors: */26 uniform_distribution_rnd();27 uniform_distribution_rnd(double a_1, double a_2);28 ~uniform_distribution_rnd();26 /*constructors, destructors: */ 27 uniform_distribution_rnd(); 28 uniform_distribution_rnd(double a_1, double a_2); 29 ~uniform_distribution_rnd(); 29 30 30 void seed( unsigned int s );31 unsigned int get_seed();32 double generator();31 void seed( unsigned int s ); 32 unsigned int get_seed(); 33 double generator(); 33 34 34 };35 }; 35 36 36 class normal_distribution_rnd37 {37 class normal_distribution_rnd 38 { 38 39 39 private:40 unsigned int _seed; // seed value41 double mean; // mean value42 double sdev; // standard deviation40 private: 41 unsigned int _seed; // seed value 42 double mean; // mean value 43 double sdev; // standard deviation 43 44 44 public:45 public: 45 46 46 /*constructors, destructors: */47 normal_distribution_rnd();48 normal_distribution_rnd(double m,double s);49 ~normal_distribution_rnd();47 /*constructors, destructors: */ 48 normal_distribution_rnd(); 49 normal_distribution_rnd(double m,double s); 50 ~normal_distribution_rnd(); 50 51 51 void seed( unsigned int s );52 double generator();52 void seed( unsigned int s ); 53 double generator(); 53 54 54 };55 }; 55 56 57 } 56 58 #endif //ifndef _RANDOMGENERATOR_H_ -
../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp
17 17 double rdnumber; 18 18 19 19 /*Define seed*/ 20 //normal_distribution_rnd distribution;20 rdngen::normal_distribution_rnd distribution; 21 21 if(seed<0){ 22 22 std::random_device rd; 23 23 seed = rd(); … … 28 28 ISSM_MPI_Comm_rank(ISSM_MPI_COMM_WORLD,&my_rank); 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; 34 34 IssmDouble *local_vector = NULL; … … 37 37 int M; 38 38 ppf->GetLocalSize(&M); 39 39 for(int i=0;i<M;i++){ 40 rdnumber = 1.0;//distribution.rnd();40 rdnumber = distribution.generator(); 41 41 ppf->SetValue(local_indices[i],rdnumber,INS_VAL); 42 42 } 43 43 ppf->Assemble();
Note:
See TracBrowser
for help on using the repository browser.