Skip to content

Commit bd87049

Browse files
fixes input output handling for array variables (#1808)
1 parent 74e2571 commit bd87049

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/inputoutput.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ function same_or_inner_namespace(u, var)
119119
nv = get_namespace(var)
120120
nu == nv || # namespaces are the same
121121
startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namepsace to nu
122-
occursin('', var) && !occursin('', u) # or u is top level but var is internal
122+
occursin('', string(Symbolics.getname(var))) &&
123+
!occursin('', string(Symbolics.getname(u))) # or u is top level but var is internal
123124
end
124125

125126
function inner_namespace(u, var)
126127
nu = get_namespace(u)
127128
nv = get_namespace(var)
128129
nu == nv && return false
129130
startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namepsace to nu
130-
occursin('', var) && !occursin('', u) # or u is top level but var is internal
131+
occursin('', string(Symbolics.getname(var))) &&
132+
!occursin('', string(Symbolics.getname(u))) # or u is top level but var is internal
131133
end
132134

133135
"""
@@ -136,7 +138,7 @@ end
136138
Return the namespace of a variable as a string. If the variable is not namespaced, the string is empty.
137139
"""
138140
function get_namespace(x)
139-
sname = string(x)
141+
sname = string(Symbolics.getname(x))
140142
parts = split(sname, '')
141143
if length(parts) == 1
142144
return ""

test/input_output_handling.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ using ModelingToolkit: get_namespace, has_var, inputs, outputs, is_bound, bound_
55
# Test input handling
66
@parameters tv
77
D = Differential(tv)
8-
@variables x(tv) u(tv) [input = true]
8+
@variables x(tv) u(tv) [input = true] v(tv)[1:2] [input = true]
99
@test isinput(u)
1010

1111
@named sys = ODESystem([D(x) ~ -x + u], tv) # both u and x are unbound
12+
@named sys1 = ODESystem([D(x) ~ -x + v[1] + v[2]], tv) # both v and x are unbound
1213
@named sys2 = ODESystem([D(x) ~ -sys.x], tv, systems = [sys]) # this binds sys.x in the context of sys2, sys2.x is still unbound
14+
@named sys21 = ODESystem([D(x) ~ -sys.x], tv, systems = [sys1]) # this binds sys.x in the context of sys2, sys2.x is still unbound
1315
@named sys3 = ODESystem([D(x) ~ -sys.x + sys.u], tv, systems = [sys]) # This binds both sys.x and sys.u
16+
@named sys31 = ODESystem([D(x) ~ -sys.x + sys1.v[1]], tv, systems = [sys1]) # This binds both sys.x and sys1.v[1]
1417

1518
@named sys4 = ODESystem([D(x) ~ -sys.x, u ~ sys.u], tv, systems = [sys]) # This binds both sys.x and sys3.u, this system is one layer deeper than the previous. u is directly forwarded to sys.u, and in this case sys.u is bound while u is not
1619

@@ -23,13 +26,19 @@ D = Differential(tv)
2326
@test get_namespace(sys.x) == "sys"
2427
@test get_namespace(sys2.x) == "sys2"
2528
@test get_namespace(sys2.sys.x) == "sys2₊sys"
29+
@test get_namespace(sys21.sys1.v) == "sys21₊sys1"
2630

2731
@test !is_bound(sys, u)
2832
@test !is_bound(sys, x)
2933
@test !is_bound(sys, sys.u)
3034
@test is_bound(sys2, sys.x)
3135
@test !is_bound(sys2, sys.u)
3236
@test !is_bound(sys2, sys2.sys.u)
37+
@test is_bound(sys21, sys.x)
38+
@test !is_bound(sys21, sys1.v[1])
39+
@test !is_bound(sys21, sys1.v[2])
40+
@test is_bound(sys31, sys1.v[1])
41+
@test !is_bound(sys31, sys1.v[2])
3342

3443
# simplification turns input variables into parameters
3544
ssys = structural_simplify(sys)

0 commit comments

Comments
 (0)