63
63
@structural_parameters begin
64
64
f = sin
65
65
N = 2
66
+ M = 3
66
67
end
67
68
begin
68
69
v_var = 1.0
69
70
end
70
71
@variables begin
71
72
v(t) = v_var
72
- v_array(t)[1:2 , 1:3 ]
73
+ v_array(t)[1:N , 1:M ]
73
74
v_for_defaults(t)
74
75
end
75
76
@extend ModelB(; p1)
301
302
302
303
For more examples of usage, checkout [ ModelingToolkitStandardLibrary.jl] ( https://github.com/SciML/ModelingToolkitStandardLibrary.jl/ )
303
304
304
- ## More on ` Model.structure `
305
+ ## [ More on ` Model.structure ` ] ( @ id model_structure)
305
306
306
307
` structure ` stores metadata that describes composition of a model. It includes:
307
308
@@ -324,17 +325,57 @@ For example, the structure of `ModelC` is:
324
325
julia> ModelC. structure
325
326
Dict{Symbol, Any} with 10 entries:
326
327
: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))
328
329
: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 ))
331
332
:independent_variable => t
332
333
:constants => Dict {Symbol, Dict} (:c => Dict {Symbol, Any} (:value => 1 , :type => Int64, :description => " Example constant." ))
333
334
:extend => Any[[:p2 , :p1 ], Symbol (" #mtkmodel__anonymous__ModelB" ), :ModelB ]
334
335
:defaults => Dict {Symbol, Any} (:v_for_defaults => 2.0 )
335
336
:equations => Any[" model_a.k ~ f(v)" ]
336
337
```
337
338
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
+
338
379
### Using conditional statements
339
380
340
381
#### Conditional elements of the system
0 commit comments