Fortran 使用种类参数声明类型时出错

Fortran 使用种类参数声明类型时出错,fortran,fortran90,Fortran,Fortran90,使用以下程序时,我会遇到错误 Program COM !Input !No of Atoms !No of Iterations !Respective Positions. !As of now for homogeneous clusters. Implicit None Real, Parameter :: R8B=selected_real_kind(10) Real, Parameter :: R4B=selected_real_kind(4) Integer, Par

使用以下程序时,我会遇到错误

Program COM

!Input
!No of Atoms
!No of Iterations
!Respective Positions.
!As of now for homogeneous clusters.

 Implicit None

 Real, Parameter :: R8B=selected_real_kind(10)
 Real, Parameter :: R4B=selected_real_kind(4)

 Integer, Parameter :: I1B=selected_int_kind(2)
 Integer, Parameter :: I2B=selected_int_kind(4)
 Integer, Parameter :: I4B=selected_int_kind(9)
 Integer, Parameter :: I8B=selected_int_kind(18)


 Real (R8B), Dimension (:,:), Allocatable :: Posx, Posy, Posz
 Real (R8B), Dimension (:), Allocatable   :: Posx_n, Posy_n, Posz_n
 Real (R8B), Dimension (:), Allocatable   :: dist_com, avj_dist_com

 Integer (I4B), Dimension (:), Allocatable :: bin_array


 Real (R8B) :: comx, comy, comz

 Integer (I8B) :: nIter, nAtom, dist
 Integer (I8B) :: I,J,ii,k
 Integer (I1B) :: xyz_format, FlagR, FlagM, Flag_com

 Integer (I8B) :: bin
 Integer (R8B) :: max_dist

 Character (50) POS_file, COM_file,Bin_file
 Character (2) jj

 Read (*,*) POS_file
 Read (*,*) COM_file
 Read (*,*) Bin_file
 Read (*,*) nAtom
 Read (*,*) nIter
 Read (*,*) xyz_format
 Read (*,*) max_dist, bin

! if Flag_com == 1 then compute dist from COM
! if its 0 then specify the atom no and g(r) will be computed..
! i.e. no of atoms from that atom between dist r and r + dr

 Allocate (Posx(nAtom,nIter))
 Allocate (Posy(nAtom,nIter))
 Allocate (Posz(nAtom,nIter))

 ! xyz_format = 0 ==> old_ks
 ! xyz_format = 1 ==> xmakemol
 ! xyz_format = 2 ==> Envision

 write(*,*)POS_file
 Open (unit=99, file=POS_file)
 if (xyz_format == 0 ) then
   do i = 1,nIter
    read(99,*)
    do j = 1,nAtom
       read(99,*)ii,Posx(j,i),Posy(j,i),Posz(j,i),ii
    enddo
   enddo 
 elseif (xyz_format == 1 ) then
   do i = 1,nIter
    read(99,*)ii
    read(99,*)
    do j = 1,nAtom
       read(99,*)jj,Posx(j,i),Posy(j,i),Posz(j,i)
    enddo
   enddo 
 elseif (xyz_format == 2 ) then
   read(99,*)
   read(99,*)
   read(99,*)
   read(99,*)
   do i = 1,nIter
    do j = 1,nAtom
       read(99,*)
       read(99,*)Posx(j,i),Posy(j,i),Posz(j,i)
    enddo
   enddo 
 endif
 Close (99)

 Write (*,'(\1x,"Reading Complete")')

 allocate (avj_dist_com (nIter))
 allocate (dist_com (nAtom))

 avj_dist_com = 0.0d0
 dist_com = 0.0d0

 Allocate (Posx_n(nAtom))
 Allocate (Posy_n(nAtom))
 Allocate (Posz_n(nAtom))

 Allocate (Bin_Array(bin))

 Posx_n = 0.0d0
 Posy_n = 0.0d0
 Posz_n = 0.0d0
 bin_array = 0.0d0

 Open (unit=2, file=COM_file)
 Do I = 1, nIter

  comx = 0.0d0
  comy = 0.0d0
  comz = 0.0d0

  Do J = 1, nAtom
     comx = comx + Posx(j,i)
     comy = comy + Posy(j,i)
     comz = comz + Posz(j,i)
  Enddo

     comx = comx/nAtom
     comy = comy/nAtom
     comz = comz/nAtom

  Write (*,*) i, comx, comy, comz

  Do J = 1, nAtom

     Posx_n (j) = Posx(j,i) - comx
     Posy_n (j) = Posy(j,i) - comy
     Posz_n (j) = Posz(j,i) - comz

     dist_com (j) = dsqrt ( Posx_n(j)*Posx_n(j)  &
                          + Posy_n(j)*Posy_n(j)  &
                          + Posz_n(j)*Posz_n(j)  )

     avj_dist_com (i)  = avj_dist_com(i) + dist_com(j)
  Enddo
  avj_dist_com(i)  = avj_dist_com(i)/nAtom

  Do j = 1, nAtom

    dist = dist_com (j) * dfloat((bin/max_dist))
    bin_array(dist) = bin_array(dist) + 1

  Enddo


  write (2,'(2x,i6,143(2x,f10.7))') I, avj_dist_com(i),(dist_com(k),k=1,nAtom)
  write(*,*) i

 Enddo
 close (2)

 Open (unit=3, file=Bin_file)
 do i = 1, bin
   write (3,'(2x,i6,4x,i8)') i , bin_array(i)
 enddo
 close (3)


 deAllocate (Posx)
 deAllocate (Posy)
 deAllocate (Posz)
 deAllocate (Posx_n)
 deAllocate (Posy_n)
 deAllocate (Posz_n)
 deallocate (avj_dist_com)
 deallocate (dist_com)
 deallocate (bin_array)

 Stop
End Program COM
这些错误看起来像

Real(KIND=r8b), Dimension (:), Allocatable   :: Posx, Posy, Posz
            1
Error: Integer expression required at (1)
还有更多


如何纠正这些错误?

类型的种类参数必须是整数常量表达式。由于使用命名常量
R8B
R4B
,因此后面的部分已被删除

但是,这就是错误消息所说的,您没有使用整型常量表达式。您应该注意到,
selected\u real\u kind
返回一个整数值,甚至与所选real类型的种类相同。因此,您可以使用

Integer, Parameter :: R8B=selected_real_kind(10)
Integer, Parameter :: R4B=selected_real_kind(4)

我已经尝试过稍微整理一下主体,但是我认为对于下一个问题(如果您有问题的话),您应该尝试为有问题的代码生成尽可能少的示例。我建议用一个更具描述性的标题,但如果你找到一个更好的,请随意更改。非常感谢!!由于我是fortran新手,我将阅读本文,同时也是stackoverflow新手,我将学习。。成功了!!感谢这对你有用,你应该考虑接受答案!