-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyrun.py
More file actions
174 lines (126 loc) · 4.42 KB
/
pyrun.py
File metadata and controls
174 lines (126 loc) · 4.42 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
#run the primordial chem network one zone test in python
from mynet import rates, sym_rates, massfracs
import constants as cons
import rate
import rate_collection as ratecol
import numpy as np
from scipy.integrate import solve_ivp
import functools
from pylab import genfromtxt
import sympy as sp
from operator import mul, add
method = int(input('enter 1 for pure python calculation, 2 for sympy calculation '))
if method == 1:
object = ratecol.ChemRateCollection(rates=list(rates.values()), tdot_switch=0)
print('Using dT/dt')
def main_rhs(t,y):
return object.rhs(t,y)
def main_fft(y):
return object.fft(y)
elif method == 2:
if massfracs == 1:
raise ValueError('Sympy calculation can only work with number densities!')
object = ratecol.SympyChemRateCollection(rates=list(sym_rates.values()), tdot_switch=0, ydots_lambdified=True)
def main_rhs(t,y):
return object.rhs_numdens(t,y)
def main_fft(y):
return object.fft_numdens(y)
def main_jac(t,y):
return object.jac_numdens(t,y)
else:
raise ValueError('Invalid choice!')
def normalize(val):
aal = [max(0,q) for q in val]
bal = [q/sum(aal) for q in aal]
return bal
masses = [cons.mass_elec, cons.mass_hp, cons.mass_h, cons.mass_hm, cons.mass_dp, cons.mass_d, cons.mass_h2p, \
cons.mass_dm, cons.mass_h2, cons.mass_hdp, cons.mass_hd, cons.mass_hepp, cons.mass_hep, cons.mass_he]
krome = genfromtxt('datafiles/fort22_wD.dat').T
ntot = 1.0 #number density
Tgas = [1e2]
Eint = [10254504084.93553]
y0_labels = ['elec', 'H+', 'H', 'H-', 'D+', 'D', 'H2+', 'D-', 'H2', 'HD+', 'HD', 'He++', 'He+', 'He', 'Tgas']
#number densities of each specie
y0 = [1e-4, 1e-4, 1e0, 1e-40, 1e-40, 1e-40, 1e-40, 1e-40, 1e-6, 1e-40, 1e-40, 1e-40, 1e-40, 0.0775e0]
y0 = [x*ntot for x in y0]
dd = ntot
abd_elec = []
abd_hp = []
abd_h = []
abd_hm = []
abd_dp = []
abd_d = []
abd_h2p = []
abd_dm = []
abd_h2 = []
abd_hdp = []
abd_hd = []
abd_hepp = []
abd_hep = []
abd_he = []
temp = []
time = []
dens = []
rstep = 1
rstep_max = int(input('Input max number of steps '))
tff_fac = float(input('Input fac, where dt = fac * tff '))
while rstep < rstep_max:
dd1 = dd
#at each step, y0 just consists of the species, nont the temperature (see below)
tff = main_fft(y0)
dtH = tff_fac * tff #define time-step
deldd = (dd/tff) * dtH #density increase
dd = dd + deldd #update density
#add temperature separately here
if rstep == 1:
y0 = [x*dd/dd1 for x in y0] + Tgas
else:
y0 = [x*dd/dd1 for x in y0] + [sol.y[14][0]]
dt = dtH
if dd > 1e18:
break
if dt < 10:
break
#charge conservation
y0[0] = -y0[3] - y0[7] + y0[1] + y0[12] + y0[6] + y0[4] + y0[9] + 2.0*y0[11]
#integrate from 0 till dt, and put sol.y at dt as the updated y0
if method == 1:
sol = solve_ivp(main_rhs, t_span=[0,dt], t_eval=[dt], y0=y0, method='BDF', atol=1e-4, rtol=1e-4)
elif method == 2:
sol = solve_ivp(fun=lambda t, y: main_rhs(t,y), t_span=[0,dt], t_eval=[dt], y0=y0, method='BDF', atol=1e-4, rtol=1e-4)
#mass conservation?
#ns = sol.y.flatten()[:-1]
#xs = [x*y for x,y in zip(ns, masses)] #ns*mu*mh
#rho = sum(xs)
#xs = [x/rho for x in xs] #ns*mu*mh/rho = xs
#xs = normalize(xs)
#a1 = [x/y for x,y in zip(xs, masses)] #xs/(mu*mh)
#y0 = [rho*x for x in a1] #xs*rho/(mu*mh) = ns
if not sol.success:
breakpoint()
#charge conservation
y0[0] = -y0[3] - y0[7] + y0[1] + y0[12] + y0[6] + y0[4] + y0[9] + 2.0*y0[11]
#recreate y0 but do not include temperature
idx = 0
y0 = [sol.y[0][idx], sol.y[1][idx], sol.y[2][idx], sol.y[3][idx], sol.y[4][idx], sol.y[5][idx], sol.y[6][idx], sol.y[7][idx], \
sol.y[8][idx], sol.y[9][idx], sol.y[10][idx], sol.y[11][idx], sol.y[12][idx], sol.y[13][idx]]
temp.append(sol.y[14][idx])
time.append(dt)
dens.append(dd)
abd_elec.append(y0[0])
abd_hp.append(y0[1])
abd_h.append(y0[2])
abd_hm.append(y0[3])
abd_dp.append(y0[4])
abd_d.append(y0[5])
abd_h2p.append(y0[6])
abd_dm.append(y0[7])
abd_h2.append(y0[8])
abd_hdp.append(y0[9])
abd_hd.append(y0[10])
abd_hepp.append(y0[11])
abd_hep.append(y0[12])
abd_he.append(y0[13])
rhotot = sum([a*b for a,b in zip(y0,masses)])
print('%0.10e %0.10e %0.10e %d' %(dd, rhotot, sol.y[14][idx], rstep))
rstep += 1