Re: [f2py] Calling external functions

Pearu Peterson <[email protected]> Wed, 23 Nov 2011 10:55:54 +0200
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
Hi,

There are several issues with the code. For instance, the functions
don't have return values. Due to limitations of f2py, the DP definition
should be repeated in all routine blocks and f2py does not understand
external kw in attributes list so that explicit `external f0` statement
must be used.

The modified Fortran file that builds with f2py is attached.

HTH,
Pearu

On 11/22/2011 05:25 PM, Davor Horvatic wrote:
> I have a major issue with calling external functions in subroutine. All
> code is attached in
> the end of this mail. Code is used in Sage, and the file is read in and
> then fortran command
> called, i.e. f2py wrapper. This code just wont compile to a module and I
> checked
> user manual and exhausted all options. Can you see what I'm doing wrong ?
>
> Sage is producing this error:
>
> getctype: No C-type found in "{'intent': ['out', 'out=f0']}", assuming void.
> getctype: No C-type found in "{'intent': ['out', 'out=f0']}", assuming void.
>
>
> CODE
> -------------------------------------------------
>
>
> !f90
> module r1potgap
> implicit none
>
> integer, parameter :: DP = kind(1.0d0)
>
> real (DP), parameter :: pi=2.D0*ASIN(1.D0)
> real (DP), parameter :: nf=2.d0
> real (DP), parameter :: nc=3.d0
>
> real (DP), parameter :: l0=0.687d0
> real (DP), parameter :: mu=0.0096d0*l0
> real (DP), parameter :: dq0=128.d0/l0**2
>
> contains
>
> function f0(r, res)
> implicit none
>
> real (DP) :: r, res
>
> intent (in) :: r
> intent (out) :: res
>
> !f2py intent (in) r
> !f2py intent (out) res
>
> res=exp(-r/l0**2)
>
> end function f0
>
> function df0(r, res)
> implicit none
>
> real (DP) :: r, res
>
> intent (in) :: r
> intent (out) :: res
>
> ! # Pragmas f2py-specific
> !f2py intent (in) r
> !f2py intent (out) res
>
> res=-exp(-r/l0**2)/l0**2
>
> end function df0
>
> subroutine potqf(r, k, b, t, phi3, m, res)
> implicit none
>
> !f2py intent (callback) f0
> real (DP), external:: f0
>
> real (DP) :: r, k, b, t, phi3, m, res
> real (DP) :: rn2,bamp,meas,tmpgb,tmp
> integer :: alpha
> !f2py rn2 = f0(rn2)
>
> intent (in) :: r, k, b, t, phi3, m
> intent (out) :: res
>
>
> !f2py intent (in) r, k, b, t, phi3, m
> !f2py intent (out) res
>
> meas=((4.d0*pi)/(2.d0*pi)**3)*(r**2)
>
> tmp=0
>
> DO alpha = -1,1,1
>       rn2 = r**2 + ((2.d0*k+1.d0)*pi*t + alpha*phi3)**2
>       bamp = m+b*f0(rn2)
>       tmpgb = -2.d0*t*meas*(log(rn2+bamp**2)-log(rn2+m**2))
>       tmp = tmp+tmpgb
> ENDDO
>
> res=tmp
>
> end subroutine potqf
>
> end module r1potgap
>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users

_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users
r1potgap.f90 (text/x-fortran, 1.4 KB)
!f90
module r1potgap
implicit none

integer, parameter :: DP = kind(1.0d0)

real (DP), parameter :: pi=2.D0*ASIN(1.D0)
real (DP), parameter :: nf=2d0
real (DP), parameter :: nc=3d0

real (DP), parameter :: l0=0.687d0
real (DP), parameter :: mu=0.0096d0*l0
real (DP), parameter :: dq0=128.d0/l0**2

contains

function f0(r) result (res)
implicit none
integer, parameter :: DP = kind(1.0d0)
real (DP) :: r, res

intent (in) :: r
!intent (out) :: res

!f2py indent (in) r
!!f2py indent (out) res

res=exp(-r/l0**2)

end function f0

function df0(r)  result (res)
implicit none
integer, parameter :: DP = kind(1.0d0)
real (DP) :: r, res

intent (in) :: r
!intent (out) :: res

! # Pragmas f2py-specific
!f2py intent (in) r
!!f2py intent (out) res

res=-exp(-r/l0**2)/l0**2

end function df0

subroutine potqf(r, k, b, t, phi3, m, res)
implicit none
integer, parameter :: DP = kind(1.0d0)

!f2py intent (callback) f0
!f2py external f0
real (DP), external:: f0

real (DP) :: r, k, b, t, phi3, m, res
real (DP) :: rn2,bamp,meas,tmpgb,tmp
integer :: alpha
!!f2py rn2 = f0(rn2)

intent (in) :: r, k, b, t, phi3, m
intent (out) :: res


!f2py intent (in) r, k, b, t, phi3, m
!f2py intent (out) res

meas=((4.d0*pi)/(2.d0*pi)**3)*(r**2)

tmp=0

DO alpha = -1,1,1
     rn2 = r**2 + ((2.d0*k+1.d0)*pi*t + alpha*phi3)**2
     bamp = m+b*f0(rn2)
     tmpgb = -2.d0*t*meas*(log(rn2+bamp**2)-log(rn2+m**2))
     tmp = tmp+tmpgb
ENDDO

res=tmp

end subroutine potqf

end module r1potgap