thermotools / thermopack

Thermopack is a thermodynamic model library for fluid properties and PVT calculations
Other
52 stars 13 forks source link

Issues with cpp interface #118

Closed jenrei closed 10 months ago

jenrei commented 10 months ago

Der thermopack community,

I was thinking about the possibility of integration of thermopack with Julia and Modelica but encountered some issues

  1. Are there any ongoing efforts for Julia oder Modelica integration?
    • To my knowledge Julia supports calling C and FORTRAN via ccall but I'm completey unsure about mangling and datatypes
    • Modellica, to my knowledge, support calling C and FORTRAN77 not C++ or FORTRAN90 so additional wrapping might be necessary
  2. As a next step I was looking at the cpp interface example provided in include/test_includes
    • I was able to compile test.cpp after commenting everything concerning RGAS, TMIN and TMAX in test.cpp and thermopack.h
    • I get a segmentation fault as soon as thermopack_init_c is called. I'm was using current main branch with gfortran-7.5 on OpenSUSE Linux

I would appreaciate any insight about the feasability of integration of thermopack in either Julia or Modelica. Or on the issues concerning the cpp interface. Thanks in advance

Reinhard

vegardjervell commented 10 months ago

Hi, and thank you for your interest!

We have been discussing the creation of a more complete and robust interface to C++ for a future release, but I must admit that it will probably not be complete before a little way down the line. For a look at the wrapping approach we are currently considering, you can take a look at the beta_v3/main branch, and the prototype directory.

I don't have experience with julia or modelica, but maybe @morteham has some input on how the name mangling is handled in the fortran binaries.

morteham commented 10 months ago

Hi,

Looks like the C interface is a bit out of sync with some changes to the code. We will try to fix the issues in order to make the examples run. In any case the interface has very limited functionally, and we need to improve this.

I have no experience with Julia or Modelica, but if C interfacing is possible, interfacing Thermopack should be doable. The python wrapper accesses a lot of the code using ctypes, which is a form of C interfacing.

vegardjervell commented 10 months ago

Regarding the segfault: Parameters that are optional in fortran must be "pointer-to-pointer" in C/C++, and when the parameter is "not passed", you should pass in a pointer to nullptr. If a plain nullptr is passed in, Fortrans if (present(ptr)) will trigger a segfault.

As Morten says: The C interface appears to be a bit out of sync, we can take a look at that to bring the basic functionality back up to date asap. To read more, I recommend a look at this readme, specifically the "Interfacing" section.