Changeset 26514


Ignore:
Timestamp:
10/27/21 21:43:46 (3 years ago)
Author:
bulthuis
Message:

CHG: Implement a pseudo-random number generator for Gaussian variables in solutionsequence_sampling.cpp

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/solutionsequences/solutionsequence_sampling.cpp

    r26512 r26514  
    1010#include <random>
    1111
     12#undef M_PI
     13#define M_PI 3.141592653589793238462643
     14
     15double lcg(long long* seed){/*{{{*/
     16
     17
     18        int N = 5;
     19        long long M = 1LL<<32;
     20        long long a = 1812433253;
     21        long long c = 1;
     22
     23        for(int i=0;i<N;i++){
     24                *seed = a * *seed + c;
     25                *seed = *seed%M;
     26        }
     27
     28  return ((double) *seed) / ((double) M);
     29
     30}
     31
     32double BoxMuller(long long* pseed){
     33
     34        double u1 = lcg(pseed);
     35        double u2 = lcg(pseed);
     36
     37        double R = sqrt(-2*log(u1));
     38        double theta = 2*M_PI*u2;
     39
     40        return R*cos(theta);
     41
     42}
     43
    1244void GaussianVector(Vector<IssmDouble>* ppf,int seed){/*{{{*/
     45
    1346
    1447        /*Intermediaries*/
    1548        double      rdnumber;
    1649
    17         /*Define random number generator*/
    18         std::normal_distribution<double> distribution(0.0,1.0);  // Define probability distribution as the standard normal distribution
     50        /*Define seed*/
    1951        if(seed<0){
    2052                std::random_device rd;
     
    2759                seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number)
    2860        }
    29         //std::default_random_engine generator(seed);
    30         std::mt19937 generator(seed);
     61
     62        long long seedbis;
     63        seedbis  = (long long) seed;
    3164
    3265        int        *local_indices = NULL;
     
    3770        ppf->GetLocalSize(&M);
    3871        for(int i=0;i<M;i++){
    39                 rdnumber = distribution(generator);
     72                rdnumber = BoxMuller(&seedbis);
    4073                ppf->SetValue(local_indices[i],rdnumber,INS_VAL);
    4174        }
Note: See TracChangeset for help on using the changeset viewer.