-
Notifications
You must be signed in to change notification settings - Fork 9
/
mc_randg.cc
executable file
·174 lines (145 loc) · 3.9 KB
/
mc_randg.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cmath>
#include <stdio.h>
//Toby set SEED as 985456376 + rank to make different SEEDs for different CPUs
//#define SEED 985456376
#include "sprng.h"
#include "mc_input.h"
#include "mc_randg.h"
#include "mc_utils.h"
#include "mc_setup.h"
const int MAXGENS=15; // max number of rnd generators [gauss #8,9 N,M #>=10]
int *STREAM[MAXGENS]; // rnd numbers streams
void RandomInit(int mpi_id, int max_mpi_procs) // sprng init
{
int max_streams=MAXGENS*max_mpi_procs; // number of streams
for (int ip=0;ip<MAXGENS;ip++)
{
int streamnum = mpi_id*MAXGENS+ip;
STREAM[ip] = init_sprng(streamnum,max_streams,SEED,SPRNG_DEFAULT); // initialize stream
// print_sprng(stream);
}
}
void RandomFree(void) // free memory used by sprng
{
for (int ip=0;ip<MAXGENS;ip++)
free_sprng(STREAM[ip]);
}
void RandomIO(int tstatus, const char file_name[]) // check point for sprng streams
{
const char *_proc_=__func__; // "RandIO()";
const char *fop="r"; // file operation
switch (tstatus)
{
case IOWrite: fop="w"; break;
case IORead : fop="r"; break;
default :
nrerror (_proc_,IO_ERR_WMODE);
break;
}
FILE *fp = fopen(file_name,fop);
if(fp == NULL)
_io_error(_proc_,IO_ERR_FOPEN,file_name);
#ifdef USE_MPI
not implemented
#endif
switch (tstatus)
{
case IOWrite:
char *bytes;
for (int ip=0;ip<MAXGENS;ip++)
{
int size = pack_sprng(STREAM[ip],&bytes); // pack a stream state
fwrite(&size,1,sizeof(int),fp); // store # of bytes
fwrite(bytes,1,size,fp); // store a stream state
free(bytes);
}
break;
case IORead:
char buffer[MAX_PACKED_LENGTH];
int size;
for (int ip=0;ip<MAXGENS;ip++)
{
free_sprng(STREAM[ip]);
if (fread(&size,1,sizeof(int),fp) != sizeof(int))
nrerror(_proc_,"Failed to read stream size");
if (fread(buffer,1,size,fp) != size)
nrerror(_proc_,"Failed to read stream data");
STREAM[ip] = unpack_sprng(buffer);
}
break;
default:
nrerror (_proc_,IO_ERR_WMODE);
break;
}
fclose(fp);
}
double rnd(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[0]);
}
double rnd1(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[1]);
}
double rnd2(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[2]);
}
double rnd3(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[3]);
}
double rnd4(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[4]);
}
double rnd5(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[5]);
}
double rnd6(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[6]);
}
double rnd7(void)
// double random number generator in the range [0,1]
{
return sprng(STREAM[7]);
}
double gauss(double alpha)
// random numbers according Gaussian distribution exp{-alpha*x^2}
{
double r1 = sprng(STREAM[8]);
double r2 = sprng(STREAM[9]);
double x1 = sqrt(-log(r1))*cos(2.0*M_PI*r2);
// double x1 = sqrt(-log(r1))*cos(2.0*M_PI*r2);
// double x2 = sqrt(-log(r1))*sin(2.0*M_PI*r2);
return (x1/sqrt(alpha));
}
int nrnd1(int n)
// integer random number generator in the range [0,n-1]
{
return (int)floor(n*sprng(STREAM[10]));
}
int nrnd2(int n)
// integer random number generator in the range [0,n-1]
{
return (int)floor(n*sprng(STREAM[11]));
}
int nrnd3(int n)
// integer random number generator in the range [0,n-1]
{
return (int)floor(n*sprng(STREAM[12]));
}
int nrnd4(int n)
// integer random number generator in the range [0,n-1]
{
return (int)floor(n*sprng(STREAM[13]));
}