Skip to content

Commit 86d345f

Browse files
committed
docs: add component array in the docs
1 parent d47af77 commit 86d345f

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ end
5858
end
5959
@structural_parameters begin
6060
f = sin
61+
N = 2
6162
end
6263
begin
6364
v_var = 1.0
@@ -69,6 +70,11 @@ end
6970
@extend ModelB(; p1)
7071
@components begin
7172
model_a = ModelA(; k_array)
73+
model_array_a = [ModelA(; k = i) for i in 1:N]
74+
model_array_b = for i in 1:N
75+
k = i^2
76+
ModelA(; k)
77+
end
7278
end
7379
@equations begin
7480
model_a.k ~ f(v)
@@ -146,6 +152,7 @@ julia> @mtkbuild model_c2 = ModelC(; p1 = 2.0)
146152
#### `@components` begin block
147153

148154
- Declare the subcomponents within `@components` begin block.
155+
- Array of components can be declared with a for loop or a list comprehension.
149156
- The arguments in these subcomponents are promoted as keyword arguments as `subcomponent_name__argname` with `nothing` as default value.
150157
- Whenever components are created with `@named` macro, these can be accessed with `.` operator as `subcomponent_name.argname`
151158
- In the above example, as `k` of `model_a` isn't listed while defining the sub-component in `ModelC`, its default value can't be modified by users. While `k_array` can be set as:
@@ -223,7 +230,7 @@ end
223230
```
224231

225232
!!! note
226-
233+
227234
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
228235

229236
## More on `Model.structure`
@@ -247,14 +254,13 @@ For example, the structure of `ModelC` is:
247254
```julia
248255
julia> ModelC.structure
249256
Dict{Symbol, Any} with 9 entries:
250-
:components => Any[Union{Expr, Symbol}[:model_a, :ModelA]]
257+
: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)]]
251258
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var, :type=>Real), :v_array=>Dict(:type=>Real, :size=>(2, 3)))
252259
:icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png")
253-
:constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant."))
254-
:kwargs => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :v=>Dict{Symbol, Union{Nothing, Symbol}}(:value=>:v_var, :type=>Real), :v_array=>Dict(:value=>nothing, :type=>Real), :p1=>Dict(:value=>nothing))
255-
:structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin))
260+
: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}), :p1=>Dict(:value=>nothing))
261+
:structural_parameters => Dict{Symbol, Dict}(:f=>Dict(:value=>:sin), :N=>Dict(:value=>2))
256262
:independent_variable => t
257-
:constants => Dict{Symbol, Dict}(:c=>Dict(:value=>1))
263+
:constants => Dict{Symbol, Dict}(:c=>Dict{Symbol, Any}(:value=>1, :type=>Int64, :description=>"Example constant."))
258264
:extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB]
259265
:equations => Any["model_a.k ~ f(v)"]
260266
```

0 commit comments

Comments
 (0)