Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

python - Error occurs when coming back from subroutine of fortran by numpy.f2py

I made hoge as simple as possible and errors still coming. Please tell me what problems are.

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

I want to use a Fortran subroutine. I compiled like below

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

and use as below

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.

When the line has a change like below

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

the error does not occur... (for your information..) 1..6 brings the error.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

According to the F2PY User Guide and Reference Manual:

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.

Thus, the declaration complex(kind(0d0)) in your code example is exactly the kind of function call or other expression that f2py cannot interpret.

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...