Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

creating dll from model

creating dll from model

Hello!

Is there any way to compile model to dll, not exe? I'll try to create hardware in loop simulation. The plan is to use simulation as DLL file, import it in other program and using dll import function sets input and gets resault using exportable function.

best regards
Dominik

Re: creating dll from model

translateModelFMU(ModelName)

Re: creating dll from model

Ok, what next?  How to create a dll( API C) from Functional Mockup Interface. Any guide or example?

Maybe i'll give more details:
- im using windows, so probably i'll have to create dll in MinGW compiler.
- i want to import a simulation using a dllimport in c#, set the simulation parameters an visualize the results.

Re: creating dll from model

Re: creating dll from model

Ok i see. It's created a dll file. And the import functions are :

void setStartValues(ModelInstance *comp);
void setDefaultStartValues(ModelInstance *comp);
void eventUpdate(ModelInstance* comp, fmiEventInfo* eventInfo);
fmiReal getReal(ModelInstance* comp, const fmiValueReference vr);
fmiStatus setReal(ModelInstance* comp, const fmiValueReference vr, const fmiReal value);
fmiInteger getInteger(ModelInstance* comp, const fmiValueReference vr);
fmiStatus setInteger(ModelInstance* comp, const fmiValueReference vr, const fmiInteger value);
fmiBoolean getBoolean(ModelInstance* comp, const fmiValueReference vr);
fmiStatus setBoolean(ModelInstance* comp, const fmiValueReference vr, const fmiBoolean value);
fmiString getString(ModelInstance* comp, const fmiValueReference vr);
fmiStatus setExternalFunction(ModelInstance* c, const fmiValueReference vr, const void* value);

and that's the way to comunicate with simulation? All function describes are in https://www.fmi-standard.org/. Am i correct?

Re: creating dll from model

That sounds about right from my limited knowledge of FMI. Note that you probably also need to read the xml-file to get the index of a variable, and so on.

Re: creating dll from model

probably, the other way to comunicate with simulation is to implement a socket(tcp) comunnication. Maby you know an example of that? Somebody try it?

Re: creating dll from model

Many people have, but I do not have any example code except this. I would recommend writing your own code since that example is based on a very old OpenModelica and it lacks the C-code anyway.

Use external objects or external C and use the simulation itself as the master using a fixed-step solver. Communicate using when sample(...) and the external object. In the external C code you write, synchronize the time (to real-time) and do the communication (with the hardware). I believe this is a lot less work than writing your own FMI master.

Re: creating dll from model

Thanks current/smile this example is great. When i finish i'll write how it goes!

Re: creating dll from model

Unfortunately the example isn't as good as i thought. It's reading input file and when something is in it he do the simulation based on value readed from that file.
So nevermind, i'll try to do scocket (tcp) comunication.
But i've got some questions :
-is it posiible to add c code (global function or values) at the begining (constructor or somethink like that) of the simulation? I know i can do it manualy by editing model.c file.
- how to stop the simulation? Is only solusion are endles loop whlie calculating (adding c function to model) or sample event which will don't stop the simulation?
- is it possible to change step size while simulation is in progres?

Re: creating dll from model

External objects have a constructor function that is only called once in a simulation. when initial() may be called multiple times but is commonly used to call functions at the start of the simulation.

