Skip to content

Commit

Permalink
Merge pull request #2830 from ven-k/vkb/vector-defaults
Browse files Browse the repository at this point in the history
fix: allow vector defaults in `@mtkmodel`
  • Loading branch information
ChrisRackauckas authored Jul 27, 2024
2 parents 737490d + 716a627 commit c91b53e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c91b53e

Please sign in to comment.