-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarBoNpy.py
198 lines (151 loc) · 6.42 KB
/
CarBoNpy.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
# -*- coding: utf-8 -*-
'''
CarBon_1.0.1.py - Kinetic Molecular Chemistry Code for Supernova Ejecta
Copyright (c) 2012-2016 Ethan Deneault
This file is part of CarBoN
'''
import numpy as np
import matplotlib.pyplot as plt
from assimulo.problem import Explicit_Problem
from assimulo.solvers import CVode
from sys import exit
import CarBoN_Input_Processor as cip
import datainput as d
import models as m
def chemnet(t,y):
'''
This function defines the rhs of the system of differential equations
used in the chemical network. It dynamically builds the equations from
the DataFrame constructed by m.KIDA_Input()
'''
if model_type=='Cons':
T,Ndens=m.constantD(t,dens=Ndensinit,T0=temperature)
else:
exit("No Model Loaded, Exiting Now.")
f=np.zeros([len(kida_spec.index)]) # Define the rhs array
#f -= 3*y/t
for num in range(len(list(kida_reac.index))):
in1=kida_reac.loc[num]['Input1']
in2=kida_reac.loc[num]['Input2']
out1=kida_reac.loc[num]['Output1']
out2=kida_reac.loc[num]['Output2']
out3=kida_reac.loc[num]['Output3']
alpha=kida_reac.loc[num]['alpha']
beta=kida_reac.loc[num]['beta']
gamma=kida_reac.loc[num]['gamma']
fo=kida_reac.loc[num]['Fo']
if num==0:
print('Still going! t={0}, Temp={1}'.format(t,T))
if in2!=0 and in2!=99:
f[in1] -= m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] * y[in2]
f[in2] -= m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] * y[in2]
f[out1] += m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] * y[in2]
if np.isnan(out2) == False and out2!=0:
f[out2] += m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] * y[in2]
if np.isnan(out3) == False and out3!=0:
f[out3] += m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] * y[in2]
elif in2==0:
f[in1] -= m.arrhenius(alpha, beta, gamma, T, fo) * y[in1]
f[out1] += m.arrhenius(alpha, beta, gamma, T, fo) * y[in1]
if np.isnan(out2) == False and out2!=0:
f[out2] += m.arrhenius(alpha, beta, gamma, T, fo) * y[in1]
if np.isnan(out3) == False and out3!=0:
f[out3] += m.arrhenius(alpha, beta, gamma, T, fo) * y[in1]
return f
'''
for num in range(len(list(grains_reac.index))):
in1=grains_reac.loc[num]['Input1']
in2=grains_reac.loc[num]['Input2']
out1=grains_reac.loc[num]['Output1']
out2=grains_reac.loc[num]['Output2']
fijk=kida_reac.loc[num]['f_ijk']
kij=kida_reac.loc[num]['K_ij']
Hamaker=kida_reac.loc[num]['Hamaker']
fo=kida_reac.loc[num]['Fo']
f[in1] -= kij * np.sqrt(T) * m.VdW(r1,r2,T,Hamaker)
f[in2] -= kij * np.sqrt(T) * m.VdW(r1,r2,T,Hamaker)
f[out1] += fijk * kij * np.sqrt(T) * m.VdW(r1,r2,T,Hamaker)
f[out2] += (1-fijk) * kij * np.sqrt(T) * m.VdW(r1,r2,T,Hamaker)
'''
########### Reactions with moderators needs rewrite!
#
# elif in2==99:
# f[in1] -= Ndens*m.arrhenius(alpha,beta,gamma,T,fo) \
# * y[in1] * (kida_spec.loc[0]['species_num']\
# + kida_spec.loc[2]['species_num'] \
# + monoatomic_list[2])
# f[out1] += Ndens*m.arrhenius(alpha,beta,gamma,T,fo) \
# * y[in1] * (kida_spec.loc[0]['species_num'] + kida_spec.loc[1]['species_#'] \
# + monoatomic_list[2])
# if isinstance(out2,int) == True:
# f[out2] += Ndens*m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] \
# * (kida_spec.loc[0]['species_num'] + kida_spec.loc[1]['species_#'] \
# + monoatomic_list[2])
# if isinstance(out3,int) == True:
# f[out3] += Ndens*m.arrhenius(alpha,beta,gamma,T,fo) * y[in1] \
# * (kida_spec.loc[0]['species_num'] + kida_spec.loc[1]['species_#'] \
# + monoatomic_list[2])
############ NEEDS CLEANUP
"""
Import data from the settings file, the KIDA database files and the initial abundances file.
"""
# This is to read files from whatever they are stored in.
file_format, species_file, reactions_file, output_file, model_type, density, temperature, start_time, end_time, outfile = d.settings()
Kida_file = cip.Kida("data/kida_reac_C_O_Si_only.dat", "data/kida_spec_C_O_Si_only.dat")
Kida_file.read_species()
Kida_file.read_reactions()
kida_reac, kida_spec, spec_dict = Kida_file.output()
print(kida_reac)
print(kida_spec)
print(spec_dict)
abund_df = d.abundances(spec_dict)
"""
Initialize the variables
"""
yinit = np.zeros([len(kida_spec.index)])
yinit[abund_df.Species.values] = abund_df.Abundance.values
Ndensinit = density #np.sum(yinit)*density
"""
Initialize Assimulo, calculate with CVode.
"""
start_time *= 86400
end_time *= 86400
model=Explicit_Problem(chemnet,yinit,start_time)
model.name='Chemnet Test'
sim=CVode(model)
sim.atol=1.e-12
sim.rtol=1.e-12
sim.maxord=3
sim.discr='BDF'
sim.iter='Newton'
t,y=sim.simulate(end_time)
#sim.plot()
t = np.array(t)/86400
plt.ylim([1e5,2e10])
plt.semilogy(t,y[:,1],label='C')
plt.semilogy(t,y[:,2],label='O')
plt.semilogy(t,y[:,3],label='C2')
plt.semilogy(t,y[:,4],label='CO')
#plt.semilogy(t,y[:,5],label='C3')
#plt.semilogy(t,y[:,6],label='C4')
plt.semilogy(t,y[:,7],label='Si')
plt.semilogy(t,y[:,8],label='SiC')
plt.semilogy(t,y[:,9],label='SiO')
#plt.semilogy(t,y[:,10],label='Si2O2')
#plt.semilogy(t,y[:,11], "-.",label='Si2C2')
#plt.semilogy(t,y[:,12], "-.",label='O2')
#plt.semilogy(t,y[:,13], "-.",label='C+')
#plt.semilogy(t,y[:,14], "-.",label='O+')
#plt.semilogy(t,y[:,15], "-.",label='Si+')
#plt.semilogy(t,y[:,16], "-.",label='CO+')
#plt.semilogy(t,y[:,17], "-.",label='C2+')
#plt.semilogy(t,y[:,18], "-.",label='SiC+')
#plt.semilogy(t,y[:,19], "-.",label='e-')
plt.legend()
plt.show()
##############################################################################
# The following writes to a data file
##############################################################################
#
#np.savez(output_file,time=time,speciesidx=kida_spec['species_num'].to_dict(), y = y,
# abundance=abundance,speciesmass=kida_spec['atoms_num'].to_dict())