使用 kind 参数声明类型时出错

Error declaring types with kind parameter

使用以下程序时遇到错误。

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 (*,'(x,"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)

还有更多

我该如何纠正这些问题?

类型的种类参数必须是整型常量表达式。你有后一部分,因为你正在使用命名常量 R8BR4B.

但是,正如错误消息所说,您没有使用 integer 常量表达式。您应该注意到 selected_real_kind returns 是一个整数值,即使是所选实型的种类也是如此。因此,您可以使用

更正您的代码
Integer, Parameter :: R8B=selected_real_kind(10)
Integer, Parameter :: R4B=selected_real_kind(4)