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
6 changes: 5 additions & 1 deletion lib/OrdinaryDiffEqBDF/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
SciMLTesting = "09d9d899-5365-40a9-917a-5f67fddea283"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SparseConnectivityTracer = "9f842d2f-2579-4b1d-911e-f412cf18a3f5"
SparseMatrixColorings = "0a514795-09f3-496d-8182-132a7b665d35"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[sources]
Expand Down Expand Up @@ -83,7 +85,9 @@ DiffEqBase = "7"
Reexport = "1.2.2"
SafeTestsets = "0.1.0"
SparseArrays = "1.10"
SparseConnectivityTracer = "1"
SparseMatrixColorings = "0.4.23"
StaticArrays = "1.9.18"

[targets]
test = ["DiffEqDevTools", "DifferentiationInterface", "ForwardDiff", "Random", "SafeTestsets", "StaticArrays", "Test", "ODEProblemLibrary", "NonlinearSolve", "Enzyme", "LinearSolve", "Pkg", "SciMLTesting", "OrdinaryDiffEqRosenbrock", "SparseArrays", "SciMLOperators"]
test = ["DiffEqDevTools", "DifferentiationInterface", "ForwardDiff", "Random", "SafeTestsets", "StaticArrays", "Test", "ODEProblemLibrary", "NonlinearSolve", "Enzyme", "LinearSolve", "Pkg", "SciMLTesting", "OrdinaryDiffEqRosenbrock", "SparseArrays", "SparseConnectivityTracer", "SparseMatrixColorings", "SciMLOperators"]
101 changes: 101 additions & 0 deletions lib/OrdinaryDiffEqBDF/test/dae_autosparse_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using OrdinaryDiffEqBDF, SparseArrays, Test
import ADTypes, SciMLBase
using ADTypes: AutoFiniteDiff, AutoForwardDiff, AutoSparse
using SciMLBase: DAEFunction, DAEProblem, NoInit, isinplace, successful_retcode
using SparseConnectivityTracer: TracerSparsityDetector
using SparseMatrixColorings: GreedyColoringAlgorithm

const sparse_fd = AutoSparse(
AutoForwardDiff(); sparsity_detector = TracerSparsityDetector(),
coloring_algorithm = GreedyColoringAlgorithm()
)

# ∂F/∂du = diag(1, 1, 0) and ∂F/∂u are both strict subsets of the pattern of J.
function oscillator!(res, du, u, p, t)
res[1] = du[1] - u[2]
res[2] = du[2] + u[1]
res[3] = u[3] - u[1]
return nothing
end
oscillator(du, u, p, t) = [du[1] - u[2], du[2] + u[1], u[3] - u[1]]

# Heat equation with algebraic Dirichlet end nodes and a long-range du coupling,
# so ∂F/∂du has entries outside ∂F/∂u's tridiagonal band.
function heat!(res, du, u, p, t)
n = length(u)
res[1] = u[1] - sin(t)
for i in 2:(n - 1)
res[i] = du[i] - (u[i - 1] - 2u[i] + u[i + 1])
end
res[2] += du[n - 1] / 10
res[n] = u[n]
return nothing
end

@testset "DFBDF AutoSparse with differing ∂F/∂u and ∂F/∂du patterns" begin
heat_n = 12
problems = (
DAEProblem(
oscillator!, [0.0, -1.0, 0.0], [1.0, 0.0, 1.0], (0.0, 1.0);
differential_vars = [true, true, false]
),
DAEProblem{false}(
oscillator, [0.0, -1.0, 0.0], [1.0, 0.0, 1.0], (0.0, 1.0);
differential_vars = [true, true, false]
),
DAEProblem(
heat!, [1.0; zeros(heat_n - 1)], zeros(heat_n), (0.0, 1.0);
differential_vars = [false; trues(heat_n - 2); false]
),
)
for prob in problems
sol_dense = solve(prob, DFBDF(); abstol = 1.0e-10, reltol = 1.0e-10, initializealg = NoInit())
sol_sparse = solve(
prob, DFBDF(; autodiff = sparse_fd);
abstol = 1.0e-10, reltol = 1.0e-10, initializealg = NoInit()
)
@test successful_retcode(sol_sparse)
@test sol_sparse.u[end] ≈ sol_dense.u[end] rtol = 1.0e-8
@test sol_sparse.stats.njacs > 0
if isinplace(prob)
integrator = init(prob, DFBDF(; autodiff = sparse_fd); initializealg = NoInit())
@test integrator.cache.nlsolver.cache.J isa SparseMatrixCSC
@test integrator.cache.nlsolver.cache.dae_jacobians.J_du isa SparseMatrixCSC
end
end
@test solve(
problems[1], DFBDF(; autodiff = sparse_fd); abstol = 1.0e-10, reltol = 1.0e-10
).u[end][1] ≈ cos(1) rtol = 1.0e-6
end

