Opened 4 years ago

Closed 4 years ago

#6058 closed defect (fixed)

CVODE FMI variables stuck

Reported by: federico.terraneo@… Owned by: Andreas Heuermann
Priority: high Milestone: 1.16.0
Component: FMI Version: v1.16.0-dev
Keywords: Cc: Francesco Casella, albertoleva

Description

I've been trying the CVODE with FMI and I think I found a bug.
The issue can be replicated with this simple example:

https://github.com/fedetft/fmi-interface-cpp

by adding the line

setCommandLineOptions("-d=initialization --fmiFlags=s:cvode");

to the top of buildfmi.mos

(compiled with mkdir build; cd build; cmake ..; make)

As can be seen from the attached figure, the expected behavior is in black and is produced by an FMI with the default explicit solver, while the behavior in red is observed with CVODE.

It's as if at some point the readback of the variables through the FMI interface gets stuck and the same value is returned in the subsequent steps.

Attachments (1)

bug.png (7.6 KB ) - added by federico.terraneo@… 4 years ago.

Download all attachments as: .zip

Change History (7)

by federico.terraneo@…, 4 years ago

Attachment: bug.png added

comment:1 by Francesco Casella, 4 years ago

Milestone: Future1.16.0

If this is confirmed it is quite serious, we should fix it in 1.16.0 or in 1.16.1 immediately after the holidays.

comment:2 by Andreas Heuermann, 4 years ago

I'm trying to reproduce the problem with OMSimulator:

loadString("
package FmiTest
  model Model
    Modelica.Blocks.Interfaces.RealOutput i;
    Modelica.Blocks.Interfaces.RealInput v;
    Modelica.Electrical.Analog.Basic.GeneralVoltageToCurrentAdaptor adaptor(use_fder = false, use_pder = false);
    Modelica.Electrical.Analog.Basic.Ground gnd;
    Modelica.Electrical.Analog.Basic.Resistor r(R = 1000);
    Modelica.Electrical.Analog.Basic.Capacitor c(C = 100e-6);
  equation
    connect(r.n, c.p);
    connect(gnd.p, c.n);
    connect(r.p, adaptor.pin_p);
    connect(gnd.p, adaptor.pin_n);
    connect(v, adaptor.p);
    connect(adaptor.f, i);
  end Model;

  model inputValues
    Modelica.Blocks.Interfaces.RealOutput v;
  equation
    when time >= 0.5 then
      v = 1;
    end when;
  end inputValues;

  annotation(uses(Modelica(version =\"3.2.3\")));
end FmiTest;
"); getErrorString();

setCommandLineOptions("--fmiFlags=s:cvode"); getErrorString();

translateModelFMU(FmiTest.Model, version="2.0", fmuType="cs"); getErrorString();
translateModelFMU(FmiTest.inputValues, version="2.0", fmuType="cs"); getErrorString();

writeFile("runWitchOMSimulator.lua","
oms_setTempDirectory(\"./temp/\")
oms_newModel(\"model\")
oms_addSystem(\"model.root\", oms_system_wc)
-- instantiate FMUs
oms_addSubModel(\"model.root.system1\", \"FmiTest.inputValues.fmu\")
oms_addSubModel(\"model.root.system2\", \"FmiTest.Model.fmu\")
-- add connections
oms_addConnection(\"model.root.system1.v\", \"model.root.system2.v\")
-- simulation settings
oms_setResultFile(\"model\", \"results.mat\")
oms_setStopTime(\"model\", 1)
oms_setFixedStepSize(\"model.root\", 0.002)
oms_instantiate(\"model\")
oms_initialize(\"model\")
oms_simulate(\"model\")
oms_terminate(\"model\")
");
getErrorString();

system("OMSimulator runWitchOMSimulator.lua"); getErrorString();

and get a segmentation fault:

$ omc test.mos
true
""
true
""
"/home/aheuermann1/workspace/Testitesttest/ticket6058/FmiTest.Model.fmu"
"Notification: Automatically loaded package Modelica 3.2.3 due to uses annotation.
Notification: Automatically loaded package Complex 3.2.3 due to uses annotation.
Notification: Automatically loaded package ModelicaServices 3.2.3 due to uses annotation.
Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
"
"/home/aheuermann1/workspace/Testitesttest/ticket6058/FmiTest.inputValues.fmu"
"Warning: The initial conditions are not fully specified. For more information set -d=initialization. In OMEdit Tools->Options->Simulation->OMCFlags, in OMNotebook call setCommandLineOptions(\"-d=initialization\").
"
true
""
info:    Set temp directory to    "/home/aheuermann1/workspace/Testitesttest/ticket6058"
info:    Set working directory to "/home/aheuermann1/workspace/Testitesttest/ticket6058"
info:    Set temp directory to    "/home/aheuermann1/workspace/Testitesttest/ticket6058/temp"
info:    New model "model" with corresponding temp directory "/home/aheuermann1/workspace/Testitesttest/ticket6058/temp/model-v7uae1nd"
LOG_SOLVER        | info    | CVODE linear multistep method CV_BDF
LOG_SOLVER        | info    | CVODE maximum integration order CV_NEWTON
LOG_SOLVER        | info    | CVODE use equidistant time grid YES
LOG_SOLVER        | info    | CVODE Using relative error tolerance 1.000000e-06
LOG_SOLVER        | info    | CVODE uses internal dense numeric jacobian method
LOG_SOLVER        | info    | CVODE uses internal root finding method NO
LOG_SOLVER        | info    | CVODE maximum absolut step size 0
LOG_SOLVER        | info    | CVODE initial step size is set automatically
LOG_SOLVER        | info    | CVODE maximum integration order 5
LOG_SOLVER        | info    | CVODE maximum number of nonlinear convergence failures permitted during one step 10
LOG_SOLVER        | info    | CVODE BDF stability limit detection algorithm OFF
LOG_SOLVER        | info    | CVODE linear multistep method CV_BDF
LOG_SOLVER        | info    | CVODE maximum integration order CV_NEWTON
LOG_SOLVER        | info    | CVODE use equidistant time grid YES
Segmentation fault
139
""

I'm not sure if it's the cause for your problem but I'll start fixing that.

comment:3 by Andreas Heuermann, 4 years ago

I solved the segmentation fault, see #6082, and now I get wrong simulation results with CVODE as well.

comment:4 by Andreas Heuermann, 4 years ago

Okay, the problem is that data->simulationInfo->inputVars is not updated correctly for FMI. Every time data->callback->input_function(data, threadData); is called the input got reset to its start value 0.
We can only call this after calling externalInputUpdate(), which is not available (and needed?) for FMI.

Moving input_function() into #ifndef OMC_FMI_RUNTIME ... #endif as we did with externalInputUpdate() should solve the problem.

See PR #6691.

comment:5 by Andreas Heuermann, 4 years ago

Merged with https://github.com/OpenModelica/OpenModelica/commit/a9063124f48aab91f3e83e9f42867c8c22ffd355.

The changes are available in the next nightly build (7th of August 2020).
@federico.terraneo If this solves your issue you can close this ticket.

comment:6 by federico.terraneo@…, 4 years ago

Resolution: fixed
Status: newclosed

@AnHeuermann I tried with the latest nightly build and it works, both the simple example and the more complex models I've been working on. Thanks!

I would also like to take the opportunity to thank you for the great work on supporting CVODE with FMI!

Note: See TracTickets for help on using tickets.