Multiple calls to a dll from Dymola

Viewed 85

I am implementing a model that requires to call a dll library twice, in order to receive from it specific values

  • the first call is to set up a system inside the library (for example, a powertrain from a catalogue of components and some design parameters)
  • the second call is to retrieve the performance of a component from such system (let's say the efficiency of a specific electric machine when used in that powertrain)

These two calls to the dll go together, and may be repeated during simulation time.

So far, I've only managed to interface my model to the dll through separate calls from Modelica external functions (one for the first call, one for the second). However, the state of the system is reset between the first and the second call.

  • Is there a way in Modelica to load a dll, call the same instance of it multiple times, and eventually close it when the job is done?

  • Perhaps is it only possible to achieve such feature by bundling the whole functionality in an external function?

  • Or am I attempting something that just doesn't work, because of some technical aspects that I am not aware of? (I don't know, perhaps the way it all gets compiled during translation)

1 Answers

If I understand this correctly, it looks like your external DLL has some kind of object pointer that is returned when you instantiate it, and this needs to be passed in at every subsequent function call to other functions in the DLL (to preserve the state).

So to do this in Modelica, you need to create an external object class. These are used to preserve state externally and has constructors and destructors to manage its memory. You can write small wrapper C functions to interface with your DLL functions that you can directly include in Modelica annotations, or write a wrapper lib.

Documentation

https://build.openmodelica.org/Documentation/ModelicaReference.Classes.ExternalObject.html

Simple Example

https://www.claytex.com/tech-blog/external-object-example-detecting-initial-rising-edge/

Detailed Example

https://github.com/modelica-3rdparty/ExternData/releases

Related