0

I have a legacy code in Fortran for creation of FDq-matrices. The code consists of a subroutine, which I would like to be able to call in Python. This subroutine itself uses Fortran modules.

Subroutine which I need to call from Python:

  • finiteDifferences.f90

Modules used within subroutine:

  • Lagrange_Interpolation_Method.f
  • Linear_Algebra_Lapack.f
  • Nonlinear_Algebra.f
  • Piecewise_Polynomial_Interpolation.f
  • Optimum_Grid_Methods.f

Ideally, I would like to end up with an fdq.so file which I could import into my python code.

I am perfectly capable of using F2PY to create .so file from a single subroutine, even including Lapack. But I don't know how to work with subroutines which call for modules.

For simple compilation I add a program main.f90 which calls the subroutine and I use following command:

gfortran -O3 -o fdq -fcheck=all -ffree-form -fdefault-double-8 -fdefault-real-8 -framework Accelerate -Wall Lagrange_Interpolation_Method.f90 Nonlinear_Algebra.f90 Linear_Algebra_Lapack.f90 Piecewise_Polynomial_Interpolation.f90 Optimum_Grid_Methods.f90 finiteDifferences.f90 main.f90

Then when calling for F2PY with following command:

f2py3 -c --verbose --opt='-O3 -fcheck=all -ffree-form -fdefault-double-8 -fdefault-real-8 -framework Accelerate' Lagrange_Interpolation_Method.f90 Nonlinear_Algebra.f90 Linear_Algebra_Lapack.f90 Piecewise_Polynomial_Interpolation.f90 Optimum_Grid_Methods.f90 finiteDifferences.f90 -m fdq  

The first error produced by the above command:

/var/folders/bj/4qqzkp4560d75dw4wr7tpmr00000gn/T/tmp39eelon3/src.macosx-13-arm64-3.11/fdqmodule.c:578:23: error: expected expression nodes_Dims[0]=1 + ; ^
vpezlar
  • 1
  • 1
  • What is your problem? Is there anything wrong when you run the command you show at the end? Does the compilation finish or are there anu error messages? Are what you call "modules" actual Fortran modules or just source files with external procedures? – Vladimir F Героям слава Jul 12 '23 at 18:44
  • The modules are actual Fortran modules. The first error which is given after using the last command is: /var/folders/bj/4qqzkp4560d75dw4wr7tpmr00000gn/T/tmp39eelon3/src.macosx-13-arm64-3.11/fdqmodule.c:578:23: error: expected expression nodes_Dims[0]=1 + ; ^ – vpezlar Jul 12 '23 at 19:07
  • Do I understand correctly, that it is a syntax error in the .c file created by the F2PY? – vpezlar Jul 12 '23 at 19:08
  • Please [edit] the question to include the description of the errors. Do not use comments for such information, they can be short-lived. Yes, I think it is an error in the produced C file. – Vladimir F Героям слава Jul 12 '23 at 19:26
  • Thank you. Do you have any suggestions or ideas where this error is coming from? Fortran side, or do I have to change my compilation commands? – vpezlar Jul 12 '23 at 22:13
  • f2py is old, buggy, and does not support complete Fortran. Without the code we cannot say which part causes the problem. Perhaps one could just link with pre-compiled .o files instead of putting everything into f2py. – Vladimir F Героям слава Jul 12 '23 at 22:28
  • I'm not sure because I almost do not use Python, let alone calling Fortran from Python, but I think the other way is to write a wrapper on the Fortran side to prepare calling it from C, and on the Python side use the usual techniques to call C code. – PierU Jul 13 '23 at 06:07
  • @PierU Thank you for the recommendations. Unfortunately, I am not proficient with C and I don't want to spend massive amounts of time on this issue atm. I'll go on for now with interfacing via textfiles and put this on "low-heat" for now :D – vpezlar Jul 13 '23 at 17:18
  • Actually this solution doesn't require any C code between Python and Fortran. – PierU Jul 13 '23 at 17:27
  • @VladimirFГероямслава I tried that, but it also didn't work. I also tried f90wrap https://github.com/jameskermode/f90wrap, but not much difference. As I said in the previous comment. Will leave it for later time... – vpezlar Jul 13 '23 at 17:33
  • @PierU is that the iso-c-bindings approach? I have done that yet – vpezlar Jul 13 '23 at 17:34
  • Yes, you should use `iso_c_binding` for that – PierU Jul 13 '23 at 18:53

0 Answers0