I'm developing a Modelica library, called "MyLib" (in the OpenModelica environment).
I wrote the following external C module ("ExtTempDataLib.c"):
#ifndef __EXT_LIB_C
#define __EXT_LIB_C
double temperatures [] = {25, 18, 30, 21, 16};
double GetTemperature(int index)
{
// Modelica indexes start from 1
return temperatures[index - 1];
}
void SetTemperature(int index, double newTempVal)
{
// Modelica indexes start from 1
temperatures[index - 1] = newTempVal;
}
#endif
It implements an array of numerical values ("temperatures") representing temperatures, which can be read/overwritten via the "GetTemperature(...)" and "SetTemperature(...)" functions.
The goal is to provide to the Modelica library a set of global data (i.e. accessible by all models) that are not constant.
Otherwise, I could simply declare the array as a constant inside my "MyData2" Modelica package:
constant Real temperatures [:] = {25, 18, 30, 21, 16};
and then directly access any of its elements (clearly in read-only mode) from any model by simply typing:
MyLib.MyData2.temperatures[index];
The "MyData" Modelica package:
within MyLib;
package MyData
model SingleTemperatureOverwrite
parameter Integer index = 1 "Temperature data index";
parameter Real newVal = 50 "New temperature value";
algorithm
when initial() then
MyLib.MyData.SetTemperature(index, newVal);
end when;
end SingleTemperatureOverwrite;
function GetTemperature
input Integer index;
output Real temperature;
external "C" temperature = GetTemperature(index);
annotation(Include = "#include \"ExtTempDataLib.c\"");
end GetTemperature;
function SetTemperature
input Integer index;
input Real newTempVal;
external "C" SetTemperature(index, newTempVal);
annotation(Include = "#include \"ExtTempDataLib.c\"");
end SetTemperature;
end MyData;
defines:
- the "GetTemperature" and "SetTemperature" functions, which call the corresponding external C functions;
- the "SingleTemperatureOverwrite" model, which, at the initial time, overwrites the array value at the specified index with the specified new value (parameters "index" and "newVal", respectively).
The "Test" Modelica package:
within MyLib;
package Test
model TemperatureTest
parameter Integer index = 1 "Temperature data index";
Real temperature (start = 0) "Temperature value";
equation
temperature = MyLib.MyData.GetTemperature(index);
end TemperatureTest;
end Test;
defines the "TemperatureTest" model, which reads the array value at the specified index (the "index" parameter) and "copies" it to the "temperature" variable.
I created a Modelica test model, including just one instance of "SingleTemperatureOverwrite" and one of "TemperatureTest" ("singleTemperatureOverwrite1" and "temperatureTest1", respectively), leaving all parameters at their default value.
The model compiles and executes correctly but, when I plot the "temperature" variable of "temperatureTest1", instead of getting "50", as I expected, I get "25" (i.e. the starting value of the first element of the array defined in the external C module).
Can somebody please explain me what I'm doing wrong?