Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/OrdinaryDiffEqBDF/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OrdinaryDiffEqBDF"
uuid = "6ad6398a-0878-4a85-9266-38940aa047c8"
authors = ["ParamThakkar123 <paramthakkar864@gmail.com>"]
version = "2.4.14"
version = "2.4.15"

[deps]
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"
Expand Down Expand Up @@ -78,7 +78,7 @@ ADTypes = "1.22.0"
RecursiveArrayTools = "4.2.0"
ODEProblemLibrary = "1"
OrdinaryDiffEqRosenbrock = "2"
OrdinaryDiffEqNonlinearSolve = "2"
OrdinaryDiffEqNonlinearSolve = "2.10"
DiffEqBase = "7"
Reexport = "1.2.2"
SafeTestsets = "0.1.0"
Expand Down
124 changes: 124 additions & 0 deletions lib/OrdinaryDiffEqBDF/test/dae_krylov_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using OrdinaryDiffEqBDF, LinearSolve, ADTypes, SciMLBase, Test
using LinearAlgebra: I, mul!, norm
using ADTypes: AutoForwardDiff, AutoFiniteDiff
using SciMLBase: DAEProblem, DAEFunction, successful_retcode
using SciMLOperators: update_coefficients!
using OrdinaryDiffEqDifferentiation: dae_jvp_cache, set_dae_jvp_cj!

# https://github.com/SciML/OrdinaryDiffEq.jl/issues/1673

function decay!(res, du, u, p, t)
@. res = du + u
return nothing
end

# Semi-explicit index-1 DAE: u[3] is algebraic, so ∂F/∂du is singular.
function semi_explicit!(res, du, u, p, t)
res[1] = du[1] + u[1] - u[2]^2
res[2] = du[2] + 2u[2] - u[3]
res[3] = u[3] - sin(u[1])
return nothing
end

@testset "DFBDF with a matrix-free linear solver on a DAEProblem" begin
prob = DAEProblem(decay!, -ones(4), ones(4), (0.0, 1.0))
sol = solve(prob, DFBDF(; linsolve = KrylovJL_GMRES()); abstol = 1.0e-8, reltol = 1.0e-8)
@test successful_retcode(sol)
@test sol.u[end] ≈ fill(exp(-1), 4) rtol = 1.0e-5
# Matrix-free: no Jacobian is ever formed.
@test sol.stats.njacs == 0

u0 = [1.0, 0.5, sin(1.0)]
du0 = [-1.0 + 0.25, -1.0 + sin(1.0), 0.0]
prob = DAEProblem(
semi_explicit!, du0, u0, (0.0, 2.0); differential_vars = [true, true, false]
)
opts = (; abstol = 1.0e-9, reltol = 1.0e-9)
reference = solve(prob, DFBDF(); opts...)
for autodiff in (AutoForwardDiff(), AutoFiniteDiff())
sol = solve(prob, DFBDF(; autodiff, linsolve = KrylovJL_GMRES()); opts...)
@test successful_retcode(sol)
@test sol.stats.njacs == 0
@test sol.u[end] ≈ reference.u[end] rtol = 1.0e-6
end

# User `precs` replace the residual weights; the Krylov stopping test is relative to
# the residual in whatever norm they induce, so it holds without the weights too.
linsolve = KrylovJL_GMRES(; precs = Returns((I, I)))
sol = solve(prob, DFBDF(; linsolve); opts...)
@test successful_retcode(sol)
@test sol.u[end] ≈ reference.u[end] rtol = 1.0e-6
end

# The Krylov stopping test must scale with the residual: a residual scaled by 1e-12 once
# satisfied a fixed `abstol` at `dz = 0` and returned `Success` with `u = u0`.
f!(r, d, u, p, t) = (@. r = 1.0e-12 * (d + u); nothing)
p = DAEProblem(f!, -ones(4), ones(4), (0.0, 1.0))
@testset "Residual scale must not change solution" begin
for ad in (AutoForwardDiff(), AutoFiniteDiff())
sol = solve(p, DFBDF(; linsolve = KrylovJL_GMRES(), autodiff = ad); abstol = 1.0e-8, reltol = 1.0e-8)
@test SciMLBase.successful_retcode(sol)
@test sol.u[end] ≈ fill(exp(-1), 4) rtol = 1.0e-5
end
end

