python調(diào)用fortran模塊
在python中調(diào)用fortran代碼,要用到f2py這個(gè)程序。它的項(xiàng)目主頁(yè)在此?,F(xiàn)在該項(xiàng)目已經(jīng)合并到numpy中了,先安裝python再裝好numpy,就可以使用f2py。不過(guò)對(duì)windows平臺(tái)必須使用gnu的fortran編譯器gfortran,在此下載。裝完了python,numpy和gfortran這三樣?xùn)|西之后,還必須更改如下幾個(gè)環(huán)境變量:
1.在$PATH中添加gfortran的路徑,我的是c:\Program Files\pythonxy\mingw\bin\
2.在$PATH中添加python的路徑,我的是c:\Python26\
3.新建環(huán)境變量C_INCLUDE_PATH,添加gfortran頭文件的路徑,我的是c:\Program Files\pythonxy\mingw\include\
好啦現(xiàn)在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
運(yùn)行
$ 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支持的數(shù)據(jù)類型有
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
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡
相關(guān)文章
構(gòu)建可視化?web的?Python?神器streamlit
這篇文章主要介紹了構(gòu)建可視化web的Python神器streamlit,Streamlit是一個(gè)用于機(jī)器學(xué)習(xí)、數(shù)據(jù)可視化的Python框架,它能幾行代碼就構(gòu)建出一個(gè)精美的在線app應(yīng)用2022-06-06
利用Pandas讀取表格行數(shù)據(jù)判斷是否相同的方法
Python中字符串列表的相互轉(zhuǎn)換實(shí)際應(yīng)用場(chǎng)景
python中子類調(diào)用父類函數(shù)的方法示例
python事件驅(qū)動(dòng)event實(shí)現(xiàn)詳解

