Skip to content

Commit 0d4a2dc

Browse files
committed
refactor: Blocks/sources.jl (limited)
1 parent 9875e7f commit 0d4a2dc

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

src/Blocks/sources.jl

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function triangular(x, f, amplitude, offset, start_time)
5959
end
6060

6161
"""
62+
Constant(; name, k = 0.0)
63+
6264
Generate constant signal.
6365

6466
# Parameters:
@@ -69,17 +71,20 @@ Generate constant signal.
6971

7072
- `output`
7173
"""
72-
@component function Constant(; name, k = 1)
73-
@named output = RealOutput()
74-
pars = @parameters k=k [description = "Constant output value of block $name"]
75-
eqs = [
76-
output.u ~ k,
77-
]
78-
compose(ODESystem(eqs, t, [], pars; name = name), [output])
74+
@mtkmodel Constant begin
75+
@components begin
76+
output = RealOutput()
77+
end
78+
@parameters begin
79+
k = 0.0, [description = "Constant output value of block"]
80+
end
81+
@equations begin
82+
output.u ~ k
83+
end
7984
end
8085

8186
"""
82-
TimeVaryingFunction(f; t=t, name)
87+
TimeVaryingFunction(f; name)
8388

8489
Outputs ``f(t)``.
8590

@@ -88,15 +93,23 @@ The input variable `t` can be changed by passing a different variable as the key
8893
# Connectors:
8994
- `output`
9095
"""
91-
@component function TimeVaryingFunction(f; t = t, name)
92-
@named output = RealOutput()
93-
eqs = [
94-
output.u ~ f(t),
95-
]
96-
compose(ODESystem(eqs, Blocks.t; name = name), [output])
96+
@mtkmodel TimeVaryingFunction begin
97+
@parameters begin
98+
f
99+
end
100+
@components begin
101+
output = RealOutput()
102+
end
103+
@equations begin
104+
output.u ~ first(getdefault(f))(t)
105+
end
97106
end
107+
TimeVaryingFunction.f(f; name) = TimeVaryingFunction.f(; f = [f], name)
98108

99109
"""
110+
Sine(; name, frequency, amplitude = 1, phase = 0, offset = 0, start_time = 0,
111+
smooth = false)
112+
100113
Generate sine signal.
101114

102115
# Parameters:
@@ -138,7 +151,10 @@ Generate sine signal.
138151
end
139152

140153
"""
141-
Generate cosine signal.
154+
Cosine(; name, frequency, amplitude = 1, phase = 0, offset = 0, start_time = 0,
155+
smooth = false)
156+
157+
Cosine signal.
142158

143159
# Parameters:
144160
- `frequency`: [Hz] Frequency of sine wave
@@ -177,6 +193,8 @@ Generate cosine signal.
177193
end
178194

179195
"""
196+
ContinuousClock(; name, offset = 0, start_time = 0)
197+
180198
Generate current time signal.
181199

182200
# Parameters:
@@ -199,6 +217,8 @@ Generate current time signal.
199217
end
200218

201219
"""
220+
Ramp(; name, height = 1, duration = 1, offset = 0, start_time = 0, smooth = false)
221+
202222
Generate ramp signal.
203223

204224
# Parameters:
@@ -239,6 +259,8 @@ Generate ramp signal.
239259
end
240260

241261
"""
262+
Square(; name, frequency = 1.0, amplitude = 1.0, offset = 0.0, start_time = 0.0,
263+
smooth = false)
242264
Generate smooth square signal.
243265

244266
# Parameters:
@@ -321,7 +343,9 @@ Generate step signal.
321343
end
322344

323345
"""
324-
Generate exponentially damped sine signal.
346+
ExpSine(; name, frequency, amplitude = 1, damping = 0.1, phase = 0, offset = 0, start_time = 0, smooth = false)
347+
348+
Exponentially damped sine signal.
325349

326350
# Parameters:
327351

@@ -367,6 +391,9 @@ Generate exponentially damped sine signal.
367391
end
368392

369393
"""
394+
Triangular(; name, amplitude = 1.0, frequency = 1.0, offset = 0.0,
395+
start_time = 0.0, smooth = false)
396+
370397
Generate smooth triangular signal for frequencies less than or equal to 25 Hz
371398

372399
# Parameters:
@@ -548,7 +575,7 @@ end
548575
"""
549576
SampledData(; name, buffer)
550577

551-
data input component.
578+
data input component.
552579

553580
# Parameters:
554581
- `buffer`: a `Parameter` type which holds the data and sample time

src/Electrical/Analog/ideal_components.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,17 @@ Creates an ideal capacitor.
8989
- `C`: [`F`] Capacitance
9090
- `v_start`: [`V`] Initial voltage of capacitor
9191
"""
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)
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
100103
end
101104

102105
"""
@@ -118,14 +121,17 @@ See [OnePort](@ref)
118121
- `L`: [`H`] Inductance
119122
- `i_start`: [`A`] Initial current through inductor
120123
"""
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)
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
129135
end
130136

131137
"""

test/Blocks/sources.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ end
3737
systems = [int, src])
3838
sys = structural_simplify(iosys)
3939

40-
prob = ODEProblem(sys, Pair[int.x => 0.0], (0.0, 10.0))
40+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
4141

4242
sol = solve(prob, Rodas4())
4343
@test sol.retcode == Success

test/demo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using Test
1212
@named capacitor = Capacitor(C = C)
1313
@named voltage = Voltage()
1414
@named ground = Ground()
15-
@named source = Constant()
15+
@named source = Constant(k = 1)
1616

1717
rc_eqs = [connect(source.output, voltage.V)
1818
connect(voltage.p, resistor.p)

0 commit comments

Comments
 (0)