Skip to content

fix: fix namespacing of variables of variables #3711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/independent_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ Define one or more independent variables. For example:
@variables x(t)
"""
macro independent_variables(ts...)
:(@parameters $(ts...)) |> esc # TODO: treat independent variables separately from variables and parameters
Symbolics._parse_vars(:independent_variables,
Real,
ts,
toiv) |> esc
end

toiv(s::Symbolic) = setmetadata(s, MTKVariableTypeCtx, PARAMETER)
toiv(s::Symbolic) = GlobalScope(setmetadata(s, MTKVariableTypeCtx, PARAMETER))
toiv(s::Symbolics.Arr) = wrap(toiv(value(s)))
toiv(s::Num) = Num(toiv(value(s)))
8 changes: 5 additions & 3 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,9 @@ function is_array_of_symbolics(x)
end

function namespace_expr(
O, sys, n = nameof(sys); ivs = independent_variables(sys))
O, sys, n = (sys === nothing ? nothing : nameof(sys));
ivs = sys === nothing ? nothing : independent_variables(sys))
sys === nothing && return O
O = unwrap(O)
# Exceptions for arrays of symbolic and Ref of a symbolic, the latter
# of which shows up in broadcasts
Expand Down Expand Up @@ -1538,9 +1540,9 @@ function defaults_and_guesses(sys::AbstractSystem)
merge(guesses(sys), defaults(sys))
end

unknowns(sys::Union{AbstractSystem, Nothing}, v) = renamespace(sys, v)
unknowns(sys::Union{AbstractSystem, Nothing}, v) = namespace_expr(v, sys)
for vType in [Symbolics.Arr, Symbolics.Symbolic{<:AbstractArray}]
@eval unknowns(sys::AbstractSystem, v::$vType) = renamespace(sys, v)
@eval unknowns(sys::AbstractSystem, v::$vType) = namespace_expr(v, sys)
@eval parameters(sys::AbstractSystem, v::$vType) = toparam(unknowns(sys, v))
end
parameters(sys::Union{AbstractSystem, Nothing}, v) = toparam(unknowns(sys, v))
Expand Down
6 changes: 5 additions & 1 deletion src/systems/codegen_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ function isdelay(var, iv)
if iscall(var) && !ModelingToolkit.isoperator(var, Symbolics.Operator)
args = arguments(var)
length(args) == 1 || return false
isequal(args[1], iv) || return true
arg = args[1]
isequal(arg, iv) && return false
iscall(arg) || return true
issym(operation(arg)) && !iscalledparameter(arg) && return false
return true
end
return false
end
Expand Down
23 changes: 22 additions & 1 deletion test/namespacing.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing
using ModelingToolkit: t_nounits as t, D_nounits as D, iscomplete, does_namespacing,
renamespace

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

@test_throws ["namespacing", "inner"] System(
Equation[], t; systems = [nsys], name = :a)

@testset "Variables of variables" begin
@variables x(t) y(x)
@named inner = System([D(x) ~ x, y ~ 2x + 1], t)
@test issetequal(unknowns(inner), [x, y])
ss = mtkcompile(inner)
@test isequal(only(unknowns(ss)), x)
@test isequal(only(observed(ss)), y ~ 2x + 1)

@named sys = System(Equation[], t; systems = [inner])
xx, yy = let sys = inner
xx = renamespace(sys, x)
yy = only(@variables y(xx))
xx, renamespace(sys, yy)
end
@test issetequal(unknowns(sys), [xx, yy])
ss = mtkcompile(sys)
@test isequal(only(unknowns(ss)), xx)
@test isequal(only(observed(ss)), yy ~ 2xx + 1)
end
Loading