Skip to content

Commit 9d915da

Browse files
Merge pull request #118 from TorkelE/better_nonlinear_solve_wps
Better nonlinear solve wps
2 parents d5b3b16 + fa06adf commit 9d915da

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

Project.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
1717
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1818

1919
[compat]
20+
BVProblemLibrary = "0.1"
21+
BoundaryValueDiffEq = "4, 5"
22+
DDEProblemLibrary = "0.1"
2023
DelayDiffEq = "5.20"
2124
DiffEqBase = "6.94.4"
2225
DiffEqNoiseProcess = "5.0"
2326
NLsolve = "4.2"
27+
NonlinearSolve = "1, 2"
28+
ODEProblemLibrary = "0.1"
29+
OrdinaryDiffEq = "6"
30+
ParameterizedFunctions = "5"
2431
RecipesBase = "0.7, 0.8, 1.0"
2532
RecursiveArrayTools = "2"
2633
RootedTrees = "1, 2"
34+
SDEProblemLibrary = "0.1"
2735
SciMLBase = "1.74, 2"
36+
StochasticDelayDiffEq = "1"
37+
StochasticDiffEq = "6"
2838
julia = "1.6"
2939

3040
[extras]

src/benchmark.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ end
372372

373373
# Work precision information for a nonlinear problem.
374374
function WorkPrecision(prob::NonlinearProblem, alg, abstols, reltols, dts = nothing; name = nothing, appxsol = nothing, error_estimate = :l2, numruns = 20, seconds = 2, kwargs...)
375-
isnothing(appxsol) && error("Must provide the real value as the \"appxsol\" kwarg.")
376-
377375
N = length(abstols)
378376
errors = Vector{Float64}(undef, N)
379377
times = Vector{Float64}(undef, N)
@@ -394,7 +392,11 @@ function WorkPrecision(prob::NonlinearProblem, alg, abstols, reltols, dts = noth
394392
sol = solve(_prob, alg; kwargs..., abstol = abstols[i], reltol = reltols[i])
395393

396394
if error_estimate == :l2
397-
errors[i] = sqrt(sum(abs2, sol .- appxsol))
395+
if isnothing(appxsol)
396+
errors[i] = sqrt(sum(abs2, sol.resid))
397+
else
398+
errors[i] = sqrt(sum(abs2, sol .- appxsol))
399+
end
398400
else
399401
error("Unsupported norm used: $(error_estimate).")
400402
end
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Fetch pakages.
2-
using NonlinearSolve, DiffEqDevTools, Plots
1+
# Fetch packages.
2+
using DiffEqDevTools, NonlinearSolve, Plots, Test
33

4-
# Prepares NonlinearProblem.
54
let
6-
f(u, p) = u .* u .- p
7-
u0 = [1.0, 1.0]
8-
p = 2.0
5+
# Prepares NonlinearProblem.
6+
f(u, p) = 3u .^3 .+ 2u .^2 .+ u + .- p
7+
u0 = [1.0, 6.0]
8+
p = [1.0, 3.0]
99
static_prob = NonlinearProblem(f, u0, p)
1010
real_sol = solve(static_prob, NewtonRaphson(), reltol = 1e-15, abstol = 1e-15)
1111

@@ -14,11 +14,17 @@ let
1414
reltols = 1.0 ./ 10.0 .^ (8:12)
1515
setups = [Dict(:alg=>NewtonRaphson())
1616
Dict(:alg=>TrustRegion())]
17-
solnames = ["NewtonRaphson";"TrustRegion"]
17+
solnames = ["NewtonRaphson"; "TrustRegion"]
1818

1919
# Makes WP-diagram
2020
wp = WorkPrecisionSet(static_prob, abstols, reltols, setups; names=solnames, numruns=100, appxsol=real_sol, error_estimate=:l2)
2121

2222
# Checks that all errors are small (they definitely should be).
23-
all(vcat(getfield.(wp.wps, :errors)...) .< 10e-9)
23+
@test all(vcat(getfield.(wp.wps, :errors)...) .< 10e-9)
24+
@test length(plot(wp).series_list) == 2
25+
26+
# Check without appxsol.
27+
wp = WorkPrecisionSet(static_prob, abstols, reltols, setups; names=solnames, numruns=100, error_estimate=:l2)
28+
@test all(vcat(getfield.(wp.wps, :errors)...) .< 10e-9)
29+
@test length(plot(wp).series_list) == 2
2430
end

0 commit comments

Comments
 (0)