You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/basics/MTKModel_Connector.md
+74-13Lines changed: 74 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,23 @@
1
-
# Defining components with `@mtkmodel`
1
+
# [Components and Connectors](@id mtkmodel_connector)
2
2
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`.
4
21
5
22
## What can an MTK-Model definition have?
6
23
@@ -56,9 +73,9 @@ end
56
73
57
74
- Parameters and variables are declared with respective begin blocks.
58
75
- 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.
60
77
- 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.
62
79
63
80
```julia
64
81
julia >@named model_c =ModelC(; v =2.0);
@@ -72,7 +89,7 @@ julia > ModelingToolkit.getdefault(model_c.v)
72
89
- 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.
73
90
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
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`.
90
108
91
109
### `@components` begin block
92
110
@@ -110,13 +128,26 @@ And as `k2` isn't listed in the sub-component definition of `ModelC`, its defaul
110
128
111
129
- Any other Julia operations can be included with dedicated begin blocks.
112
130
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.
114
141
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`.
116
147
117
148
A simple connector can be defined with syntax similar to following example:
118
149
119
-
```julia
150
+
```@example connector
120
151
using ModelingToolkit
121
152
122
153
@connector Pin begin
@@ -125,17 +156,47 @@ using ModelingToolkit
125
156
end
126
157
```
127
158
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).
129
160
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
+
```
131
183
132
184
!!! note
133
185
134
186
For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)
135
187
136
-
### What's a `structure` dictionary?
188
+
## More on `Model.structure`
189
+
190
+
`structure` stores metadata that describes composition of a model. It includes:
137
191
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).
0 commit comments