SUBROUTINE  add2indzmat(na,izm,n1,n2,n3,n4,ind_zmat,x,y,z)
  ! This subroutine adds the description of atom n1
  ! into ind_zmat at position izm
  ! it checks that atoms n1 n2 n3 are not aligned
  ! and if so, it describes n1 as n1 n2 n4 n3 instead of n1 n2 n3 n4.
  ! As n2 n3 n4 are not aligned, this is sufficient to ensure that the description
  ! of n1 is ok.

!----------------------------------------------------------------------
!  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.
!----------------------------------------------------------------------

  IMPLICIT NONE

  INTEGER, PARAMETER :: KINT=KIND(1)
  INTEGER, PARAMETER :: KREAL=KIND(1.0D0)


  INTEGER(KINT), INTENT(IN) :: na,izm,n1,n2
  INTEGER(KINT), INTENT(INOUT) :: n3,n4
  INTEGER(KINT) :: ind_zmat(na,5),n0
  REAL(KREAL) :: x(na), y(na), z(na)

  real(KREAL) :: vx1,vy1,vz1,norm1
  real(KREAL) :: vx2,vy2,vz2,norm2
  real(KREAL) :: vx3,vy3,vz3,norm3
  REAL(KREAL) :: pi

  LOGICAL :: debug


  REAL(KREAL) :: Ang123,Ang234,sangle

  INTERFACE
     function valid(string) result (isValid)
       CHARACTER(*), intent(in) :: string
       logical                  :: isValid
     END function VALID

     FUNCTION angle(v1x,v1y,v1z,norm1,v2x,v2y,v2z,norm2)
       use Path_module, only :  Pi,KINT, KREAL
       real(KREAL) ::  v1x,v1y,v1z,norm1
       real(KREAL) ::  v2x,v2y,v2z,norm2
       real(KREAL) ::  angle
     END FUNCTION ANGLE

  END INTERFACE

  debug=valid("add2indzmat")
  pi=acos(-1.d0)

  if (debug) WRITE(*,*) "Entering Add2IndZmat with izm,n1,n2,n3,n4",izm,n1,n2,n3,n4
  Call vecteur(n2,n1,x,y,z,vx1,vy1,vz1,norm1)
  Call vecteur(n2,n3,x,y,z,vx2,vy2,vz2,norm2)
  Call vecteur(n3,n4,x,y,z,vx3,vy3,vz3,norm3)

  Ang123=angle(vx1,vy1,vz1,norm1,vx2,vy2,vz2,norm2)/180.*pi
  Ang234=angle(vx2,vy2,vz2,norm2,vx3,vy3,vz3,norm3)/180.*pi

  sangle=sin(ang123)
  if (sangle.LE.0.09d0) THEN
     sangle=sin(ang234) 
     if (sangle.LE.0.09d0) THEN
        WRITE(*,*) "ERROR in Add2IndZmat : all four atoms aligned"
        WRITE(*,*) n1,n2,n3,n4
        STOP
     END IF
     n0=n3
     n3=n4
     n4=n0
  END IF
  ind_zmat(izm,1)=n1
  ind_zmat(izm,2)=n2
  ind_zmat(izm,3)=n3
  ind_zmat(izm,4)=n4

  RETURN
END SUBROUTINE add2indzmat

