type Voltage = Real(unit = "V");
type Current = Real(unit = "A");
connector Pin
Voltage v;
flow Current i;
end Pin;
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.
partial class TwoPin "Superclass of elements with two electrical pins"
Pin p, n;
Voltage v;
Current i;
equation
v = p.v - n.v;
p.i + n.i = 0;
i = p.i;
end TwoPin;
class Resistor "Ideal electrical Resistor"
extends TwoPin;
parameter Real R(unit = "Ohm") "Resistance";
equation
R*i = v;
end Resistor;
class Capacitor "Ideal electrical Capacitor"
extends TwoPin;
parameter Real C(unit = "F") "Capacitance";
equation
C*der(v) = i;
end Capacitor;
class Inductor "Ideal electrical Inductor"
extends TwoPin;
parameter Real L(unit = "H") "Inductance";
equation
v = L*der(i);
end Inductor;
class VsourceAC "Sin-wave voltage source"
extends TwoPin;
parameter Voltage VA = 220 "Amplitude";
parameter Real f(unit = "Hz") = 50 "Frequency";
constant Real PI = 3.141592653589793;
input Voltage u;
equation
v = u;
u = VA*sin(2*PI*f*time);
end VsourceAC;
class Ground
Pin p;
equation
p.v = 0;
end Ground;
Based on these classes we can declare the Simple Circuit Model:
class SimpleCircuit
Resistor R1(R = 10);
Capacitor C(C = 0.01, v(fixed = true));
Resistor R2(R = 100);
Inductor L(L = 0.1, i(fixed = true));
VsourceAC AC(f = 1);
Ground G;
equation
connect(AC.p, R1.p); // 1, Capacitor circuit
connect(R1.n, C.p); // Wire 2
connect(C.n, AC.n); // Wire 3
connect(R1.p, R2.p); // 2, Inductor circuit
connect(R2.n, L.p); // Wire 5
connect(L.n, C.n); // Wire 6
connect(AC.n, G.p); // 7, Ground
end SimpleCircuit;
simulate( SimpleCircuit )
plot( {R1.v, R2.v } )
plot( {R1.i,R2.i} )