Combining python with fortran, tutorial problem

I am following this tutorial http://www.sam.math.ethz.ch/~raoulb/teaching/PythonTutorial/combining.html

I am using the same code

      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

      

but when i use the command

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

      

I am getting the following error:

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

      

I have no Fortran experience, I only need to implement existing Fortran code for use in Python.

+3


source to share


2 answers


Your code is missing an interface for hw1

. Or specify it explicitly:

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

      

Or better yet, put functions in a module and a use

module.



BTW: Change *8

to something more appropriate like:

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

      

Or use ISO_Fortran_env

both the predefined parameters REAL64

and REAL32

.

+2


source


Just to wrap things up:

      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

      

Compile with f2pyA -m hw -c hw.f



Finally, a call from python like:

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)

      

0


source







All Articles