日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

fortran的gui开发 python_python和fortran的接口

發布時間:2024/10/14 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 fortran的gui开发 python_python和fortran的接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于歷史緣故,很多成熟的計算代碼都是用fortran寫成的。在python中調用fortran代碼,要用到f2py這個程序。現在該項目已經合并到numpy中了,先安裝python再裝好numpy,就可以使用f2py。

一個簡單的例子:

foo.f90

subroutine hello (a)

integer a

write(*,*)'Hello from Fortran90!!!', a

end subroutine hello

用終端編譯:

f2py -m foo -c foo.f90

在python控制臺中運行:

$ python

>>> import foo

>>> foo.hello(15)

Hello from Fortran90!!! 15

明確定義接口的參數:

另外一個例子,非常明確地定義了接口的輸入、輸出參數:

!SUBROUTINE

SUBROUTINE ADDSUB(A,B,C,D)

IMPLICIT NONE

DOUBLE PRECISION A,B,C,D

!f2py intent(in) :: A,B

!f2py intent(out) :: C,D

C = A + B

D = A - B

print*, "ADDSUB From Fortran!"

print*, "ADD=",C

print*, "SUB=",D

RETURN

END

注意這兩行的代碼:

!f2py intent(in) :: A,B

!f2py intent(out) :: C,D

對于Fortran只是注釋,但對于f2py卻很重要,相當于”簽名”.

注意簽名的注釋前面不能有空格!

當然也可以寫成如下的形式:

Cf2py intent(in) :: A,B

Cf2py intent(out) :: C,D

下面開始編譯Fortran代碼為python模塊,打開CMD窗口,輸入如下命令:

f2py -m testfortran -c testfortran.f90

會在當前目錄下生成testfortran.pyd的文件. 下面就可以再python中使用這個模塊了:

In [1]: import testfortran

In [2]: print testfortran.__doc__

This module 'testfortran' is auto-generated with f2py (version:2).

Functions:

c,d = addsub(a,b)

In [3]: x=testfortran.addsub(4,9)

ADDSUB From Fortran!

ADD= 13.000

SUB= -5.000

In [4]: x

Out[4]: (13.0, -5.0)

接口參數是數組:

接口參數是數組的情況比較復雜。需要Python中必須確定數組的維數,并且NumPy定義的數組存儲方式必須是Fortran的按列存儲。

實現方式:

在Python中使用NumPy定義Fortran方式存儲的二維數組,利用ndpointer定義數組類型和維數,用ctypes模塊調用其data屬性將數組首地址傳入將二維數組的首地址和維數信息傳入Fortran中進行計算并返回。

fortran代碼:py2f90.f90

module py2f90

use,intrinsic::iso_c_binding

implicit none

contains

subroutine transferMat2For(matrix,n1,n2)bind(c,name='array2py')

implicit none

integer(c_int),intent(in),value::n1,n2

real(c_float),intent(out)::matrix(n1,n2)

integer::i,j

! initialize matrix

matrix = 0.0E0

! loop

do i=1,n1

do j=1,n2

matrix(i,j) = real(i,4)*1.E1+real(j,4)*2.E0

write(*,"('Row:',i4,1x,'Col:',i4,1x,'Value:',1x,F5.2)")i,j,matrix(i,j)

enddo

enddo

return

end subroutine

end module

test_mat.f90:

program test

use py2f90

implicit none

real(kind=4)::aa(4,5)

call transferMat2For(aa,4,5)

end program

python調用代碼:test_mat.py

#! /usr/bin/env python

#coding=utf-8

import numpy as np

from numpy.ctypeslib import load_library,ndpointer

from ctypes import c_int

# shape of 2d array

n1,n2 = 2, 5

# create an empty 2d array

data = np.empty(shape=(n1,n2),dtype='f4',order='f')

flib = load_library("test_mat","./")

flib.argtypes = [ndpointer(dtype='f4',ndim=2),c_int,c_int]

flib.array2py(data.ctypes.data,n1,n2)

print("*"*80)

print(data)

終端編譯指令:

f2py -m test_mat -c test_mat.f90 py2f90.f90

在終端輸入:python test_mat.py,得到:

image.png

作者:Alex_lxy

原文地址:https://www.jianshu.com/p/1f04e2a81798

總結

以上是生活随笔為你收集整理的fortran的gui开发 python_python和fortran的接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。