PyFMI: Setting parameters sometimes not possible (model depending)

281 views Asked by At

I created a minimal example (DC-Voltage, Load) in OpenModelica. Afterwards, I created an FMU and used it via Python.

Problem summary: By using just a Resistor as a load, i canĀ“t change the parameters via python. After adding an Inductor to the model, I can change some parameters and the simulation reacts to this changes.

In this simple model, there is nothing which could cause troubles from the electrical engineering part, the problem must be somewhere else.

Model Set-up

Problematical Model:

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;

Partly working model:

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;

Used Software

  • Windows 10
  • OpenModelica 1.16.0
  • PyFMI 2.5
  • Spyder 3.3.6

FMU Creation

Created the FMU with a .mos file with the following settings:

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 code for testing

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()

Bugs

In the second model (with the inductor), a change of the DC voltage (model.set('v_DC', 5700)) leads to a proportional change of the current at resistor1. This works as it should.

A change of the value of the resistor (model.set('resistor.R', 10000)) does not affect the current in the resistor in any way. The printing the value shows the one which was actually set (see second plotted figure), but for the calculation of the current, the default value which was selected in OpenModelica is used.

In the first model (without inductor), neither a change in the DC voltage nor a change in the Resistor affect the current in the resistor, though should.

Within other models, changing the value of the resistor works with the demanded result, and we had difficulties setting the voltage of the source.

Is there a mistake in the way of setting the parameters, a bug in OpenModelica, in PyFMI, or what is going on here?

0

There are 0 answers