function decay!(r, du, u, p, t)
r[1] = du[1] + u[1]
r[2] = u[2] - u[1]
return nothing
end

@testset "Known sparsity pattern with stored zeros" begin
# The pattern's entries are all stored zeros; they are still structural.
P = sparse([1, 2, 2], [1, 1, 2], [0.0, 0.0, 0.0], 2, 2)
backend = AutoSparse(AutoFiniteDiff(); sparsity_detector = ADTypes.KnownJacobianSparsityDetector(P), coloring_algorithm = GreedyColoringAlgorithm())
prob = DAEProblem(decay!, [-1.0, -1.0], [1.0, 1.0], (0.0, 0.4); differential_vars = [true, false])
sol = solve(prob, DFBDF(autodiff = backend); initializealg = SciMLBase.NoInit(), abstol = 1.0e-10, reltol = 1.0e-10)
@test SciMLBase.successful_retcode(sol)
@test sol.u[end] ≈ fill(exp(-0.4), 2) atol = 1.0e-7 rtol = 1.0e-7

integrator = init(prob, DFBDF(autodiff = backend); initializealg = NoInit())
nlcache = integrator.cache.nlsolver.cache
pattern(A) = findnz(A)[1:2]
@test pattern(nlcache.J) == pattern(P)
@test pattern(nlcache.dae_jacobians.J_du) == pattern(P)
@test pattern(nlcache.W) == pattern(P)
solve!(integrator)
# After a step, `J` holds ∂F/∂u and `J_du` holds ∂F/∂(du).
@test Matrix(nlcache.J) ≈ [1.0 0.0; -1.0 1.0]
@test Matrix(nlcache.dae_jacobians.J_du) ≈ [1.0 0.0; 0.0 0.0] atol = 1.0e-12

