0 Votes

Getting started with Co-Simulation

Last modified by superadmin on 2025/01/14 17:42

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:

  1. Idalibn.dll
  2. 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.

First import the ctypes library.
import ctypes
and load the library which contains the communication routines we shall use.
mydll=ctypes.CDLL('.\idalibN.dll')
Define function prototypes for the arguments and return values of the functions.
mydll.CreateStorage.restype = ctypes.c_longlong
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_name   = b'ICE_zone';
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_name  = b'ICE_heatCtr';
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.

The data that will be communicated need to be initialized with the right c_types
dData = ctypes.c_double(0);
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:

StoreData takes as arguments the name of the storage, the time of stored slice, a pointer to the data to store/send and whether to extrapolate or not. The pointer to the data is easiest created by using the ctypes.byref function. The data to store/send needs to be cast to the right type before used in the function
dData = ctypes.c_double(PID_sig);
mydll.StoreData(export_Channel,tt,ctypes.byref(dData),extrap);
RetrieveStoredData takes as arguments the name of the storage, the time of the retrieved slice and a pointer to the retrieved data
mydll.RetrieveStoredData(import_Channel,tt,ctypes.byref(dData_sim));
The retrieved data needs to be cast to the right type before used
FeedBack[counter+1] = dData_sim.value;
 At the end of the Python script the storages should be closed using the function CloseStorage
mydll.CloseStorage(import_channel);
mydll.CloseStorage(export_channel);
Previous (Setup_ICE-model)     |     Back to Start     |     Next (setup_MATLAB)