Skip to content

Commit f09464f

Browse files
committed
Remove h from CosmologySolution (get it as any other parameter)
1 parent d027fae commit f09464f

File tree

5 files changed

+31
-25
lines changed

5 files changed

+31
-25
lines changed

src/constants.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ const EHet2p2s = EHet2p1s - EHet2s1s
4444

4545
δkron(i, j) = (i == j ? 1 : 0) # Kronecker delta
4646

47-
function k_dimensionless(k::Number, h)
48-
if unit(k) == NoUnits
49-
return k
50-
else
51-
H₀ = h * H100 # s⁻¹
52-
k0 = (H₀ / c) / u"m"
53-
return NoUnits(k / k0)
54-
end
55-
end
47+
k_dimensionless(k::Number, h) = k
48+
k_dimensionless(k::Quantity, h) = NoUnits(k / (h*H100 / c / u"m"))
49+
k_dimensionless(k::Number, bgsol::ODESolution) = k
50+
k_dimensionless(k::Quantity, bgsol::ODESolution) = k_dimensionless(k, getsym(bgsol, :h)(bgsol))

src/models/inflation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function harrison_zeldovich(g; kwargs...)
1111
P ~ 2*Num(π)^2 / k^3 * As * (k/kpivot)^(ns-1)
1212
]
1313
defaults = [
14-
kpivot => 0.05 / (H100*g.h/c) / Mpc # k = 0.05/Mpc ≠ 0.05/(Mpc/h)
14+
kpivot => 0.05 / Mpc / (H100/c) / g.h # k = 0.05/Mpc ≠ 0.05/(Mpc/h)
1515
ln_As1e10 => NaN # keep uninitialized if not needed
1616
ns => NaN
1717
]

src/observables/fourier.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ using MatterPower
33
using TwoFAST
44

55
"""
6-
spectrum_primordial(k, h, As, ns=1.0; kp = k_dimensionless(0.05 / u"Mpc", h))
6+
spectrum_primordial(k, h, As, ns=1.0; kp = 0.05/u"Mpc")
77
88
Compute the primordial power spectrum
99
```math
1010
P₀(k) = 2π² Aₛ (k/kₚ)^{nₛ-1} / k³
1111
```
1212
with spectral amplitude `As`, spectral index `ns` and pivot scale wavenumber `kp` at the wavenumber(s) `k`.
1313
"""
14-
function spectrum_primordial(k, h, As, ns=1.0; kp = k_dimensionless(0.05 / u"Mpc", h))
15-
P = @. 2*π^2 / k^3 * As
14+
function spectrum_primordial(k, h, As, ns=1.0; kp = 0.05/u"Mpc")
15+
P = 2*π^2 * As ./ k.^3
1616

17-
# compute k / kpivot with equal wavenumber units
17+
# ensure both k and kp to dimensionless wavenumbers k/(H₀/c) before taking ratio
1818
k = k_dimensionless.(k, h)
19-
@. P *= (k/kp)^(ns-1)
19+
kp = k_dimensionless(kp, h)
20+
P .*= (k./kp).^(ns-1)
2021

