Skip to content

Fix @mtkbuild macro keyword parsing #3371

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 2 commits into from
Feb 5, 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
6 changes: 4 additions & 2 deletions docs/src/basics/Variable_metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
@variables k(t) [connect = Stream]
hasconnect(i)
```

```@example connect
getconnect(k)
```
Expand Down Expand Up @@ -197,12 +198,13 @@ state_priority(important_dof)

## Units

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.
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.

```@example metadata
@variable speed [unit=u"m/s"]
@variable speed [unit = u"m/s"]
hasunit(speed)
```

```@example metadata
getunit(speed)
```
Expand Down
15 changes: 10 additions & 5 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2248,15 +2248,20 @@ macro mtkbuild(exprs...)
expr = exprs[1]
named_expr = ModelingToolkit.named_expr(expr)
name = named_expr.args[1]
kwargs = if length(exprs) > 1
NamedTuple{Tuple(ex.args[1] for ex in Base.tail(exprs))}(Tuple(ex.args[2]
for ex in Base.tail(exprs)))
kwargs = Base.tail(exprs)
kwargs = map(kwargs) do ex
@assert ex.head == :(=)
Expr(:kw, ex.args[1], ex.args[2])
end
if isempty(kwargs)
kwargs = ()
else
(;)
kwargs = (Expr(:parameters, kwargs...),)
end
call_expr = Expr(:call, structural_simplify, kwargs..., name)
esc(quote
$named_expr
$name = $structural_simplify($name; $(kwargs)...)
$name = $call_expr
end)
end

Expand Down
9 changes: 6 additions & 3 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ ModelingToolkit.dump_variable_metadata(p)
"""
function dump_variable_metadata(var)
uvar = unwrap(var)
variable_source, name = Symbolics.getmetadata(uvar, VariableSource, (:unknown, :unknown))
variable_source, name = Symbolics.getmetadata(
uvar, VariableSource, (:unknown, :unknown))
type = symtype(uvar)
if type <: AbstractArray
shape = Symbolics.shape(var)
Expand Down Expand Up @@ -102,7 +103,9 @@ getconnect(x::Symbolic) = Symbolics.getmetadata(x, VariableConnectType, nothing)
Determine whether variable `x` has a connect type. See also [`getconnect`](@ref).
"""
hasconnect(x) = getconnect(x) !== nothing
setconnect(x, t::Type{T}) where T <: AbstractConnectType = setmetadata(x, VariableConnectType, t)
function setconnect(x, t::Type{T}) where {T <: AbstractConnectType}
setmetadata(x, VariableConnectType, t)
end

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

## Units ======================================================================
"""
Expand Down
14 changes: 14 additions & 0 deletions test/if_lifting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,17 @@ end
@test operation(eq.rhs) === ifelse
end
end

@testset "`@mtkbuild` macro accepts `additional_passes`" begin
@mtkmodel SimpleAbs begin
@variables begin
x(t)
y(t)
end
@equations begin
D(x) ~ abs(y)
y ~ sin(t)
end
end
@test_nowarn @mtkbuild sys=SimpleAbs() additional_passes=[IfLifting]
end
Loading