-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGX_io.py
More file actions
409 lines (284 loc) · 10.8 KB
/
Copy pathGX_io.py
File metadata and controls
409 lines (284 loc) · 10.8 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import numpy as np
from netCDF4 import Dataset
import subprocess
import os
class GX_Runner():
# This class handles GX input files, and also execution
def __init__(self,template):
self.read_input(template)
def read_input(self, fin):
with open(fin) as f:
data = f.readlines()
obj = {}
header = ''
for line in data:
# strip comments
if line.find('#') > -1:
end = line.find('#')
line = line[:end]
# parse headers
if line.find('[') == 0:
header = line.split('[')[1].split(']')[0]
obj[header] = {}
continue
# skip blanks
if line.find('=') < 0:
continue
# store data
key, value = line.split('=')
key = key.strip()
value = value.strip()
if header == '':
obj[key] = value
else:
obj[header][key] = value
self.inputs = obj
self.filename = fin
def write(self, fout='temp.in'):
# do not overwrite
if (os.path.exists(fout)):
print( ' input exists, skipping write', fout )
return
with open(fout,'w') as f:
for item in self.inputs.items():
if ( type(item[1]) is not dict ):
print(' %s = %s ' % item, file=f)
continue
header, nest = item
print('\n[%s]' % header, file=f)
longest_key = max( nest.keys(), key=len)
N_space = len(longest_key)
for pair in nest.items():
s = ' {:%i} = {}' % N_space
print(s.format(*pair), file=f)
print(' wrote input:', fout)
def execute(self):
# assume Trinity is in a salloc environment with GPUs
# write input file, write batch file, execute srun
pass
def list_inputs(self, header=False):
if header:
print( "name, tprim, fprim, temp, dens, taufac" )
return
name = self.filename.split('/')[-1]
def load(label):
# load from species
data = self.inputs['species'][label][1:-1].split(',')
return np.array(data,float)
tprim = load("tprim")
fprim = load("fprim")
dens = load("dens")
temp = load("temp")
taufac = float(self.inputs['Boltzmann']['tau_fac'])
print(name, f"{str(tprim):16}", f"{str(fprim):16}"
, f"{str(temp):16}", dens, f"{taufac:.3f}")
def list_resolution(self, header=False):
if header:
print( 'name', 'ntheta', 'nx', 'ny', 'nhermite', 'nlaguerre', 'dt', 'nstep')
return
ntheta = int(self.inputs['Dimensions']['ntheta'])
nx = int(self.inputs['Dimensions']['nx'])
ny = int(self.inputs['Dimensions']['ny'])
nhermite = int(self.inputs['Dimensions']['nhermite'])
nlaguerre = int(self.inputs['Dimensions']['nlaguerre'])
dt = float(self.inputs['Time']['dt'])
nstep = int(self.inputs['Time']['nstep'])
tag = self.filename.split('/')[-1]
print( tag, ntheta,nx,ny, nhermite, nlaguerre, dt, nstep)
def pretty_print(self, entry=''):
# dumps out current input data, in GX input format
# if entry, where entry is one of the GX input headers
# only print the inputs nested under entry
for item in self.inputs.items():
# a catch for the debug input, which has no header
if ( type(item[1]) is not dict ):
if (entry == ''):
print(' %s = %s ' % item)
continue
header, nest = item
# special case
if (entry != ''):
header = entry
nest = self.inputs[entry]
print('\n[%s]' % header)
longest_key = max( nest.keys(), key=len)
N_space = len(longest_key)
for pair in nest.items():
s = ' {:%i} = {}' % N_space
print(s.format(*pair))
# special case
if (entry != ''):
break
# should this be a class? Yes, this is now outdated 8/20
def read_GX_output(fname):
try:
f = Dataset(fname, mode='r')
except:
print(' read_GX_output: could not read', fname)
t = f.variables['time'][:]
q = f.groups['Fluxes'].variables['qflux'][:,0]
# check for NANs
if ( np.isnan(q).any() ):
print(' nans found in', fname)
q = np.nan_to_num(q)
# median of a sliding median
N = len(q)
med = np.median( [ np.median( q[::-1][:k] ) for k in np.arange(1,N)] )
#print(' read GX output: qflux = ', med)
return med # this is the qflux
class GX_Output():
def __init__(self,fname):
try:
f = Dataset(fname, mode='r')
#f = nc.netcdf_file(fname, 'r')
except:
print(' read_GX_output: could not read', fname)
qflux = f.groups['Fluxes'].variables['qflux'][:,0]
# check for NANs
if ( np.isnan(qflux).any() ):
print(' nans found in', fname)
qflux = np.nan_to_num(qflux)
self.qflux = qflux
self.pflux = f.groups['Fluxes'].variables['pflux'][:,0]
self.time = f.variables['time'][:]
self.tprim = f.groups['Inputs']['Species']['T0_prime'][:]
self.fprim = f.groups['Inputs']['Species']['n0_prime'][:]
self.fname = fname
self.data = f
def median_estimator(self):
N = len(self.qflux)
med = np.median( [ np.median( self.qflux[::-1][:k] ) for k in np.arange(1,N)] )
self.q_median = med
return med
def exponential_window_estimator(self, tau=100):
### todo: separate data from physics. let this take flux as an argument, so it can work for both heat and particle flux
# load data
time = self.time
qflux = self.qflux
# initial state
t0 = time[0]
qavg = qflux[0]
var_qavg = 0
Q_avg = []
Var_Q_avg = []
# loop through time
N = len(qflux)
for k in np.arange(N):
# get q(t)
q = qflux[k]
t = time [k]
# compute weights
gamma = (t - t0)/tau
alpha = np.e**( - gamma)
delta = q - qavg
# update averages
qavg = alpha * qavg + q * (1 - alpha)
var_qavg = alpha * ( var_qavg + (1-alpha)* delta**2)
t0 = t
# save
Q_avg.append(qavg)
Var_Q_avg.append(var_qavg)
self.Q_avg = Q_avg
self.Var_Q_avg = Var_Q_avg
return Q_avg[-1], Var_Q_avg[-1]
def check_convergence(self, tau_list=[10,50,100], threshold=0.5):
# turtle: I don't think this is being used yet, it may not have been tested 11/17
'''
Runs the exponential moving average for a list of taus
Decides whether to halt, by comparing neighboring taus.
'''
print("tau_list", tau_list)
data = [ self.exponential_window_estimator(tau=tau) for tau in tau_list]
avg,var = np.transpose(data)
std = np.sqrt(var)
check = std/avg < threshold
print(check)
print(avg)
N = len(tau_list)
for j in range(N-1):
c0 = check[j]
if c0:
c1 = check[j+1]
if c1:
# execute halt command
run_name = self.fname[:-3]
cmd = f"touch {run_name}.stop"
print(cmd)
return
class VMEC_GX_geometry_module():
# this class handles VMEC-GX Geometry .ing input files
def __init__(self, engine,
f_sample = 'gx-geometry-sample.ing',
tag = 'default',
input_path = 'gx-files/',
output_path = './'
):
self.engine = engine
self.data = self.read(input_path + f_sample)
self.output_path = output_path
self.input_path = input_path
self.tag = tag
# this function is run at __init__
# it parses a sample GX_geometry.ing input file
# as a dictionary for future modifications
def read(self,fin):
with open(fin) as f:
indata = f.readlines()
data = {} # create dictionary
for line in indata:
# remove comments
info = line.split('#')[0]
# skip blanks
if info.strip() == '':
continue
# parse
key,val = info.split('=')
key = key.strip()
val = val.strip()
# save
data[key] = val
return data
def write(self, fname): # writes a .ing file
# load
data = self.data
path = self.output_path
# set spacing
longest_key = max( data.keys(), key=len)
N_space = len(longest_key)
# write
fout = path + fname + '.ing'
with open(fout,'w') as f:
for pair in data.items():
s = ' {:%i} = {}' % N_space
#print(s.format(*pair)) # print to screen for debugging
print(s.format(*pair), file=f)
def set_vmec(self,wout, vmec_path='./', output_path='./'):
# copy vmec output from vmec_path to output_path
#cmd = 'cp {:}{:} {:}'.format(vmec_path, wout, output_path)
#os.system(cmd)
self.data['vmec_file'] = '"{:}"'.format(wout)
self.data['out_path'] = '"{:}"'.format(output_path)
self.data['vmec_path'] = '"{:}"'.format(vmec_path)
def init_radius(self,rho,r_idx):
# set radius
s = rho**2
self.data['desired_normalized_toroidal_flux'] = s
t_idx = self.engine.t_idx
file_tag = f"vmec-t{t_idx}-r{r_idx}"
self.data['file_tag'] = f"\"{file_tag}\""
# write input
in_path = self.input_path
out_path = self.output_path
fname = self.tag + '-psi-{:.2f}'.format(s)
self.write(fname)
print(' wrote .ing', out_path+fname)
# run
system = os.environ['GK_SYSTEM']
cmd = ['{:}convert_VMEC_to_GX'.format(in_path), out_path+fname]
if system == 'traverse':
cmd = ['convert_VMEC_to_GX', out_path+fname]
f_log = out_path + fname + '.log'
with open(f_log, 'w') as fp:
subprocess.call(cmd,stdout=fp)
f_geometry = f"gx_geo_{file_tag}.nc"
return f_geometry