Skip to content

Commit b107633

Browse files
Merge pull request #3371 from AayushSabharwal/as/mtkbuild-passes
Fix `@mtkbuild` macro keyword parsing
2 parents f5ea344 + 9012d9b commit b107633

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

docs/src/basics/Variable_metadata.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
5656
@variables k(t) [connect = Stream]
5757
hasconnect(i)
5858
```
59+
5960
```@example connect
6061
getconnect(k)
6162
```
@@ -197,12 +198,13 @@ state_priority(important_dof)
197198

198199
## Units
199200

200-
Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions.
201+
Units for variables can be designated using symbolic metadata. For more information, please see the [model validation and units](@ref units) section of the docs. Note that `getunit` is not equivalent to `get_unit` - the former is a metadata getter for individual variables (and is provided so the same interface function for `unit` exists like other metadata), while the latter is used to handle more general symbolic expressions.
201202

202203
```@example metadata
203-
@variable speed [unit=u"m/s"]
204+
@variable speed [unit = u"m/s"]
204205
hasunit(speed)
205206
```
207+
206208
```@example metadata
207209
getunit(speed)
208210
```

src/systems/abstractsystem.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2248,15 +2248,20 @@ macro mtkbuild(exprs...)
22482248
expr = exprs[1]
22492249
named_expr = ModelingToolkit.named_expr(expr)
22502250
name = named_expr.args[1]
2251-
kwargs = if length(exprs) > 1
2252-
NamedTuple{Tuple(ex.args[1] for ex in Base.tail(exprs))}(Tuple(ex.args[2]
2253-
for ex in Base.tail(exprs)))
2251+
kwargs = Base.tail(exprs)
2252+
kwargs = map(kwargs) do ex
2253+
@assert ex.head == :(=)
2254+
Expr(:kw, ex.args[1], ex.args[2])
2255+
end
2256+
if isempty(kwargs)
2257+
kwargs = ()
22542258
else
2255-
(;)
2259+
kwargs = (Expr(:parameters, kwargs...),)
22562260
end
2261+
call_expr = Expr(:call, structural_simplify, kwargs..., name)
22572262
esc(quote
22582263
$named_expr
2259-
$name = $structural_simplify($name; $(kwargs)...)
2264+
$name = $call_expr
22602265
end)
22612266
end
22622267

src/variables.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ ModelingToolkit.dump_variable_metadata(p)
2828
"""
2929
function dump_variable_metadata(var)
3030
uvar = unwrap(var)
31-
variable_source, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown))
31+
variable_source, name = Symbolics.getmetadata(
32+
uvar, VariableSource, (:unknown, :unknown))
3233
type = symtype(uvar)
3334
if type <: AbstractArray
3435
shape = Symbolics.shape(var)
@@ -102,7 +103,9 @@ getconnect(x::Symbolic) = Symbolics.getmetadata(x, VariableConnectType, nothing)
102103
Determine whether variable `x` has a connect type. See also [`getconnect`](@ref).
103104
"""
104105
hasconnect(x) = getconnect(x) !== nothing
105-
setconnect(x, t::Type{T}) where T <: AbstractConnectType = setmetadata(x, VariableConnectType, t)
106+
function setconnect(x, t::Type{T}) where {T <: AbstractConnectType}
107+
setmetadata(x, VariableConnectType, t)
108+
end
106109

107110
### Input, Output, Irreducible
108111
isvarkind(m, x::Union{Num, Symbolics.Arr}) = isvarkind(m, value(x))
@@ -581,7 +584,7 @@ metadata associated with it.
581584
See also [`getmisc(x)`](@ref).
582585
"""
583586
hasmisc(x) = getmisc(x) !== nothing
584-
setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata)
587+
setmisc(x, miscdata) = setmetadata(x, VariableMisc, miscdata)
585588

586589
## Units ======================================================================
587590
"""

test/if_lifting.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,17 @@ end
110110
@test operation(eq.rhs) === ifelse
111111
end
112112
end
113+
114+
@testset "`@mtkbuild` macro accepts `additional_passes`" begin
115+
@mtkmodel SimpleAbs begin
116+
@variables begin
117+
x(t)
118+
y(t)
119+
end
120+
@equations begin
121+
D(x) ~ abs(y)
122+
y ~ sin(t)
123+
end
124+
end
125+
@test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting]
126+
end

0 commit comments

Comments
 (0)