Skip to content

Commit 3034c9f

Browse files
Merge pull request #2286 from ven-k/vkb/mtkmodel-examples
Refactor docs with `@mtkmodel`
2 parents 7949761 + 2ef4752 commit 3034c9f

File tree

7 files changed

+540
-252
lines changed

7 files changed

+540
-252
lines changed

docs/src/basics/ContextualVariables.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ for which its arguments must be specified each time it is used. This is useful w
4848
PDEs for example, where one may need to use `u(t, x)` in the equations, but will
4949
need to be able to write `u(t, 0.0)` to define a boundary condition at `x = 0`.
5050

51-
## Variable metadata [Experimental/TODO]
51+
## Variable metadata
5252

5353
In many engineering systems, some variables act like “flows” while others do not.
5454
For example, in circuit models you have current which flows, and the related
@@ -69,15 +69,17 @@ the metadata. One can get and set metadata by
6969
```julia
7070
julia> @variables x [unit = u"m^3/s"];
7171

72-
julia> hasmetadata(x, Symbolics.option_to_metadata_type(Val(:unit)))
72+
julia> hasmetadata(x, VariableUnit)
7373
true
7474

75-
julia> getmetadata(x, Symbolics.option_to_metadata_type(Val(:unit)))
75+
julia> ModelingToolkit.get_unit(x)
7676
m³ s⁻¹
7777

78-
julia> x = setmetadata(x, Symbolics.option_to_metadata_type(Val(:unit)), u"m/s")
78+
julia> x = setmetadata(x, VariableUnit, u"m/s")
7979
x
8080

81-
julia> getmetadata(x, Symbolics.option_to_metadata_type(Val(:unit)))
81+
julia> ModelingToolkit.get_unit(x)
8282
m s⁻¹
8383
```
84+
85+
See [Symbolic Metadata](@ref symbolic_metadata) for more details on variable metadata.

docs/src/basics/MTKModel_Connector.md

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
# Defining components with `@mtkmodel`
1+
# [Components and Connectors](@id mtkmodel_connector)
22

3-
`@mtkmodel` is a convenience macro to define ModelingToolkit components. It returns `ModelingToolkit.Model`, which includes a constructor that returns an ODESystem, a `structure` dictionary with metadata and flag `isconnector` which is set to `false`.
3+
## MTK Model
4+
5+
MTK represents components and connectors with `Model`.
6+
7+
```@docs
8+
ModelingToolkit.Model
9+
```
10+
11+
## Components
12+
13+
Components are models from various domains. These models contain states and their
14+
equations.
15+
16+
### [Defining components with `@mtkmodel`](@id mtkmodel)
17+
18+
`@mtkmodel` is a convenience macro to define components. It returns
19+
`ModelingToolkit.Model`, which includes a constructor that returns the ODESystem, a
20+
`structure` dictionary with metadata, and flag `isconnector` which is set to `false`.
421

522
## What can an MTK-Model definition have?
623

@@ -56,9 +73,9 @@ end
5673

5774
- Parameters and variables are declared with respective begin blocks.
5875
- Variables must be functions of an independent variable.
59-
- Optionally, default values and metadata can be specified for these parameters and variables. See `ModelB` in the above example.
76+
- Optionally, initial guess and metadata can be specified for these parameters and variables. See `ModelB` in the above example.
6077
- Along with creating parameters and variables, keyword arguments of same name with default value `nothing` are created.
61-
- Whenever a parameter or variable has default value, for example `v(t) = 0.0`, a symbolic variable named `v` with default 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.
78+
- 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.
6279

6380
```julia
6481
julia > @named model_c = ModelC(; v = 2.0);
@@ -72,7 +89,7 @@ julia > ModelingToolkit.getdefault(model_c.v)
7289
- This block is for non symbolic input arguements. These are for inputs that usually are not meant to be part of components; but influence how they are defined. One can list inputs like boolean flags, functions etc... here.
7390
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
7491

75-
### `@extend` block
92+
#### `@extend` begin block
7693

7794
To extend a partial system,
7895

@@ -86,7 +103,8 @@ julia> @named model_c = ModelC(; p1 = 2.0)
86103

87104
```
88105

89-
However, as `p2` isn't listed in the model definition, its default can't be modified by users.
106+
However, as `p2` isn't listed in the model definition, its initial guess can't
107+
specified while creating an instance of `ModelC`.
90108

91109
### `@components` begin block
92110

@@ -110,13 +128,26 @@ And as `k2` isn't listed in the sub-component definition of `ModelC`, its defaul
110128

111129
- Any other Julia operations can be included with dedicated begin blocks.
112130

