从numpy.f2py从fortran子例程返回时发生错误 [英] Error occurs when coming back from subroutine of fortran by numpy.f2py
问题描述
我使hoge尽可能简单,并且错误仍然存在.请告诉我什么问题.
I made hoge as simple as possible and errors still coming. Please tell me what problems are.
这是我的Fortran子例程代码.
This is my Fortran subroutine code.
subroutine hoge(d)
complex(kind(0d0)), intent(out):: d(5,10,15) ! 5 10 15 does not have special meanings..
! these two lines works..
! integer, parameter :: dp = kind(0d0)
! complex(dp), intent(out) :: d(5,10,15)
do i=1,15
do j=1,10
do k=1,5
d(k,j,i)=0
enddo
enddo
enddo
! instead
! d(1:5,1:10,1:15)=0 or
! d(:,:,:)=0 also brings the error.
!
print*,'returning'
return
end subroutine hoge
我想使用Fortran子例程.我像下面这样编译
I want to use a Fortran subroutine. I compiled like below
python -m numpy.f2py -c hoge.f90 -m hoge
并按以下方式使用
import hoge
hoge.hoge()
然后结果是下面的三行:
then the result is the three lines below:
Returning
double free or corruption (out)
Aborted (core dumped)
我完全不知道...请告诉我什么问题.
I totally have no idea... please tell me what problems are.
当该行发生如下更改
do j=1,10 -> do j=1,5
不会发生错误...(供您参考.)1..6带来错误.
the error does not occur... (for your information..) 1..6 brings the error.
推荐答案
根据 F2PY用户指南和参考手册:
Currently, F2PY can handle only <type spec>(kind=<kindselector>)
declarations where <kindselector> is a numeric integer (e.g. 1, 2,
4,…), but not a function call KIND(..) or any other expression.
因此,您的代码示例中的声明 complex(kind(0d0))
正是f2py无法解释的函数调用或其他表达式.
Thus, the declaration complex(kind(0d0))
in your code example is exactly the kind of function call or other expression that f2py cannot interpret.
您已经发现(但在代码中注释掉了),一种解决方法是首先生成一个整数种类说明符( integer,parameter :: dp = kind(0d0)
),然后然后在复杂变量 complex(dp)
的种类说明符中使用它.
As you have found out (but commented out in the code), one work around is to first generate an integer kind specifier (integer, parameter :: dp = kind(0d0)
), and then use that in the kind specifier of your complex variable complex(dp)
.
如果这样更改Fortran源代码不是您的选择,则文档将进一步概述映射文件(默认名称为 .f2py_f2cmap
或使用命令传递)的方式可以创建并使用行标记-f2cmap< filename>
).就您而言,例如使用具有以下内容的默认文件:
If changing the Fortran source code like this is not an option for you, the documentation further outlines how a mapping file (with default name .f2py_f2cmap
or passed using command line flag --f2cmap <filename>
) can be created and used instead. In your case, you can e.g. use the default file with the following contents:
$ cat .f2py_f2cmap
{'complex': {'KIND(0D0)': 'complex_double'}}
并像以前一样进行编译,以获得所需的行为.
and compile as before, to get the desired behaviour.
这篇关于从numpy.f2py从fortran子例程返回时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!