2122
return P
2223
end
@@ -121,7 +122,7 @@ function variance_matter(sol::CosmologySolution, R)
121122
P = spectrum_matter(sol, k)
122123
lgPspl = spline(log.(P), log.(k))
123124
Pf(k) = exp(lgPspl(log(k)))
124-
R = 1 / k_dimensionless(1 / R, sol[M.g.h]) # make dimensionless
125+
R = 1 / k_dimensionless(1 / R, sol.bg) # make dimensionless
125126
return MatterPower.sigma2(Pf, R)
126127
end
127128
"""

src/solve.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ struct CosmologyProblem{Tbg <: ODEProblem, Tpt <: Union{ODEProblem, Nothing}, Tb
3131
bgspline::Tbgspline
3232
end
3333

34-
struct CosmologySolution{Tbg <: ODESolution, Tpts <: Union{Nothing, EnsembleSolution}, Tks <: Union{Nothing, AbstractVector}, Th <: Number}
34+
struct CosmologySolution{Tbg <: ODESolution, Tpts <: Union{Nothing, EnsembleSolution}, Tks <: Union{Nothing, AbstractVector}}
3535
prob::CosmologyProblem # problem which is solved
3636
bg::Tbg # background solution
3737
ks::Tks # perturbation wavenumbers
3838
pts::Tpts # perturbation solutions
39-
h::Th # reduced Hubble parameter h = H/(100 km/s/Mpc)
4039
end
4140

4241
algname(alg) = string(nameof(typeof(alg)))
@@ -293,16 +292,15 @@ function solve(
293292
bgsol = solvebg(prob.bg; verbose, bgopts..., kwargs...)
294293
end
295294

296-
h = getsym(bgsol, :h)(bgsol) # TODO: remove symbolic getter
297295
if isnothing(ks) || isempty(ks)
298296
ks = nothing
299297
ptsol = nothing
300298
else
301-
ks = k_dimensionless.(ks, h)
299+
ks = k_dimensionless.(ks, Ref(bgsol))
302300
ptsol = solvept(prob.pt, bgsol, ks, prob.bgspline; thread, verbose, ptopts..., kwargs...)
303301
end
304302

305-
return CosmologySolution(prob, bgsol, ks, ptsol, h)
303+
return CosmologySolution(prob, bgsol, ks, ptsol)
306304
end
307305
function solve(prob::CosmologyProblem, k::Number; kwargs...)
308306
return solve(prob, [k]; kwargs...)
@@ -570,7 +568,7 @@ function getsym(provider::Union{CosmologyProblem, CosmologySolution}, p)
570568
end
571569

572570
function neighboring_modes_indices(sol::CosmologySolution, k)
573-
k = k_dimensionless.(k, sol.h)
571+
k = k_dimensionless.(k, Ref(sol.bg))
574572
if k == sol.ks[begin] # k == kmin
575573
i1 = i2 = 1
576574
elseif k == sol.ks[end] # k == kmax
@@ -588,7 +586,7 @@ function (sol::CosmologySolution)(out::AbstractArray, is::AbstractArray, ts::Abs
588586
if isnothing(sol.ks) || isempty(sol.ks)
589587
throw(error("No perturbations solved for. Pass ks to solve()."))
590588
end
591-
ks = k_dimensionless.(ks, sol.h)
589+
ks = k_dimensionless.(ks, Ref(sol.bg))
592590
kmin, kmax = extrema(sol.ks)
593591
minimum(ks) >= kmin || throw("Requested wavenumber k = $(minimum(ks)) is below the minimum solved wavenumber $kmin")
594592
maximum(ks) <= kmax || throw("Requested wavenumber k = $(maximum(ks)) is above the maximum solved wavenumber $kmax")

test/runtests.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,23 @@ end
199199
@test_throws "not an unknown" SymBoltz.mtkcompile_spline(M, [M.g.H])
200200
end
201201

202-
@testset "Primordial power spectrum pivot scale" begin
202+
@testset "Wavenumber units and primordial power spectrum pivot scale" begin
203203
h = pars[M.g.h]
204204
k = 0.05 / u"Mpc" # ≠ 0.05/(Mpc/h)
205205
sol = solve(prob, k)
206-
@test sol[M.I.kpivot] SymBoltz.k_dimensionless(k, h)
206+
@test sol[M.I.kpivot] SymBoltz.k_dimensionless(k, sol.bg)
207+
208+
ks = 1.0:100.0
209+
sol = solve(prob, ks)
210+
P1 = sol(M.I.P, sol.bg.t[begin], ks)
211+
P2 = spectrum_primordial(ks, sol)
212+
@test all(isapprox.(P1, P2))
213+
214+
ks = collect(1.0:100.0) / u"Mpc"
215+
sol = solve(prob, ks)
216+
P1 = sol(M.I.P, sol.bg.t[begin], ks)
217+
P2 = spectrum_primordial(SymBoltz.k_dimensionless.(ks, h), sol)
218+
@test all(isapprox.(P1, P2))
207219
end
208220

209221
@testset "Time and optical depth today" begin

0 commit comments

Comments
 (0)