Skip to content

Commit

Permalink
docs: demonstrate using symbolic arrays in @mtkmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Oct 11, 2023
1 parent a76b185 commit 491314f
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions docs/src/basics/MTKModel_Connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ using ModelingToolkit
@mtkmodel ModelA begin
@parameters begin
k1
k2
k
k_array[1:2]
end
end
Expand All @@ -61,10 +61,10 @@ end
end
@extend ModelB(; p1)
@components begin
model_a = ModelA(; k1)
model_a = ModelA(; k_array)
end
@equations begin
model_a.k1 ~ f(v)
model_a.k ~ f(v)
end
end
```
Expand All @@ -78,9 +78,9 @@ end
- Whenever a parameter or variable has initial value, for example `v(t) = 0.0`, a symbolic variable named `v` with initial value 0.0 and a keyword argument `v`, with default value `nothing` are created. <br> This way, users can optionally pass new value of `v` while creating a component.

```julia
julia > @named model_c = ModelC(; v = 2.0);
julia> @mtkbuild model_c1 = ModelC(; v = 2.0);

julia > ModelingToolkit.getdefault(model_c.v)
julia> ModelingToolkit.getdefault(model_c.v)
2.0
```

Expand All @@ -96,7 +96,7 @@ julia > ModelingToolkit.getdefault(model_c.v)
- Note that in above example, `p1` is promoted as an argument of `ModelC`. Users can set the value of `p1`. However, as `p2` isn't listed in the model definition, its initial guess can't be specified while creating an instance of `ModelC`.

```julia
julia> @named model_c = ModelC(; p1 = 2.0)
julia> @mtkbuild model_c2 = ModelC(; p1 = 2.0)

```

Expand All @@ -105,14 +105,25 @@ julia> @named model_c = ModelC(; p1 = 2.0)
- Declare the subcomponents within `@components` begin block.
- The arguments in these subcomponents are promoted as keyword arguments as `subcomponent_name__argname` with `nothing` as default value.
- Whenever components are created with `@named` macro, these can be accessed with `.` operator as `subcomponent_name.argname`
- In the above example, `k1` of `model_a` can be set in following ways:
- 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:

```julia
julia> @named model_c1 = ModelC(; model_a.k1 = 1);
```@example mtkmodel-example
using ModelingToolkit: getdefault
```
@mtkbuild model_c3 = ModelC(; model_a.k_array = [1.0, 2.0])
And as `k2` isn't listed in the sub-component definition of `ModelC`, its default value can't be modified by users.
getdefault(model_c3.model_a.k_array[1])
# 1.0
getdefault(model_c3.model_a.k_array[2])
# 2.0
@mtkbuild model_c4 = ModelC(model_a.k_array = 3.0)
getdefault(model_c4.model_a.k_array[1])
# 3.0
getdefault(model_c4.model_a.k_array[2])
# 3.0
```

#### `@equations` begin block

Expand Down

0 comments on commit 491314f

Please sign in to comment.