Skip to content

Commit 54785e2

Browse files
committed
Use nonzero initial conformal time and radiation-dominated initial scale factor
Update cosmologies.jl
1 parent b8406d8 commit 54785e2

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed

docs/src/comparison.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,21 @@ nothing # hide
199199
```@example class
200200
a1 = (1 ./ (sol1["background"][:,"z"] .+ 1))
201201
a2 = sol2[M.g.a]
202-
χ1 = sol1["background"][:,"conf. time [Mpc]"][end].-sol1["background"][:,"conf. time [Mpc]"]
203-
χ2 = sol2[M.χ] / (h*SymBoltz.k0)
204-
plot_compare(a1, a2, χ1, χ2, "a", "χ"; tol = 2e-3)
202+
τ1 = sol1["background"][:,"conf. time [Mpc]"]
203+
τ2 = sol2[M.τ] / (h*SymBoltz.k0)
204+
plot_compare(a1, a2, τ1, τ2, "a", "χ"; tol = 2e-2)
205205
```
206206
### Hubble function
207207
```@example class
208208
E1 = sol1["background"][:,"H [1/Mpc]"]./sol1["background"][end,"H [1/Mpc]"]
209209
E2 = sol2[M.g.E]
210-
plot_compare(a1, a2, E1, E2, "a", "E"; lgx=true, lgy=true, tol = 2e8)
210+
plot_compare(a1, a2, E1, E2, "a", "E"; lgx=true, lgy=true, tol = 8e6)
211211
```
212212
### Energy densities
213213
```@example class
214214
ρ1 = map(s -> sol1["background"][:,"(.)rho_$s"], ["g", "ur", "cdm", "b", "fld", "ncdm[0]"])
215215
ρ2 = map(s -> sol2[s.ρ] * 8π/3*(h*SymBoltz.k0)^2, [M.γ, M.ν, M.c, M.b, M.X, M.h])
216-
plot_compare(a1, a2, ρ1, ρ2, "a", ["ργ", "ρb", "ρc", "ρX", "ρν", "ρh"]; lgx=true, lgy=true, tol = 2e15)
216+
plot_compare(a1, a2, ρ1, ρ2, "a", ["ργ", "ρb", "ρc", "ρX", "ρν", "ρh"]; lgx=true, lgy=true, tol = 6e12)
217217
```
218218
### Equations of state
219219
```@example class
@@ -227,13 +227,13 @@ plot_compare(a1, a2, [wh1, wX1], [wh2, wX2], "a", ["wh", "wX"]; lgx=true, tol =
227227
```@example class
228228
rs1 = sol1["background"][:,"comov.snd.hrz."]
229229
rs2 = sound_horizon(sol2) ./ (h*SymBoltz.k0)
230-
plot_compare(a1, a2, rs1, rs2, "a", "rₛ"; lgx = true, tol = 2e-2)
230+
plot_compare(a1, a2, rs1, rs2, "a", "rₛ"; lgx = true, tol = 1e-2)
231231
```
232232
### Luminosity distance
233233
```@example class
234234
dL1 = sol1["background"][:,"lum. dist."]
235235
dL2 = SymBoltz.distance_luminosity(sol2) / SymBoltz.Mpc
236-
plot_compare(a1, a2, dL1, dL2, "a", "dL"; lgx=true, lgy=true, tol = 1e6)
236+
plot_compare(a1, a2, dL1, dL2, "a", "dL"; lgx=true, lgy=true, tol = 3e5)
237237
```
238238

239239
## Thermodynamics
@@ -262,15 +262,15 @@ plot_compare(a1, a2, v1, v2, "a", "v"; lgx=true, lgy=false, tol = 2e-6)
262262
```@example class
263263
Xe1 = reverse(sol1["thermodynamics"][:,"x_e"])
264264
Xe2 = sol2[M.b.Xe]
265-
plot_compare(a1, a2, Xe1, Xe2, "a", "Xe"; lgx=true, lgy=false, tol = 5e-4)
265+
plot_compare(a1, a2, Xe1, Xe2, "a", "Xe"; lgx=true, lgy=false, tol = 4e-4)
266266
```
267267
### Baryon temperature
268268
```@example class
269269
Tb1 = reverse(sol1["thermodynamics"][:,"Tb [K]"])
270270
Tb2 = sol2[M.b.T]
271271
dTb1 = reverse(sol1["thermodynamics"][:,"dTb [K]"])
272272
dTb2 = sol2[M.b.DT] ./ -sol2[M.g.E] # convert my dT/dt̂ to CLASS' dT/dz = -1/H * dT/dt
273-
plot_compare(a1, a2, [Tb1, dTb1], [Tb2, dTb2], "a", ["Tb", "dTb"]; lgx=true, lgy=true, tol = 6e0)
273+
plot_compare(a1, a2, [Tb1, dTb1], [Tb2, dTb2], "a", ["Tb", "dTb"]; lgx=true, lgy=true, tol = 5e0)
274274
```
275275
### Baryon equation of state
276276
```@example class
@@ -294,7 +294,7 @@ a1 = sol1["perturbations_k0_s"][:,"a"]
294294
a2 = sol2[1, M.g.a]
295295
Φ1, Ψ1 = sol1["perturbations_k0_s"][:,"phi"], sol1["perturbations_k0_s"][:,"psi"]
296296
Φ2, Ψ2 = sol2[1, M.g.Φ], sol2[1, M.g.Ψ]
297-
plot_compare(a1, a2, [Φ1, Ψ1], [Φ2, Ψ2], "a", ["Ψ", "Φ"]; lgx=true, tol = 2e-4)
297+
plot_compare(a1, a2, [Φ1, Ψ1], [Φ2, Ψ2], "a", ["Ψ", "Φ"]; lgx=true, tol = 2e-3)
298298
```
299299
### Energy overdensities
300300
```@example class
@@ -306,25 +306,25 @@ plot_compare(a1, a2, δ1, δ2, "a", ["δb", "δc", "δγ", "δν", "δh"]; lgx=t
306306
```@example class
307307
θ1 = map(s -> sol1["perturbations_k0_s"][:,"theta_$s"], ["b", "cdm", "g", "ur", "ncdm[0]"])
308308
θ2 = map(s -> sol2[1, s.θ] * (h*SymBoltz.k0), [M.b, M.c, M.γ, M.ν, M.h])
309-
plot_compare(a1, a2, θ1, θ2, "a", ["θb", "θc", "θγ", "θν", "θh"]; lgx=true, lgy=true, tol = 2e-2)
309+
plot_compare(a1, a2, θ1, θ2, "a", ["θb", "θc", "θγ", "θν", "θh"]; lgx=true, lgy=true, tol = 3e-2)
310310
```
311311
### Dark energy overdensity
312312
```@example class
313313
δρX1 = sol1["perturbations_k0_s"][:,"delta_rho_fld"]
314314
δρX2 = sol2[1, M.X.δ*M.X.ρ] * 8π/3*(h*SymBoltz.k0)^2
315-
plot_compare(a1, a2, δρX1, δρX2, "a", "δρX"; lgx=true, lgy=true, tol = 2e-7)
315+
plot_compare(a1, a2, δρX1, δρX2, "a", "δρX"; lgx=true, lgy=true, tol = 4e-7)
316316
```
317317
### Dark energy momentum
318318
```@example class
319319
pX1 = sol1["perturbations_k0_s"][:,"rho_plus_p_theta_fld"]
320320
pX2 = sol2[1, (M.X.ρ+M.X.P)*M.X.θ * 8π/3*(h*SymBoltz.k0)^3]
321-
plot_compare(a1, a2, pX1, pX2, "a", "pX"; lgx=true, lgy=true, tol = 7e-8)
321+
plot_compare(a1, a2, pX1, pX2, "a", "pX"; lgx=true, lgy=true, tol = 3e-7)
322322
```
323323
### Shear stresses
324324
```@example class
325325
σ1 = [sol1["perturbations_k0_s"][:,"shear_g"], sol1["perturbations_k0_s"][:,"shear_ur"]]
326326
σ2 = [sol2[1, M.γ.σ], sol2[1, M.ν.F[2]/2]]
327-
plot_compare(a1, a2, σ1, σ2, "a", ["σγ", "σν"]; lgx=true, tol = 6e-4)
327+
plot_compare(a1, a2, σ1, σ2, "a", ["σγ", "σν"]; lgx=true, tol = 7e-4)
328328
```
329329
### Polarization
330330
```@example class
@@ -354,7 +354,7 @@ function P_symboltz(k, pars)
354354
end
355355
k, P1 = P_class(pars)
356356
P2 = P_symboltz(k, pars)
357-
plot_compare(k, k, P1, P2, "k/Mpc⁻¹", "P/Mpc³"; lgx = true, lgy = true, tol = 4e1)
357+
plot_compare(k, k, P1, P2, "k/Mpc⁻¹", "P/Mpc³"; lgx = true, lgy = true, tol = 5e1)
358358
```
359359
```@example class
360360
using ForwardDiff, FiniteDiff

docs/src/observables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ sol = solve(prob)
135135
zs = 0.0:1.0:10.0
136136
τs = SymBoltz.timeseries(sol, M.g.z, zs) # times at given redshifts
137137
dLs = SymBoltz.distance_luminosity(sol, τs) / SymBoltz.Gpc
138-
@assert dLs[begin] == 0.0 || zs[begin] != 0.0 # ensure bug does not reappear # hide
138+
@assert isapprox(dLs[begin], 0.0; atol = 1e-14) || zs[begin] != 0.0 # ensure bug does not reappear # hide
139139
plot(zs, dLs; marker=:dot, xlabel="z", ylabel="dL / Gpc", label=nothing)
140140
```
141141

src/models/cosmologies.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ function ΛCDM(;
5959
end
6060
defs = Dict(
6161
C => 1//2,
62-
τ0 => NaN
62+
τ0 => NaN,
63+
g.a => (sum(s.Ω₀ for s in [ν, h, γ] if have(s))) * τ # default initial scale factor
6364
)
6465
ics = [
6566
g.Ψ ~ 20C / (15 + 4fν) # Φ found from solving initialization system
@@ -152,7 +153,8 @@ function RMΛ(;
152153
]
153154
defs = Dict(
154155
g.Ψ => 20 // 15,
155-
τ0 => NaN
156+
τ0 => NaN,
157+
g.a => (r.Ω₀) * τ # default initial scale factor
156158
)
157159
append!(eqs, Ω₀_eqs(G, species)) # parameter equations
158160
connections = System(eqs, τ, vars, [pars; k]; defaults = defs, name)

src/models/metric.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ function metric(; name = :g, kwargs...)
2020
vars = a, ℰ, E, ℋ, H, Ψ, Φ, Ψ̇, Φ̇, z, ż = GlobalScope.(vars)
2121

2222
pars = h, = GlobalScope.(@parameters h [description = "Dimensionless Hubble parameter today (H₀/(100km/s/Mpc))"])
23-
defaults = Dict(
24-
a => 1e-8 # default initial scale factor # TODO: move elsewhere?
25-
)
2623
description = "Spacetime FLRW metric in Newtonian gauge"
2724
return System([
2825
z ~ 1/a - 1
@@ -31,5 +28,5 @@ function metric(; name = :g, kwargs...)
3128
E ~/ a # E = H/H₀
3229
~* H100 * h
3330
H ~ E * H100 * h
34-
], τ, vars, pars; defaults, name, description, kwargs...)
31+
], τ, vars, pars; name, description, kwargs...)
3532
end

src/observables/angular.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ function spectrum_cmb(modes::AbstractVector, prob::CosmologyProblem, ls::Abstrac
166166
τ0 = getsym(sol, prob.M.τ0)(sol)
167167
ks_coarse = kτ0s_coarse ./ τ0
168168
τs = sol.bg.t # by default, use background (thermodynamics) time points for line of sight integration
169+
τi = τs[begin]
169170
if Nlos != 0 # instead choose Nlos time points τ = τ(u) corresponding to uniformly spaced u
170171
τmin, τmax = extrema(τs)
171172
umin, umax = u(τmin), u(τmax)
172173
us = range(umin, umax, length = Nlos)
173174
τs = u⁻¹.(us)
174-
τs[begin] = 0.0
175+
τs[begin] = τi
175176
τs[end] = τ0
176177
end
177178

src/solve.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ end
104104
"""
105105
CosmologyProblem(
106106
M::System, pars::Dict, shoot_pars = Dict(), shoot_conditions = [];
107-
ivspan = (0.0, 100.0), bg = true, pt = true, spline = true, debug = false, fully_determined = true, jac = false, kwargs...
107+
ivspan = (1e-6, 100.0), bg = true, pt = true, spline = true, debug = false, fully_determined = true, jac = false, kwargs...
108108
)
109109
110110
Create a numerical cosmological problem from the model `M` with parameters `pars`.
@@ -116,7 +116,7 @@ If `spline` is a `Vector`, it rather decides which (unknown and observed) variab
116116
"""
117117
function CosmologyProblem(
118118
M::System, pars::Dict, shoot_pars = Dict(), shoot_conditions = [];
119-
ivspan = (0.0, 100.0), bg = true, pt = true, spline = true, debug = false, fully_determined = true, jac = false, kwargs... # TODO: restore jac = true, not working with massive neutrinos?
119+
ivspan = (1e-6, 100.0), bg = true, pt = true, spline = true, debug = false, fully_determined = true, jac = false, kwargs... # TODO: restore jac = true, not working with massive neutrinos?
120120
)
121121
pars = merge(pars, shoot_pars) # save full dictionary for constructor
122122
parsk = merge(pars, Dict(M.k => NaN)) # k is unused, but must be set

test/runtests.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,16 @@ end
141141
end
142142

143143
@testset "Initial conditions" begin
144+
τini = prob.bg.tspan[1]
144145
ks = [1e-1, 1e0] / u"Mpc"
145146
sol = solve(prob, ks)
146147

147148
# Check that Fₗ(0) ∝ kˡ
148-
Fls = sol([M.γ.F0; collect(M.γ.F)], 0.0, ks)
149+
Fls = sol([M.γ.F0; collect(M.γ.F)], τini, ks)
149150
@test all(Fls[:,1] ./ Fls[:,2] .≈ map(l -> (ks[1]/ks[2])^l, 0:size(Fls)[1]-1))
150151

151152
# Check initial ratio of metric potentials
152-
@test all(isapprox.(sol(M.g.Φ / M.g.Ψ, 0.0, ks), sol((1+2/5*M.fν), 0.0); atol = 1e-5))
153+
@test all(isapprox.(sol(M.g.Φ / M.g.Ψ, τini, ks), sol((1+2/5*M.fν), τini); atol = 1e-4))
153154
end
154155

155156
@testset "Automatic background/thermodynamics splining" begin
@@ -336,8 +337,9 @@ end
336337

337338
# custom output_func for e.g. source function
338339
getS = SymBoltz.getsym(prob.pt, M.ST0)
340+
τi = bgsol.t[begin]
339341
τ0 = bgsol.t[end]
340-
τs = range(0.0, τ0, length = 768)
342+
τs = range(τi, τ0, length = 768)
341343
ks = range(1.0, 1000.0, length = 1000)
342344
ptsol = solvept(prob.pt, bgsol, ks, prob.var2spl; saveat = τs, output_func = (ptsol, _) -> (getS(ptsol), false))
343345
Ss = stack(ptsol.u)
@@ -348,15 +350,16 @@ end
348350
# TODO: define closure based on this?
349351
@testset "Type stability" begin
350352
getτ0 = SymBoltz.getsym(prob.bg, M.τ0)
351-
sol = solve(prob; save_everystep = false, save_start = false, save_end = true)
353+
sol = solve(prob; save_everystep = false, save_start = true, save_end = true)
354+
τi = sol.bg.t[begin]
352355
τ0 = @inferred getτ0(sol.bg)
353-
@test sol.bg.t[begin] == sol.bg.t[end] == τ0
356+
@test sol.bg.t[end] == τ0
354357

355358
ls = 20:20:500
356359
kτ0s_coarse, kτ0s_fine = SymBoltz.cmb_kτ0s(ls[begin], ls[end])
357360
ks_coarse, ks_fine = kτ0s_coarse / τ0, kτ0s_fine / τ0
358361
ks_fine = clamp.(ks_fine, ks_coarse[begin], ks_coarse[end]) # numerics can change endpoint slightly
359-
τs = range(0.0, τ0, length = 768)
362+
τs = range(τi, τ0, length = 768)
360363
#sol = solve(prob, ks_coarse)
361364
#Ss = sol(ks_fine, τs, M.ST0)
362365
sol = solve(prob, ks_coarse; ptopts = (alg = SymBoltz.KenCarp4(), reltol = 1e-8, abstol = 1e-8, saveat = τs,))

0 commit comments

Comments
 (0)