FORTRAN计数器循环返回相同值的多次迭代

时间:2015-04-13 23:07:28

标签: gfortran do-loops chemistry

首先,我是FORTRAN的完全新手。据说我试图“构建”一个盒子,然后随机生成100个原子的x,y,z坐标。从那里,目标是计算每个原子之间的距离,这成为Lennard-Jones势能方程的值“r”。然后计算LJ势,最后求出整个盒子的潜力。我之前提出的关于这个项目的问题是here。问题是我一遍又一遍地得到相同的计算值。我的代码如下。

    program energytot
    implicit none

    integer, parameter :: n = 100
    integer :: i, j, k, seed(12)
    double precision :: sigma, r, epsilon, lx, ly, lz
    double precision, dimension(n) :: x, y, z, cx, cy, cz
    double precision, dimension(n*(n+1)/2) :: dx, dy, dz, LJx, LJy, LJz
    sigma = 4.1
    epsilon = 1.7
    !Box length with respect to the axis
    lx = 15
    ly = 15
    lz = 15

    do i=1,12
        seed(i)=i+3
    end do
    !generate n random numbers for x, y, z
    call RANDOM_SEED(PUT = seed)
    call random_number(x)
    call random_number(y)
    call random_number(z)
    !convert random numbers into x, y, z coordinates
    cx = ((2*x)-1)*(lx*0.5)
    cy = ((2*y)-1)*(lx*0.5)
    cz = ((2*z)-1)*(lz*0.5)

    do j=1,n-1
        do k=j+1,n
            dx = ABS((cx(j) - cx(k)))
            LJx = 4 * epsilon * ((sigma/dx(j))**12 - (sigma/dx(j))**6)
            dy = ABS((cy(j) - cy(k)))
            LJy = 4 * epsilon * ((sigma/dy(j))**12 - (sigma/dy(j))**6)
            dz = ABS((cz(j) - cz(k)))
            LJz = 4 * epsilon * ((sigma/dz(j))**12 - (sigma/dz(j))**6)
        end do
    end do

    print*, dx
    end program energytot

2 个答案:

答案 0 :(得分:1)

你的问题究竟是什么?你希望你的代码做什么,以及它做了什么呢?

如果您对最终的打印声明print*, dx有疑问,请尝试以下方法:

  print *, 'dx = '
  do i = 1, n * (n + 1) / 2
    print *, dx(i)
  end do

似乎dx太大而无法在没有循环的情况下打印。

此外,您似乎重复将数组dx(以及循环中的其他数组)分配给单个值。试试这个:

  i = 0
  do j=1,n-1
      do k=j+1,n
          i = i + 1
          dx(i) = ABS((cx(j) - cx(k)))
      end do
  end do

这样,每个值cx(j) - cx(k)都会保存到dx的其他元素,而不是覆盖以前保存的值。

答案 1 :(得分:0)

我的新代码是这样的:

    program energytot
    implicit none

    integer, parameter :: n = 6
    integer :: i, j, k, seed(12)
    double precision :: sigma, r, epsilon, lx, ly, lz, etot, pot, rx, ry, rz
    double precision, dimension(n) :: x, y, z, cx, cy, cz
    sigma = 4.1
    epsilon = 1.7
    etot=0
    !Box length with respect to the axis
    lx = 15
    ly = 15
    lz = 15

    do i=1,12
        seed(i)=i+90
    end do
    !generate n random numbers for x, y, z
    call RANDOM_SEED(PUT = seed)
    call random_number(x)
    call random_number(y)
    call random_number(z)
    !convert random numbers into x, y, z coordinates
    cx = ((2*x)-1)*(lx*0.5)
    cy = ((2*y)-1)*(lx*0.5)
    cz = ((2*z)-1)*(lz*0.5)

    do j=1,n-1
        do k=j+1,n
            rx = (cx(j) - cx(k))
            ry = (cy(j) - cy(k))
            rz = (cz(j) - cz(k))
            !Apply minimum image convention
            rx=rx-lx*anint(rx/lx)
            ry=ry-ly*anint(ry/ly)
            rz=rz-lz*anint(rz/lz)

            r=sqrt(rx**2+ry**2+rz**2)
            pot=4 * epsilon * ((sigma/r)**12 - (sigma/r)**6)
            print*,pot
            etot=etot+pot

        end do
    end do

    print*, etot

    end program energytot