Hello there,

Quickly following my previous post, I also intend to show some actual results here, as everything up till now was mostly theory and implementation details.

Taking the example of previously described “chaos” block, we can see the output during Modelica blocks compilation and simulation using the new OpenModelica support.

  class chaos
  input Real eps,gamma;
  Real x(start=0), y(start=1);
  equation
  der(x)=y;
  der(y)=x-x^3-eps*y+gamma*cos(time);
  end chaos;
  

(Corresponding Modelica code)

Building the Block

Some text output commands were added to the different Scilab scripts (.sci) involved in Modelica blocks generation, so that we can get some information after running the “chaos” model demonstration:

(Scilab console output during Modelica code generation, flattening, FMU packaging and C library linkage)

(Modelica block properties taken from XML description file)

After that, we can also see the output inside current temporary files folder:

$ ls /tmp/SCI_TMP_4662_B7K5x5/
Chaos_Modelica_im/  Chaos_Modelica_im.c    Chaos_Modelica_im_FMU.libs      Chaos_Modelica_im_info.json  Chaos_Modelica_im.mo   libChaos_Modelica_im.so*
modelica/           Chaos_Modelica_imf.mo  Chaos_Modelica_im_FMU.log       Chaos_Modelica_im.libs       Chaos_Modelica_im.mos  loader.sce
Workspace/          Chaos_Modelica_im.fmu  Chaos_Modelica_im_FMU.makefile  Chaos_Modelica_im.log        cleaner.sce            unix.err

containing the main Modelica block code (Chaos_Modelica_im.mo):

model Chaos_Modelica_im
    chaos 	chaos1;
    OutPutPort 	OutPutPort1;
    OutPutPort 	OutPutPort2;
    InPutPort 	InPutPort1;
    InPutPort 	InPutPort2;
equation
    chaos1.x = OutPutPort1.vi;
    chaos1.y = OutPutPort2.vi;
    InPutPort1.vo = chaos1.gamma;
    InPutPort2.vo = chaos1.eps;
end Chaos_Modelica_im;

and its flattened form (Chaos_Modelica_imf.mo), combined with the other source files:

class Chaos_Modelica_im
    Real chaos1__eps;
    Real chaos1__gamma;
    Real chaos1__x(start = 0.0);
    Real chaos1__y(start = 1.0);
    output Real OutPutPort1__vo;
    Real OutPutPort1__vi;
    output Real OutPutPort2__vo;
    Real OutPutPort2__vi;
    Real InPutPort1__vo;
    input Real InPutPort1__vi;
    Real InPutPort2__vo;
    input Real InPutPort2__vi;
equation
    der(chaos1__x) = chaos1__y;
    der(chaos1__y) = chaos1__x + chaos1__gamma * cos(time) + (-chaos1__eps) * chaos1__y - chaos1__x ^ 3.0;
    OutPutPort1__vi = OutPutPort1__vo;
    OutPutPort2__vi = OutPutPort2__vo;
    InPutPort1__vi = InPutPort1__vo;
    InPutPort2__vi = InPutPort2__vo;
    chaos1__x = OutPutPort1__vi;
    chaos1__y = OutPutPort2__vi;
    InPutPort1__vo = chaos1__gamma;
    InPutPort2__vo = chaos1__eps;
end Chaos_Modelica_im;

as well as the partially generated C code file (Chaos_Modelica_im.c) glue between FMI2 library (binaries/platform/Chaos_Modelica_im.so) and Scicos block API:

#include "fmi2Functions.h"
#include "fmi2FunctionTypes.h"
#include "fmi2TypesPlatform.h"

static fmi2Real inputsList[ 2 ] = { 0.0 };
static const fmi2ValueReference INPUT_REFS_LIST[ 2 ] = { 5,6 };
static const fmi2ValueReference STATE_REFS_LIST[ 2 ] = { 0,1 };
static fmi2Real stateDerivativesList[ 2 ] = { 0.0 };
static const fmi2ValueReference STATE_DER_REFS_LIST[ 2 ] = { 2,3 };
static fmi2Real outputsList[ 2 ] = { 0.0 };
static const fmi2ValueReference OUTPUT_REFS_LIST[ 2 ] = { 7,8 };
static fmi2Real parametersList[ 1 ] = { 0.0 };
static const fmi2ValueReference PARAMETER_REFS_LIST[ 1 ] = {  };

#define BLOCK_FUNCTION_NAME Chaos_Modelica_im
#define MODEL_NAME "Chaos_Modelica_im"
#define MODEL_GUID "{dede0425-3960-4087-a20c-59351a4e4695}"

#include "fmi2_wrapper.h"       // Generic wrapper code

Also, we get the unzipped/extracted FMU package (Chaos_Modelica_im.fmu) folder:

$ ls /tmp/SCI_TMP_4662_B7K5x5/Chaos_Modelica_im/
binaries/   Chaos_Modelica_im.c    config.guess*   config.sub*   depcomp*                 libtool*   Makefile.am    missing*
sources/    Chaos_Modelica_im.lo   config.log      configure*    install-sh*              ltmain.sh  Makefile.in    modelDescription.xml
aclocal.m4  compilerDetection.sh*  config.status*  configure.ac  libChaos_Modelica_im.la  Makefile   Makefile.orig

and the resulting C library for the block (libChaos_Modelica_im.so), linked to the whole Xcos model for simulation.

Running The Code

Executing Scilab from command line, we can also see the output from the FMI2 instance log calls:

Chaos_Modelica_im called with flag 4
fmi2Instantiate: GUID={47ad6a3c-e0df-4c1e-8dbe-ddca1fb1883f}
fmi2SetReal: nvr = 2
fmi2SetReal: #r5# = 0.3
fmi2SetReal: #r6# = 0.22
fmi2SetupExperiment: toleranceDefined=0 tolerance=0 startTime=0 stopTimeDefined=0 stopTime=1
fmi2EnterInitializationMode...
fmi2EnterInitializationMode: succeed
fmi2ExitInitializationMode...
fmi2ExitInitializationMode: succeed
fmi2NewDiscreteStates
fmi2EventUpdate: Start Event Update! Next Sample Event 0
fmi2EventUpdate: newDiscreteStatesNeeded false
fmi2EventUpdate: Checked for Sample Events! Next Sample Event 0
fmi2EnterContinuousTimeMode
fmi2GetContinuousStates: #r0# = 0
fmi2GetContinuousStates: #r1# = 1
fmi2GetReal: #r7# = 0
fmi2GetReal: #r8# = 1
Chaos_Modelica_im called with flag 6
fmi2SetTime: time=0
fmi2SetContinuousStates: #r0# = 0
fmi2SetContinuousStates: #r1# = 1
fmi2GetReal: #r7# = 0
fmi2GetReal: #r8# = 1
Chaos_Modelica_im called with flag 6
fmi2SetTime: time=0
fmi2SetContinuousStates: #r0# = 0
fmi2SetContinuousStates: #r1# = 1
fmi2GetReal: #r7# = 0
fmi2GetReal: #r8# = 1
...
fmi2GetReal: #r7# = 0
fmi2GetReal: #r8# = 1
Chaos_Modelica_im called with flag 1
...

(The flag lines were added manually to the computational function code)

Final Thoughts

The tests also revealed bug in the implementation which were partially fixed (and also updated here in the blog posts). Some issues still need to be addressed, but for now things seems to be progressing well.

That’s it for now. Thanks one more time for sticking by. See you soon !