Skip to content

Commit 1873f93

Browse files
committed
ConstantCurrent added and tested
1 parent c109448 commit 1873f93

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

src/Electrical/Analog/sources.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ See [OnePort](@ref)
2424
end
2525

2626
"""
27-
ConstantVoltage(; name)
27+
ConstantVoltage(; name, V)
2828
2929
Acts as an ideal constant voltage source with no internal resistance.
3030
@@ -75,3 +75,31 @@ See [OnePort](@ref)
7575
i ~ I.u
7676
end
7777
end
78+
79+
"""
80+
ConstantCurrent(; name, I)
81+
82+
Acts as an ideal constant current source with no internal resistance.
83+
84+
# States:
85+
86+
See [OnePort](@ref)
87+
88+
# Connectors:
89+
90+
- `p` Positive pin
91+
- `n` Negative pin
92+
93+
# Parameters:
94+
95+
- `I`: [`A`] Constant current value
96+
"""
97+
@mtkmodel ConstantCurrent begin
98+
@extend v, i = oneport = OnePort()
99+
@parameters begin
100+
I, [description = "Constant current value"]
101+
end
102+
@equations begin
103+
i ~ I
104+
end
105+
end

src/Electrical/Electrical.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include("Analog/ideal_components.jl")
2121
export CurrentSensor, PotentialSensor, VoltageSensor, PowerSensor, MultiSensor
2222
include("Analog/sensors.jl")
2323

24-
export Voltage, ConstantVoltage, Current
24+
export Voltage, ConstantVoltage, Current, ConstantCurrent
2525
include("Analog/sources.jl")
2626

2727
# include("Digital/gates.jl")

test/Electrical/analog.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,27 @@ end
215215
@test sum(reduce(vcat, sol[capacitor.v]) .- y(sol.t, start_time))0 atol=1e-2
216216
end
217217

218+
# RC with constant current source
219+
@testset "RC with constant current source" begin
220+
@named current = ConstantCurrent(I = 1)
221+
@named resistor = Resistor(R = 1)
222+
@named capacitor = Capacitor(C = 1, v = 0.0)
223+
@named ground = Ground()
224+
225+
connections = [connect(current.p, resistor.n)
226+
connect(capacitor.n, resistor.p)
227+
connect(capacitor.p, current.n, ground.g)]
228+
229+
@named model = ODESystem(connections, t;
230+
systems = [ground, resistor, current, capacitor])
231+
sys = structural_simplify(model)
232+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
233+
sol = solve(prob, Tsit5())
234+
y(x, st) = (x .> st) .* abs.(collect(x) .- st)
235+
@test SciMLBase.successful_retcode(sol)
236+
@test sum(reduce(vcat, sol[capacitor.v]) .- y(sol.t, 0.0))0 atol=1e-2
237+
end
238+
218239
@testset "Integrator" begin
219240
R = 1e3
220241
f = 1

0 commit comments

Comments
 (0)