1 |
|
#include <iostream> |
2 |
|
#include <math.h> |
3 |
|
|
4 |
< |
#include "randomSPRNG.hpp" |
5 |
< |
#include "simError.h" |
4 |
> |
#include "math/randomSPRNG.hpp" |
5 |
> |
#include "utils/simError.h" |
6 |
|
#include <sprng.h> |
7 |
|
|
8 |
|
#ifdef IS_MPI |
9 |
< |
#include "mpiSimulation.hpp" |
9 |
> |
|
10 |
> |
#include <mpi.h> |
11 |
|
#endif |
12 |
|
|
13 |
|
using namespace std; |
17 |
|
|
18 |
|
int randomSPRNG::nStreamsInitialized = 0; |
19 |
|
|
20 |
< |
randomSPRNG::randomSPRNG(int iseed){ |
21 |
< |
int newSeed; |
22 |
< |
nStreamsInitialized++; |
23 |
< |
newSeed = abs(iseed) + nStreamsInitialized; |
23 |
< |
if( newSeed < 0 ) newSeed = abs( newSeed ); |
20 |
> |
randomSPRNG::randomSPRNG( int iseed ) { |
21 |
> |
int newSeed; |
22 |
> |
nStreamsInitialized++; |
23 |
> |
newSeed = abs(iseed) + nStreamsInitialized; |
24 |
|
|
25 |
+ |
if (newSeed < 0) |
26 |
+ |
newSeed = abs(newSeed); |
27 |
+ |
|
28 |
|
#ifdef IS_MPI |
29 |
|
|
30 |
< |
nSPRNGStreams = mpiSim->getNProcessors(); |
31 |
< |
|
32 |
< |
myStreamNumber = mpiSim->getMyNode(); |
30 |
> |
//random number generator should not depend on MpiSimulation |
31 |
> |
MPI_Comm_size(MPI_COMM_WORLD, &nSPRNGStreams); |
32 |
> |
myStreamNumber = worldRank; |
33 |
|
|
31 |
– |
|
32 |
– |
|
34 |
|
#else |
35 |
|
|
36 |
< |
nSPRNGStreams = 1; |
37 |
< |
myStreamNumber = 0; |
36 |
> |
nSPRNGStreams = 1; |
37 |
> |
myStreamNumber = 0; |
38 |
|
|
39 |
|
#endif |
40 |
|
|
41 |
< |
|
42 |
< |
thisStream = init_sprng(GTYPE,myStreamNumber,nSPRNGStreams, |
42 |
< |
newSeed,SPRNG_DEFAULT); |
41 |
> |
thisStream = init_sprng(GTYPE, myStreamNumber, nSPRNGStreams, newSeed, |
42 |
> |
SPRNG_DEFAULT); |
43 |
|
} |
44 |
|
|
45 |
< |
randomSPRNG::~randomSPRNG(){ |
46 |
< |
if ( thisStream != NULL){ |
47 |
< |
free_sprng(thisStream); |
48 |
< |
nStreamsInitialized--; |
49 |
< |
} |
45 |
> |
randomSPRNG::~randomSPRNG() { |
46 |
> |
if (thisStream != NULL) { |
47 |
> |
free_sprng(thisStream); |
48 |
> |
nStreamsInitialized--; |
49 |
> |
} |
50 |
|
} |
51 |
|
|
52 |
+ |
double randomSPRNG::getRandom() { return sprng(thisStream); } |
53 |
|
|
53 |
– |
double randomSPRNG::getRandom(){ |
54 |
– |
return sprng(thisStream); |
55 |
– |
} |
56 |
– |
|
57 |
– |
|
54 |
|
// Gaussian SPRNG class... |
55 |
|
|
56 |
< |
double gaussianSPRNG::getGaussian(){ |
57 |
< |
double ranNum1; |
58 |
< |
double ranNum2; |
59 |
< |
double gaussianNumber; |
56 |
> |
double gaussianSPRNG::getGaussian() { |
57 |
> |
double ranNum1; |
58 |
> |
double ranNum2; |
59 |
> |
double gaussianNumber; |
60 |
|
|
61 |
< |
ranNum1 = getRandom(); |
62 |
< |
ranNum2 = getRandom(); |
61 |
> |
ranNum1 = getRandom(); |
62 |
> |
ranNum2 = getRandom(); |
63 |
|
|
64 |
< |
gaussianNumber = sqrt(-2.0 * log(ranNum1)) * cos(2 * M_PI * ranNum2); |
64 |
> |
gaussianNumber = sqrt(-2.0 * log(ranNum1)) * cos(2 * M_PI * ranNum2); |
65 |
|
|
66 |
< |
return gaussianNumber; |
66 |
> |
return gaussianNumber; |
67 |
|
} |