Skip to content

Commit 29f3c6e

Browse files
Merge pull request #3711 from AayushSabharwal/as/var-of-var-of-iv
fix: fix namespacing of variables of variables
2 parents 1deee30 + 6482983 commit 29f3c6e

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/independent_variables.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ Define one or more independent variables. For example:
77
@variables x(t)
88
"""
99
macro independent_variables(ts...)
10-
:(@parameters $(ts...)) |> esc # TODO: treat independent variables separately from variables and parameters
10+
Symbolics._parse_vars(:independent_variables,
11+
Real,
12+
ts,
13+
toiv) |> esc
1114
end
1215

13-
toiv(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, PARAMETER)
16+
toiv(s::Symbolic) = GlobalScope(setmetadata(s, MTKVariableTypeCtx, PARAMETER))
17+
toiv(s::Symbolics.Arr) = wrap(toiv(value(s)))
1418
toiv(s::Num) = Num(toiv(value(s)))

src/systems/abstractsystem.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,9 @@ function is_array_of_symbolics(x)
11691169
end
11701170

11711171
function namespace_expr(
1172-
O, sys, n = nameof(sys); ivs = independent_variables(sys))
1172+
O, sys, n = (sys === nothing ? nothing : nameof(sys));
1173+
ivs = sys === nothing ? nothing : independent_variables(sys))
1174+
sys === nothing && return O
11731175
O = unwrap(O)
11741176
# Exceptions for arrays of symbolic and Ref of a symbolic, the latter
11751177
# of which shows up in broadcasts
@@ -1538,9 +1540,9 @@ function defaults_and_guesses(sys::AbstractSystem)
15381540
merge(guesses(sys), defaults(sys))
15391541
end
15401542

1541-
unknowns(sys::Union{AbstractSystem, Nothing}, v) = renamespace(sys, v)
1543+
unknowns(sys::Union{AbstractSystem, Nothing}, v) = namespace_expr(v, sys)
15421544
for vType in [Symbolics.Arr, Symbolics.Symbolic{<:AbstractArray}]
1543-
@eval unknowns(sys::AbstractSystem, v::$vType) = renamespace(sys, v)
1545+
@eval unknowns(sys::AbstractSystem, v::$vType) = namespace_expr(v, sys)
15441546
@eval parameters(sys::AbstractSystem, v::$vType) = toparam(unknowns(sys, v))
15451547
end
15461548
parameters(sys::Union{AbstractSystem, Nothing}, v) = toparam(unknowns(sys, v))

src/systems/codegen_utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,11 @@ function isdelay(var, iv)
123123
if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator)
124124
args = arguments(var)
125125
length(args) == 1 || return false
126-
isequal(args[1], iv) || return true
126+
arg = args[1]
127+
isequal(arg, iv) && return false
128+
iscall(arg) || return true
129+
issym(operation(arg)) && !iscalledparameter(arg) && return false
130+
return true
127131
end
128132
return false
129133
end

test/namespacing.jl

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ModelingToolkit
2-
using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing
2+
using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing,
3+
renamespace
34

45
@variables x(t)
56
@parameters p
@@ -24,3 +25,23 @@ nsys = toggle_namespacing(sys, false)
2425

2526
@test_throws ["namespacing", "inner"] System(
2627
Equation[], t; systems = [nsys], name = :a)
28+
29+
@testset "Variables of variables" begin
30+
@variables x(t) y(x)
31+
@named inner = System([D(x) ~ x, y ~ 2x + 1], t)
32+
@test issetequal(unknowns(inner), [x, y])
33+
ss = mtkcompile(inner)
34+
@test isequal(only(unknowns(ss)), x)
35+
@test isequal(only(observed(ss)), y ~ 2x + 1)
36+
37+
@named sys = System(Equation[], t; systems = [inner])
38+
xx, yy = let sys = inner
39+
xx = renamespace(sys, x)
40+
yy = only(@variables y(xx))
41+
xx, renamespace(sys, yy)
42+
end
43+
@test issetequal(unknowns(sys), [xx, yy])
44+
ss = mtkcompile(sys)
45+
@test isequal(only(unknowns(ss)), xx)
46+
@test isequal(only(observed(ss)), yy ~ 2xx + 1)
47+
end

0 commit comments

Comments
 (0)