Modelica socket communication (Part 1: socket library)
Now that I’ve demonstrated a method of using the OpenModelica CORBA interface to set up and run simulations, I will move on to a way of passing data out of and in to a running simulation (for example, to use a control algorithm that is more easily implemented in another language). I will use sockets for this as they are one of the simplest and most generic communication methods available.
The first step in setting up socket communication within a Modelica simulation is to create a socket library that can be called from the simulation code. Fortunately, this is very simple C code. You can find the relevant code here).
Now that we have the source file, we will compile the library. Instead of running the commands by hand, we will create a Makefile to do it for us. It’s a little bit of extra effort for a huge saving later on if you come back to change things or recompile on a different machine. The makefile is a very simple one with the following content:
.PHONY: all all: libomsocket.a Socket.lo: Socket.c libtool --mode=compile gcc -c $< libomsocket.a: Socket.lo libtool --mode=link gcc -o $@ $< clean: -rm Socket.lo Socket.o -rm -rf .libs
Note that the clean target doesn’t delete the final library. It is used for cleaning up the other miscellaneous files created during compilation and linking.
I used libtool to compile the library as it prevents me having the remember the full commands (I do this so little that I have to look them up). If you don’t already have libtool then you should install it via your package manager (e.g. sudo apt-get install libtool in Ubuntu/Mint).
The socket code I provided is based on code found here. If you’re interesting in the example given there then note that one of the links in the relevant post is not formatted correctly and one is missing. The complete set of files referenced in the post are: