Skip to content
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

feat: support components with fully-qualified names with @mtkmodel #2330

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
6 changes: 4 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@
expr = Expr(:block)
varexpr = Expr(:block)
# push!(exprs, varexpr)
comps = Vector{Symbol}[]
comps = Vector{Union{Symbol, Expr}}[]

Check warning on line 676 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L676

Added line #L676 was not covered by tests
comp_names = []

for arg in body.args
Expand All @@ -692,7 +692,9 @@
arg.args[2] = b
push!(expr.args, arg)
push!(comp_names, a)
push!(comps, [a, b.args[1]])
if (isa(b.args[1], Symbol) || Meta.isexpr(b.args[1], :.))
push!(comps, [a, b.args[1]])

Check warning on line 696 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L695-L696

Added lines #L695 - L696 were not covered by tests
end
end
_ => error("Couldn't parse the component body: $arg")
end
Expand Down
42 changes: 25 additions & 17 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ using Unitful

ENV["MTK_ICONS_DIR"] = "$(@__DIR__)/icons"

# Mock module used to test if the `@mtkmodel` macro works with fully-qualified names as well.
module MyMockModule
using ..ModelingToolkit, ..Unitful

export Pin
@connector Pin begin
v(t), [unit = u"V"] # Potential at the pin [V]
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
@icon "pin.png"
end

@mtkmodel Ground begin
@components begin
g = Pin()
end
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
@equations begin
g.v ~ 0
end
end
end

using .MyMockModule

@connector RealInput begin
u(t), [input = true, unit = u"V"]
end
Expand All @@ -28,12 +52,6 @@ end
@variables t [unit = u"s"]
D = Differential(t)

@connector Pin begin
v(t), [unit = u"V"] # Potential at the pin [V]
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
@icon "pin.png"
end

@named p = Pin(; v = π)
@test getdefault(p.v) == π
@test Pin.isconnector == true
Expand All @@ -57,16 +75,6 @@ end

@test OnePort.isconnector == false

@mtkmodel Ground begin
@components begin
g = Pin()
end
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
@equations begin
g.v ~ 0
end
end

resistor_log = "$(@__DIR__)/logo/resistor.svg"
@mtkmodel Resistor begin
@extend v, i = oneport = OnePort()
Expand Down Expand Up @@ -127,7 +135,7 @@ end
capacitor = Capacitor(; C = C_val)
source = Voltage()
constant = Constant(; k = k_val)
ground = Ground()
ground = MyMockModule.Ground()
end

@equations begin
Expand Down
Loading