Skip to content

Commit e2f5ab3

Browse files
Merge pull request #420 from SciML/as/mtk-v11
refactor: depend on MTKBase
2 parents d25ebf1 + 1ce2cf4 commit e2f5ab3

72 files changed

Lines changed: 2649 additions & 1570 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Project.toml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
name = "ModelingToolkitStandardLibrary"
22
uuid = "16a59e39-deab-5bd0-87e4-056b12336739"
3-
authors = ["Chris Rackauckas and Julia Computing"]
43
version = "2.25.0"
4+
authors = ["Chris Rackauckas and Julia Computing"]
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
88
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
99
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
1010
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
11-
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
11+
ModelingToolkitBase = "7771a370-6774-4173-bd38-47e70ca0b839"
1212
PreallocationTools = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
1313
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1414

@@ -18,19 +18,21 @@ Aqua = "0.8"
1818
ChainRulesCore = "1.24"
1919
ControlSystemsBase = "1.4"
2020
DataFrames = "1.7"
21-
DataInterpolations = "6"
21+
DataInterpolations = "8"
2222
DiffEqBase = "6.152"
23-
ForwardDiff = "0.10"
23+
ForwardDiff = "0.10, 1"
2424
IfElse = "0.1"
2525
LinearAlgebra = "1.10"
26-
ModelingToolkit = "10"
26+
ModelingToolkit = "11"
27+
ModelingToolkitBase = "1"
2728
OrdinaryDiffEq = "6.87"
2829
OrdinaryDiffEqDefault = "1.1"
2930
PreallocationTools = "0.4.23"
3031
SafeTestsets = "0.1"
32+
SciCompDSL = "1"
3133
SciMLStructures = "1.4.2"
3234
SymbolicIndexingInterface = "0.3.28"
33-
Symbolics = "6.14"
35+
Symbolics = "7.4.1"
3436
Test = "1"
3537
julia = "1.10"
3638

@@ -42,12 +44,14 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
4244
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
4345
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
4446
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
47+
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
4548
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
4649
OrdinaryDiffEqDefault = "50262376-6c5a-4cf5-baba-aaf4f84d72d7"
4750
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
51+
SciCompDSL = "91a8cdf1-4ca6-467b-a780-87fda3fff15e"
4852
SciMLStructures = "53ae85a6-f571-4167-b2af-e1d143709226"
4953
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
5054
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5155

5256
[targets]
53-
test = ["ADTypes", "Aqua", "LinearAlgebra", "OrdinaryDiffEqDefault", "OrdinaryDiffEq", "SafeTestsets", "Test", "ControlSystemsBase", "DataFrames", "DataInterpolations", "SciMLStructures", "SymbolicIndexingInterface", "ForwardDiff"]
57+
test = ["ADTypes", "Aqua", "LinearAlgebra", "OrdinaryDiffEqDefault", "OrdinaryDiffEq", "SafeTestsets", "Test", "ControlSystemsBase", "DataFrames", "DataInterpolations", "SciMLStructures", "SymbolicIndexingInterface", "ForwardDiff", "SciCompDSL", "ModelingToolkit"]

src/Blocks/Blocks.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
The module `Blocks` contains common input-output components, referred to as blocks.
33
"""
44
module Blocks
5-
using ModelingToolkit, Symbolics
5+
using ModelingToolkitBase, Symbolics
66
import IfElse: ifelse
77
import ..@symcheck
8-
using ModelingToolkit: getdefault, t_nounits as t, D_nounits as D
8+
using ModelingToolkitBase: getdefault, t_nounits as t, D_nounits as D
99

1010
export RealInput, RealInputArray, RealOutput, RealOutputArray, SISO
1111
include("utils.jl")
@@ -28,7 +28,4 @@ export Integrator, Derivative, FirstOrder, SecondOrder, StateSpace, TransferFunc
2828
export PI, LimPI, PID, LimPID
2929
include("continuous.jl")
3030

31-
export AnalysisPoint, get_sensitivity, get_comp_sensitivity,
32-
get_looptransfer, open_loop
33-
3431
end

src/Blocks/continuous.jl

Lines changed: 112 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,28 @@ Initial value of integrator state ``x`` can be set with `x`
1717
1818
- `x`: State of Integrator. Defaults to 0.0.
1919
"""
20-
@mtkmodel Integrator begin
21-
@extend u, y = siso = SISO()
22-
@variables begin
23-
x(t) = 0.0, [description = "State of Integrator"]
20+
@component function Integrator(; name, k = 1, x = 0.0)
21+
@named siso = SISO()
22+
@unpack u, y = siso
23+
24+
pars = @parameters begin
25+
k = k, [description = "Gain"]
2426
end
25-
@parameters begin
26-
k = 1, [description = "Gain"]
27+
28+
systems = @named begin
2729
end
28-
@equations begin
29-
D(x) ~ k * u
30-
y ~ x
30+
31+
vars = @variables begin
32+
x(t) = x, [description = "State of Integrator"]
3133
end
34+
35+
equations = Equation[
36+
D(x) ~ k * u,
37+
y ~ x
38+
]
39+
40+
sys = System(equations, t, vars, pars; name, systems)
41+
return extend(sys, siso)
3242
end
3343

