Getting started with Co-Simulation
Previous (Setup_ICE-model) | Back to Start | Next (setup_MATLAB)
Setting up the Python model
You need two files in order to set up and running a co-simulation with Python:
- Idalibn.dll
- libzmq-mt-4_3_1.dll
Make sure that you have the 64-bit version of the above files and located in the same folder.
mydll.CreateStorage.argtypes = [ctypes.c_char_p, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_longlong, ctypes.c_void_p, ctypes.c_longlong]
mydll.RetrieveStoredData.restype = ctypes.c_longlong
mydll.RetrieveStoredData.argtypes =[ctypes.c_longlong, ctypes.c_double, ctypes.c_void_p]
mydll.StoreData.restype = ctypes.c_longlong
mydll.StoreData.argtypes =[ctypes.c_longlong, ctypes.c_double,ctypes.c_void_p, ctypes.c_longlong]
mydll.CloseStorage.restype = ctypes.c_longlong
mydll.CloseStorage.argtypes =[ctypes.c_longlong]
Creating and opening the channels.
Creating the channel is done using the function CreateStorage which requires 6 arguments: pointer to channel name, number of variables passed on the channel, number of clients linked to the same variable, extrapolation time, whether to interpolate the variables or not, and whether there is an opc server or not. I what follows an example is shown for the channel that will be reading the data from the “Export” in ICE, which should be matched with the import channel here. We must use the same c_types as in the function prototype.
Channel_ptr = ctypes.c_char_p(Channel_name);
nVar = ctypes.c_longlong(1);
nSync = ctypes.c_longlong(1);
dTExtrap = ctypes.c_longlong(0);
VarInterp = ctypes.c_double(0);
opc = ctypes.c_longlong(0);
import_Channel = ctypes.c_longlong;
import_Channel=mydll.CreateStorage(Channel_ptr,nVar,nSync,dTExtrap,ctypes.byref(VarInterp),opc);
The second channel that will communicate with the “Import” in ICE and thus is an export channel here, can be created and opened in the same way, but takes the following parameters
Channel2_ptr = ctypes.c_char_p(Channel2_name);
nVar = ctypes.c_longlong(1);
nSync = ctypes.c_longlong(1);
dTExtrap = ctypes.c_longlong(75);
VarInterp = ctypes.c_double(0);
opc = ctypes.c_longlong(0);
export_Channel = ctypes.c_longlong;
export_Channel=mydll.CreateStorage(Channel2_ptr,nVar,nSync,dTExtrap,ctypes.byref(VarInterp),opc);
In the above function calls the variable VarInterp is called by reference using the ctypes.byref Function.
dData_sim = ctypes.c_double(0);
extrap = ctypes.c_longlong(0);
Retrieve and send data
To retrieve and send data now, all that needs to be done is to use the functions StoreData and RetrieveStoredData:
mydll.StoreData(export_Channel,tt,ctypes.byref(dData),extrap);
mydll.CloseStorage(export_channel);