      SUBROUTINE VectorPer(n1,n2,na,nb,nc,x,y,z,vx,vy,vz,norm)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! VectorPer
!
!!!!!!!!!!!!!!!!!!!!!!!
!
! This subroutine caclulates the vector defined by atom i and j
! with j being displaced to a periodic image of the system.
!
! Input:
!  i: (INTEGER) index of the first atom, in the central cell
!  j: (INTEGER) index of the second atom, in a periodic image
!  na,nb,nc: (INTEGER) number of displacement of j atom, in lattice vectors.
!  x(Nat), y(nat), z(nat) : (REAL) Cartesian coordinates of the system.
!
! Output:
! vx,vy,vz: (REAL) Vector i->j
! Norm:     (REAL) Norm of the vector.
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Nat and Lattice vectors (lat_a, lat_b, lat_c) are taken from Path_Module
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!----------------------------------------------------------------------
!  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 VarTypes
        use Path_module, only : NAt,Lat_a,Lat_b,Lat_c

        integer(KINT) :: n1,n2
        INTEGER(KINT) :: na,nb,nc
        real(KREAL) ::  x(Nat),y(Nat),z(Nat)
        real(KREAL) ::  vx,vy,vz,norm

        vx=x(n2)-x(n1)+na*Lat_a(1)+nb*Lat_b(1)+nc*Lat_c(1)
        vy=y(n2)-y(n1)+na*Lat_a(2)+nb*Lat_b(2)+nc*Lat_c(2)
        vz=z(n2)-z(n1)+na*Lat_a(3)+nb*Lat_b(3)+nc*Lat_c(3)

        norm=dsqrt( vx*vx + vy*vy + vz*vz )

!       write(6,*) "Dbg VectorPer : n1,n2, na,nb,nc :",n1,n2,na,nb,nc
!       write(6,*) vx,vy,vz,norm
!       write(6,*) 

        RETURN
      END SUBROUTINE VectorPer
