Skip to content

Commit d05e21e

Browse files
authored
Merge pull request #44 from hersle/minor_changes
Minor performance improvement and consistency improvements
2 parents 402f337 + 30f4b3f commit d05e21e

File tree

12 files changed

+64
-47
lines changed

12 files changed

+64
-47
lines changed

Project.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1919
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
2020
NumericalIntegration = "e7bfaba1-d571-5449-8927-abc22e82249b"
2121
OhMyThreads = "67456a42-1dca-4109-a031-0a68de7e3ad5"
22-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
22+
OrdinaryDiffEqNonlinearSolve = "127b3ac7-2247-4354-8eb6-78cf4e7c58e8"
23+
OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce"
24+
OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf"
25+
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
2326
PeriodicTable = "7b2266bf-644c-5ea3-82d8-af4bbd25a884"
2427
PhysicalConstants = "5ad8b20f-a522-5ce9-bfc9-ddf1d5bda6ab"
2528
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
@@ -53,7 +56,10 @@ ModelingToolkit = "~10.25.0"
5356
NonlinearSolve = "4.3"
5457
NumericalIntegration = "0.3.4"
5558
OhMyThreads = "0.8.0"
56-
OrdinaryDiffEq = "6.90.1"
59+
OrdinaryDiffEqNonlinearSolve = "1.12.0"
60+
OrdinaryDiffEqRosenbrock = "1.14.0"
61+
OrdinaryDiffEqSDIRK = "1.6.0"
62+
OrdinaryDiffEqTsit5 = "1.5.0"
5763
PeriodicTable = "1.2.1"
5864
PhysicalConstants = "0.2.4"
5965
PreallocationTools = "0.4.25"

docs/Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ MKL = "33e6dc65-8f57-5167-99aa-e5a354878fb2"
1818
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1919
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
2020
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
21-
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
21+
OrdinaryDiffEqBDF = "6ad6398a-0878-4a85-9266-38940aa047c8"
22+
OrdinaryDiffEqRosenbrock = "43230ef6-c299-4910-a778-202eb28ce4ce"
23+
OrdinaryDiffEqSDIRK = "2d112036-d095-4a1e-ab9a-08536f3ecdbf"
24+
OrdinaryDiffEqTsit5 = "b1df2697-797e-41e3-8120-5422d3b24e4a"
2225
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
2326
PairPlots = "43a3c2be-4208-490b-832a-a21dcd55d7da"
2427
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

docs/src/benchmarks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
This page is a work in progress.
55

