Skip to content

Commit 78d1805

Browse files
committed
PR#349 rebased, conflicts handled and tested
1 parent b8597e7 commit 78d1805

File tree

3 files changed

+54
-96
lines changed

3 files changed

+54
-96
lines changed

src/Electrical/Analog/ideal_components.jl

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ node.
1818
end
1919

2020
"""
21-
Resistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0, T_dep = false)
21+
Resistor(; name, R = 1.0, T_ref = 300.15, alpha = 0, T_dep = false)
2222
2323
Generic resistor with optional temperature dependency.
2424
@@ -205,47 +205,6 @@ See [OnePort](@ref)
205205
end
206206
end
207207

208-
"""
209-
HeatingResistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0)
210-
211-
Temperature dependent electrical resistor
212-
213-
# States
214-
215-
- See [OnePort](@ref)
216-
- `R(t)`: [`Ohm`] Temperature dependent resistance `R ~ R_ref*(1 + alpha*(heat_port.T(t) - T_ref))`
217-
218-
# Connectors
219-
220-
- `p` Positive pin
221-
- `n` Negative pin
222-
223-
# Parameters:
224-
225-
- `R_ref`: [`Ω`] Reference resistance
226-
- `T_ref`: [K] Reference temperature
227-
- `alpha`: [K⁻¹] Temperature coefficient of resistance
228-
"""
229-
@mtkmodel HeatingResistor begin
230-
@extend v, i = oneport = OnePort()
231-
@components begin
232-
heat_port = HeatPort()
233-
end
234-
@parameters begin
235-
R_ref = 1.0, [description = "Reference resistance"]
236-
T_ref = 300.15, [description = "Reference temperature"]
237-
alpha = 0, [description = "Temperature coefficient of resistance"]
238-
end
239-
@variables begin
240-
R(t), [guess = R_ref]
241-
end
242-
@equations begin
243-
R ~ R_ref * (1 + alpha * (heat_port.T - T_ref))
244-
heat_port.Q_flow ~ -v * i # -LossPower
245-
v ~ i * R
246-
end
247-
end
248-
249208
"""
250209
EMF(; name, k)
251210
@@ -291,7 +250,7 @@ Electromotoric force (electric/mechanic transformer)
291250
end
292251

293252
"""
294-
Diode(; name, Is = 1e-6, n = 1, T_ref = 300.15, T_dep = false)
253+
Diode(; name, Is = 1e-6, n = 1, T = 300.15, T_dep = false)
295254
296255
Generic diode with optional temperature dependency.
297256
@@ -303,13 +262,13 @@ Generic diode with optional temperature dependency.
303262
304263
- `p` Positive pin
305264
- `n` Negative pin
306-
- `port` [HeatPort](@ref) (only if `T_dep = true`) Heat port to model the temperature dependency
265+
- `port` [HeatPort](@ref) (only if `T_dep = true`) Heat port to model variable temperature dependency
307266
308267
# Parameters:
309268
310269
- `Is`: [`A`] Saturation current
311270
- `n`: Ideality factor
312-
- `T_ref`: [K] Reference temperature
271+
- `T`: [K] Constant ambient temperature - only used if T_dep=false
313272
- `T_dep`: [bool] Temperature dependency
314273
"""
315274
@mtkmodel Diode begin
@@ -326,8 +285,7 @@ Generic diode with optional temperature dependency.
326285
@parameters begin
327286
Is = 1e-6, [description = "Saturation current (A)"]
328287
n = 1, [description = "Ideality factor"]
329-
T_ref = 300.15, [description = "Reference temperature (K)"]
330-
Vt_const = k * T_ref / q, [description = "Constant thermal voltage"]
288+
T = 300.15, [description = "Ambient temperature"]
331289
end
332290

