@@ -12,34 +12,11 @@ electrical circuits:
12
12
``` julia
13
13
using ModelingToolkit, OrdinaryDiffEq
14
14
15
- function connect_pin (ps... )
16
- eqs = [
17
- 0 ~ sum (p-> p. i, ps) # KCL
18
- ]
19
- # KVL
20
- for i in 1 : length (ps)- 1
21
- push! (eqs, ps[i]. v ~ ps[i+ 1 ]. v)
22
- end
23
-
24
- return eqs
25
- end
26
-
27
- function connect_heat (ps... )
28
- eqs = [
29
- 0 ~ sum (p-> p. Q_flow, ps)
30
- ]
31
- for i in 1 : length (ps)- 1
32
- push! (eqs, ps[i]. T ~ ps[i+ 1 ]. T)
33
- end
34
-
35
- return eqs
36
- end
37
-
38
15
# Basic electric components
39
16
@variables t
40
17
const D = Differential (t)
41
- function Pin (;name)
42
- @variables v (t)= 1.0 i (t)= 1.0
18
+ @connector function Pin (;name)
19
+ @variables v (t)= 1.0 i (t)= 1.0 [connect = Flow]
43
20
ODESystem (Equation[], t, [v, i], [], name= name)
44
21
end
45
22
@@ -61,8 +38,8 @@ function ConstantVoltage(;name, V = 1.0)
61
38
compose (ODESystem (eqs, t, [], [V], name= name), p, n)
62
39
end
63
40
64
- function HeatPort (;name)
65
- @variables T (t)= 293.15 Q_flow (t)= 0.0
41
+ @connector function HeatPort (;name)
42
+ @variables T (t)= 293.15 Q_flow (t)= 0.0 [connect = Flow]
66
43
ODESystem (Equation[], t, [T, Q_flow], [], name= name)
67
44
end
68
45
@@ -120,10 +97,10 @@ function parallel_rc_model(i; name, source, ground, R, C)
120
97
heat_capacitor = HeatCapacitor (name= Symbol (:heat_capacitor , i))
121
98
122
99
rc_eqs = [
123
- connect_pin (source. p, resistor. p)
124
- connect_pin (resistor. n, capacitor. p)
125
- connect_pin (capacitor. n, source. n, ground. g)
126
- connect_heat (resistor. h, heat_capacitor. h)
100
+ connect (source. p, resistor. p)
101
+ connect (resistor. n, capacitor. p)
102
+ connect (capacitor. n, source. n, ground. g)
103
+ connect (resistor. h, heat_capacitor. h)
127
104
]
128
105
129
106
compose (ODESystem (rc_eqs, t, name= Symbol (name, i)),
@@ -139,8 +116,8 @@ we can connect a bunch of RC components as follows:
139
116
140
117
``` julia
141
118
V = 2.0
142
- source = ConstantVoltage (name = :source , V= V)
143
- ground = Ground (name = :ground )
119
+ @named source = ConstantVoltage (V= V)
120
+ @named ground = Ground ()
144
121
N = 50
145
122
Rs = 10 .^ range (0 , stop= - 4 , length= N)
146
123
Cs = 10 .^ range (- 3 , stop= 0 , length= N)
@@ -170,19 +147,9 @@ Yes, that's a good question! Let's investigate a little bit more what had happen
170
147
If you look at the system we defined:
171
148
172
149
``` julia
173
- equations (big_rc)
174
-
175
- 1051 - element Vector{Equation}:
176
- Differential(t)(E(t)) ~ rc10₊resistor10₊h₊Q_flow(t) + rc11₊resistor11₊h₊Q_flow(t) + rc12₊resistor12₊h₊Q_flow(t) + rc13₊resistor13₊h₊Q_flow(t) + rc14₊resistor14₊h₊Q_flow(t) + rc15₊resistor15₊h₊Q_flow(t) + rc16₊resistor16₊h₊Q_flow(t) + rc17₊resistor17₊h₊Q_flow(t) + rc18₊resistor18₊h₊Q_flow(t) + rc19₊resistor19₊h₊Q_flow(t) + rc1₊resistor1₊h₊Q_flow(t) + rc20₊resistor20₊h₊Q_flow(t) + rc21₊resistor21₊h₊Q_flow(t) + rc22₊resistor22₊h₊Q_flow(t) + rc23₊resistor23₊h₊Q_flow(t) + rc24₊resistor24₊h₊Q_flow(t) + rc25₊resistor25₊h₊Q_flow(t) + rc26₊resistor26₊h₊Q_flow(t) + rc27₊resistor27₊h₊Q_flow(t) + rc28₊resistor28₊h₊Q_flow(t) + rc29₊resistor29₊h₊Q_flow(t) + rc2₊resistor2₊h₊Q_flow(t) + rc30₊resistor30₊h₊Q_flow(t) + rc31₊resistor31₊h₊Q_flow(t) + rc32₊resistor32₊h₊Q_flow(t) + rc33₊resistor33₊h₊Q_flow(t) + rc34₊resistor34₊h₊Q_flow(t) + rc35₊resistor35₊h₊Q_flow(t) + rc36₊resistor36₊h₊Q_flow(t) + rc37₊resistor37₊h₊Q_flow(t) + rc38₊resistor38₊h₊Q_flow(t) + rc39₊resistor39₊h₊Q_flow(t) + rc3₊resistor3₊h₊Q_flow(t) + rc40₊resistor40₊h₊Q_flow(t) + rc41₊resistor41₊h₊Q_flow(t) + rc42₊resistor42₊h₊Q_flow(t) + rc43₊resistor43₊h₊Q_flow(t) + rc44₊resistor44₊h₊Q_flow(t) + rc45₊resistor45₊h₊Q_flow(t) + rc46₊resistor46₊h₊Q_flow(t) + rc47₊resistor47₊h₊Q_flow(t) + rc48₊resistor48₊h₊Q_flow(t) + rc49₊resistor49₊h₊Q_flow(t) + rc4₊resistor4₊h₊Q_flow(t) + rc50₊resistor50₊h₊Q_flow(t) + rc5₊resistor5₊h₊Q_flow(t) + rc6₊resistor6₊h₊Q_flow(t) + rc7₊resistor7₊h₊Q_flow(t) + rc8₊resistor8₊h₊Q_flow(t) + rc9₊resistor9₊h₊Q_flow(t)
177
- 0 ~ rc1₊resistor1₊p₊i (t) + rc1₊source₊p₊i (t)
178
- rc1₊source₊p₊v (t) ~ rc1₊resistor1₊p₊v (t)
179
- 0 ~ rc1₊capacitor1₊p₊i (t) + rc1₊resistor1₊n₊i (t)
180
- rc1₊resistor1₊n₊v (t) ~ rc1₊capacitor1₊p₊v (t)
181
- ⋮
182
- rc50₊source₊V ~ rc50₊source₊p₊v (t) - rc50₊source₊n₊v (t)
183
- 0 ~ rc50₊source₊n₊i (t) + rc50₊source₊p₊i (t)
184
- rc50₊ground₊g₊v (t) ~ 0
185
- Differential (t)(rc50₊heat_capacitor50₊h₊T (t)) ~ rc50₊heat_capacitor50₊h₊Q_flow (t)* (rc50₊heat_capacitor50₊V^- 1 )* (rc50₊heat_capacitor50₊cp^- 1 )* (rc50₊heat_capacitor50₊rho^- 1 )
150
+ length (equations (big_rc))
151
+
152
+ 801
186
153
```
187
154
188
155
You see it started as a massive 1051 set of equations. However, after eliminating
@@ -211,7 +178,7 @@ investigate what this means:
211
178
212
179
``` julia
213
180
using ModelingToolkit. BipartiteGraphs
214
- big_rc = initialize_system_structure (big_rc)
181
+ big_rc = initialize_system_structure (expand_connections ( big_rc) )
215
182
inc_org = BipartiteGraphs. incidence_matrix (structure (big_rc). graph)
216
183
blt_org = StructuralTransformations. sorted_incidence_matrix (big_rc, only_algeqs= true , only_algvars= true )
217
184
blt_reduced = StructuralTransformations. sorted_incidence_matrix (sys, only_algeqs= true , only_algvars= true )
0 commit comments