Skip to content

Commit e4d9101

Browse files
authored
Merge pull request #1798 from ValentinKaisermayer/vk-discrete-var-metadata
Adds binary and integer options to variable metadata
2 parents 6abbdc9 + d5c9c0b commit e4d9101

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ export NonlinearSystem, OptimizationSystem
176176
export alias_elimination, flatten
177177
export connect, @connector, Connection, Flow, Stream, instream
178178
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
179-
tunable_parameters, isirreducible, getdescription, hasdescription
179+
tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar,
180+
isintegervar
180181
export ode_order_lowering, dae_order_lowering, liouville_transform
181182
export PDESystem
182183
export Differential, expand_derivatives, @derivatives

src/variables.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,37 @@ end
289289
function hasdescription(x)
290290
getdescription(x) != ""
291291
end
292+
293+
## binary variables =================================================================
294+
struct VariableBinary end
295+
Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary
296+
297+
isbinaryvar(x::Num) = isbinaryvar(Symbolics.unwrap(x))
298+
299+
"""
300+
isbinaryvar(x)
301+
302+
Determine if a variable is binary.
303+
"""
304+
function isbinaryvar(x)
305+
p = Symbolics.getparent(x, nothing)
306+
p === nothing || (x = p)
307+
return Symbolics.getmetadata(x, VariableBinary, false)
308+
end
309+
310+
## integer variables =================================================================
311+
struct VariableInteger end
312+
Symbolics.option_to_metadata_type(::Val{:integer}) = VariableInteger
313+
314+
isintegervar(x::Num) = isintegervar(Symbolics.unwrap(x))
315+
316+
"""
317+
isintegervar(x)
318+
319+
Determine if a variable is integer.
320+
"""
321+
function isintegervar(x)
322+
p = Symbolics.getparent(x, nothing)
323+
p === nothing || (x = p)
324+
return Symbolics.getmetadata(x, VariableInteger, false)
325+
end

test/test_variable_metadata.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,19 @@ sp = Set(p)
8282
@named sys = ODESystem([u ~ p], t)
8383

8484
@test_nowarn show(stdout, "text/plain", sys)
85+
86+
@testset "binary" begin
87+
@parameters t
88+
@variables u(t) [binary = true]
89+
@parameters p [binary = true]
90+
@test isbinaryvar(u)
91+
@test isbinaryvar(p)
92+
end
93+
94+
@testset "integer" begin
95+
@parameters t
96+
@variables u(t) [integer = true]
97+
@parameters p [integer = true]
98+
@test isintegervar(u)
99+
@test isintegervar(p)
100+
end

0 commit comments

Comments
 (0)