2 years ago

#42961

test-img

zpan

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

I have a Fortran code and try to compile it into a Python module with f2py. The Fortran Code "drandn.f" is below:

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

And I try to compile it with the command below:

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

The "drandn.pyf" Code:

!    -*- 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/

And I try to compile it with the command below:

f2py -c -m drandn drandn.pyf

But when I import the drandn module, I get an error:

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_

(The path is right. I have compiled another fortran code and it works.) How can I solve this problem? I'm so confused.

python

fortran77

f2py

0 Answers

Your Answer

Accepted video resources