Skip to content

Commit 39801a2

Browse files
committed
Add sort option to let adaptive source grid function return results in insertion order
1 parent 859711f commit 39801a2

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/observables/fourier.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,20 @@ end
250250
# TODO: Hermite interpolation
251251
# TODO: create SourceFunction type that does k and τ interpolation?
252252
"""
253-
source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), kwargs...)
253+
source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), sort = true, kwargs...)
254254
255255
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`.
256256
The source functions are first evaluated on the (coarse) initial grid `ks`.
257257
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.
258258
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.
259259
It receives the keyword arguments `kwargs` passed to this function, so `atol`, `rtol` and/or `norm` can be specified to tune the tolerance.
260260
261+
Returns the refined wavenumbers sorted in ascending order and a grid with the corresponding source function values.
262+
If not `sort`, the wavenumbers and source function values are instead left in the order in which they were inserted in the refinement process.
263+
261264
The options `bgopts` and `ptopts` are passed to the background and perturbation solves.
262265
"""
263-
function source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), kwargs...)
266+
function source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, ks; bgopts = (), ptopts = (), sort = true, kwargs...)
264267
bgsol = solvebg(prob.bg; bgopts...)
265268
ptprob0, ptprobgen = setuppt(prob.pt, bgsol, prob.bgspline)
266269

@@ -287,9 +290,13 @@ function source_grid_adaptive(prob::CosmologyProblem, Ss::AbstractVector, τs, k
287290
end
288291

289292
# sort according to k
290-
is = sortperm(ks)
291-
ks = ks[is]
292-
Ss = Ss[:, :, is]
293+
if sort
294+
is = sortperm(ks)
295+
ks = ks[is]
296+
Ss = Ss[:, :, is]
297+
else
298+
Ss = Ss[:, :, 1:length(ks)]
299+
end
293300

294301
return ks, Ss
295302
end

0 commit comments

Comments
 (0)