-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecombinationHistory.py
More file actions
386 lines (321 loc) · 14.1 KB
/
RecombinationHistory.py
File metadata and controls
386 lines (321 loc) · 14.1 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import numpy as np
from matplotlib import pyplot as plt
import scipy.integrate as integrate
from scipy.interpolate import CubicSpline
from scipy.integrate import solve_ivp
import warnings
from Global import const
import BackgroundCosmology
warnings.filterwarnings('ignore')
class RecombinationHistory:
"""
This is a class for solving the recombination (and reionization) history of the Universe.
It holds recombination parameters and functions relevant for the recombination history.
Input Parameters:
cosmo (BackgroundCosmology) : The cosmology we use to solve for the recombination history
Yp (float): Primordial helium fraction
reionization (bool) : Include reionization or not
z_reion (float): Reionization redshift
delta_z_reion (float): Reionization width
helium_reionization (bool) : Include helium+ reionization
z_helium_reion (float): Reionization redshift for helium+
delta_z_helium_reion (float): Reionization width for helium+
Attributes:
tau_reion (float): The optical depth at reionization
z_star (float): The redshift for the LSS (defined as peak of visibility function or tau=1)
Functions:
tau_of_x (float->float) : Optical depth as function of x=log(a)
dtaudx_of_x (float->float) : First x-derivative of optical depth as function of x=log(a)
ddtauddx_of_x (float->float) : Second x-derivative of optical depth as function of x=log(a)
g_tilde_of_x (float->float) : Visibility function dexp(-tau)dx as function of x=log(a)
dgdx_tilde_of_x (float->float) : First x-derivative of visibility function as function of x=log(a)
ddgddx_tilde_of_x (float->float) : Second x-derivative of visibility function as function of x=log(a)
Xe_of_x (float->float) : Free electron fraction dXedx as function of x=log(a)
ne_of_x (float->float) : Electron number density as function of x=log(a)
"""
# Settings for solver
x_start = np.log(1e-8)
x_end = np.log(1.0)
npts = 1000
def __init__(self, BackgroundCosmology, Yp = 0.0,
reionization = False, z_reion = 11.0, delta_z_reion = 0.5,
helium_reionization = False, z_helium_reion = 3.5, delta_z_helium_reion = 0.5):
self.cosmo = BackgroundCosmology
self.Yp = Yp
self.reionization = reionization
self.z_reion = z_reion
self.delta_z_reion = delta_z_reion
self.helium_reionization = helium_reionization
self.z_helium_reion = z_helium_reion
self.delta_z_helium_reion = delta_z_helium_reion
#=========================================================================
# Methods availiable after solving
#=========================================================================
def tau_of_x(self,x):
if not hasattr(self, 'tau_of_x_spline'):
raise NameError('The spline tau_of_x_spline has not been created')
return self.tau_of_x_spline(x)
def dtaudx_of_x(self,x):
if not hasattr(self, 'dtaudx_of_x_spline'):
raise NameError('The spline dtaudx_of_x_spline has not been created')
return self.dtaudx_of_x_spline(x)
def ddtauddx_of_x(self,x):
if not hasattr(self, 'ddtauddx_of_x_spline'):
raise NameError('The spline ddtauddx_of_x_spline has not been created')
return self.ddtauddx_of_x_spline(x)
def g_tilde_of_x(self,x):
if not hasattr(self, 'g_tilde_of_x_spline'):
raise NameError('The spline g_tilde_of_x_spline has not been created')
return self.g_tilde_of_x_spline(x)
def dgdx_tilde_of_x(self,x):
if not hasattr(self, 'dgdx_tilde_of_x_spline'):
raise NameError('The spline dgdx_tilde_of_x_spline has not been created')
return self.dgdx_tilde_of_x_spline(x)
def ddgddx_tilde_of_x(self,x):
if not hasattr(self, 'ddgddx_tilde_of_x_spline'):
raise NameError('The spline ddgddx_tilde_of_x_spline has not been created')
return self.ddgddx_tilde_of_x_spline(x)
def Xe_of_x(self,x):
if not hasattr(self, 'log_Xe_of_x_spline'):
raise NameError('The spline log_Xe_of_x_spline has not been created')
return np.exp(self.log_Xe_of_x_spline(x))
def ne_of_x(self,x):
if not hasattr(self, 'log_ne_of_x_spline'):
raise NameError('The spline log_ne_of_x_spline has not been created')
return np.exp(self.log_ne_of_x_spline(x))
#=========================================================================
#=========================================================================
#=========================================================================
def info(self):
print("")
print("Recombination History:")
print("Yp: %8.7f" % self.Yp)
print("reionization: %8.7f" % self.reionization)
print("z_reion: %8.7f" % self.z_reion)
print("delta_z_reion: %8.7f" % self.delta_z_reion)
print("helium_reionization: %8.7f" % self.helium_reionization)
print("z_helium_reion: %8.7f" % self.z_helium_reion)
print("delta_z_helium_reion: %8.7f" % self.delta_z_helium_reion)
def solve(self):
"""
Main driver for doing all the solving
We first compute Xe(x) and ne(x)
Then we compute the optical depth tau(x) and the visibility function g(x)
"""
self.solve_number_density_electrons()
self.solve_for_optical_depth_tau()
def plot(self):
"""
Make some useful plots
"""
npts = 10000
xarr = np.linspace(self.x_start, self.x_end, num = npts)
Xe = [self.Xe_of_x(xarr[i]) for i in range(npts)]
ne = [self.ne_of_x(xarr[i]) for i in range(npts)]
tau = [self.tau_of_x(xarr[i]) for i in range(npts)]
dtaudx = [-self.dtaudx_of_x(xarr[i]) for i in range(npts)]
ddtauddx = [self.ddtauddx_of_x(xarr[i]) for i in range(npts)]
g_tilde = self.g_tilde_of_x(xarr)
dgdx_tilde = self.dgdx_tilde_of_x(xarr)
ddgddx_tilde = self.ddgddx_tilde_of_x(xarr)
# Recombination g_tilde
plt.xlim(-7.5,-6.5)
#plt.ylim(-4,6)
plt.title('Visibility function and derivatives close to recombination')
plt.plot(xarr, g_tilde, label = r'$\tilde{g}(x)$')
plt.plot(xarr, dgdx_tilde/15., label = r'$\frac{d\tilde{g} (x)}{dx}$')
plt.plot(xarr, ddgddx_tilde/300., label = r'$\frac{d^2\tilde{g} (x)}{dx^2}$')
plt.legend()
plt.show()
# Xe(x) of x
plt.yscale('log')
plt.title('Free electron fraction')
plt.plot(xarr, Xe, label = r'$X_e(x)$')
plt.legend()
plt.show()
# ne of x
plt.yscale('log')
plt.title('Electron numberdensity')
plt.plot(xarr, ne, label = r'$n_e(x)$')
plt.legend()
plt.show()
# tau
plt.yscale('log')
plt.title('Tau and derivatives')
plt.ylim(1e-8,1e8)
plt.xlim(-12.,0.)
plt.plot(xarr, tau, label = r'$\tau (x)$')
plt.plot(xarr, dtaudx, label = r'$\frac{d\tau (x)}{dx}$')
plt.plot(xarr, ddtauddx, label = r'$\frac{d^2\tau (x)}{dx^2}$')
plt.legend()
plt.show()
#=========================================================================
#=========================================================================
#=========================================================================
def solve_number_density_electrons(self):
"""
Solve for the evolution of the electron number density by solving
the Saha and Peebles equations
"""
#Transition Saha-Peebles
Xe_saha_limit = 0.99
# Settings for the arrays we use below
npts = self.npts
x_start = self.x_start
x_end = self.x_end
# Set up arrays to compute X_e and n_e on
x_array = np.linspace(x_start, x_end, num=npts)
Xe_arr = np.zeros(npts)
ne_arr = np.zeros(npts)
# Calculate recombination history
for i in range(npts):
# Current scale factor
x = x_array[i]
a = np.exp(x)
#==============================================================
# Get f_e from solving the Saha equation
#==============================================================
Xe_current = self.electron_fraction_from_saha_equation(x)
# Store the results from the Saha equation
Xe_arr[i] = Xe_current
# Two regimes: Saha and Peebles regime
if(Xe_current < Xe_saha_limit):
#==============================================================
# We need to solve the Peebles equation for the rest of the time
#==============================================================
# Physical constants
G = const.G
m_H = const.m_H
# Cosmological parameters
OmegaB = self.cosmo.OmegaB
H0 = self.cosmo.H0
# Make x-array for Peebles system from current time till the end
npts_aux = npts - (i + 1)
# Solve the Peebles ODE
Solution_ODE = solve_ivp(self.rhs_peebles_ode, [x, x_end], [Xe_current], t_eval = np.linspace(x, x_end, npts_aux))
# Fill up array with the result
Xe_arr = np.concatenate([Xe_arr[:-npts_aux], Solution_ODE.y[0]])
for j in range(npts):
ne_arr[j] = 3 * H0**2 * OmegaB * Xe_arr[j] / (8 * np.pi * G * m_H * np.exp(x_array[j])**3)
# We are done so exit for loop
break
# Make splines of log(Xe) and log(ne) as function of x = log(a)
self.log_Xe_of_x_spline = CubicSpline(x_array, np.log(Xe_arr))
self.log_ne_of_x_spline = CubicSpline(x_array, np.log(ne_arr))
def solve_for_optical_depth_tau(self):
"""
Solve for the optical depth tau(x) by integrating up
dtaudx = -c sigmaT ne/H
"""
# Set up x_array
npts = self.npts
x_end = self.x_start
x_start = self.x_end
x_array = np.linspace(self.x_start, self.x_end, num=npts)
# Set initial conditions for tau
tau_start = 0.
# Solve the tau ODE and normalize it such that tau(0) = 0.0
tau_Solution = np.flip(integrate.odeint(self.rhs_tau_ode, tau_start, np.linspace(x_start, x_end, num = npts), tfirst = True).flatten())
# Spline it up
self.tau_of_x_spline = CubicSpline(x_array, tau_Solution)
# Compute and spline the derivatives of tau
dtaudx = [self.rhs_tau_ode(x_array[i], 1.) for i in range(npts)]
ddtauddx = np.gradient(dtaudx, x_array)
self.dtaudx_of_x_spline = CubicSpline(x_array, dtaudx)
self.ddtauddx_of_x_spline = CubicSpline(x_array, ddtauddx)
# Compute and spline visibility function and it derivatives
g_tilde_of_x = -np.array(dtaudx) * np.exp(-tau_Solution)
dgdx_tilde_of_x = np.gradient(g_tilde_of_x, x_array)
ddgddx_tilde_of_x = np.gradient(dgdx_tilde_of_x, x_array)
self.g_tilde_of_x_spline = CubicSpline(x_array, g_tilde_of_x)
self.dgdx_tilde_of_x_spline = CubicSpline(x_array, dgdx_tilde_of_x)
self.ddgddx_tilde_of_x_spline = CubicSpline(x_array, ddgddx_tilde_of_x)
# Compute z_star (peak of visibility function or tau = 1)
xmax_i = np.argmax(g_tilde_of_x)
xmax = x_array[xmax_i]
amax = np.exp(xmax)
zmax = 1 / amax - 1
print("\nPeak of Visibility function or tau = 1:")
print("\tx0 = " + str(xmax))
print("\tz0 = " + str(zmax))
def electron_fraction_from_saha_equation(self,x):
"""
Solve the Saha equations for hydrogen and helium recombination
Returns: Xe, ne with Xe = ne/nH beging the free electron fraction
and ne the electon number density
"""
# Physical constants
k_b = const.k_b;
G = const.G;
c = const.c;
m_e = const.m_e;
hbar = const.hbar;
m_H = const.m_H;
epsilon_0 = const.epsilon_0;
xhi0 = const.xhi0;
xhi1 = const.xhi1;
# Cosmological parameters and variables
a = np.exp(x)
Yp = self.Yp
OmegaB = self.cosmo.OmegaB
TCMB = self.cosmo.TCMB
H0 = self.cosmo.H0
H = self.cosmo.H_of_x(x)
Tb = TCMB / a
nb = 3 * H0**2 * OmegaB / (8 * np.pi * G * m_H * a**3)
# Solve Saha equation for Xe
Saha_Coeff = (1 / nb) * (m_e * k_b * Tb/(2 * np.pi * hbar**2))**(3/2) * np.exp(-epsilon_0 / (k_b * Tb))
Coefficients = [1, Saha_Coeff, -Saha_Coeff]
Saha_Solution = np.roots(Coefficients)
Xe = Saha_Solution[1]
# Return Xe
return Xe
def rhs_tau_ode(self, x, y):
"""
Right hand side of the optical depth ODE dtaudx = RHS
"""
# Physical constants
c = const.c
sigma_T = const.sigma_T
# Cosmological parameters
H = self.cosmo.H_of_x(x)
ne = self.ne_of_x(x)
# Set the right hand side
dtaudx = - c * sigma_T * ne / H
return dtaudx
def rhs_peebles_ode(self, x, y):
"""
Right hand side of Peebles ODE for the free electron fraction dXedx = RHS
"""
# Solver variables
X_e = y[0];
a = np.exp(x);
# Physical constants
k_b = const.k_b
G = const.G
c = const.c
m_e = const.m_e
hbar = const.hbar
m_H = const.m_H
sigma_T = const.sigma_T
lambda_2s1s = const.lambda_2s1s
epsilon_0 = const.epsilon_0
# Cosmological parameters
Yp = self.Yp
OmegaB = self.cosmo.OmegaB
TCMB = self.cosmo.TCMB
H0 = self.cosmo.H0
H = self.cosmo.H_of_x(x)
Tb = TCMB / a
nb = 3 * H0**2 * OmegaB / (8 * np.pi * G * m_H * a**3)
nH = nb
n1s = (1 - X_e) * nH
lambda_alpha = H * (3 * epsilon_0)**3 / ((8 * np.pi)**2 * c**3 * hbar**3 * n1s)
phi2 = 0.448 * np.log(epsilon_0 / (k_b * Tb))
alpha2 = (8 / np.sqrt(3 * np.pi)) * c * sigma_T * np.sqrt(epsilon_0 / (k_b * Tb)) * phi2
beta = alpha2 * (m_e * k_b * Tb / (2 * np.pi * hbar**2))**(3/2) * np.exp(-epsilon_0 / (k_b * Tb))
beta2 = np.nan_to_num(beta * np.exp(3 * epsilon_0 / (4 * k_b * Tb)))
Cr = (lambda_2s1s + lambda_alpha) / (lambda_2s1s + lambda_alpha + beta2)
# Set right hand side of the Peebles equation
dXedx = (Cr / H) * (beta * (1 - X_e) - nH * alpha2 * X_e**2)
return dXedx