@@ -4,6 +4,7 @@ using ModelingToolkitStandardLibrary.Blocks: Step,
44 Constant, Sine, Cosine, ExpSine, Ramp,
55 Square, Triangular
66using ModelingToolkitStandardLibrary. Blocks: square, triangular
7+ using ModelingToolkitStandardLibrary. Thermal: FixedTemperature
78using OrdinaryDiffEq: ReturnCode. Success
89
910# using Plots
421422 # title = "Diode Test")
422423 # savefig(plt, "diode_test")
423424end
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