Skip to content

fix: fix array variables in mtkmodel #3063

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ import Symbolics: rename, get_variables!, _solve, hessian_sparsity,
ParallelForm, SerialForm, MultithreadedForm, build_function,
rhss, lhss, prettify_expr, gradient,
jacobian, hessian, derivative, sparsejacobian, sparsehessian,
substituter, scalarize, getparent, hasderiv, hasdiff
substituter, scalarize, getparent, hasderiv, hasdiff, Arr

import DiffEqBase: @add_kwonly
export independent_variables, unknowns, parameters, full_parameters, continuous_events,
1 change: 1 addition & 0 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
@@ -1112,6 +1112,7 @@ end

apply_to_variables(f::F, ex) where {F} = _apply_to_variables(f, ex)
apply_to_variables(f::F, ex::Num) where {F} = wrap(_apply_to_variables(f, unwrap(ex)))
apply_to_variables(f::F, ex::Arr) where {F} = wrap(_apply_to_variables(f, unwrap(ex)))
function _apply_to_variables(f::F, ex) where {F}
if isvariable(ex)
return f(ex)
2 changes: 1 addition & 1 deletion src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -714,7 +714,7 @@ function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
end

push!(varexpr.args, metadata_expr)
return vv isa Num ? name : :($name...), varexpr
return name, varexpr
end

function handle_conditional_vars!(
28 changes: 26 additions & 2 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ using ModelingToolkit: get_connector_type, get_defaults, get_gui_metadata,
get_systems, get_ps, getdefault, getname, readable_code,
scalarize, symtype, VariableDescription, RegularConnector,
get_unit
using SymbolicIndexingInterface
using URIs: URI
using Distributions
using DynamicQuantities, OrdinaryDiffEq
@@ -245,7 +246,7 @@ end
kval = 5
@named model = MockModel(; b2 = [1, 3], kval, cval = 1, func = identity)

@test lastindex(parameters(model)) == 31
@test lastindex(parameters(model)) == 21

@test all(getdescription.([model.e2...]) .== "e2")
@test all(getdescription.([model.h2...]) .== "h2(t)")
@@ -475,7 +476,7 @@ using ModelingToolkit: getdefault, scalarize
@named model_with_component_array = ModelWithComponentArray()

@test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"")
@test lastindex(parameters(model_with_component_array)) == 3
@test lastindex(parameters(model_with_component_array)) == 1

# Test the constant `k`. Manually k's value should be kept in sync here
# and the ModelParsingPrecompile.
@@ -876,3 +877,26 @@ end
end),
false)
end

@testset "Array parameters and subcomponent defaults" begin
@mtkmodel Foo begin
@parameters begin
p[1:3]
end
end
@mtkmodel Bar begin
@parameters begin
q[1:3]
end
@components begin
foo = Foo(p = q)
end
end
@named model = Bar()
model = complete(model)
# non-scalarized versions are parameters
@test is_parameter(model, model.q)
@test is_parameter(model, model.foo.p)
# default is correctly (not) namespaced
@test isequal(ModelingToolkit.defaults(model)[model.foo.p], model.q)
end