# Nonlinear in `du`, with exact solution `[exp(-t), exp(-2t)]` (the second factor of the
# first residual is positive). At `u = [1, 1]`, `du = [-1, -2]` its Newton matrix is
# `[3(cj + 1) 0; -2 1]`.
function manufactured!(r, d, u, p, t)
r[1] = (d[1] + u[1]) * (1 + d[1]^2 + u[2]^2)
r[2] = u[2] - u[1]^2
return nothing
end

@testset "Matrix-free DAE Newton matrix at a large cj" begin
u, du, v = [1.0, 1.0], [-1.0, -2.0], [1.0, 0.5]
for autodiff in (AutoForwardDiff(), AutoFiniteDiff()), cj in (1.0, 1.0e6, 1.0e10)
J = dae_jvp_cache(DAEFunction{true}(manufactured!), copy(u), nothing, 0.0; autodiff)
update_coefficients!(J, copy(u), nothing, 0.0; du = copy(du))
set_dae_jvp_cj!(J, cj)
exact = [3(cj + 1) 0; -2 1] * v
@test norm(mul!(zeros(2), J, v) - exact) <= 1.0e-6 * norm(exact)
end

prob = DAEProblem(
manufactured!, [-1.0, -2.0], [1.0, 1.0], (0.0, 1.0); differential_vars = [true, false]
)
for autodiff in (AutoForwardDiff(), AutoFiniteDiff())
sol = solve(
prob, DFBDF(; autodiff, linsolve = KrylovJL_GMRES()); abstol = 1.0e-7, reltol = 1.0e-7
)
@test successful_retcode(sol)
@test maximum(maximum(abs, u .- [exp(-t), exp(-2t)]) for (u, t) in zip(sol.u, sol.t)) <
1.0e-6
end
end

# The matrix-free Newton test measures the Krylov residual through `Pl` applied by `ldiv!`.
# Under `ldiv = false`, with Krylov.jl's own `M`, `W` or `atol`, with DQGMRES, which
# LinearSolve calls without preconditioners, or with MINRES, which measures
# `sqrt(r' * (Pl \ r))`, the solve tests another norm, and `ldiv = false` returned
# `Success` with `u = u0`.
function swapped!(res, du, u, p, t)
res[1] = du[2] + 2u[1] - sin(t)
res[2] = du[1] + 2u[2] - cos(t)
return nothing
end

@testset "Krylov solvers on the matrix-free DAE path" begin
prob = DAEProblem(swapped!, [0.0, 0.0], [0.0, 0.5], (0.0, 1.0); differential_vars = [true, true])
opts = (; abstol = 1.0e-8, reltol = 1.0e-8)
reference = solve(prob, DFBDF(); opts...)
for linsolve in (
KrylovJL_GMRES(; ldiv = false), KrylovJL(; KrylovAlg = LinearSolve.Krylov.dqgmres!),
KrylovJL_MINRES(), KrylovJL_GMRES(; W = 1.0e-20I), KrylovJL_GMRES(; M = 1.0e20I),
KrylovJL_GMRES(; atol = 1.0e20),
)
@test_throws ArgumentError solve(prob, DFBDF(; linsolve); opts...)
end
for linsolve in (KrylovJL_GMRES(), KrylovJL_FGMRES(), KrylovJL_BICGSTAB())
sol = solve(prob, DFBDF(; linsolve); opts...)
@test successful_retcode(sol)
@test sol.u[end] ≈ reference.u[end] rtol = 1.0e-5
end
end
1 change: 1 addition & 0 deletions lib/OrdinaryDiffEqBDF/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if TEST_GROUP == "Core" || TEST_GROUP == "ALL"
@time @safetestset "DAE Reinitialization Tests" include("dae_reinit_tests.jl")
@time @safetestset "DAE Nonlinear Solve Path Tests" include("dae_nlsolve_path_tests.jl")
@time @safetestset "DAE Sparse Jacobian Tests" include("dae_sparse_jacobian_tests.jl")
@time @safetestset "DAE Krylov Tests" include("dae_krylov_tests.jl")

@time @safetestset "BDF Inference Tests" include("inference_tests.jl")
@time @safetestset "BDF Convergence Tests" include("bdf_convergence_tests.jl")
Expand Down
2 changes: 1 addition & 1 deletion lib/OrdinaryDiffEqDifferentiation/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "OrdinaryDiffEqDifferentiation"
version = "3.13.0"
version = "3.14.0"
uuid = "4302a76b-040a-498a-8c04-15b101fed76b"
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>", "Yingbo Ma <mayingbo5@gmail.com>"]

