Skip to content

Commit 4acbee6

Browse files
committed
refactor: Blocks/utils.jl (limited)
1 parent 0d4a2dc commit 4acbee6

File tree

4 files changed

+39
-35
lines changed

4 files changed

+39
-35
lines changed

src/Blocks/Blocks.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Blocks
55
using ModelingToolkit, Symbolics
66
using IfElse: ifelse
77
import ..@symcheck
8+
using ModelingToolkit: getdefault
89

910
@parameters t
1011
D = Differential(t)

src/Blocks/utils.jl

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,36 @@ Connector with one output signal of type Real.
5555
""" RealOutput
5656

5757
"""
58-
SISO(;name, u_start=0.0, y_start=0.0)
58+
SISO(;name, u_start = 0.0, y_start = 0.0)
5959
6060
Single input single output (SISO) continuous system block.
6161
6262
# Parameters:
6363
64-
- `u_start`: Initial value for the input
65-
- `y_start`: Initial value for the output
64+
- `u`: Initial value for the input
65+
- `y`: Initial value for the output
6666
"""
67-
@component function SISO(; name, u_start = 0.0, y_start = 0.0)
68-
@named input = RealInput(u_start = u_start)
69-
@named output = RealOutput(u_start = y_start)
70-
@variables(u(t)=u_start, [description = "Input of SISO system $name"],
71-
y(t)=y_start, [description = "Output of SISO system $name"],)
72-
eqs = [u ~ input.u
73-
y ~ output.u]
74-
return ODESystem(eqs, t, [u, y], []; name = name, systems = [input, output])
67+
@mtkmodel SISO begin
68+
@parameters begin
69+
u_start = 0.0
70+
y_start = 0.0
71+
end
72+
@variables begin
73+
u(t) = u_start, [description = "Input of SISO system"]
74+
y(t) = y_start, [description = "Output of SISO system"]
75+
end
76+
@components begin
77+
input = RealInput(u_start = 0.0)
78+
output = RealOutput(u_start = 0.0)
79+
end
80+
@equations begin
81+
u ~ input.u
82+
y ~ output.u
83+
end
7584
end
7685

7786
"""
78-
MIMO(;name, nin=1, nout=1, u_start=zeros(nin), y_start=zeros(nout))
87+
MIMO(; name, nin = 1, nout = 1, u_start = zeros(nin), y_start = zeros(nout))
7988
8089
Base class for a multiple input multiple output (MIMO) continuous system block.
8190

src/Electrical/Analog/ideal_components.jl

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,14 @@ Creates an ideal capacitor.
8989
- `C`: [`F`] Capacitance
9090
- `v_start`: [`V`] Initial voltage of capacitor
9191
"""
92-
@mtkmodel Capacitor begin
93-
@parameters begin
94-
C
95-
end
96-
@variables begin
97-
v
98-
end
99-
@extend v, i = oneport = OnePort(; v = v)
100-
@equations begin
101-
D(v) ~ i / C
102-
end
92+
@component function Capacitor(; name, C, v_start = 0.0)
93+
@named oneport = OnePort(; v_start = v_start)
94+
@unpack v, i = oneport
95+
pars = @parameters C = C
96+
eqs = [
97+
D(v) ~ i / C,
98+
]
99+
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
103100
end
104101

105102
"""
@@ -121,17 +118,14 @@ See [OnePort](@ref)
121118
- `L`: [`H`] Inductance
122119
- `i_start`: [`A`] Initial current through inductor
123120
"""
124-
@mtkmodel Inductor begin # name, L, i_start = 0.0)
125-
@parameters begin
126-
L
127-
end
128-
@variables begin
129-
i
130-
end
131-
@extend v, i = oneport = OnePort(; i = i)
132-
@equations begin
133-
D(i) ~ 1 / L * v
134-
end
121+
@component function Inductor(; name, L, i_start = 0.0)
122+
@named oneport = OnePort(; i_start = i_start)
123+
@unpack v, i = oneport
124+
pars = @parameters L = L
125+
eqs = [
126+
D(i) ~ 1 / L * v,
127+
]
128+
extend(ODESystem(eqs, t, [], pars; name = name), oneport)
135129
end
136130

137131
"""

test/Hydraulic/isothermal_compressible.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ end
256256
if use_input
257257
@named input = B.SampledData(Float64)
258258
else
259-
@named input = B.TimeVaryingFunction(f; t)
259+
@named input = B.TimeVaryingFunction(f)
260260
end
261261

262262
push!(systems, input)

0 commit comments

Comments
 (0)