Skip to content

Commit 5c05ebd

Browse files
committed
Tests added
1 parent a2c1949 commit 5c05ebd

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/Electrical/Analog/ideal_components.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,10 @@ An electrical switch that is controlled by a boolean input.
421421
@extend v, i = oneport = OnePort()
422422

423423
@parameters begin
424-
state_init = false
425-
Gon = 1e5
426-
τ = 1e-3
424+
state_init = false,
425+
[description = "Initial switch state of type Bool. false: switch is open. true: switch is closed."]
426+
Gon = 1e5, [description = "Conductance when switch is closed"]
427+
τ = 1e-3, [description = "Parameter for filtered delay"]
427428
end
428429

429430
@components begin

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

2121
export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor

test/Electrical/analog.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ end
413413

414414
# Tests
415415
@test all(diode_current .>= -1e-3)
416-
@test capacitor_voltage[end].8.26 rtol=3e-1
416+
@test capacitor_voltage[end] .≈ 8.26 rtol=3e-1
417417

418418
# For visual inspection
419419
# plt = plot(sol; idxs = [diode.i, resistor.i, capacitor.v],
@@ -707,3 +707,41 @@ end
707707
0.0,
708708
atol = 1e-16)
709709
end
710+
711+
@testset "Switch test" begin
712+
V_value = 10.0
713+
714+
@mtkmodel SwitchTest begin
715+
@parameters begin
716+
V = V_value
717+
C = 1.0
718+
R = 1.0
719+
end
720+
721+
@components begin
722+
step = Step(start_time = 10.0, height = true, smooth = false)
723+
switch = Switch()
724+
voltage = Voltage()
725+
resistor = Resistor(R = R)
726+
capacitor = Capacitor(C = C, v = 0.0)
727+
ground = Ground()
728+
end
729+
730+
@equations begin
731+
connect(voltage.p, switch.p)
732+
connect(switch.n, resistor.p)
733+
connect(resistor.n, capacitor.p)
734+
connect(voltage.n, capacitor.n, ground.g)
735+
connect(step.output, switch.input)
736+
voltage.V.u ~ V
737+
end
738+
end
739+
740+
@mtkbuild sys = SwitchTest()
741+
prob = ODEProblem(sys, [], (0.0, 25.0))
742+
sol = solve(prob, Rodas4())
743+
744+
@test SciMLBase.successful_retcode(sol)
745+
@test isapprox(sol[sys.capacitor.v][end], V_value, atol = 1e-3)
746+
@test isapprox(sol[sys.capacitor.i][end], 0.0, atol = 1e-3)
747+
end

0 commit comments

Comments
 (0)