Skip to content

Commit c109448

Browse files
committed
ConstantVoltage added and tested
1 parent b6801d4 commit c109448

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/Electrical/Analog/sources.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ See [OnePort](@ref)
2323
end
2424
end
2525

26+
"""
27+
ConstantVoltage(; name)
28+
29+
Acts as an ideal constant voltage source with no internal resistance.
30+
31+
# States:
32+
33+
See [OnePort](@ref)
34+
35+
# Connectors:
36+
37+
- `p` Positive pin
38+
- `n` Negative pin
39+
40+
# Parameters:
41+
42+
- `V`: [`V`] Constant voltage value
43+
"""
44+
@mtkmodel ConstantVoltage begin
45+
@extend v, i = oneport = OnePort()
46+
@parameters begin
47+
V, [description = "Constant voltage value"]
48+
end
49+
@equations begin
50+
v ~ V
51+
end
52+
end
53+
2654
"""
2755
Current(; name)
2856

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, Current
24+
export Voltage, ConstantVoltage, Current
2525
include("Analog/sources.jl")
2626

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

test/Electrical/analog.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ end
108108
@test sol[capacitor.v][end]10 atol=1e-3
109109
end
110110

111+
# simple RC with constant voltage source
112+
@testset "RC with constant voltage source" begin
113+
@named voltage = ConstantVoltage(V = 10)
114+
@named resistor = Resistor(R = 1)
115+
@named capacitor = Capacitor(C = 1, v = 0.0)
116+
@named ground = Ground()
117+
118+
connections = [connect(voltage.p, resistor.p)
119+
connect(resistor.n, capacitor.p)
120+
connect(capacitor.n, voltage.n, ground.g)]
121+
122+
@named model = ODESystem(connections, t;
123+
systems = [resistor, capacitor, voltage, ground])
124+
sys = structural_simplify(model)
125+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
126+
sol = solve(prob, Tsit5())
127+
128+
# Plots.plot(sol; vars=[source.v, capacitor.v])
129+
@test SciMLBase.successful_retcode(sol)
130+
@test sol[capacitor.v][end]10 atol=1e-3
131+
end
132+
111133
# simple RL
112134
@testset "RL" begin
113135
@named source = Constant(k = 10)

0 commit comments

Comments
 (0)