Skip to content

Commit 2e84c48

Browse files
committed
Merge branch 'heatingdiode' into diode
2 parents 8c0efc0 + 563faa7 commit 2e84c48

File tree

5 files changed

+149
-2
lines changed

5 files changed

+149
-2
lines changed

.github/workflows/Downstream.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
exit(0) # Exit immediately, as a success
4949
end
5050
- uses: julia-actions/julia-processcoverage@v1
51-
- uses: codecov/codecov-action@v4
51+
- uses: codecov/codecov-action@v5
5252
with:
5353
file: lcov.info
5454
token: ${{ secrets.CODECOV_TOKEN }}

docs/src/API/electrical.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Capacitor
3232
Inductor
3333
IdealOpAmp
3434
Diode
35+
HeatingDiode
3536
```
3637

3738
## Analog Sensors

src/Electrical/Analog/ideal_components.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,84 @@ Ideal diode based on the Shockley diode equation.
299299
i ~ Is * (exp(v * q / (n * k * T)) - 1)
300300
end
301301
end
302+
303+
"""
304+
Diode(; name, Is = 1e-6, n = 1, T = 300.15)
305+
306+
Ideal diode based on the Shockley diode equation.
307+
308+
# States
309+
310+
- See [OnePort](@ref)
311+
312+
# Connectors
313+
314+
- `p` Positive pin
315+
- `n` Negative pin
316+
317+
# Parameters
318+
319+
- `Is`: [`A`] Saturation current
320+
- `n`: Ideality factor
321+
- `T`: [K] Ambient temperature
322+
"""
323+
@mtkmodel Diode begin
324+
begin
325+
k = 1.380649e-23 # Boltzmann constant (J/K)
326+
q = 1.602176634e-19 # Elementary charge (C)
327+
end
328+
329+
@extend v, i = oneport = OnePort(; v = 0.0)
330+
@parameters begin
331+
Is = 1e-6, [description = "Saturation current (A)"]
332+
n = 1, [description = "Ideality factor"]
333+
T = 300.15, [description = "Ambient temperature"]
334+
end
335+
@equations begin
336+
i ~ Is * (exp(v * q / (n * k * T)) - 1)
337+
end
338+
end
339+
340+
"""
341+
HeatingDiode(; name, Is = 1e-6, n = 1)
342+
343+
Temperature dependent diode based on the Shockley diode equation.
344+
345+
# States
346+
347+
- See [OnePort](@ref)
348+
349+
# Connectors
350+
351+
- `p` Positive pin
352+
- `n` Negative pin
353+
- `port` [HeatPort](@ref) Heat port to model the temperature dependency
354+
355+
# Parameters:
356+
357+
- `Is`: [`A`] Saturation current
358+
- `n`: Ideality factor
359+
"""
360+
@mtkmodel HeatingDiode begin
361+
begin
362+
k = 1.380649e-23 # Boltzmann constant (J/K)
363+
q = 1.602176634e-19 # Elementary charge (C)
364+
end
365+
366+
@extend v, i = oneport = OnePort(; v = 0.0)
367+
@components begin
368+
port = HeatPort()
369+
end
370+
@parameters begin
371+
Is = 1e-6, [description = "Saturation current (A)"]
372+
n = 1, [description = "Ideality factor"]
373+
end
374+
@variables begin
375+
Vt(t), [description = "Thermal voltage"]
376+
end
377+
@equations begin
378+
Vt ~ k * port.T / q # Thermal voltage equation
379+
i ~ Is * (exp(v / (n * Vt)) - 1) # Shockley diode equation
380+
port.Q_flow ~ -v * i # -LossPower
381+
end
382+
end

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
18+
HeatingResistor, Diode, HeatingDiode
1919
include("Analog/ideal_components.jl")
2020

2121
export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor

test/Electrical/analog.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using ModelingToolkitStandardLibrary.Blocks: Step,
44
Constant, Sine, Cosine, ExpSine, Ramp,
55
Square, Triangular
66
using ModelingToolkitStandardLibrary.Blocks: square, triangular
7+
using ModelingToolkitStandardLibrary.Thermal: FixedTemperature
78
using OrdinaryDiffEq: ReturnCode.Success
89

910
# using Plots
@@ -421,3 +422,67 @@ end
421422
# title = "Diode Test")
422423
# savefig(plt, "diode_test")
423424
end
425+
426+
@testset "HeatingDiode 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 = HeatingDiode(n = n, Is = Is)
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)
458+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
459+
sol = solve(prob)
460+
461+
# Extract solutions for testing
462+
diode_voltage = sol[heating_diode.v]
463+
diode_current = sol[heating_diode.i]
464+
resistor_current = sol[resistor.i]
465+
capacitor_voltage = sol[capacitor.v]
466+
467+
# Expected thermal voltage at given temperature
468+
k = 1.380649e-23 # Boltzmann constant (J/K)
469+
q = 1.602176634e-19 # Elementary charge (C)
470+
471+
# Tests
472+
@test all(diode_current .>= -Is) # Diode current should not exceed reverse saturation
473+
@test capacitor_voltage[end]V rtol=3e-1 # Final capacitor voltage close to input voltage
474+
475+
# For visual inspection
476+
# plt = plot(sol; vars = [heating_diode.i, resistor.i, capacitor.v],
477+
# size = (800, 600), dpi = 300,
478+
# labels = ["HeatingDiode Current" "Resistor Current" "Capacitor Voltage"],
479+
# title = "HeatingDiode Test")
480+
# savefig(plt, "heating_diode_test")
481+
482+
# Remake model with higher amb. temperature, final capacitor voltage should be lower
483+
T = 400.0
484+
model = remake(prob; p = [temp.T => T])
485+
sol = solve(model)
486+
@test SciMLBase.successful_retcode(sol)
487+
@test sol[capacitor.v][end] < capacitor_voltage[end]
488+
end

0 commit comments

Comments
 (0)