Skip to content

Commit 737459a

Browse files
committed
Move around
1 parent 711c703 commit 737459a

File tree

4 files changed

+42
-25
lines changed

4 files changed

+42
-25
lines changed

test/components.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
22
using ModelingToolkit, OrdinaryDiffEq
33

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

66
sys = structural_simplify(rc_model)
77
@test !isempty(ModelingToolkit.defaults(sys))

test/rc_model.jl renamed to test/models/electrical_components.jl

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,18 @@ function Capacitor(name; C = 1.0)
5555
ODESystem(eqs, t, [v], [C], systems=[p, n], defaults=Dict(C => val), name=name)
5656
end
5757

58-
R = 1.0
59-
C = 1.0
60-
V = 1.0
61-
resistor = Resistor(:resistor, R=R)
62-
capacitor = Capacitor(:capacitor, C=C)
63-
source = ConstantVoltage(:source, V=V)
64-
ground = Ground(:ground)
65-
66-
function connect(ps...)
58+
function Inductor(; name, L = 1.0)
59+
val = L
60+
@named p = Pin()
61+
@named n = Pin()
62+
@variables v(t) i(t)
63+
@parameters L
64+
D = Differential(t)
6765
eqs = [
68-
0 ~ sum(p->p.i, ps) # KCL
66+
v ~ p.v - n.v
67+
0 ~ p.i + n.i
68+
i ~ p.i
69+
D(i) ~ v / L
6970
]
70-
# KVL
71-
for i in 1:length(ps)-1
72-
push!(eqs, ps[i].v ~ ps[i+1].v)
73-
end
74-
75-
return eqs
71+
ODESystem(eqs, t, [v, i], [L], systems=[p, n], defaults=Dict(L => val), name=name)
7672
end
77-
rc_eqs = [
78-
connect(source.p, resistor.p)
79-
connect(resistor.n, capacitor.p)
80-
connect(capacitor.n, source.n, ground.g)
81-
]
82-
83-
rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground], name=:rc)

test/models/rc_model.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
include("electrical_components.jl")
2+
3+
R = 1.0
4+
C = 1.0
5+
V = 1.0
6+
resistor = Resistor(:resistor, R=R)
7+
capacitor = Capacitor(:capacitor, C=C)
8+
source = ConstantVoltage(:source, V=V)
9+
ground = Ground(:ground)
10+
11+
function connect(ps...)
12+
eqs = [
13+
0 ~ sum(p->p.i, ps) # KCL
14+
]
15+
# KVL
16+
for i in 1:length(ps)-1
17+
push!(eqs, ps[i].v ~ ps[i+1].v)
18+
end
19+
20+
return eqs
21+
end
22+
rc_eqs = [
23+
connect(source.p, resistor.p)
24+
connect(resistor.n, capacitor.p)
25+
connect(capacitor.n, source.n, ground.g)
26+
]
27+
28+
rc_model = ODESystem(rc_eqs, t, systems=[resistor, capacitor, source, ground], name=:rc)

test/print_tree.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ModelingToolkit, AbstractTrees, Test
22

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

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

0 commit comments

Comments
 (0)