f2py不为f77阵列分配内存

时间:2014-12-15 11:19:00

标签: python arrays memory f2py

我试图用f2py包装一个旧的,有点凌乱的f77代码。我可以使用2个不同的编译器(gfortran和ifort)编译所有内容,但是从python执行代码时段错误,而原始f77可以工作。原因似乎是数组的未分配内存。更确切地说,在主子例程中,变量声明如下:

  implicit double precision (a-h,o-z)
  include 'gmm01f.par.f'
  include 'ampld.par.f'
  parameter (nangmax=1801)
  parameter (nmp=np*(np+2),nmp0=(np+1)*(np+4)/2)
  parameter (ni0=np*(np+1)*(2*np+1)/3+np*np)
  parameter (ng0=np*(2*np**3+10*np**2+19*np+5)/6)
  parameter (nrc=4*np*(np+1)*(np+2)/3+np)
  integer u,v,u0,nmax(nLp),uvmax(nLp),ind(nLp),idshp(nLp)
  double precision k,r0(9,nLp),x(nLp),xc(nLp),r00(3,nLp),
 +   besj(0:2*np+1),besy(0:2*np+1),smue(4,4),i11(nangmax),
 +   i21(nangmax),i22(nangmax),i12(nangmax),inat(nangmax),
 +   pol(nangmax),dang(nangmax),
 +   cscaxi(nLp),cscayi(nLp),cextxi(nLp),cextyi(nLp),
 +   cabsxi(nLp),cabsyi(nLp),cexti(nLp),cabsi(nLp),cscai(nLp),
 +   assymi(nLp),assymxi(nLp),assymyi(nLp),cprxi(nLp),
 +   cpryi(nLp),cpri(nLp),drot(nrc),c0i(nLp),c1i(nLp)
  double precision  shp(3,nLp),
 &   RT11(NPN6,NPN4,NPN4),RT12(NPN6,NPN4,NPN4),
 &   RT21(NPN6,NPN4,NPN4),RT22(NPN6,NPN4,NPN4),
 &   IT11(NPN6,NPN4,NPN4),IT12(NPN6,NPN4,NPN4),
 &   IT21(NPN6,NPN4,NPN4),IT22(NPN6,NPN4,NPN4)
  complex*16 A,B,cmz,Aj,Bj,A2,B2,Aj2,Bj2,A0,B0,ci,cin,
 +   s2x,s4x,s3y,s1y,s2xj,s4xj,s3yj,s1yj,
 +   ref(nLp),refc(nLp),atr(2,np,nmp),at(nmp),bt(nmp),ek(np),
 +   p0(nLp,nmp),q0(nLp,nmp),an(np),bn(np),atj(nmp),btj(nmp),
 +   as00(nLp,nmp),bs00(nLp,nmp),as0(nLp,nmp),bs0(nLp,nmp),
 +   as(nLp,nmp),bs(nLp,nmp),asc(nLp,nmp),bsc(nLp,nmp),
 +   as1(nLp,nmp),bs1(nLp,nmp),ast(nLp,nmp),bst(nLp,nmp),
 +   asp(nLp,nmp),bsp(nLp,nmp),asv(nLp,nmp),bsv(nLp,nmp),
 +   asx(nLp,nmp),bsx(nLp,nmp),aty(nmp),bty(nmp),atyj(nmp),
 +   btyj(nmp),Ay(2),By(2),Ayj(2),Byj(2),
 +   tbar(nLp,2,2,nmp,nmp),tbar0(2,2,nmp,nmp),bar(2,2),
 +   ekt(-2*np:2*np)
  CHARACTER FLNAME*20,fileout*20,fileout1*19,flout*22
  COMMON/MIESUB/ twopi,pih
  common/rot/bcof(0:np+2),dc(-np:np,0:nmp)
  common/fnr/fnr(0:2*(np+2))
  common/pitau/pi(nmp0),tau(nmp0)
  common/tran/atr
  common/ig0/iga0(ni0)
  common/g0/ga0(ng0)
  common/cofmnv0/cof0(ni0)
  common/crot/cofsr(nmp)
  COMMON /TMAT/ RT11,RT12,RT21,RT22,IT11,IT12,IT21,IT22

其中 gmm01f.par.f 如下:

      parameter (nLp=10,np=30)

ampld.par.f 是:

  Parameter (NPN1=100, NPNG1=500, NPNG2=2*NPNG1, NPN2=2*NPN1,
 &           NPL=NPN2+1, NPN3=NPN1+1,
 &           NPN4=NPN1, NPN5=2*NPN4, NPN6=NPN4+1)

