# 电气部件¶

connector PositivePin "Positive pin of an electric component"
Modelica.SIunits.Voltage v "Potential at the pin";
flow Modelica.SIunits.Current i "Current flowing into the pin";
end PositivePin;

connector NegativePin "Negative pin of an electric component"
Modelica.SIunits.Voltage v "Potential at the pin";
flow Modelica.SIunits.Current i "Current flowing into the pin";
end NegativePin;


## 基本组件模型¶

within ModelicaByExample.Components.Electrical.VerboseApproach;
model Resistor "A resistor model"
parameter Modelica.SIunits.Resistance R;
Modelica.Electrical.Analog.Interfaces.PositivePin p
annotation (Placement(transformation(extent={{-110,-10},{-90,10}})));
Modelica.Electrical.Analog.Interfaces.NegativePin n
annotation (Placement(transformation(extent={{90,-10},{110,10}})));
protected
Modelica.SIunits.Voltage v = p.v-n.v;
equation
p.i + n.i = 0 "Conservation of charge";
v = p.i*R "Ohm's law";
end Resistor;


within ModelicaByExample.Components.Electrical.VerboseApproach;
model Inductor "An inductor model"
parameter Modelica.SIunits.Inductance L;
Modelica.Electrical.Analog.Interfaces.PositivePin p
annotation (Placement(transformation(extent={{-110,-10},{-90,10}})));
Modelica.Electrical.Analog.Interfaces.NegativePin n
annotation (Placement(transformation(extent={{90,-10},{110,10}})));
protected
Modelica.SIunits.Voltage v = p.v-n.v;
equation
p.i + n.i = 0 "Conservation of charge";
L*der(p.i) = p.v;
end Inductor;

within ModelicaByExample.Components.Electrical.VerboseApproach;
model Capacitor "A capacitor model"
parameter Modelica.SIunits.Capacitance C;
Modelica.Electrical.Analog.Interfaces.PositivePin p
annotation (Placement(transformation(extent={{-110,-10},{-90,10}})));
Modelica.Electrical.Analog.Interfaces.NegativePin n
annotation (Placement(transformation(extent={{90,-10},{110,10}})));
protected
Modelica.SIunits.Voltage v = p.v-n.v;
equation
p.i + n.i = 0 "Conservation of charge";
C*der(v) = p.i;
end Capacitor;


## DRY原则¶

within ModelicaByExample.Components.Electrical.DryApproach;
partial model TwoPin "Common elements of two pin electrical components"
Modelica.Electrical.Analog.Interfaces.PositivePin p
annotation (Placement(transformation(extent={{-110,-10},{-90,10}})));
Modelica.Electrical.Analog.Interfaces.NegativePin n
annotation (Placement(transformation(extent={{90,-10},{110,10}})));
protected
Modelica.SIunits.Voltage v = p.v-n.v;
Modelica.SIunits.Current i = p.i;
equation
p.i + n.i = 0 "Conservation of charge";
end TwoPin;


within ModelicaByExample.Components.Electrical.DryApproach;
model Resistor "A DRY resistor model"
parameter Modelica.SIunits.Resistance R;
extends TwoPin;
equation
v = i*R "Ohm's law";
end Resistor;


within ModelicaByExample.Components.Electrical.DryApproach;
model Capacitor "A DRY capacitor model"
parameter Modelica.SIunits.Capacitance C;
extends TwoPin;
equation
C*der(v) = i;
end Capacitor;

within ModelicaByExample.Components.Electrical.DryApproach;
model Inductor "A DRY inductor model"
parameter Modelica.SIunits.Inductance L;
extends TwoPin;
equation
L*der(i) = v;
end Inductor;


## 电路模型¶

within ModelicaByExample.Components.Electrical.DryApproach;
model StepVoltage "A DRY step voltage source"
parameter Modelica.SIunits.Voltage V0;
parameter Modelica.SIunits.Voltage Vf;
parameter Modelica.SIunits.Time stepTime;
extends TwoPin;
color={0,0,255},


within ModelicaByExample.Components.Electrical.DryApproach;
model Ground "Electrical ground"
Modelica.Electrical.Analog.Interfaces.PositivePin ground "Ground pin"
annotation (Placement(transformation(extent={{-10,70},{10,90}})));
equation
ground.v = 0;
end Ground;


Ground模型只有一个引脚，因此不能继承TwoPin。回想一下在讨论传热组件环境条件模型时，我们是如何描述无限大容器的。Ground模型提供了非常类似的功用。不管多少电流流入或流出电接地，电压保持为零。

within ModelicaByExample.Components.Electrical.Examples;
model SwitchedRLC "Recreation of the switched RLC circuit"
DryApproach.StepVoltage Vs(V0=0, Vf=24, stepTime=0.5)
annotation (Placement(transformation(
extent={{-10,-10},{10,10}},
rotation=270,
origin={-40,0})));
DryApproach.Inductor inductor(L=1)
annotation (Placement(transformation(extent={{-10,10},{10,30}})));
DryApproach.Capacitor capacitor(C=1e-3) annotation (Placement(transformation(
extent={{-10,-10},{10,10}},
rotation=90,
origin={30,0})));
DryApproach.Resistor resistor(R=100) annotation (Placement(transformation(
extent={{-10,-10},{10,10}},
rotation=90,
origin={60,2})));
DryApproach.Ground ground
annotation (Placement(transformation(extent={{-10,-38},{10,-18}})));
equation
connect(inductor.n, resistor.n) annotation (Line(
points={{10,20},{60,20},{60,12}},
color={0,0,255},
smooth=Smooth.None));
connect(capacitor.n, inductor.n) annotation (Line(
points={{30,10},{30,20},{10,20}},
color={0,0,255},
smooth=Smooth.None));
connect(inductor.p, Vs.p) annotation (Line(
points={{-10,20},{-40,20},{-40,10}},
color={0,0,255},
smooth=Smooth.None));
connect(capacitor.p, ground.ground) annotation (Line(
points={{30,-10},{30,-20},{0,-20}},
color={0,0,255},
smooth=Smooth.None));
connect(resistor.p, ground.ground) annotation (Line(
points={{60,-8},{60,-20},{0,-20}},
color={0,0,255},
smooth=Smooth.None));
connect(Vs.n, ground.ground) annotation (Line(
points={{-40,-10},{-40,-20},{0,-20}},
color={0,0,255},
smooth=Smooth.None));
end SwitchedRLC;