
这是一个RCL串并联基础电路模型。
1.主要组成部分:
Pin(连接器):
定义电路的接口,包含电压V和电流I(流量属性flow表示电流遵循KCL,电流之和为零)。
TwoPin(二端口组件):
基础电路元件的抽象类,扩展电阻、电感和电容等模型。定义了两个端口p和n,以及电流和电压的基本关系。
Resistor(电阻模型):
扩展TwoPin,定义了电阻R和电流、电压的关系。
Capacitor(电容模型):
使用微分方程描述电容特性,C * der(v) = i 表示电流等于电容电压的变化率乘以电容值。
Inductor(电感模型):
电感使用微分方程表示,v = L * der(i) 表示电感两端的电压等于电感电流的变化率乘以电感值。
VsourceAC(交流电源):
交流电源提供正弦波电压,方程v = VA * sin(2 * PI * f * time)表示交流电压随时间的变化。
Ground(接地):
定义接地点,将电压固定为0。
2. 电路搭建解析:
在模型实例部分,电路由以下元件组成:
交流电源(VsourceAC)
电阻R1和R2(10Ω)
电感L(0.1H)
电容C(0.01F)
接地Ground
connect(R1.p, AC.p):R1的一端连接交流电源正端,提供电流路径。
connect(R1.n, C.p):R1另一端连接电容C的正端。
connect(R2.p, R1.p):R2的正端与R1并联。
connect(R2.n, L.p):R2负端连接电感L正端,形成分支。
connect(C.n, AC.n):电容负端连接到电源负端,完成回路。
connect(L.n, C.n):电感负端连接到电容负端,形成串联分支。
connect(ground.p, AC.n):电源负端接地。
3. 模型组件库:
NGM中建立的模型组件可以保存显示在左侧库浏览器中,方便的拖动实例,实现模型组件复用。

4.仿真运行:

