Aug-30-2023, 07:30 PM
Hi,
I am just trying to learn creating Python bindings to legacy Fortran using the ctypes module. My code is segfaulting in the Fortran function invocation. I was not able to decipher what gdb was telling me. I would be grateful to be pointed in the right direction here.
A simple Fortran function I am trying to bind is given below. It was compiled using "gfortran -shared euler.f -o euler.so"
I am just trying to learn creating Python bindings to legacy Fortran using the ctypes module. My code is segfaulting in the Fortran function invocation. I was not able to decipher what gdb was telling me. I would be grateful to be pointed in the right direction here.
A simple Fortran function I am trying to bind is given below. It was compiled using "gfortran -shared euler.f -o euler.so"
subroutine euler(f, neq, y, t, tout, h) implicit none real y(neq), t, tout, h, dy(neq), ti integer neq, i, stepnum stepnum = 1 ti = t c Euler iteration 10 continue call f( neq, t, y, dy ) do 20 i = 1, neq y(i) = y(i) + h*dy(i) 20 continue t = ti + stepnum*h stepnum = stepnum + 1 if ( t < tout ) goto 10 return endThe Python code calling this Fortran subroutine is
import ctypes as ct flib = ct.CDLL('euler.so') ode_spec = ct.CFUNCTYPE(None, ct.c_int, ct.c_float, ct.POINTER(ct.c_float), ct.POINTER(ct.c_float)) flib.euler_.restype = None flib.euler_.argtypes = [ ct.POINTER(ode_spec), ct.c_int, ct.POINTER(ct.c_float), ct.c_float, ct.c_float, ct.c_float ] @ode_spec def df(neq, t, y, dy): dy[0] = y[0] return t = 0.1 to = 0.2 neq = 1 realarr = ct.c_float * neq y = realarr(0.1) h = 0.001 flib.euler_(df, len(y), y, t, to, h)