From a4ba071e2a93583ed7984a849914a4fcdba2d1ff Mon Sep 17 00:00:00 2001 From: cstjean Date: Tue, 20 May 2025 10:49:00 +0900 Subject: [PATCH 1/4] Fix @mtkmodel to work without `using ModelingToolkit` See https://github.com/SciML/ModelingToolkit.jl/issues/3640 Feels like `const MTK = ModelingToolkit` should be at the top-level for convenience. --- src/systems/model_parsing.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 024c249363..85a7cf354b 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -42,8 +42,9 @@ function flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}}) end function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) + MTK = ModelingToolkit if fullname isa Symbol - name, type = fullname, :System + name, type = fullname, :($MTK.System) else if fullname.head == :(::) name, type = fullname.args @@ -74,9 +75,9 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector) push!(exprs.args, :(parameters = [])) # We build `System` by default; vectors can't be created for `System` as it is # a function. - push!(exprs.args, :(systems = ModelingToolkit.AbstractSystem[])) - push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[])) - push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}())) + push!(exprs.args, :(systems = $MTK.AbstractSystem[])) + push!(exprs.args, :(equations = Union{$MTK.Equation, Vector{$MTK.Equation}}[])) + push!(exprs.args, :(defaults = Dict{$MTK.Num, Union{Number, Symbol, Function}}())) Base.remove_linenums!(expr) for arg in expr.args From 23db74ee509033322e2739c8e96894076e9703db Mon Sep 17 00:00:00 2001 From: cstjean Date: Tue, 20 May 2025 11:05:18 +0900 Subject: [PATCH 2/4] Write test for #3640 --- test/model_parsing.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index fe2bcbfca6..7d1d661139 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1058,3 +1058,24 @@ end @test isequal(constrs[2], -4 + ex.y ≲ 0) @test ModelingToolkit.get_consolidate(ex)([1, 2]) ≈ 1 + log(2) end + +module NoUsingMTK # From #3640 +using ModelingToolkit: @mtkmodel, @variables, @parameters, t_nounits as t + +@mtkmodel MyModel begin + @variables begin + x(t) + y(t) + end + @parameters begin + a + end + @equations begin + y ~ a * x + end +end +end + +@testset "Model can be defined even without `using MTK` (#3640)" begin + @test NoUsingMTK.MyModel(; name=:foo) isa ModelingToolkit.AbstractSystem # just test that it ran fine +end From 52de720cd7b36d68fa03b441f6dac47249198859 Mon Sep 17 00:00:00 2001 From: cstjean Date: Tue, 20 May 2025 11:17:49 +0900 Subject: [PATCH 3/4] Respect 92 char line length limit --- test/model_parsing.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 7d1d661139..01c850110d 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1077,5 +1077,6 @@ end end @testset "Model can be defined even without `using MTK` (#3640)" begin - @test NoUsingMTK.MyModel(; name=:foo) isa ModelingToolkit.AbstractSystem # just test that it ran fine + # Just test that it runs without error + @test NoUsingMTK.MyModel(; name=:foo) isa ModelingToolkit.AbstractSystem end From 321836b5c16871c509ede3083dcbb91978784986 Mon Sep 17 00:00:00 2001 From: cstjean Date: Tue, 20 May 2025 11:20:33 +0900 Subject: [PATCH 4/4] Run JuliaFormatter --- test/model_parsing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/model_parsing.jl b/test/model_parsing.jl index 01c850110d..91b122f87c 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -1078,5 +1078,5 @@ end @testset "Model can be defined even without `using MTK` (#3640)" begin # Just test that it runs without error - @test NoUsingMTK.MyModel(; name=:foo) isa ModelingToolkit.AbstractSystem + @test NoUsingMTK.MyModel(; name = :foo) isa ModelingToolkit.AbstractSystem end