3444
"""
@@ -37,8 +47,8 @@ end
3747
Outputs an approximate derivative of the input. The transfer function of this block is
3848
3949
```
40-
k k ks
41-
─ - ─────── = ──────
50+
k k ks
51+
─ - ─────── = ──────
4252
T sT² + T sT + 1
4353
```
4454
@@ -62,23 +72,32 @@ Initial value of the state ``x`` can be set with `x`.
6272
- `input`
6373
- `output`
6474
"""
65-
@mtkmodel Derivative begin
66-
@extend u, y = siso = SISO()
67-
@variables begin
68-
x(t) = 0.0, [description = "Derivative-filter state"]
69-
end
70-
@parameters begin
75+
@component function Derivative(; name, k = 1, T = nothing, x = 0.0)
76+
@symcheck T > 0 ||
77+
throw(ArgumentError("Time constant `T` has to be strictly positive"))
78+
79+
@named siso = SISO()
80+
@unpack u, y = siso
81+
82+
pars = @parameters begin
7183
T = T, [description = "Time constant"]
72-
k = 1, [description = "Gain"]
84+
k = k, [description = "Gain"]
7385
end
74-
begin
75-
@symcheck T > 0 ||
76-
throw(ArgumentError("Time constant `T` has to be strictly positive"))
86+
87+
systems = @named begin
7788
end
78-
@equations begin
79-
D(x) ~ (u - x) / T
80-
y ~ (k / T) * (u - x)
89+
90+
vars = @variables begin
91+
x(t) = x, [description = "Derivative-filter state"]
8192
end
93+
94+
equations = Equation[
95+
D(x) ~ (u - x) / T,
96+
y ~ (k / T) * (u - x)
97+
]
98+
99+
sys = System(equations, t, vars, pars; name, systems)
100+
return extend(sys, siso)
82101
end
83102

84103
"""
@@ -116,26 +135,32 @@ Initial value of the state `x` can be set with `x`
116135
117136
See also [`SecondOrder`](@ref)
118137
"""
119-
@mtkmodel FirstOrder begin
120-
@extend u, y = siso = SISO()
121-
@structural_parameters begin
122-
lowpass = true
123-
end
124-
@variables begin
125-
x(t) = 0.0, [description = "State of FirstOrder filter"]
126-
end
127-
@parameters begin
138+
@component function FirstOrder(; name, lowpass = true, T = nothing, k = 1.0, x = 0.0)
139+
@symcheck T > 0 ||
140+
throw(ArgumentError("Time constant `T` has to be strictly positive"))
141+
142+
@named siso = SISO()
143+
@unpack u, y = siso
144+
145+
pars = @parameters begin
128146
T = T, [description = "Time constant"]
129-
k = 1.0, [description = "Gain"]
147+
k = k, [description = "Gain"]
130148
end
131-
begin
132-
@symcheck T > 0 ||
133-
throw(ArgumentError("Time constant `T` has to be strictly positive"))
149+
150+
systems = @named begin
134151
end
135-
@equations begin
136-
D(x) ~ (k * u - x) / T
137-
lowpass ? y ~ x : y ~ k * u - x
152+
153+
vars = @variables begin
154+
x(t) = x, [description = "State of FirstOrder filter"]
138155
end
156+
157+
equations = Equation[
158+
D(x) ~ (k * u - x) / T,
159+
lowpass ? (y ~ x) : (y ~ k * u - x)
160+
]
161+
162+
sys = System(equations, t, vars, pars; name, systems)
163+
return extend(sys, siso)
139164
end
140165

