Changeset 26554


Ignore:
Timestamp:
11/08/21 14:41:51 (3 years ago)
Author:
bulthuis
Message:

NEW: add pseudo-random number generators + use in solutionsequence_sampling.cpp

Location:
issm/trunk-jpl
Files:
2 added
2 edited

Legend:

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

    r26514 r26554  
    88#include "../modules/modules.h"
    99#include "../analyses/analyses.h"
     10#include "../shared/Random/randomgenerator.h"
    1011#include <random>
    11 
    12 #undef M_PI
    13 #define M_PI 3.141592653589793238462643
    14 
    15 double 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 
    32 double 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 }
    4312
    4413void GaussianVector(Vector<IssmDouble>* ppf,int seed){/*{{{*/
     
    4918
    5019        /*Define seed*/
     20        normal_distribution_rnd distribution;
    5121        if(seed<0){
    5222                std::random_device rd;
     
    5929                seed = seed + 783728*my_rank; // change default seed for parallel simulations (by considering an arbitrary shif based on the rank number)
    6030        }
    61 
    62         long long seedbis;
    63         seedbis  = (long long) seed;
     31        distribution.seed(seed);
    6432
    6533        int        *local_indices = NULL;
     
    7038        ppf->GetLocalSize(&M);
    7139        for(int i=0;i<M;i++){
    72                 rdnumber = BoxMuller(&seedbis);
     40                rdnumber = distribution.rnd();
    7341                ppf->SetValue(local_indices[i],rdnumber,INS_VAL);
    7442        }
Note: See TracChangeset for help on using the changeset viewer.