最陡的下降算法的麻烦

时间:2014-02-13 00:17:04

标签: arrays physics fortran77 chemistry gradient-descent

我正在尝试编写一个程序,使用最速下降算法最小化二维400原子系统的总能量。我的计划的一般想法如下:

  1. 获取原子坐标(x,y)
  2. 随机选择一个原子
  3. 计算原子上力的x和y分量
  4. 计算x和y位置的变化,dx和dy
  5. 生成新坐标(x + dx,y + dy)并更新数组
  6. 重复步骤2-5,直到每个原子上的力变为〜0
  7. 由于原子210上的初始力的大小很大,因此它是系统接近收敛的良好标记。我还没有修复代码,以便当力在某个容差因子内时迭代停止。话虽如此,我的代码在原子210上打印了力的x分量,这样我就能看出力是否趋于0。

    当我运行我的代码时,似乎坐标数组没有更新(上面的步骤5)。我不确定是否在本网站或物理网站上发布我的问题,但是,我相信我的问题涉及在Fortran 77中更新数组的一些技术性。如果这超出了本网站的范围,我很抱歉。我只是不确定在哪里转弯。谢谢大家提供的任何帮助。如果我的注释不清楚或者是否有人需要更多信息,请告诉我。这是代码。

      program molstat_stpdesc
      implicit none
    
      !variables
      integer i, j, k, count
      real rmin, sigma, coord(3,400), rcut, fx, fy, drx
      real neighbors(400,24), nofneighbors(400), dry, lambda
      real forces(2,400,400), tforces(2,400)
      parameter (sigma=3.405)
      !i: iteration #
      !j: loop variable
      !k: random atom
      !count: lets me know if I need to update the map of neighbors
      !rmin: initial separation between the atoms (in angstroms)
      !sigma: Lennard-Jones sigma parameter
      !rcut: distance beyond which the potential energy of interaction is 0
      !lambda: factor multiplying the force to generate dr, usually small
      !forces: not used in this calculation
      !tforces: array containing x- and y-component of force on an atom
    
      open(9,file='minimization.out',status='replace')
    
      rmin=2**(1.0/6.0)*sigma
      rcut=7.5
      lambda=(1.0/5.0)*rmin
    
      !get the starting coordinates, coord
      !coord(1,i): number identifying atom i
      !coord(2,i): x-coordinate of atom i
      !coord(3,i): y-coordinate of atom i
      call construct(rmin,coord)
    
      !get the starting map of neighbors
      call map(rmin,rcut,coord1,neighbors,nofneighbors)
    
      count=0
      write(9,'("Forces on atom 210")')
      do i=1,1000 !# of iterations
         do j=1,400 !try to sample every atom per iteration
            k=int(400*rand(0)) !choose a random atom
            if (k .eq. 0) then !because random number generator may give me 0
               continue
            else
               !get the forces
               call force(coord,nofneighbors,neighbors,forces,tforces)
               fx=tforces(1,k)  !x-component of the force on atom j
               fy=tforces(2,k)  !y-component
               drx=lambda*fx    !step size in the x-direction
               dry=lambda*fy    !y-direction
               coord(2,k)=coord(2,k)+drx !step in the x-direction
               coord(3,k)=coord(3,k)+dry !y-direction
            endif
         enddo
         write(9,'("Iteration: ",I4)') i
         !print the force of an atom in the center of the block
         write(9,'("x-component: ",F7.4)') tforces(1,210)
         write(9,'()')
         count=count+1
         if (count .eq. 10) then
            !update map of neighbors
            call map(rmin,rcut,coord1,neighbors,nofneighbors)
            count=0
         else
            continue
         endif
      enddo
    
      close(9)
    
      open(10,file='minimization.xy',status='replace')
      write(10,'("#x-coordinate     y-coordinate")')
      write(10,'("#------------     ------------")')
      do i=1,400
         write(10,'("    ",F5.2,"            ",F5.2)')
     +        coord(2,i), coord(3,i)
      enddo
    
      end
    

0 个答案:

没有答案