forked from SciML/OrdinaryDiffEq.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffEqBaseUnitfulExt.jl
More file actions
47 lines (41 loc) · 1.35 KB
/
DiffEqBaseUnitfulExt.jl
File metadata and controls
47 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module DiffEqBaseUnitfulExt
using DiffEqBase
import SciMLBase: unitfulvalue, value
using Unitful
# Support adaptive errors should be errorless for exponentiation
value(x::Type{Unitful.AbstractQuantity{T, D, U}}) where {T, D, U} = T
value(x::Unitful.AbstractQuantity) = x.val
unitfulvalue(x::Type{T}) where {T <: Unitful.AbstractQuantity} = T
unitfulvalue(x::Unitful.AbstractQuantity) = x
DiffEqBase.stripunits(x::Unitful.AbstractQuantity) = Unitful.ustrip(x)
@inline function DiffEqBase.ODE_DEFAULT_NORM(
u::AbstractArray{
<:Unitful.AbstractQuantity,
N,
},
t
) where {N}
return sqrt(
sum(
x -> DiffEqBase.ODE_DEFAULT_NORM(x[1], x[2]),
zip((value(x) for x in u), Iterators.repeated(t))
) / length(u)
)
end
@inline function DiffEqBase.ODE_DEFAULT_NORM(
u::Array{<:Unitful.AbstractQuantity, N},
t
) where {N}
return sqrt(
sum(
x -> DiffEqBase.ODE_DEFAULT_NORM(x[1], x[2]),
zip((value(x) for x in u), Iterators.repeated(t))
) / length(u)
)
end
@inline DiffEqBase.ODE_DEFAULT_NORM(u::Unitful.AbstractQuantity, t) = abs(value(u))
@inline function DiffEqBase.UNITLESS_ABS2(x::Unitful.AbstractQuantity)
return real(abs2(x) / oneunit(x) * oneunit(x))
end
DiffEqBase._rate_prototype(u, t, onet) = u / unit(t)
end