PyFMI:有时无法设置参数(取决于型号)

2024-05-23 17:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在OpenModelica中创建了一个最小的示例(直流电压、负载)。之后,我创建了一个FMU并通过Python使用它

问题摘要:仅使用电阻器作为负载,我无法通过python更改参数。在模型中添加一个电感器后,我可以更改一些参数,并且模拟会对这些更改做出反应

在这个简单的模型中,没有任何东西会引起电气工程部分的故障,问题一定在其他地方

模型设置

问题模型:

enter image description here

 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;

部分工作模式:

enter image description here

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
  • Spyder 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中是否存在错误,或者这里发生了什么


Tags: truemodelconnectlineannotationoriginextentplacement