Now I am using the f2py
to call Python function from Fortran code. I have tried a very easy example but it didn't work.
Fortran90 code:
subroutine foo(fun,r)
external fun
integer ( kind = 4 ) i
real ( kind = 8 ) r
r=0.0D+00
do i= 1,5
r=r+fun(i)
enddo
end
using the commandline:
f2py -c -m callback callback.f90
Python code:
import callback
def f(i):
return i * i
print callback.foo(f)
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: `Required argument 'r' (pos 2) not found`
You need to declare r
as a return value... this is good Fortran 90 practice anyway. Right now f2py
is assuming it is an input value.
subroutine foo(fun,r)
external fun
real ( kind = 8 ), intent(out) :: r
integer ( kind = 4 ) :: i
r=0.0D+00
do i= 1,5
r=r+fun(i)
enddo
end
f2py
uses Fortran's intent directives to determine what is passed to the function and what is returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With