module velrescalemodule
contains
subroutine velrescale(N,vx,vy,vz,Temp)

	implicit none
	integer, intent(IN) :: N
    double precision, intent(IN) :: Temp
	double precision, dimension(:), intent(INOUT) :: vx,vy,vz
	double precision lamida,q,sum1,sum2,sum3
	integer :: i

	!T=86*0.00836758  !dimensionless temp of 86 K
	q=0
    sum1=0
    sum2=0
    sum3=0


	do i=1,N
        sum1=sum1+vx(i)   !system moving with velocity vx=a
        sum2=sum2+vy(i)   !system moving with velocity vy=b
        sum3=sum3+vz(i)	!system moving with velocity vz=c
		q=q+(vx(i)*vx(i)+vy(i)*vy(i)+vz(i)*vz(i))
	end do

    sum1=sum1/(N*1.0)
    sum2=sum2/(N*1.0)
    sum3=sum3/(N*1.0)


	lamida=sqrt(3*N*Temp/q)  !vx=p*vx, p= lamida

	!scaling and reducing by vel of system
	do i=1,N
        vx(i)=lamida*(vx(i)-sum1)
        vy(i)=lamida*(vy(i)-sum2)
        vz(i)=lamida*(vz(i)-sum3)
	end do


end subroutine velrescale
end module velrescalemodule
	  