Expand Down
15 changes: 11 additions & 4 deletions lib/OrdinaryDiffEqDifferentiation/src/derivative_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,10 @@ function _calc_W!(alg, integrator, W, nlsolver, cache, dtgamma, repeat_step, new
end

# calculate W
if W isa WOperator
if isdae && W isa JVPCache
# Matrix-free: compute_step! sets the point at each Newton iterate.
new_W && set_dae_jvp_cj!(W, dae_cj(nlsolver, integrator))
elseif W isa WOperator
if isnewton(nlsolver)
# we will call `update_coefficients!` for u/p/t in NLNewton
update_coefficients!(W; gamma = dtgamma)
Expand Down Expand Up @@ -1372,10 +1375,14 @@ function build_J_W(
# If the user has chosen GMRES but no sparse Jacobian, assume that the dense
# Jacobian is a bad idea and create a fully matrix-free solver. This can
# be overridden with concrete_jac.
jacvec = JVPCache(f, copy(u), u, p, t, autodiff = alg_autodiff(alg))
if f isa DAEFunction
J = W = dae_jvp_cache(f, u, p, t; autodiff = alg_autodiff(alg))
else
jacvec = JVPCache(f, copy(u), u, p, t, autodiff = alg_autodiff(alg))

J = jacvec
W = WOperator{IIP}(f.mass_matrix, dtgamma_prototype, J, _vec(u), jacvec)
J = jacvec
W = WOperator{IIP}(f.mass_matrix, dtgamma_prototype, J, _vec(u), jacvec)
end
elseif alg.linsolve !== nothing && !LinearSolve.needs_concrete_A(alg.linsolve) ||
concrete_jac(alg) !== nothing && concrete_jac(alg)
# The linear solver does not need a concrete Jacobian, but the user has
Expand Down
17 changes: 11 additions & 6 deletions lib/OrdinaryDiffEqDifferentiation/src/linsolve_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ function set_linear_reltol!(linsolve, reltol; default_algorithm = false)
end

"""
dolinsolve(integrator, linsolve; alg = unwrap_alg(integrator, true), A = nothing, linu = nothing, b = nothing, reltol = …) -> linres
dolinsolve(integrator, linsolve; alg = unwrap_alg(integrator, true), A = nothing, linu = nothing, b = nothing, reltol = …, abstol = nothing) -> linres

Solve the linear system with the LinearSolve.jl cache `linsolve`, optionally
resetting its matrix `A`, unknown `linu`, right-hand side `b`, and tolerance
`reltol` (see `set_linear_reltol!`). `alg` is the integrator's concrete member
resetting its matrix `A`, unknown `linu`, right-hand side `b`, and tolerances
`reltol` (see `set_linear_reltol!`) and `abstol` (left unchanged when `nothing`; set only
for an iterative solver). `alg` is the integrator's concrete member
algorithm; callers holding a cache that stores it (e.g. `cache.alg`) should
pass it so the call stays a static dispatch. Charges `stats.nf` for the
Jacobian-vector products the solve applied (see `drain_jvp_count!`) and
Expand All @@ -50,14 +51,15 @@ function dolinsolve(
integrator, linsolve;
alg = integrator === nothing ? nothing : unwrap_alg(integrator, true),
A = nothing, linu = nothing, b = nothing,
reltol = integrator === nothing ? nothing : integrator.opts.reltol
reltol = integrator === nothing ? nothing : integrator.opts.reltol,
abstol = nothing
)
b !== nothing && (linsolve.b = b)
linu !== nothing && (linsolve.u = linu)
return _dolinsolve(alg, integrator, linsolve, A, reltol)
return _dolinsolve(alg, integrator, linsolve, A, reltol, abstol)
end

function _dolinsolve(_alg, integrator, linsolve, A, reltol)
function _dolinsolve(_alg, integrator, linsolve, A, reltol, abstol)
if !isnothing(A)
if integrator isa DEIntegrator
(; u, p, t) = integrator
Expand All @@ -71,6 +73,9 @@ function _dolinsolve(_alg, integrator, linsolve, A, reltol)
linsolve, reltol;
default_algorithm = hasproperty(_alg, :linsolve) && isnothing(_alg.linsolve)
)
if abstol !== nothing && !LinearSolve.needs_concrete_A(linsolve.alg)
LinearSolve.update_tolerances!(linsolve; abstol)
end
linres = solve!(linsolve)

# TODO: this ignores the add of the `f` count for add_steps!
Expand Down
Loading