-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradex_pipeline.py
276 lines (237 loc) · 11.3 KB
/
radex_pipeline.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
import os
import numpy as np
from joblib import Parallel, delayed
import time
start_time = time.time()
# Parameter Settings
molecule_0 = 'co'
molecule_1 = '13co'
molecule_2 = 'c18o'
model = '5d_coarse2'
sou_model = 'radex_model/'
num_cores = 20
linewidth = 15
Nco = np.arange(15.,20.1,0.2)
Tkin = np.arange(1.,2.8,0.1)
nH2 = np.arange(2.,5.1,0.2)
X_13co = np.arange(10,205,10)
X_c18o = np.arange(2,21,1)
round_dens = 1
round_temp = 1
# Pre-processing
incr_dens = round(Nco[1] - Nco[0],1)
incr_temp = round(Tkin[1] - Tkin[0],1)
co_dex = np.round(10**np.arange(0.,1.,incr_dens), 4)
Tk_dex = np.round(10**np.arange(0.,1.,incr_temp), 4)
num_Nco = Nco.shape[0]
num_Tk = Tkin.shape[0]
num_nH2 = nH2.shape[0]
factors_13co = 1./X_13co
factors_c18o = 1./X_c18o
cycle_dens = co_dex.shape[0]
cycle_temp = Tk_dex.shape[0]
num_X12to13 = X_13co.shape[0]
num_X13to18 = X_c18o.shape[0]
diff_Tk = Tkin[1] - Tkin[0]
def write_inputs_m0(i,j,k):
powi = str(i//cycle_temp + int(Tkin[0]))
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
powj = j//cycle_dens + int(nH2[0])
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prei = str(Tk_dex[i%cycle_temp])
prej = str(co_dex[j%cycle_dens])
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
prek = str(co_dex[k%cycle_dens])
codex = str(round(co_dex[k%cycle_dens], round_dens))
file = open('input_'+model+'_'+molecule_0+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'.inp','w')
file.write(molecule_0+'.dat\n')
file.write('output_'+model+'_'+molecule_0+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'.out\n')
file.write('100'+' '+'400'+'\n')
file.write(prei+'e'+powi+'\n')
file.write('1\n')
file.write('H2\n')
file.write(prej+'e'+n_h2+'\n')
file.write('2.73'+'\n')
file.write(prek+'e'+N_co+'\n')
file.write(str(linewidth)+'\n')
file.write('0\n')
file.close()
def write_inputs_m1(i,j,k,m):
powi = str(i//cycle_temp + int(Tkin[0]))
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
powj = j//cycle_dens + int(nH2[0])
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prei = str(Tk_dex[i%cycle_temp])
prej = str(co_dex[j%cycle_dens])
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
prek = str(round(factors_13co[m]*co_dex[k%cycle_dens],4))
x13co = str(X_13co[m])
codex = str(round(co_dex[k%cycle_dens], round_dens))
file = open('input_'+model+'_'+molecule_1+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'.inp','w')
file.write(molecule_1+'.dat\n')
file.write('output_'+model+'_'+molecule_1+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'.out\n')
file.write('100'+' '+'400'+'\n')
file.write(prei+'e'+powi+'\n')
file.write('1\n')
file.write('H2\n')
file.write(prej+'e'+n_h2+'\n')
file.write('2.73'+'\n')
file.write(prek+'e'+N_co+'\n')
file.write(str(linewidth)+'\n')
file.write('0\n')
file.close()
def write_inputs_m2(i,j,k,m,n):
powi = str(i//cycle_temp + int(Tkin[0]))
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
powj = j//cycle_dens + int(nH2[0])
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prei = str(Tk_dex[i%cycle_temp])
prej = str(co_dex[j%cycle_dens])
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
prek = str(round(factors_c18o[n]*factors_13co[m]*co_dex[k%cycle_dens],6))
x13co = str(X_13co[m])
xc18o = str(X_c18o[n])
codex = str(round(co_dex[k%cycle_dens], round_dens))
file = open('input_'+model+'_'+molecule_2+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'_'+xc18o+'.inp','w')
file.write(molecule_2+'.dat\n')
file.write('output_'+model+'_'+molecule_2+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'_'+xc18o+'.out\n')
file.write('100'+' '+'400'+'\n')
file.write(prei+'e'+powi+'\n')
file.write('1\n')
file.write('H2\n')
file.write(prej+'e'+n_h2+'\n')
file.write('2.73'+'\n')
file.write(prek+'e'+N_co+'\n')
file.write(str(linewidth)+'\n')
file.write('0\n')
file.close()
def run_radex_m0(i,j,k):
powj = j//cycle_dens + int(nH2[0])
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
codex = str(round(co_dex[k%cycle_dens], round_dens))
run = os.system('radex < input_'+model+'_'+molecule_0+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'.inp')
return run
def run_radex_m1(i,j,k,m):
powj = j//cycle_dens + int(nH2[0])
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
x13co = str(X_13co[m])
codex = str(round(co_dex[k%cycle_dens], round_dens))
run = os.system('radex < input_'+model+'_'+molecule_1+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'.inp')
return run
def run_radex_m2(i,j,k,m,n):
powj = j//cycle_dens + int(nH2[0])
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
x13co = str(X_13co[m])
xc18o = str(X_c18o[n])
codex = str(round(co_dex[k%cycle_dens], round_dens))
run = os.system('radex < input_'+model+'_'+molecule_2+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'_'+xc18o+'.inp')
return run
def radex_flux(i,j,k,m,n):
powj = j//cycle_dens + int(nH2[0])
Tk = str(round(diff_Tk*i + int(Tkin[0]), round_temp))
n_h2 = str(powj)
N_co = str(k//cycle_dens + int(Nco[0]))
prej_r = str(round(co_dex[j%cycle_dens], round_dens))
x13co = str(X_13co[m])
xc18o = str(X_c18o[n])
codex = str(round(co_dex[k%cycle_dens], round_dens))
outfile_0 = 'output_'+model+'_'+molecule_0+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'.out'
outfile_1 = 'output_'+model+'_'+molecule_1+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'.out'
outfile_2 = 'output_'+model+'_'+molecule_2+'/'+Tk+'_'+prej_r+'e'+n_h2+'_'+codex+'e'+N_co+'_'+x13co+'_'+xc18o+'.out'
# Extract reliable flux predictions (avoid those with convergence issues)
if np.loadtxt(outfile_0,skiprows=10,max_rows=1,dtype='str')[3] == '****':
flux_0 = np.full((3,),np.nan)
else:
flux_0 = np.genfromtxt(outfile_0, skip_header=13)[:,11]
if np.loadtxt(outfile_1,skiprows=10,max_rows=1,dtype='str')[3] == '****':
flux_1 = np.full((3,),np.nan)
else:
flux_1 = np.genfromtxt(outfile_1, skip_header=13)[:,11]
if np.loadtxt(outfile_2,skiprows=10,max_rows=1,dtype='str')[3] == '****':
flux_2 = np.full((3,),np.nan)
else:
flux_2 = np.genfromtxt(outfile_2, skip_header=13)[:,11]
return k,i,j,m,n,flux_0,flux_1,flux_2
# Run input files for molecules 0,1,2
print('Writing input files...')
Parallel(n_jobs=num_cores)(delayed(write_inputs_m0)(i,j,k) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
Parallel(n_jobs=num_cores)(delayed(write_inputs_m1)(i,j,k,m) for m in range(0,num_X12to13) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
Parallel(n_jobs=num_cores)(delayed(write_inputs_m2)(i,j,k,m,n) for n in range(0,num_X13to18) for m in range(0,num_X12to13) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
input_time = time.time()
print('Elapsed time for writing inputs: %s sec' % ((input_time - start_time)))
# Run RADEX for molecules 0,1,2
print('Running RADEX with inputs...')
Parallel(n_jobs=num_cores, verbose=5)(delayed(run_radex_m0)(i,j,k) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
Parallel(n_jobs=num_cores, verbose=5)(delayed(run_radex_m1)(i,j,k,m) for m in range(0,num_X12to13) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
Parallel(n_jobs=num_cores, verbose=5)(delayed(run_radex_m2)(i,j,k,m,n) for n in range(0,num_X13to18) for m in range(0,num_X12to13) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
radex_time = time.time()
print('Elapsed time for running RADEX: %s sec' % ((radex_time - input_time)))
# Construct 3D - 5D flux models
print('Constructing flux model grids...')
results = Parallel(n_jobs=num_cores, verbose=5)(delayed(radex_flux)(i,j,k,m,n) for n in range(0,num_X13to18) for m in range(0,num_X12to13) for k in range(num_Nco) for j in range(num_nH2) for i in range(num_Tk))
flux_co10 = np.full((num_Nco,num_Tk,num_nH2),np.nan)
flux_co21 = np.full((num_Nco,num_Tk,num_nH2),np.nan)
flux_co32 = np.full((num_Nco,num_Tk,num_nH2),np.nan)
flux_13co10 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13),np.nan)
flux_13co21 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13),np.nan)
flux_13co32 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13),np.nan)
flux_c18o10 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13,num_X13to18),np.nan)
flux_c18o21 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13,num_X13to18),np.nan)
flux_c18o32 = np.full((num_Nco,num_Tk,num_nH2,num_X12to13,num_X13to18),np.nan)
for result in results:
k, i, j, m, n, flux_0, flux_1, flux_2 = result
flux_co10[k,i,j] = flux_0[0]
flux_co21[k,i,j] = flux_0[1]
flux_co32[k,i,j] = flux_0[2]
flux_13co10[k,i,j,m] = flux_1[0]
flux_13co21[k,i,j,m] = flux_1[1]
flux_13co32[k,i,j,m] = flux_1[2]
flux_c18o10[k,i,j,m] = flux_2[0]
flux_c18o21[k,i,j,m,n] = flux_2[1]
flux_c18o32[k,i,j,m,n] = flux_2[2]
np.save(sou_model+'flux_'+model+'_co10.npy', flux_co10)
np.save(sou_model+'flux_'+model+'_co21.npy', flux_co21)
np.save(sou_model+'flux_'+model+'_co32.npy', flux_co32)
np.save(sou_model+'flux_'+model+'_13co10.npy', flux_13co10)
np.save(sou_model+'flux_'+model+'_13co21.npy', flux_13co21)
np.save(sou_model+'flux_'+model+'_13co32.npy', flux_13co32)
np.save(sou_model+'flux_'+model+'_c18o10.npy', flux_c18o10)
np.save(sou_model+'flux_'+model+'_c18o21.npy', flux_c18o21)
np.save(sou_model+'flux_'+model+'_c18o32.npy', flux_c18o32)
print('Flux models saved; elapsed time for construction: %s sec' % ((time.time() - radex_time)))
# Construct 5D ratio models
temp = np.repeat(flux_co21[:, :, :, np.newaxis], num_X12to13, axis=3)
co21_5d = np.repeat(temp[:, :, :, :, np.newaxis], num_X13to18, axis=4)
temp2 = np.repeat(flux_co10[:, :, :, np.newaxis], num_X12to13, axis=3)
co10_5d = np.repeat(temp2[:, :, :, :, np.newaxis], num_X13to18, axis=4)
c13o_21_5d = np.repeat(flux_13co21[:, :, :, :, np.newaxis], num_X13to18, axis=4)
c13o_32_5d = np.repeat(flux_13co32[:, :, :, :, np.newaxis], num_X13to18, axis=4)
ratio_co = co21_5d/co10_5d
ratio_13co = c13o_32_5d/c13o_21_5d
ratio_c18o = flux_c18o32/flux_c18o21
ratio_co213co = co21_5d/c13o_21_5d
ratio_co2c18o = co21_5d/flux_c18o21
ratio_13co2c18o_21 = c13o_21_5d/flux_c18o21
ratio_13co2c18o_32 = c13o_32_5d/flux_c18o32
np.save(sou_model+'ratio_'+model+'_co_21_10.npy',ratio_co)
np.save(sou_model+'ratio_'+model+'_13co_32_21.npy',ratio_13co)
np.save(sou_model+'ratio_'+model+'_c18o_32_21.npy',ratio_c18o)
np.save(sou_model+'ratio_'+model+'_co_13co_21.npy',ratio_co213co)
np.save(sou_model+'ratio_'+model+'_co_c18o_21.npy',ratio_co2c18o)
np.save(sou_model+'ratio_'+model+'_13co_c18o_21.npy',ratio_13co2c18o_21)
np.save(sou_model+'ratio_'+model+'_13co_c18o_32.npy',ratio_13co2c18o_32)
print('Ratio models saved.')
end_time = time.time()
print('Total lapsed time: %s sec' % ((end_time - start_time)))