-
Notifications
You must be signed in to change notification settings - Fork 6
/
pycall.f90
366 lines (269 loc) · 10.6 KB
/
pycall.f90
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
! here is the tabular helmholtz free energy eos:
subroutine call_helmeos(nrow, den, temp, abar, zbar)
include 'implno.dek'
include 'vector_eos.dek'
! tests the eos routine
!
! ionmax = number of isotopes in the network
! xmass = mass fraction of isotope i
! aion = number of nucleons in isotope i
! zion = number of protons in isotope i
integer, intent(in) :: nrow
double precision, intent(in), dimension(nrow) :: den, temp, abar, zbar
!f2py INTEGER, INTENT(hide) :: nrow
!f2py DOUBLE PRECISION, DIMENSION(nrow), INTENT(in) :: den, temp, abar, zbar
integer :: i
! don't try and overfill the array
! if (ninput.gt.nrowmax) then stop
! set the input vector. pipeline is only 1 element long
jlo_eos = 1 ; jhi_eos = nrow
do i = 1, nrow
temp_row(i) = temp(i)
den_row(i) = den(i)
abar_row(i) = abar(i)
zbar_row(i) = zbar(i)
end do
! read the data table and call the eos
call read_helm_table
call helmeos
end
subroutine call_helmeos_DP(nrow, den, pres, abar, zbar, tguess)
include 'implno.dek'
include 'vector_eos.dek'
! tests the eos routine
!
! ionmax = number of isotopes in the network
! xmass = mass fraction of isotope i
! aion = number of nucleons in isotope i
! zion = number of protons in isotope i
integer, intent(in) :: nrow
double precision, intent(in), dimension(nrow) :: den, pres, abar, zbar, tguess
!f2py INTEGER, INTENT(hide) :: nrow
!f2py DOUBLE PRECISION, DIMENSION(nrow), INTENT(in) :: den, pres, abar, zbar, tguess
double precision, dimension(nrow) :: rerr_P, rerr_T
double precision, dimension(nrow) :: delta_P, delta_T
double precision, dimension(nrow) :: T_lower, T_upper
double precision, dimension(nrow) :: Pgoal
logical, dimension(nrow) :: NR_converged
double precision, parameter :: temp_floor = 1e4
double precision, parameter :: rtol = 1e-6
integer :: i, iter
integer, parameter :: max_iter = 100
! don't try and overfill the array
! if (ninput.gt.nrowmax) then stop
! read the data table
call read_helm_table
! set the input vector. pipeline is only 1 element long
jlo_eos = 1 ; jhi_eos = nrow
do i = 1, nrow
Pgoal(i) = pres(i)
temp_row(i) = tguess(i)
den_row(i) = den(i)
abar_row(i) = abar(i)
zbar_row(i) = zbar(i)
end do
T_lower = temp_floor
T_upper = 1e12
NR_converged = .FALSE.
! do the NR iteration
do iter = 1, max_iter
call helmeos
do i = 1, nrow
! if this point is converged, go to the next one
if (NR_converged(i)) cycle
! energy difference
delta_P(i) = Pgoal(i) - ptot_row(i)
! keep things safe with bisect-limits
if (delta_P(i).gt.0) then
t_lower(i) = temp_row(i)
else
t_upper(i) = temp_row(i)
end if
! update temperature
delta_T(i) = delta_P(i) / dpt_row(i)
temp_row(i) = temp_row(i) + delta_T(i)
! if this took us out of bounds, don't let it happen
! choose a new point inside the interval [t_lower, t_upper]
! the point is in the middle of the interval (logarthmically)
if ((temp_row(i).gt.t_upper(i)).OR.(temp_row(i).lt.t_lower(i))) then
temp_row(i) = sqrt(t_lower(i) * t_upper(i))
end if
! calculate relative errors
rerr_P(i) = delta_P(i) / Pgoal(i)
rerr_T(i) = delta_T(i) / temp_row(i)
! if we're at tolerances, end this
if ((abs(rerr_P(i)).LE.rtol).AND.(abs(rerr_T(i)).LE.rtol)) then
NR_converged(i) = .TRUE.
endif
!allow points at the temperature floor to "converge"
if (t_upper(i).le.temp_floor * (1d0 + rtol)) then
NR_converged(i) = .TRUE.
temp_row(i) = temp_floor
end if
end do
if (ALL(NR_converged)) exit
end do
! once more, with feeling
NR_converged = .FALSE.
call helmeos
end subroutine call_helmeos_DP
subroutine call_helmeos_DS(nrow, den, entr, abar, zbar, tguess)
include 'implno.dek'
include 'vector_eos.dek'
! tests the eos routine
!
! ionmax = number of isotopes in the network
! xmass = mass fraction of isotope i
! aion = number of nucleons in isotope i
! zion = number of protons in isotope i
integer, intent(in) :: nrow
double precision, intent(in), dimension(nrow) :: den, entr, abar, zbar, tguess
!f2py INTEGER, INTENT(hide) :: nrow
!f2py DOUBLE PRECISION, DIMENSION(nrow), INTENT(in) :: den, entr, abar, zbar, tguess
double precision, dimension(nrow) :: rerr_S, rerr_T
double precision, dimension(nrow) :: delta_S, delta_T
double precision, dimension(nrow) :: T_lower, T_upper
double precision, dimension(nrow) :: Sgoal
logical, dimension(nrow) :: NR_converged
double precision, parameter :: temp_floor = 1e4
double precision, parameter :: rtol = 1e-6
integer :: i, iter
integer, parameter :: max_iter = 100
! don't try and overfill the array
! if (ninput.gt.nrowmax) then stop
! read the data table
call read_helm_table
! set the input vector. pipeline is only 1 element long
jlo_eos = 1 ; jhi_eos = nrow
do i = 1, nrow
Sgoal(i) = entr(i)
temp_row(i) = tguess(i)
den_row(i) = den(i)
abar_row(i) = abar(i)
zbar_row(i) = zbar(i)
end do
T_lower = temp_floor
T_upper = 1e12
NR_converged = .FALSE.
! do the NR iteration
do iter = 1, max_iter
call helmeos
do i = 1, nrow
! if this point is converged, go to the next one
if (NR_converged(i)) cycle
! energy difference
delta_S(i) = Sgoal(i) - stot_row(i)
! keep things safe with bisect-limits
if (delta_S(i).gt.0) then
t_lower(i) = temp_row(i)
else
t_upper(i) = temp_row(i)
end if
! update temperature
delta_T(i) = delta_S(i) / dst_row(i)
temp_row(i) = temp_row(i) + delta_T(i)
! if this took us out of bounds, don't let it happen
! choose a new point inside the interval [t_lower, t_upper]
! the point is in the middle of the interval (logarthmically)
if ((temp_row(i).gt.t_upper(i)).OR.(temp_row(i).lt.t_lower(i))) then
temp_row(i) = sqrt(t_lower(i) * t_upper(i))
end if
! calculate relative errors
rerr_S(i) = delta_S(i) / Sgoal(i)
rerr_T(i) = delta_T(i) / temp_row(i)
! if we're at tolerances, end this
if ((abs(rerr_S(i)).LE.rtol).AND.(abs(rerr_T(i)).LE.rtol)) then
NR_converged(i) = .TRUE.
endif
!allow points at the temperature floor to "converge"
if (t_upper(i).le.temp_floor * (1d0 + rtol)) then
NR_converged(i) = .TRUE.
temp_row(i) = temp_floor
end if
end do
if (ALL(NR_converged)) exit
end do
! once more, with feeling
NR_converged = .FALSE.
call helmeos
end subroutine call_helmeos_DS
subroutine call_helmeos_DE(nrow, den, ener, abar, zbar, tguess)
include 'implno.dek'
include 'vector_eos.dek'
! tests the eos routine
!
! ionmax = number of isotopes in the network
! xmass = mass fraction of isotope i
! aion = number of nucleons in isotope i
! zion = number of protons in isotope i
integer, intent(in) :: nrow
double precision, intent(in), dimension(nrow) :: den, ener, abar, zbar, tguess
!f2py INTEGER, INTENT(hide) :: nrow
!f2py DOUBLE PRECISION, DIMENSION(nrow), INTENT(in) :: den, ener, abar, zbar, tguess
double precision, dimension(nrow) :: rerr_e, rerr_T
double precision, dimension(nrow) :: delta_e, delta_T
double precision, dimension(nrow) :: T_lower, T_upper
double precision, dimension(nrow) :: egoal
logical, dimension(nrow) :: NR_converged
double precision, parameter :: temp_floor = 1e4
double precision, parameter :: rtol = 1e-6
integer :: i, iter
integer, parameter :: max_iter = 100
! don't try and overfill the array
! if (ninput.gt.nrowmax) then stop
! read the data table
call read_helm_table
! set the input vector. pipeline is only 1 element long
jlo_eos = 1 ; jhi_eos = nrow
do i = 1, nrow
egoal(i) = ener(i) / den(i) ! eos works on specific internal energy
temp_row(i) = tguess(i)
den_row(i) = den(i)
abar_row(i) = abar(i)
zbar_row(i) = zbar(i)
end do
T_lower = temp_floor
T_upper = 1e12
NR_converged = .FALSE.
! do the NR iteration
do iter = 1, max_iter
call helmeos
do i = 1, nrow
! if this point is converged, go to the next one
if (NR_converged(i)) cycle
! energy difference
delta_E(i) = egoal(i) - etot_row(i)
! keep things safe with bisect-limits
if (delta_E(i).gt.0) then
t_lower(i) = temp_row(i)
else
t_upper(i) = temp_row(i)
end if
! update temperature
delta_T(i) = delta_E(i) / det_row(i)
temp_row(i) = temp_row(i) + delta_T(i)
! if this took us out of bounds, don't let it happen
! choose a new point inside the interval [t_lower, t_upper]
! the point is in the middle of the interval (logarthmically)
if ((temp_row(i).gt.t_upper(i)).OR.(temp_row(i).lt.t_lower(i))) then
temp_row(i) = sqrt(t_lower(i) * t_upper(i))
end if
! calculate relative errors
rerr_e(i) = delta_e(i) / egoal(i)
rerr_T(i) = delta_T(i) / temp_row(i)
! if we're at tolerances, end this
if ((abs(rerr_e(i)).LE.rtol).AND.(abs(rerr_T(i)).LE.rtol)) then
NR_converged(i) = .TRUE.
endif
!allow points at the temperature floor to "converge"
if (t_upper(i).le.temp_floor * (1d0 + rtol)) then
NR_converged(i) = .TRUE.
temp_row(i) = temp_floor
end if
end do
if (ALL(NR_converged)) exit
end do
! once more, with feeling
NR_converged = .FALSE.
call helmeos
end subroutine call_helmeos_DE