Skip to content

Commit 0c6c628

Browse files
Merge pull request #1213 from lamorton/comparisons
Handle conditionals & comparisons correctly in unit validation.
2 parents f804e54 + e61a0f5 commit 0c6c628

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/systems/validation.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ function get_unit(x::Symbolic)
5151
equivalent(other,firstunit) || throw(ValidationError(", in sum $x, units [$termlist] do not match."))
5252
end
5353
return firstunit
54+
elseif operation(x) in ( Base.:> , Base.:< , == )
55+
terms = get_unit.(arguments(x))
56+
equivalent(terms[1],terms[2]) || throw(ValidationError(", in comparison $x, units [$(terms[1])] and [$(terms[2])] do not match."))
57+
return unitless
58+
elseif operation(x) == ifelse || operation(x) == IfElse.ifelse
59+
terms = get_unit.(arguments(x))
60+
terms[1] == unitless || throw(ValidationError(", in $x, [$(terms[1])] is not dimensionless."))
61+
equivalent(terms[2],terms[3]) || throw(ValidationError(", in $x, units [$(terms[2])] and [$(terms[3])] do not match."))
62+
return terms[2]
5463
elseif operation(x) == Symbolics._mapreduce
5564
if x.arguments[2] == +
5665
get_unit(x.arguments[3])

test/units.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ModelingToolkit, Unitful, OrdinaryDiffEq, DiffEqJump
1+
using ModelingToolkit, Unitful, OrdinaryDiffEq, DiffEqJump, IfElse
22
using Test
33
MT = ModelingToolkit
44
@parameters τ [unit = u"ms"]
@@ -196,4 +196,14 @@ maj2 = MassActionJump(γ, [I => 1], [I => -1, R => 1])
196196

197197
maj1 = MassActionJump(2.0, [0 => 1], [S => 1])
198198
maj2 = MassActionJump(γ, [S => 1], [S => -1])
199-
@named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ])
199+
@named js4 = JumpSystem([maj1, maj2], t, [S], [β, γ])
200+
201+
#Test comparisons
202+
@parameters t
203+
vars = @variables x(t)
204+
D = Differential(t)
205+
eqs =
206+
[
207+
D(x) ~ IfElse.ifelse(t>0.1,2,1)
208+
]
209+
@named sys = ODESystem(eqs, t, vars, [])

0 commit comments

Comments
 (0)