Skip to content

Commit

Permalink
docs: update MTKModel docs about the conditional statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Oct 30, 2023
1 parent 6519ef7 commit 2c1cdf8
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions docs/src/basics/MTKModel_Connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,83 @@ Dict{Symbol, Any} with 6 entries:
:extend => Any[[:p1, :p2], :model_b, :ModelB]
:equations => ["model_a.k1 ~ f(v)"]
```

### Using conditional statements

Both `@mtkmodel` and `components` support conditionally defining parameters,
variables, equations, and components.

The if-elseif-else statements can be used inside `@equations`, `@parameters`,
`@variables`, `@components`.

```@example branches-inside-macros
@mtkmodel C begin end
@mtkmodel BranchInsideTheBlock begin
@structural_parameters begin
flag = true
end
@parameters begin
if flag
a1
else
a2
end
end
@components begin
if flag
sys1 = C()
else
sys2 = C()
end
end
end
```

Alternatively, the `@equations`, `@parameters`, `@variables`, `@components` can be
used inside the if-elseif-else statements.

```@example branches-outside-macros
@mtkmodel BranchOutsideTheBlock begin
@structural_parameters begin
flag = true
end
if flag
@parameters begin
a1
end
@components begin
sys1 = C()
end
@equations begin
a1 ~ 0
end
else
@parameters begin
a2
end
@equations begin
a2 ~ 0
end
end
end
```

```julia
julia> BranchOutsideTheBlock.structure
Dict{Symbol, Any} with 5 entries:
:components => Any[(:if, :flag, [[:sys1, :C]], Any[])]
:kwargs => Dict{Symbol, Any}(:flag=>true)
:independent_variable => t
:parameters => Dict{Symbol, Dict{Symbol, Any}}(:a1=>Dict(:condition=>(:if, :flag, Dict{Symbol, Any}(:kwargs => Dict{Any, Any}(:a1 => nothing), :parameters => Any[Dict{Symbol, Dict{Symbol, Any}}(:a1 => Dict())]), Dict{Symbol, Any}(:kwargs => Dict{Any, Any}(:a2 => nothing), :parameters => Any[Dict{Symbol, Dict{Symbol, Any}}(:a2 => Dict())]))
:equations => Any[(:if, :flag, ["a1 ~ 0"], ["a2 ~ 0"])]
```
Conditional entries are entered in the format of `(branch, condition, [case when it is true], [case when it is false])`;
where branch is either `:if` or `:elseif`
The `[case when it is false]` is an empty vector or `nothing` when only if branch is
present; it is a vector or dictionary whenever else branch is present; it is a conditional tuple
whenever elseif branches are present.
For the conditional components and equations these condition tuples are added
directly, while for parameters and variables these are added as `:condition` metadata.

0 comments on commit 2c1cdf8

Please sign in to comment.