Oct-25-2016, 03:44 AM
Recently Ive tried using Fortran subroutine with ctypeps in Python
the question is how can I send array to fortran?
here are my Fortran code and python code.
could you help me please!!
thanks..
the question is how can I send array to fortran?
here are my Fortran code and python code.
subroutine add_array(a,b,N) implicit none real(8),dimension(0:1024),intent(in) :: a real(8),dimension(0:1024),intent(inout):: b integer(8),intent(in) :: N if (N.LT.1024) then write(*,*) "Maxsize is too small." end if b(0:N-1) = b(0:N-1) + a(0:N-1) end subroutine stop end
#!/usr/bin/env python #vim:fileencoding=utf8 from ctypes import * import numpy as np add_np = cdll.LoadLibrary("C:\\Users\\Owner\\Desktop\\Forpython\\add_np\\add_np\\add_np\\Debug\\add_np.dll") add_np.add_array_.argtypes = [ np.ctypeslib.ndpointer(dtype=np.float64),#a np.ctypeslib.ndpointer(dtype=np.float64),#b POINTER(c_int64),] #N add_np.add_array_.restype = c_void_p #b a = np.arange(0.,10.,dtype=np.float64) # 0,1,2,3,4,5,..,9 b = a*2 # 0,2,4,6,8,...,18 N = byref(c_int64(b.N)) add_np.add_array_(byref(a),byref(b),byref(N)) print b # [ 0 3 6 9 12 .. 27 ]but Python message is
Error:Traceback (most recent call last):
File "C:\Users\Owner\Desktop\Forpython\add_np\add_np.py", line 11, in <module>
POINTER(c_int64),] #N
File "C:\Python27\lib\ctypes\__init__.py", line 375, in __getattr__
func = self.__getitem__(name)
File "C:\Python27\lib\ctypes\__init__.py", line 380, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'add_array_' not found
and are these codes right?could you help me please!!
thanks..