1

I have a Modelica file that references c code during simulation through an external library *.a file.

For example:

model CallAdd
    input Real FirstInput(start=0);
    input Real SecondInput(start=0);
    output Real FMUOutput(start=0); 
    function CAdd
        input Real x(start=0);
        input Real y(start=0);
        output Real z(start=0);
        external "C"  annotation(Library = "CAdd", LibraryDirectory = "modelica://CallAdd");
    end CAdd;

equation
    FMUOutput = CAdd(FirstInput,SecondInput);
    annotation(uses(Modelica(version = "3.2.1")));
end CallAdd;

When opening the Modelica model in OpenModelica the required files appear to be automatically loaded because it simulates and gives appropriate results.

However, when I try to compile the Modelica file with JModelica-SDK-1.12 I receive an error that the library *.a file could not be found.

So my question is: What is the proper way to reference additional files when using compile_fmu in JModelica?

With no success, I've tried:

# Import the compiler function
from pymodelica import compile_fmu
model_name = "CallAdd"
mo_file = "CallAdd.mo"

# Compile the model and save the return argument, for use later if wanted
my_fmu = compile_fmu(model_name, mo_file, target="cs",compiler_options = {'extra_lib_dirs':'C:/ToFolderContainingLib/'})

The strange thing is that when I was using JModelica-1.17 (non-SDK) the file compiled fine but the results didn't make sense. I was recommended to try the SDK version to see if it fixed my errors in my previous post here.

Community
  • 1
  • 1
Justin Shultz
  • 199
  • 1
  • 11

2 Answers2

2

If is a small piece of C code, as a last alternative you could try to include the C file directly in the Modelica code:

external "C"  annotation(Include="
// the entire C code here
");

Hopefully the JModelica people will give you a better answer soon. You could try to ask this on their website also: http://www.jmodelica.org/forum

Adrian Pop
  • 4,034
  • 13
  • 16
  • Thank you for your quick reply. I'll give it a try. When I've tried putting the C code directly into a Modelica file in the past I received errors like, "Undefined reference to function". In other words, when the model is instantiated the function is lost. For reference: https://www.dropbox.com/s/lrz2sy0yics80w5/CallDirect.mo?dl=0 – Justin Shultz Aug 24 '16 at 22:03
  • I seemed to have figured it out. I believe the C function name needs to match the name of the Modelica function name, is that correct? Is there a syntax that I should follow when working between C and Modelica. I have found limited information? The Xogeny book (http://book.xogeny.com/behavior/functions/external/) and this interoperability post (https://www.openmodelica.org/doc/OpenModelicaUsersGuide/latest/interop_c_python.html) has been the best resources I've found thus far. But debugging often requires deeper information. Thank you for your time. – Justin Shultz Aug 24 '16 at 22:31
2

Try positioning the external library in sub-folder named as the platform your currently on. So in your example, I'd position the library (libCAdd.a) in sub-folder named linux64, as I'm on a 64bit Linux machine and then run the code.

Christian Winther
  • 1,113
  • 1
  • 9
  • 17