Skip to content

Commit

Permalink
Merge pull request #2330 from thazhemadam/at/fix-model-parsing-mtkmod…
Browse files Browse the repository at this point in the history
…el-from-libs

feat: support components with fully-qualified names with `@mtkmodel`
  • Loading branch information
ChrisRackauckas authored Dec 7, 2023
2 parents 493e695 + d0b2e33 commit ce7eed0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
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 @@ function _parse_components!(exprs, body, kwargs)
expr = Expr(:block)
varexpr = Expr(:block)
# push!(exprs, varexpr)
comps = Vector{Symbol}[]
comps = Vector{Union{Symbol, Expr}}[]
comp_names = []

for arg in body.args
Expand All @@ -692,7 +692,9 @@ function _parse_components!(exprs, body, kwargs)
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]])
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

0 comments on commit ce7eed0

Please sign in to comment.