Skip to content

Commit 84bf015

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 d7d82bf commit 84bf015

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

docs/src/basics/MTKLanguage.md

Lines changed: 41 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,46 @@ 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-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+
339379
### Using conditional statements
340380

341381
#### Conditional elements of the system

0 commit comments

Comments
 (0)