-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_rates.py
313 lines (295 loc) · 10.5 KB
/
new_rates.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
import numpy as np
import os
import math
from sympy import FU
ksi = 5e-17 # cr rate is 1/s
Av = 15
T = 10 # calculate uncertainties for T=10K
kb = 1.38e-16 # Boltzmann in cgs
a_dust = 1e-5 # grain radius in cm
amu = 1.66e-24 # atomic mass in g
cr_duration = 1e-5 # peak duration after CR hits dust
cr_T = 70 # peak temperature afte CR hits dust
Fe_ion = 3e-14
n_s = 1.5e15 # site density on grains in 1/cm2
N_s = n_s * 4 * np.pi * a_dust**2 # number of sites on 1 grain
nh2 = 1e5 # h2 density in 1/cm3, overall gas density basically
chem_barr_thick = 1e-8 # chemical barrier thickness in cm
hbar = 1.055e-27 # hbar in cgs
FUV = 1.
UVCR = ksi/1.3e-17
gotdn = 100.
eb_file = 'pnautius_own/example_simulation/surface_parameters.in'
br_f = 'branching_ratios.txt'
def get_stick(species):
if species[-1] == '+' or species[-1] == '-':
return 0
else:
return 1
def get_masses(species):
mass = []
for spec in species:
ms = 0
for i in range(spec):
if spec[i] == 'H':
if i+1 < len(spec) and spec[i+1] == 'e':
ms += 4
else:
ms += 1
elif spec[i] == 'C':
if i+1 < len(spec) and spec[i+1] == 'l':
ms += 31
else:
ms += 12
elif spec[i] == 'N':
if i+1 < len(spec) and spec[i+1] == 'a':
ms += 23
else:
ms += 14
elif spec[i] == 'O':
ms += 16
elif spec[i] == 'S':
if i+1 < len(spec) and spec[i+1] == 'i':
ms += 28
else:
ms += 32
elif spec[i] == 'F':
if i+1 < len(spec) and spec[i+1] == 'e':
ms += 56
else:
ms += 19
elif spec[i] == 'M':
ms += 24
elif spec[i] == 'P':
ms += 31
mass.append(ms)
return mass
def get_Eb(spec):
with open() as f:
for row in f:
if row.split()[0] == spec:
val = row.split()
eb = float(val[3])
ed = float(val[2])
if eb == 0 and spec[0] == 'J':
return ed / 0.4
elif eb == 0 and spec[0] == 'K':
return ed / 0.8
else:
return eb
def get_ED(spec):
with open() as f:
for row in f:
if row.split()[0] == spec:
return float(row.split()[2])
def vib_freq(spec, E_b, mass):
return np.sqrt(2*kb/(np.pi**2*amu) * n_s * E_b/mass)
def calc_barr(species, Ea):
m = get_masses(species) # get both reactant's masses
redmas = m[0]*m[1]/np.sum(m)
surf_react_proba = 2 * (chem_barr_thick/hbar) * np.sqrt(2*amu*redmas*kb*Ea)
if Ea < surf_react_proba:
return np.exp(-Ea/T)
else:
return np.exp(-surf_react_proba)
def calc_diff(species):
m = get_masses(species)
Eb1 = get_Eb(species[0])
Eb2 = get_Eb(species[1])
diff_bar1 = get_ED(species[0])
diff_bar2 = get_ED(species[1])
nu1 = vib_freq(species[0], Eb1, m[0])
nu2 = vib_freq(species[1], Eb2, m[1])
return (nu1*np.exp(-diff_bar1/T) + nu2*np.exp(-diff_bar2/T)) / N_s
def get_r_from_br(line):
items = line.split()
r_br = []
for item in items:
if item == '->':
break
else:
r_br.append(item)
return r_br
def comp_r(r1, r2):
rcheck = False
for item in r1:
rcheck = item in r2
if rcheck == False:
break
return rcheck
def rate(a, b, c, typ, reactants):
if typ == '0':
return a * (T/300)**b # from ode_solver.f90
elif typ == '1':
return a * ksi
elif typ == '2':
return a * np.exp(-c*Av)
elif typ == '3':
return a * (T/300)**b * np.exp(-c/T)
elif typ == '4':
return a * b * (0.62 + 0.4767*c*np.sqrt(300/T))
elif typ == '5':
return a * b * (1. + 0.0967*c*np.sqrt(300/T) + (c**2/10.526)*300/T)
elif typ == '10':
return a # according to ode_solver.f90, these two aren't relevant for us since surface reactions are enabled
elif typ == '11':
return a
elif typ == '14':
with open(br_f) as B:
for row in B:
rea = get_r_from_br(row)
if comp_r(reactants, rea) == True:
BR = float(row.split()[-2])
Ea = float(row.split()[-1])
BARR = calc_barr(reactants, Ea)
DIFF = calc_diff(reactants)
return a * BR * BARR * DIFF * gotdn / nh2
elif typ == '15':
m = get_masses(reactants)
Eb = get_Eb(reactants[0])
nu = vib_freq(reactants[0], Eb, m[0])
return a * 1 * nu * np.exp(-Eb/T)
# branching ratio is assumet to be one (no info, should not change much in the lognoral dist)
elif typ == '16':
m = get_masses(reactants)
Eb = get_Eb(reactants[0])
nu = vib_freq(reactants[0], Eb, m[0])
return a * 1 * (ksi/1e3-17) * nu * Fe_ion * cr_duration * np.exp(-Eb/cr_T)
elif typ == '17' or typ == '18':
return a * ksi * nh2 * 2
elif typ == '19' or typ == '20':
return a * np.exp(-c*Av) * FUV
elif typ == '66':
return a * FUV * 1e8 * np.exp(-2*Av) / n_s
elif typ == '67':
return a * 1e4 * UVCR / n_s
elif typ == '99': # this is only if species is neutral
m = get_masses(reactants)
S = get_stick(reactants[0])
if S == 1:
pre = np.pi * a_dust**2 * np.sqrt(8*kb/(np.pi*amu)) / np.sqrt(m)
return a * 1 * pre * np.sqrt(T) * nh2 / gotdn
# assume branching ratio is one (did not find for these reactions)
else:
return a
def new_a(k, b, c, typ, reactants):
if typ == '0':
new = k / (T/300)**b
elif typ == '1':
new = k / ksi
elif typ == '2':
new = k / np.exp(-c*Av)
elif typ == '3':
new = k / ((T/300)**b * np.exp(-c/T))
elif typ == '4':
new = k / (b * (0.62 + 0.4767*c*np.sqrt(300/T)))
elif typ == '5':
new = k / (b * (1. + 0.0967*c*np.sqrt(300/T) + (c**2/10.526)*300/T))
elif typ == '10':
new = k
elif typ == '11':
new = k
elif typ == '14':
with open(br_f) as B:
for row in B:
rea = get_r_from_br(row)
if comp_r(reactants, rea) == True:
BR = float(row.split()[-2])
Ea = float(row.split()[-1])
BARR = calc_barr(reactants, Ea)
DIFF = calc_diff(reactants)
new = k / (BR * BARR * DIFF * gotdn / nh2)
elif typ == '15':
m = get_masses(reactants)
Eb = get_Eb(reactants[0])
nu = vib_freq(reactants[0], Eb, m[0])
new = k / (1 * nu * np.exp(-Eb/T))
# branching ratio is assumed to be 1
elif typ == '16':
m = get_masses(reactants)
Eb = get_Eb(reactants[0])
nu = vib_freq(reactants[0], Eb, m[0])
new = k / (1 * (ksi/1e3-17) * nu * Fe_ion * cr_duration * np.exp(-Eb/cr_T))
elif typ == '17' or typ == '18':
new = k / (ksi * nh2 * 2)
elif typ == '19' or typ == '20':
new = k / (np.exp(-c*Av) * FUV)
elif typ == '66':
new = k / (FUV * 1e8 * np.exp(-2*Av) / n_s)
elif typ == '67':
new = k / (1e4 * UVCR / n_s)
elif typ == '99': # this is only if species is neutral
m = get_masses(reactants)
S = get_stick(reactants[0])
if S == 1:
pre = np.pi * a_dust**2 * np.sqrt(8*kb/(np.pi*amu)) / np.sqrt(m)
new = k / (1 * pre * np.sqrt(T) * nh2 / gotdn)
# one is branching ration, did not findfor these reactions, so assume
else:
new = k
if new == float('inf') or new == float('-inf') or math.isnan(new) == True: # just to make sure
return 1e-13
else:
return max([new, 1e-13]) # 1e-13 basically turns it off, smallest than every in the original
def new_params(line):
values = line.split()
F0_typ = values[-8] # logn or NA ()
F0 = 2. # say it's 2 in default (in case NA)
if F0_typ == 'logn' and float(values[-10]) != 0:
F0 = float(values[-10])
typ = values[-4]
alpha = float(values[-13])
beta = float(values[-12])
gamma = float(values[-11])
rrrr = line[:23].split()
k0 = rate(alpha, beta, gamma, typ, rrrr)
N = np.random.normal(0, 1)
k = np.exp(np.log(k0) + np.log(F0)*N)
return '{:.3e}'.format(new_a(k, beta, gamma, typ, rrrr))
def self_check(line, a):
b = line.split()[-12]
c = line.split()[-11]
if a == b:
print('Beta is tha same as alpha.')
elif a == c:
print('Gamma is tha same as alpha.')
def write(new_path):
original_path = '/lustre/astro/gfriss/og_inputs'
original_gas = os.path.join(original_path, 'gas_reactions.in')
original_grain = os.path.join(original_path, 'grain_reactions.in')
with open(original_gas, mode = 'r') as og:
rows = og.readlines()
with open(os.path.join(new_path,'gas_reactions.in'), mode = 'w') as f:
for row in rows:
if row[0] == '!':
f.write(row)
elif row == rows[-1]:
new_alpha = new_params(row)
old_alpha = row.split()[-13]
self_check(row, old_alpha)
new_row = row.replace(old_alpha, new_alpha).rstrip()
f.write(new_row)
else:
new_alpha = new_params(row)
old_alpha = row.split()[-13]
self_check(row, old_alpha)
new_row = row.replace(old_alpha, new_alpha)
f.write(new_row)
with open(original_grain, mode = 'r') as og:
rows = og.readlines()
with open(os.path.join(new_path, 'grain_reactions.in'), mode = 'w') as f:
for row in rows:
if row[0] == '!':
f.write(row)
elif row == rows[-1]:
new_alpha = new_params(row)
old_alpha = row.split()[-13]
self_check(row, old_alpha)
new_row = row.replace(old_alpha, new_alpha).rstrip()
f.write(new_row)
else:
new_alpha = new_params(row)
old_alpha = row.split()[-13]
self_check(row, old_alpha)
new_row = row.replace(old_alpha, new_alpha)
f.write(new_row)