5.模型代码:
model RCL
connector Pin
Real V;
flow Real I;
annotation (Diagram(graphics={ Rectangle(
extent = {{-40, 40}, {40, -40}},
lineColor = {0, 0, 255},
fillColor = {0, 0, 255},
fillPattern = FillPattern.Solid)}), Icon(graphics={
Rectangle(
extent = {{-100, 100}, {100, -100}},
lineColor = {0, 0, 255},
fillColor = {0, 0, 255},
fillPattern = FillPattern.Solid)}));
end Pin;
partial model TwoPin
Real v;
Real i;
Pin p annotation (Placement(transformation(extent = {{-100, 0}, {-80, 20}}),
iconTransformation(extent = {{-100, 0}, {-80, 20}})));
Pin n annotation (Placement(transformation(extent = {{80, 0}, {100, 20}}),
iconTransformation(extent = {{80, 0}, {100, 20}})));
equation
v = p.V - n.V;
0 = p.I + n.I;
i = p.I;
end TwoPin;
model Resistor1 = Resistor(R=10);
model Resistor
"Ideal electrical resistor"
extends TwoPin;
parameter Real R(unit = "Ohm")
"Resistance";
equation
R * i = v;
annotation (Icon(graphics={
Rectangle(
extent = {{-70, 40}, {70, -20}},
lineColor = {0, 0, 255},
fillColor = {255, 255, 255},
fillPattern = FillPattern.Solid),
Line(points = {{-80, 10}, {-70, 10}}, color = {28, 108, 200}),
Line(points = {{70, 10}, {80, 10}}, color = {28, 108, 200}),
Text(
extent = {{-144, 92}, {156, 52}},
textString = "%name",
textColor = {0, 0, 255})}));
end Resistor;
model Capacitor
extends TwoPin;
parameter Real C(unit = "F")
"Capacitance";
equation
C * der(v) = i;
annotation (Icon(graphics={
Line(
points = {{-20, 60}, {-20, -40}},
color = {28, 108, 200},
thickness = 1),
Line(
points = {{20, 60}, {20, -40}},
color = {28, 108, 200},
thickness = 1),
Line(
points = {{-80, 12}, {-20, 12}},
color = {28, 108, 200},
thickness = 1),
Line(
points = {{20, 12}, {80, 12}},
color = {28, 108, 200},
thickness = 1),
Text(
extent = {{-144, 104}, {156, 64}},
textString = "%name",
textColor = {0, 0, 255})}));
end Capacitor;
model Inductor
extends TwoPin;
parameter Real L(unit = "H")
"Inductance";
equation
v = L * der(i);
annotation (Icon(graphics={
Line(points = {{60, 10}, {80, 10}},
color = {0, 0, 255}),
Line(points = {{-80, 10}, {-60, 10}},
color = {0, 0, 255}),
Line(
points = {{-60, 10}, {-59, 16}, {-52, 24}, {-38, 24}, {-31, 16}, {-30, 10}},
color = {0, 0, 255},
smooth = Smooth.Bezier),
Line(
points = {{-30, 10}, {-29, 16}, {-22, 24}, {-8, 24}, {-1, 16}, {0, 10}},
color = {0, 0, 255},
smooth = Smooth.Bezier),
Line(
points = {{0, 10}, {1, 16}, {8, 24}, {22, 24}, {29, 16}, {30, 10}},
color = {0, 0, 255},
smooth = Smooth.Bezier),
Line(
points = {{30, 10}, {31, 16}, {38, 24}, {52, 24}, {59, 16}, {60, 10}},
color = {0, 0, 255},
smooth = Smooth.Bezier),
Text(
extent = {{-146, 84}, {154, 44}},
textString = "%name",
textColor = {0, 0, 255})}));
end Inductor;
model VsourceAC
"Sin-wave voltage source"
extends TwoPin;
parameter Real VA = 220
"Amplitude";
parameter Real f(unit = "Hz") = 50
"Frequency";
constant Real PI = 3.141592653589793;
equation
v = VA * sin(2 * PI * f * time);
annotation (Icon(graphics={
Ellipse(
extent = {{-50, 60}, {50, -40}},
lineColor = {0, 0, 255},
fillColor = {255, 255, 255},
fillPattern = FillPattern.Solid),
Text(
extent = {{-150, 70}, {150, 110}},
textString = "%name",
textColor = {0, 0, 255}),
Line(points = {{-80, 10}, {80, 10}},
color = {0, 0, 255}),
Line(points = {{-80, 30}, {-60, 30}}, color = {0, 0, 255}),
Line(points = {{-70, 40}, {-70, 20}}, color = {0, 0, 255}),
Line(points = {{60, 30}, {80, 30}}, color = {0, 0, 255}),
Line(points = {{-70, -60}, {0, -60}, {0, 80}, {69, 80}},
color = {192, 192, 192})}));
end VsourceAC;
model Ground
"Ground"
Pin p annotation (Placement(transformation(extent = {{-10, 40}, {10, 60}}),
iconTransformation(extent = {{-10, 40}, {10, 60}})));
equation
p.V = 0;
annotation (Icon(graphics={
Line(points = {{-60, 0}, {60, 0}}, color = {0, 0, 255}),
Line(points = {{-40, -20}, {40, -20}},
color = {0, 0, 255}),
Line(points = {{-20, -40}, {20, -40}},
color = {0, 0, 255}),
Line(points = {{0, 40}, {0, 0}}, color = {0, 0, 255}),
Text(
extent = {{-150, -60}, {150, -100}},
textString = "%name",
textColor = {0, 0, 255})}));
end Ground;
Ground G;
Resistor R1( R = 10) annotation (Placement(transformation(extent={{-10,-10},{10,10}},origin={-14.0838,27.7397})));
Capacitor C(C = 0.01) annotation (Placement(transformation(extent = {{-10, -10}, {10,
10}}, origin={18,26})));
Inductor L(L = 0.1)
annotation (Placement(transformation(extent={{-10,-10},{10,10}}, origin={18,50})));
VsourceAC AC
annotation (Placement(transformation(extent = {{-10, -10}, {10, 10}}, origin = {0, -20})));
Ground ground annotation (Placement(transformation(extent={{-10,-10},{10,10}},origin={32,-56})));
Resistor R2( R = 10) annotation (Placement(transformation(extent={{-10,-10},{10,10}},origin={-14,50})));
equation
connect(R1.p,AC.p) annotation(Line(
points={{-23.0838,28.7397},{-23.0838,-19},{-9,-19}},
color={0,0,255}));
connect(R1.n,C.p) annotation(Line(
points={{-5.0838,28.7397},{9,28.7397},{9,27}},
color={0,0,255}));
connect(R2.n,L.p) annotation(Line(
points={{-5,51},{9,51}},
color={0,0,255}));
connect(R2.p,R1.p) annotation(Line(
points={{-23,51},{-23,28.7397},{-23.0838,28.7397}},
color={0,0,255}));
connect(C.n, AC.n) annotation (Line(points={{27,27},{32,27},{32,-19},{9,-19}},
color={0,0,255}));
connect(L.n, C.n) annotation (Line(points={{27,51},{32,51},{32,27},{27,27}},
color={0,0,255}));
connect(ground.p, AC.n)
annotation (Line(points={{32,-51},{32,-19},{9,-19}}, color={0,0,255}));
annotation(Icon(graphics={Ellipse(
extent={{-92,94},{94,-88}},
lineColor={ 28, 108,200},
lineThickness=1,
fillColor={ 255, 255,255},
fillPattern=FillPattern.Solid),
Rectangle(
extent={{28,72},{50,-64}},
lineColor={ 28, 108,200},
lineThickness=1,
fillColor={ 12, 121,255},
fillPattern=FillPattern.Solid),
Polygon(
points={{-28,72},{-8,72},{30,-64},{8,-64},{-28,72}},
lineColor={ 28, 108,200},
lineThickness=1,
fillColor={ 12, 121,255},
fillPattern=FillPattern.Solid),
Rectangle(
extent={{-50,72},{-28,-64}},
lineColor={ 28, 108,200},
lineThickness=1,
fillColor={ 12, 121,255},
fillPattern=FillPattern.Solid)}));
end RCL;