141166
"""
@@ -165,22 +190,32 @@ Initial value of the state `x` can be set with `x`, and of derivative state `xd`
165190
- `input`
166191
- `output`
167192
"""
168-
@mtkmodel SecondOrder begin
169-
@extend u, y = siso = SISO()
170-
@variables begin
171-
x(t), [description = "State of SecondOrder filter", guess = 0.0]
172-
xd(t), [description = "Derivative state of SecondOrder filter", guess = 0.0]
193+
@component function SecondOrder(; name, k = 1.0, w = 1.0, d = 1.0, x = nothing, xd = nothing)
194+
@named siso = SISO()
195+
@unpack u, y = siso
196+
197+
pars = @parameters begin
198+
k = k, [description = "Gain"]
199+
w = w, [description = "Bandwidth (angular frequency)"]
200+
d = d, [description = "Relative damping"]
173201
end
174-
@parameters begin
175-
k = 1.0, [description = "Gain"]
176-
w = 1.0, [description = "Bandwidth (angular frequency)"]
177-
d = 1.0, [description = "Relative damping"]
202+
203+
systems = @named begin
178204
end
179-
@equations begin
180-
D(x) ~ xd
181-
D(xd) ~ w * (w * (k * u - x) - 2 * d * xd)
182-
y ~ x
205+
206+
vars = @variables begin
207+
x(t) = x, [description = "State of SecondOrder filter", guess = 0.0]
208+
xd(t) = xd, [description = "Derivative state of SecondOrder filter", guess = 0.0]
183209
end
210+
211+
equations = Equation[
212+
D(x) ~ xd,
213+
D(xd) ~ w * (w * (k * u - x) - 2 * d * xd),
214+
y ~ x
215+
]
216+
217+
sys = System(equations, t, vars, pars; name, systems)
218+
return extend(sys, siso)
184219
end
185220

186221
"""
@@ -206,29 +241,35 @@ U(s) = k (1 + \\dfrac{1}{sT}) E(S)
206241
207242
See also [`LimPI`](@ref)
208243
"""
209-
@mtkmodel PI begin
210-
@parameters begin
211-
k = 1.0, [description = "Proportional gain"]
212-
T = 1.0, [description = "Integrator time constant"]
213-
end
214-
begin
215-
@symcheck T > 0 ||
216-
throw(ArgumentError("Time constant `T` has to be strictly positive"))
244+
@component function PI(; name, k = 1.0, T = 1.0, gainPI__k = nothing)
245+
@symcheck T > 0 ||
246+
throw(ArgumentError("Time constant `T` has to be strictly positive"))
247+
248+
pars = @parameters begin
249+
k = k, [description = "Proportional gain"]
250+
T = T, [description = "Integrator time constant"]
217251
end
218-
@components begin
252+
253+
systems = @named begin
219254
err_input = RealInput() # control error
220255
ctr_output = RealOutput() # control signal
221-
gainPI = Gain(; k)
256+
gainPI = Gain(; k = gainPI__k)
222257
addPI = Add()
223258
int = Integrator(k = 1 / T, x = 0.0)
224259
end
225-
@equations begin
226-
connect(err_input, addPI.input1)
227-
connect(addPI.output, gainPI.input)
228-
connect(gainPI.output, ctr_output)
229-
connect(err_input, int.input)
230-
connect(int.output, addPI.input2)
260+
261+
vars = @variables begin
231262
end
263+
264+
equations = Equation[
265+
connect(err_input, addPI.input1),
266+
connect(addPI.output, gainPI.input),
267+
connect(gainPI.output, ctr_output),
268+
connect(err_input, int.input),
269+
connect(int.output, addPI.input2)
270+
]
271+
272+
return System(equations, t, vars, pars; name, systems)
232273
end
233274

234275
"""
@@ -337,7 +378,7 @@ The simplified expression above is given without the anti-windup protection.
337378
- `err_input`
338379
- `ctr_output`
339380
"""
340-
@component function LimPI(; name, k = 1, T, u_max, u_min = -u_max, Ta, int__x = 0.0)
381+
@component function LimPI(; name, k = 1, T = nothing, u_max = nothing, u_min = -u_max, Ta = nothing, int__x = 0.0)
341382
@symcheck Ta > 0 ||
342383
throw(ArgumentError("Time constant `Ta` has to be strictly positive"))
343384
@symcheck T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
@@ -610,7 +651,7 @@ To set the initial state, it's recommended to set the initial condition for `x`,
610651
- `input`
611652
- `output`
612653
613-
See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/stable/) for an interface between [ControlSystems.jl](https://juliacontrol.github.io/ControlSystems.jl/stable/) and ModelingToolkit.jl for advanced manipulation of transfer functions and linear statespace systems. For linearization, see [`linearize`](@ref) and [Linear Analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/).
654+
See also [`StateSpace`](@ref) which handles MIMO systems, as well as [ControlSystemsMTK.jl](https://juliacontrol.github.io/ControlSystemsMTK.jl/stable/) for an interface between [ControlSystems.jl](https://juliacontrol.github.io/ControlSystems.jl/stable/) and ModelingToolkitBase.jl for advanced manipulation of transfer functions and linear statespace systems. For linearization, see [`linearize`](@ref) and [Linear Analysis](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/API/linear_analysis/).
614655
"""
615656
@component function TransferFunction(; b = [1], a = [1, 1], name)
616657
nb = length(b)

0 commit comments

Comments
 (0)