0

I can compile the following code just fine using gfortran -c test.f90.

module math
    contains
    function addition(a, b) result(f)
        use iso_fortran_env, only: REAL64
        implicit none
        real(REAL64), intent(in) :: a, b
        real(REAL64) :: f
        f = a + b
    end function
end module

I use f2py

python -m numpy.f2py -c test.f90 -m test

and I get the following error

/tmp/tmpJLxRSe/src.linux-x86_64-2.7/test-f2pywrappers2.f90:7:16:

       real(kind=real64) a
                1
Error: Parameter ‘real64’ at (1) has not been declared or is a variable, which does not reduce to a constant expression

I have verified that I can successfully use selected_real_kind(15,307), as suggested in this SO post. However, I was interested in using iso_fortran_env, if possible.

francescalus
  • 30,576
  • 16
  • 61
  • 96
David Hansen
  • 501
  • 4
  • 14
  • The error message you show does not match the code. Try posting the code that actually corresponds to error? – evets Dec 11 '20 at 20:37
  • I used copy and paste for the code above. If it helps I am using gfortran 7.5.0 and numpy 1.13.3. – David Hansen Dec 11 '20 at 21:01
  • Also try `use, intrinsic :: iso_fortran_env, only : real64` to be clear you want the intrinsic module. – francescalus Dec 11 '20 at 21:24
  • @francescalus Thank you for the suggestion to include the intrinsic keyword. I tried it and verified that it compiles fine when not using f2py. However, I get the same error message as above when using f2py. – David Hansen Dec 11 '20 at 21:32
  • 3
    f2py does not work well with modern Fortran. – Vladimir F Героям слава Dec 11 '20 at 21:51
  • As Vladimir F says, it's a limitation of f2py, but there is a workaround, see e.g. https://stackoverflow.com/a/61869003/3967096 – jbdv Dec 11 '20 at 23:28
  • You did not show the code that gfortran is trying to compile. You are showing an error message for code that python has mutilated. – evets Dec 11 '20 at 23:55

1 Answers1

0

Simplest way is to just use real(8) or real*8 as long as you are using gfortran or ifort with F2PY.

Alternatively, as mentioned for example here you could possibly solve it by making a file .f2py_f2cmap with the content:

dict(real=dict(REAL64='double'))

But anyway the iso_fortran_env is then not relevant anymore. An alternative is to call the real code from a simplified fortran subroutine that has only real(8) definitions.

Jonatan Öström
  • 2,428
  • 1
  • 16
  • 27