Interoperability - C and Python

Below is information and examples about the OpenModelica external C interfaces, as well as examples of Python interoperability.

Calling External C functions

The following is a small example (ExternalLibraries.mo) to show the use of external C functions:

model ExternalLibraries

  function ExternalFunc1
    input Real x;
    output Real y;
  external y=ExternalFunc1_ext(x) annotation(Library="ExternalFunc1.o", LibraryDirectory="modelica://ExternalLibraries", Include="#include \"ExternalFunc1.h\"");
  end ExternalFunc1;

  function ExternalFunc2
    input Real x;
    output Real y;
  external "C" annotation(Library="ExternalFunc2", LibraryDirectory="modelica://ExternalLibraries");
  end ExternalFunc2;

  Real x(start=1.0, fixed=true), y(start=2.0, fixed=true);
equation
  der(x)=-ExternalFunc1(x);
  der(y)=-ExternalFunc2(y);
end ExternalLibraries;

These C (.c) files and header files (.h) are needed (note that the headers are not needed since OpenModelica will generate the correct definition if it is not present; using the headers it is possible to write C-code directly in the Modelica source code or declare non-standard calling conventions):

Listing 10 ExternalFunc1.c
double ExternalFunc1_ext(double x)
{
  double res;
  res = x+2.0*x*x;
  return res;
}
Listing 11 ExternalFunc1.h
double ExternalFunc1_ext(double);
Listing 12 ExternalFunc2.c
double ExternalFunc2(double x)
{
  double res;
  res = (x-1.0)*(x+2.0);
  return res;
}

The following script file ExternalLibraries.mos will perform everything that is needed, provided you have gcc installed in your path:

>>> system(getCompiler() + " -c -o ExternalFunc1.o ExternalFunc1.c")
0
>>> system(getCompiler() + " -c -o ExternalFunc2.o ExternalFunc2.c")
0
>>> system("ar rcs libExternalFunc2.a ExternalFunc2.o")
0
>>> simulate(ExternalLibraries)
record SimulationResult
    resultFile = "«DOCHOME»/ExternalLibraries_res.mat",
    simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'ExternalLibraries', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "LOG_SUCCESS       | info    | The initialization finished successfully without homotopy method.
LOG_SUCCESS       | info    | The simulation finished successfully.
",
    timeFrontend = 0.002813561,
    timeBackend = 0.002928111,
    timeSimCode = 0.00119783,
    timeTemplates = 0.003767221,
    timeCompile = 0.609624961,
    timeSimulation = 0.014376035,
    timeTotal = 0.6348890389999999
end SimulationResult;

Note

Notification: Model statistics after passing the front-end and creating the data structures used by the back-end:

* Number of equations: 2

* Number of variables: 2

Notification: Model statistics after passing the back-end for initialization:

* Number of independent subsystems: 2

* Number of states: 0 ()

* Number of discrete variables: 0 ()

* Number of discrete states: 0 ()

* Number of clocked states: 0 ()

* Top-level inputs: 0

Notification: Strong component statistics for initialization (4):

* Single equations (assignments): 4

* Array equations: 0

* Algorithm blocks: 0

* Record equations: 0

* When equations: 0

* If-equations: 0

* Equation systems (not torn): 0

* Torn equation systems: 0

* Mixed (continuous/discrete) equation systems: 0

Notification: Model statistics after passing the back-end for simulation:

* Number of independent subsystems: 2

* Number of states: 2 (y,x)

* Number of discrete variables: 0 ()

* Number of discrete states: 0 ()

* Number of clocked states: 0 ()

* Top-level inputs: 0

Notification: Strong component statistics for simulation (2):

* Single equations (assignments): 2

* Array equations: 0

* Algorithm blocks: 0

* Record equations: 0

* When equations: 0

* If-equations: 0

* Equation systems (not torn): 0

* Torn equation systems: 0

* Mixed (continuous/discrete) equation systems: 0

And plot the results:

_images/externallibraries.svg

Figure 139 Plot generated by OpenModelica+gnuplot

Calling external Python Code from a Modelica model

The following calls external Python code through a very simplistic external function (no data is retrieved from the Python code). By making it a dynamically linked library, you might get the code to work without changing the linker settings.

function pyRunString
  input String s;
