Skip to content

Commit 535d1f4

Browse files
Merge pull request #3107 from ven-k/vkb/array-length-as-input
Support Symbolic Arrays of arbitrary length
2 parents 7c90426 + 4c0b1ed commit 535d1f4

File tree

3 files changed

+377
-128
lines changed

3 files changed

+377
-128
lines changed

docs/src/basics/MTKLanguage.md

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ end
6363
@structural_parameters begin
6464
f = sin
6565
N = 2
66+
M = 3
6667
end
6768
begin
6869
v_var = 1.0
6970
end
7071
@variables begin
7172
v(t) = v_var
72-
v_array(t)[1:2, 1:3]
73+
v_array(t)[1:N, 1:M]
7374
v_for_defaults(t)
7475
end
7576
@extend ModelB(; p1)
@@ -301,7 +302,7 @@ end
301302

302303
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
303304

304-
## More on `Model.structure`
305+
## [More on `Model.structure`](@id model_structure)
305306

306307
`structure` stores metadata that describes composition of a model. It includes:
307308

@@ -324,17 +325,57 @@ For example, the structure of `ModelC` is:
324325
julia> ModelC.structure
325326
Dict{Symbol, Any} with 10 entries:
326327
: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)]]
327-
: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))
328+
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_for_defaults=>Dict(:type=>Real))
328329
:icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png")
329-
: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))
330-
:structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2))
330+
: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)),
331+
:structural_parameters => Dict{Symbol, Dict}(:f => Dict(:value => :sin), :N => Dict(:value => 2), :M => Dict(:value => 3))
331332
:independent_variable => t
332333
:constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant."))
333334
:extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB]
334335
:defaults => Dict{Symbol, Any}(:v_for_defaults=>2.0)
335336
:equations => Any["model_a.k ~ f(v)"]
336337
```
337338

339+
### Different ways to define symbolics arrays:
340+
341+
`@mtkmodel` supports symbolics arrays in both `@parameters` and `@variables`.
342+
Using a structural parameters, symbolic arrays of arbitrary lengths can be defined.
343+
Refer the following example for different ways to define symbolic arrays.
344+
345+
```@example mtkmodel-example
346+
@mtkmodel ModelWithArrays begin
347+
@structural_parameters begin
348+
N = 2
349+
M = 3
350+
end
351+
@parameters begin
352+
p1[1:4]
353+
p2[1:N]
354+
p3[1:N, 1:M] = 10,
355+
[description = "A multi-dimensional array of arbitrary length with description"]
356+
(p4[1:N, 1:M] = 10),
357+
[description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"]
358+
end
359+
@variables begin
360+
v1(t)[1:2] = 10, [description = "An array of variable `v1`"]
361+
v2(t)[1:3] = [1, 2, 3]
362+
end
363+
end
364+
```
365+
366+
The size of symbolic array can be accessed via `:size` key, along with other metadata (refer [More on `Model.structure`](@ref model_structure))
367+
of the symbolic variable.
368+
369+
```julia
370+
julia> ModelWithArrays.structure
371+
Dict{Symbol, Any} with 5 entries:
372+
: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,)))
373+
: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)))
374+
:structural_parameters => Dict{Symbol, Dict}(:N => Dict(:value => 2), :M => Dict(:value => 3))
375+
:independent_variable => :t
376+
: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)
377+
```
378+
338379
### Using conditional statements
339380

340381
#### Conditional elements of the system

0 commit comments

Comments
 (0)