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

Porting 3rd party libraries to OpenModelica...

Porting 3rd party libraries to OpenModelica...

Hello All,

I am new to Modelica and excited by what it appears to offer. I have been trying to 'get inside' Modelica as quickly as possible and have installed OpenModelica, amongst other things.

I am interested in porting external C/C++ libraries such that they can be represented/used in Modelica simulations. Furthermore that C/C++ can be generated for these models for compilation/execution elsewhere. A quick shufti through the indexes in the books gives me Chapter 12 in  'Introduction to Physical Modeling with Modelica', but it doesn't touch what I want to know. I don't see any connection between the Modelica library and the libraries that will be linked to to run the simulation.

Please take for example the Neural Network library here here. If you download this model, you'll see a definition file NeuralNetwork.mo which is human readable and seems to contain the Modelica definitions for the Modelica environment. MathModelica can read this file and gives me a hierarchy of available components within the library. Great! Just what I wanted! However, although there is passing reference to a Dymola library I can't see the source code to which the .mo definitions refer and hence can't see how any C/C++ could be generated and run against the library.

The books I have bought seem to be focused on users of a Modelica environment rather than engineers trying to bring new functionality in terms of external libraries into the Modelica domain.

What I am trying to say is that information on how a/ write a C/C++ library or b/ port an exiting C/C++ library and create the corresponding Modelica definitions such that these components can be used in a Modelica environment (whatever that may be) is thin on the ground.

I would be so very grateful for some useful pointers here, i.e., have I missed a really good FAQ/wiki/guide/book, or site containing some simple and more complex examples of libraries and how they might be visible to a Modelica environment.

Best regards, Paul.

Re: Porting 3rd party libraries to OpenModelica...

OK, not much action here :-)

I have downloaded the source for OpenModelica and had a look around.

I found ExternalLibraries.mo, ExternalLibraries.mos, ExternalFunc1.c and ExternalFunc2.c in  OpenModelica/Examples which are interesting and useful.

I want to go further than this, write my own components (or wrap someone elses) such that they can be used as components in any modelica models, such as Modelica.Electrical.Analog.Basic.Resistor r1(R=10); in dcmotor.mo in OpenModelica/Examples.

I'll have a further rummage in the source but again, any helpers or pointers in how to do this/where to look would be appreciated.

Best regards, Paul.

Re: Porting 3rd party libraries to OpenModelica...

Hi,

We could improve the documentation a bit about calling external functions.

For now you can read a bit about this in the Modelica Specification:
http://www.modelica.org/documents/ModelicaSpec31.pdf
OpenModelica implements the external interface explained in the specification.

We still have some problems with tables so the Neural Network won't yet work.
We're actively working on fixing bugs but it might take a while as now we're focusing
on getting the Modelica.Mechanics.MultiBody and Modelica.Media to work with OpenModelica.

You can also find a lot more examples in out testsuite:
https://openmodelica.org/svn/OpenModeli … testsuite/
user: anonymous
pass: none (actually write none here)

Basically to wrap external C/C++ functions or libraries there are 2 ways to go:
1. Implement the same functionality in Modelica (using functions)
2. Add Modelica external function wrappers to the external libraries

You need to understand that Modelica functions/external functions have some
limitations by comparison to C. Read about those in the Modelica specification.
Basically, things are mostly static, so you cannot dynamically create objects
on the fly.

Also, functions can be classified into two categories:
1. pure functions (same input gives exactly the same output, so no side effects)
    these are mostly mathematical functions
2. impure functions (same input gives different output, so they have side effects)
    these would be mostly called within when equations or initial equations/algorithms.
    these are mostly logging facilities or reading input files, etc.

Well, enough for now, let us know if we can help you more.

Cheers,
Adrian Pop/

Re: Porting 3rd party libraries to OpenModelica...

Thanks for taking the trouble to reply.

I found quite a few bits&pieces by doing...

$ find . -name '*.mos' -print

I am thinking (hoping) that we are at cross purposes here. I want to write external 'C' or C++ functions (or use someone elses) declare them in modelica syntax as in Examples/ExternalLibraries.mo and compile them for calling as in Examples/ExternalLibraries.mos. I also want to be able to use them in Eclipse/OpenModelica.

I hadn't imagined there would be limitations in how I write the 'C' code. It looks like I have some bedtime reading of that document.

Best regards, Paul.

Re: Porting 3rd party libraries to OpenModelica...

Hello Adrian,

I've been a bit distracted with Exciting Things like annual accounts :-(. Back on it, have read 12.9 and got from it:-

Fom 12.9.1.1, I can use double as real, int as Integer, int as Boolean, const char * as String, int as Enumeration Type, struct as record
From 12.9.3, I can't change input parameters. That's fine.

It looks very useful, but I have some questions. 12.9.4 is where we put it all together, what isn't clear to me is:-
with what tools should an external library be compiled such that OpenModelica can link to it and functions can be resolved?
external libraries may be dependant on other libaries - how might these be defined?

For example, my widgets are held in a nice postgreSQL database for which I have provided wrapper functions to return an array of structs containing widget info plucked from the database within a date range. Hence my external library is dependant on PostgreSQL library function calls. How do I express this to Modelica?

Best regards, Paul.

Re: Porting 3rd party libraries to OpenModelica...

Hi, libraries should preferably be compiled using the same compiler you are using to compile OpenModelica (mingw-gcc by default). If you mix MSVC and GCC, you run into problems because they use different C runtimes.

If you wish to link more libraries with your simulation, add those libraries to the annotation(Library = "myext.o postgresql.o"). You could also load dll's from your external function without linking to those using LoadLibrary (Windows) or dlopen (Unix).

Re: Porting 3rd party libraries to OpenModelica...

Hello Adrian,

Hi, libraries should preferably be compiled using the same compiler you are using to compile OpenModelica (mingw-gcc by default). If you mix MSVC and GCC, you run into problems because they use different C runtimes.

OK, I thought of that after my post (FX: Blush!) makes sense.

It looks like 'quite a lot' is possible, what about statically linked libraries, .a's? Same strategy?

Thanks again for your knowledgeable reply, you are giving me direction.

Best regards, Paul.

Re: Porting 3rd party libraries to OpenModelica...

Yes, .a's also work. The way it works (if I recall correctly) is the Library annotation is used as arguments to the gcc command used to compile the simulation file.
Thus, any gcc command will work (for example -L/usr/local/mypackage/lib/ mypack.o, if you want to link mypack.o and search /usr/local/mypackage/lib when looking for object files).
You can even specify C files in the annotation. But if g++ is used by OpenModelica (the svn trunk does) you have to use extern "C" {} in order to make the external function accessible.

Re: Porting 3rd party libraries to OpenModelica...

Hi Paul,

Just wanted to point out is not me that gave the latest replies, so the thanks go to Martin Sjölund, not me current/smile

Cheers,
Adrian Pop/

There are 0 guests and 0 other users also viewing this topic
You are here: