I try to call from Python several routines contained in a .so library generated from some Fortran 90 routines. All the objects in the library are resolved using the -fPIC flag when compiling the .so.
I have a sample code which looks like
Expand|Select|Wrap|Line Numbers
- from numpy import *
- from ctypes import *
- lib_test = cdll.LoadLibrary("./lib_test.so")
- method = lib_test.__test_MOD_wrap_xbelem
- method.restype = None
- MaxElNod = 3
- NumElems = 2
- NumNodes = (c_int *NumElems)()
- Length = zeros(NumElems, dtype=double, order="Fortran")
- RBMass = zeros((MaxElNod*NumElems,6,6), dtype=double, order="Fortran")
- # Call the function in the .so
- method(byref(c_int(NumElems)), byref(NumNodes), Length.ctypes.data_as(POINTER(c_double)), RBMass.ctypes.data_as(POINTER(c_double)) )
1) The code works fine as given above. However, I would like to pass the integer array (bold font above) as done with the array of type double. I would be keen to have a code which looks more like this
Expand|Select|Wrap|Line Numbers
- NumNodes = zeros(NumElems, dtype=int, order="Fortran")
- method(byref(c_int(NumElems)), NumNodes.ctypes.data_as(POINTER(c_int)), Length.ctypes.data_as(POINTER(c_double)), RBMass.ctypes.data_as(POINTER(c_double)) )
2) The other question I have is about derived types. The Fortran 90 code makes use of derived types, and this is the reason that we have decided to create a .so library out of it to work with Python. At the moment, the Python script does not work with a corresponding structure and the fields of the derived types have been given each to a separate variable. This means that several input arguments are passed to a wrapper in the .so, and then appropriate fields are compacted to form a derived type in Fortran90. Arrays are also converted to multi-dimensional entities when it is the case.
I suppose I can ask for your expertise on some further tips. Imagine in Fortran 90 there is a derived type of the form
Expand|Select|Wrap|Line Numbers
- type xbelem
- integer:: NumNodes
- real(8):: Length
- real(8):: RBMass(MaxElNod,6,6)
- end type xbelem
Expand|Select|Wrap|Line Numbers
- type(xbelem),intent(inout):: Elem(NumElems)
I would like to thank for any help you can provide.
Kind regards,
Andrea.