使用 f2py 模块时如何处理内在功能?

How to deal with the intrinsic funtion when using f2py module?

提问人:zpan 提问时间:1/13/2022 更新时间:1/13/2022 访问量:61

问:

我有一个 Fortran 代码,并尝试使用 f2py 将其编译成 Python 模块。Fortran 代码“drandn.f”如下:

SUBROUTINE DRANDN (N,DX,SEED)
      INTRINSIC DBLE, IABS, MOD
      INTEGER N, SEED
      DOUBLE PRECISION DX(N)
      INTEGER I, J
      DOUBLE PRECISION DMAX
      INTEGER IM, IMAX, IS
      SAVE DMAX, IM, IMAX, IS
      DATA IM/0/
      IF (IM.EQ.0) THEN
         J  = 0
         IM = 1
         DO 10 I = 1, 31
            J = J + 1
            IF (IM*2.LE.IM) GO TO 20
            IM = IM * 2
 10      CONTINUE
 20      IMAX = (IM-1) * 2 + 1
         DMAX = DBLE(IMAX)
         DO 30 I = 1, MOD(J,3)
            J = J - 1
            IM = IM / 2
 30      CONTINUE
         IM = IM + 5
         IS = IABS(MOD(IM*30107,IMAX))
      END IF
      IF (SEED.GT.0) IS = (SEED / 2) * 2 + 1
      DO 40 I = 1, N
         DX(I) = DBLE(IS) / DMAX
         IS    = IABS(MOD(IM*IS,IMAX))
 40   CONTINUE
      RETURN
      END

我尝试使用以下命令编译它:

f2py drandn.f -m drandn -h drandn.pyf

“drandn.pyf”代码:

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module drandn ! in
    interface  ! in :drandn
        subroutine drandn(n,dx,seed) ! in :drandn:drandn.f
            integer :: n
            double precision dimension(n) :: dx
            integer :: seed
        end subroutine drandn
    end interface
end python module drandn

! This file was auto-generated with f2py (version:2).
! See http://cens.ioc.ee/projects/f2py2e/

我尝试使用以下命令编译它:

f2py -c -m drandn drandn.pyf

但是当我导入 drandn 模块时,我收到一个错误:

ImportError                               Traceback (most recent call last)
<ipython-input-1-2d31b1e24e91> in <module>
      3 import numpy as np
      4 import math
----> 5 from fortran_sourcecode import drandn

ImportError: /public/home/zpan/pyscf-gsc/fortran_sourcecode/drandn.cpython-37m-x86_64-linux-gnu.so: undefined symbol: drandn_

(路径是正确的。我已经编译了另一个 fortran 代码,它可以工作。 我该如何解决这个问题?我很困惑。

Python Fortran77 F2PY

评论

0赞 cup 5/2/2022
您是否尝试过不生成 pyf 文件:just f2py -c drandn.f -m drandn

答: 暂无答案