Opened 7 years ago

Closed 7 years ago

#4455 closed defect (fixed)

Unreasonable high deviation between imported FMU and original model

Reported by: Bernhard Thiele Owned by: Lennart Ochel
Priority: high Milestone: Future
Component: FMI Version:
Keywords: Cc: Lennart Ochel, Adrian Pop

Description

I wrote an exercise for exporting and importing model-exchange FMUs using OpenModelica and observed rather large deviations between the original model and the imported FMU which I can't explain. Exercise and underlying model are attached to this ticket.

The model below feeds the original PI controller with the same input as the FMU wrapped PI controller.


If I simulate that using standard settings (dassl, tolerance 1e-6) and plot the respective outputs, I get following result:


It is possible to get closer if one manually sets the step size and makes it very small (e.g., Euler with step size 0.0001s). The strange thing is, that the FMU is rather sensitive to changing the step size, e.g., for 0.001s it will be significantly different than for 0.0001s, while the result for the Modelica PI will not change significantly when doing this change.

Attachments (6)

screenshot1.png (15.1 KB ) - added by Bernhard Thiele 7 years ago.
plot1.png (14.3 KB ) - added by Bernhard Thiele 7 years ago.
FMIExercise.mo (11.8 KB ) - added by Bernhard Thiele 7 years ago.
FMIExercise.pdf (595.4 KB ) - added by Bernhard Thiele 7 years ago.
plot2.png (11.8 KB ) - added by Bernhard Thiele 7 years ago.
FMIExercise_Components_PI_me_FMU.mo (15.5 KB ) - added by Bernhard Thiele 7 years ago.
Manually changed import file which is more closely aligned to the pseudo code in the specification

Download all attachments as: .zip

Change History (10)

by Bernhard Thiele, 7 years ago

Attachment: screenshot1.png added

by Bernhard Thiele, 7 years ago

Attachment: plot1.png added

by Bernhard Thiele, 7 years ago

Attachment: FMIExercise.mo added

by Bernhard Thiele, 7 years ago

Attachment: FMIExercise.pdf added

comment:1 by Bernhard Thiele, 7 years ago

I looked at the generated Modelica import code (FMIExercise_Components_PI_me_FMU.mo) and tried to improve it.

The current import code doesn't enforce a particular ordering when setting the inputs. By using an algorithm section, as indicated in the diff below, it is possible to enforce that the input is set directly after the new time is set:

--- FMIExercise_Components_PI_me_FMU.mo 2017-06-27 15:41:59.379988359 +0200
***************
*** 25,27 ****
    Real flowStatesInputs;
!   Real fmi_input_u;
    Boolean callEventUpdate;
--- 25,27 ----
    Real flowStatesInputs;
!   Real fmi_input_u[1];
    Boolean callEventUpdate;
***************
*** 40,44 ****
  initial equation
  equation
-   flowTime = fmi2Functions.fmi2SetTime(fmi2me, time, flowInitialized);
-   {fmi_input_u} = fmi2Functions.fmi2SetReal(fmi2me, {2.0}, {u});
    flowStatesInputs = fmi2Functions.fmi2SetContinuousStates(fmi2me, fmi_x, flowParamsStart + flowTime);
--- 40,45 ----
  initial equation
+ algorithm
+   flowTime := fmi2Functions.fmi2SetTime(fmi2me, time, flowInitialized);
+   fmi_input_u := fmi2Functions.fmi2SetReal(fmi2me, {2.0}, {u});
  equation
    flowStatesInputs = fmi2Functions.fmi2SetContinuousStates(fmi2me, fmi_x, flowParamsStart + flowTime);

This gives a better result, e.g., simulation using euler with a step size of 0.001s results in:


Hence, it seems reasonable to change the importer if that improved results hold in general.

I also tried to programmatically change the logic so that it resembles more closely to the pseudo code given in the specification. The resulting file is attached. It works for this example, but misses more generic features (no event handling). Anyway, I will attach it, so that it is not lost.

by Bernhard Thiele, 7 years ago

Attachment: plot2.png added

by Bernhard Thiele, 7 years ago

Manually changed import file which is more closely aligned to the pseudo code in the specification

comment:2 by Bernhard Thiele, 7 years ago

Update: I have implemented the idea in PR https://github.com/OpenModelica/OMCompiler/pull/1712, but it needs some extra checking.

comment:3 by Bernhard Thiele, 7 years ago

Actually everything passes the testsuite, which surprises me a bit since some FMI tests failed when running them on my local machine (due to slighly different output to stdout).

https://test.openmodelica.org/hudson/job/OpenModelica_TEST_PULL_REQUEST/4372/

I think we can and should merge the PR.

@adrpo, @lochel: Do you have comments?

comment:4 by Bernhard Thiele, 7 years ago

Resolution: fixed
Status: newclosed

I merged the PR which should fix the issue.

Note: See TracTickets for help on using tickets.