Skip to content

Commit 8f01c90

Browse files
Merge pull request #114 from TorkelE/master
Work precision diagram for nonlinearproblem
2 parents 41adf1e + 995b768 commit 8f01c90

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ julia = "1.6"
3030
[extras]
3131
DelayDiffEq = "bcd4f6db-9728-5f36-b5f7-82caef46ccdb"
3232
DDEProblemLibrary = "f42792ee-6ffc-4e2a-ae83-8ee2f22de800"
33+
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
3334
ODEProblemLibrary = "fdc4e326-1af4-4b90-96e7-779fcce2daa5"
3435
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
3536
ParameterizedFunctions = "65888b18-ceab-5e60-b2b9-181511a3b968"
@@ -41,4 +42,4 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
4142
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4243

4344
[targets]
44-
test = ["DelayDiffEq", "DDEProblemLibrary", "ODEProblemLibrary", "SDEProblemLibrary", "OrdinaryDiffEq", "ParameterizedFunctions", "Random", "StochasticDiffEq", "StochasticDelayDiffEq", "Plots", "Test"]
45+
test = ["DelayDiffEq", "DDEProblemLibrary", "NonlinearSolve", "ODEProblemLibrary", "SDEProblemLibrary", "OrdinaryDiffEq", "ParameterizedFunctions", "Random", "StochasticDiffEq", "StochasticDelayDiffEq", "Plots", "Test"]

src/benchmark.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,56 @@ function WorkPrecision(prob, alg, abstols, reltols, dts = nothing;
273273
return WorkPrecision(prob, abstols, reltols, errors, times, name, N)
274274
end
275275

276+
# Work precision information for a nonlinear problem.
277+
function WorkPrecision(prob::NonlinearProblem, alg, abstols, reltols, dts = nothing; name = nothing, appxsol = nothing, error_estimate = :l2, numruns = 20, seconds = 2, kwargs...)
278+
isnothing(appxsol) && error("Must provide the real value as the \"appxsol\" kwarg.")
279+
280+
N = length(abstols)
281+
errors = Vector{Float64}(undef, N)
282+
times = Vector{Float64}(undef, N)
283+
if name === nothing
284+
name = "WP-Alg"
285+
end
286+
287+
if haskey(kwargs, :prob_choice)
288+
_prob = prob[kwargs[:prob_choice]]
289+
elseif prob isa AbstractArray
290+
_prob = prob[1]
291+
else
292+
_prob = prob
293+
end
294+
295+
let _prob = _prob
296+
for i in 1:N
297+
sol = solve(_prob, alg; kwargs..., abstol = abstols[i], reltol = reltols[i])
298+
299+
if error_estimate == :l2
300+
errors[i] = sqrt(sum(abs2, sol .- appxsol))
301+
else
302+
error("Unsupported norm used: $(error_estimate).")
303+
end
304+
305+
benchmark_f = let dts = dts, _prob = _prob, alg = alg, sol = sol,
306+
abstols = abstols, reltols = reltols, kwargs = kwargs
307+
308+
() -> @elapsed solve(_prob, alg;
309+
abstol = abstols[i],
310+
reltol = reltols[i],
311+
kwargs...)
312+
end
313+
benchmark_f() # pre-compile
314+
315+
b_t = benchmark_f()
316+
if b_t > seconds
317+
times[i] = b_t
318+
else
319+
times[i] = mapreduce(i -> benchmark_f(), min, 2:numruns; init = b_t)
320+
end
321+
end
322+
end
323+
return WorkPrecision(prob, abstols, reltols, errors, times, name, N)
324+
end
325+
276326
function WorkPrecisionSet(prob,
277327
abstols, reltols, setups;
278328
print_names = false, names = nothing, appxsol = nothing,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Fetch pakages.
2+
using NonlinearSolve, DiffEqDevTools, Plots
3+
4+
# Prepares NonlinearProblem.
5+
let
6+
f(u, p) = u .* u .- p
7+
u0 = [1.0, 1.0]
8+
p = 2.0
9+
static_prob = NonlinearProblem(f, u0, p)
10+
real_sol = solve(static_prob, NewtonRaphson(), reltol = 1e-15, abstol = 1e-15)
11+
12+
# Sets WP input.
13+
abstols = 1.0 ./ 10.0 .^ (8:12)
14+
reltols = 1.0 ./ 10.0 .^ (8:12)
15+
setups = [Dict(:alg=>NewtonRaphson())
16+
Dict(:alg=>TrustRegion())]
17+
solnames = ["NewtonRaphson";"TrustRegion"]
18+
19+
# Makes WP-diagram
20+
wp = WorkPrecisionSet(static_prob, abstols, reltols, setups; names=solnames, numruns=100, appxsol=real_sol, error_estimate=:l2)
21+
22+
# Checks that all errors are small (they definitely should be).
23+
all(vcat(getfield.(wp.wps, :errors)...) .< 10e-9)
24+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ using Test
99
@time @testset "Analyticless Stochastic WP" begin include("analyticless_stochastic_wp.jl") end
1010
@time @testset "Stability Region Tests" begin include("stability_region_test.jl") end
1111
@time @testset "Plot Recipes" begin include("plotrecipes_tests.jl") end
12+
@time @testset "Plot Recipes (Nonlinearsolve WP-diagrams)" begin include("nonlinearsolve_wpdiagram_tests.jl") end

0 commit comments

Comments
 (0)