Skip to content

Commit 455ccad

Browse files
committed
Simplify temperature dependency for analog components
1 parent be8d22f commit 455ccad

File tree

4 files changed

+81
-115
lines changed

4 files changed

+81
-115
lines changed

src/Electrical/Analog/ideal_components.jl

Lines changed: 72 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,57 @@ node.
1818
end
1919

2020
"""
21-
Resistor(; name, R)
21+
Resistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0, T_dep = false)
2222
23-
Creates an ideal Resistor following Ohm's Law.
23+
Generic resistor with optional temperature dependency.
2424
2525
# States:
2626
27-
See [OnePort](@ref)
27+
- See [OnePort](@ref)
28+
- `R(t)`: [`Ω`] Resistance (temperature dependent if `T_dep = true`)
2829
2930
# Connectors:
3031
3132
- `p` Positive pin
3233
- `n` Negative pin
34+
- `heat_port` [HeatPort](@ref) (only if `T_dep = true`) Heat port to model the temperature dependency
3335
3436
# Parameters:
3537
36-
- `R`: [`Ohm`] Resistance
38+
- `R`: [`Ω`] Reference resistance
39+
- `T_ref`: [K] Reference temperature
40+
- `alpha`: [K⁻¹] Temperature coefficient of resistance
41+
- `T_dep`: [bool] Temperature dependency
3742
"""
3843
@mtkmodel Resistor begin
3944
@extend v, i = oneport = OnePort()
45+
46+
@structural_parameters begin
47+
T_dep = false
48+
end
49+
4050
@parameters begin
41-
R, [description = "Resistance"]
51+
R = 1.0, [description = "Reference resistance", unit = "Ω"]
52+
T_ref = 300.15, [description = "Reference temperature", unit = "K"]
53+
alpha = 0.0, [description = "Temperature coefficient of resistance", unit = "K⁻¹"]
4254
end
43-
@equations begin
44-
v ~ i * R
55+
56+
if T_dep
57+
@components begin
58+
heat_port = HeatPort()
59+
end
60+
@variables begin
61+
R_T(t), [description = "Temperature-dependent resistance", unit = "Ω"]
62+
end
63+
@equations begin
64+
R_T ~ R * (1 + alpha * (heat_port.T - T_ref)) # Temperature-dependent resistance
65+
heat_port.Q_flow ~ -v * i # -LossPower
66+
v ~ i * R_T # Ohm's Law
67+
end
68+
else
69+
@equations begin
70+
v ~ i * R # Ohm's Law for constant resistance
71+
end
4572
end
4673
end
4774

@@ -178,47 +205,6 @@ See [OnePort](@ref)
178205
end
179206
end
180207

