      FUNCTION test_zmat(na,ind_zmat)

! Test the validity of a z-matrix 

!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

      use Path_module
      use Io_module

      IMPLICIT NONE

      logical :: pos,test_zmat
      integer(KINT) :: i,na,n
      integer(KINT) :: ind_zmat(Na,5)
      integer(KINT), ALLOCATABLE :: ind_pos(:)

      ALLOCATE(ind_pos(Na))
       pos=.true.

      DO i=1,na
       ind_pos(ind_zmat(i,1))=i
      END DO

      DO i=2,na
        n=ind_zmat(i,2)
        IF (ind_pos(n) .GE. i) THEN
           pos=.false. .AND. pos
           write(IOOUT,*) 'Bond:',n,ind_pos(n),i,pos
        END IF
      END DO

      DO i=3,na
        n=ind_zmat(i,3)
        IF (ind_pos(n) .GE. i) THEN
           pos=.false. .AND. pos
           write(IOOUT,*) 'Angle:',n,ind_pos(n),i,pos
        END IF
      END DO

      DO i=4,na
        n=ind_zmat(i,4)
        IF (ind_pos(n) .GE. i) THEN
           pos=.false. .AND. pos
           write(IOOUT,*) 'Dihedral:',n,ind_pos(n),i,pos
        END IF
      END DO

      test_zmat=pos
      DEALLOCATE(ind_pos)

      END
