Skip to content

Commit d2999d3

Browse files
committed
docs: add a dedicated section to showcase symbolic-array definition
With many ways to define, this feature demands a dedicated section.
1 parent 61dbb19 commit d2999d3

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/src/basics/MTKLanguage.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ end
302302

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

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

307307
`structure` stores metadata that describes composition of a model. It includes:
308308

@@ -336,6 +336,44 @@ Dict{Symbol, Any} with 10 entries:
336336
:equations => Any["model_a.k ~ f(v)"]
337337
```
338338

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_arrays
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, [description = "A multi-dimensional array of arbitrary length with description"]
355+
(p4[1:N, 1:M] = 10), [description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"]
356+
end
357+
@variables begin
358+
v1(t)[1:2] = 10, [description = "An array of variable `v1`"]
359+
v2(t)[1:3] = [1, 2, 3]
360+
end
361+
end
362+
```
363+
364+
The size of symbolic array can be accessed via `:size` key, along with other metadata (refer [More on `Model.structure`](@ref model_structure))
365+
of the symbolic variable.
366+
367+
```julia
368+
julia> ModelWithArrays.structure
369+
Dict{Symbol, Any} with 5 entries:
370+
: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,)))
371+
: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)))
372+
:structural_parameters => Dict{Symbol, Dict}(:N => Dict(:value => 2), :M => Dict(:value => 3))
373+
:independent_variable => :t
374+
: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)
375+
```
376+
339377
### Using conditional statements
340378

341379
#### Conditional elements of the system

0 commit comments

Comments
 (0)