      SUBROUTINE OVERLP(NEWMOD,NVAR)
      implicit double precision (A-H,O-Z)
      include 'SIZES'
      include 'MOP6FILE'
      common/OPTEF/OLDF(MAXPAR),D(MAXPAR),VMODE(MAXPAR),
     1             U(MAXPAR,MAXPAR),DD,MODE,NSTEP,NEGREQ,IPRNT
C
C  ON THE FIRST STEP SIMPLY DETERMINE WHICH MODE TO FOLLOW
C
      if (NSTEP.EQ.1) then
C
         if (MODE.GT.NVAR) then
            write(MIOB,*) 'ERROR!! MODE IS LARGER THAN NVAR',MODE
            call filclo  !  J/M
            stop ' in OVERLP - wrong mode requested'
         end if
C
         IT=MODE
         write(MIOB,40) MODE
C
      else
C
C  ON SUBSEQUENT STEPS DETERMINE WHICH HESSIAN EIGENVECTOR HAS
C  THE GREATEST OVERLAP WITH THE MODE WE ARE FOLLOWING
C
         IT=1
         TOVLP=DOT(U(1,1),VMODE,NVAR)
         TOVLP=ABS(TOVLP)
         do 10 I=2,NVAR
            OVLP=DOT(U(1,I),VMODE,NVAR)
            OVLP=ABS(OVLP)
            if (OVLP.GT.TOVLP) then
               TOVLP=OVLP
               IT=I
            end if
   10    continue
C
         write(MIOB,30) TOVLP
      end if
C
C  SAVE THE EIGENVECTOR IN VMODE
C
      do 20 I=1,NVAR
         VMODE(I)=U(I,IT)
   20 continue
C
      NEWMOD=IT
      return
C
   30 format(5X,'OVERLAP OF CURRENT MODE WITH PREVIOUS MODE IS ',F12.6)
C
   40 format(5X,'HESSIAN MODE FOLLOWING SWITCHED ON'/
     1     '     FOLLOWING MODE ',I3)
C
      end
