Skip to content

Electrical switch added #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Electrical/Analog/ideal_components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,56 @@ R = R_const + pos * R_ref * (1 + alpha * (port.T - T_ref))
v ~ i * R
end
end

"""
Switch(; Gon = 1e5, state_init = false)

An electrical switch that is controlled by a boolean input.

The switch is modelled as follows:
- i = G*v
- G = state*G_on
- state: boolean input

G will therefore be 0 when the input is false [0] and G_on when the input is true [1].

# States

- See [OnePort](@ref)
- `state(t)`: Boolean input
- `G(t)`: Conductance

# Connectors

- `p` Positive pin
- `n` Negative pin
- `input` [RealInput](@ref) Input of type Bool. false: switch is open. true: switch is closed.

# Parameters:

- `Gon`: [`S`] Conductance when switch is closed. Value should be finite to avoid a singularity.
- `state_init`: Initial guess for the state of the switch.
"""
@mtkmodel Switch begin
@extend v, i = oneport = OnePort()

@parameters begin
Gon = 1e5, [description = "Conductance when switch is closed"]
state_init = false, [description = "Initial guess for the state of the switch"]
end

@components begin
input = RealInput()
end

@variables begin
state(t)::Bool, [guess = state_init]
G(t)
end

@equations begin
state ~ input.u
G ~ state * Gon
i ~ G * v
end
end
2 changes: 1 addition & 1 deletion src/Electrical/Electrical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include("utils.jl")

export Capacitor,
Ground, Inductor, Resistor, Conductor, Short, IdealOpAmp, EMF,
Diode, VariableResistor
Diode, VariableResistor, Switch
include("Analog/ideal_components.jl")

export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor
Expand Down
42 changes: 42 additions & 0 deletions test/Electrical/analog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,45 @@ end
0.0,
atol = 1e-16)
end

@testset "Switch test" begin
V_value = 10.0

@mtkmodel SwitchTest begin
@parameters begin
V = V_value
C = 1.0
R = 1.0
end

@components begin
step = Step(start_time = 10.0, height = true, smooth = 1e-5)
switch = Switch()
voltage = Voltage()
resistor = Resistor(R = R)
capacitor = Capacitor(C = C)
ground = Ground()
end

@equations begin
connect(voltage.p, switch.p)
connect(switch.n, resistor.p)
connect(resistor.n, capacitor.p)
connect(voltage.n, capacitor.n, ground.g)
connect(step.output, switch.input)
voltage.V.u ~ V
end
end

@mtkbuild sys = SwitchTest()
u0 = [
sys.capacitor.v => 0.0,
sys.capacitor.i => 0.0
]
prob = ODEProblem(sys, u0, (0.0, 25.0))
sol = solve(prob, Rodas4())

@test SciMLBase.successful_retcode(sol)
@test isapprox(sol[sys.capacitor.v][end], V_value, atol = 1e-3)
@test isapprox(sol[sys.capacitor.i][end], 0.0, atol = 1e-3)
end
Loading