Skip to content

Commit bcc8ef0

Browse files
committed
port to C, operate on arrays of r
1 parent 3cc6648 commit bcc8ef0

File tree

4 files changed

+303
-73
lines changed

4 files changed

+303
-73
lines changed

cluster_toolkit/pressure_profile.py

Lines changed: 163 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,136 @@
1717
obtain a more precise answer.
1818
'''
1919

20+
from cluster_toolkit import _dcast, _lib
2021
import numpy as np
2122
from scipy.integrate import quad
2223
import scipy.special as spec
2324

2425

26+
def P_BBPS(r, M, z, omega_b, omega_m,
27+
params_P_0=(18.1, 0.154, -0.758),
28+
params_x_c=(0.497, -0.00865, 0.731),
29+
params_beta=(4.35, 0.0393, 0.415),
30+
alpha=1, gamma=-0.3,
31+
delta=200):
32+
'''
33+
The best-fit pressure profile presented in BBPS2.
34+
35+
Args:
36+
r (float or array): Radii from the cluster center, \
37+
in Mpc :math:`h^{-2/3}`. If an array, an array \
38+
is returned, if a scalar, a scalar is returned.
39+
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
40+
z (float): Cluster redshift.
41+
omega_b (float): Baryon fraction.
42+
omega_m (float): Matter fraction.
43+
params_P_0 (tuple): 3-tuple of :math:`P_0` mass, redshift dependence \
44+
parameters A, :math:`\\alpha_m`, :math:`\\alpha_z`, \
45+
respectively. See BBPS2 Equation 11.
46+
params_x_c (tuple): 3-tuple of :math:`x_c` mass, redshift dependence, \
47+
same as `params_P_0`.
48+
params_beta (tuple): 3-tuple of :math:`\\beta` mass, redshift \
49+
dependence, same as `params_P_0`.
50+
51+
Returns:
52+
float: Pressure at distance `r` from the cluster, in units of \
53+
:math:`h^{8/3} Msun s^{-2} Mpc^{-1}`.
54+
'''
55+
r = np.asarray(r, dtype=np.double)
56+
57+
scalar_input = False
58+
if r.ndim == 0:
59+
scalar_input = True
60+
# Convert r to 2d
61+
r = r[None]
62+
if r.ndim > 1:
63+
raise Exception('r cannot be a >1D array.')
64+
65+
P_out = np.zeros_like(r, dtype=np.double)
66+
67+
# Set parameters
68+
P_0 = _A_BBPS(M, z, *params_P_0)
69+
x_c = _A_BBPS(M, z, *params_x_c)
70+
beta = _A_BBPS(M, z, *params_beta)
71+
72+
_lib.P_BBPS(_dcast(P_out),
73+
_dcast(r), len(r),
74+
M, z,
75+
omega_b, omega_m,
76+
P_0, x_c, beta,
77+
float(alpha), gamma,
78+
delta)
79+
80+
if scalar_input:
81+
return np.squeeze(P_out)
82+
return P_out
83+
84+
85+
def projected_P_BBPS(r, M, z, omega_b, omega_m,
86+
params_P=(18.1, 0.154, -0.758),
87+
params_x_c=(0.497, -0.00865, 0.731),
88+
params_beta=(4.35, 0.0393, 0.415),
89+
alpha=1, gamma=-0.3,
90+
delta=200,
91+
limit=1000,
92+
epsabs=1e-15, epsrel=1e-3,
93+
return_errs=False):
94+
'''
95+
Computes the projected line-of-sight pressure of a cluster at a radius r
96+
from the cluster center.
97+
98+
Args:
99+
r (float or array): Radius from the cluster center, in Mpc.
100+
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
101+
z (float): Cluster redshift.
102+
omega_b (float): Baryon fraction.
103+
omega_m (float): Matter fraction.
104+
105+
Returns:
106+
float or array: Integrated line-of-sight pressure at distance `r` from \
107+
the cluster, in units of Msun s^{-2} h^{8/3}.
108+
'''
109+
r = np.asarray(r, dtype=np.double)
110+
111+
scalar_input = False
112+
if r.ndim == 0:
113+
scalar_input = True
114+
# Convert r to 2d
115+
r = r[None]
116+
if r.ndim > 1:
117+
raise Exception('r cannot be a >1D array.')
118+
119+
P_out = np.zeros_like(r, dtype=np.double)
120+
P_err_out = np.zeros_like(r, dtype=np.double)
121+
122+
# Set parameters
123+
P_0 = _A_BBPS(M, z, *params_P)
124+
x_c = _A_BBPS(M, z, *params_x_c)
125+
beta = _A_BBPS(M, z, *params_beta)
126+
127+
rc = _lib.projected_P_BBPS(_dcast(P_out), _dcast(P_err_out),
128+
_dcast(r), len(r),
129+
M, z,
130+
omega_b, omega_m,
131+
P_0, x_c, beta,
132+
alpha, gamma,
133+
delta,
134+
limit,
135+
epsabs, epsrel)
136+
137+
if rc != 0:
138+
msg = 'C_projected_P_BBPS returned error code: {}'.format(rc)
139+
raise RuntimeError(msg)
140+
141+
if scalar_input:
142+
if return_errs:
143+
return np.squeeze(P_out), np.squeeze(P_err_out)
144+
return np.squeeze(P_out)
145+
if return_errs:
146+
return P_out, P_err_out
147+
return P_out
148+
149+
25150
def _rho_crit(z, omega_m):
26151
'''
27152
The critical density of the universe :math:`\\rho_{crit}`, in units of
@@ -45,7 +170,7 @@ def R_delta(M, z, omega_m, delta=200):
45170
46171
Args:
47172
M (float): Halo mass :math:`M_{\\Delta}`, in units of Msun.
48-
omega_b (float): The baryon fraction :math:`\\Omega_b`.
173+
z (float): Redshift to the cluster center.
49174
omega_m (float): The matter fraction :math:`\\Omega_m`.
50175
delta (float): The halo overdensity :math:`\\Delta`.
51176
@@ -111,84 +236,24 @@ def P_simple_BBPS(x, M, z):
111236
return P_simple_BBPS_generalized(x, M, z, P_0, x_c, beta)
112237

