e2 = 0 ain = [2,3,4] aout = [] calldll.call _foreign_function(addr,''hhll'',''l'',(e1,e2,ain,aout)) 回溯(最近一次调用最后一次): 文件"<互动输入>",第1行,在? TypeError:需要一个整数 有没有人提供与Pyfort类似的示例,F2Py或calldll? 提前致谢。 解决方案 属性DLLEXPORT ::样品 !DEC 属性别名:''SAMPLE'':: SAMPLE INTEGER,INTENT(OUT):: IERR1 ,IERR2 REAL * 8,INTENT(IN):: AIN(*) REAL * 8,INTENT(OUT):: AOUT(*) C C ***设定阵列的最大预期元素AIN和AOUT C 参数(MAXNVIN = 101,MAXNVOUT = 200) C C ***设置错误指标为零 C IERR1 = 0 IERR2 = 0 C C ***获取输入值的数量 C NVIN = AIN(1) C ***如果超过最大预期设置错误和返回数量 IF(NVIN .GT。 MAXNVIN)那么 IERR1 = 1 IERR2 = 1 返回 ENDIF C C ***设定输出值数 C NVOUT = 2 * NVIN C ***如果数量超过最大预期设置错误和返回 IF(NVOUT .GT.MAXNVOUT)那么 IERR1 = 1 IERR2 = 2 返回 ENDIF C C *** INITIALIZE AOUT INDEX C JOUT = 0 C C ***为每个输入值计算两个输出值 C DO I = 1,NVIN JOUT = JOUT + 1 AOUT(JOUT)= 2. * AIN(I + 1) JOUT = JOUT + 1 AOUT(JOUT)= 3. * AIN(I + 1) END DO 返回 结束 编译成dll并从另一个Fortran程序调用此dll 成功,所以这告诉我dll没关系。 这就是我试图从python 2.4调用它的方法 import calldll handle = calldl l.load_library(''c:/sample_dll.dll'') addr = calldll.get_proc_address(handle,''SAMPLE'') #so far good ,我有一个句柄和地址 e1 = 0 e2 = 0 ain = [2,3,4] aout = [] calldll.call_foreign_function(addr,''hhll'',''l'',(e1,e2,ain,aout)) Traceback(最近一次调用最后一次): 文件"<交互式输入>",第1行,在? TypeError:需要一个整数 有没有人提供与Pyfort,F2Py或calldll类似的例子? 提前致谢。 Mangabasi写道: 有没有人提供与Pyfort,F2Py或calldll类似的例子? 使用最新的f2py numpy: Howdy, I have been trying to call the following Fortran function from Python(using Windows XP, Compaq Fortran and Python 2.4). I tried F2Py,Pyfort and calldll with no success. I think I came very close with calldll. Here is a short summary Fortran code: SUBROUTINE SAMPLE(IERR1,IERR2,AIN,AOUT)CC SIMPLE SAMPLE OF A DLLC!DEC$ATTRIBUTES DLLEXPORT :: SAMPLE!DEC$ATTRIBUTES ALIAS:''SAMPLE'' :: SAMPLEINTEGER,INTENT(OUT) :: IERR1,IERR2REAL*8,INTENT(IN) :: AIN(*)REAL*8,INTENT(OUT) :: AOUT(*)CC *** SET MAXIMUM EXPECTED ELEMENTS OF ARRAY AIN AND AOUTCPARAMETER (MAXNVIN=101,MAXNVOUT=200)CC *** SET ERROR INDICATORS TO ZEROCIERR1=0IERR2=0CC *** GET NUMBER OF INPUT VALUESCNVIN=AIN(1)C *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURNIF(NVIN .GT. MAXNVIN) THENIERR1=1IERR2=1RETURNENDIFCC *** SET NUMBER OF OUTPUT VALUESCNVOUT=2*NVINC *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURNIF(NVOUT .GT. MAXNVOUT) THENIERR1=1IERR2=2RETURNENDIFCC *** INITIALIZE AOUT INDEXCJOUT=0CC *** COMPUTE TWO OUTPUT VALUES FOR EACH INPUT VALUECDO I=1,NVINJOUT=JOUT+1AOUT(JOUT)=2.*AIN(I+1)JOUT=JOUT+1AOUT(JOUT)=3.*AIN(I+1)END DORETURNEND compiled it to a dll and called this dll from another Fortran programwith success, so this tells me that dll is OK. This is how I tried to call it from python 2.4 import calldllhandle = calldll.load_library(''c:/sample_dll.dll'')addr = calldll.get_proc_address(handle, ''SAMPLE'')#so far so good, I got a handle and addresse1 = 0e2 = 0ain = [2, 3, 4]aout = [ ]calldll.call_foreign_function(addr, ''hhll'', ''l'',(e1, e2,ain,aout))Traceback (most recent call last):File "<interactive input>", line 1, in ?TypeError: an integer is required Has anyone provide a similar example with Pyfort, F2Py or calldll? Thanks in advance. 解决方案 ATTRIBUTES DLLEXPORT :: SAMPLE!DECATTRIBUTES ALIAS:''SAMPLE'' :: SAMPLEINTEGER,INTENT(OUT) :: IERR1,IERR2REAL*8,INTENT(IN) :: AIN(*)REAL*8,INTENT(OUT) :: AOUT(*)CC *** SET MAXIMUM EXPECTED ELEMENTS OF ARRAY AIN AND AOUTCPARAMETER (MAXNVIN=101,MAXNVOUT=200)CC *** SET ERROR INDICATORS TO ZEROCIERR1=0IERR2=0CC *** GET NUMBER OF INPUT VALUESCNVIN=AIN(1)C *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURNIF(NVIN .GT. MAXNVIN) THENIERR1=1IERR2=1RETURNENDIFCC *** SET NUMBER OF OUTPUT VALUESCNVOUT=2*NVINC *** IF NUMBER EXCEEDS MAXIMUM EXPECTED SET ERRORS AND RETURNIF(NVOUT .GT. MAXNVOUT) THENIERR1=1IERR2=2RETURNENDIFCC *** INITIALIZE AOUT INDEXCJOUT=0CC *** COMPUTE TWO OUTPUT VALUES FOR EACH INPUT VALUECDO I=1,NVINJOUT=JOUT+1AOUT(JOUT)=2.*AIN(I+1)JOUT=JOUT+1AOUT(JOUT)=3.*AIN(I+1)END DORETURNEND compiled it to a dll and called this dll from another Fortran programwith success, so this tells me that dll is OK. This is how I tried to call it from python 2.4 import calldllhandle = calldll.load_library(''c:/sample_dll.dll'')addr = calldll.get_proc_address(handle, ''SAMPLE'')#so far so good, I got a handle and addresse1 = 0e2 = 0ain = [2, 3, 4]aout = [ ]calldll.call_foreign_function(addr, ''hhll'', ''l'',(e1, e2,ain,aout))Traceback (most recent call last):File "<interactive input>", line 1, in ?TypeError: an integer is required Has anyone provide a similar example with Pyfort, F2Py or calldll? Thanks in advance. Mangabasi wrote: Has anyone provide a similar example with Pyfort, F2Py or calldll?With the latest f2py in numpy: 这篇关于从Python调用Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-16 19:37