Partial Classes Describe Common Properties

1 Types from Modelica.SIunits

x
 
1
type Voltage = Real(unit = "V");
2

2
 
1
type Current = Real(unit = "A");
2

2 Electrical Connectors

5
 
1
connector Pin
2
  Voltage v;
3
  flow Current i;
4
end Pin;
5

3 Components

3.1 TwoPin

The class prefix partial is used to indicate that a class is incomplete such that it cannot be instatiated. The class TwoPin can be used to define our own Resistor, Capacitor, Inductor, Voltage source AC and Ground classes.


10
 
1
partial class TwoPin        "Superclass of elements with two electrical pins"
2
  Pin p, n;
3
  Voltage v;
4
  Current i;
5
equation
6
  v = p.v - n.v;
7
  p.i + n.i = 0;
8
  i = p.i;
9
end TwoPin;
10

3.2 Resistor

7
 
1
class Resistor             "Ideal electrical Resistor"
2
  extends TwoPin;
3
  parameter Real R(unit = "Ohm")   "Resistance";
4
equation
5
  R*i = v;
6
end Resistor;
7

3.3 Capacitor

7
 
1
class Capacitor           "Ideal electrical Capacitor"
2
  extends TwoPin;
3
  parameter Real C(unit = "F")     "Capacitance";
4
equation
5
  C*der(v) = i;
6
end Capacitor;
7

3.4 Inductor

7
 
1
class Inductor             "Ideal electrical Inductor"
2
  extends TwoPin;
3
  parameter Real L(unit = "H")     "Inductance";
4
equation
5
  v = L*der(i);
6
end Inductor;
7

3.5 VsourceAC

11
 
1
class VsourceAC           "Sin-wave voltage source"
2
  extends TwoPin;
3
  parameter Voltage VA      = 220 "Amplitude";
4
  parameter Real f(unit = "Hz") = 50 "Frequency";
5
  constant Real PI        = 3.141592653589793;
6
  input    Voltage u;
7
equation
8
  v = u;
9
  u = VA*sin(2*PI*f*time);
10
end VsourceAC;
11

3.6 Ground

6
 
1
class Ground
2
  Pin p;
3
equation
4
  p.v = 0;
5
end Ground;
6

4 SimpleCircuit

Based on these classes we can declare the Simple Circuit Model:


17
 
1
class SimpleCircuit
2
  Resistor     R1(R = 10);
3
  Capacitor   C(C = 0.01, v(fixed = true));
4
  Resistor     R2(R = 100);
5
  Inductor     L(L = 0.1, i(fixed = true));
6
  VsourceAC   AC(f = 1);
7
  Ground     G;
8
equation
9
  connect(AC.p, R1.p);      // 1, Capacitor circuit
10
  connect(R1.n, C.p);      //    Wire 2
11
  connect(C.n, AC.n);      //     Wire 3
12
  connect(R1.p, R2.p);      // 2, Inductor circuit
13
  connect(R2.n, L.p);      //    Wire 5
14
  connect(L.n, C.n);      //     Wire 6
15
  connect(AC.n, G.p);      // 7, Ground
16
end SimpleCircuit;
17

5 SimpleCircuit Simulation

2
 
1
simulate( SimpleCircuit )
2

2
 
1
plot( {R1.v, R2.v } )
2

R1.v
R2.v
-200
-100
0
100
200
0
0.2
0.4
0.6
0.8

2
 
1
plot( {R1.i,R2.i} )
2

R1.i
R2.i
-10
-5
0
5
10
0
0.2
0.4
0.6
0.8