C
C $Id: resmove.F,v 1.6 1998/07/16 16:40:38 jjv5 Exp $
C
C------------------------------------------------------------------------
      subroutine resmove(ires, picked, npicked, movedres,
     &     subpicked1)

      implicit double precision (a-h,o-z)
#include "divcon.dim"
#include "divcon.h"

C     does the mc move for the residues

C     arguments:
      dimension ires(maxres)
      logical picked(maxres), subpicked1(maxsub)

CCC-------------------------------------------------------------CCC

C     needed for random number generator later on: don't change it!
      irnd = 2

      if ((npicked + nmove) .gt. nres) then
         movedres = nres - npicked
      else
         movedres = nmove
      endif

      do i=1,3
         ndangle(i) = 0
         ndrmc(i) = 0
      enddo

      if (frozenmc.and.(.not.stand)) then
         do i=1,nsub
            notfroz(i) = subpicked1(i)
            subpicked1(i) = .false.
         enddo
      endif	

      do 1000 imc=1,movedres
C     .  take a random residue
 10      ir = irandom(iseed,nres)
         if (picked(ir)) goto 10
         picked(ir) = .true.
         npicked = npicked + 1
         ires(imc) = ir

         if (frozenmc.and.(.not.stand)) then
C     .     check for every subsystem if an atom of residue ir is in core
            iate = irpnt(ir+1)-1
            do iat=irpnt(ir),iate
               do 20 k=1,nsub
                  if (notfroz(k)) goto 20
                  ise = iatom1(k+1)-1
                  do is=iatom1(k),ise
                     if (iatoms(is).eq.iat) then
                        if (iabuff(is).eq.0) then
                           notfroz(k) = .true.
                           subpicked1(k) = .true.
                           goto 20
                        endif
                     elseif (iatoms(is).gt.iat) then
                        goto 20
                     endif
                  enddo
 20            continue
            enddo
         endif
 100     call rottr(ir)
 1000 continue

C     calculate xyzmin in case of PME-calculation
      if (pme.or.gridsub) then
         do i=1,3
            xyzmin(i) = xyz(i,1)
            xyzmax(i) = xyz(i,1)
         enddo
         do 2010 iat=2,natoms
            do i=1,3
               xyzmin(i) = min(xyzmin(i),xyz(i,iat))
               xyzmax(i) = max(xyzmax(i),xyz(i,iat))
            enddo
 2010    continue
         do i=1,3
            dxyz(i) = xyzmax(i)-xyzmin(i)
         enddo
      endif

      end