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

Problem with local path of c function

Problem with local path of c function

The C function are checked directly and the path for reading matrix in file is ok. It's the same for the includes. Only the Libraries doesn't work when a string variable is in annotation, it's localPatho.
This code modelica works because I wrote directly the path of the o-file :
within ;
  package Interpolate
    constant String [:,2] glibraries := OpenModelica.Scripting.getLoadedLibraries();
    constant String localPath = Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/");
    constant String localPatho = Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/") + "/Clibraries/Document.o";
    constant String localPathh = "#include \"" + Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/") + "/Clibraries/Interpolate.h\"";
    model Test
      extends Modelica.Icons.Example;
      MyTable table1 = MyTable("tab.txt", "table1");
      Real y;
      
    equation
      der(y) = interpolateMyTable(table1, 300 * (time + 1), "S_adm_f");
      annotation(experiment(StartTime = 0.0, StopTime = 1.0, Tolerance = 0.000001));
    end Test;

    class MyTable
      extends ExternalObject;

      function constructor
        input String fileName;
        input String tableName;
        output MyTable table;
       
       
        external "C" table = initMyTable(fileName, tableName, localPath + "/donnees/") annotation(LibraryDirectory = localPatho , IncludeDirectory = localPathh);
      end constructor;
      
      
      function destructor
        input MyTable table;
     
        external "C" closeMyTable(table) annotation(Library = "C:/OpenModelica1.9.1Beta2/tmp/Test_interp/Clibraries/Document.o" , IncludeDirectory = localPathh);
      end destructor;
      annotation(Documentation(info = "<HTML>
                                        <p>
                                       Subclass of the predefined class ExternalObject allowing to access to external data structures such     as tables which are keeped between calls and passed to an external c-function for interpolation             purposes.
                                        <p>
                                       This class has only two external c-functions named constructor and destructor which are called             once before (resp. after) the first (resp. last) use of the obejct it refers to.
                                       <p>
                                       The tables which are to be interpolated are stored in only one structure containing :
                                       </P>
                                       <UL>
                                           <LI><P >keywords naming the tables,
                                           </P>
                                           <LI><P >for each table, an integer defining the lengths of the table, 
                                           </P>
                                           <LI><P >the data are given under the form of a two-dimensional table the entries of are                 respectively a crank    angle and a value.  The interpolation is carried out between two consecutive         angles</P>
                                       </UL>
                                        </HTML>
                                        "));
    end MyTable;

    function interpolateMyTable
      extends Modelica.Icons.Function;
      input MyTable table;
      input Real u;
      input String Nom_table;
      output Real y;
      
      external "C" y = interpolateMyTable(table, u, Nom_table) annotation(LibraryDirectory = localPatho , IncludeDirectory = localPathh);
      annotation(Documentation(info = "<HTML>
                                       <p> Interpolate entries of the table \"Nom-table\" sorted in the previously defined structure of                 name \"table\" with the help of the external c-function \" double interpolateMyTable(void* object,             double u, const char* nom_tab)  \"
                                       </HTML>"));
    end interpolateMyTable;
    annotation(Documentation(info = "<HTML>
                                        <p>
                                       Package of functions allowing linear interpolation of data such as : flow rate coefficients according         to the valve lifting laws and the flow direction. These data depend on the cranck angle (between 0         and 720 degres), are stored in a .txt file and arise  from external simulations or testing results
                                        <p><b>Release Notes:</b></p>
                                        <ul>
                                        <li><i>  </i> </li>
                                        </ul>
                                        </HTML>
                                        "));
   
   
  end Interpolate;

We can find the -o file named Document.o in the compilation results :
"C:\OpenModelica1.9.1Beta2\\MinGW\bin\mingw32-make.exe" -j4 -f Interpolate.Test.makefile
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test.o Interpolate.Test.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_functions.o Interpolate.Test_functions.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_records.o Interpolate.Test_records.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_01exo.o Interpolate.Test_01exo.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_02nls.o Interpolate.Test_02nls.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_03lsy.o Interpolate.Test_03lsy.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_04set.o Interpolate.Test_04set.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_05evt.o Interpolate.Test_05evt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_06inz.o Interpolate.Test_06inz.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_07dly.o Interpolate.Test_07dly.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_08bnd.o Interpolate.Test_08bnd.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_09alg.o Interpolate.Test_09alg.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_10asr.o Interpolate.Test_10asr.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_11mix.o Interpolate.Test_11mix.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_12jac.o Interpolate.Test_12jac.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_13opt.o Interpolate.Test_13opt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_14lnz.o Interpolate.Test_14lnz.c
gcc -I. -o Interpolate.Test.exe Interpolate.Test.o Interpolate.Test_functions.o Interpolate.Test_records.o Interpolate.Test_01exo.o Interpolate.Test_02nls.o Interpolate.Test_03lsy.o Interpolate.Test_04set.o Interpolate.Test_05evt.o Interpolate.Test_06inz.o Interpolate.Test_07dly.o Interpolate.Test_08bnd.o Interpolate.Test_09alg.o Interpolate.Test_10asr.o Interpolate.Test_11mix.o Interpolate.Test_12jac.o Interpolate.Test_13opt.o Interpolate.Test_14lnz.o -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  C:/OpenModelica1.9.1Beta2/tmp/Test_interp/Clibraries/Document.o    -falign-functions -msse2 -mfpmath=sse     -L"C:/OpenModelica1.9.1Beta2//lib/omc" -L"C:/OpenModelica1.9.1Beta2//lib" -Wl,--stack,0x2000000,-rpath,"C:/OpenModelica1.9.1Beta2//lib/omc" -Wl,-rpath,"C:/OpenModelica1.9.1Beta2//lib"  -lregex -lexpat -lgc -lpthread -fopenmp -loleaut32  -lSimulationRuntimeC -lgc -lexpat -lregex -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lcoinmetis -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -ltmglib-mingw -lblas-mingw -lf2c -linteractive -lwsock32 -llis -lstdc++

But with the localPatho variable, that doesn't work :
  within ;
  package Interpolate
    constant String [:,2] glibraries := OpenModelica.Scripting.getLoadedLibraries();
    constant String localPath = Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/");
    constant String localPatho = Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/") + "/Clibraries/Document.o";
    constant String localPathh = "#include \"" + Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/") + "/Clibraries/Interpolate.h\"";
    model Test
      extends Modelica.Icons.Example;
      MyTable table1 = MyTable("tab.txt", "table1");
      Real y;
      
    equation
      der(y) = interpolateMyTable(table1, 300 * (time + 1), "S_adm_f");
      annotation(experiment(StartTime = 0.0, StopTime = 1.0, Tolerance = 0.000001));
    end Test;

    class MyTable
      extends ExternalObject;

      function constructor
        input String fileName;
        input String tableName;
        output MyTable table;
       
       
        external "C" table = initMyTable(fileName, tableName, localPath + "/donnees/") annotation(LibraryDirectory = localPatho , IncludeDirectory = localPathh);
      end constructor;
      
      
      function destructor
        input MyTable table;
     
        external "C" closeMyTable(table) annotation(LibraryDirectory = localPatho , IncludeDirectory = localPathh);
      end destructor;
      annotation(Documentation(info = "<HTML>
                                        <p>
                                       Subclass of the predefined class ExternalObject allowing to access to external data structures such     as tables which are keeped between calls and passed to an external c-function for interpolation             purposes.
                                        <p>
                                       This class has only two external c-functions named constructor and destructor which are called             once before (resp. after) the first (resp. last) use of the obejct it refers to.
                                       <p>
                                       The tables which are to be interpolated are stored in only one structure containing :
                                       </P>
                                       <UL>
                                           <LI><P >keywords naming the tables,
                                           </P>
                                           <LI><P >for each table, an integer defining the lengths of the table, 
                                           </P>
                                           <LI><P >the data are given under the form of a two-dimensional table the entries of are                 respectively a crank    angle and a value.  The interpolation is carried out between two consecutive         angles</P>
                                       </UL>
                                        </HTML>
                                        "));
    end MyTable;

    function interpolateMyTable
      extends Modelica.Icons.Function;
      input MyTable table;
      input Real u;
      input String Nom_table;
      output Real y;
      
      external "C" y = interpolateMyTable(table, u, Nom_table) annotation(LibraryDirectory = localPatho , IncludeDirectory = localPathh);
      annotation(Documentation(info = "<HTML>
                                       <p> Interpolate entries of the table \"Nom-table\" sorted in the previously defined structure of                 name \"table\" with the help of the external c-function \" double interpolateMyTable(void* object,             double u, const char* nom_tab)  \"
                                       </HTML>"));
    end interpolateMyTable;
    annotation(Documentation(info = "<HTML>
                                        <p>
                                       Package of functions allowing linear interpolation of data such as : flow rate coefficients according         to the valve lifting laws and the flow direction. These data depend on the cranck angle (between 0         and 720 degres), are stored in a .txt file and arise  from external simulations or testing results
                                        <p><b>Release Notes:</b></p>
                                        <ul>
                                        <li><i>First version Mai 2009 M. Demoulin & C. Fourcade2</i> </li>
                                        </ul>
                                        </HTML>
                                        "));
   
   
  end Interpolate;


And we can not find the path for Document.o in the compilation results :
"C:\OpenModelica1.9.1Beta2\\MinGW\bin\mingw32-make.exe" -j4 -f Interpolate.Test.makefile
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test.o Interpolate.Test.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_functions.o Interpolate.Test_functions.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_records.o Interpolate.Test_records.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_01exo.o Interpolate.Test_01exo.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_02nls.o Interpolate.Test_02nls.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_03lsy.o Interpolate.Test_03lsy.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_04set.o Interpolate.Test_04set.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_05evt.o Interpolate.Test_05evt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_06inz.o Interpolate.Test_06inz.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_07dly.o Interpolate.Test_07dly.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_08bnd.o Interpolate.Test_08bnd.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_09alg.o Interpolate.Test_09alg.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_10asr.o Interpolate.Test_10asr.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_11mix.o Interpolate.Test_11mix.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_12jac.o Interpolate.Test_12jac.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_13opt.o Interpolate.Test_13opt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_14lnz.o Interpolate.Test_14lnz.c
gcc -I. -o Interpolate.Test.exe Interpolate.Test.o Interpolate.Test_functions.o Interpolate.Test_records.o Interpolate.Test_01exo.o Interpolate.Test_02nls.o Interpolate.Test_03lsy.o Interpolate.Test_04set.o Interpolate.Test_05evt.o Interpolate.Test_06inz.o Interpolate.Test_07dly.o Interpolate.Test_08bnd.o Interpolate.Test_09alg.o Interpolate.Test_10asr.o Interpolate.Test_11mix.o Interpolate.Test_12jac.o Interpolate.Test_13opt.o Interpolate.Test_14lnz.o -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME      -falign-functions -msse2 -mfpmath=sse     -L"C:/OpenModelica1.9.1Beta2//lib/omc" -L"C:/OpenModelica1.9.1Beta2//lib" -Wl,--stack,0x2000000,-rpath,"C:/OpenModelica1.9.1Beta2//lib/omc" -Wl,-rpath,"C:/OpenModelica1.9.1Beta2//lib"  -lregex -lexpat -lgc -lpthread -fopenmp -loleaut32  -lSimulationRuntimeC -lgc -lexpat -lregex -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lcoinmetis -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -ltmglib-mingw -lblas-mingw -lf2c -linteractive -lwsock32 -llis -lstdc++
Interpolate.Test_functions.o:Interpolate.Test_functions.ccurrent/sad.text+0x13): undefined reference to `closeMyTable'
Interpolate.Test_functions.o:Interpolate.Test_functions.ccurrent/sad.text+0x38): undefined reference to `initMyTable'
Interpolate.Test_functions.o:Interpolate.Test_functions.ccurrent/sad.text+0x8c): undefined reference to `interpolateMyTable'
collect2: ld returned 1 exit status
mingw32-make: *** [omc_main_target] Error 1
Compilation process exited with code 2


I tried with Library = .. or LibraryDirectory = .. but it is the same issue.

Somebody could help me ?

Re: Problem with local path of c function

First of all, I would not use the OpenModelica.Scripting functions within a model. Second, the Library annotation only works for string literals or array of string literals. It does not lookup any constants in the code.

You do not need to use the scripting function to refer to the resources directory. Simplify use LibraryDirectory="modelica://MyLibrary/Clibraries/". An object-file is not really a linkable library (needs absolute paths to the o-file, which is not a portable option).

Create an a-file or lib-file instead, name it something like: libMyLibrary.a and add Library="MyLibrary", and OpenModelica will add the magic compiler flags: -L /path/to/MyLibrary/Clibraries -lMyLibrary, which makes the project compile fine.

Re: Problem with local path of c function

Thank you.
I c-file is Document.c and I created "libDocument.a" with OMShell (system("gcc -c -o libDocument.a Document.c") which return 0 so it is ok.
However by replacing LibraryDirectory= localPatho, ... by LibraryDirectory="modelica://Document/Clibraries/" , Library="Document", ... that doesn't work. I have to store the a-file in a particular directory ?


Here my mo-file :
  within ;
  package Interpolate
    constant String [:,2] glibraries := OpenModelica.Scripting.getLoadedLibraries();
    constant String localPath = Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/");
    constant String localPathh = "#include \"" + Modelica.Utilities.Strings.replace(glibraries[2,2],"\\","/") + "/Clibraries/Interpolate.h\"";
    model Test
      extends Modelica.Icons.Example;
      MyTable table1 = MyTable("tab.txt", "table1");
      Real y;
      
    equation
      der(y) = interpolateMyTable(table1, 300 * (time + 1), "S_adm_f");
      annotation(experiment(StartTime = 0.0, StopTime = 1.0, Tolerance = 0.000001));
    end Test;

    class MyTable
      extends ExternalObject;

      function constructor
        input String fileName;
        input String tableName;
        output MyTable table;
       
       
        external "C" table = initMyTable(fileName, tableName, localPath + "/donnees/") annotation( LibraryDirectory="modelica://Document/Clibraries/" , Library="Document", IncludeDirectory = localPathh);
      end constructor;
      
      
      function destructor
        input MyTable table;
     
        external "C" closeMyTable(table) annotation( LibraryDirectory="modelica://Document/Clibraries/", Library="Document" , IncludeDirectory = localPathh);
      end destructor;
      annotation(Documentation(info = "<HTML>
                                        <p>
                                       Subclass of the predefined class ExternalObject allowing to access to external data structures such     as tables which are keeped between calls and passed to an external c-function for interpolation             purposes.
                                        <p>
                                       This class has only two external c-functions named constructor and destructor which are called             once before (resp. after) the first (resp. last) use of the obejct it refers to.
                                       <p>
                                       The tables which are to be interpolated are stored in only one structure containing :
                                       </P>
                                       <UL>
                                           <LI><P >keywords naming the tables,
                                           </P>
                                           <LI><P >for each table, an integer defining the lengths of the table, 
                                           </P>
                                           <LI><P >the data are given under the form of a two-dimensional table the entries of are                 respectively a crank    angle and a value.  The interpolation is carried out between two consecutive         angles</P>
                                       </UL>
                                        </HTML>
                                        "));
    end MyTable;

    function interpolateMyTable
      extends Modelica.Icons.Function;
      input MyTable table;
      input Real u;
      input String Nom_table;
      output Real y;
      
      external "C" y = interpolateMyTable(table, u, Nom_table) annotation( LibraryDirectory="modelica://Document/Clibraries/" , Library="Document", IncludeDirectory = localPathh);
      annotation(Documentation(info = "<HTML>
                                       <p> Interpolate entries of the table \"Nom-table\" sorted in the previously defined structure of                 name \"table\" with the help of the external c-function \" double interpolateMyTable(void* object,             double u, const char* nom_tab)  \"
                                       </HTML>"));
    end interpolateMyTable;
    annotation(Documentation(info = "<HTML>
                                        <p>
                                       Package of functions allowing linear interpolation of data such as : flow rate coefficients according         to the valve lifting laws and the flow direction. These data depend on the cranck angle (between 0         and 720 degres), are stored in a .txt file and arise  from external simulations or testing results
                                        <p><b>Release Notes:</b></p>
                                        <ul>
                                        <li><i>  </i> </li>
                                        </ul>
                                        </HTML>
                                        "));
   
   
  end Interpolate;
 
The compilation results are here :
"C:\OpenModelica1.9.1Beta2\\MinGW\bin\mingw32-make.exe" -j4 -f Interpolate.Test.makefile
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test.o Interpolate.Test.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_functions.o Interpolate.Test_functions.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_records.o Interpolate.Test_records.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_01exo.o Interpolate.Test_01exo.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_02nls.o Interpolate.Test_02nls.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_03lsy.o Interpolate.Test_03lsy.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_04set.o Interpolate.Test_04set.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_05evt.o Interpolate.Test_05evt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_06inz.o Interpolate.Test_06inz.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_07dly.o Interpolate.Test_07dly.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_08bnd.o Interpolate.Test_08bnd.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_09alg.o Interpolate.Test_09alg.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_10asr.o Interpolate.Test_10asr.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_11mix.o Interpolate.Test_11mix.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_12jac.o Interpolate.Test_12jac.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_13opt.o Interpolate.Test_13opt.c
gcc   -falign-functions -msse2 -mfpmath=sse     -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -c -o Interpolate.Test_14lnz.o Interpolate.Test_14lnz.c
gcc -I. -o Interpolate.Test.exe Interpolate.Test.o Interpolate.Test_functions.o Interpolate.Test_records.o Interpolate.Test_01exo.o Interpolate.Test_02nls.o Interpolate.Test_03lsy.o Interpolate.Test_04set.o Interpolate.Test_05evt.o Interpolate.Test_06inz.o Interpolate.Test_07dly.o Interpolate.Test_08bnd.o Interpolate.Test_09alg.o Interpolate.Test_10asr.o Interpolate.Test_11mix.o Interpolate.Test_12jac.o Interpolate.Test_13opt.o Interpolate.Test_14lnz.o -I"C:/OpenModelica1.9.1Beta2//include/omc/c" -I.  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -lDocument    -falign-functions -msse2 -mfpmath=sse     -L"C:/OpenModelica1.9.1Beta2//lib/omc" -L"C:/OpenModelica1.9.1Beta2//lib" -Wl,--stack,0x2000000,-rpath,"C:/OpenModelica1.9.1Beta2//lib/omc" -Wl,-rpath,"C:/OpenModelica1.9.1Beta2//lib"  -lregex -lexpat -lgc -lpthread -fopenmp -loleaut32  -lSimulationRuntimeC -lgc -lexpat -lregex -static-libgcc -luuid -loleaut32 -lole32 -lws2_32 -lsundials_kinsol -lsundials_nvecserial -lipopt -lcoinmumps -lcoinmetis -lpthread -lm -lgfortranbegin -lgfortran -lmingw32 -lgcc_eh -lmoldname -lmingwex -lmsvcrt -luser32 -lkernel32 -ladvapi32 -lshell32 -llapack-mingw -ltmglib-mingw -lblas-mingw -lf2c -linteractive -lwsock32 -llis -lstdc++
c:/openmodelica1.9.1beta2/mingw/bin/../lib/gcc/mingw32/4.4.0/../../../../mingw32/bin/ld.exe: cannot find -lDocument
collect2: ld returned 1 exit status
mingw32-make: *** [omc_main_target] Error 1
Compilation process exited with code 2

It's better because I can see  -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME  -lDocument but I have another problem.



Ps : I need to get the local path (get with constant String [:,2] glibraries := OpenModelica.Scripting.getLoadedLibraries()current/wink in order to send this path to the c function which read the file "tab.txt". This part works and the c function read the good path.

Re: Problem with local path of c function

For information, I use Windows 7

Re: Problem with local path of c function

"Ps : I need to get the local path (get with constant String [:,2] glibraries := OpenModelica.Scripting.getLoadedLibraries()current/wink in order to send this path to the c function which read the file "tab.txt". This part works and the c function read the good path."

No, you don't current/smile https://build.openmodelica.org/Document … ource.html translates from modelica://.../Something.txt into an absolute filename.

You didn't create an a-file though. You created an o-file with .a extension. An a-file is created by running something like "ar -ru libmylib.a mylib1.o mylib2.o mylib3.o". Not that the when specifying -lX, the linker searches for "libX.a", "libX.so", etc.

Re: Problem with local path of c function

I read on "https://modelica.org/documents/ModelicaSpec32Revision2.pdf" that for Windows (a-file and so-file for linux), I need to create dll library for dynamic libraries (page 153). How is it possible with OMShell or other tools (I won't use visual studio) ? The a-file or so-file works on windows ?
I would just use c code with dynamic location for the libraries or includes in order to have a portable OpenModelica file (or application) with dynamic c function extension. Is there a easy way to do this ?

Re: Problem with local path of c function

The Windows version of OpenModelica will not load dll's since it uses MinGW. It will link a-files just fine; these are static libraries.

Re: Problem with local path of c function

But the a-file is automatically stored in the MinGW ? And the ld.exe linker find the file ?

Re: Problem with local path of c function

An a-file is just a set of object-files stored in the ar file format. As long as you generate the objects using the MinGW gcc, it should work just fine. ld.exe will look for any a-file in the given directories. For some reason the -LPath/To/Document/Clibaries is not added though. This usually happens if omc cannot find the directory. The base path it will look in is based on the location of the mo-file.
So if you have the .a-file as C:/OpenModelica1.9.1Beta2/tmp/Test_interp/Clibraries/Document.a and LibraryDirectory="modelica://Interpolate/Clibraries", you should have Interpolate.mo at C:/OpenModelica1.9.1Beta2/tmp/Test_interp/Interpolate.mo

Re: Problem with local path of c function

Thank you for your help. The c code works now with the a-file. Here my mo-file :
  within ;
  package Interpolate
    model Test
      extends Modelica.Icons.Example;
      MyTable table1 = MyTable("tab.txt", "table1");
      Real y;
      
    equation
      der(y) = interpolateMyTable(table1, 300 * (time + 1), "S_adm_f");
      annotation(experiment(StartTime = 0.0, StopTime = 1.0, Tolerance = 0.000001));
    end Test;

    class MyTable
      extends ExternalObject;

      function constructor
        input String fileName;
        input String tableName;
        output MyTable table;
       
       
        external "C" table = initMyTable(fileName, tableName, Modelica.Utilities.Files.loadResource("modelica://Interpolate/donnees/")) annotation( LibraryDirectory = "modelica://Interpolate/Clibraries", Library = "Document");
      end constructor;
      
      
      function destructor
        input MyTable table;
     
        external "C" closeMyTable(table) annotation( LibraryDirectory = "modelica://Interpolate/Clibraries", Library = "Document");
      end destructor;
      annotation(Documentation(info = "<HTML>
                                        <p>
                                       Subclass of the predefined class ExternalObject allowing to access to external data structures such     as tables which are keeped between calls and passed to an external c-function for interpolation             purposes.
                                        <p>
                                       This class has only two external c-functions named constructor and destructor which are called             once before (resp. after) the first (resp. last) use of the obejct it refers to.
                                       <p>
                                       The tables which are to be interpolated are stored in only one structure containing :
                                       </P>
                                       <UL>
                                           <LI><P >keywords naming the tables,
                                           </P>
                                           <LI><P >for each table, an integer defining the lengths of the table, 
                                           </P>
                                           <LI><P >the data are given under the form of a two-dimensional table the entries of are                 respectively a crank    angle and a value.  The interpolation is carried out between two consecutive         angles</P>
                                       </UL>
                                        </HTML>
                                        "));
    end MyTable;

    function interpolateMyTable
      extends Modelica.Icons.Function;
      input MyTable table;
      input Real u;
      input String Nom_table;
      output Real y;
      
      external "C" y = interpolateMyTable(table, u, Nom_table) annotation( LibraryDirectory = "modelica://Interpolate/Clibraries", Library = "Document");
      annotation(Documentation(info = "<HTML>
                                       <p> Interpolate entries of the table \"Nom-table\" sorted in the previously defined structure of                 name \"table\" with the help of the external c-function \" double interpolateMyTable(void* object,             double u, const char* nom_tab)  \"
                                       </HTML>"));
    end interpolateMyTable;
    annotation(Documentation(info = "<HTML>
                                        <p>
                                       Package of functions allowing linear interpolation of data such as : flow rate coefficients according         to the valve lifting laws and the flow direction. These data depend on the cranck angle (between 0         and 720 degres), are stored in a .txt file and arise  from external simulations or testing results
                                        <p><b>Release Notes:</b></p>
                                        <ul>
                                        <li><i>  </i> </li>
                                        </ul>
                                        </HTML>
                                        "));
   
   
  end Interpolate;
 

The a-file has been created with OMShell with this command :
system("gcc -c -a libDocument.a Document.o")

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