# Storage that cannot hold the known pattern must fail rather than skip derivatives.
f_short = DAEFunction(decay!; jac_prototype = sparse([1, 2], [1, 2], [1.0, 1.0]))
prob_short = DAEProblem(f_short, [-1.0, -1.0], [1.0, 1.0], (0.0, 0.4); differential_vars = [true, false])
@test_throws DimensionMismatch init(prob_short, DFBDF(autodiff = backend); initializealg = NoInit())
end
1 change: 1 addition & 0 deletions lib/OrdinaryDiffEqBDF/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ end
if TEST_GROUP == "Core" || TEST_GROUP == "ALL"
@time @safetestset "DAE Convergence Tests" include("dae_convergence_tests.jl")
@time @safetestset "DAE AD Tests" include("dae_ad_tests.jl")
@time @safetestset "DAE AutoSparse Tests" include("dae_autosparse_tests.jl")
@time @safetestset "DAE Event Tests" include("dae_event.jl")
@time @safetestset "DAE derivative_discontinuity! Tests" include("dae_derivative_discontinuity_tests.jl")
@time @safetestset "DAE Initialization Tests" include("dae_initialization_tests.jl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import SparseArrays: nonzeros, nzrange, rowvals, spzeros, SparseMatrixCSC, Abstr
# Override the sparse checking functions
OrdinaryDiffEqDifferentiation.is_sparse(::AbstractSparseMatrix) = true
OrdinaryDiffEqDifferentiation.is_sparse_csc(::SparseMatrixCSC) = true
OrdinaryDiffEqDifferentiation.jac_from_pattern(::Type{T}, pattern::SparseMatrixCSC) where {T} =
SparseMatrixCSC{T}(pattern)

# Override the sparse array manipulation functions
OrdinaryDiffEqDifferentiation.nonzeros(A::AbstractSparseMatrix) = nonzeros(A)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ import DiffEqBase: OrdinaryDiffEqTag
is_sparse(::Any) = false
is_sparse_csc(::Any) = false

# Allocate a Jacobian with `pattern`'s structure. A known pattern may hold stored zeros,
# which are structural entries the coloring decompresses into, but broadcasting drops them.
jac_from_pattern(::Type{T}, pattern) where {T} = convert.(T, pattern)

# Seeding a sparsity pattern from the mass matrix uses `findall`/`getindex`, which a
# SciMLOperator does not support. `convert` reads the operator's currently-cached array
# rather than re-evaluating it, which is what is wanted here: only the structural
Expand Down
10 changes: 4 additions & 6 deletions lib/OrdinaryDiffEqDifferentiation/src/derivative_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1386,12 +1386,10 @@ function build_J_W(
if alg_autodiff(alg) isa AutoSparse
if isnothing(f.sparsity)
!isnothing(jac_config) ?
convert.(
eltype(u), sparsity_pattern(jac_config[1])
) :
jac_from_pattern(eltype(u), sparsity_pattern(jac_config[1])) :
spzeros(eltype(u), length(u), length(u))
elseif eltype(f.sparsity) == Bool
convert.(eltype(u), f.sparsity)
jac_from_pattern(eltype(u), f.sparsity)
else
f.sparsity
end
Expand Down Expand Up @@ -1421,10 +1419,10 @@ function build_J_W(
elseif f.jac_prototype === nothing
if alg_autodiff(alg) isa AutoSparse
if isnothing(f.sparsity)
!isnothing(jac_config) ? convert.(eltype(u), sparsity_pattern(jac_config[1])) :
!isnothing(jac_config) ? jac_from_pattern(eltype(u), sparsity_pattern(jac_config[1])) :
spzeros(eltype(u), length(u), length(u))
elseif eltype(f.sparsity) == Bool
convert.(eltype(u), f.sparsity)
jac_from_pattern(eltype(u), f.sparsity)
else
f.sparsity
end
Expand Down
2 changes: 1 addition & 1 deletion lib/OrdinaryDiffEqDifferentiation/test/qa/qa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ run_qa(
Symbol("prepare!_derivative"), Symbol("prepare!_jacobian"),
# this sublib's own internal sparse-handling API, accessed
# qualified from OrdinaryDiffEqDifferentiationSparseArraysExt
:get_nzval, :is_sparse, :is_sparse_csc, :nonzeros,
:get_nzval, :is_sparse, :is_sparse_csc, :jac_from_pattern, :nonzeros,
Symbol("set_all_nzval!"), :spzeros, :_update_sparse_diagonal!,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import OrdinaryDiffEqDifferentiation: update_W!, is_always_new, build_uf, build_
build_jac_config, dolinsolve, set_linear_reltol!,
resize_jac_config!, jacobian2W!, jacobian!, calc_J

import SparseArrays: SparseMatrixCSC, findnz, sparse
import StaticArraysCore: StaticArray

include("type.jl")
Expand Down
34 changes: 32 additions & 2 deletions lib/OrdinaryDiffEqNonlinearSolve/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,35 @@ struct DAEJacobiansConstantCache{JduType, ufuType, ufduType}
uf_du::ufduType
end

# `J_u` and `J_du` are stored in `J`'s sparsity pattern (the union of dF/du and dF/d(du)),
# and sparse AD decompresses only into the exact pattern it colored, so the separated
# configs must color `J`'s pattern rather than detect their own. That is only sound if `J`
# stores every entry of a known pattern, stored zeros included; otherwise the derivatives
# missing from `J` would be silently skipped.
function with_jac_sparsity(alg, J, uf, du1, u)
ad = alg_autodiff(alg)
(ad isa ADTypes.AutoSparse && J isa SparseMatrixCSC) || return alg
detector = ADTypes.sparsity_detector(ad)
if detector isa ADTypes.KnownJacobianSparsityDetector
known = sparse(ADTypes.jacobian_sparsity(uf, du1, u, detector))
stored(A) = zip(findnz(A)[1], findnz(A)[2])
issubset(stored(known), Set(stored(J))) || throw(
DimensionMismatch(
"The Jacobian storage does not hold every entry of the known sparsity \
pattern (stored zeros included), so the DAE Jacobians cannot be \
decompressed into it. Make `jac_prototype` cover the sparsity pattern, or \
update OrdinaryDiffEqDifferentiation."
)
)
end
autodiff = ADTypes.AutoSparse(
ADTypes.dense_ad(ad);
sparsity_detector = ADTypes.KnownJacobianSparsityDetector(J),
coloring_algorithm = ADTypes.coloring_algorithm(ad)
)
return remake(alg; autodiff)
end

function _nlalg_with_linsolve(inner_alg, linsolve)
linsolve === nothing && return inner_alg
!hasfield(typeof(inner_alg), :descent) && return inner_alg
Expand Down Expand Up @@ -962,11 +991,12 @@ function build_nlsolver(
else
uf_u = DAEJacobianUWrapper(f, p, zero(uprev), t)
uf_du = DAEJacobianDuWrapper(f, p, copy(uprev), t)
dae_jac_alg = with_jac_sparsity(alg, J, uf, du1, u)
jac_config_u = build_jac_config(
alg, nf, uf_u, du1, uprev, u, ztmp, dz
dae_jac_alg, nf, uf_u, du1, uprev, u, ztmp, dz
)
jac_config_du = build_jac_config(
alg, nf, uf_du, du1, uprev, u, ztmp, dz
dae_jac_alg, nf, uf_du, du1, uprev, u, ztmp, dz
)
J_du = fill!(similar(J), false)
dae_jacobians = DAEJacobiansCache(
Expand Down
Loading