3 |
|
|
4 |
|
#include "randomSPRNG.hpp" |
5 |
|
#include "simError.h" |
6 |
+ |
#include "sprng.h" |
7 |
|
|
8 |
+ |
#ifdef IS_MPI |
9 |
+ |
#include "mpiSimulation.hpp" |
10 |
+ |
#endif |
11 |
|
|
12 |
|
/* randomStreamSPRNF creates a new SPRNG stream for random numbers |
13 |
|
*/ |
14 |
|
|
15 |
< |
int ranStreamSPRNG::nSPRNGStreams; |
15 |
> |
int randomSPRNG::nStreamsInitialized = 0; |
16 |
|
|
17 |
< |
randomStreamSPRNG::randomStreamSPRNG(){ |
17 |
> |
randomSPRNG::randomSPRNG(int iseed){ |
18 |
> |
int newSeed; |
19 |
> |
nStreamsInitialized++; |
20 |
> |
newSeed = abs(iseed) + nStreamsInitialized; |
21 |
> |
if( newSeed < 0 ) newSeed = abs( newSeed ); |
22 |
> |
|
23 |
|
#ifdef IS_MPI |
24 |
|
|
25 |
+ |
nSPRNGStreams = mpiSim->getNumberProcessors(); |
26 |
+ |
|
27 |
+ |
myStreamNumber = mpiSim->getMyNode(); |
28 |
+ |
|
29 |
|
|
30 |
|
|
31 |
|
#else |
19 |
– |
nSPRNGStreams++; |
32 |
|
|
33 |
< |
myStreamNumber = nSPRNGStreams; |
33 |
> |
nSPRNGStreams = 1; |
34 |
> |
myStreamNumber = nStreamsInitialized; |
35 |
|
|
36 |
< |
thisStream = init_sprng(GTYPE,streamnum,nstreams,SEED,SPRNG_DEFAULT); |
36 |
> |
#endif |
37 |
|
|
38 |
+ |
thisStream = init_sprng(GTYPE,myStreamNumber,nSPRNGStreams, |
39 |
+ |
newSeed,SPRNG_DEFAULT); |
40 |
+ |
} |
41 |
|
|
42 |
< |
#endif |
42 |
> |
randomSPRNG::~randomSPRNG(){ |
43 |
> |
if ( thisStream != NULL){ |
44 |
> |
// free_sprng(*thisStream); |
45 |
> |
nStreamsInitialized--; |
46 |
> |
} |
47 |
|
} |
48 |
|
|
29 |
– |
randomStreamSPRNG::~randomStreamSPRNG(){ |
49 |
|
|
50 |
< |
free_sprng(int *thisStream); |
51 |
< |
nSPRNGStreams--; |
50 |
> |
double randomSPRNG::getRandom(){ |
51 |
> |
return sprng(thisStream); |
52 |
> |
} |
53 |
|
|
54 |
|
|
55 |
< |
} |
55 |
> |
// Gaussian SPRNG class... |
56 |
|
|
57 |
+ |
double gaussianSPRNG::getGaussian(){ |
58 |
+ |
double ranNum1; |
59 |
+ |
double ranNum2; |
60 |
+ |
double gaussianNumber; |
61 |
|
|
62 |
< |
double getRanStreamSPRNG(){ |
63 |
< |
double ranNum; |
64 |
< |
nanNum = sprng(thisStream); |
65 |
< |
return ranNum; |
62 |
> |
ranNum1 = getRandom(); |
63 |
> |
ranNum2 = getRandom(); |
64 |
> |
|
65 |
> |
gaussianNumber = sqrt(-2.0 * log(ranNum1)) * cos(2 * M_PI * ranNum2); |
66 |
> |
|
67 |
> |
return gaussianNumber; |
68 |
|
} |