用f2py在Python中嵌入Fortran [英] Embedding Fortran in Python with f2py

查看:126
本文介绍了用f2py在Python中嵌入Fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个脚本来遍历目录结构,从目录中的文件中提取数字,然后对这些数字执行计算。我使用Python作为脚本的主要语言,但想要使用Fortran进行数值计算。 (我更喜欢Fortran,它是一个更好的工具)



我正在尝试使用f2py,但我一直在收到奇怪的错误。 f2py正在抱怨我的变量声明,试图将字符(*)更改为整数并追加!当我在变量声明之后立即注释时,将其添加到我的变量名称中。



子过程太长而无法在此处发布,但需要两个参数,即输入文件名和输出文件名。它打开输入文件,读取数字,处理它们,然后写入输出文件。我打算使用Python脚本在每个目录中写入数字文件,并在其上调用Fortran子例程。



我可以尝试发布一个具有相同问题的较小示例,但是有没有与f2py共同的'陷阱'?我正在使用gfortran v4.6.1,python v3.2.2和f2py v2。



编辑:以下是一个小错误:



itimes-sf(包含要从python使用的子例程的文件):

  module它的

包含

子程序itimes(infile,outfile)

隐含无

!常量
整数,参数:: dp = selected_real_kind(15)

!子程序输入
字符(*),intent(in):: infile!输入文件名
字符(*),intent(in):: outfile!输出文件名

!内部变量
real(dp):: num!从文件读取的数字
integer :: inu!输入单元号
integer :: outu!输出单元号
integer :: ios! IOSTAT用于读取文件

inu = 11
outu = 22

open(inu,file = infile,action ='read')
open如果(ios< ios< ios> ; 0)exit

write(outu,*)num ** 2
end do

end子程序itimes

end module its

itests.f(Fortran驱动程序):

 程序itests 

使用它的

字符(5):: outfile
字符(5):: infile

outfile ='b.txt'
infile ='a.txt'

调用itimes(infile,outfile)

结束program itests

a.txt:

  1 
2
3
4
5
6
7
8
9
10.2

b.txt仅在使用gfortran编译和运行itests和itimes-s后:

  1.0000000000000000 
4.0000000000000000
9.0000000000000000
16.000000000000000
25.000000000000000
36.000000000000000
49.000000000000000
64.000000000000000
81.000000000000000
104.03999999999999

使用 f2py.py运行f2py -c -m itimes-然而,sf 会产生大量错误。 (不是由于长度发布,但如果有人想要我可以发布它们)

解决方案

我从来没有尝试过使用f2py来包装完整的Fortran模块。但是,如果从模块中提取 itimes 函数到它自己的文件中,然后运行相同的 f2py 命令,似乎工作,当我在本地尝试(f2py v2,numpy 1.6.1,python 2.7.2,gfortran 4.1.2)。

另外,请注意,你不是明确地关闭你的输入和输出文件,尽管这对f2py的工作或没有真正的影响。


I need a script to recurse across a directory structure, extract numbers from files in the directories, then perform calculations on those numbers. I am using Python as the main language for the script, but wanted to use Fortran for the numerical computations. (I am more comfortable with Fortran and it is a better tool for numerical work)

I am trying to use f2py, but I keep getting strange errors. f2py is complaining about my variable declarations, trying to change character(*) to integer and appending ! onto my variable names when I have a comment immediately following the variable declaration.

The subroutine is too long to post here, but takes two arguments, an input file name and output file name. It opens the input file, reads the numbers, processes them, then writes to the output file. I intend to use the Python script to write the numerical file in each directory and call the Fortran subroutine on it.

I can try to post a smaller example with the same issues, but are there any common 'gotchas' with f2py? I am using gfortran v4.6.1, python v3.2.2, and f2py v2.

EDIT: Here is a small example with the same errors:

itimes-s.f (File containing subroutine to be used from python):

  module its

  contains

  subroutine itimes(infile,outfile)

    implicit none

    ! Constants
    integer, parameter :: dp = selected_real_kind(15)

    ! Subroutine Inputs
    character(*), intent(in) :: infile    ! input file name
    character(*), intent(in) :: outfile   ! output file name

    ! Internal variables
    real(dp) :: num               ! number to read from file
    integer :: inu                ! input unit number
    integer :: outu               ! output unit number
    integer :: ios                ! IOSTAT for file read

    inu = 11
    outu = 22

    open(inu,file=infile,action='read')
    open(outu,file=outfile,action='write',access='append')

    do
      read(inu,*,IOSTAT=ios) num
      if (ios < 0) exit

      write(outu,*) num**2
    end do

  end subroutine itimes

  end module its

itests.f (Fortran driver program):

  program itests

  use its

  character(5) :: outfile
  character(5) :: infile

  outfile = 'b.txt'
  infile = 'a.txt'

  call itimes(infile, outfile)

  end program itests

a.txt:

1
2
3
4
5
6
7
8
9
10.2

b.txt after compiling and running itests and itimes-s using gfortran only:

   1.0000000000000000     
   4.0000000000000000     
   9.0000000000000000     
   16.000000000000000     
   25.000000000000000     
   36.000000000000000     
   49.000000000000000     
   64.000000000000000     
   81.000000000000000     
   104.03999999999999     

Running f2py using f2py.py -c -m its itimes-s.f produces numerous errors however. (not posted due to length, but if someone wants I can post them)

解决方案

I have never tried using f2py to wrap a full Fortran module. However, if you extract the itimes function from the module into its own file and then run the same f2py command, everything appears to work when I tried it locally (f2py v2, numpy 1.6.1, python 2.7.2, gfortran 4.1.2).

Also, note that you are not explicitly closing your input and output files, though this has no real bearing on f2py's working or not.

这篇关于用f2py在Python中嵌入Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