i have trouble in python calling fortran dll , want , advice. problem passing dynamic array function(provided fortran dll) .
for instance , have type dynamic array:
module class_rotor implicit none type,public ::type_rotor real(kind=8),public::mass real(kind=8),allocatable,public::lamda(:,:) end type type_rotor end module class_rotor module class_trim implicit none type,public::type_trim real(kind=8),public::coll real(kind=8),public::lngcyc end type type_trim end module class_trim and want use dynamic array in fortran subroutine
subroutine calculaterotor(trim,rotor) !dec$ attributes dllexport, stdcall, reference, mixed_str_len_arg, alias:"calculaterotor" :: calculaterotor use class_rotor use class_trim type(type_trim),intent(in)::trim type(type_rotor),intent(inout)::rotor rotor%mass = trim%coll + trim%lngcyc end subroutine calculaterotor subroutine calculatetrim(rotor,trim) !dec$ attributes dllexport, stdcall, reference, mixed_str_len_arg, alias:"calculatetrim" :: calculatetrim use class_rotor use class_trim type(type_rotor),intent(in)::rotor type(type_trim),intent(inout)::trim trim%coll = rotor%mass+rotor%lamda(2,2) end subroutine calculatetrim in addition,i define sunroutine alc allocate space of rotor%lamda,and pass value it.
subroutine alc(rotor,n,lamarray) !dec$ attributes dllexport, stdcall, reference, mixed_str_len_arg, alias:"alc" :: alc use class_rotor type(type_rotor),intent(inout):: rotor integer(kind=4)::i,j integer(kind=4),intent(in)::n real(kind = 8),dimension(n,n),intent(in)::lamarray if(allocated(rotor%lamda))then deallocate(rotor%lamda) endif allocate(rotor%lamda(n,n)) i=1,n j=1,n rotor%lamda(i,j)=lamarray(i,j) enddo enddo end subroutine alc i try this: new fortran dll project(vs2010+ivf), use dll in python:
from ctypes import * import numpy np numpy.ctypeslib import load_library,ndpointer class type_rotor(structure): _fields_ = [ ('mass', c_double), ('lamda', pointer(c_double)), ] def __init__(self,cols): self.cols_count = cols pc = (pointer(c_double)*cols*cols)() self.lamda = cast(pc,pointer(c_double)) class type_trim(structure): _fields_ = [ ('coll', c_double), ('lngcyc', c_double), ] def run(): mydll = windll.loadlibrary('dll3.dll') calculaterotor = mydll.calculaterotor calculatetrim = mydll.calculatetrim rotor = type_rotor(3) trim = type_trim() rotor.mass = 1.0 temp = (c_double *3*3) ((1,2,3),(4,5,6),(7,8,9)) trim.coll = 11.0 trim.lngcyc = -3.0 mydll.alc(byref(rotor),byref(c_int(rotor.cols_count)),byref(temp)) in range(15): calculaterotor(byref(trim), byref(rotor)) print trim.coll,trim.lngcyc,rotor.mass calculatetrim(byref(rotor), byref(trim)) print trim.coll,trim.lngcyc,rotor.mass if __name__=="__main__": run() under win7,the first time run python scripts command line,return answer that:a pointer passed deallocate points object cannot deallocated, second time gives right answer
under winxp,the first time run python scripts command line,return answer that:a pointer passed deallocate points object cannot deallocated, second time gives right answer,but,then crush!!!somtimes,it return :allocatable array allocated
i don't know why? , how use dynamic array?
Comments
Post a Comment