我在 OpenModelica 中创建了一个最小示例(直流电压、负载)。之后,我创建了一个 FMU 并通过 Python 使用它。
问题摘要:仅使用电阻器作为负载,我无法通过 python 更改参数。在模型中添加一个电感器后,我可以更改一些参数,并且仿真会对这些更改做出反应。
在这个简单的模型中,没有什么可能导致电气工程部分出现问题,问题一定出在其他地方。
模型设置
有问题的模型:
model testbench
parameter Real v_DC (start = 100);
Modelica.Blocks.Sources.RealExpression realExpression(y = v_DC) annotation(
Placement(visible = true, transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Sources.SignalVoltage signalVoltage annotation(
Placement(visible = true, transformation(origin = {-30, 46}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Basic.Resistor resistor(R = 10) annotation(
Placement(visible = true, transformation(origin = {-30, 14}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Basic.Ground ground annotation(
Placement(visible = true, transformation(origin = {28, 28}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(realExpression.y, signalVoltage.v) annotation(
Line(points = {{-58, 70}, {-30, 70}, {-30, 58}, {-30, 58}}, color = {0, 0, 127}));
connect(signalVoltage.n, resistor.n) annotation(
Line(points = {{-20, 46}, {-6, 46}, {-6, 14}, {-20, 14}, {-20, 14}}, color = {0, 0, 255}));
connect(signalVoltage.n, ground.p) annotation(
Line(points = {{-20, 46}, {28, 46}, {28, 38}, {28, 38}}, color = {0, 0, 255}));
connect(resistor.p, signalVoltage.p) annotation(
Line(points = {{-40, 14}, {-40, 14}, {-40, 46}, {-40, 46}}, color = {0, 0, 255}));
annotation(
uses(Modelica(version = "3.2.3")));
end testbench;
部分工作模式:
model testbench
parameter Real v_DC (start = 100);
Modelica.Blocks.Sources.RealExpression realExpression(y = v_DC) annotation(
Placement(visible = true, transformation(origin = {-70, 70}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Sources.SignalVoltage signalVoltage annotation(
Placement(visible = true, transformation(origin = {-30, 46}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Basic.Resistor resistor(R = 10) annotation(
Placement(visible = true, transformation(origin = {-30, 14}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Basic.Ground ground annotation(
Placement(visible = true, transformation(origin = {28, 28}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Electrical.Analog.Basic.Inductor inductor annotation(
Placement(visible = true, transformation(origin = {-60, 14}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
connect(realExpression.y, signalVoltage.v) annotation(
Line(points = {{-58, 70}, {-30, 70}, {-30, 58}, {-30, 58}}, color = {0, 0, 127}));
connect(signalVoltage.n, resistor.n) annotation(
Line(points = {{-20, 46}, {-6, 46}, {-6, 14}, {-20, 14}, {-20, 14}}, color = {0, 0, 255}));
connect(signalVoltage.n, ground.p) annotation(
Line(points = {{-20, 46}, {28, 46}, {28, 38}, {28, 38}}, color = {0, 0, 255}));
connect(inductor.n, resistor.p) annotation(
Line(points = {{-50, 14}, {-40, 14}, {-40, 14}, {-40, 14}}, color = {0, 0, 255}));
connect(signalVoltage.p, inductor.p) annotation(
Line(points = {{-40, 46}, {-74, 46}, {-74, 14}, {-70, 14}, {-70, 14}}, color = {0, 0, 255}));
annotation(
uses(Modelica(version = "3.2.3")));
end testbench;
二手软件
- 视窗 10
- OpenModelica 1.16.0
- PyFMI 2.5
- 蜘蛛 3.3.6
FMU 创建
使用具有以下设置的 .mos 文件创建了 FMU:
OpenModelica.Scripting.loadFile("testbench.mo"); getErrorString();
setCommandLineOptions("-d=newInst"); getErrorString();
setCommandLineOptions("-d=initialization"); getErrorString();
setCommandLineOptions("--simCodeTarget=Cpp"); getErrorString();
setCommandLineOptions("-d=-disableDirectionalDerivatives"); getErrorString();
OpenModelica.Scripting.translateModelFMU(testbench, version="2.0", fmuType = "me"); getErrorString();
用于测试的 Python 代码
import pylab as P
import numpy as N
from pyfmi import load_fmu
def run_demo(with_plots=True):
model = load_fmu('testbench.fmu')
Tstart = 0.0 #
Tend = 0.1
dt = 0.0001
model.setup_experiment(start_time = Tstart)
model.enter_initialization_mode()
model.exit_initialization_mode()
time = Tstart
model.enter_continuous_time_mode()
x = model.continuous_states
model.set('resistor.R', 1000)
model.set('v_DC', 5700)
vref = [model.get_variable_valueref('resistor.i')]
vref2 = [model.get_variable_valueref('resistor.R')]
t_sol = [Tstart]
sol = [model.get_real(vref)]
sol2 = [model.get_real(vref2)]
while time < Tend:
dx = model.get_derivatives()
time = time + dt
model.time = time
x = x + dt*dx
model.continuous_states = x
t_sol += [time]
sol += [model.get_real(vref)]
sol2 += [model.get_real(vref2)]
if with_plots:
P.figure(1)
P.plot(t_sol,N.array(sol)[:,0])
P.title(model.get_name())
P.ylabel('Current at Resistor1 (A)')
P.xlabel('Time (s)')
P.figure(2)
P.plot(t_sol,N.array(sol2)[:,0])
P.title(model.get_name())
P.ylabel('Resistor1 (Ohm)')
P.xlabel('Time (s)')
if __name__ == "__main__":
run_demo()
错误
在第二个模型(使用电感器)中,直流电压的变化 (model.set('v_DC', 5700)) 会导致电阻器 1 处的电流成比例变化。这可以正常工作。
电阻值的变化 (model.set('resistor.R', 10000)) 不会以任何方式影响电阻器中的电流。打印的值显示了实际设置的值(参见第二张图),但是对于电流的计算,使用的是在 OpenModelica 中选择的默认值。
在第一个模型(没有电感器)中,直流电压的变化和电阻器的变化都不会影响电阻器中的电流,尽管应该。
在其他模型中,改变电阻值可以达到要求的结果,我们很难设置源电压。
设置参数的方式是否有错误,OpenModelica 中的错误,PyFMI 中的错误,或者这里发生了什么?