113-
## Defining connectors with `@connector`
131+
## Connectors
132+
133+
Connectors are special models that can be used to connect different components together.
134+
MTK provides 3 distinct connectors:
135+
136+
- `DomainConnector`: A connector which has only one state which is of `Flow` type,
137+
specified by `[connect = Flow]`.
138+
- `StreamConnector`: A connector which has atleast one stream variable, specified by
139+
`[connect = Stream]`. A `StreamConnector` must have exactly one flow variable.
140+
- `RegularConnector`: Connectors that don't fall under above categories.
114141

115-
`@connector` returns `ModelingToolkit.Model`. It includes a constructor that returns a connector ODESystem, a `structure` dictionary with metadata and flag `isconnector` which is set to `true`.
142+
### [Defining connectors with `@connector`](@id connector)
143+
144+
`@connector` returns `ModelingToolkit.Model`. It includes a constructor that returns
145+
a connector ODESystem, a `structure` dictionary with metadata, and flag `isconnector`
146+
which is set to `true`.
116147

117148
A simple connector can be defined with syntax similar to following example:
118149

119-
```julia
150+
```@example connector
120151
using ModelingToolkit
121152
122153
@connector Pin begin
@@ -125,17 +156,47 @@ using ModelingToolkit
125156
end
126157
```
127158

128-
- Variables (as function of independent variable) are listed out in the definition. These variables can optionally have default values and metadata like `descrption`, `connect` and so on.
159+
Variables (as functions of independent variable) are listed out in the definition. These variables can optionally have initial values and metadata like `description`, `connect` and so on. For more details on setting metadata, check out [Symbolic Metadata](@ref symbolic_metadata).
129160

130-
`@connector`s accepts begin blocks of `@components`, `@equations`, `@extend`, `@parameters`, `@structural_parameters`, `@variables`. These keywords mean the same as described above for `@mtkmodel`.
161+
Similar to `@mtkmodel`, `@connector` accepts begin blocks of `@components`, `@equations`, `@extend`, `@parameters`, `@structural_parameters`, `@variables`. These keywords mean the same as described above for `@mtkmodel`.
162+
For example, the following `HydraulicFluid` connector is defined with parameters, variables and equations.
163+
164+
```@example connector
165+
@connector HydraulicFluid begin
166+
@parameters begin
167+
ρ = 997
168+
β = 2.09e9
169+
μ = 0.0010016
170+
n = 1
171+
let_gas = 1
172+
ρ_gas = 0.0073955
173+
p_gas = -1000
174+
end
175+
@variables begin
176+
dm(t) = 0.0, [connect = Flow]
177+
end
178+
@equations begin
179+
dm ~ 0
180+
end
181+
end
182+
```
131183

132184
!!! note
133185

134186
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
135187

136-
### What's a `structure` dictionary?
188+
## More on `Model.structure`
189+
190+
`structure` stores metadata that describes composition of a model. It includes:
137191

138-
For components defined with `@mtkmodel` or `@connector`, a dictionary with metadata is created. It lists `:components` (sub-component list), `:extend` (the extended states and base system), `:parameters`, `:variables`, ``:kwargs`` (list of keyword arguments), `:independent_variable`, `:equations`.
192+
- `:components`: List of sub-components in the form of [[name, sub_component_name],...].
193+
- `:extend`: The list of extended states, name given to the base system, and name of the base system.
194+
- `:structural_parameters`: Dictionary of structural parameters mapped to their default values.
195+
- `:parameters`: Dictionary of symbolic parameters mapped to their metadata.
196+
- `:variables`: Dictionary of symbolic variables mapped to their metadata.
197+
- `:kwargs`: Dictionary of keyword arguments mapped to their default values.
198+
- `:independent_variable`: Independent variable, which is added while generating the Model.
199+
- `:equations`: List of equations (represented as strings).
139200

140201
For example, the structure of `ModelC` is:
141202

docs/src/basics/Variable_metadata.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Symbolic metadata
1+
# [Symbolic Metadata](@id symbolic_metadata)
22

33
It is possible to add metadata to symbolic variables, the metadata will be displayed when calling help on a variable.
44

@@ -39,6 +39,22 @@ help?> u
3939
Symbolics.VariableSource: (:variables, :u)
4040
```
4141

42+
## Connect
43+
44+
Variables in connectors can have `connect` metadata which describes the type of connections.
45+
46+
`Flow` is used for variables that represent physical quantities that "flow" ex:
47+
current in a resistor. These variables sum up to zero in connections.
48+
49+
`Stream` can be specified for variables that flow bi-directionally.
50+
51+
```@example connect
52+
using ModelingToolkit
53+
54+
@variables t, i(t) [connect = Flow]
55+
@variables k(t) [connect = Stream]
56+
```
57+
4258
## Input or output
4359

4460
Designate a variable as either an input or an output using the following

0 commit comments

Comments
 (0)