|
| 1 | +using ModelingToolkit, Test |
| 2 | + |
| 3 | +@connector RealInput begin u(t), [input = true] end |
| 4 | +@connector RealOutput begin u(t), [output = true] end |
| 5 | +@model Constant begin |
| 6 | + @components begin output = RealOutput() end |
| 7 | + @parameters begin k, [description = "Constant output value of block"] end |
| 8 | + @equations begin output.u ~ k end |
| 9 | +end |
| 10 | + |
| 11 | +@variables t |
| 12 | +D = Differential(t) |
| 13 | + |
| 14 | +@connector Pin begin |
| 15 | + v(t) = 0 # Potential at the pin [V] |
| 16 | + i(t), [connect = Flow] # Current flowing into the pin [A] |
| 17 | +end |
| 18 | + |
| 19 | +@model OnePort begin |
| 20 | + @components begin |
| 21 | + p = Pin() |
| 22 | + n = Pin() |
| 23 | + end |
| 24 | + @variables begin |
| 25 | + v(t) |
| 26 | + i(t) |
| 27 | + end |
| 28 | + @equations begin |
| 29 | + v ~ p.v - n.v |
| 30 | + 0 ~ p.i + n.i |
| 31 | + i ~ p.i |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +@model Ground begin |
| 36 | + @components begin g = Pin() end |
| 37 | + @equations begin g.v ~ 0 end |
| 38 | +end |
| 39 | + |
| 40 | +@model Resistor begin |
| 41 | + @extend v, i = oneport = OnePort() |
| 42 | + @parameters begin R = 1 end |
| 43 | + @equations begin v ~ i * R end |
| 44 | +end |
| 45 | + |
| 46 | +@model Capacitor begin |
| 47 | + @extend v, i = oneport = OnePort() |
| 48 | + @parameters begin C = 1 end |
| 49 | + @equations begin D(v) ~ i / C end |
| 50 | +end |
| 51 | + |
| 52 | +@model Voltage begin |
| 53 | + @extend v, i = oneport = OnePort() |
| 54 | + @components begin V = RealInput() end |
| 55 | + @equations begin v ~ V.u end |
| 56 | +end |
| 57 | + |
| 58 | +@model RC begin |
| 59 | + @components begin |
| 60 | + resistor = Resistor() |
| 61 | + capacitor = Capacitor() |
| 62 | + source = Voltage() |
| 63 | + constant = Constant() |
| 64 | + ground = Ground() |
| 65 | + end |
| 66 | + @equations begin |
| 67 | + connect(constant.output, source.V) |
| 68 | + connect(source.p, resistor.p) |
| 69 | + connect(resistor.n, capacitor.p) |
| 70 | + connect(capacitor.n, source.n, ground.g) |
| 71 | + end |
| 72 | +end |
| 73 | +@named rc = RC() |
| 74 | +@test length(equations(structural_simplify(rc))) == 1 |
0 commit comments