c=====================================================================
c ==================================================
c         Copyright And License Agreement
c ==================================================
c
c MMSQ Version 1.0, 4th of april 1991.
c
c You may copy and distribute copies of the complete MMSQ source
c code as you receive it, in any medium, provided that you
c conspicuously and appropriately publish on each file a valid
c copyright notice "Copyright (C) 1991 Rob Hooft, Utrecht
c University"; keep intact the notices on all files that refer to
c this License Agreement and give other recipients of MMSQ a copy
c of this license agreement along with the program. You may charge
c a distribution fee for the physical act of transferring a copy. 
c
c You may copy and distribute MMSQ in object code or executable
c form under the terms mentioned above provided that you also
c accompany it with the complete corresponding machine-readable
c source code, which must also be distributed under the terms
c mentioned above.
c
c You may not copy, sublicense, distribute or transfer MMSQ except 
c as expressly provided under this license agreement.
c
c (This license agreement has been modelled after the 
c GNU GENERAL PUBLIC LICENSE)
c
c=====================================================================
      program mmfinish

c====================Read the SQUARE.DAT file=====================

      include 'square.inc'

c====================Read the parameters==========================

      call getpar(0,line,ier)
      if (ier.eq.0) call getpar(1,outfile,ier)
      if (ier.eq.0) call getpar(2,line,ier)
      if (ier.eq.0) read(line,*) k1      
      if (ier.eq.0) call getpar(3,line,ier)
      if (ier.eq.0) read(line,*) k2      
      if (ier.ne.0) stop 'Parameter error : Needs 3 of them.'
      val1=dr_start(1)+k1*dr_step(1)
      val2=dr_start(2)+k2*dr_step(2)

