Skip to content

Commit 2d84a6b

Browse files
committed
Document source grid functions
1 parent 86cd36c commit 2d84a6b

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

docs/src/observables.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,36 @@ rs = sound_horizon(sol)
157157
plot(τs, rs; xlabel = "τ / H₀⁻¹", ylabel = "rₛ / (c/H₀)")
158158
```
159159

160+
## Source functions
161+
162+
```@docs
163+
source_grid
164+
source_grid_adaptive
165+
```
166+
167+
```@example
168+
using SymBoltz, Plots, DataInterpolations
169+
M = ΛCDM(h = nothing, ν = nothing)
170+
pars = parameters_Planck18(M)
171+
prob = CosmologyProblem(M, pars)
172+
sol = solve(prob)
173+
174+
τs = sol[M.τ] # conformal times in background solution
175+
ks = [1.0, 2000.0] # initial coarse grid
176+
ks, Ss = source_grid_adaptive(prob, [M.ST0], τs, ks; atol = 5.0)
177+
iτ = argmax(sol[M.b.v]) # index of decoupling time
178+
iτs = iτ-75:iτ+75 # indices around decoupling
179+
p1 = surface(ks, τs[iτs], Ss[1, iτs, :]; camera = (45, 25), xlabel = "k", ylabel = "τ", zlabel = "S", colorbar = false)
180+
181+
lgas = -6.0:0.2:0.0
182+
τs = LinearInterpolation(sol[M.τ], sol[log10(M.g.a)])(lgas) # τ at given lg(a)
183+
ks = 5.0:5.0:100.0
184+
Ss = source_grid(prob, [M.g.Ψ], τs, ks)
185+
p2 = wireframe(ks, lgas, Ss[1, :, :]; camera = (75, 20), xlabel = "k", ylabel = "lg(a)", zlabel = "Φ")
186+
187+
plot(p1, p2)
188+
```
189+
160190
## Line-of-sight integration
161191

162192
```@docs

src/observables/fourier.jl

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ end
152152
# TODO: take in ks and N_skip_interp = 1, 2, 3, ... for more efficient? same for τ?
153153
# TODO: source_grid(prob::CosmologyProblem seemed type-stable?
154154
# TODO: return getter for (ks, τs)
155+
"""
156+
source_grid(sol::CosmologySolution, Ss::AbstractVector, τs)
157+
158+
Evaluate and return source functions ``S(τ,k)`` from the solution `sol`.
159+
The source functions are given by symbolic expressions `Ss`, and evaluated on a grid with conformal times `τs` and the same wavenumbers as `sol` is solved for.
160+
"""
155161
function source_grid(sol::CosmologySolution, Ss::AbstractVector, τs)
156162
# Evaluate integrated perturbations on coarse grid
157163
ks = sol.ks
@@ -165,6 +171,13 @@ function source_grid(sol::CosmologySolution, Ss::AbstractVector, τs)
165171
return Ss
166172
end
167173

174+
"""
175+
source_grid(Ss_coarse::AbstractArray, ks_coarse, ks_fine; ktransform = identity)
176+
177+
Interpolate values `Ss_coarse` of source functions ``S(τ,k)`` from a coarse wavenumber grid `ks_coarse` to a fine grid `ks_fine`.
178+
The interpolation is linear in `ktransform(k)` (e.g. `identity` for interpolation in ``k`` or `log` for interpolation in ``\\ln k``.
179+
Conformal times are unchanged.
180+
"""
168181
function source_grid(Ss_coarse::AbstractArray, ks_coarse, ks_fine; ktransform = identity)
169182
size_coarse = size(Ss_coarse)
170183
size_fine = (size_coarse[1], size_coarse[2], length(ks_fine))
@@ -182,11 +195,17 @@ function source_grid(Ss_coarse::AbstractArray, ks_coarse, ks_fine; ktransform =
182195
return Ss_fine
183196
end
184197

185-
# TODO: take in kτ0s and xs
186-
function source_grid(prob::CosmologyProblem, S::AbstractArray, τs, ks; bgopts = (), ptopts = (), thread = true, verbose = false)
198+
"""
199+
source_grid(prob::CosmologyProblem, Ss::AbstractArray, τs, ks; bgopts = (), ptopts = (), thread = true, verbose = false)
200+
201+
Compute and evaluate source functions ``S(τ,k)`` with symbolic expressions `Ss` on a grid with conformal times `τs` and wavenumbers `ks` from the problem `prob`.
202+
203+
The options `bgopts` and `ptopts` are passed to the background and perturbation solves.
204+
"""
205+
function source_grid(prob::CosmologyProblem, Ss::AbstractArray, τs, ks; bgopts = (), ptopts = (), thread = true, verbose = false)
187206
bgsol = solvebg(prob.bg; bgopts..., verbose)
188-
getSs = map(s -> getsym(prob.pt, s), S)
189-
Ss = similar(bgsol, length(S), length(τs), length(ks))
207+
getSs = map(S -> getsym(prob.pt, S), Ss)
208+
Ss = similar(bgsol, length(Ss), length(τs), length(ks))
190209
minimum(τs) bgsol.t[begin] && maximum(τs) bgsol.t[end] || error("input τs and computed background solution have different timespans")
191210
function output_func(sol, ik)
192211
for iS in eachindex(getSs)
@@ -198,6 +217,7 @@ function source_grid(prob::CosmologyProblem, S::AbstractArray, τs, ks; bgopts =
198217
return Ss
199218
end
200219

220+
# Called internally by source_grid_adaptive
201221
function source_grid_refine(i1, i2, ks, Ss, Sk, savelock; verbose = false, kwargs...)
202222
k1 = ks[i1]
203223
k2 = ks[i2]
@@ -229,6 +249,17 @@ end
229249

230250
# TODO: Hermite interpolation
231251
# TODO: create SourceFunction type that does k and τ interpolation?
252+
"""
253+
source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), kwargs...)
254+
255+
Adaptively compute and evaluate source functions ``S(τ,k)`` with symbolic expressions `Ss` on a grid with fixed conformal times `τs`, but adaptively refined grid of wavenumbers from the problem `prob`.
256+
The source functions are first evaluated on the (coarse) initial grid `ks`.
257+
Each subinterval ``(k₁, k₂)`` of `ks` is then adaptively refined until the linear interpolation ``Sᵢ = (S(k₁)+S(k₂))/2`` to the midpoint ``k=(k₁+k₂)/2`` approximates the actual value ``S(k)`` there within some tolerance.
258+
The comparison ``Sᵢ ≈ S`` is done with `isapprox(Sᵢ, S; kwargs...)`, where `S` and `Sᵢ` are vectors with the (conformal) timeseries of the source function for that wavenumber.
259+
It receives the keyword arguments `kwargs` passed to this function, so `atol`, `rtol` and/or `norm` can be specified to tune the tolerance.
260+
261+
The options `bgopts` and `ptopts` are passed to the background and perturbation solves.
262+
"""
232263
function source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), kwargs...)
233264
bgsol = solvebg(prob.bg; bgopts...)
234265

0 commit comments

Comments
 (0)