-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonicscan.py
346 lines (289 loc) · 8.59 KB
/
sonicscan.py
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
import os
import sys
import subprocess
import math
import numpy as np
# Begin some global definitions
#xini = {'c12': 0.5, 'o16': 0.48, 'ne20': 0.0, 'ne22': 0.02,
# 'na23': 0.0, 'mg24': 0.0}
xini = {'c12': 0.15, 'o16': 0.45, 'ne20': 0.35, 'ne22': 0.01,
'na23': 0.05, 'mg24': 0.05}
xfin = {'c12': 0.05, 'o16': 0.45, 'ne20': 0.45, 'ne22': 0.01,
'na23': 0.05, 'mg24': 0.05}
xall = {'n': 0.0, 'h1': 0.0, 'he4': 0.0, 'c12': 0.0, 'c13': 0.0,
'n14': 0.0, 'o16': 0.0, 'ne20': 0.0, 'ne22': 0.0,
'na23': 0.0, 'mg24': 0.0, 'si28': 0.0, 'fe52': 0.0,
'fe54': 0.0, 'fe56': 0.0, 'ni56': 0.0}
vdetopt = 1057232460.76
maxvit = 1000
roughvit = 6
vdettol = 0.0001e9
fdvdet = 0.2
# Refinement settings: note that the top-level is currentrefine=0
maxrefine = 4
currentrefine = 0
refpoints = 10 # Factor by which to shrink the w spacing
torchoutlabel = 'w'
wlo = 0.0
whi = 1.0
# Number of top-level points in w to use
wnum = 100
#Unnecessary: we're in the torch directory
#os.chdir('./torch-dmt-dw')
mainlog = open('sonicscan.log','w')
fsr = open('scanrange_log.txt','w')
# End global definitions
def main():
global fsr
global xall
global wnum
global wlo
global whi
xk = xall.keys()
# Write log header
fsr.write('w vdet ')
for k in xk:
fsr.write(k + ' ')
fsr.write('\n')
#scanrange(wlo,whi,wnum)
bruteforce(1.0e9,1.2e9,wnum,1.0)
closelogs()
def criticalhalt():
writetolog('Critical halt!')
closelogs()
sys.exit()
def closelogs():
global fsr
global mainlog
fsr.close()
mainlog.close()
def bruteforce(vdetlo,vdethi,numpts,w):
global xall
computex(w)
foundgoodnan = False
foundgoodovr = False
nanvdet = 0.0
ovrvdet = 0.0
bf = open('bruteforce.log','w')
bf.write('vdetk isnan\n')
for k in range(0,numpts+1):
vdetk = math.pow(10.0,(math.log10(vdetlo) + float(k)*
(math.log10(vdethi)-math.log10(vdetlo))
/float(numpts)))
errstate = testvdetsonic(vdetk)
if not hascriticalerror(errstate):
if hasnan(errstate):
foundgoodnan = True
writetolog('Found nan state. vdetk = ' + str(vdetk))
bf.write(str(vdetk) + ' 1\n')
nanvdet = vdetk
else:
foundgoodovr = True
writetolog('Found ovr state. vdetk = ' + str(vdetk))
bf.write(str(vdetk) + ' 0\n')
ovrvdet = vdetk
# if (foundgoodnan and foundgoodovr and (nanvdet < ovrvdet)):
# vdet = subdividevdet(ovrvdet,nanvdet)
# foundvdet(xall['c12'],vdet)
# return
# writetolog('Could not find vdet.')
bf.close()
return
def scanrange(wmin,wmax,n):
# First check the current refinement level...
global maxrefine
global currentrefine
if (currentrefine > maxrefine):
writetolog('Error: maxrefine exceeded!')
criticalhalt()
# Iterate through the given range of w, finding vdet
# Return the vdet corresponding to wmax.
# w domain is n total points in: [wmin,wmax]
global fsr
global xall
global vdetopt
global fdvdet
global torchoutlabel
wmin = float(wmin)
wmax = float(wmax)
# By default, the endpoint of the range is included
wrange = np.linspace(wmin,wmax,n)
# Generate a list of vdets for this refinement level
vdetlist = [0.0 for k in range(0,n)]
j = 0
for w in wrange:
# Advance the composition to w
computex(w)
# Set the label for the torch output
torchoutlabel = str(w)
writetolog('starting torch runs...')
# Find the overdriven velocity at w
prevw = w
if (j > 0):
prevw = wrange[j-1]
vdetopt = findoverdriven(vdetlist,j,prevw,w)
foundvdet(xall['c12'],vdetopt)
vdetlist[j] = vdetopt
# Archive torch log files, etc.
tidytorch(w)
# Write to log file
xk = xall.keys()
fsr.write(str(w) + ' ')
fsr.write(str(vdetopt) + ' ')
for k in xk:
fsr.write(str(xall[k]) + ' ')
fsr.write('\n')
writetolog('Completed w: ' + str(w))
j = j + 1
return vdetopt
def testvdetsonic(vdet):
writefile(vdet)
runtorch()
return geterrorstate()
def writetolog(s):
global mainlog
print s
mainlog.write(s + '\n')
def findoverdriven(vdetlist,vdetindx,prevw,thisw):
global vdettol
global vdetopt
global xall
global roughvit
global refpoints
global currentrefine
vdetstep = 0.0
# Compute initial step:
if (vdetindx >= 2):
vdetstep = abs(vdetlist[vdetindx-1] - vdetlist[vdetindx-2])
if (vdetstep < vdettol):
vdetstep = vdettol
hnan = hasnan(testvdetsonic(vdetopt))
# Get minimally overdriven vdet...
# Raise/lower vdet to get new upper/lower limit depending on hnan
if hnan:
writetolog('vdetopt: ' + str(vdetopt) + ' is sonic.')
vdetlo = vdetopt
else:
writetolog('vdetopt: ' + str(vdetopt) + ' is not sonic.')
vdethi = vdetopt
raisefrac = 2.0
for k in range(0,roughvit):
if hnan:
vdetk = vdetopt+vdetstep
else:
vdetk = vdetopt-vdetstep
errstate = testvdetsonic(vdetk)
if (k==roughvit-1):
# Allowed iterations exceeded: need to refine
writetolog('Refining...')
currentrefine = currentrefine + 1
thisvd = scanrange(prevw,thisw,refpoints)
currentrefine = currentrefine - 1
return thisvd
elif hascriticalerror(errstate):
# critical error, lower step
# Note that the lowering fraction should be
# less than 1 over the raising fraction
# since this will aid convergence instead of
# a forever alternating state.
writetolog('Lowering step...')
vdetstep = 0.3*vdetstep
raisefrac = 1.0+(raisefrac-1.0)*0.7
continue
elif hasnan(errstate):
# no critical error but still sonic, raise step
writetolog('Raising step...')
vdetstep = raisefrac*vdetstep
else:
# no critical error and not sonic, subdivide interval
if hnan:
vdethi = vdetk
else:
vdetlo = vdetk
return subdividevdet(vdethi,vdetlo)
def subdividevdet(vdethi,vdetlo):
global maxvit
global vdettol
# Iterate maxvit times or until tolerance is reached
domain = vdethi-vdetlo
for j in range(0,maxvit):
vdetj = vdetlo + 0.5*domain
errstate = testvdetsonic(vdetj)
if hasnan(errstate):
vdetlo = vdetj
domain = vdethi - vdetlo
else:
if (abs(vdetj-vdetlo) < vdettol):
writetolog('Returning from subdivide, iteration: ' + str(j))
writetolog('vdetlo is: ' + str(vdetlo))
writetolog('vdethi is: ' + str(vdethi))
writetolog('vdetj is: ' + str(vdetj))
writetolog('domain is: ' + str(domain))
return vdetj
else:
vdethi = vdetj
domain = vdethi - vdetlo
if domain < vdettol:
return vdethi
writetolog('Insufficient iterations maxvit to get required tolerance!')
return vdethi
def foundvdet(xc12,vdet):
writetolog('xc12: ' + str(xc12))
writetolog('Found minimally overdriven vdet: ' + str(vdet))
def runtorch():
subprocess.call('./torch < intorch.txt > foo_log.dat',shell=True)
def tidytorch(w):
os.system('mv foo_log.dat foo_log_srw_' + str(w) + '.dat')
os.system('mv intorch.txt intorch_' + str(w) + '.txt')
def hasnan(errstate):
return errstate[0]
def hascriticalerror(errstate):
return errstate[1]
def geterrorstate():
flog = open('foo_log.dat','r')
retval = [False, True]
for l in flog:
if (l.find('stepsize: nan error encountered')!=-1):
# NaN error
retval[0] = True
if (l.find('STOP normal termination')!=-1):
# Normal termination, no critical error
retval[1] = False
flog.close()
return retval
def computex(w):
# w normalized to wmax
global xini
global xall
global xfin
kall = xall.keys()
for k in kall:
if not k in xini:
xall[k] = 0.0
else:
xall[k] = xini[k] + (xfin[k] - xini[k])*w
def writefile(vdet):
global xall
global torchoutlabel
vdet = float(vdet)
fin = open('intorch.txt','w')
fin.write("8\n0\n")
fin.write("1 0 0 0 0 0 0 1 1 1e30\n")
fin.write("1\n0\n")
fin.write("1e3 4e8 1e7\n")
fin.write("3\n")
strx = (str(xall['n']) + " " + str(xall['h1']) + " " + str(xall['he4']) + " " +
str(xall['c12']) + " " + str(xall['c13']) + " " + str(xall['n14']) +
" " + str(xall['o16']) + " " + str(xall['ne20']) + " " +
str(xall['ne22']) + " " +
str(xall['na23']) + " " + str(xall['mg24']) + " " +
str(xall['si28']) + " " + str(xall['fe52']) +
" " + str(xall['fe54']) + " " + str(xall['fe56']) + " " +
str(xall['ni56']))
fin.write(strx + '\n')
fin.write("xc12_" + torchoutlabel + "_\n")
fin.write(str(vdet) + '\n')
fin.write("0")
fin.close()
# Call main
main()