[f2py] (no subject)

pearu at cens.ioc.ee pearu at cens.ioc.ee
Mon Jan 23 21:40:39 EET 2006



On Mon, 23 Jan 2006, LOPEZ GARCIA DE LOMANA, ADRIAN wrote:

> 
> Hi people, 
> 
> I am a Python user. While writting a program, I need to call a Fortran routine for doing some optimizations. I found f2py and started using it. But there is something I'm stuck with and if someone have any idea will be vey helpful.
> 
> >From the Fortran code I need to calculate a value which is given by a Python function. The problem is that the Fortran routine that calls the Python function is not directly called from the Python program. I'm trying to do something like this (I hope to be able to explain myself):
> 
> Python:
> 
> import fortran_program
> 
> def fun(x):
> 
>     x = x*2
> 
>     return x
> 
> fortran_program.principal()
> 
> 
> Fortran:
> 
>       subroutine principal   
> 
>       integer a
> 
>       a = 1
> 
>       write(*, *) a
> 
>       call foo
> 
>       return
>       
>       end
> 
>       subroutine foo
> 
> cf2py intent(callback) fun
> 
>       external fun
> 
>       real y
> 
>       y = 3.
>       
>       y = fun(y)
> 
>       write(*, *) y
> 
>       return
>       
>       end
> 
> This is not working. If I call the Python function fun from the subroutine principal, it works, but I guess fun is not properly defined at foo.
> 
> If someone could give me some suggestions, it would be great, 

fun must be specified in foo argument list and then don't use
intent(callback):

      subroutine foo(fun)

      external fun 
 
      real y
 
      y = 3.
      
      y = fun(y)
      
      write(*, *) y
      
      return
      
      end

Or, use the following code

      subroutine foo()

cf2py intent(callback,hide) fun
      external fun 
 
      real y
 
      y = 3.
      
      y = fun(y)
      
      write(*, *) y
      
      return
      
      end

Then after `f2py -c foo.f -m t` the following works:

>>> import t
>>> t.fun = lambda x:x+2
>>> t.foo()
  5.

HTH,
Pearu




More information about the f2py-users mailing list