This repository was archived by the owner on May 12, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 126
fix: add DynamicQuantities extension for unitful factorization paths #1293
Merged
ChrisRackauckas
merged 2 commits into
SciML:master
from
MilesCranmerBot:wave2-dq-compat
Mar 17, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| module DiffEqBaseDynamicQuantitiesExt | ||
|
|
||
| using DiffEqBase | ||
| using DynamicQuantities | ||
| using LinearAlgebra | ||
| import DiffEqBase: default_factorize | ||
|
|
||
| @inline DiffEqBase.ODE_DEFAULT_NORM(u::UnionAbstractQuantity, t) = abs(ustrip(u)) | ||
| @inline function DiffEqBase.UNITLESS_ABS2(x::UnionAbstractQuantity) | ||
| return real(abs2(ustrip(x))) | ||
| end | ||
|
|
||
| DiffEqBase._rate_prototype(u, t::UnionAbstractQuantity, onet) = u / oneunit(t) | ||
| DiffEqBase.timedepentdtmin(t::UnionAbstractQuantity, dtmin) = | ||
| abs(ustrip(dtmin / oneunit(t)) * oneunit(t)) | ||
|
|
||
| # Rosenbrock/SDIRK solvers form W/J matrices with Quantity eltype. Factorize/solve in | ||
| # value-space (Float64), but return solutions with the RHS units. | ||
| struct DQUnitlessLU{F, UT} | ||
| F::F | ||
| ut::UT | ||
| end | ||
|
|
||
| @inline function _infer_ut(A::AbstractMatrix{<:UnionAbstractQuantity}) | ||
| @inbounds for a in A | ||
| va = ustrip(a) | ||
| if !iszero(va) | ||
| return oneunit(inv(a)) | ||
| end | ||
| end | ||
| return oneunit(1.0) | ||
| end | ||
|
|
||
| function default_factorize(A::AbstractMatrix{<:UnionAbstractQuantity}) | ||
| isempty(A) && return DQUnitlessLU( | ||
| lu(Matrix{Float64}(undef, 0, 0); check = false), | ||
| oneunit(1.0), | ||
| ) | ||
| ut = _infer_ut(A) | ||
| return DQUnitlessLU(lu(ustrip.(A); check = false), ut) | ||
| end | ||
|
|
||
| function LinearAlgebra.ldiv!( | ||
| x::AbstractVector{<:UnionAbstractQuantity}, | ||
| W::DQUnitlessLU, | ||
| b::AbstractVector{<:UnionAbstractQuantity}, | ||
| ) | ||
| vb = ustrip.(b) | ||
| vx = similar(vb) | ||
| LinearAlgebra.ldiv!(vx, W.F, vb) | ||
| @inbounds for i in eachindex(x) | ||
| x[i] = vx[i] * (oneunit(b[i]) * W.ut) | ||
| end | ||
| return x | ||
| end | ||
|
|
||
| function Base.:(\)(W::DQUnitlessLU, b::AbstractVector{<:UnionAbstractQuantity}) | ||
| vb = ustrip.(b) | ||
| vx = W.F \ vb | ||
| out = similar(b) | ||
| @inbounds for i in eachindex(out) | ||
| out[i] = vx[i] * (oneunit(b[i]) * W.ut) | ||
| end | ||
| return out | ||
| end | ||
|
|
||
| end | ||
|
Comment on lines
+43
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are piracy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DQUnitlessLU is defined above, so I think it's fine, no?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, okay. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using Test | ||
| using DiffEqBase | ||
| using DynamicQuantities | ||
| using LinearAlgebra | ||
|
|
||
| @testset "DiffEqBaseDynamicQuantitiesExt" begin | ||
| # Basic quantity hooks | ||
| q = 3.0u"m" | ||
| @test DiffEqBase.ODE_DEFAULT_NORM(q, 0.0) == 3.0 | ||
|
|
||
| qc = (3.0 + 4.0im)u"m" | ||
| @test DiffEqBase.UNITLESS_ABS2(qc) == 25.0 | ||
|
|
||
| r = DiffEqBase._rate_prototype(2.0u"m", 4.0u"s", 1) | ||
| @test isapprox(ustrip(r), 2.0) | ||
| @test oneunit(r) == oneunit(1.0u"m") / oneunit(1.0u"s") | ||
|
|
||
| dt = DiffEqBase.timedepentdtmin(1.0u"s", 1.0u"ms") | ||
| @test isapprox(ustrip(dt), 0.001) | ||
| @test oneunit(dt) == oneunit(1.0u"s") | ||
|
|
||
| # Factorization bridge for Quantity matrices | ||
| A = [2.0u"m" 0.0u"m"; 0.0u"m" 4.0u"m"] | ||
| b = [4.0u"m", 8.0u"m"] | ||
|
|
||
| W = DiffEqBase.default_factorize(A) | ||
| x = W \ b | ||
| @test maximum(abs.(ustrip.(A * x .- b))) ≤ 1e-12 | ||
|
|
||
| x2 = similar(b) | ||
| ldiv!(x2, W, b) | ||
| @test maximum(abs.(ustrip.(A * x2 .- b))) ≤ 1e-12 | ||
|
|
||
| # _infer_ut fallback when all entries are zero | ||
| Az = fill(0.0u"m", 2, 2) | ||
| Wz = DiffEqBase.default_factorize(Az) | ||
| @test Wz.ut == oneunit(1.0) | ||
|
|
||
| # empty-matrix path (coverage + sanity) | ||
| A0 = Matrix{typeof(1.0u"m")}(undef, 0, 0) | ||
| W0 = DiffEqBase.default_factorize(A0) | ||
| b0 = typeof(1.0u"m")[] | ||
| @test length(W0 \ b0) == 0 | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MilesCranmerBot I think you forgot to include unittests in this PR? Please add them and ensure 100% code coverage here.