Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making python and fortran friends

Assume we need to call fortran function, which returns some values, in python program. I found out that rewriting fortran code in such way:

subroutine pow2(in_x, out_x)
      implicit none
      real, intent(in)      :: in_x
!f2py real, intent(in, out) :: out_x
      real, intent(out)     :: out_x
      out_x = in_x ** 2
      return
end

and calling it in python in such way:

import modulename
a = 2.0
b = 0.0
b = modulename.pow2(a, b)

gives us working result. Can I call fortran function in other way, cause I think the first way is a bit clumsy?

like image 412
user983302 Avatar asked Nov 19 '12 21:11

user983302


1 Answers

I think you just need to change your f2py function signature slightly (so that out_x is only intent(out) and in_x is only intent(in)):

subroutine pow2(in_x, out_x)
  implicit none
  real, intent(in)   :: in_x
  !f2py real, intent(in) :: in_x
  real, intent(out)     :: out_x
  !f2py real, intent(out) :: out_x
  out_x = in_x ** 2
  return
end subroutine pow2

Now compile:

f2py -m test -c test.f90

Now run:

>>> import test
>>> test.pow2(3)   #only need to pass intent(in) parameters :-)
9.0
>>>

Note that in this case, f2py is able to correctly scan the signature of the function without the special !f2py comments:

!test2.f90
subroutine pow2(in_x, out_x)
  implicit none
  real, intent(in)   :: in_x
  real, intent(out)     :: out_x
  out_x = in_x ** 2
  return
end subroutine pow2

Compile:

f2py -m test2 -c test2.f90

run:

>>> import test2
>>> test2.pow2(3)   #only need to pass intent(in) parameters :-)
9.0
>>>
like image 106
mgilson Avatar answered Sep 18 '22 15:09

mgilson