How to pass multidimensional functions as argument in f2py?

235 Views Asked by At

I want to call a fortran subroutine from python, where I want to pass a function to the subroutine that has an array as input and output. As a first step I am trying this on a quite simple example, but I cannot even seem to get this to work. F2py seems to have a problem when it gets to this callback function. Here is my

subroutine foo(B, A, f, n)
  integer, parameter :: dp=kind(0.d0)

  integer,  intent(in)  :: n
  real(dp), intent(out) :: B(n)
  real(dp), intent(in)  :: A(n)

  external f

  interface
      function f(arr) result(res)
        integer, parameter :: dp=kind(0.d0)

        real(dp), intent(in) :: arr(:)
        real(dp)             :: res(size(arr))
      end function 
  end interface

  B = f(A)
end subroutine 

The python callback function could look something like this:

def f(x):
    return np.array([2*x[1], x[0]*x[1]])

Currently I am getting the error capi_return is NULL Call-back cb_func_in_foo__user__routines failed.

Edit: Found out that using a subroutine for the callback f instead of a function fixed the problem.

0

There are 0 best solutions below