external "C" annotation(Include="
#include <Python.h>

void pyRunString(const char *str)
{
  Py_SetProgramName(\"pyRunString\");  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString(str);
  Py_Finalize();
}
");
end pyRunString;

model CallExternalPython
algorithm
  pyRunString("
print 'Python says: simulation time',"+String(time)+"
");
end CallExternalPython;
>>> system("python-config --cflags > pycflags")
127
>>> system("python-config --ldflags > pyldflags")
127
>>> pycflags := stringReplace(readFile("pycflags"),"\n","");
>>> pyldflags := stringReplace(readFile("pyldflags"),"\n","");
>>> setCFlags(getCFlags()+pycflags)
true
>>> setLinkerFlags(getLinkerFlags()+pyldflags)
true
>>> simulate(CallExternalPython, stopTime=2)
record SimulationResult
    resultFile = "",
    simulationOptions = "startTime = 0.0, stopTime = 2.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'CallExternalPython', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
    messages = "Failed to build model: CallExternalPython",
    timeFrontend = 0.002620091,
    timeBackend = 0.012050094,
    timeSimCode = 0.00098565,
    timeTemplates = 0.003202921,
    timeCompile = 0.367471877,
    timeSimulation = 0.0,
    timeTotal = 0.386364803
end SimulationResult;

Error

Notification: Model statistics after passing the front-end and creating the data structures used by the back-end:

* Number of equations: 0

* Number of variables: 0

Notification: Model statistics after passing the back-end for initialization:

* Number of independent subsystems: 1

* Number of states: 0 ()

* Number of discrete variables: 0 ()

* Number of discrete states: 0 ()

* Number of clocked states: 0 ()

* Top-level inputs: 0

Notification: Strong component statistics for initialization (0):

* Single equations (assignments): 0

* Array equations: 0

* Algorithm blocks: 0

* Record equations: 0

* When equations: 0

* If-equations: 0

* Equation systems (not torn): 0

* Torn equation systems: 0

* Mixed (continuous/discrete) equation systems: 0

Notification: Model statistics after passing the back-end for simulation:

* Number of independent subsystems: 1

* Number of states: 0 ()

* Number of discrete variables: 0 ()

* Number of discrete states: 0 ()

* Number of clocked states: 0 ()

* Top-level inputs: 0

Notification: Strong component statistics for simulation (0):

* Single equations (assignments): 0

* Array equations: 0

* Algorithm blocks: 0

* Record equations: 0

* When equations: 0

* If-equations: 0

* Equation systems (not torn): 0

* Torn equation systems: 0

* Mixed (continuous/discrete) equation systems: 0

Error: Error building simulator. Build log: make[1]: Entering directory '«DOCHOME»'

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython.o CallExternalPython.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_functions.o CallExternalPython_functions.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_records.o CallExternalPython_records.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_01exo.o CallExternalPython_01exo.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_02nls.o CallExternalPython_02nls.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_03lsy.o CallExternalPython_03lsy.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_04set.o CallExternalPython_04set.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_05evt.o CallExternalPython_05evt.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_06inz.o CallExternalPython_06inz.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_07dly.o CallExternalPython_07dly.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_08bnd.o CallExternalPython_08bnd.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_09alg.o CallExternalPython_09alg.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_10asr.o CallExternalPython_10asr.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_11mix.o CallExternalPython_11mix.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_12jac.o CallExternalPython_12jac.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_13opt.o CallExternalPython_13opt.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_14lnz.o CallExternalPython_14lnz.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_15syn.o CallExternalPython_15syn.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_16dae.o CallExternalPython_16dae.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_17inl.o CallExternalPython_17inl.c

clang -Os -DOM_HAVE_PTHREADS -fPIC -falign-functions -mfpmath=sse -fno-dollars-in-identifiers -Wno-parentheses-equality -I"«OPENMODELICAHOME»/include/omc/c" -I"«OPENMODELICAHOME»/include/omc" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=CallExternalPython -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0 -c -o CallExternalPython_18spd.o CallExternalPython_18spd.c

In file included from CallExternalPython_functions.c:7:

./CallExternalPython_includes.h:8:10: fatal error: 'Python.h' file not found

#include <Python.h>

^~~~~~~~~~

1 error generated.

make[1]: *** [<builtin>: CallExternalPython_functions.o] Error 1

make[1]: *** Waiting for unfinished jobs....

make[1]: Leaving directory '«DOCHOME»'

Calling OpenModelica from Python Code

This section describes a simple-minded approach to calling Python code from OpenModelica. For a description of Python scripting with OpenModelica, see OMPython - OpenModelica Python Interface.

The interaction with Python can be perfomed in four different ways whereas one is illustrated below. Assume that we have the following Modelica code:

Listing 13 CalledbyPython.mo
model CalledbyPython
  Real x(start=1.0), y(start=2.0);
  parameter Real b = 2.0;
equation
  der(x) = -b*y;
  der(y) = x;
end CalledbyPython;

In the following Python (.py) files the above Modelica model is simulated via the OpenModelica scripting interface:

Listing 14 PythonCaller.py
#!/usr/bin/python
import sys,os
global newb = 0.5
execfile('CreateMosFile.py')
os.popen(r"omc CalledbyPython.mos").read()
execfile('RetrResult.py')
Listing 15 CreateMosFile.py
#!/usr/bin/python
mos_file = open('CalledbyPython.mos','w', 1)
mos_file.write('loadFile("CalledbyPython.mo");\n')
mos_file.write('setComponentModifierValue(CalledbyPython,b,$Code(="+str(newb)+"));\n')
mos_file.write('simulate(CalledbyPython,stopTime=10);\n')
mos_file.close()
Listing 16 RetrResult.py
#!/usr/bin/python
def zeros(n): #
  vec = [0.0]
  for i in range(int(n)-1): vec = vec + [0.0]
  return vec
res_file = open("CalledbyPython_res.plt",'r',1)
line = res_file.readline()
size = int(res_file.readline().split('=')[1])
time = zeros(size)
y = zeros(size)
while line != ['DataSet: time\\n']:
  line = res_file.readline().split(',')[0:1]
for j in range(int(size)):
  time[j]=float(res\_file.readline().split(',')[0])
while line != ['DataSet: y\\n']:
  line=res_file.readline().split(',')[0:1]
for j in range(int(size)):
  y[j]=float(res\_file.readline().split(',')[1])
res_file.close()

A second option of simulating the above Modelica model is to use the command buildModel instead of the simulate command and setting the parameter value in the initial parameter file, CalledbyPython_init.txt instead of using the command setComponentModifierValue. Then the file CalledbyPython.exe is just executed.

The third option is to use the Corba interface for invoking the compiler and then just use the scripting interface to send commands to the compiler via this interface.

The fourth variant is to use external function calls to directly communicate with the executing simulation process.