diff --git a/docs/src/basics/MTKLanguage.md b/docs/src/basics/MTKLanguage.md index a2fb7d0870..685c549429 100644 --- a/docs/src/basics/MTKLanguage.md +++ b/docs/src/basics/MTKLanguage.md @@ -63,13 +63,14 @@ end @structural_parameters begin f = sin N = 2 + M = 3 end begin v_var = 1.0 end @variables begin v(t) = v_var - v_array(t)[1:2, 1:3] + v_array(t)[1:N, 1:M] v_for_defaults(t) end @extend ModelB(; p1) @@ -301,7 +302,7 @@ end For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/) -## More on `Model.structure` +## [More on `Model.structure`](@id model_structure) `structure` stores metadata that describes composition of a model. It includes: @@ -324,10 +325,10 @@ For example, the structure of `ModelC` is: julia> ModelC.structure Dict{Symbol, Any} with 10 entries: :components => Any[Union{Expr, Symbol}[:model_a, :ModelA], Union{Expr, Symbol}[:model_array_a, :ModelA, :(1:N)], Union{Expr, Symbol}[:model_array_b, :ModelA, :(1:N)]] - :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:type=>Real, :size=>(2, 3)), :v_for_defaults=>Dict(:type=>Real)) + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real)) :icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png") - :kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2), :v=>Dict{Symbol, Any}(:value=>:v_var, :type=>Real), :v_array=>Dict{Symbol, Union{Nothing, UnionAll}}(:value=>nothing, :type=>AbstractArray{Real}), :v_for_defaults=>Dict{Symbol, Union{Nothing, DataType}}(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing)) - :structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2)) + :kwargs => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3), :v => Dict{Symbol, Any}(:value => :v_var, :type => Real), :v_for_defaults => Dict{Symbol, Union{Nothing, DataType}}(:value => nothing, :type => Real), :p1 => Dict(:value => nothing)), + :structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3)) :independent_variable => t :constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant.")) :extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB] @@ -335,6 +336,46 @@ Dict{Symbol, Any} with 10 entries: :equations => Any["model_a.k ~ f(v)"] ``` +### Different ways to define symbolics arrays: + +`@mtkmodel` supports symbolics arrays in both `@parameters` and `@variables`. +Using a structural parameters, symbolic arrays of arbitrary lengths can be defined. +Refer the following example for different ways to define symbolic arrays. + +```@example mtkmodel-example +@mtkmodel ModelWithArrays begin + @structural_parameters begin + N = 2 + M = 3 + end + @parameters begin + p1[1:4] + p2[1:N] + p3[1:N, 1:M] = 10, + [description = "A multi-dimensional array of arbitrary length with description"] + (p4[1:N, 1:M] = 10), + [description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"] + end + @variables begin + v1(t)[1:2] = 10, [description = "An array of variable `v1`"] + v2(t)[1:3] = [1, 2, 3] + end +end +``` + +The size of symbolic array can be accessed via `:size` key, along with other metadata (refer [More on `Model.structure`](@ref model_structure)) +of the symbolic variable. + +```julia +julia> ModelWithArrays.structure +Dict{Symbol, Any} with 5 entries: + :variables => Dict{Symbol, Dict{Symbol, Any}}(:v2 => Dict(:value => :([1, 2, 3]), :type => Real, :size => (3,)), :v1 => Dict(:value => :v1, :type => Real, :description => "An array of variable `v1`", :size => (2,))) + :kwargs => Dict{Symbol, Dict}(:p2 => Dict{Symbol, Any}(:value => nothing, :type => Real, :size => (:N,)), :v1 => Dict{Symbol, Any}(:value => :v1, :type => Real, :description => "An array of variable `v1`", :size => (2,)), :N => Dict(:value => 2), :M => Dict(:value => 3), :p4 => Dict{Symbol, Any}(:value => 10, :type => Real, :description => "An alternate syntax for p3 to match the syntax of vanilla parameters macro", :size => (:N, :M)), :v2 => Dict{Symbol, Any}(:value => :([1, 2, 3]), :type => Real, :size => (3,)), :p1 => Dict{Symbol, Any}(:value => nothing, :type => Real, :size => (4,)), :p3 => Dict{Symbol, Any}(:value => :p3, :type => Real, :description => "A multi-dimensional array of arbitrary length with description", :size => (:N, :M))) + :structural_parameters => Dict{Symbol, Dict}(:N => Dict(:value => 2), :M => Dict(:value => 3)) + :independent_variable => :t + :parameters => Dict{Symbol, Dict{Symbol, Any}}(:p2 => Dict(:value => nothing, :type => Real, :size => (:N,)), :p4 => Dict(:value => 10, :type => Real, :description => "An alternate syntax for p3 to match the syntax of vanilla parameters macro", :size => (:N, :M)), :p1 => Dict(:value => nothing, :type => Real, :size => (4,)), :p3 => Dict(:value => :p3, :type => Real, :description => "A multi-dimensional array of arbitrary length with description", :size => (:N, :M)))), false) +``` + ### Using conditional statements #### Conditional elements of the system diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 7b6705bbd5..1298d72506 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -151,44 +151,26 @@ pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key) struct NoValue end const NO_VALUE = NoValue() -function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, +function update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) - if indices isa Nothing - if !isnothing(meta) && haskey(meta, VariableUnit) - uvar = gensym() - push!(where_types, uvar) - push!(kwargs, - Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE)) - else - push!(kwargs, - Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) - end - dict[:kwargs][getname(var)] = Dict(:value => def, :type => type) + if !isnothing(meta) && haskey(meta, VariableUnit) + uvar = gensym() + push!(where_types, uvar) + push!(kwargs, + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE)) else - vartype = gensym(:T) push!(kwargs, - Expr(:kw, - Expr(:(::), a, - Expr(:curly, :Union, :Nothing, :Missing, NoValue, - Expr(:curly, :AbstractArray, vartype))), - NO_VALUE)) - if !isnothing(meta) && haskey(meta, VariableUnit) - push!(where_types, vartype) - else - push!(where_types, :($vartype <: $type)) - end - dict[:kwargs][getname(var)] = Dict(:value => def, :type => AbstractArray{type}) + Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE)) end + dict[:kwargs][a] = Dict(:value => def, :type => type) if dict[varclass] isa Vector - dict[varclass][1][getname(var)][:type] = AbstractArray{type} + dict[varclass][1][a][:type] = AbstractArray{type} else - dict[varclass][getname(var)][:type] = type + dict[varclass][a][:type] = type end end -function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; - def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, - type::Type = Real, meta = Dict{DataType, Expr}()) +function update_readable_metadata!(varclass_dict, meta::Dict, varname) metatypes = [(:connection_type, VariableConnectType), (:description, VariableDescription), (:unit, VariableUnit), @@ -203,91 +185,263 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; (:tunable, VariableTunable), (:dist, VariableDistribution)] + var_dict = get!(varclass_dict, varname) do + Dict{Symbol, Any}() + end + + for (type, key) in metatypes + if (mt = get(meta, key, nothing)) !== nothing + key == VariableConnectType && (mt = nameof(mt)) + var_dict[type] = mt + end + end +end + +function update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) + dict[varclass] = get!(dict, varclass) do + Dict{Symbol, Dict{Symbol, Any}}() + end + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass]) + + merge!(varclass_dict[], + Dict(varname => Dict( + :size => tuple([index_arg.args[end] for index_arg in indices]...), + :value => varval, + :type => type + ))) + + vartype = gensym(:T) + push!(kwargs, + Expr(:kw, + Expr(:(::), varname, + Expr(:curly, :Union, :Nothing, :Missing, NoValue, + Expr(:curly, :AbstractArray, vartype))), + NO_VALUE)) + if !isnothing(meta) && haskey(meta, VariableUnit) + push!(where_types, vartype) + else + push!(where_types, :($vartype <: $type)) + end + + # Useful keys for kwargs entry are: value, type and size. + dict[:kwargs][varname] = varclass_dict[][varname] + + meta !== nothing && update_readable_metadata!(varclass_dict[], meta, varname) +end + +function unit_handled_variable_value(meta, varname) + varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing + varname + else + :($convert_units($(meta[VariableUnit]), $varname)) + end + return varval +end + +# This function parses various variable/parameter definitions. +# +# The comments indicate the syntax matched by a block; either when parsed directly +# when it is called recursively for parsing a part of an expression. +# These variable definitions are part of test suite in `test/model_parsing.jl` +function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types; + def = nothing, type::Type = Real, meta = Dict{DataType, Expr}()) arg isa LineNumberNode && return MLStyle.@match arg begin + # Parses: `a` + # Recursively called by: `c(t) = cval + jval` + # Recursively called by: `d = 2` + # Recursively called by: `e, [description = "e"]` + # Recursively called by: `f = 3, [description = "f"]` + # Recursively called by: `k = kval, [description = "k"]` + # Recursively called by: `par0::Bool = true` a::Symbol => begin - var = generate_var!(dict, a, varclass; indices, type) - update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, + var = generate_var!(dict, a, varclass; type) + update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end + # Parses: `par5[1:3]::BigFloat` + # Parses: `par6(t)[1:3]::BigFloat` + # Recursively called by: `par2(t)::Int` + # Recursively called by: `par3(t)::BigFloat = 1.0` Expr(:(::), a, type) => begin type = getfield(mod, type) parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) end - Expr(:(::), Expr(:call, a, b), type) => begin - type = getfield(mod, type) - def = _type_check!(def, a, type, varclass) - parse_variable_def!( - dict, mod, a, varclass, kwargs, where_types; def, type, meta) - end + # Recursively called by: `i(t) = 4, [description = "i(t)"]` + # Recursively called by: `h(t), [description = "h(t)"]` + # Recursively called by: `j(t) = jval, [description = "j(t)"]` + # Recursively called by: `par2(t)::Int` + # Recursively called by: `par3(t)::BigFloat = 1.0` Expr(:call, a, b) => begin - var = generate_var!(dict, a, b, varclass, mod; indices, type) - update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var, + var = generate_var!(dict, a, b, varclass, mod; type) + update_kwargs_and_metadata!(dict, kwargs, a, def, type, varclass, where_types, meta) return var, def, Dict() end + # Condition 1 parses: + # `(l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"]` + # `(l2(t)[1:N, 1:M] = 2), [description = "l is more than 1D, with arbitrary length"]` + # `(l3(t)[1:3] = 3), [description = "l2 is 1D"]` + # `(l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"]` + # + # Condition 2 parses: + # `(l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"]` + # `(l6(t)[1:N]::Int = 6), [description = "l3 is 1D and has a type, with arbitrary length"]` + # + # Condition 3 parses: + # `e2[1:2]::Int, [description = "e2"]` + # `h2(t)[1:2]::Int, [description = "h2(t)"]` + # + # Condition 4 parses: + # `e2[1:2], [description = "e2"]` + # `h2(t)[1:2], [description = "h2(t)"]` + Expr(:tuple, Expr(:(=), Expr(:ref, a, indices...), default_val), meta_val) || + Expr(:tuple, Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), default_val), meta_val) || + Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) || + Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin + (@isdefined type) || (type = Real) + varname = Meta.isexpr(a, :call) ? a.args[1] : a + meta = parse_metadata(mod, meta_val) + varval = (@isdefined default_val) ? default_val : + unit_handled_variable_value(meta, varname) + if varclass == :parameters + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval), + $meta_val)) + else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval), + $meta_val)) + end + update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) + (:($varname...), var), nothing, Dict() + end + # Condition 1 parses: + # `par7(t)[1:3, 1:3]::BigFloat = 1.0, [description = "with description"]` + # + # Condition 2 parses: + # `d2[1:2] = 2` + # `l(t)[1:2, 1:3] = 2, [description = "l is more than 1D"]` + Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) || + Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin + (@isdefined type) || (type = Real) + varname = Meta.isexpr(a, :call) ? a.args[1] : a + if Meta.isexpr(def_n_meta, :tuple) + meta = parse_metadata(mod, def_n_meta) + varval = unit_handled_variable_value(meta, varname) + val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end]) + if varclass == :parameters + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $val : $varname; + $varname = $first(@parameters ($a[$(indices...)]::$type = $varval), + $(def_n_meta...))) + else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $val : $varname; + $varname = $first(@variables $a[$(indices...)]::$type = ( + $varval), + $(def_n_meta...))) + end + else + if varclass == :parameters + Meta.isexpr(a, :call) && + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; + $varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + else + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $varname === $NO_VALUE ? $def_n_meta : $varname; + $varname = $first(@variables $a[$(indices...)]::$type = $varname)) + end + varval, meta = def_n_meta, nothing + end + update_array_kwargs_and_metadata!( + dict, indices, kwargs, meta, type, varclass, varname, varval, where_types) + (:($varname...), var), nothing, Dict() + end + # Condition 1 is recursively called by: + # `par5[1:3]::BigFloat` + # `par6(t)[1:3]::BigFloat` + # + # Condition 2 parses: + # `b2(t)[1:2]` + # `a2[1:2]` + Expr(:(::), Expr(:ref, a, indices...), type) || + Expr(:ref, a, indices...) => begin + (@isdefined type) || (type = Real) + varname = a isa Expr && a.head == :call ? a.args[1] : a + if varclass == :parameters + Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname)) + elseif varclass == :variables + Meta.isexpr(a, :call) || + throw("$a is not a variable of the independent variable") + assert_unique_independent_var(dict, a.args[end]) + var = :($varname = $first(@variables $a[$(indices...)]::$type = $varname)) + else + throw("Symbolic array with arbitrary length is not handled for $varclass. + Please open an issue with an example.") + end + update_array_kwargs_and_metadata!( + dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types) + (:($varname...), var), nothing, Dict() + end + # Parses: `c(t) = cval + jval` + # Parses: `d = 2` + # Parses: `f = 3, [description = "f"]` + # Parses: `i(t) = 4, [description = "i(t)"]` + # Parses: `j(t) = jval, [description = "j(t)"]` + # Parses: `k = kval, [description = "k"]` + # Parses: `par0::Bool = true` + # Parses: `par3(t)::BigFloat = 1.0` Expr(:(=), a, b) => begin Base.remove_linenums!(b) def, meta = parse_default(mod, b) var, def, _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; def, type, meta) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][:default] = def - else - dict[varclass][getname(var)][:default] = def - end + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : + Ref(dict[varclass]) + varclass_dict[][getname(var)][:default] = def if meta !== nothing - for (type, key) in metatypes - if (mt = get(meta, key, nothing)) !== nothing - key == VariableConnectType && (mt = nameof(mt)) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][type] = mt - else - dict[varclass][getname(var)][type] = mt - end - end - end + update_readable_metadata!(varclass_dict[], meta, getname(var)) var, metadata_with_exprs = set_var_metadata(var, meta) return var, def, metadata_with_exprs end return var, def, Dict() end + # Parses: `e, [description = "e"]` + # Parses: `h(t), [description = "h(t)"]` + # Parses: `par2(t)::Int` Expr(:tuple, a, b) => begin meta = parse_metadata(mod, b) var, def, _ = parse_variable_def!( dict, mod, a, varclass, kwargs, where_types; type, meta) + varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : + Ref(dict[varclass]) if meta !== nothing - for (type, key) in metatypes - if (mt = get(meta, key, nothing)) !== nothing - key == VariableConnectType && (mt = nameof(mt)) - if dict[varclass] isa Vector - dict[varclass][1][getname(var)][type] = mt - else - dict[varclass][getname(var)][type] = mt - end - end - end + update_readable_metadata!(varclass_dict[], meta, getname(var)) var, metadata_with_exprs = set_var_metadata(var, meta) return var, def, metadata_with_exprs end return var, def, Dict() end - Expr(:ref, a, b...) => begin - indices = map(i -> UnitRange(i.args[2], i.args[end]), b) - parse_variable_def!(dict, mod, a, varclass, kwargs, where_types; - def, indices, type, meta) - end _ => error("$arg cannot be parsed") end end -function generate_var(a, varclass; - indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, - type = Real) - var = indices === nothing ? Symbolics.variable(a; T = type) : - first(@variables $a[indices...]::type) +function generate_var(a, varclass; type = Real) + var = Symbolics.variable(a; T = type) if varclass == :parameters var = toparam(var) elseif varclass == :independent_variables @@ -317,17 +471,25 @@ function generate_var!(dict, a, varclass; vd isa Vector && (vd = first(vd)) vd[a] = Dict{Symbol, Any}() indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices))) - generate_var(a, varclass; indices, type) + generate_var(a, varclass; type) +end + +function assert_unique_independent_var(dict, iv::Num) + assert_unique_independent_var(dict, nameof(iv)) +end +function assert_unique_independent_var(dict, iv) + prev_iv = get!(dict, :independent_variable) do + iv + end + prev_iv isa Num && (prev_iv = nameof(prev_iv)) + @assert isequal(iv, prev_iv) "Multiple independent variables are used in the model $(typeof(iv)) $(typeof(prev_iv))" end function generate_var!(dict, a, b, varclass, mod; indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing, type = Real) iv = b == :t ? get_t(mod, b) : generate_var(b, :independent_variables) - prev_iv = get!(dict, :independent_variable) do - iv - end - @assert isequal(iv, prev_iv) "Multiple independent variables are used in the model" + assert_unique_independent_var(dict, iv) check_name_uniqueness(dict, a, varclass) vd = get!(dict, varclass) do Dict{Symbol, Dict{Symbol, Any}}() @@ -386,14 +548,23 @@ function parse_default(mod, a) end end -function parse_metadata(mod, a) +function parse_metadata(mod, a::Expr) MLStyle.@match a begin - Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles) + Expr(:vect, b...) => Dict(parse_metadata(mod, m) for m in b) + Expr(:tuple, a, b...) => parse_metadata(mod, b) Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b) _ => error("Cannot parse metadata $a") end end +function parse_metadata(mod, metadata::AbstractArray) + ret = Dict() + for m in metadata + merge!(ret, parse_metadata(mod, m)) + end + ret +end + function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr) push!(metadata_with_exprs, m => v) a @@ -655,6 +826,8 @@ function convert_units(varunits::DynamicQuantities.Quantity, value) DynamicQuantities.SymbolicUnits.as_quantity(varunits), value)) end +convert_units(::DynamicQuantities.Quantity, value::NoValue) = NO_VALUE + function convert_units( varunits::DynamicQuantities.Quantity, value::AbstractArray{T}) where {T} DynamicQuantities.ustrip.(DynamicQuantities.uconvert.( @@ -665,62 +838,63 @@ function convert_units(varunits::Unitful.FreeUnits, value) Unitful.ustrip(varunits, value) end +convert_units(::Unitful.FreeUnits, value::NoValue) = NO_VALUE + function convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) where {T} Unitful.ustrip.(varunits, value) end -function convert_units(varunits::Unitful.FreeUnits, value::Num) - value -end +convert_units(::Unitful.FreeUnits, value::Num) = value -function convert_units(varunits::DynamicQuantities.Quantity, value::Num) - value -end +convert_units(::DynamicQuantities.Quantity, value::Num) = value function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types) vv, def, metadata_with_exprs = parse_variable_def!( dict, mod, arg, varclass, kwargs, where_types) - name = getname(vv) - - varexpr = if haskey(metadata_with_exprs, VariableUnit) - unit = metadata_with_exprs[VariableUnit] - quote - $name = if $name === $NO_VALUE - $setdefault($vv, $def) - else - try - $setdefault($vv, $convert_units($unit, $name)) - catch e - if isa(e, $(DynamicQuantities.DimensionError)) || - isa(e, $(Unitful.DimensionError)) - error("Unable to convert units for \'" * string(:($$vv)) * "\'") - elseif isa(e, MethodError) - error("No or invalid units provided for \'" * string(:($$vv)) * - "\'") - else - rethrow(e) + if !(vv isa Tuple) + name = getname(vv) + varexpr = if haskey(metadata_with_exprs, VariableUnit) + unit = metadata_with_exprs[VariableUnit] + quote + $name = if $name === $NO_VALUE + $setdefault($vv, $def) + else + try + $setdefault($vv, $convert_units($unit, $name)) + catch e + if isa(e, $(DynamicQuantities.DimensionError)) || + isa(e, $(Unitful.DimensionError)) + error("Unable to convert units for \'" * string(:($$vv)) * "\'") + elseif isa(e, MethodError) + error("No or invalid units provided for \'" * string(:($$vv)) * + "\'") + else + rethrow(e) + end end end end - end - else - quote - $name = if $name === $NO_VALUE - $setdefault($vv, $def) - else - $setdefault($vv, $name) + else + quote + $name = if $name === $NO_VALUE + $setdefault($vv, $def) + else + $setdefault($vv, $name) + end end end - end - metadata_expr = Expr(:block) - for (k, v) in metadata_with_exprs - push!(metadata_expr.args, - :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) - end + metadata_expr = Expr(:block) + for (k, v) in metadata_with_exprs + push!(metadata_expr.args, + :($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v)))) + end - push!(varexpr.args, metadata_expr) - return vv isa Num ? name : :($name...), varexpr + push!(varexpr.args, metadata_expr) + return vv isa Num ? name : :($name...), varexpr + else + return vv + end end function handle_conditional_vars!( diff --git a/test/model_parsing.jl b/test/model_parsing.jl index f80dc77dc4..3e2bd2045c 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -259,7 +259,8 @@ end @test all(collect(hasmetadata.(model.l, ModelingToolkit.VariableDescription))) @test all(lastindex.([model.a2, model.b2, model.d2, model.e2, model.h2]) .== 2) - @test size(model.l) == MockModel.structure[:parameters][:l][:size] == (2, 3) + @test size(model.l) == (2, 3) + @test MockModel.structure[:parameters][:l][:size] == (2, 3) model = complete(model) @test getdefault(model.cval) == 1 @@ -278,6 +279,38 @@ end @test MockModel.structure[:defaults] == Dict(:n => 1.0, :n2 => "g()") end +@testset "Arrays using vanilla-@variable syntax" begin + @mtkmodel TupleInArrayDef begin + @structural_parameters begin + N + M + end + @parameters begin + (l(t)[1:2, 1:3] = 1), [description = "l is more than 1D"] + (l2(t)[1:N, 1:M] = 2), + [description = "l is more than 1D, with arbitrary length"] + (l3(t)[1:3] = 3), [description = "l2 is 1D"] + (l4(t)[1:N] = 4), [description = "l2 is 1D, with arbitrary length"] + (l5(t)[1:3]::Int = 5), [description = "l3 is 1D and has a type"] + (l6(t)[1:N]::Int = 6), + [description = "l3 is 1D and has a type, with arbitrary length"] + end + end + + N, M = 4, 5 + @named arr = TupleInArrayDef(; N, M) + @test getdefault(arr.l) == 1 + @test getdefault(arr.l2) == 2 + @test getdefault(arr.l3) == 3 + @test getdefault(arr.l4) == 4 + @test getdefault(arr.l5) == 5 + @test getdefault(arr.l6) == 6 + + @test size(arr.l2) == (N, M) + @test size(arr.l4) == (N,) + @test size(arr.l6) == (N,) +end + @testset "Type annotation" begin @mtkmodel TypeModel begin @structural_parameters begin @@ -474,7 +507,8 @@ using ModelingToolkit: getdefault, scalarize @named model_with_component_array = ModelWithComponentArray() - @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"Ω") + @test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == + eval(u"Ω") @test lastindex(parameters(model_with_component_array)) == 3 # Test the constant `k`. Manually k's value should be kept in sync here