Skip to content

Commit acaeed6

Browse files
committed
Improve error handling for the low-k approximation
1 parent c32ca1b commit acaeed6

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

pttools/ssmtools/low_k/join.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import logging
2+
13
import numpy as np
24
from scipy.special import erf, erfc
35

46
from pttools.ssmtools.low_k import integration, intersection
57

8+
logger = logging.getLogger(__name__)
9+
610

711
def pow_gw_junction(
812
z: np.ndarray[tuple[int], np.float64],
@@ -12,7 +16,8 @@ def pow_gw_junction(
1216
cs: float,
1317
nu: float,
1418
tau_star: float,
15-
tau_end: float):
19+
tau_end: float,
20+
HLf: float):
1621
r"""
1722
Create the junction of the gravitational wave power spectrum between different regimes
1823
starting from the profiles in each regime.
@@ -22,19 +27,30 @@ def pow_gw_junction(
2227
:param Pgw_int: array of gravitational wave power spectrum values in the intermediate-frequency regime
2328
:param Pgw_high: array of gravitational wave power spectrum values in the high-frequency regime
2429
:param cs: sound speed, $0 < c_s < \frac{1}{\sqrt{3}}$
30+
:param nu: $\nu_\text{gdh2024}$
2531
:param tau_star: $\tau_* = \frac{\eta_*}{L_f}$
2632
:param tau_end: $\tau_{end} = \frac{\eta_{end}}{L_f}$
33+
:param HLf: $H L_f = r_*$
2734
:return: gravitational wave power spectrum values at the given momentum
2835
"""
29-
# z_star = 4*cs*np.pi * (1+nu) / HLf
36+
# if not (z.size and Pgw_low.size and Pgw_int.size and Pgw_high.size):
37+
# raise ValueError(
38+
# "Input arrays must not be empty. Got sizes: "
39+
# f"z={z.size}, Pgw_low={Pgw_low.size}, Pgw_int={Pgw_int.size}, Pgw_high={Pgw_high.size}"
40+
# )
41+
3042
difference = Pgw_high - Pgw_int
3143
index = np.where(difference > 0)[0]
32-
z_star = z[index[0]] # if len(index) > 0 else z_star
44+
if index.size:
45+
z_star = z[index[0]]
46+
else:
47+
z_star = 4*cs*np.pi * (1+nu) / HLf
48+
logger.warning("Using fallback z_star=%s for low-k junction.", z_star)
3349
z_cross = intersection.cross_z_junction(cs=cs, nu=nu, tau_star=tau_star, tau_end=tau_end)
3450

3551
term_low = 0.5 * erfc(2 * np.pi * tau_star * (z - z_cross)) * Pgw_low
36-
term_int = 0.5 * (1 + erf(2 * np.pi * tau_star * (z - z_cross))) * Pgw_int * 0.5 * erfc(
37-
2 * np.pi * tau_star * (z - z_star))
52+
term_int = 0.5 * (1 + erf(2 * np.pi * tau_star * (z - z_cross))) * Pgw_int * \
53+
0.5 * erfc(2 * np.pi * tau_star * (z - z_star))
3854
term_high = 0.5 * (1 + erf(2 * np.pi * tau_star * (z - z_star))) * Pgw_high
3955

4056
return term_low + term_int + term_high

pttools/ssmtools/spectrum.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ def __init__(
6767
self.label_latex = self.bubble.label_latex[:-1] + label_suffix_latex if label_latex is None else label_latex
6868
self.label_unicode = self.bubble.label_unicode + label_suffix_unicode if label_unicode is None else label_unicode
6969

70-
if self.r_star <= 0:
71-
raise ValueError("r_star must be positive. Got r_star={r_star}.")
72-
if self.r_star >= 1:
73-
# Todo: Find a better reference for this.
74-
logger.warning(
75-
"r_star < 1 is required for the phase transition to complete. "
76-
"Got r_star=%s. See Hindmarsh & Hijazi 2019, p. 6.",
77-
self.r_star
78-
)
70+
if not (self.r_star is None or np.isnan(r_star)):
71+
if self.r_star <= 0:
72+
raise ValueError("r_star must be positive. Got r_star={r_star}.")
73+
if self.r_star >= 1:
74+
# Todo: Find a better reference for this.
75+
logger.warning(
76+
"r_star < 1 is required for the phase transition to complete. "
77+
"Got r_star=%s. See Hindmarsh & Hijazi 2019, p. 6.",
78+
self.r_star
79+
)
7980

8081
# Values generated by compute()
8182
# $|A(z)|^2$
@@ -92,6 +93,8 @@ def __init__(
9293
self.pow_v: tp.Optional[np.ndarray] = None
9394
#: $\mathcal{P}_{\text{gw}}(k)$
9495
self.pow_gw: tp.Optional[np.ndarray] = None
96+
#: $\mathcal{P}_{\text{gw}}(k)$, expanded to low frequencies
97+
self.pow_gw_expanded: tp.Optional[np.ndarray] = None
9598
#: $\mathcal{P}_{\text{gw}}(k)$ for intermediate frequencies
9699
self.pow_gw_int: tp.Optional[np.ndarray] = None
97100
#: $\mathcal{P}_{\text{gw}}(k)$ for low frequencies
@@ -163,7 +166,8 @@ def compute(self):
163166
Pgw_low=self.pow_gw_low * factor,
164167
Pgw_int=self.pow_gw_int * factor,
165168
Pgw_high=self.pow_gw_ssm * factor,
166-
cs=self.cs, nu=self.bubble.nu_gdh2024, tau_star=tau_star, tau_end=tau_end
169+
cs=self.cs, nu=self.bubble.nu_gdh2024, tau_star=tau_star, tau_end=tau_end,
170+
HLf=self.r_star
167171
) / factor
168172
if self.low_k:
169173
self.pow_gw = self.pow_gw_expanded

0 commit comments

Comments
 (0)