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/tutorial/Appendix.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,8 @@ OrderedDict{Symbol, Any} with 2 entries:
78
78
:r=>200
79
79
```
80
80
81
-
The values can also be a quoted expression, i.e. an expression enclosed in `:( )`, an array of quoted expressions encloded in `:[ ]` or just a quoted symbol, `:x`. This mechanism is used to encode equations and expressions of the model which needs to be manipulated before the model can be simulated.
81
+
The values can also be a quoted expression, i.e. an expression enclosed in `:( )`, an array of quoted expressions enclosed in `:[ ]` or just a quoted symbol, `:x`.
82
+
This mechanism is used to encode equations and expressions of the model which needs to be manipulated before the model can be simulated.
82
83
83
84
Julia defines a very useful merge operation between dictionaries:
84
85
@@ -90,7 +91,8 @@ OrderedDict{Symbol, Any} with 3 entries:
90
91
:r=>200
91
92
```
92
93
93
-
If a key already exists `q` in the first dictionary, it's value is overwritten otherwise it's added, `r`. Such a merge semantics allows for unification of parameter modifications and inheritance as will be demonstrated below.
94
+
If a key already exists in the first dictionary (like `:q`), its value is overwritten (like `:r`) otherwise it's added (like `:p`).
95
+
Such a merge semantic allows for unification of parameter modifications and inheritance as will be demonstrated below.
A model is defined with a constructor `Model` taking a comma separated list of name/value pairs.
39
-
The model consist of a definition of a parameter `T` with default value 0.2.
39
+
The model consists of a definition of a parameter `T` with default value 0.2.
40
40
Constructor `Var` with an `init` key is used to define the initial condition `0.2` of the state `x`, and one equation. Equations can have a Julia expression on both sides of the equal sign and are given as a *quoted* array expression `:[ ]` assigned to a unique identifier such as `equation`.
41
41
42
-
Macro `@instantiateModel(..)` symbolically processes the model, in particular solves the equation
42
+
The macro `@instantiateModel(..)` symbolically processes the model, in particular solves the equation
43
43
for the derivative `der(x)`, so the following equation will be used by the integrator:
44
44
45
45
```math
46
46
\frac{dx}{dt} = (1 - x) / T
47
47
```
48
48
49
49
Furthermore, a Julia function is generated and compiled to evaluate this equation. `@instantiateModel(..)`
50
-
returns an instance containing all the information needed for the further steps.
50
+
returns an instance containing all the information needed for the next steps.
51
51
52
52
The first [`simulate!`](@ref) function performs one simulation with the Modia default integrator
53
53
`Sundials.CVODE_BDF()`. The second `simulate!` call defines the integrator as second argument.
Copy file name to clipboardExpand all lines: docs/src/tutorial/Modeling.md
+56-49Lines changed: 56 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -152,7 +152,9 @@ equations = :[
152
152
153
153
## 2.4 Hierarchical modeling
154
154
155
-
Sofar, the composition of models have resulted in dictionaries of key/value pairs with values being numeric values or quoted expressions. Hierarchical models are obtained if the values themself are `Models`, i.e. dictionaries. A model with two filters can, for example, be defined as follows:
155
+
So far, the composition of models has resulted in dictionaries of key/value pairs with values being numeric values or quoted expressions.
156
+
Hierarchical models are obtained if the values themselves are `Models`, i.e. dictionaries.
157
+
A model with two filters can, for example, be defined as follows:
156
158
157
159
```julia
158
160
TwoFilters = (
@@ -161,7 +163,7 @@ TwoFilters = (
161
163
)
162
164
```
163
165
164
-
Note, that the previous definitions of HighPassFilter and LowPassFilter was used instead of making the Model defintions inline.
166
+
Note, that the previous definitions of `HighPassFilter` and `LowPassFilter` was used instead of making the Model definitions inline.
165
167
166
168
A band pass filter is a series connection of a high pass filter and a low pass filter and can be described as:
167
169
@@ -178,9 +180,11 @@ BandPassFilter = (
178
180
)
179
181
```
180
182
181
-
A new input, `u`, has been defined which is propagated to `high.u`. The series connection itself is obtained by the equation `low.u = high.y`. Note, that dot-notation is allowed in equations.
183
+
A new input, `u`, has been defined which is propagated to `high.u`.
184
+
The series connection itself is obtained by the equation `low.u = high.y`.
185
+
Note, that dot-notation is allowed in equations.
182
186
183
-
The input and output for the BandPassFilter when using the same input definition as for the TestLowPassFilter
187
+
The input and output for the `BandPassFilter` when using the same input definition as for the `TestLowPassFilter`
184
188
185
189
```julia
186
190
TestBandPassFilter = BandPassFilter |Map(
@@ -199,22 +203,29 @@ is shown below:
199
203
200
204
## 2.5 Physically oriented modeling
201
205
202
-
Sofar, only signal flow modeling has been used, i.e. input/output blocks coupled with equations between outputs and inputs. For object oriented modeling more high level constructs are neccessary. Coupling is then acausal and involves potentials such as electric potential, positions, pressure, etc. and flows such as electric current, forces and torques and mass flow rate.
206
+
So far, only signal flow modeling has been used, i.e. input/output blocks coupled with equations between outputs and inputs.
207
+
For object oriented modeling more high level constructs are necessary.
208
+
Coupling is then acausal and involves potentials such as electric potential, positions, pressure, etc. and flows such as electric current, forces and torques and mass flow rate.
203
209
204
210
### 2.5.1 Connectors
205
211
206
-
Models which contain any `flow` variable, i.e. a variable having an attribute `flow=true`, are considered connectors. Connectors must have equal number of flow and potential variables, i.e. variables having an attribute `potential=true`, and have matching array sizes. Connectors may not have any equations. An example of an electrical connector with potential (in Volt) and current (in Ampere) is shown below.
212
+
Models which contain any `flow` variable, i.e. a variable having an attribute `flow=true`, are considered connectors.
213
+
Connectors must have equal number of flow and potential variables, i.e. variables having an attribute `potential=true`, and have matching array sizes.
214
+
Connectors may not have any equations.
215
+
An example of an electrical connector with potential (in Volt) and current (in Ampere) is shown below.
207
216
208
217
```julia
209
218
Pin =Model( v = potential, i = flow )
210
219
```
211
-
`potential` is a shortcut for `Var(potential=true)` and similarly for `flow`.
220
+
221
+
The value `potential` is a shortcut for `Var(potential=true)` and similarly for `flow`.
212
222
213
223
### 2.5.2 Components
214
224
215
-
Components are declared in a similar ways as blocks. However, the interfaces between components are defined using connector instances.
225
+
Components are declared in a similar ways as blocks.
226
+
However, the interfaces between components are defined using connector instances.
216
227
217
-
An electrical resistor can be descibed as follows:
228
+
An electrical resistor can be described as follows:
218
229
219
230
```julia
220
231
Resistor =Model(
@@ -231,9 +242,12 @@ Resistor = Model(
231
242
232
243
### 2.5.3 Inheritance
233
244
234
-
Various physical components sometimes share common properties. One mechanism to handle this is to use inheritance. In Modia, **merging** is used.
245
+
Various physical components sometimes share common properties.
246
+
One mechanism to handle this is to use inheritance.
247
+
In Modia, **merging** is used.
235
248
236
-
Electrical components such as resistors, capacitors and inductors are categorized as oneports which have two pins. Common properties are: constraint on currents at the pins and definitions of voltage over the component and current through the component.
249
+
Electrical components such as resistors, capacitors and inductors are categorized as oneports which have two pins.
250
+
Common properties are: constraint on currents at the pins and definitions of voltage over the component and current through the component.
237
251
238
252
```julia
239
253
OnePort =Model(
@@ -245,7 +259,7 @@ OnePort = Model(
245
259
i = p.i ] )
246
260
```
247
261
248
-
Having such a OnePort definition makes it convenient to define electrical component models by merging OnePort with specific parameter definitions with default values and equations:
262
+
Having such a `OnePort` definition makes it convenient to define electrical component models by merging `OnePort` with specific parameter definitions with default values and equations:
249
263
250
264
```julia
251
265
Resistor = OnePort |Model( R =1.0u"Ω", equation = :[ R*i = v ], )
A connect reference has either the form 'connect instance name' or 'component instance name'.'connect instance name' with 'connect instance name' being either a connector instance, input or output variable.
291
300
292
301
Examples
@@ -299,7 +308,8 @@ Examples
299
308
]
300
309
```
301
310
302
-
For connectors, all the potentials of the connectors in the same connect tuple are set equal and the sum of all incoming flows to the model are set equal to the sum of the flows into sub-components. A Modelica inspired form of connections, i.e. connect-equations, are also supported:
311
+
For connectors, all the potentials of the connectors in the same connect tuple are set equal and the sum of all incoming flows to the model are set equal to the sum of the flows into sub-components.
312
+
A Modelica inspired form of connections, i.e. connect-equations, are also supported:
303
313
304
314
```julia
305
315
equations = :[
@@ -316,8 +326,7 @@ Having the above electrical component models, enables defining a filter
by instanciating components, setting parameters and defining connections.
320
-
329
+
by instantiating components, setting parameters and defining connections.
321
330
322
331
```julia
323
332
Filter = (
@@ -345,7 +354,10 @@ The connect tuples are translated to:
345
354
346
355
### 2.5.6 Parameter propagation
347
356
348
-
Hierarchical modification of parameters is powerful but sometimes a bit inconvenient. It is also possible to propagate parameters introduced on a high level down in the hierarchy. The following Filter model defines three parameters, `r`, `c` and `v`. The `r` parameter is used to set the resistance of the resistor R: `Map(R=:r)`.
357
+
Hierarchical modification of parameters is powerful but sometimes a bit inconvenient.
358
+
It is also possible to propagate parameters introduced on a high level down in the hierarchy.
359
+
The following Filter model defines three parameters, `r`, `c` and `v`.
360
+
The `r` parameter is used to set the resistance of the resistor R: `Map(R=:r)`.
349
361
350
362
```julia
351
363
Filter2 =Model(
@@ -369,9 +381,11 @@ Two separate filters can then be defined with:
369
381
TwoFilters =Model( f1 = Filter |Map( r =10.0, c =2.0), f2 = Filter )
370
382
```
371
383
372
-
### 2.5.7 Redeclarations
384
+
### 2.5.7 Re-declarations
373
385
374
-
It is possible to reuse a particular model topology by redeclaring the model of particular components. For example, changing the filter `f1` to a voltage divider by changing C from a Capacitor to a Resistor. A predefined definition `redeclare` is used for this purpose.
386
+
It is possible to reuse a particular model topology by redeclaring the models of particular components.
387
+
For example, changing the filter `f1` to a voltage divider by changing `C` from a Capacitor to a Resistor.
388
+
A predefined definition `redeclare` is used for this purpose.
and demonstrates how to build up a hierarchical, multi-domain model consisting
394
-
of a servo-system with a load, where the servo-system consists of
395
-
an electric motor with a current and speed controller, as well with a more
396
-
detailed model of a gearbox.
406
+
and demonstrates how to build up a hierarchical, multi-domain model consisting of a servo-system with a load, where the servo-system consists of an electric motor with a current and speed controller, as well with a more detailed model of a gearbox.
397
407
398
408
399
409
## 2.6 Arrays
400
410
401
-
Model parameters and variables can be arrays. For example a linear state space system
411
+
Model parameters and variables can be arrays.
412
+
For example a linear state space system
402
413
403
414
```math
404
415
\begin{aligned}
@@ -443,15 +454,15 @@ SecondOrder = Model(
443
454
equations = :[sys.u = [1.0]]
444
455
)
445
456
```
457
+
446
458
Variables `sys.u` and `sys.y` are vectors with one element each.
447
459
448
-
Note, `[0; w^2]` is a vector in Julia and not a column matrix
449
-
(see the discussion [here](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617)).
460
+
Note, `[0; w^2]` is a vector in Julia and not a column matrix (see the discussion [here](https://discourse.julialang.org/t/construct-a-2-d-column-array/30617)).
450
461
In order that `B` is defined as column matrix, the function `col(..)` is used.
451
462
452
463
Array equations remain array equations during symbolic transformation and in the generated code,
453
-
so the code is both compact and efficient. In order that this is reasonably possible, the definition
454
-
of an array cannot be split in different statements:
464
+
so the code is both compact and efficient.
465
+
In order that this is reasonably possible, the definition of an array cannot be split in different statements:
455
466
456
467
```julia
457
468
equations = :[ # error, vector v is not defined as one symbol
@@ -460,8 +471,7 @@ equations = :[ # error, vector v is not defined as one symbol
460
471
]
461
472
```
462
473
463
-
If scalar equations are needed in which arrays are used, then the arrays have
464
-
to be first defined and then elements can be used.
474
+
If scalar equations are needed in which arrays are used, then the arrays have to be first defined and then elements can be used.
465
475
466
476
```julia
467
477
v =Var(init=zeros(2)),
@@ -476,8 +486,7 @@ equations = :[
476
486
477
487
## 2.7 Model libraries
478
488
479
-
Modia provides a small set of pre-defined model components in directory
480
-
`Modia.modelsPath`:
489
+
Modia provides a small set of pre-defined model components in directory `Modia.modelsPath`:
481
490
482
491
-`AllModels.jl` - Include all model libraries
483
492
-`Blocks.jl` - Input/output control blocks
@@ -487,11 +496,9 @@ Modia provides a small set of pre-defined model components in directory
0 commit comments