My problem is that when compiling with f2py, some module variables are not recognized by functions defined within the module. The errors are raised where variable types of arguments passed to the function are declared (such as a variable describing the type of real
, or dimension elements). I do not get this error when compiling with gfortran. What is the difference and how can I remedy these errors when compiling with f2py?
My example file, moddata.f90
, contains the following code:
module mod
implicit none
integer, parameter :: dp = selected_real_kind(15)
integer, parameter :: nelem = 3
real(kind=dp), dimension(nelem) :: b
parameter (b=(/3,1,2/))
contains
function foo(x,y) result(z)
! dp, nelem are defined in module above
real(kind=dp), intent(in) :: x !scalar
integer, dimension(nelem), intent(in) :: y
! local variable
real(kind=dp) :: z
z = sum(b*y*x)
end function foo
end module mod
and I compile with
f2py -c -m moddata moddata.f90
and I get these errors:
y_Dims[0]=(nelem);
^
1 warning and 1 error generated.reduce to a constant expression
if I redefine integer, parameter :: nelem=3
before integer, dimension(nelem), intent(in) :: y
and recompile, I get
real(kind=dp) foof2pywrap
1
Error: Parameter 'dp' at (1) has not been declared or is a variable, which does not reduce to a constant expression
with same errors for each of the real(kind=dp)
declarations, and
foof2pywrap = foo(x, y)
1
Warning: Possible change of value in conversion from REAL(8) to REAL(4) at (1)
So I have to redefine dp
by integer, parameter :: dp = selected_real_kind(15)
in the function. And then it works.
I don't get these errors when I compile this module with a fortran wrapper. I wonder why nelem
and dp
in the function is not properly scoped with f2py?