Skip to content

Commit ff2a778

Browse files
committed
docs: reword Components and Connectors and elaborate Connectors and Model.structure.
1 parent 438a225 commit ff2a778

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

docs/src/basics/MTKModel_Connector.md

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

33
## MTK Model
44

@@ -73,9 +73,9 @@ end
7373

7474
- Parameters and variables are declared with respective begin blocks.
7575
- Variables must be functions of an independent variable.
76-
- 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.
7777
- Along with creating parameters and variables, keyword arguments of same name with default value `nothing` are created.
78-
- 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.
7979

8080
```julia
8181
julia > @named model_c = ModelC(; v = 2.0);
@@ -89,7 +89,7 @@ julia > ModelingToolkit.getdefault(model_c.v)
8989
- 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.
9090
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
9191

92-
### `@extend` block
92+
#### `@extend` begin block
9393

9494
To extend a partial system,
9595

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

104104
```
105105

106-
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`.
107108

108109
### `@components` begin block
109110

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

128129
- Any other Julia operations can be included with dedicated begin blocks.
129130

130-
## Defining connectors with `@connector`
131+
## Connectors
131132

132-
`@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`.
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.
141+
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`.
133147

134148
A simple connector can be defined with syntax similar to following example:
135149

136-
```julia
150+
```@example connector
137151
using ModelingToolkit
138152
139153
@connector Pin begin
@@ -142,17 +156,47 @@ using ModelingToolkit
142156
end
143157
```
144158

145-
- 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](@id symbolic_metadata).
146160

147-
`@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+
```
148183

149184
!!! note
150185

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

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

155-
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).
156200

157201
For example, the structure of `ModelC` is:
158202

0 commit comments

Comments
 (0)