Skip to content

Commit

Permalink
Use preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Nov 3, 2023
1 parent 33f9763 commit 347f22a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 65 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Precompilation can be controlled via `Preferences.jl`
- `PrecompileMIRK` -- Precompile the MIRK2 - MIRK6 algorithms (default: `true`).
- `PrecompileShooting` -- Precompile the single shooting algorithms (default: `true`). This is triggered when `OrdinaryDiffEq` is loaded.
- `PrecompileMultipleShooting` -- Precompile the multiple shooting algorithms (default: `true`). This is triggered when `OrdinaryDiffEq` is loaded.
- `PrecompileMIRKNLLS` -- Precompile the MIRK2 - MIRK6 algorithms for under-determined and over-determined BVPs (default: `true` on Julia Version 1.10 and above).
- `PrecompileShootingNLLS` -- Precompile the single shooting algorithms for under-determined and over-determined BVPs (default: `true` on Julia Version 1.10 and above). This is triggered when `OrdinaryDiffEq` is loaded.
- `PrecompileMultipleShootingNLLS` -- Precompile the multiple shooting algorithms for under-determined and over-determined BVPs (default: `true` on Julia Version 1.10 and above). This is triggered when `OrdinaryDiffEq` is loaded.

To set these preferences before loading the package, do the following (replacing `PrecompileShooting` with the preference you want to set, or pass in multiple pairs to set them together):

Expand Down
129 changes: 69 additions & 60 deletions ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,69 +64,78 @@ end
end
end

if VERSION v"1.10-"
function f1_nlls!(du, u, p, t)
du[1] = u[2]
du[2] = -u[1]
end
function f1_nlls!(du, u, p, t)
du[1] = u[2]
du[2] = -u[1]
end

f1_nlls(u, p, t) = [u[2], -u[1]]
f1_nlls(u, p, t) = [u[2], -u[1]]

function bc1_nlls!(resid, sol, p, t)
solₜ₁ = sol(0.0)
solₜ₂ = sol(100.0)
resid[1] = solₜ₁[1]
resid[2] = solₜ₂[1] - 1
resid[3] = solₜ₂[2] + 1.729109
return nothing
end
bc1_nlls(sol, p, t) = [sol(0.0)[1], sol(100.0)[1] - 1, sol(100.0)[2] + 1.729109]

bc1_nlls_a!(resid, ua, p) = (resid[1] = ua[1])
bc1_nlls_b!(resid, ub, p) = (resid[1] = ub[1] - 1; resid[2] = ub[2] + 1.729109)

bc1_nlls_a(ua, p) = [ua[1]]
bc1_nlls_b(ub, p) = [ub[1] - 1, ub[2] + 1.729109]

tspan = (0.0, 100.0)
u0 = [0.0, 1.0]
bcresid_prototype1 = Array{Float64}(undef, 3)
bcresid_prototype2 = (Array{Float64}(undef, 1), Array{Float64}(undef, 2))

probs = [
BVProblem(BVPFunction(f1_nlls!, bc1_nlls!; bcresid_prototype = bcresid_prototype1),
u0, tspan),
BVProblem(BVPFunction(f1_nlls, bc1_nlls; bcresid_prototype = bcresid_prototype1),
u0, tspan),
TwoPointBVProblem(f1_nlls!, (bc1_nlls_a!, bc1_nlls_b!), u0, tspan;
bcresid_prototype = bcresid_prototype2),
TwoPointBVProblem(f1_nlls, (bc1_nlls_a, bc1_nlls_b), u0, tspan;
bcresid_prototype = bcresid_prototype2),
]

algs = [
Shooting(Tsit5();
nlsolve = LevenbergMarquardt(; autodiff = AutoForwardDiff(chunksize = 2))),
Shooting(Tsit5();
nlsolve = GaussNewton(; autodiff = AutoForwardDiff(chunksize = 2))),
MultipleShooting(10,
Tsit5();
nlsolve = LevenbergMarquardt(; autodiff = AutoForwardDiff(chunksize = 2)),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparseForwardDiff(; chunksize = 2))),
MultipleShooting(10,
Tsit5();
nlsolve = GaussNewton(; autodiff = AutoForwardDiff(chunksize = 2)),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparseForwardDiff(; chunksize = 2))),
]
function bc1_nlls!(resid, sol, p, t)
solₜ₁ = sol(0.0)
solₜ₂ = sol(100.0)
resid[1] = solₜ₁[1]
resid[2] = solₜ₂[1] - 1
resid[3] = solₜ₂[2] + 1.729109
return nothing
end
bc1_nlls(sol, p, t) = [sol(0.0)[1], sol(100.0)[1] - 1, sol(100.0)[2] + 1.729109]