113238

114-
def P_BBPS(r, M, z, omega_b, omega_m,
115-
params_P_0=(18.1, 0.154, -0.758),
116-
params_x_c=(0.497, -0.00865, 0.731),
117-
params_beta=(4.35, 0.0393, 0.415),
118-
alpha=1, gamma=-0.3,
119-
delta=200):
120-
'''
121-
The best-fit pressure profile presented in BBPS2.
122-
123-
Args:
124-
r (float): Radius from the cluster center, in Mpc :math:`h^{-2/3}`.
125-
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
126-
z (float): Cluster redshift.
127-
omega_b (float): Baryon fraction.
128-
omega_m (float): Matter fraction.
129-
params_P_0 (tuple): 3-tuple of :math:`P_0` mass, redshift dependence \
130-
parameters A, :math:`\\alpha_m`, :math:`\\alpha_z`, \
131-
respectively. See BBPS2 Equation 11.
132-
params_x_c (tuple): 3-tuple of :math:`x_c` mass, redshift dependence, \
133-
same as `params_P_0`.
134-
params_beta (tuple): 3-tuple of :math:`\\beta` mass, redshift \
135-
dependence, same as `params_P_0`.
136-
137-
Returns:
138-
float: Pressure at distance `r` from the cluster, in units of \
139-
:math:`h^{8/3} Msun s^{-2} Mpc^{-1}`.
140-
'''
141-
P_0 = _A_BBPS(M, z, *params_P_0)
142-
x_c = _A_BBPS(M, z, *params_x_c)
143-
beta = _A_BBPS(M, z, *params_beta)
144-
145-
x = r / R_delta(M, z, omega_m, delta=delta)
146-
Pd = P_delta(M, z, omega_b, omega_m, delta=delta)
147-
return Pd * P_simple_BBPS_generalized(x, M, z, P_0, x_c, beta,
148-
alpha=alpha, gamma=gamma, delta=delta)
149-
150-
151-
def projected_P_BBPS(r, M, z, omega_b, omega_m,
152-
dist=8, epsrel=1e-3):
153-
'''
154-
Computes the projected line-of-sight density of a cluster at a radius r
155-
from the cluster center.
156-
157-
Args:
158-
r (float): Radius from the cluster center, in Mpc.
159-
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
160-
z (float): Cluster redshift.
161-
omega_b (float): Baryon fraction.
162-
omega_m (float): Matter fraction.
163-
164-
Returns:
165-
float: Integrated line-of-sight pressure at distance `r` from the \
166-
cluster, in units of Msun s^{-2}.
167-
'''
168-
R_del = R_delta(M, z, omega_m)
169-
return quad(lambda x: P_BBPS(np.sqrt(x*x + r*r), M, z,
170-
omega_b, omega_m),
171-
-dist * R_del, dist * R_del,
172-
epsrel=epsrel)[0] / (1 + z)
173-
174-
175239
def projected_y_BBPS(r, M, z, omega_b, omega_m,
176-
Xh=0.76, dist=8, epsrel=1e-3):
240+
Xh=0.76, epsrel=1e-3):
177241
'''
178242
Projected Compton-y parameter along the line of sight, at a perpendicular
179243
distance `r` from a cluster of mass `M` at redshift `z`. All arguments have
180244
the same meaning as `projected_P_BBPS`.
181245
182246
Args:
183-
r (float): Radius from the cluster center, in Mpc.
247+
r (float or array): Radius from the cluster center, in Mpc.
184248
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
185249
z (float): Cluster redshift.
186250
omega_b (float): Baryon fraction.
187251
omega_m (float): Matter fraction.
188252
Xh (float): Primordial hydrogen mass fraction.
189253
190254
Returns:
191-
float: Compton y parameter. Unitless.
255+
float or array: Compton y parameter. Units are :math:`h^{8/3}`, so \
256+
multiply by :math:`h^{8/3}` to obtain the true value.
192257
'''
193258
# The constant is \sigma_T / (m_e * c^2), i.e. the Thompson cross-section
194259
# divided by the mass-energy of the electron, in units of s^2 Msun^{-1}.
@@ -198,11 +263,11 @@ def projected_y_BBPS(r, M, z, omega_b, omega_m,
198263
# equation to do so, see BBPS2 p. 3.
199264
ch = (2 * Xh + 2) / (5 * Xh + 3)
200265
return ch * cy * projected_P_BBPS(r, M, z, omega_b, omega_m,
201-
dist=dist, epsrel=epsrel)
266+
epsrel=epsrel)
202267

