|
1 | 1 | module MechGlueDiffEqBase |
2 | | -import Unitfu: AbstractQuantity, Quantity, ustrip, norm, unit |
3 | | -import DiffEqBase: value, ODE_DEFAULT_NORM, UNITLESS_ABS2, zero |
| 2 | +import Base: similar |
| 3 | +import Unitfu: AbstractQuantity, Quantity, ustrip, norm, unit, zero |
| 4 | +import Unitfu: Dimensions, FreeUnits |
| 5 | +import DiffEqBase: value, ODE_DEFAULT_NORM, UNITLESS_ABS2 |
4 | 6 | import DiffEqBase: calculate_residuals, @muladd |
5 | 7 | using RecursiveArrayTools |
6 | 8 | export value, ODE_DEFAULT_NORM, UNITLESS_ABS2, Unitfu, AbstractQuantity, Quantity |
7 | | -export norm , ArrayPartition # Probably no longer necessary with changes in Unitfu 1.7.7. We could perhaps drop this depencency. |
| 9 | +export norm , ArrayPartition, similar, zero |
8 | 10 |
|
9 | 11 | # This is identical to what DiffEqBase defines for Unitful |
10 | 12 | function value(x::Type{AbstractQuantity{T,D,U}}) where {T,D,U} |
|
45 | 47 | abs2(xul)::T |
46 | 48 | end |
47 | 49 |
|
| 50 | +# Vectors with compatible units, treat as normal |
| 51 | +zero(x::Vector{Quantity{T, D, U}}) where {T,D,U} = fill!(similar(x), zero(Quantity{T, D, U})) |
| 52 | +# Vectors with incompatible units, special inferreable treatment |
| 53 | +function zero(x::Vector{Q}) where {Q<:AbstractQuantity{T, D, U} where {D,U}} where T |
| 54 | + x0 = copy(x) |
| 55 | + for i in eachindex(x0) |
| 56 | + x = x0[i] |
| 57 | + x0[i] = 0 * x * sign(x) |
| 58 | + end |
| 59 | + x0 |
| 60 | +end |
| 61 | + |
| 62 | +# Vectors with compatible units, treat as normal |
| 63 | +similar(x::Vector{Quantity{T, D, U}}) where {T,D,U} = Vector{Quantity{T, D, U}}(undef, size(x,1)) |
| 64 | +#similar(a::Array{T,1}) where {T} = Vector{T}(undef, size(a,1)) |
| 65 | +# Vectors with incompatible units, special inferreable treatment |
| 66 | +# VERY similar is still similar and (very slightly) different |
| 67 | +similar(x::Vector{Q}) where {Q<:AbstractQuantity{T, D, U} where {D,U}} where T = copy(x) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +# KISS pre-compillation to reduce loading times |
| 73 | +# This is simply a boiled-down obfuscated test_4.jl |
| 74 | +import Unitfu: m, s, kg, N, ∙ |
| 75 | +let |
| 76 | + r0ul = [1131.340, -2282.343, 6672.423] |
| 77 | + r0ul = [1131.340, -2282.343, 6672.423] |
| 78 | + v0ul = [-5.64305, 4.30333, 2.42879] |
| 79 | + rv0ul = ArrayPartition(r0ul,v0ul) |
| 80 | + ODE_DEFAULT_NORM(rv0ul, 0.0) |
| 81 | + r0 = [1131.340, -2282.343, 6672.423]∙kg |
| 82 | + v0 = [-5.64305, 4.30333, 2.42879]∙kg/s |
| 83 | + rv0 = ArrayPartition(r0, v0) |
| 84 | + ODE_DEFAULT_NORM(rv0, 0.0s) |
| 85 | + r0 = [1.0kg, 2.0N, 3.0m/s, 4.0m/s] |
| 86 | + v0 = [1.0kg/s, 2.0N/s, 3.0m/s^2, 4m/s^2] |
| 87 | + rv0 = ArrayPartition(r0, v0) |
| 88 | + r0ul = [1131.340, -2282.343, 6672.423] |
| 89 | + v0ul = [-5.64305, 4.30333, 2.42879] |
| 90 | + rv0ul = ArrayPartition(r0ul, v0ul) |
| 91 | + r0 = [1131.340, -2282.343, 6672.423]∙kg |
| 92 | + r1 = [1kg, 2.0m] |
| 93 | + zero(r0) |
| 94 | + zero(r1) |
| 95 | + rv0 = ArrayPartition(r0) |
| 96 | + zero(rv0) |
| 97 | + rv1 = ArrayPartition(r1) |
| 98 | + zero(rv1) |
| 99 | + r0 = [1.0kg, -2kg, 3m/s, 4m/s] |
| 100 | + zero(r0) |
| 101 | + rv0 = ArrayPartition(r0) |
| 102 | + zer = zero(rv0) |
| 103 | + zer == [0.0kg, 0.0kg, 0.0m/s, 0.0m/s] |
| 104 | + r0 = [1131.340, -2282.343, 6672.423]∙kg |
| 105 | + simi = similar(r0) |
| 106 | + rv0 = ArrayPartition(r0) |
| 107 | + sima = similar(rv0) |
| 108 | + r0 = [1.0kg, -2kg, 3m/s, 4m/s] |
| 109 | + similar(r0) |
| 110 | + rv0 = ArrayPartition(r0) |
| 111 | + sima = similar(rv0) |
| 112 | + r0ul = [1131.340, -2282.343, 6672.423] |
| 113 | + v0ul = [-5.64305, 4.30333, 2.42879] |
| 114 | + rv0ul = ArrayPartition(r0ul,v0ul) |
| 115 | + UNITLESS_ABS2(rv0ul) |
| 116 | + r0 = [1.0kg, 2.0N, 3.0m/s, 4.0m/s] |
| 117 | + v0 = [1.0kg/s, 2.0N/s, 3.0m/s^2, 4m/s^2] |
| 118 | + rv0 = ArrayPartition(r0, v0) |
| 119 | + UNITLESS_ABS2(1.0kg) |
| 120 | + UNITLESS_ABS2(r0) |
| 121 | + UNITLESS_ABS2(rv0) |
| 122 | + nothing |
| 123 | +end |
| 124 | + |
48 | 125 | end |
0 commit comments