@compile_workload begin
for prob in probs, alg in algs
solve(prob, alg)
end
bc1_nlls_a!(resid, ua, p) = (resid[1] = ua[1])
bc1_nlls_b!(resid, ub, p) = (resid[1] = ub[1] - 1; resid[2] = ub[2] + 1.729109)

bc1_nlls_a(ua, p) = [ua[1]]
bc1_nlls_b(ub, p) = [ub[1] - 1, ub[2] + 1.729109]

tspan = (0.0, 100.0)
u0 = [0.0, 1.0]
bcresid_prototype1 = Array{Float64}(undef, 3)
bcresid_prototype2 = (Array{Float64}(undef, 1), Array{Float64}(undef, 2))

probs = [
BVProblem(BVPFunction(f1_nlls!, bc1_nlls!; bcresid_prototype = bcresid_prototype1),
u0, tspan),
BVProblem(BVPFunction(f1_nlls, bc1_nlls; bcresid_prototype = bcresid_prototype1),
u0, tspan),
TwoPointBVProblem(f1_nlls!, (bc1_nlls_a!, bc1_nlls_b!), u0, tspan;
bcresid_prototype = bcresid_prototype2),
TwoPointBVProblem(f1_nlls, (bc1_nlls_a, bc1_nlls_b), u0, tspan;
bcresid_prototype = bcresid_prototype2),
]

algs = []

if @load_preference("PrecompileShootingNLLS", VERSIONv"1.10-")
append!(algs,

Check warning on line 109 in ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl#L109

Added line #L109 was not covered by tests
[
Shooting(Tsit5();
nlsolve = LevenbergMarquardt(;
autodiff = AutoForwardDiff(chunksize = 2))),
Shooting(Tsit5();
nlsolve = GaussNewton(; autodiff = AutoForwardDiff(chunksize = 2))),
])
end

if @load_preference("PrecompileMultipleShootingNLLS", VERSIONv"1.10-")
append!(algs,

Check warning on line 120 in ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl#L120

Added line #L120 was not covered by tests
[
MultipleShooting(10, Tsit5();
nlsolve = LevenbergMarquardt(;
autodiff = AutoForwardDiff(chunksize = 2)),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparseForwardDiff(; chunksize = 2))),
MultipleShooting(10, Tsit5();
nlsolve = GaussNewton(; autodiff = AutoForwardDiff(chunksize = 2)),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparseForwardDiff(; chunksize = 2))),
])
end

@compile_workload begin
for prob in probs, alg in algs
solve(prob, alg)
end

Check warning on line 139 in ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/BoundaryValueDiffEqOrdinaryDiffEqExt.jl#L138-L139

Added lines #L138 - L139 were not covered by tests
end
end
Expand Down
19 changes: 14 additions & 5 deletions src/BoundaryValueDiffEq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,21 @@ end

nlsolvers = [LevenbergMarquardt(), GaussNewton()]

@compile_workload begin
for prob in probs, nlsolve in nlsolvers,
alg in (MIRK2(; jac_alg, nlsolve), MIRK3(; jac_alg, nlsolve),
MIRK4(; jac_alg, nlsolve), MIRK5(; jac_alg, nlsolve),
MIRK6(; jac_alg, nlsolve))
algs = []

if Preferences.@load_preference("PrecompileMIRKNLLS", VERSIONv"1.10-")
for nlsolve in nlsolvers
append!(algs,

Check warning on line 140 in src/BoundaryValueDiffEq.jl

View check run for this annotation

Codecov / codecov/patch

src/BoundaryValueDiffEq.jl#L139-L140

Added lines #L139 - L140 were not covered by tests
[
MIRK2(; jac_alg, nlsolve), MIRK3(; jac_alg, nlsolve),
MIRK4(; jac_alg, nlsolve), MIRK5(; jac_alg, nlsolve),
MIRK6(; jac_alg, nlsolve),
])
end

Check warning on line 146 in src/BoundaryValueDiffEq.jl

View check run for this annotation

Codecov / codecov/patch

src/BoundaryValueDiffEq.jl#L146

Added line #L146 was not covered by tests
end

@compile_workload begin
for prob in probs, alg in algs
solve(prob, alg; dt = 0.2)
end

Check warning on line 152 in src/BoundaryValueDiffEq.jl

View check run for this annotation

Codecov / codecov/patch

src/BoundaryValueDiffEq.jl#L151-L152

Added lines #L151 - L152 were not covered by tests
end
Expand Down

0 comments on commit 347f22a

Please sign in to comment.