Skip to content

Commit 75c44d1

Browse files
committed
Add serial_inductor
1 parent 737459a commit 75c44d1

File tree

6 files changed

+61
-34
lines changed

6 files changed

+61
-34
lines changed

Diff for: test/models/electrical_components.jl renamed to examples/electrical_components.jl

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ function Pin(;name)
88
ODESystem(Equation[], t, [v, i], [], name=name, defaults=[v=>1.0, i=>1.0])
99
end
1010

11-
function Ground(name)
11+
function Ground(;name)
1212
@named g = Pin()
1313
eqs = [g.v ~ 0]
1414
ODESystem(eqs, t, [], [], systems=[g], name=name)
1515
end
1616

17-
function ConstantVoltage(name; V = 1.0)
17+
function ConstantVoltage(;name, V = 1.0)
1818
val = V
1919
@named p = Pin()
2020
@named n = Pin()
@@ -26,7 +26,7 @@ function ConstantVoltage(name; V = 1.0)
2626
ODESystem(eqs, t, [], [V], systems=[p, n], defaults=Dict(V => val), name=name)
2727
end
2828

29-
function Resistor(name; R = 1.0)
29+
function Resistor(;name, R = 1.0)
3030
val = R
3131
@named p = Pin()
3232
@named n = Pin()
@@ -40,7 +40,7 @@ function Resistor(name; R = 1.0)
4040
ODESystem(eqs, t, [v], [R], systems=[p, n], defaults=Dict(R => val), name=name)
4141
end
4242

43-
function Capacitor(name; C = 1.0)
43+
function Capacitor(;name, C = 1.0)
4444
val = C
4545
@named p = Pin()
4646
@named n = Pin()
@@ -70,3 +70,15 @@ function Inductor(; name, L = 1.0)
7070
]
7171
ODESystem(eqs, t, [v, i], [L], systems=[p, n], defaults=Dict(L => val), name=name)
7272
end
73+
74+
function connect_pins(ps...)
75+
eqs = [
76+
0 ~ sum(p->p.i, ps) # KCL
77+
]
78+
# KVL
79+
for i in 1:length(ps)-1
80+
push!(eqs, ps[i].v ~ ps[i+1].v)
81+
end
82+
83+
return eqs
84+
end

Diff for: examples/rc_model.jl

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include("electrical_components.jl")
2+
3+
R = 1.0
4+
C = 1.0
5+
V = 1.0
6+
@named resistor = Resistor(R=R)
7+
@named capacitor = Capacitor(C=C)
8+
@named source = ConstantVoltage(V=V)
9+
@named ground = Ground()
10+
11+
rc_eqs = [
12+
connect_pins(source.p, resistor.p)
13+
connect_pins(resistor.n, capacitor.p)
14+
connect_pins(capacitor.n, source.n, ground.g)
15+
]
16+
17+
@named rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground])

Diff for: examples/serial_inductor.jl

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include("electrical_components.jl")
2+
3+
@named source = ConstantVoltage(V=10.0)
4+
@named resistor = Resistor(R=1.0)
5+
@named inductor1 = Inductor(L=8.0e-9)
6+
@named inductor2 = Inductor(L=2.0e-9)
7+
@named ground = Ground()
8+
9+
eqs = [
10+
connect_pins(source.p, resistor.p)
11+
connect_pins(resistor.n, inductor1.p)
12+
connect_pins(inductor1.n, inductor2.p)
13+
connect_pins(source.n, inductor2.n, ground.g)
14+
]
15+
16+
@named ll_model = ODESystem(eqs, t, systems=[source, resistor, inductor1, inductor2, ground])

Diff for: test/components.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
22
using ModelingToolkit, OrdinaryDiffEq
33

4-
include("models/rc_model.jl")
4+
include("../examples/rc_model.jl")
55

66
sys = structural_simplify(rc_model)
77
@test !isempty(ModelingToolkit.defaults(sys))
@@ -31,3 +31,13 @@ sol = solve(prob, Tsit5())
3131
@test sol[resistor.v] == sol[source.p.v] - sol[capacitor.p.v]
3232
#using Plots
3333
#plot(sol)
34+
35+
include("../examples/serial_inductor.jl")
36+
sys = structural_simplify(ll_model)
37+
u0 = [
38+
inductor1.i => 0.0
39+
inductor2.i => 0.0
40+
inductor2.v => 0.0
41+
]
42+
prob = ODEProblem(sys, u0, (0, 10.0))
43+
sol = solve(prob, Rodas4())

Diff for: test/models/rc_model.jl

-28
This file was deleted.

Diff for: test/print_tree.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ModelingToolkit, AbstractTrees, Test
22

3-
include("models/rc_model.jl")
3+
include("../examples/rc_model.jl")
44

55
io = IOBuffer()
66
print_tree(io, rc_model)

0 commit comments

Comments
 (0)