-
Notifications
You must be signed in to change notification settings - Fork 1
/
darwin_random.F
244 lines (211 loc) · 5.83 KB
/
darwin_random.F
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
C $Header: /u/gcmpack/MITgcm_contrib/darwin2/pkg/darwin/darwin_random.F,v 1.2 2011/05/12 16:13:11 jahn Exp $
C $Name: $
#include "CPP_OPTIONS.h"
#include "DARWIN_OPTIONS.h"
#ifdef PORT_RAND
#ifdef OLDSEED
These lines are here intentionally to cause a compile-time error:
If you really want to use PORT_RAND with OLDSEED, comment them out.
#endif
#endif
#ifdef ALLOW_PTRACERS
#ifdef ALLOW_DARWIN
CBOP
C !ROUTINE: DARWIN_RANDOM_INIT
C !INTERFACE:
SUBROUTINE DARWIN_RANDOM_INIT(seed, myThid)
C !DESCRIPTION: \bv
C *==========================================================*
C | SUBROUTINE DARWIN_RANDOM_INIT
C | o Initializes the random number generator.
C | seed must be positive.
C | NOTE: not thread-safe yet!!!
C *===========================================================
C | Algorithms:
C | - With PORT_RAND defined, this uses port_rand.f, which is
C | Knuth's portable random number generator [see Numerical
C | Recipes, Ch.7.1: ran3].
C | We use the floating-point version.
C | In order to obtain unique sequences of random numbers,
C | the seed should be between 1 and 1618032.
C | - With PORT_RAND undefined, it uses the system's RAND
C | function. See the system documentation for ranges, etc.
C *==========================================================*
C \ev
C !USES:
IMPLICIT NONE
#include "EEPARAMS.h"
C !INPUT/OUTPUT PARAMETERS:
C == Routine arguments ==
C myThid :: thread number
INTEGER seed
INTEGER myThid
C !FUNCTIONS:
C == Functions ==
#ifdef PORT_RAND
real*8 port_rand
external port_rand
#endif
Coj these are needed for the Intel compiler, define the macro IFORT
Coj in your optfile if you want to use it
#ifdef IFORT
real*4 RAND
EXTERNAL RAND
#endif
C !LOCAL VARIABLES:
C == Local variables ==
C msgBuf - Informational/error meesage buffer
CHARACTER*(MAX_LEN_MBUF) msgBuf
_RL RandNo
INTEGER nrand
#ifdef PORT_RAND
REAL*8 Dseed
#else
INTEGER Iseed
#endif
INTEGER IRand
CHARACTER*16 random_name
CEOP
IF (myThid .GT. 1) THEN
CALL PRINT_ERROR('DARWIN_RANDOM_INIT: threading no supported',
& myThid)
STOP 'ABNORMAL END: S/R DARWIN_RANDOM_INIT'
END IF
IF (seed .LE. 0) THEN
CALL PRINT_ERROR('DARWIN_RANDOM_INIT: seed must be positive'
& , myThid)
END IF
#ifdef PORT_RAND
Dseed = float(seed)
RandNo = port_rand(Dseed)
C need to call again to get a non-zero random number
Dseed = -1.D0
RandNo = port_rand(Dseed)
random_name = 'port_rand'
#else
#ifdef OLDSEED
Iseed = seed
do nrand = 1,Iseed
Irand = 0
RandNo = rand(Irand)
end do
random_name = 'rand/oldseed'
#else
Iseed = -seed
RandNo = rand(Iseed)
random_name = 'rand'
#endif
#endif
WRITE(msgbuf,'(A,A,I10,X,F20.16)')
& 'QQ random ', random_name, seed, RandNo
CALL PRINT_MESSAGE( msgBuf, standardMessageUnit,
& SQUEEZE_RIGHT , myThid)
RETURN
END
CBOP
C !ROUTINE: DARWIN_RANDOM
C !INTERFACE:
FUNCTION DARWIN_RANDOM(myThid)
C !DESCRIPTION: \bv
C *==========================================================*
C | FUNCTION DARWIN_RANDOM
c | o returns a uniform random number between 0 and 1
C *==========================================================*
C \ev
C !USES:
IMPLICIT NONE
#include "EEPARAMS.h"
C !INPUT/OUTPUT PARAMETERS:
C == Routine arguments ==
C DARWIN_RANDOM :: uniform random number
C myThid :: thread number
_RL DARWIN_RANDOM
INTEGER myThid
C !FUNCTIONS:
C == Functions ==
#ifdef PORT_RAND
real*8 port_rand
external port_rand
#endif
#ifdef IFORT
real*4 RAND
EXTERNAL RAND
#endif
C !LOCAL VARIABLES:
C == Local variables ==
#ifdef PORT_RAND
real*8 Dseed
#else
INTEGER Iseed
#endif
CEOP
IF (myThid .GT. 1) THEN
CALL PRINT_ERROR('DARWIN_RANDOM: threading no supported',
& myThid)
STOP 'ABNORMAL END: S/R DARWIN_RANDOM'
END IF
#ifdef PORT_RAND
Dseed = -1.d0
darwin_random = port_rand(Dseed)
#else
Iseed = 0
darwin_random = rand(Iseed)
#endif
RETURN
END
CBOP
C !ROUTINE: DARWIN_RANDOM_NORMAL
C !INTERFACE:
FUNCTION DARWIN_RANDOM_NORMAL(myThid)
C !DESCRIPTION: \bv
C *==========================================================*
C | FUNCTION DARWIN_RANDOM_NORMAL
C | o returns a normally distributed random number with
C | mean 0 and stddev 1
C *==========================================================*
C \ev
C !USES:
IMPLICIT NONE
#include "EEPARAMS.h"
C !INPUT/OUTPUT PARAMETERS:
C == Routine arguments ==
C DARWIN_RANDOM_NORMAL :: normally distributed random number
C myThid :: thread number
_RL DARWIN_RANDOM_NORMAL
INTEGER myThid
C !FUNCTIONS:
C == Functions ==
#ifdef PORT_RAND
real*8 port_rand_norm
external port_rand_norm
#endif
#ifdef IFORT
real*4 RAND
EXTERNAL RAND
#endif
C !LOCAL VARIABLES:
C == Local variables ==
#ifndef PORT_RAND
INTEGER Iseed
real*8 uniform
real*8 normal
#endif
CEOP
IF (myThid .GT. 1) THEN
CALL PRINT_ERROR('DARWIN_RANDOM: threading no supported',
& myThid)
STOP 'ABNORMAL END: S/R DARWIN_RANDOM'
END IF
#ifdef PORT_RAND
darwin_random_normal = port_rand_norm()
#else
Iseed = 0
uniform = rand(Iseed)
CALL invnormal(normal, uniform, 0.d0, 1.d0)
darwin_random_normal = normal
#endif
RETURN
END
#endif /*DARWIN*/
#endif /*ALLOW_PTRACERS*/
c ==========================================================