66
```@example bench
7-
using MKL, SymBoltz, OrdinaryDiffEq, BenchmarkTools, Plots, BenchmarkPlots, StatsPlots, InteractiveUtils
7+
using MKL, SymBoltz, OrdinaryDiffEqRosenbrock, OrdinaryDiffEqSDIRK, OrdinaryDiffEqBDF, BenchmarkTools, Plots, BenchmarkPlots, StatsPlots, InteractiveUtils
88
M = SymBoltz.ΛCDM(ν = nothing, K = nothing, h = nothing)
99
pars = SymBoltz.parameters_Planck18(M)
1010
prob = CosmologyProblem(M, pars)

docs/src/parameter_fitting.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fig
5252

5353
To predict [luminosity distances](@ref "Luminosity distance") theoretically, we solve the w0waCDM model:
5454
```@example fit
55-
using SymBoltz
55+
using SymBoltz, OrdinaryDiffEqTsit5
5656
g = SymBoltz.metric()
5757
K = SymBoltz.curvature(g)
5858
X = SymBoltz.w0wa(g; analytical = true)
@@ -68,7 +68,7 @@ dL = SymBoltz.distance_luminosity_function(M, pars_fixed, pars_varying, data.zcm
6868
6969
# Show example predictions
7070
Mb = -19.3 # absolute supernova brightness (constant since SN-Ia are standard candles)
71-
bgopts = (alg = SymBoltz.Tsit5(), reltol = 1e-5, maxiters = 1e3)
71+
bgopts = (alg = Tsit5(), reltol = 1e-5, maxiters = 1e3)
7272
p0 = [9.3e-5, 0.3, 0.0, 0.7, -1.0, 0.0] # fiducial parameters
7373
μs = μ(p0)
7474
mbs = μs .+ Mb
@@ -92,12 +92,12 @@ using Turing
9292
wa ~ Uniform(-1.0, +1.0)
9393
9494
p = [Ωr0, Ωm0, Ωk0, h, w0, wa]
95-
μs_pred = μ_pred(p)
96-
if isempty(μs_pred)
95+
mbs_pred = μ_pred(p)
96+
if isempty(mbs_pred)
9797
Turing.@addlogprob! -Inf
9898
return nothing
9999
end
100-
mbs_pred = μs_pred .+ Mb
100+
mbs_pred .+= Mb
101101
return mbs ~ MvNormal(mbs_pred, C) # read "measurements sampled from multivariate normal with predictions and covariance matrix"
102102
103103
# equivalently:
@@ -238,7 +238,7 @@ pp_fc
238238

239239
```@setup fit
240240
#=
241-
using SymBoltz, OrdinaryDiffEq, Turing
241+
using SymBoltz, OrdinaryDiffEqTsit5, Turing
242242
243243
function dL_fast(z, Ωm0, Ωk0, h; Ωr0 = 9.3e-5, aini = 1e-8, reltol = 1e-8, alg = Tsit5(), maxiters = 1e3)
244244
ΩΛ0 = 1 - Ωr0 - Ωm0 - Ωk0

notebooks/paper/paper.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,12 @@
903903
" w0 ~ Uniform(-2.0, 0.0)\n",
904904
"\n",
905905
" p = [Ωm0, h, w0]\n",
906-
" μs_pred = μ_pred(p)\n",
907-
" if isempty(μs_pred)\n",
906+
" mbs_pred = μ_pred(p)\n",
907+
" if isempty(mbs_pred)\n",
908908
" Turing.@addlogprob! -Inf\n",
909909
" return nothing\n",
910910
" end\n",
911-
" mbs_pred = μs_pred .+ Mb\n",
911+
" mbs_pred .+= Mb\n",
912912
" return mbs ~ MvNormal(mbs_pred, C) # read \"measurements sampled from multivariate normal with predictions and covariance matrix\"\n",
913913
"\n",
914914
" # equivalently:\n",

src/SymBoltz.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module SymBoltz
33
using Reexport
44
import ModelingToolkit: parameters, unknowns # explicitly imported because they are extended
55
@reexport using ModelingToolkit # re-export commonly used ModelingToolkit functions
6-
using OrdinaryDiffEq
6+
using OrdinaryDiffEqRosenbrock, OrdinaryDiffEqSDIRK, OrdinaryDiffEqTsit5
7+
using OrdinaryDiffEqNonlinearSolve: NLNewton
78
using NonlinearSolve
89
using OhMyThreads
910
using Base.Threads

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/distances.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ function distance_luminosity(sol::CosmologySolution, ivs = sol.bg.t, τ0 = sol[s
1515
r = sinc.((-Ωk0+0im)*χ/π) .* χ |> real # Julia's sinc(x) = sin(π*x) / (π*x)
1616
H0 = H100 * sol[M.g.h]
1717
a = sol(M.g.a, ivs)
18-
return @. r / a * SymBoltz.c / H0 # to meters
18+
return @. r / a * c / H0 # to meters
1919
end
2020

2121
# TODO: test @inferred
22-
function distance_luminosity_function(M::System, pars_fixed, pars_varying, zs; bgopts = (alg = SymBoltz.Tsit5(), reltol = 1e-5, maxiters = 1e3))
22+
function distance_luminosity_function(M::System, pars_fixed, pars_varying, zs; bgopts = (alg = Tsit5(), reltol = 1e-5, maxiters = 1e3))
2323
isequal(ModelingToolkit.get_iv(M), M.g.a) || error("Independent variable must be $(M.g.a)")
2424

2525
pars = merge(pars_fixed, Dict(pars_varying .=> NaN))
2626
as = @. 1 / (zs + 1)
2727
prob = CosmologyProblem(M, pars; pt = false, ivspan = (minimum(as), 1.0))
28-
probgen = SymBoltz.parameter_updater(prob, pars_varying; build_initializeprob = Val{false})
28+
probgen = parameter_updater(prob, pars_varying; build_initializeprob = Val{false})
2929

3030
geta = getsym(prob, M.g.a)
3131
getτ = getsym(prob, M.τ)
@@ -42,8 +42,8 @@ function distance_luminosity_function(M::System, pars_fixed, pars_varying, zs; b
4242
τ0 = τ[end] # time today
4343
χ = τ0 .- τ
4444
r = @. real(sinc((-Ωk0+0im)*χ/π) * χ) # Julia's sinc(x) = sin(π*x) / (π*x)
45-
H0 = SymBoltz.H100 * h
46-
return @. r / a * SymBoltz.c / H0 # luminosity distance in meters
45+
H0 = H100 * h
46+
return @. r / a * c / H0 # luminosity distance in meters
4747
end
4848
end
4949

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
"""

0 commit comments

Comments
 (0)