203268

204269
def Cxl(M, z, omega_b, omega_m, da, ell,
205-
dist=8, epsrel=1e-3):
270+
epsrel=1e-3):
206271
'''
207272
The Fourier-transform of a cluster's Compton y parameter on the sky. Assumes
208273
the flat-sky approximation.
@@ -221,22 +286,50 @@ def Cxl(M, z, omega_b, omega_m, da, ell,
221286
'''
222287
return quad(lambda theta: 2 * np.pi * theta * spec.j0(ell * theta)
223288
* projected_y_BBPS(theta * da, M, z, omega_b, omega_m,
224-
dist=dist, epsrel=epsrel),
289+
epsrel=epsrel),
225290
0, 2 * np.pi,
226291
epsrel=epsrel)[0]
227292

228293

229294
def smoothed_xi(theta, M, z, omega_b, omega_m, da,
230-
dist=8, epsrel=1e-3, maxl=10000):
295+
epsrel=1e-3, maxl=10000):
231296
# Convert from arcmin to radians
232297
theta = theta * 60 * np.pi / 180
233298
return quad(lambda ell: 1 / (2 * np.pi) * ell * spec.j0(ell * theta)
234299
* Cxl(M, z, omega_b, omega_m, da, ell,
235-
dist=dist, epsrel=epsrel),
300+
epsrel=epsrel),
236301
0, maxl,
237302
epsrel=epsrel)[0]
238303

239304

305+
##################################################
306+
# The following functions are for testing only!! #
307+
##################################################
308+
309+
def py_projected_P_BBPS(r, M, z, omega_b, omega_m,
310+
dist=8, epsrel=1e-3):
311+
'''
312+
Computes the projected line-of-sight density of a cluster at a radius r
313+
from the cluster center.
314+
315+
Args:
316+
r (float): Radius from the cluster center, in Mpc.
317+
M (float): Cluster :math:`M_{\\Delta}`, in Msun.
318+
z (float): Cluster redshift.
319+
omega_b (float): Baryon fraction.
320+
omega_m (float): Matter fraction.
321+
322+
Returns:
323+
float: Integrated line-of-sight pressure at distance `r` from the \
324+
cluster, in units of Msun s^{-2}.
325+
'''
326+
R_del = R_delta(M, z, omega_m)
327+
return quad(lambda x: P_BBPS(np.sqrt(x*x + r*r), M, z,
328+
omega_b, omega_m),
329+
-dist * R_del, dist * R_del,
330+
epsrel=epsrel)[0] / (1 + z)
331+
332+
240333
def projected_P_BBPS_real(r, M, z, omega_b, omega_m, chis, zs,
241334
dist=8, epsrel=1e-3):
242335
'''

include/C_pressure_profile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void P_BBPS(double *P_out, const double *r, unsigned Nr, double M_delta, double z, double omega_b, double omega_m, double P_0, double x_c, double beta, double alpha, double gamma, double delta);
2+
int projected_P_BBPS(double *P_out, double *P_err_out, const double *r, unsigned Nr, double M_delta, double z, double omega_b, double omega_m, double P_0, double x_c, double beta, double alpha, double gamma, double delta, unsigned limit, double epsabs, double epsrel);

0 commit comments

Comments
 (0)