假设我们需要在python程序中调用fortran函数,它返回一些值.我发现以这种方式重写fortran代码:
subroutine pow2(in_x, out_x)
implicit none
real, intent(in) :: in_x
!f2py real, intent(in, out) :: out_x
real, intent(out) :: out_x
out_x = in_x ** 2
return
end
Run Code Online (Sandbox Code Playgroud)
并以这种方式在python中调用它:
import modulename
a = 2.0
b = 0.0
b = modulename.pow2(a, b)
Run Code Online (Sandbox Code Playgroud)
给我们工作的结果.我可以用其他方式调用fortran函数,因为我认为第一种方式有点笨拙吗?
在下面的Python中,我有五个函数包含在func我必须集成的数组中.代码调用使用f2py以下代码生成的外部Fortran模块:
import numpy as np
from numpy import cos, sin , exp
from trapzdv import trapzdv
def func(x):
return np.array([x**2, x**3, cos(x), sin(x), exp(x)])
if __name__ == '__main__':
xs = np.linspace(0.,20.,100)
ans = trapzdv(func,xs,5)
print 'from Fortran:', ans
print 'exact:', np.array([20**3/3., 20**4/4., sin(20.), -cos(20.), exp(20.)])
Run Code Online (Sandbox Code Playgroud)
Fortran例程是:
subroutine trapzdv(f,xs,nf,nxs,result)
integer :: I
double precision :: x1,x2
integer, intent(in) :: nf, nxs
double precision, dimension(nf) :: fx1,fx2
double precision, intent(in), dimension(nxs) :: xs
double precision, intent(out), dimension(nf) :: …Run Code Online (Sandbox Code Playgroud) [回调函数]也可以在模块中明确设置.然后,没有必要将参数列表中的函数传递给Fortran函数.如果调用python回调函数的Fortran函数本身由另一个Fortran函数调用,则可能需要这样做.
但是,我似乎无法找到如何做到这一点的例子.
考虑以下Fortran/Python组合:
test.f:
subroutine test(py_func)
use iso_fortran_env, only stdout => output_unit
!f2py intent(callback) py_func
external py_func
integer py_func
!f2py integer y,x
!f2py y = py_func(x)
integer :: a
integer :: b
a = 12
write(stdout, *) a
end subroutine
Run Code Online (Sandbox Code Playgroud)
call_test.py:
import test
def func(x):
return x * 2
test.test(func)
Run Code Online (Sandbox Code Playgroud)
使用以下命令编译(英特尔编译器):
python f2py.py -c test.f --fcompiler=intelvem -m test
Run Code Online (Sandbox Code Playgroud)
我需要采取哪些更改才能以func模块的形式暴露给整个Fortran程序,以便我可以从子程序内部调用函数test,或者在项目中的任何其他fortran文件中调用任何其他子程序?
我应该用这个庞大的Fortran 77程序进行研究(我最近将它移植到Fortran 90表面).它是一个非常古老的软件,用于使用有限元方法进行建模.
我想做的事:
f2py,因此性能没有损失.我已经告诉该节目是该死在当前状态快?希望将对数字子程序和I/O的调用移植到python不会减慢到不切实际的水平(或者它会吗?).我想知道:
f2py适合并完成包装众多子程序和公共块而没有任何混淆的任务?我只在网上看过单文件示例f2py; 我知道numpy已经用它来包装LAPACK和东西,但我需要保证这f2py是一个足以完成这项任务的工具.笔记:
我正在为我的研究进行一些模拟工作,并且遇到了将fortran导入我的python脚本的麻烦。作为背景,我已经使用Python多年了,并且只有在需要时才在Fortran内部玩弄。
过去,我使用Fortran实现了一些简单的OpenMP功能,做了一些工作。我不是这方面的专家,但是我已经掌握了基础知识。
我现在正在使用f2py创建可以从python脚本调用的库。当我尝试编译openmp时,它可以正确编译并运行完成,但是速度没有零改善,总的来说,我发现CPU使用率表明只有一个线程正在运行。
我搜索了f2py的文档(文档记录不充分),并进行了常规的网络搜索以获取答案。我已经包含了我正在编译的Fortran代码以及对其进行调用的简单python脚本。我还抛出了我正在使用的编译命令。
目前,我将模拟比例降低到10 ^ 4,作为一个不错的基准。在我的系统上,运行需要3秒钟。最终,我需要运行许多10 ^ 6的粒子模拟,所以我需要减少一些时间。
如果有人可以指出如何使我的代码正常工作的方向,将不胜感激。我还可以根据需要尝试包含有关系统的任何详细信息。
干杯,里尔坎
1)编译
f2py -c --f90flags='-fopenmp' -lgomp -m calc_accel_jerk calc_accel_jerk.f90
Run Code Online (Sandbox Code Playgroud)
2)调用的Python脚本
import numpy as N
import calc_accel_jerk
# a is a (1e5,7) array with M,r,v information
a = N.load('../test.npy')
a = a[:1e4]
out = calc_accel_jerk.calc(a,a.shape[0])
print out[:10]
Run Code Online (Sandbox Code Playgroud)
3)Fortran代码
subroutine calc (input_array, nrow, output_array)
implicit none
!f2py threadsafe
include "omp_lib.h"
integer, intent(in) :: nrow
double precision, dimension(nrow,7), intent(in) :: input_array
double precision, dimension(nrow,2), intent(out) :: output_array
! Calculation parameters with set values …Run Code Online (Sandbox Code Playgroud) 所以我试图学习,f2py并且我有以下Fortran代码
subroutine fibonacci(a, n)
implicit none
integer :: i, n
double precision :: a(n)
do i = 1, n
if (i .eq. 1) then
a(i) = 0d0
elseif (i .eq. 2) then
a(i) = 1d0
else
a(i) = a(i - 1) + a(i - 2)
endif
enddo
end subroutine fibonacci
Run Code Online (Sandbox Code Playgroud)
用f2py -c fibonacci.f -m fibonacciPython 编译并调用
subroutine fibonacci(a, n)
implicit none
integer :: i, n
double precision :: a(n)
do i = 1, n
if (i …Run Code Online (Sandbox Code Playgroud) 由于我之前描述的原因,我需要在 Python 中使用 LAPACK dgesvd 和 zgesvd 方法,而不是用 numpy 包装的方法。
有人指出,我可以使用 f2py 来创建我自己的 python 包。问题是,lapack 中的 dgesdd 调用了一堆其他方法,如 dbdsqr、dgelqf 以及一些 BLAS 例程,我不知道我应该如何处理。
谁能指出,在不必重新编译整个 lapack 库的情况下,创建 dgesvd python 模块的正确方法是什么?
非常感谢米莎
我想用python运行一些fortran代码,并使用f2py -c -m.但是,似乎只有FUNCTION被打包到.so文件中,而不是打包到PROGRAM中.那我怎么处理全局变量呢?例如,变量c放在模块中
MODULE nfw
double precision :: c
END MODULE nfw
Run Code Online (Sandbox Code Playgroud)
,在PROGRAM中被修改,并由隐含的同一文件中的FUNCTION使用
PROGRAM Compute_Profile
USE nfw
c = 5.0
END PROGRAM Compute_Profile
DOUBLE PRECISION FUNCTION y(x)
USE nfw
double precision :: x
y = c * x
return
END FUNCTION y
Run Code Online (Sandbox Code Playgroud)
我如何调用让函数y(x)知道python 中c的值?
我在Fortran中有这个简单的模块:
test.f90:
module test
implicit none
contains
subroutine foo(chid)
implicit none
character(len=*),intent(out):: chid ! char. identifier
chid = "foo"
end subroutine foo
end module test
program bar
use test
character(len=20) text
call foo(text)
write(*,*) text
end program bar
Run Code Online (Sandbox Code Playgroud)
编译它(在Windows上)gfortran test.f90 -o test.exe并运行它,如预期的那样:
foo
Run Code Online (Sandbox Code Playgroud)
我也可以使用f2py编译它: c:\Python27\python.exe c:\Python27\Scripts\f2py.py --fcompiler=gnu95 --compiler=mingw32 -c -m test \test.f90
当我运行这个Python脚本时:
test.py:
from id_map import test
print "This should be 'foo':"
print test.foo()
print "was it?"
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
This should be 'foo':
was it?
Run Code Online (Sandbox Code Playgroud)
如您所见,应为"foo"的字符串为空.为什么是这样?
我正在尝试(并且失败)在Windows 10上使用f2py和Python 3.6 编译fortran模块(特别是来自BGS的igrf12.f).使用Anaconda 4.4.10安装Python.
我的设置:
我按照SciPy文档中的 f2py指示和Dr.Michael Hirsch 的非常有用的指导.Dr.Hirsch创建了pyigrf12模块,但是通过pip安装失败了,这最初引起了我对f2py的兴趣.
我将概述我正在使用的一些方法.无论方法如何,我总是首先使用f2py igrf12.f -m pyigrf12 -h igrf12.pyf并适当地添加intent(in/out)属性来创建*.pyf签名文件.
--compiler=mingw32--compiler=msvc--compiler=mingw32--compiler=msvc方法1:
对于后台,我在C:\ MinGW有MinGW,我已将C:\ MinGW\bin添加到我的用户Path.不幸的是,我没有安装这个版本的MinGW(我从同事那里继承了这台电脑),所以我不知道它的来源.gcc --version和gfortran --version是5.3.0.
我跑f2py -c igrf12.pyf igrf12.f --compiler=mingw32.此失败并显示以下错误消息:
Building import library (arch=AMD64):
"C:\Users\Sholes\AppData\Local\Continuum\anaconda3\libs\libpython36.a"
(from C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll)
objdump.exe: C:\Users\Sholes\AppData\Local\Continuum\anaconda3\python36.dll: File format not recognized
Traceback (most recent call last):
File "C:\Users\Sholes\AppData\Local\Continuum\anaconda3\Scripts\\f2py.py", line 28, in …Run Code Online (Sandbox Code Playgroud)