python调用fortran模块

WBOY
풀어 주다: 2016-06-10 15:05:20
원래의
3235명이 탐색했습니다.

在python中调用fortran代码,要用到f2py这个程序。它的项目主页在此。现在该项目已经合并到numpy中了,先安装python再装好numpy,就可以使用f2py。不过对windows平台必须使用gnu的fortran编译器gfortran,在此下载。装完了python,numpy和gfortran这三样东西之后,还必须更改如下几个环境变量:

    1.在$PATH中添加gfortran的路径,我的是c:\Program Files\pythonxy\mingw\bin\

    2.在$PATH中添加python的路径,我的是c:\Python26\

    3.新建环境变量C_INCLUDE_PATH,添加gfortran头文件的路径,我的是c:\Program Files\pythonxy\mingw\include\

好啦现在f2py就可以用了。新建fortran程序foo.f90如下

foo.f90

subroutine hello (a)
 
integer a
 
  write(*,*)'Hello from Fortran90!!!',a
 
end subroutine hello
로그인 후 복사

编译

f2py -m foo -c foo.f90 

로그인 후 복사

运行

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
>>> foo.hello(15)
 Hello from Fortran90!!!     15
로그인 후 복사

另外附上f2py支持的数据类型有

integer[ | *1 | *2 | *4 | *8 ], logical[ | *1 | *2 | *4 | *8 ]
integer*([ -1 | -2 | -4 | -8 ])
character[ | *(*) | *1 | *2 | *3 | ... ]
real[ | *4 | *8 | *16 ], double precision
complex[ | *8 | *16 | *32 ]
<dim> | <start>:<end> | * | :
intent([ in | inout | out | hide | in,out | inout,out | c |
     copy | cache | callback | inplace | aux ])
dimension(<dimspec>)
common, parameter
allocatable
optional, required, external
depend([<names>])
check([<C-booleanexpr>])
note(<LaTeX text>)
usercode, callstatement, callprotoargument, threadsafe, fortranname
pymethoddef
entry
로그인 후 복사

以上所述就是本文的全部内容了,希望大家能够喜欢

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!