-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkawano.py
executable file
·276 lines (216 loc) · 7.81 KB
/
kawano.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
# -*- coding: utf-8 -*-
import os
import itertools
import numpy
import argparse
from scipy.integrate import simps
from subprocess import Popen, PIPE
from collections import namedtuple
from common import UNITS, CONST, utils
from common.integrators import integrate_1D
from library import SM
T_kawano = 10 * UNITS.MeV
q = (SM.particles.hadrons.neutron['mass'] - SM.particles.hadrons.proton['mass'])
# q = 1.2933 * UNITS.MeV
m_e = SM.particles.leptons.electron['mass']
a = None
Particles = namedtuple("Particles", "electron neutrino")
particles = None
def init_kawano(electron=None, neutrino=None):
global particles
particles = Particles(electron=electron, neutrino=neutrino)
def run(data_folder, input="s4.dat", output="kawano_output.dat"):
p = Popen(utils.getenv('KAWANO', 'KAWANO/kawano_noneq'), stdin=PIPE, env={
"INPUT": os.path.join(data_folder, input),
"OUTPUT": os.path.join(data_folder, output)
})
p.communicate(bytes(os.linesep.join([
# ...
"",
# Run
"4",
# Go
"2",
# ...
"",
# Exit
"4",
# Output
"5",
# Request output file
"1",
# ...
"", "", "", ""
]), 'utf-8'))
with open(os.path.join(data_folder, output), "r") as kawano_output:
return kawano_output.read()
@numpy.vectorize
def _rate1(y):
""" n + ν_e ⟶ e + p """
E_e = q * a + y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* (1. - particles.electron.distribution(y_e)) * particles.neutrino.distribution(y))
@numpy.vectorize
def _rate2(y):
""" e + p ⟶ n + ν_e """
E_e = q * a + y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* particles.electron.distribution(y_e) * (1. - particles.neutrino.distribution(y)))
@numpy.vectorize
def _rate3(y):
""" n ⟶ e + ν_e' + p """
E_e = q * a - y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* (1. - particles.electron.distribution(y_e))
* (1. - particles.neutrino.distribution(y)))
@numpy.vectorize
def _rate4(y):
""" e + ν_e' + p ⟶ n """
E_e = q * a - y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* particles.electron.distribution(y_e) * particles.neutrino.distribution(y))
@numpy.vectorize
def _rate5(y):
""" n + e' ⟶ ν_e' + p """
E_e = -q * a + y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* particles.electron.distribution(y_e) * (1. - particles.neutrino.distribution(y)))
@numpy.vectorize
def _rate6(y):
""" ν_e' + p ⟶ n + e' """
E_e = -q * a + y
if E_e < m_e * a:
return 0.
y_e = numpy.sqrt(E_e**2 - (m_e * a)**2)
return (y**2 * y_e * E_e
* (1. - particles.electron.distribution(y_e)) * particles.neutrino.distribution(y))
def baryonic_rates(_a):
global a
a = _a
grid = particles.neutrino.grid
data = []
for integrand, bounds in [
(_rate1, (grid.MIN_MOMENTUM, grid.MAX_MOMENTUM)),
(_rate2, (grid.MIN_MOMENTUM, grid.MAX_MOMENTUM)),
(_rate3, (grid.MIN_MOMENTUM, (q - m_e) * a)),
(_rate4, (grid.MIN_MOMENTUM, (q - m_e) * a)),
(_rate5, ((q + m_e) * a, grid.MAX_MOMENTUM)),
(_rate6, ((q + m_e) * a, grid.MAX_MOMENTUM))
]:
if bounds[0] < bounds[1]:
data.append(CONST.rate_normalization / particles.neutrino.params.a**5
* integrate_1D(integrand, bounds=bounds)[0])
else:
data.append(0.)
return data
Plotting = namedtuple('Plotting', 'figure plots')
parameters_plots = None
rates_plots = None
heading = [
["t", 's', UNITS.s],
["x", 'MeV', UNITS.MeV],
["Tg", '10^9K', UNITS.K9],
["dTg/dt", '10^9K/s', UNITS.K9 / UNITS.s],
["rho_tot", 'g/cm^3', UNITS.g_cm3],
["H", '1/s', 1 / UNITS.s],
["n nue->p e", 'dimensionless', 1.],
["p e->n nue", 'dimensionless', 1.],
["n->p e nue", 'dimensionless', 1.],
["p e nue->n", 'dimensionless', 1.],
["n e->p nue", 'dimensionless', 1.],
["p nue->n e", 'dimensionless', 1.]
]
def import_data(filepath):
import pandas
with open(filepath) as f:
line = f.readline()
try:
line = line.split()
if int(line[0]):
f.readline()
except Exception:
pass
data = pandas.DataFrame(
({heading[i]: float(value) for i, value in enumerate(line.strip("\n").split("\t"))}
for line in f), columns=heading)
return data
def plot(data, label=None, save=None):
global parameters_plots, rates_plots
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['toolbar'] = 'None'
if not parameters_plots:
figure, plots = plt.subplots(3, 2, num="KAWANO parameters")
plots = list(itertools.chain(*plots))
figure.subplots_adjust(hspace=0.7, wspace=0.5)
# t[s] x Tg[10^9K] dTg/dt[10^9K/s] rho_tot[g cm^-3] H[s^-1]
parameters_plots = Plotting(figure=figure, plots=plots)
for plot in plots:
plot.set_xlabel("time, s")
plot.set_xscale("log")
plot.set_yscale("log")
plots[0].set_title("Scale factor")
plots[0].set_ylabel("a, 1")
plots[0].set_yscale("log")
plots[1].set_title("Temperature")
plots[1].set_ylabel("T, 10^9 K")
plots[2].set_title("Temperature derivative")
plots[2].set_ylabel("dT/dt, 10^9 K/s")
plots[2].set_yscale('linear')
plots[3].set_title("Total energy density")
plots[3].set_ylabel("rho, g/cm^3")
plots[4].set_title("Hubble rate")
plots[4].set_ylabel("H, s^-1")
plots[5].set_title("Nuclear rates")
plots[5].set_ylabel("rate")
if not rates_plots:
figure, plots = plt.subplots(3, 2, num="KAWANO rates")
plots = list(itertools.chain(*plots))
figure.subplots_adjust(hspace=0.7, wspace=0.5)
# n nue->p e p e->n nue n->p e nue p e nue->n n e->p nue p nue->n e
rates_plots = Plotting(figure=figure, plots=plots)
for i, plot in enumerate(plots, 6):
plot.set_title(heading[i])
plot.set_xlabel("time, s")
plot.set_xscale("log")
plot.set_ylabel("Rate")
plot.set_yscale("log")
time_series = data[heading[0]]
def bias(x):
return x if abs(x) > 1e-20 else 0.
parameters_plots.plots[0].plot(time_series, data[heading[1]].apply(bias))
parameters_plots.plots[1].plot(time_series, data[heading[2]].apply(bias))
parameters_plots.plots[2].plot(time_series, data[heading[3]].apply(bias))
parameters_plots.plots[3].plot(time_series, data[heading[4]].apply(bias))
parameters_plots.plots[4].plot(time_series, data[heading[5]].apply(bias))
rates = data.ix[:, 6:12]
for i, rate in enumerate(rates):
parameters_plots.plots[5].plot(time_series, rates[rate])
rates_plots.plots[i].plot(time_series, rates[rate], label=label)
if utils.getboolenv("SHOW_PLOTS", True):
plt.ion()
plt.show()
parameters_plots.figure.savefig(save + "_params.svg")
rates_plots.figure.savefig(save + "_rates.svg")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run KAWANO program for the given input file')
parser.add_argument('--folder', required=True)
parser.add_argument('--input', default='s4.dat')
parser.add_argument('--output', default='kawano_output.dat')
args = parser.parse_args()
print(run(args.folder, input=args.input, output=args.output))