由于某种原因, cofsr(nmp)数组的内存似乎不会被分配,而且每当我尝试为数组赋值时,程序会出现段错误。这仅适用于 cofsr(nmp),而不适用于在程序开始时初始化为0的其他数组。我无法真正看到这种行为的任何原因,唯一的例外是在子程序中 cofsr(nmp)被初始化为

  subroutine cofsrd(nmax)
  include 'gmm01f.par.f'
  parameter (nmp=np*(np+2))
  double precision cofsr(nmp),lnfacd,c
  common/crot/cofsr

,否则我们只有

implicit double precision (a-h,o-z)

语句。

作为附加信息,子程序的开头如下:

  SUBROUTINE gmm03f(idMie,
 &                  idAlpha,idBeta,iseed,
 &                  small,MXINT,
 &                  NADD,
 &                  fint,
 &                  sang,pang,
 &                  w,irat,
 &                  nL,
 &                  idshp,shp,r0,
 &                  cextxi,cextyi,cscaxi,
 &                  cscayi,cabsxi,cabsyi)

Cf2py intent(in) idMie
Cf2py intent(in) idAlpha,idBeta,iseed
Cf2py intent(in) small,MXINT
Cf2py intent(in) NADD
Cf2py intent(in) fint
Cf2py intent(in) sang,pang
Cf2py intent(in) w,irat
Cf2py intent(in) nL
Cf2py intent(in) idshp,shp,r0
Cf2py intent(out) cextxi,cextyi,cscaxi,cscayi,cabsxi,cabsyi

并且签名文件是:

python module stm ! in
 interface  ! in :stm
    subroutine  gmm03f(idmie,idalpha,idbeta,iseed,small,mxint,nadd,fint,sang,pang,w,irat,nl,idshp,shp,r0,cextxi,cextyi,cscaxi,cscayi,cabsxi,cabsyi) ! in :stm:stm.f
        integer intent(in) :: idmie
        integer intent(in) :: idalpha
        integer intent(in) :: idbeta
        integer intent(in) :: iseed
        double precision intent(in) :: small
        integer intent(in) :: mxint
        integer intent(in) :: nadd
        double precision intent(in) :: fint
        double precision intent(in) :: sang
        double precision intent(in) :: pang
        double precision intent(in) :: w
        integer intent(in) :: irat
        integer intent(in) :: nl
        integer dimension(10),intent(in) :: idshp
        double precision dimension(3,10),intent(in) :: shp
        double precision dimension(9,10),intent(in) :: r0
        double precision dimension(10),intent(out) :: cextxi
        double precision dimension(10),intent(out) :: cextyi
        double precision dimension(10),intent(out) :: cscaxi
        double precision dimension(10),intent(out) :: cscayi
        double precision dimension(10),intent(out) :: cabsxi
        double precision dimension(10),intent(out) :: cabsyi
        double precision dimension(527) :: pi
        double precision dimension(527) :: tau
        double precision dimension(65) :: fnr
        double precision dimension(101,100,100) :: rt11
        double precision dimension(101,100,100) :: rt12
        double precision dimension(101,100,100) :: rt21
        double precision dimension(101,100,100) :: rt22
        double precision dimension(101,100,100) :: it11
        double precision dimension(101,100,100) :: it12
        double precision dimension(101,100,100) :: it21
        double precision dimension(101,100,100) :: it22
        integer dimension(19810) :: iga0
        double precision :: twopi
        double precision :: pih
        double precision dimension(960) :: cofsr
        complex*16 dimension(2,30,960) :: atr
        double precision dimension(19810) :: cof0
        double precision dimension(317875) :: ga0
        double precision dimension(33) :: bcof
        double precision dimension(np + 31,961) :: dc
        common /pitau/ pi,tau
        common /fnr/ fnr
        common /tmat/ rt11,rt12,rt21,rt22,it11,it12,it21,it22
        common /ig0/ iga0
        common /miesub/ twopi,pih
        common /crot/ cofsr
        common /tran/ atr
        common /cofmnv0/ cof0
        common /g0/ ga0
        common /rot/ bcof,dc
        include 'gmm01f.par.f'
        include 'ampld.par.f'
    end subroutine gmm03f
  end interface
end python module stm

非常感谢

1 个答案:

答案 0 :(得分:0)

我找到了答案,即使我不能说我理解其背后的理由。导致问题的原因是公共区块的名称:

 common/crot/cofsr(nmp)

用其他名称切换时,例如

 common/testok/cofsr(nmp)

一切都按预期工作,因此我的问题就解决了。