diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index bcfe90bab4..44dfb30327 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -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) ``` @@ -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) ``` diff --git a/src/systems/abstractsystem.jl b/src/systems/abstractsystem.jl index 0444877432..dc47457825 100644 --- a/src/systems/abstractsystem.jl +++ b/src/systems/abstractsystem.jl @@ -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 diff --git a/src/variables.jl b/src/variables.jl index 049dac790b..536119f107 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -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) @@ -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)) @@ -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 ====================================================================== """ diff --git a/test/if_lifting.jl b/test/if_lifting.jl index b72b468bf1..9c58e676d0 100644 --- a/test/if_lifting.jl +++ b/test/if_lifting.jl @@ -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