使用Engee物理建模语言创建复杂组件
Engee物理建模语言不仅允许您开发自己的组件,还可以将不同的组件组合成一个整体。 这样的组件被称为复合材料。
它就像一个构造函数,当有零件(组件)时,我们的任务是按照正确的顺序连接它们。
为什么要制造复合组件?
*将多个组件"打包"为一个(作为常规建模中的子系统)
*组件数组
复合组件的示例
作为复合部件的例子,考虑具有外部轴负载的直流电机。
为了确保组件正确组装,让我们从非定向块库中获取基元的参考实现。:
为了进行测试,我们将模拟以下场景:在运行的前0.1秒,发动机在轴上无负载运行,并达到最大速度,然后在轴上发生负载。
创建复合组件
合成组件的创建方式与使用Engee物理建模语言的自定义组件相同。 因此,组件代码必须包含在*中。ngpc文件位于Engee搜索路径上.
让我们来看看DCMotorComosite中的组件代码。ngpc文件:
@engeemodel DCMotorComposite begin
@components begin
p = EngeePhysicalFoundation.Electrical.Pin(), [view = ("+", "left")]
n = EngeePhysicalFoundation.Electrical.Pin(), [view = ("-", "left")]
r = EngeePhysicalFoundation.Mechanical.Rotational.Flange(), [view = ("R", "right")]
c = EngeePhysicalFoundation.Mechanical.Rotational.Flange(), [view = ("C", "right")]
end
@parameters begin
rotor_resistance = 3.9, [unit="Ohm"]
rotor_inductance = 12e-6, [unit="H"]
motor_inertia = 0.01, [unit="g*cm^2"]
breakaway_torque = 0.02e-3, [unit="N*m"]
coulomb_torque = 0.02e-3, [unit="N*m"]
breakaway_velocity = 0.03347, [unit="rad/s"]
back_emf_constant = 0.072e-3*60/(2*pi), [unit="V/(rad/s)"]
viscous_coefficient = 0.0, [unit="N*m/(rad/s)"]
end
@components [gui = None] begin
rotorResistor = EngeePhysicalFoundation.Electrical.Elements.Resistor(R=default(rotor_resistance))
rotorInductor = EngeePhysicalFoundation.Electrical.Elements.Inductor(L=default(rotor_inductance))
motorInertia = EngeePhysicalFoundation.Mechanical.Rotational.Elements.Inertia(I=default(motor_inertia))
friction = EngeePhysicalFoundation.Mechanical.Rotational.Elements.Friction(w_breakaway=default(breakaway_velocity),T_breakaway=default(breakaway_torque),T_coulomb=default(coulomb_torque), viscous_coefficient=default(viscous_coefficient))
rotEMechConverter = EngeePhysicalFoundation.Electrical.Elements.RotationalConverter(k=default(back_emf_constant))
end
@equations begin
connect(p, rotorResistor.p)
connect(rotorResistor.n, rotorInductor.p)
connect(rotorInductor.n, rotEMechConverter.p)
connect(rotEMechConverter.n, n)
connect(rotEMechConverter.rod_flange, friction.rod_flange, motorInertia.flange, r)
connect(rotEMechConverter.case_flange, friction.case_flange, c)
end
end
注意我们如何创建子组件-它们包含在部分中 @components. 我们用部分中的值初始化它们的参数 @parameters
子组件组装成一个单一的组件发生在节 @equations 使用函数 connect(). 请注意,您可以一次指定多个连接端口!
测试复合组件
让我们检查我们的复合组件相对于相同模型的模拟结果,但使用基元。
In [ ]:
demoroot = @__DIR__
engee.addpath(demoroot)
mdl = engee.open(joinpath(demoroot,"DCComposite.engee"))
sim_res = engee.run(mdl);
让我们绘制仿真结果。:
In [ ]:
using Plots
CustC = collect(sim_res["Cust"]);
Primitives = collect(sim_res["Prim"]);
plot(CustC.time,CustC.value, label = "自定义组件")
plot!(Primitives.time,Primitives.value, label = "积木图书馆")
plot!(legend=:outerbottom,legendcolumns=2)
Out[0]:
让我们找到最大绝对误差:
In [ ]:
err = CustC.value .- Primitives.value;
println("最大绝对误差:∞(maximum(err))")
仿真结果实际上是匹配的,这意味着我们的组件工作正常!
结论
Engee物理建模语言不仅仅是一种为一维建模编写自定义组件的语言,也是一种创建和描述复杂物理系统的语言。 与此同时,保留了代码的可读性,这对于复杂系统很重要!