Help - Search - Members - Calendar
Full Version: Implement C external functions
Modelica Forum > Modelica > Dymola
f.perrone
Hello everyone,

I´m trying to engender a 3D wind field and to do so I need to make use of the fast fourier transform.
On Internet there thousands of external C library that allow to get a fft.I have one of those and I would like to implement that in Dymola in order to call the function fft and get the transform of my starting values vector.
Please enclosed you might find the file I´m talking about.
I actually hope to get some hints as soon as possible.
Regards,

Francesco
David Elixmann
QUOTE(f.perrone @ Apr 11 2011, 02:35 PM) *

Hello everyone,

I´m trying to engender a 3D wind field and to do so I need to make use of the fast fourier transform.
On Internet there thousands of external C library that allow to get a fft.I have one of those and I would like to implement that in Dymola in order to call the function fft and get the transform of my starting values vector.
Please enclosed you might find the file I´m talking about.
I actually hope to get some hints as soon as possible.
Regards,

Francesco


Hi Francesco,

I don't understand the details of what you are trying to do, but it sounds like you just want to have a function in your Modelica model which computes some values using a routine that you have implemented in C?

If so, then it is fairly easy to do (I think). There's an explanation of how to do it in Peter Fritzson's book, in the chapter 9.4.

I can show you how I included a DLL library into my model:

You need to create an interface function in the Modelica model, like this:

CODE

function InflowData0 "Modelica external function"
  input String filename "Name of table file";
  input Real timet "Time (in days)";
  input Integer nData "Number of data columns (not time!) in the table";
  output Real data[nData] "inflow data";
  external "C" InflowData0(filename, timet, data) annotation(Include="#include <externalLib/InflowData0.c>",
    Library={"externalLib/myExternalLib0"});
end InflowData0;


In the Dymola working directory there's a subdirectory called "externalLib" in which I have the following files:
  • InflowData0.c which contains the following:
    CODE

    // file to be used for the compilation of the Modelica model


    #ifndef INFLOWDATA0_C
    #define INFLOWDATA0_C
    #include <stdlib.h>
    #include "externalLib/myExternalLib0.h"

    // also include the myExternalLib library in the compiler --> do this in Modelica

    void InflowData0(const char* filename, double time, double *value) // (double time, double* value)
    {
        int c_column;
        
        /* DEBUGGING
        printf(filename);
        fflush(1);  
        system("Pause");
        */

        if (time == 0)
        {
            c_column = file_read0(filename); //("data_inflow.txt");
        }
        interpolateValue0(time, value);

    }
    #endif
  • myExternalLib0.h which contains the headers of functions included in my DLL library
  • myExternalLib0.dll which is the DLL library that contains the functions "interpolateValue0" and "file_read0" that you see in the code above
  • myExternalLib0.lib which I had to include in this directory, too, for incomprehensible reasons.

The Dymola compiler is, in my experience, terrible at finding files at their specified locations, so I also included duplicates of all the above files also in the Dymola working directory itself. Redundant, I know, but I could not figure out how to make the compiler work correctly otherwise.

I hope this can help you? Your case seems less complicated than mine (you have all your functions in C code already, rather than in a compiled library like me) so maybe you can do it in a less cumbersome way.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.