You can stop the simulation by calling terminate() (which I hope is again working in OpenModelica; you can also use assert(false) or #include "ModelicaUtilities.h" and call any of the ModelicaError() family of functions if you would rather stop the simulation in external C code).

The Modelica language has no way of changing the step size while the simulation is running, so any such feature would have to be tool-dependent. And as far as I can tell the control structure is not a global value in OpenModelica so it would be hard to access it without stack inspection or re-writing the runtime.

Re: creating dll from model

sjoelund.se wrote:

External objects have a constructor function that is only called once in a simulation. when initial() may be called multiple times but is commonly used to call functions at the start of the simulation.

So i can make my own source (input/output) model which i'll just add to simulation and i'll don't have to change code manualy, and recompile it current/smile
Adding c code to it is no problem  ---- external "C" r=rnd(y) annotation(Include = "#include <rnd2.c>";  ---
, but how to add starting code to the constructor? And where is a description of external function?
i've read the https://www.modelica.org/documents/ModelicaSpec31.pdf.
So first i've to add include code with constructor, and next execute one of it's function whet simulation is in progress (in event or somethink)?

One more question, how to compile the model using gcc compiler, what is the formula in windows (gcc ?????????)

Ok, look what i found : https://openmodelica.org/svn/OpenModeli … pleClient/

I'll also need to change makefile to add -lwsock32 to compiler command line.

Re: creating dll from model

For an example of external objects, see:
https://openmodelica.org/svn/OpenModeli … /ExtObj.mo

To force -lwsock32 to be compiled, add Library={"wsock32"}

The simulation model is by default compiled using gcc in Windows. Use +target=msvc if you need to use visual studio (I never tested this myself).

Re: creating dll from model

Finally i've got the solution, maby not super elegant but still current/smile. So using dll was total failier, socket - ok but the includes file (socket.h,winsock2.h) are missing or it's not commatible with mingw of openmodelica.
I'll use a stdin and stdout (maby stderr) to comunicate with simultion. So i run in c# simulation using proccess, and redirect a stdout of simulation to my c# application current/wink From c# i can use socket and everythink what i want.
Thanks for help.

Re: creating dll from model

Hello all

FMI option seems a great solution. However kermit_d says that it was a total failure. What happened?. Does anybody know where it is explained how to use the FMI DLL generated by OMC?

Best regards
Koldo

Re: creating dll from model

I have obtained a FMU and tested it with the FMU Compliance Checker from https://www.fmi-standard.org/downloads.

Unfortunately although the standalone .EXE runs perfectly, the FMU fails with the message "FMU could not converge with in event update" error just in the 0.2 simulation second.

Other detail is that the .EXE is about 5Mb and the DLL is about 1Mb

Does perhaps the OMC FMI support is partial, or maybe I am doing anything wrong?

Best regards
Koldo

Re: creating dll from model

Koldo wrote:

I have obtained a FMU and tested it with the FMU Compliance Checker from https://www.fmi-standard.org/downloads.

Unfortunately although the standalone .EXE runs perfectly, the FMU fails with the message "FMU could not converge with in event update" error just in the 0.2 simulation second.

Other detail is that the .EXE is about 5Mb and the DLL is about 1Mb

Does perhaps the OMC FMI support is partial, or maybe I am doing anything wrong?

Best regards
Koldo

A detail to add is that the original model works using DASSL. However it fails using RUNGE-KUTTA or EULER.

Does FMI work with EULER by default?

Re: creating dll from model

The reason the executable is so much larger is that in omc, the executable includes all possible solvers, and so on. In FMI for model exchange it is the tool that imports the FMU (in your case, the compliance checker) that does the numerical integration. I believe the compliance checker only uses euler, so this might be your problem.

Re: creating dll from model

Hello sjoelund.se

Is there a way to define what solver to use or include in FMI?

Best regards
Koldo

Re: creating dll from model

Yes, by writing your own FMU simulator.

Re: creating dll from model

Hello sjoelund.se

If I could I would do it. However it would require to do deep changes inside OMC code.

It seems that it can be done only by core OpenModelica developers.

Best regards
Koldo

Re: creating dll from model

No, that's not true. The FMU for model exchange is just an interface which includes no solver. So no amount of work on the OpenModelica side will allow you to change the solver. It's rewriting the FMI Validator to include dassl that you need.

Re: creating dll from model

Hello sjoelund.se

As I have understand you, a program to load a FMU generated with OpenModelica has to include the solver.

Is there a simple sample about how to do that?

Thank you very much
Koldo

Re: creating dll from model

Hello sjoelund.se

Following your links there are many other links. I have opened some of them but they do not seem to include any solver.

AFAIK the FMI interface for co-simulation supposes that the FMU includes the solver. Here (http://www.jmodelica.org/5337) it is said that OpenModelica does not support co-simulation FMUs.

I would like to integrate a OpenModelica model with a CFD to do co-simulation. For that I would need to include the solver with the OpenModelica FMU in any way. Is it possible to change the FMU compiling parameters in any way to include a kind of -lSUNDIALS in .DLL linking? Would it work?. Please see this comment about how it is done in Dymola (http://www.claytex.com/exporting-models … her-tools/)

Thank you very much
Koldo

Re: creating dll from model

It wasn't that hard to find solvers from the page I linked current/wink

For example this one: https://github.com/mtiller/fmusdk/blob/ … m/fmusim.c

Re: creating dll from model

Hello sjoelund.se

That is a nice example from Michael Tiller. However I cannot see how it takes care of zero-crossing and things like that. I would prefer to use the OpenModelica solver technology instead of reinventing the wheel current/wink or doing reverse engineering of the same OpenModelica. This link could be interesting (http://www.claytex.com/exporting-models … her-tools/) .

I will read carefully this evening the FMU 2.0 for Co-Simulation... current/big_smile

Re: creating dll from model

If you want all the nice stuff in OpenModelica, don't use FMI current/smile Or wait a few (months? years?) before FMI for co-simulation is added and is stable.

Re: creating dll from model

sjoelund.se wrote:

If you want all the nice stuff in OpenModelica, don't use FMI current/smile Or wait a few (months? years?) before FMI for co-simulation is added and is stable.

... But can be done co-simulation with OpenModelica without FMI current/sad?

AFAIK OpenModelica can export slave FMU for co-simulation. Is there a way (perhaps in beta) to do a good simulation mixing OpenModelica and other solvers with all the SUNDIALS stuff inside?

Re: creating dll from model

No, OpenModelica can't export slave FMU for co-simulation. Only the model exchange FMU export is supported right now.
You can check the status here https://www.fmi-standard.org/tools.

Adeel.

Re: creating dll from model

Thank you Adeel. I see.

sjoelund.se: Perhaps a way to do it would be to reverse engineer OpenModelica importFMU() function current/big_smile

Re: creating dll from model

Sorry sjoelund.se. It was a joke: I do know it is not possible to do that with the free (gratis) license.

Do you know if is there a sample like the M. Tiller one but using a real solver like SUNDIALS?

Re: creating dll from model

I don't know any such open code. Most of them use euler.

Re: creating dll from model

Hello sjoelund.se

We are trying to do co-simulation with Modelica and a powerful international university based GPL-3 CFD tool.

We are convincing the CFD tool developers to support FMU including the solver inside. Unfortunately our Modelica simulation is rather stiff: it only works with DASSL and it does not with euler or runge kutta.

I have the personal conviction to use OpenModelica. However we suffer a strong pressure to use a commercial Modelica environment. They promise to have lower project costs including year licenses and for now it seems that it is true: the budget of getting the year OpenModelica licenses and hacking OMC code base would be higher.

I would like to demonstrate that OpenModelica is the best option and I would love to have a joint open simulation, helping to extend the OpenModelica universe. All the results would be properly shown in a paper.

Could you advice me? What is your opinion?

Re: creating dll from model

Other focus: Is it possible for an external program to force an .exe simulation generated by OMC to do a step by step simulation? (through sockets or CORBA)

If true, would it be possible that this external program could get/set values from/to the simulation?, and would it be possible to change the simulation step?

Re: creating dll from model

FMI can do what you want.

Re: creating dll from model

That is true although with nuances current/smile:

- OpenModelica translateModelFMU() cannot produce FMI for co-simulation, only for model exchange

- There are no C/C++ libraries that can simulate a FMI for model exchange. PyFMI from Modelon can do it, but is made in Python. FMIL from Modelon can only simulate FMI for co-simulation.

Re: creating dll from model

So write that C/C++ library. I promise you it will work better than the interactive interface to simulations in omc. (I have been told it is working as designed at the moment; that it works as designed is a problem however)

Re: creating dll from model

So write that C/C++ library

I would like to do it current/smile. However it is a rather complex project, nothing to do with co-simulation.

If you want to have a rough idea, just open the model automatically created when an importFMU() is done.

Edited: In fact OpenModelica runtime libraries include all the necessary nuts and bolts. The best FMI library is just here current/smile. However OpenModelica license does not let to do it.

There are 0 guests and 0 other users also viewing this topic