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/tutorials/custom_component.md
+52-62Lines changed: 52 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,7 @@ First, we need to make some imports.
10
10
using ModelingToolkit
11
11
using ModelingToolkit: t_nounits as t
12
12
using ModelingToolkitStandardLibrary.Electrical
13
-
using ModelingToolkitStandardLibrary.Electrical: OnePort
14
13
using OrdinaryDiffEq
15
-
using IfElse: ifelse
16
14
using Plots
17
15
```
18
16
@@ -36,102 +34,94 @@ end NonlinearResistor;
36
34
this can almost be directly translated to the syntax of `ModelingToolkit`.
37
35
38
36
```@example components
39
-
function NonlinearResistor(; name, Ga, Gb, Ve)
40
-
@named oneport = OnePort()
41
-
@unpack v, i = oneport
42
-
pars = @parameters Ga=Ga Gb=Gb Ve=Ve
43
-
eqs = [
37
+
@mtkmodel NonlinearResistor begin
38
+
@extend OnePort()
39
+
@parameters begin
40
+
Ga
41
+
Gb
42
+
Ve
43
+
end
44
+
@equations begin
44
45
i ~ ifelse(v < -Ve,
45
-
Gb * (v + Ve) - Ga * Ve,
46
-
ifelse(v > Ve,
47
-
Gb * (v - Ve) + Ga * Ve,
48
-
Ga * v))
49
-
]
50
-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
46
+
Gb * (v + Ve) - Ga * Ve,
47
+
ifelse(v > Ve,
48
+
Gb * (v - Ve) + Ga * Ve,
49
+
Ga * v))
50
+
end
51
51
end
52
52
nothing # hide
53
53
```
54
54
55
55
### Explanation
56
56
57
-
All components in `ModelingToolkit` are created via a function that serves as the constructor and returns some form of system, in this case, an `ODESystem`.
58
57
Since the non-linear resistor is essentially a standard electrical component with two ports, we can extend from the `OnePort` component of the library.
59
58
60
59
```julia
61
-
@named oneport =OnePort()
60
+
@extendOnePort()
62
61
```
63
62
64
-
This creates a `OnePort` with the `name = :oneport`.
65
-
For easier notation, we can unpack the states of the component
66
-
67
-
```julia
68
-
@unpack v, i = oneport
69
-
```
63
+
This extends `OnePort` and unpacks `v` and `i` variables.
70
64
71
65
It might be a good idea to create parameters for the constants of the `NonlinearResistor`.
72
66
73
67
```julia
74
-
pars =@parameters Ga=Ga Gb=Gb Ve=Ve
68
+
@parametersbegin
69
+
Ga
70
+
Gb
71
+
Ve
72
+
end
75
73
```
76
74
77
-
The syntax looks funny but it simply creates symbolic parameters with the name `Ga` where its default value is set from the function's argument `Ga`.
78
-
While this is not strictly necessary it allows the user to `remake` the problem easily with different parameters or allow for auto-tuning or parameter optimization without having to do all the costly steps that may be involved with building and simplifying a model.
79
-
The non-linear (in this case piece-wise constant) equation for the current can be implemented using `IfElse.ifelse`.
80
-
Finally, the created `oneport` component is extended with the created equations and parameters.
81
-
In this case, no extra state variables are added, hence an empty vector is supplied.
82
-
The independent variable `t` needs to be supplied as the second argument.
83
-
84
-
```julia
85
-
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
86
-
```
75
+
This creates symbolic parameters with the name `Ga`, `Gb` and `Ve` whose default values are set from the function's arguments `Ga`, `Gb` and `Ve`, respectively.
76
+
This allows the user to `remake` the problem easily with different parameters or allow for auto-tuning or parameter optimization without having to do all the costly steps that may be involved with building and simplifying a model.
77
+
The non-linear (in this case piece-wise constant) equation for the current can be implemented using `ifelse`.
87
78
88
79
## Building the Model
89
80
90
81
The final model can now be created with the components from the library and the new custom component.
First, `structural_simplify` is called on the model and an `ODEProblem` is built from the result.
123
-
Since the initial voltage of the first capacitor was already specified via `v`, no initial condition is given and an empty pair is supplied.
112
+
`@mtkbuild` builds a structurally simplified `ChaoticAttractor` model.
113
+
Since the initial voltage of the capacitors was already specified via `v` and the initial current of inductor via `i`, no initial condition is given and an empty pair is supplied.
0 commit comments