



      SUBROUTINE SETBOUNDTO (IAT,JAT,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'DATBAS.INC'
      INTEGER       IAT, JAT, KAT, IERR, IAA, JAA, IBND
      REAL          WIFDST
      LOGICAL       MOL009, WIFIRD, GVFYON









      IF ((.NOT.ISATOK(IAT)).OR.(.NOT.ISATOK(JAT))) GOTO 998
      IF (IAT.EQ.JAT) THEN
         CALL GVSTTI ('?M4571',IAT)
         GOTO 998
      END IF
      IF (BOUND_TO(8,IAT).NE.0.OR.BOUND_TO(8,JAT).NE.0) GOTO 998
      IAA=ATIAA(IAT)
      JAA=ATIAA(JAT)
      IF (WIFPAR(1208).EQ.1) CALL GVSTT6 ('?M4572')
      IF (MOL009(ONESA3(IAA)).OR.MOL009(ONESA3(JAA))) GOTO 998
      IF (WIFPAR(1208).EQ.1) CALL GVSTT6 ('?M4573')
      IF (MODNMB(IAA).NE.MODNMB(JAA)) GOTO 998
      IF (WIFPAR(1208).EQ.1) CALL GVSTT6 ('?M4575')
      IF (NOBOND(ATIAA(IAT)).OR.NOBOND(ATIAA(JAT))) GOTO 998 



      IF (WIFIRD(IAA).AND.WIFIRD(JAA).AND.IAA.NE.JAA) THEN
         IF (WEIGHT(IAT).LE.0.51.OR.WEIGHT(JAT).LE.0.51) GOTO 998
         IF (WEIGHT(IAT)+WEIGHT(JAT).LE.1.09) GOTO 998
      END IF



      IERR=0



      IF (WIFPAR(1208).GE.3) THEN
         PRINT*,'BOUND_TO:',ONESAT(IAT),ONESAT(JAT),WIFDST(IAT,JAT)
         CALL SHOIAA (ATIAA(IAT))
         CALL SHOIAA (ATIAA(JAT))
         CALL GVSLIN
      END IF
      DO 10 IBND=1,8
         KAT=BOUND_TO(IBND,IAT)
         IF (KAT.EQ.0) THEN
            BOUND_TO(IBND,IAT)=JAT
            GOTO 11
         ELSE IF (KAT.EQ.JAT) THEN
            IF (WIFPAR(1208).EQ.1) THEN
               CALL GVSTTB ('?M3091')
               CALL DIF016 (6,IAT,JAT)
               CALL GVSKIL
            END IF
            GOTO 998
         ELSE
            IF (WIFPAR(1208).EQ.1) 
     +       CALL DIF015A (KAT,'?M3892')
         END IF
 10   CONTINUE
      IF (.NOT.FOR3DM) THEN
         IF (WIFPAR(462).LE.6) THEN
            WIFPAR(462)=WIFPAR(462)+1
            CALL DIF025 (JAT)
            CALL SHOWTEXT ('?M2158')
         END IF
         IF (WIFPAR(462).EQ.6) THEN
            WIFPAR(462)=WIFPAR(462)+1
            CALL GVSTT6 ('?M4917')
         END IF
      END IF
 11   CONTINUE



      DO 20 IBND=1,8
         KAT=BOUND_TO(IBND,JAT)
         IF (KAT.EQ.0) THEN
            BOUND_TO(IBND,JAT)=IAT
            GOTO 21
         ELSE IF (KAT.EQ.IAT) THEN
            IF (WIFPAR(1208).EQ.1) THEN
               CALL GVSTTB ('?M3091')
               CALL DIF016 (6,JAT,IAT)
               CALL GVSKIL
            END IF
            GOTO 998
         ELSE
            IF (WIFPAR(1208).EQ.1) 
     +       CALL DIF015A (KAT,'?M3892')
         END IF
 20   CONTINUE
      IF (.NOT.FOR3DM) THEN
         IF (WIFPAR(462).LE.6) THEN
            WIFPAR(462)=WIFPAR(462)+1
            CALL DIF025 (JAT)
            CALL SHOWTEXT ('?M2158')
         END IF
         IF (WIFPAR(462).EQ.6) THEN
            WIFPAR(462)=WIFPAR(462)+1
            CALL GVSTT6 ('?M4917')
         END IF
      END IF
 21   CONTINUE
      IF (WIFPAR(1208).EQ.1) THEN
         CALL GVSTT6 ('?M3893')
         CALL DIF016 (6,IAT,JAT)
         IF (GVFYON('?M4576',' ')) CALL GVSKIL
      END IF



  998 CONTINUE


      RETURN
      END   
      SUBROUTINE SETUNBOUNDTO (IAT,JAT,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IAT, JAT, IBND, JBND, ICNT, JCNT, IERR







      IERR=0
      ICNT=0
      JCNT=0




















      DO 20 IBND=1,8
         IF (BOUND_TO(IBND,IAT).EQ.JAT) THEN
            IF (IBND.LE.7) THEN
               DO 10 JBND=IBND,7
                  BOUND_TO(JBND,IAT)=BOUND_TO(JBND+1,IAT)
 10            CONTINUE
            END IF
            BOUND_TO(8,IAT)=0
            ICNT=ICNT+1
         END IF
 20   CONTINUE



      DO 40 IBND=1,8
         IF (BOUND_TO(IBND,JAT).EQ.IAT) THEN
            IF (IBND.LE.7) THEN
               DO 30 JBND=IBND,7
                  BOUND_TO(JBND,JAT)=BOUND_TO(JBND+1,JAT)
 30            CONTINUE
            END IF
            BOUND_TO(8,JAT)=0
            JCNT=JCNT+1
         END IF
 40   CONTINUE



      IF (ICNT.NE.1.OR.JCNT.NE.1) THEN
         IERR=1
         PRINT*,'ASYMMETRIC BOUND_TO:',ICNT,JCNT
         PRINT*,IAT,ONESAT(IAT),ATIAA(IAT),' ',ONESA3(ATIAA(IAT)),'(',
     +    NUMNAM(ATIAA(IAT)),') ',CHANAM(ATIAA(IAT))
         PRINT*,JAT,ONESAT(JAT),ATIAA(JAT),' ',ONESA3(ATIAA(JAT)),'(',
     +    NUMNAM(ATIAA(JAT)),') ',CHANAM(ATIAA(JAT))
         CALL DIF015 (6,'IAT=',IAT)
         PRINT*,(BOUND_TO(JBND,IAT),JBND=1,8)
         PRINT*,(ONESAT(BOUND_TO(JBND,IAT)),JBND=1,8)
         CALL DIF015 (6,'JAT=',JAT)
         PRINT*,(BOUND_TO(JBND,JAT),JBND=1,8)
         PRINT*,(ONESAT(BOUND_TO(JBND,JAT)),JBND=1,8)
         CALL GVSKIL
      END IF





      RETURN
      END   
      SUBROUTINE DWNBOUNDTO (IAT)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IAT, IBND, JBND







      DO 20 IBND=1,7
         IF (BOUND_TO(IBND,IAT).EQ.0) THEN
            DO 10 JBND=IBND,7
               BOUND_TO(JBND,IAT)=BOUND_TO(JBND+1,IAT)
 10         CONTINUE
            BOUND_TO(8,IAT)=0
         END IF
 20   CONTINUE





      RETURN
      END   
      SUBROUTINE SYMBOUNDTO (IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IAT, JAT, IAA, IBND, JBND, IERR







      DO 10 IAT=1,NUMATF
         CALL DWNBOUNDTO (IAT)
 10   CONTINUE



      IERR=0



      DO 60 IAA=1,NUMAAT
         DO 50 IAT=PNTAAS(IAA),PNTAAE(IAA)
            DO 40 IBND=1,8
               IF (BOUND_TO(IBND,IAT).NE.0) THEN



                  JAT=BOUND_TO(IBND,IAT)
                  DO 20 JBND=1,8
                     IF (BOUND_TO(JBND,JAT).EQ.IAT) GOTO 40
 20               CONTINUE
                  PRINT*,'BUG, JAT NOT BOUND BACK TO IAT',IAT,JAT
                  CALL DIF015 (6,'IAT=',IAT)
                  PRINT*,(BOUND_TO(JBND,IAT),JBND=1,8)
                  CALL DIF015 (6,'JAT=',JAT)
                  PRINT*,(BOUND_TO(JBND,JAT),JBND=1,8)
                  CALL GVSKIL



                  DO 30 JBND=IBND,8
                     IF (BOUND_TO(JBND,IAT).EQ.0) BOUND_TO(JBND,IAT)=JAT
                     PRINT*,'BUT THIS PROBLEM GOT FIXED'
                     GOTO 40
 30               CONTINUE
               END IF
 40         CONTINUE
 50      CONTINUE
 60   CONTINUE





      RETURN
      END   
      SUBROUTINE SYMBOUNDIAT (IAT,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IAT, JAT, IBND, JBND, IERR







      IERR=0



      DO 40 IBND=1,8
         IF (BOUND_TO(IBND,IAT).NE.0) THEN



            JAT=BOUND_TO(IBND,IAT)
            DO 20 JBND=1,8
               IF (BOUND_TO(JBND,JAT).EQ.IAT) GOTO 40
 20         CONTINUE
            PRINT*,'BUG, JAT NOT BOUND BACK TO IAT',IAT,JAT
            CALL DIF015 (6,'IAT=',IAT)
            PRINT*,(BOUND_TO(JBND,IAT),JBND=1,8)
            CALL DIF015 (6,'JAT=',JAT)
            PRINT*,(BOUND_TO(JBND,JAT),JBND=1,8)
            CALL GVSKIL



            DO 30 JBND=IBND,8
               IF (BOUND_TO(JBND,IAT).EQ.0) BOUND_TO(JBND,IAT)=JAT
               PRINT*,'BUT THIS PROBLEM GOT FIXED'
               GOTO 40
 30         CONTINUE
         END IF
 40   CONTINUE





      RETURN
      END   
      SUBROUTINE CHKBOUNDTO (TEXT)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IAT, JAT, IBND, JBND, KBND
      LOGICAL       XYZNUL
      CHARACTER*(*) TEXT







      IF (TEXT(1:1).NE.' ') GOTO 998
      CALL GVST6C ('CHK-BOUND_TO : ',TEXT,.FALSE.)



      DO 30 IAT=1,NUMATF
         IF (.NOT.ISATOK(IAT).OR.XYZNUL(IAT)) GOTO 30
         DO 20 IBND=1,8
            JAT=BOUND_TO(IBND,IAT)
            IF (JAT.NE.0) THEN
              DO 10 JBND=1,8
                  IF (BOUND_TO(JBND,JAT).EQ.IAT) GOTO 30
 10            CONTINUE
               PRINT*,'ASYMMETRIC BOUND_TO:'
               PRINT*,IAT,ONESAT(IAT),ATIAA(IAT),' ',ONESA3(ATIAA(IAT)),
     +          '(',NUMNAM(ATIAA(IAT)),') ',CHANAM(ATIAA(IAT)),
     +          JAT,ONESAT(JAT),ATIAA(JAT),' ',ONESA3(ATIAA(JAT)),'(',
     +          NUMNAM(ATIAA(JAT)),') ',CHANAM(ATIAA(JAT))
               CALL DIF015 (6,'IAT=',IAT)
               PRINT*,(BOUND_TO(JBND,IAT),JBND=1,8)
               PRINT*,(ONESAT(BOUND_TO(JBND,IAT)),JBND=1,8)
               CALL DIF015 (6,'JAT=',JAT)
               PRINT*,(BOUND_TO(JBND,JAT),JBND=1,8)
               PRINT*,(ONESAT(BOUND_TO(JBND,JAT)),JBND=1,8)
               CALL GVSKIL
            END IF
 20      CONTINUE
 30   CONTINUE



      DO 60 IAT=1,NUMATF
         DO 50 IBND=1,7
            IF (BOUND_TO(IBND,IAT).NE.0) THEN
               DO 40 JBND=IBND+1,8
                  IF (BOUND_TO(JBND,IAT).EQ.0) GOTO 40
                  IF (BOUND_TO(JBND,IAT).EQ.BOUND_TO(IBND,IAT)) THEN
                     PRINT*,'Double BOUND_TO:'
                     CALL DIF015 (6,'IAT=',IAT)
                     PRINT*,(BOUND_TO(KBND,IAT),KBND=1,8)
                     PRINT*,(ONESAT(BOUND_TO(KBND,IAT)),KBND=1,8)
                  END IF
 40            CONTINUE
            END IF
 50      CONTINUE
 60   CONTINUE



  998 CONTINUE


      RETURN
      END   