c=================Read MM-outfile=================================
      l=length(outfile)
      open(2,file=outfile(1:l)//'.out',status='old')
210   continue
      read(2,'(A)') line
      if (line(7:25).ne.'FINAL STERIC ENERGY'.and.
     1 line(6:24).ne.'FINAL STERIC ENERGY') goto 210
      read(line,'(30x,f10.4)') energy
211   continue
      read(2,'(A)') line
      if (length(line).eq.0.or.line(7:7).eq.'(') goto 211
209   format(25x,f10.4)
212   format(31x,f10.4)
      if (line(17:27).eq.'COMPRESSION') then ! MM3
        read(line,212) en(1)
        read(2,212) en(2)
        read(2,212) en(3)
        read(2,212) en(4)
        read(2,*)
        read(2,212) en(5)
        read(2,212) en(6)
        read(2,*)
        read(2,212) en(7)
        read(2,212) en(8)
        read(2,212) en(9)
        read(2,212) en(10)
        read(2,212) en(11)
c        read(2,*)
c        read(2,*)
c        read(2,'(30x,f10.4)') en(12)
        numen=11
      else
        read(line,209) en(1)
        read(2,209) en(2)
        read(2,209) en(3)
        read(2,'(51x,f10.4,14x,f10.4)',iostat=iostat) en(4),en(5)
        numen=5
        read(2,209,err=789) en(6)
        numen=6
        read(2,209,err=789) en(7)
        numen=7
        read(2,209,err=789) en(8)
        numen=8
        read(2,209,err=789) en(9)
        numen=9
        read(2,209,err=789) en(10)
        numen=10
789     continue
      endif
cMM2HB========================================================================
c         1         2         3         4         5         6         7         8         9         0         1         2
c123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
c0     FINAL STERIC ENERGY IS     35.0475 KCAL.
c
c           COMPRESSION       1.1296
c           BENDING          10.5401
c           STRETCH-BEND      0.5802
c           VANDERWAALS                  REPULSIVE     65.5164  ATTRACTIVE    -61.7684
c             1,4 ENERGY     10.2835
c             OTHER          -6.5355
c           TORSIONAL        14.6790
c           ADDITIONAL       -2.5421     REPULSIVE      0.0000  ATTRACTIVE     -2.5421
c           DIPOLE            6.9127     DIPOLE MOMENT    3.532 D
c=============================================================================
cMM287========================================================================
c         1         2         3         4         5         6         7         8         9         0         1         2
c123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
c      FINAL STERIC ENERGY IS     36.2029 KCAL.
c
c           COMPRESSION       2.4142
c           BENDING          12.6903
c           STRETCH-BEND      1.1632
c           VANDERWAALS
c             1,4 ENERGY     19.7002
c             OTHER         -10.7223
c           TORSIONAL         4.1035
c           CHARGE-DIPOLE     0.0000
c           DIPOLE            6.8539
c=============================================================================
c=MMP85=======================================================================
c         1         2         3         4         5         6         7         8         9         0         1         2
c123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
c     FINAL STERIC ENERGY IS      1.6937 KCAL.
c
c          COMPRESSION       0.0331
c          BENDING           0.2444
c          STRETCH-BEND      0.0145
c          VANDERWAALS
c            1,4 ENERGY      0.8104
c            OTHER           0.0000
c          TORSIONAL         0.5914
c=============================================================================
c=MM3=OLD= ====================================================================
c         1         2         3         4         5         6 
c1234567890123456789012345678901234567890123456789012345678901
c      FINAL STERIC ENERGY IS      7.8909 KCAL.
c
c
c                COMPRESSION       0.2444
c                BENDING           1.3786
c                BEND-BEND         0.0141
c                STRETCH-BEND      0.1389
c                VANDERWAALS
c                  1,4 ENERGY      2.8679
c                  OTHER           0.5383
c                TORSIONAL         2.8719
c                TORSION-STRETCH  -0.1632
c                DIPOLE-DIPOLE     0.0000
c                CHARGE-DIPOLE     0.0000
c                CHARGE-CHARGE     0.0000
c=============================================================================
c=MM3=NEW= ====================================================================
c      FINAL STERIC ENERGY IS     21.2066 KCAL.
c      (WITHOUT ELECTRONIC ENERGY CORR.)
c
c
c                COMPRESSION       1.7119
c                BENDING           3.5419
c                BEND-BEND        -0.1250
c                STRETCH-BEND     -0.1358
c                VANDERWAALS
c                  1,4 ENERGY     19.3048
c                  OTHER          -0.1236
c
c                TORSIONAL        -9.5386
c                TORSION-STRETCH  -0.0960
c                DIPOLE-DIPOLE     6.6672
c                CHARGE-DIPOLE     0.0000
c                CHARGE-CHARGE     0.0000
c
c
c      FINAL ENERGY IS         -1366.4955 KCAL.
c=============================================================================


      close(2,status='delete')

c=================Update History==================================

      call waithisfile(2)
      write(2,'(2I5,12F10.4)',iostat=iostat) 
     1     nint(val1),nint(val2),energy,(en(i),i=1,numen) 
      close(2)

c=============Look at energy.=====================================

      if (en(1).lt.0) then
        write(*,*) 'Ai, The compression energy is negative.'
        energy=max_energy+1
      endif

c=================Modify Square-file.=============================

      call waitbinfile(2)

      nr=(i2+1)*k1+k2+1
      read(2,rec=nr) rec
      if (rec.busy1l) then
        rec.done1l=.true.
        rec.busy1l=.false.
      endif
      if (rec.busy1h) then
        rec.done1h=.true.
        rec.busy1h=.false.
      endif
      if (rec.busy2l) then
        rec.done2l=.true.
        rec.busy2l=.false.
      endif
      if (rec.busy2h) then
        rec.done2h=.true.
        rec.busy2h=.false.
      endif
      if (rec.first_busy) then
        rec.first_done=.true.
        rec.first_busy=.false.
      endif
      if (energy.ge.rec.total_energy) then
        write(2,rec=nr) rec
        write(*,678) k1,k2
678     format('Worse ',2i3)
        call deletefile(outfile(1:l)//'.resn')
      else
        rec.total_energy=energy
        write(2,rec=nr) rec
        write(*,679) k1,k2
679     format(' Improved ',2i3)
c
c Periodicity.
c
        k1m1=-1
        if (dr_end(1)-dr_start(1)+dr_step(1).eq.360) k1m1=i1
        if (k1.gt.0) k1m1=k1-1
        k1p1=-1
        if (dr_end(1)-dr_start(1)+dr_step(1).eq.360) k1p1=0
        if (k1.lt.i1) k1p1=k1+1
        k2m1=-1
        if (dr_end(2)-dr_start(2)+dr_step(2).eq.360) k2m1=i2
        if (k2.gt.0) k2m1=k2-1
        k2p1=-1
        if (dr_end(2)-dr_start(2)+dr_step(2).eq.360) k2p1=0
        if (k2.lt.i2) k2p1=k2+1
c
c Re-enable
c
        if (k1p1.ge.0) then
          nr=(i2+1)*(k1p1)+k2+1
          read(2,rec=nr) rec
          if (rec.done1l) then
            rec.done1l=.false.
            write(2,rec=nr) rec
            write(*,*) 'Re-enable ',k1p1,k2
          endif
        endif
        if (k1m1.ge.0) then
          nr=(i2+1)*(k1m1)+k2+1
          read(2,rec=nr) rec
          if (rec.done1h) then
            rec.done1h=.false.
            write(2,rec=nr) rec
            write(*,*) 'Re-enable ',k1m1,k2
          endif
        endif
        if (k2p1.ge.0) then
          nr=(i2+1)*(k1)+(k2p1)+1
          read(2,rec=nr) rec
          if (rec.done2l) then
            rec.done2l=.false.
            write(2,rec=nr) rec
            write(*,*) 'Re-enable ',k1,k2p1
          endif
        endif
        if (k2m1.ge.0) then
          nr=(i2+1)*(k1)+(k2m1)+1
          read(2,rec=nr) rec
          if (rec.done2h) then
            rec.done2h=.false.
            write(2,rec=nr) rec
            write(*,*) 'Re-enable ',k1,k2m1
          endif
        endif
        call deletefile(outfile(1:l)//'.res')
        call renamefile(outfile(1:l)//'.resn',outfile(1:l)//'.res')
      endif
c      call finish(6)
      call exit(1)
      end