181-
"""
182-
HeatingResistor(; name, R_ref = 1.0, T_ref = 300.15, alpha = 0)
183-
184-
Temperature dependent electrical resistor
185-
186-
# States
187-
188-
- See [OnePort](@ref)
189-
- `R(t)`: [`Ohm`] Temperature dependent resistance `R ~ R_ref*(1 + alpha*(heat_port.T(t) - T_ref))`
190-
191-
# Connectors
192-
193-
- `p` Positive pin
194-
- `n` Negative pin
195-
196-
# Parameters:
197-
198-
- `R_ref`: [`Ω`] Reference resistance
199-
- `T_ref`: [K] Reference temperature
200-
- `alpha`: [K⁻¹] Temperature coefficient of resistance
201-
"""
202-
@mtkmodel HeatingResistor begin
203-
@extend v, i = oneport = OnePort()
204-
@components begin
205-
heat_port = HeatPort()
206-
end
207-
@parameters begin
208-
R_ref = 1.0, [description = "Reference resistance"]
209-
T_ref = 300.15, [description = "Reference temperature"]
210-
alpha = 0, [description = "Temperature coefficient of resistance"]
211-
end
212-
@variables begin
213-
R(t) = R_ref
214-
end
215-
@equations begin
216-
R ~ R_ref * (1 + alpha * (heat_port.T - T_ref))
217-
heat_port.Q_flow ~ -v * i # -LossPower
218-
v ~ i * R
219-
end
220-
end
221-
222208
"""
223209
EMF(; name, k)
224210
@@ -264,9 +250,9 @@ Electromotoric force (electric/mechanic transformer)
264250
end
265251

266252
"""
267-
Diode(; name, Is = 1e-6, n = 1, T = 300.15)
253+
Diode(; name, Is = 1e-6, n = 1, T_ref = 300.15, T_dep = false)
268254
269-
Ideal diode based on the Shockley diode equation.
255+
Generic diode with optional temperature dependency.
270256
271257
# States
272258
@@ -276,12 +262,14 @@ Ideal diode based on the Shockley diode equation.
276262
277263
- `p` Positive pin
278264
- `n` Negative pin
265+
- `port` [HeatPort](@ref) (only if `T_dep = true`) Heat port to model the temperature dependency
279266
280-
# Parameters
281-
267+
# Parameters:
268+
282269
- `Is`: [`A`] Saturation current
283270
- `n`: Ideality factor
284-
- `T`: [K] Ambient temperature
271+
- `T_ref`: [K] Reference temperature
272+
- `T_dep`: [bool] Temperature dependency
285273
"""
286274
@mtkmodel Diode begin
287275
begin
@@ -290,57 +278,34 @@ Ideal diode based on the Shockley diode equation.
290278
end
291279

292280
@extend v, i = oneport = OnePort(; v = 0.0)
293-
@parameters begin
294-
Is = 1e-6, [description = "Saturation current (A)"]
295-
n = 1, [description = "Ideality factor"]
296-
T = 300.15, [description = "Ambient temperature"]
297-
end
298-
@equations begin
299-
i ~ Is * (exp(v * q / (n * k * T)) - 1)
300-
end
301-
end
302-
303-
"""
304-
HeatingDiode(; name, Is = 1e-6, n = 1)
305-
306-
Temperature dependent diode based on the Shockley diode equation.
307-
308-
# States
309281

310-
- See [OnePort](@ref)
311-
312-
# Connectors
313-
314-
- `p` Positive pin
315-
- `n` Negative pin
316-
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
317-
318-
# Parameters:
319-
320-
- `Is`: [`A`] Saturation current
321-
- `n`: Ideality factor
322-
"""
323-
@mtkmodel HeatingDiode begin
324-
begin
325-
k = 1.380649e-23 # Boltzmann constant (J/K)
326-
q = 1.602176634e-19 # Elementary charge (C)
282+
@structural_parameters begin
283+
T_dep = false
327284
end
328285

329-
@extend v, i = oneport = OnePort(; v = 0.0)
330-
@components begin
331-
port = HeatPort()
332-
end
333286
@parameters begin
334287
Is = 1e-6, [description = "Saturation current (A)"]
335288
n = 1, [description = "Ideality factor"]
289+
T_ref = 300.15, [description = "Reference temperature (K)"]
290+
Vt_const = k * T_ref / q, [description = "Constant thermal voltage"]
336291
end
337-
@variables begin
338-
Vt(t), [description = "Thermal voltage"]
339-
end
340-
@equations begin
341-
Vt ~ k * port.T / q # Thermal voltage equation
342-
i ~ Is * (exp(v / (n * Vt)) - 1) # Shockley diode equation
343-
port.Q_flow ~ -v * i # -LossPower
292+
293+
if T_dep
294+
@components begin
295+
port = HeatPort()
296+
end
297+
@variables begin
298+
Vt(t), [description = "Thermal voltage"]
299+
end
300+
@equations begin
301+
Vt ~ k * port.T / q # Thermal voltage equation
302+
i ~ Is * (exp(v / (n * Vt)) - 1) # Shockley diode equation with temperature dependence
303+
port.Q_flow ~ -v * i # -LossPower
304+
end
305+
else
306+
@equations begin
307+
i ~ Is * (exp(v / (n * Vt_const)) - 1) # Shockley diode equation
308+
end
344309
end
345310
end
346311

@@ -368,19 +333,19 @@ R = R_const + pos * R_ref * (1 + alpha * (port.T - T_ref))
368333
369334
# Connectors
370335
371-
- `p` Positive pin
372-
- `n` Negative pin
373-
- `position` RealInput to set the position of the wiper
374-
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
336+
- `p` Positive pin
337+
- `n` Negative pin
338+
- `position` RealInput to set the position of the wiper
339+
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
375340
376341
# Parameters
377342
378-
- `R_ref`: [`Ω`] Resistance at temperature T_ref when fully closed (pos=1.0)
379-
- `T_ref`: [K] Reference temperature
380-
- `R_const`: [`Ω`] Constant resistance between p and n
381-
- `T_dep`: Temperature dependency
382-
- `alpha`: [K⁻¹] Temperature coefficient of resistance
383-
- `enforce_bounds`: Enforce bounds for the position of the wiper (0-1)
343+
- `R_ref`: [`Ω`] Resistance at temperature T_ref when fully closed (pos=1.0)
344+
- `T_ref`: [K] Reference temperature
345+
- `R_const`: [`Ω`] Constant resistance between p and n
346+
- `T_dep`: [bool] Temperature dependency
347+
- `alpha`: [K⁻¹] Temperature coefficient of resistance
348+
- `enforce_bounds`: Enforce bounds for the position of the wiper (0-1)
384349
"""
385350
@mtkmodel VariableResistor begin
386351
@extend v, i = oneport = OnePort()

