Recent

Author Topic: Calling Fortran code from Pascal / Mixing Fortran and Pascal / ODE-Pack  (Read 4529 times)

CG

  • New Member
  • *
  • Posts: 10
Hi everybody,

I am working on a Pascal based simulation tool dealing with a system of differential equations to estimate the response of a chemical network upon change of temperature.

I am interested in different options to numerically integrate the system of differential equations with good accuracy.

So far I tried the ODE unit that is included in the Lazarus IDE suit. To me it seems that this is an implementation of the runge kutta method for integration of differential equations. However, the code is mainly uncommented, there are hardly any examples for using the ODE unit and to me it seems that the code is no longer maintained. Therefore, despite of being successful (that is getting things started and some reasonable output) I decided to look into alternative solutions.

The second thing I tried was the DMath library. This library contains well commented code. There is very good and extensive documentation about usage of that library including even examples that are ready for “plug and play”. The author did a magnificent job.

After some time, I still decided that I need an alternative solution. The DMath library as the ODE unit just contain a “basic/standard” implementation of the runge kutta method for integration of differential equations. I noted that simulation of my network becomes significantly slowed down when I am trying to describe certain network conditions. I suspect that my differential equation system is then transferred into a stiff differential equation system. It is known that stiff differential equation systems cannot be efficiently solved by the “basic/standard” runge kutta method.

Thus, I am looking for ODE solvers that can handle stiff and non-stiff differential equations at the same time, e.g. switching automatically to the appropriate implementation that does the solving job. Searching the web I found the ODE Pack offering several of such solvers. However, the ODE Pack is legendary code written in Fortran back some years ago. Even today the Fortran code is heavily used e.g. in Octave, SciPy and other modern tools. Unfortunately, I haven’t found any ports to C/C++ or Pascal. Since I am not fluent in Fortran and because of time restrictions I would like to avoid transcoding Fortran to Pascal.

I am using Lazarus 1.6.4 with FPC 3.0.2 in a Windows environment and have a couple of questions:
  • Is there anybody who has ever tried to make Fortran code callable from Pascal code?
  • Is there anybody who has specifically tried to get the ODE pack running in a Lazarus environment?
  • What would be a good strategy to get things running with a minimum budget of time? Can the job be done spending a week-end?
  • Would it be possible to write a somehow generic pascal wrapper for the Fortran code to have easily and smoothly access to the originally Fortran code?
  • I found general info regarding mixing Fortran with Pascal here (http://alignment.hep.brandeis.edu/Software/Mixing/Mixing_Manual.html). Is this approach compatible with a Windows environment? Or would I have to use gcc in a Linux environment cross-compiling for windows?
Looking forward to any hints. Thanks a lot!

Best,
Christian

other Links/References:
stiff equations (https://en.wikipedia.org/wiki/Stiff_equation)
DMath library (https://sourceforge.net/projects/dmath/), (http://wiki.freepascal.org/DMath)
ODE Pack (https://computation.llnl.gov/casc/odepack/)

Phil

  • Hero Member
  • *****
  • Posts: 2737
Looking forward to any hints. Thanks a lot!

Is the Fortran code compiled to dynamic libraries? (.dll on Windows, .so on Linux)

If so, are C header files included for calling the libraries from C? These could be parsed to create Pascal external function declarations.

If not, you can write the Pascal external function declarations yourself.

You should upgrade to FPC 3.0.4.

CG

  • New Member
  • *
  • Posts: 10
Hi Phil,

thanks for your prompt reply!

At the ODEPack web page there is only the Fortran Code available. Therefore, I still have to build the DLL. Currently, I’m considering using gcc for that task. Once I have the DLL I will write external function declarations as suggested.

I will update FPC as soon as I can find some time. Is there a time efficient updating process? Or do I have to deinstall and run through the complete installation process again. If I remember correctly I installed a FPC+LazarusIDE bundle. I don’t want to break a working system by updating a single component.

Thanks,
Christian

 

TinyPortal © 2005-2018