-
Notifications
You must be signed in to change notification settings - Fork 2
/
Arriero.py
436 lines (400 loc) · 17.6 KB
/
Arriero.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# –¿Qué es lo que pasa, doña Eduviges?
# Ella sacudieo la cabeza como si despertara de un sueño
# –Es el caballo de Miguel Páramo, que galopa por el camino de la Media Luna.
import os
import subprocess
import sys
from time import strftime, localtime
from Eduviges.misc import fortran_double
#
# ##### ## ##### ## # # ####
# # # # # # # # # ## ## #
# # # # # # # # # # ## # ####
# ##### ###### ##### ###### # # #
# # # # # # # # # # # #
# # # # # # # # # # ####
class parameters(object):
'''This is the parameters class
'''
# ----- PARAMETERS -----
def rParams(self):
self.R = 1e15 # radius of emitting region (assuming spherical)
self.R0 = 1e14 # distance from central engine
self.dLum = 4.0793e26 # luminosity distance (default Mrk 421)
self.z = 0.03 # redshift (default Mrk 421)
self.theta_obs = 5.0 # observer viewing angle
self.gamma_bulk = 1e2 # emitting region bulk Lorentz factor
self.mu_mag = 1.0 # (1 + sigma) Gamma_bulk
self.sigma = 1.0 # magnetization (sigma)
self.f_rec = 1.0 # magnetic reconection dissipative efficiency
self.b_index = 0.0 # magnetic field decay index
self.Bfield = 1.0 # magnetic field decay index
self.eps_B = 0.03 # epsilon_B
self.eps_e = 0.1 # epsilon_e
self.eps_acc = 1e-2 # epsilon_acc
self.theta_e = 10.0 # electrons temperature
self.zeta_e = 0.99 # fraction of non-thermal particles
self.f_esc = 1.0 # electrons escape time factor
self.tstep = 1e-2 # time step factor
self.tmax = 1e5 # maximum time
self.tmin = 0e0 # minimum time
self.tvar = 2e0 # variability time scale
self.L_jet = 1e45 # jet luminosity
self.E0 = 1e50 # energy of the blast wave
self.n_ext = 1.0 # number density of the external medium
self.g1 = 1e2 # power-law min Lorentz factor
self.g2 = 1e4 # power-law max Lorentz factor
self.gmin = 1.01 # EED minimum Lorentz factor
self.gmax = 2e4 # EED maximum Lorentz factor
self.pind = 2.5 # EED power-law index
self.nu_ext = 1e14 # external radiation field freq.
self.u_ext = 1e-4 # external radiation field ener. dens.
self.numin = 1e7 # minimum frequency
self.numax = 1e15 # maximum frequency
self.NG = 128 # number of EED bins
self.NT = 300 # number of time steps
self.NF = 256 # number of frequencies
self.time_grid = 1 # kind of cooling
self.params_file = 'input.par' # name of the parameters file
def __init__(self, **kwargs):
self.rParams()
self.__dict__.update(kwargs)
def wParams(self):
with open(self.params_file, 'w') as f:
print(fortran_double(self.R), ' ! Radius', file=f)
print(fortran_double(self.R0), ' ! Initial radius', file=f)
print(fortran_double(self.dLum), ' ! luminosity distance', file=f)
print(fortran_double(self.z), ' ! redshift', file=f)
print(fortran_double(self.theta_obs), ' ! viewing angle', file=f)
print(fortran_double(self.gamma_bulk), ' ! bulk Lorentz factor', file=f)
print(fortran_double(self.mu_mag), ' ! (1 + sigma) Gamma', file=f)
print(fortran_double(self.sigma), ' ! magnetization', file=f)
print(fortran_double(self.f_rec), ' ! dissipative efficiency of magnetic reconection', file=f)
print(fortran_double(self.b_index), ' ! magnetic field decay index', file=f)
print(fortran_double(self.Bfield), ' ! magnetic field strength', file=f)
print(fortran_double(self.theta_e), ' ! electrons temperature', file=f)
print(fortran_double(self.zeta_e), ' ! fraction of nonthermal electrons', file=f)
print(fortran_double(self.f_esc), ' ! electrons escape time', file=f)
print(fortran_double(self.tstep), ' ! time step factor', file=f)
print(fortran_double(self.tmax), ' ! maximum time', file=f)
print(fortran_double(self.tmin), ' ! minimum time', file=f)
print(fortran_double(self.tvar), ' ! variability time scale', file=f)
print(fortran_double(self.L_jet), ' ! jet luminosity', file=f)
print(fortran_double(self.E0), ' ! energy of the blast wave', file=f)
print(fortran_double(self.n_ext), ' ! number density of the external medium', file=f)
print(fortran_double(self.eps_e), ' ! epsilon_e', file=f)
print(fortran_double(self.eps_B), ' ! epsilon_B', file=f)
print(fortran_double(self.eps_acc), ' ! epsilon_acc', file=f)
print(fortran_double(self.g1), ' ! power-law min Lorentz factor', file=f)
print(fortran_double(self.g2), ' ! power-law max Lorentz factor', file=f)
print(fortran_double(self.gmin), ' ! EED min Lorentz factor', file=f)
print(fortran_double(self.gmax), ' ! EED max Lorentz factor', file=f)
print(fortran_double(self.pind), ' ! EED power-law index', file=f)
print(fortran_double(self.nu_ext), ' ! external rad. field frequency', file=f)
print(fortran_double(self.u_ext), ' ! external rad. field ener. density', file=f)
print(fortran_double(self.numin), ' ! min frequency', file=f)
print(fortran_double(self.numax), ' ! max frequency', file=f)
print(self.NG, ' ! number of EED bins', file=f)
print(self.NT, ' ! number of time steps', file=f)
print(self.NF, ' ! number of frequencies', file=f)
print(self.time_grid, ' ! kind of time grid', file=f)
# print(self.file_label, ' ! label to identify each output', file=f)
print("--> Parameters file: ", self.params_file)
# #### #### # # ##### # # ######
# # # # # ## ## # # # # #
# # # # # ## # # # # # #####
# # # # # # ##### # # #
# # # # # # # # # # #
# #### #### # # # # ###### ######
class compiler(object):
'''This is the compilation class
This function sets the value of the compilation options. The value of these
options can be changed as argumens or kwargs of the compiler class.
Parameters
----------
COMP : int, optional
Compiler to be used. 0 for GCC (default), 1 for Intel.
OMP : bool, optional
If True, compilation is done with OpenMP flag. Default False.
DBG : bool, optional
If True compilation is done with debugging flags. Default False.
HDF5 : bool, optional
If True (default), data saved in HDF5 data files. Default False.
Note: other formats need to be included
CONFIG : int, optional
Program to be compiled. 0 for tests, 1 for blazars, 2 for afterflow, 3 for turbulence, 4 for Mezcal
server : int, optional
Computer where code whill be compiled. 0 for unix PC, 1 for Brown (Purdue) server, 2 for SPORC (RC-RIT)
compileDir : str, optional
Full path where Paramo is located. Must end with '/'. Default is './'
'''
# ----- COMPILER FLAGS & RULES -----
def flags(self):
self.COMP = 0 # 0 (GCC), 1 (INTEL)
self.OMP = False # compile with OpenMP
self.DBG = False # compile for debugging
self.HDF5 = True # save data with HDF5
self.CONFIG = 0 # 0 (tests), 1 (blazars), 2 (afterflow), 3 (turbulence), 4 (Mezcal)
self.server = 0 # 0 (UNIX PC) 1 (Brown@Purdue) 2 (RC@RIT)
self.compileDir = './' # the path to Paramo... must end with '/'
def __init__(self, **kwargs):
self.flags()
self.__dict__.update(kwargs)
self.cwd = os.getcwd()
def compile(self):
print(strftime("\n\n%a, %d %b %Y %H:%M:%S %Z", localtime()))
make = ["make", "Paramo", "CONFIG="+str(self.problem), "COMPILER="+str(self.COMP)]
if self.OMP:
make.append("OPENMP=1")
else:
make.append("OPENMP=0")
if self.DBG:
make.append("DEBUGGING=1")
else:
make.append("DEBUGGING=0")
if self.HDF5:
make.append("USEHDF5=1")
else:
make.append("USEHDF5=0")
os.chdir(self.compileDir)
print("\nCompile dir: " + os.getcwd())
print("--> Running Makefile\n ", " ".join(make), "\n")
try:
make_out = subprocess.run(make, capture_output=True, text=True, check=True)
with open("make.out", "w") as logfile:
logfile.write(make_out.stdout)
except subprocess.CalledProcessError as err:
print(err.stdout)
print("Compilation Error")
print(err.stderr)
sys.exit(err.returncode)
os.chdir(self.cwd)
print("Working dir: " + os.getcwd())
def cleanup(self):
os.chdir(self.compileDir)
os.system("make clean")
os.chdir(self.cwd)
# ##### # # # #
# # # # # ## #
# # # # # # # #
# ##### # # # # #
# # # # # # ##
# # # #### # #
class Runner(object):
'''
Description
-----------
This class sets up the exectuable instructions.
'''
def __init__(self, flabel='DriverTest', par_kw={}, comp_kw={}):
self.par = parameters(**par_kw)
self.par.wParams()
self.cwd = os.getcwd()
self.comp_kw = comp_kw
self.flabel = flabel # a label to identify each output
def run_test(self, clean=False):
comp = compiler(rules='xTests', **self.comp_kw)
if clean:
comp.cleanup()
comp.compile()
run_cmd = '{0}xTests'.format(comp.compileDir)
print("\n--> Running:\n ", run_cmd, "\n")
os.system(run_cmd)
print("\n--> Finished")
# -----> BlazMag compilation and run
def run_blazMag(self, cmd_args=(None, None), pream=None, clean=False, cl=False):
if cmd_args[0] is None or cmd_args[0] is False:
in1 = 'F'
else:
in1 = 'T'
if cmd_args[1] is None or cmd_args[1] is False:
in2 = 'F'
else:
in2 = 'T'
comp = compiler(problem=1, **self.comp_kw)
if clean:
comp.cleanup()
comp.compile()
outfile = self.flabel + '.jp.h5'
if pream is None:
run_cmd = '{0}Paramo {1} {2} {3} {4}'.format(comp.compileDir, self.par.params_file, outfile, in1, in2)
else:
run_cmd = '{0} {1}Paramo {2} {3} {4} {5}'.format(pream, comp.compileDir, self.par.params_file, outfile, in1, in2)
print("\n--> Parameters:")
os.system("cat -n " + self.par.params_file)
if cl:
print("\n--> Running:\n ", run_cmd, "\n")
return run_cmd
else:
print("\n--> Running:\n ", run_cmd, "\n")
os.system(run_cmd)
print("\n--> Finished")
#####
def run_Aglow(self, cmd_args=(False, False, False), pream=None, clean=False, cl=False, wMezcal=False):
'''
Description
-----------
Aglow compilation and run.
Parameters
----------
cmd_arg
pream
clean
cl
wMezcal
'''
if cmd_args[0] is None or cmd_args[0] is False:
in1 = 'F'
else:
in1 = 'T'
if cmd_args[1] is None or cmd_args[1] is False:
in2 = 'F'
else:
in2 = 'T'
if cmd_args[2] is None or cmd_args[2] is False:
in3 = 'F'
else:
in3 = 'T'
if wMezcal:
comp = compiler(problem=4, **self.comp_kw)
else:
comp = compiler(problem=2, **self.comp_kw)
if clean:
comp.cleanup()
comp.compile()
outfile = self.flabel + '.jp.h5'
if pream is None:
run_cmd = '{0}Paramo {1} {2} {3} {4} {5}'.format(comp.compileDir, self.par.params_file, outfile, in1, in2, in3)
else:
run_cmd = '{0} {1}Paramo {2} {3} {4} {5} {6}'.format(pream, comp.compileDir, self.par.params_file, outfile, in1, in2, in3)
print("\n--> Parameters:")
os.system("cat -n " + self.par.params_file)
if cl:
print("\n--> Running:\n ", run_cmd, "\n")
return run_cmd
else:
print("\n--> Running:\n ", run_cmd, "\n")
os.system(run_cmd)
print("\n--> Finished")
# -----> turbulence compilation and run
def run_turb(self, cmd_args=(None, None), pream=None, clean=False, cl=False):
if cmd_args[0] is None or cmd_args[0] is False:
wCool = False
else:
wCool = True
if cmd_args[1] is None or cmd_args[1] is False:
wAbs = False
else:
wAbs = True
comp = compiler(rules='xTurBlaz', **self.comp_kw)
if clean:
comp.cleanup()
comp.compile()
outfile = self.flabel + '.jp.h5'
if pream is None:
run_cmd = '{0}xTurBlaz {1} {2} {3} {4}'.format(comp.compileDir, self.par.params_file, wCool, wAbs, outfile)
else:
run_cmd = '{0} {1}xTurBlaz {2} {3} {4} {5}'.format(pream, comp.compileDir, self.par.params_file, wCool, wAbs, outfile)
print("\n--> Parameters:")
os.system("cat -n " + self.par.params_file)
if cl:
print("\n--> Running:\n ", run_cmd, "\n")
return run_cmd
else:
print("\n--> Running:\n ", run_cmd, "\n")
os.system(run_cmd)
print("\n--> Finished")
# ##### ##### #### ###### # # ######
# # # # # # # # # #
# # # ##### #### ##### # # #####
# ##### # # # # # # #
# # # # # # # # # #
# # ##### #### # # ###### ######
def PBSfile(jname, qname, xcmd, depen=None, nodes=None, cores=None, mail=None, htime=None):
'''This function generates the PBS file to queue a simulation
'''
from datetime import timedelta as td
if htime is None:
t = str(td(hours=2.0))
else:
t = str(td(hours=htime))
sname = "{0}.sub".format(jname)
if nodes is None:
n = 1
else:
n = nodes
if (cores is None) or (qname == 'debug'):
c = 24
else:
c = cores
with open(sname, 'w') as f:
print("#!/bin/bash -l\n", file=f)
print("# FILENAME: {0}\n".format(sname), file=f)
print("#PBS -q " + qname, file=f)
print("#PBS -l nodes={0:d}:ppn={1:d},naccesspolicy=singleuser".format(n, c), file=f)
print("#PBS -l walltime={0}".format(t), file=f)
print("#PBS -N " + jname, file=f)
print("#PBS -o /scratch/brown/jruedabe/joboutput/{0}.out".format(jname), file=f)
print("#PBS -e /scratch/brown/jruedabe/joboutput/{0}.err".format(jname), file=f)
if depen is not None:
print("#PBS -W depend=afterok:{0}".format(depen), file=f)
if mail is not None:
print("#PBS -M {0}".format(mail), file=f)
print("#PBS -m bae", file=f)
print("Working at: {0}".format(os.getcwd()))
print("\ncd {0}".format(os.getcwd()), file=f)
if (c > 1) or not (qname == 'debug'):
print("export OMP_NUM_THREADS={0}".format(c), file=f)
print("\n# RUN", file=f)
print(xcmd, file=f)
return sname
#
# SLURM
#
def SlurmFile(jname, qname, xcmd, depen=None, nodes=None, cores=None, mail=None, htime=None, box=None):
'''
Description
-----------
This function generates the Slurm file to queue a simulation
'''
from datetime import timedelta as td
if htime is None:
t = str(td(hours=2.0))
else:
t = str(td(hours=htime))
sname = "{0}.sub".format(jname)
if nodes is None:
n = 1
else:
n = nodes
if (cores is None) or (qname == 'debug'):
c = 24
else:
c = cores
with open(sname, 'w') as f:
print("#!/bin/sh -l\n", file=f)
print("# FILENAME: {0}\n".format(sname), file=f)
print("#SBATCH -A {0}".format(qname), file=f)
print("#SBATCH -N {0:d}".format(n), file=f)
print("#SBATCH -n {0:d}".format(c), file=f)
print("#SBATCH --exclusive", file=f)
print("#SBATCH -t {0}".format(t), file=f)
print("#SBATCH --job-name={0}".format(jname), file=f)
print("#SBATCH -o /scratch/brown/jruedabe/joboutput/{0}.out".format(jname), file=f)
print("#SBATCH -e /scratch/brown/jruedabe/joboutput/{0}.err".format(jname), file=f)
if depen is not None:
print("#SBATCH --depend=afterok:{0}".format(depen), file=f)
if mail is not None:
print("#SBATCH --mail-user={0}".format(mail), file=f)
print("#SBATCH --mail-type=FAIL", file=f)
else:
print("#SBATCH --mail-type=NONE", file=f)
print("Working at: {0}".format(os.getcwd()))
print("\ncd {0}".format(os.getcwd()), file=f)
if (c > 1) or not (qname == 'debug'):
print("export OMP_NUM_THREADS={0}".format(c), file=f)
if box == 'brown':
print("module load intel impi hdf5", file=f)
print("\n# RUN", file=f)
print(xcmd, file=f)
return sname