forked from SciML/OrdinaryDiffEq.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffEqBaseFlexUnitsExt.jl
More file actions
47 lines (41 loc) · 1.21 KB
/
DiffEqBaseFlexUnitsExt.jl
File metadata and controls
47 lines (41 loc) · 1.21 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 DiffEqBaseFlexUnitsExt
using DiffEqBase
import SciMLBase: unitfulvalue, value
using FlexUnits
# Support adaptive errors should be errorless for exponentiation
value(::Type{Quantity{T, U}}) where {T, U} = T
value(x::Quantity{T, U}) where {T, U} = dstrip(x)
unitfulvalue(::Type{T}) where {T <: Quantity} = T
unitfulvalue(x::Quantity) = x
DiffEqBase.stripunits(x::Quantity) = dstrip(x)
@inline function DiffEqBase.ODE_DEFAULT_NORM(
u::AbstractArray{
<:Quantity,
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{<:Quantity, 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::Quantity, t) = abs(value(u))
@inline function DiffEqBase.UNITLESS_ABS2(x::Quantity)
return real(abs2(dstrip(x)))
end
DiffEqBase._rate_prototype(u, t::Quantity, onet) = u / unit(t)
end