333291
if T_dep
@@ -344,7 +302,7 @@ Generic diode with optional temperature dependency.
344302
end
345303
else
346304
@equations begin
347-
i ~ Is * (exp(v / (n * Vt_const)) - 1) # Shockley diode equation
305+
i ~ Is * (exp(v * q / (n * k * T)) - 1) # Shockley diode equation
348306
end
349307
end
350308
end
@@ -372,20 +330,20 @@ R = R_const + pos * R_ref * (1 + alpha * (port.T - T_ref))
372330
- `R(t)`: Resistance
373331
374332
# Connectors
375-
376-
- `p` Positive pin
377-
- `n` Negative pin
378-
- `position` RealInput to set the position of the wiper
379-
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
333+
334+
- `p` Positive pin
335+
- `n` Negative pin
336+
- `position` RealInput to set the position of the wiper
337+
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
380338
381339
# Parameters
382-
383-
- `R_ref`: [`Ω`] Resistance at temperature T_ref when fully closed (pos=1.0)
384-
- `T_ref`: [K] Reference temperature
385-
- `R_const`: [`Ω`] Constant resistance between p and n
386-
- `T_dep`: [bool] Temperature dependency
387-
- `alpha`: [K⁻¹] Temperature coefficient of resistance
388-
- `enforce_bounds`: Enforce bounds for the position of the wiper (0-1)
340+
341+
- `R_ref`: [`Ω`] Resistance at temperature T_ref when fully closed (pos=1.0)
342+
- `T_ref`: [K] Reference temperature
343+
- `R_const`: [`Ω`] Constant resistance between p and n
344+
- `T_dep`: Temperature dependency
345+
- `alpha`: [K⁻¹] Temperature coefficient of resistance
346+
- `enforce_bounds`: Enforce bounds for the position of the wiper (0-1)
389347
"""
390348
@mtkmodel VariableResistor begin
391349
@extend v, i = oneport = OnePort()

test/Electrical/analog.jl

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -423,38 +423,38 @@ end
423423
# savefig(plt, "diode_test")
424424
end
425425

426-
@testset "Diode with temperature dependency component test" begin
427-
# Parameter values
428-
R = 1.0
429-
C = 1.0
430-
V = 10.0
431-
T = 300.0 # Ambient temperature in Kelvin
432-
n = 2.0
433-
Is = 1e-6
434-
f = 1.0
435-
436-
# Components
437-
@named resistor = Resistor(R = R)
438-
@named capacitor = Capacitor(C = C, v = 0.0)
439-
@named source = Voltage()
440-
@named heating_diode = Diode(n = n, Is = Is, T_dep = true)
441-
@named ac = Sine(frequency = f, amplitude = V)
442-
@named ground = Ground()
443-
@named temp = FixedTemperature(T = T)
444-
445-
# Connections
446-
connections = [connect(ac.output, source.V),
447-
connect(source.p, heating_diode.p),
448-
connect(heating_diode.n, resistor.p),
449-
connect(resistor.n, capacitor.p),
450-
connect(capacitor.n, ground.g),
451-
connect(source.n, ground.g),
452-
connect(temp.port, heating_diode.port)]
453-
454-
# Model
455-
@named model = ODESystem(connections, t;
456-
systems = [resistor, capacitor, source, heating_diode, ac, ground, temp])
457-
sys = structural_simplify(model)
426+
@testset "HeatingDiode component test" begin
427+
@mtkmodel HeatingDiodeTest begin
428+
@parameters begin
429+
R = 1.0
430+
C = 1.0
431+
V = 10.0
432+
T = 300.0 # Ambient temperature in Kelvin
433+
n = 2.0
434+
Is = 1e-6
435+
f = 1.0
436+
end
437+
@components begin
438+
resistor = Resistor(R = R)
439+
capacitor = Capacitor(C = C, v = 0.0)
440+
source = Voltage()
441+
heating_diode = Diode(n = n, Is = Is, T_dep = true)
442+
ac = Sine(frequency = f, amplitude = V)
443+
ground = Ground()
444+
temp = FixedTemperature(T = T)
445+
end
446+
@equations begin
447+
connect(ac.output, source.V)
448+
connect(source.p, heating_diode.p)
449+
connect(heating_diode.n, resistor.p)
450+
connect(resistor.n, capacitor.p)
451+
connect(capacitor.n, ground.g)
452+
connect(source.n, ground.g)
453+
connect(temp.port, heating_diode.port)
454+
end
455+
end
456+
457+
@mtkbuild sys = HeatingDiodeTest()
458458
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
459459
sol = solve(prob, Rodas4())
460460

@@ -473,10 +473,10 @@ end
473473
@test capacitor_voltage[end]7.75 rtol=3e-1 # Final capacitor voltage close to input voltage
474474

475475
# For visual inspection
476-
# plt = plot(sol; idxs = [heating_diode.i, resistor.i, capacitor.v],
476+
# plt = plot(sol; vars = [heating_diode.i, resistor.i, capacitor.v],
477477
# size = (800, 600), dpi = 300,
478-
# labels = ["Diode Current" "Resistor Current" "Capacitor Voltage"],
479-
# title = "Diode Test")
478+
# labels = ["HeatingDiode Current" "Resistor Current" "Capacitor Voltage"],
479+
# title = "HeatingDiode Test")
480480
# savefig(plt, "heating_diode_test")
481481

482482
# Remake model with higher amb. temperature, final capacitor voltage should be lower

test/multi_domain.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ end
175175
end
176176
end
177177

178-
@testset "El. Heating Circuit" begin
178+
@testset "Electrical Heating Circuit" begin
179179
@mtkmodel ElHeatingCircuit begin
180180
@components begin
181181
ground = Ground()

0 commit comments

Comments
 (0)