将python与fortran相结合,麻烦与教程

时间:2014-12-16 10:48:28

标签: python fortran f2py

我遵循本教程 http://www.sam.math.ethz.ch/~raoulb/teaching/PythonTutorial/combining.html

我使用相同的代码

      program hwtest
      real*8 r1, r2 ,s
      r1 = 1.0
      r2 = 0.0
      s = hw1(r1, r2)
      write(*,*) 'hw1, result:',s
      write(*,*) 'hw2, result:'
      call hw2(r1, r2)
      call hw3(r1, r2, s)
      write(*,*) 'hw3, result:', s
      end

      real*8 function hw1(r1, r2)
      real*8 r1, r2
      hw1 = sin(r1 + r2)
      return
      end

      subroutine hw2(r1, r2)
      real*8 r1, r2, s
      s = sin(r1 + r2)
      write(*,1000) 'Hello, World! sin(',r1+r2,')=',s
 1000 format(A,F6.3,A,F8.6)
      return
      end

C     special version of hw1 where the result is
C     returned as an argument:

      subroutine hw3_v1(r1, r2, s)
      real*8 r1, r2, s
      s = sin(r1 + r2)
      return
      end

C     F2py treats s as input arg. in hw3_v1; fix this:

      subroutine hw3(r1, r2, s)
      real*8 r1, r2, s
Cf2py intent(out) s
      s = sin(r1 + r2)
      return
      end

C     test case sensitivity:

      subroutine Hw4(R1, R2, s)
      real*8 R1, r2, S
Cf2py intent(out) s
      s = SIN(r1 + r2)
      ReTuRn
      end

C end of F77 file

但是当我使用命令

f2py -m hw -c ../hw.f

我收到以下错误:

gfortran:f77: ../hw.f
../hw.f:5.13:

      s = hw1(r1, r2)                                                   
             1
Error: Return type mismatch of function 'hw1' at (1) (REAL(4)/REAL(8))
../hw.f:5.13:

      s = hw1(r1, r2)                                                   
             1
Error: Return type mismatch of function 'hw1' at (1) (REAL(4)/REAL(8))
error: Command "/usr/bin/gfortran -Wall -ffixed-form -fno-second-underscore -fPIC -O3 -funroll-loops -I/tmp/tmpRGHzqj/src.linux-x86_64-2.7 -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -c -c ../hw.f -o /tmp/tmpRGHzqj/fortran/hw.o" failed with exit status 1

我没有使用Fortran的经验,我只需要在Python中实现现有的Fortran代码。

2 个答案:

答案 0 :(得分:2)

代码中缺少hw1的界面。要么明确指定它:

program hwtest
  real*8 r1, r2 ,s
  real*8 hw1
c [...]

或者,更好的是,将函数放入模块中,并use模块。

顺便说一句: 通过更适合的方式更改*8,例如:

program hwtest
  integer,parameter :: dp = kind(1.d0)
  real(dp) r1, r2 ,s
  real(dp) hw1

或者,使用ISO_Fortran_env以及预定义的参数REAL64REAL32

答案 1 :(得分:0)

只是为了结束:

      program hwtest
      implicit none
      real*8 r1, r2 ,s
C     Adding interface for the function
      real*8 hw1
      r1 = 1.0
      r2 = 0.0
      s = hw1(r1, r2)
      write(*,*) 'hw1, result:',s
      write(*,*) 'hw2, result:'
      call hw2(r1, r2)
      call hw3(r1, r2, s)
      write(*,*) 'hw3, result:', s
      end

      real*8 function hw1(r1, r2)
      real*8 r1, r2
      hw1 = sin(r1 + r2)
      return
      end

      subroutine hw2(r1, r2)
      real*8 r1, r2, s
      s = sin(r1 + r2)
      write(*,1000) 'Hello, World! sin(',r1+r2,')=',s
1000 format(A,F6.3,A,F8.6)
      return
      end

C     special version of hw1 where the result is
C     returned as an argument:

      subroutine hw3_v1(r1, r2, s)
      real*8 r1, r2, s
      s = sin(r1 + r2)
      return
      end

C     F2py treats s as input arg. in hw3_v1; fix this:

      subroutine hw3(r1, r2, s)
      real*8 r1, r2, s
Cf2py intent(out) s
      s = sin(r1 + r2)
      return
      end

C     test case sensitivity:

      subroutine Hw4(R1, R2, s)
      real*8 R1, r2, S
Cf2py intent(out) s
      s = SIN(r1 + r2)
      ReTuRn
      end

C end of F77 file

使用f2pyA -m hw -c hw.f

进行编译

最后从python调用:

import sys
from hw import hw1, hw2
try:
    r1 = float(sys.argv[1]);  r2 = float(sys.argv[2])
except IndexError:
    print 'Usage:', sys.argv[0], 'r1 r2'; sys.exit(1)
print 'Fortran hw1, result:', hw1(r1, r2)
print 'Fortran hw2, result: ', hw2(r1, r2)