From 716a627399f934b156b2b8d6fa4718f1c9152a0f Mon Sep 17 00:00:00 2001 From: Venakteshprasad <32921645+ven-k@users.noreply.github.com> Date: Sat, 29 Jun 2024 13:01:07 +0530 Subject: [PATCH] fix: allow vector defaults in `@mtkmodel` --- src/systems/model_parsing.jl | 3 +++ test/model_parsing.jl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index d79282ae27..b10b7125a2 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -340,6 +340,9 @@ function parse_default(mod, a) (expr, nothing) end Expr(:if, condition, x, y) => (a, nothing) + Expr(:vect, x...) => begin + (a, nothing) + end _ => error("Cannot parse default $a $(typeof(a))") end end diff --git a/test/model_parsing.jl b/test/model_parsing.jl index d8665190c8..f1bd7a7cea 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -817,3 +817,38 @@ end @test defs[ordermodel.c] == 2 @test defs[ordermodel.d] == 1 end + +@testset "Vector defaults" begin + @mtkmodel VectorDefaultWithMetadata begin + @parameters begin + n[1:3] = [1, 2, 3], [description = "Vector defaults"] + end + end + + @named vec = VectorDefaultWithMetadata() + for i in 1:3 + @test getdefault(vec.n[i]) == i + end + + @mtkmodel VectorConditionalDefault begin + @structural_parameters begin + flag = true + end + @parameters begin + n[1:3] = if flag + [2, 2, 2] + else + 1 + end + end + end + + @named vec_true = VectorConditionalDefault() + for i in 1:3 + @test getdefault(vec_true.n[i]) == 2 + end + @named vec_false = VectorConditionalDefault(flag = false) + for i in 1:3 + @test getdefault(vec_false.n[i]) == 1 + end +end