用numpy.f2py从fortran的子程序返回时发生错误。

问题描述 投票:0回答:1

我把hoge做得尽可能简单,但还是出现了错误.请告诉我是什么问题。

这是我的Fortran子程序代码。

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子程序。我的编译如下

python -m numpy.f2py -c hoge.f90 -m hoge

并作如下使用

import hoge
hoge.hoge()

那么结果就是下面这三行。

Returning
double free or corruption (out)
Aborted (core dumped)

我完全不知道... 请告诉我有什么问题。

当这一行有如下变化时

do j=1,10   -> do j=1,5

错误不会发生... (供你参考...)1...6带来的错误。

python fortran f2py
1个回答
2
投票

根据 F2PY用户指南和参考手册:

Currently, F2PY can handle only &lttype spec&gt(kind=&ltkindselector&gt)
declarations where &ltkindselector&gt is a numeric integer (e.g. 1, 2,
4,…), but not a function call KIND(..) or any other expression.

因此,声明 complex(kind(0d0)) 在你的代码例子中,正是那种 函数调用 f2py无法解释。

正如你所发现的那样 (但在代码中做了注释),一个变通的方法是先生成一个整数种类指定器 (integer, parameter :: dp = kind(0d0)),然后在你的复合变量的种类指定器中使用该变量 complex(dp).

如果您不愿意这样修改Fortran源代码,文档中进一步概述了如何在源代码中使用 映射文件 (默认名称为 .f2py_f2cmap 或使用命令行标志传递 --f2cmap <filename>)可以被创建和使用。在你的情况下,你可以使用以下内容的默认文件。

$ cat .f2py_f2cmap
{'complex': {'KIND(0D0)': 'complex_double'}}

然后像之前一样编译,以获得所需的行为。

© www.soinside.com 2019 - 2024. All rights reserved.