src/Electrical/Electrical.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include("utils.jl")
1515

1616
export Capacitor,
1717
Ground, Inductor, Resistor, Conductor, Short, IdealOpAmp, EMF,
18-
HeatingResistor, Diode, HeatingDiode, VariableResistor
18+
Diode, VariableResistor
1919
include("Analog/ideal_components.jl")
2020

2121
export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor

test/Electrical/analog.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,14 @@ end
416416
@test capacitor_voltage[end].V rtol=3e-1
417417

418418
# For visual inspection
419-
# plt = plot(sol; vars = [diode.i, resistor.i, capacitor.v],
419+
# plt = plot(sol; idxs = [diode.i, resistor.i, capacitor.v],
420420
# size = (800, 600), dpi = 300,
421421
# labels = ["Diode Current" "Resistor Current" "Capacitor Voltage"],
422422
# title = "Diode Test")
423423
# savefig(plt, "diode_test")
424424
end
425425

426-
@testset "HeatingDiode component test" begin
426+
@testset "Diode with temperature dependency component test" begin
427427
# Parameter values
428428
R = 1.0
429429
C = 1.0
@@ -437,7 +437,7 @@ end
437437
@named resistor = Resistor(R = R)
438438
@named capacitor = Capacitor(C = C, v = 0.0)
439439
@named source = Voltage()
440-
@named heating_diode = HeatingDiode(n = n, Is = Is)
440+
@named heating_diode = Diode(n = n, Is = Is, T_dep = true)
441441
@named ac = Sine(frequency = f, amplitude = V)
442442
@named ground = Ground()
443443
@named temp = FixedTemperature(T = T)
@@ -473,10 +473,10 @@ end
473473
@test capacitor_voltage[end]V rtol=3e-1 # Final capacitor voltage close to input voltage
474474

475475
# For visual inspection
476-
# plt = plot(sol; vars = [heating_diode.i, resistor.i, capacitor.v],
476+
# plt = plot(sol; idxs = [heating_diode.i, resistor.i, capacitor.v],
477477
# size = (800, 600), dpi = 300,
478-
# labels = ["HeatingDiode Current" "Resistor Current" "Capacitor Voltage"],
479-
# title = "HeatingDiode Test")
478+
# labels = ["Diode Current" "Resistor Current" "Capacitor Voltage"],
479+
# title = "Diode 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ end
181181
ground = Ground()
182182
source = Voltage()
183183
voltage_sine = Blocks.Sine(amplitude = 220, frequency = 1)
184-
heating_resistor = HeatingResistor(R_ref = 100, alpha = 1e-3, T_ref = 293.15)
184+
heating_resistor = Resistor(R = 100, alpha = 1e-3,
185+
T_ref = 293.15, T_dep = true)
185186
thermal_conductor = ThermalConductor(G = 50)
186187
env = FixedTemperature(T = 273.15 + 20)
187188
end

0 commit comments

Comments
 (0)