source: issm/oecreview/Archive/25834-26739/ISSM-26611-26612.diff

Last change on this file was 26740, checked in by Mathieu Morlighem, 3 years ago

CHG: added 25834-26739

File size: 6.9 KB
  • ../trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp

     
    88#include "../modules/modules.h"
    99#include "../analyses/analyses.h"
    1010#include "../shared/Random/randomgenerator.h"
    11 #include <random>
     11//#include <random>
    1212
    1313void GaussianVector(Vector<IssmDouble>* ppf,int seed){/*{{{*/
    1414
     
    1717        double      rdnumber;
    1818
    1919        /*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)
    2622        {
    2723                int my_rank;
    2824                ISSM_MPI_Comm_rank(ISSM_MPI_COMM_WORLD,&my_rank);
    2925                seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number)
    3026        }
    31         distribution.seed(seed);
     27        random_engine.seed(seed);
    3228
     29        /* Define univariate distribution */
     30
     31        rnd::normal_distribution distribution(0.0,1.0);
     32
    3333        int        *local_indices = NULL;
    3434        IssmDouble *local_vector = NULL;
    3535        ppf->GetLocalVector(&local_vector,&local_indices);
     
    3737  int M;
    3838        ppf->GetLocalSize(&M);
    3939        for(int i=0;i<M;i++){
    40                 rdnumber = distribution.generator();
     40                rdnumber = distribution.generator(random_engine);
    4141                ppf->SetValue(local_indices[i],rdnumber,INS_VAL);
    4242        }
    4343        ppf->Assemble();
  • ../trunk-jpl/src/c/shared/Random/randomgenerator.cpp

     
    1212
    1313namespace rnd{
    1414
    15         uniform_distribution::uniform_distribution(){/*{{{*/
     15  /* Linear congruential engine */
    1616
    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
    2324                        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;
    2459        }
    2560        /*}}}*/
    26         uniform_distribution::uniform_distribution(double lower,double upper){/*{{{*/
    2761
    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;
    3465                        return;
    3566        }
    3667        /*}}}*/
     68
    3769        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;
    4373        }
    4474
     75  /* Normal distribution */
    4576
    4677        normal_distribution::normal_distribution(){/*{{{*/
    47 
    48                         _seed = 0;
    4978                        mean   = 0;
    5079                        sdev  = 1.0;
    5180                        return;
    5281        }
    5382        /*}}}*/
     83
    5484        normal_distribution::normal_distribution(double m,double s){/*{{{*/
    55 
    56                         _seed = 0;
    5785                        mean   = m;
    5886                        sdev  = s;
    5987                        return;
    6088        }
    61                 /*}}}*/
     89        /*}}}*/
     90
    6291        normal_distribution::~normal_distribution(){}
    63         void normal_distribution::seed( unsigned int s ) { _seed = s; }
    64         double normal_distribution::generator(){/*{{{*/
    6592
     93        double normal_distribution::generator(rnd::linear_congruential_engine random_engine){/*{{{*/
     94
    6695                        rnd::uniform_distribution       unifdistri;
    67                         unifdistri.seed(_seed);
    6896
    69                         double u1 = unifdistri.generator();
    70                         double u2 = unifdistri.generator();
     97                        double u1 = unifdistri.generator(random_engine);
     98                        double u2 = unifdistri.generator(random_engine);
    7199
    72100                        double R = sqrt(-2*log(u1));
    73101                        double theta = 2*M_PI*u2;
    74102
    75                         seed(unifdistri.get_seed());
    76 
    77103                        return mean + sdev * (R*cos(theta));
    78104
    79105        }
  • ../trunk-jpl/src/c/shared/Random/randomgenerator.h

     
    1010
    1111namespace rnd{
    1212
     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
    1334  class uniform_distribution
    1435  {
    1536
    1637    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
    2340
    2441    public:
    2542
    2643      /*constructors, destructors: */
    2744      uniform_distribution();
    28       uniform_distribution(double a_1,double a_2);
     45      uniform_distribution(double _a,double _b);
    2946      ~uniform_distribution();
    3047
    31       void seed( unsigned int s );
    32       unsigned int get_seed();
    33       double generator();
     48      double generator(rnd::linear_congruential_engine random_engine);
    3449
    3550  };
    3651
     
    3853  {
    3954
    4055    private:
    41       unsigned _seed;
    4256      double mean;
    4357      double sdev;
    4458
     
    4963      normal_distribution(double m,double s);
    5064      ~normal_distribution();
    5165
    52       void seed( unsigned int s );
    53       double generator();
     66      double generator(rnd::linear_congruential_engine random_engine);
    5467
    5568  };
    5669
     70
    5771}
    5872
    59 #endif //ifndef _RANDOMGENERATOR_H_
     73#endif //* _RANDOMGENERATOR_H_ */
Note: See TracBrowser for help on using the repository browser.