// File: /workspace/github_cpp_makefile_star2_O3/shuwenyue[P]MD_code_cell_list/particle.cpp
// Function: particle::verlet(double, double, double, std::vector<vec3<double>, std::allocator<vec3<double>>>)
double particle::verlet(double dt, double NParticles, double BoxLength, vector<double3> forceV)
{
	// define variable: total velocities in each direction, total velocity^2, new positions, particle velocities
    double totalVx = 0;
    double totalVy = 0;
    double totalVz = 0;
    double totalV2 = 0;
    double newX = 0;
    double newY = 0;
    double newZ = 0;
    double velocityx = 0;
    double velocityy = 0;
    double velocityz = 0;
    
    // define objects to use functions in other classes 
    force forceObj;
	box boxObj;

	// loop through all particles to determine new positions
    for (int a = 0; a < NParticles; a++)
    {
		// calculate new position coordinates from verlet algorithm
        newX = 2 * positionVector[a].x - positionPrevVector[a].x + dt*dt * forceV[a].x;
        newY = 2 * positionVector[a].y - positionPrevVector[a].y + dt*dt * forceV[a].y;
        newZ = 2 * positionVector[a].z - positionPrevVector[a].z + dt*dt * forceV[a].z;

		// periodic boundary conditions
		newX = boxObj.pbc(newX,BoxLength);
		newY = boxObj.pbc(newY,BoxLength);
		newZ = boxObj.pbc(newZ,BoxLength);

        // calculate new velocity from verlet algorithm
        velocityx = (boxObj.boundaries(newX - positionPrevVector[a].x,BoxLength))/(2*dt);
        velocityy = (boxObj.boundaries(newY - positionPrevVector[a].y,BoxLength))/(2*dt);
        velocityz = (boxObj.boundaries(newZ - positionPrevVector[a].z,BoxLength))/(2*dt);

        // velocity center of mass
        //totalVx = totalVx + velocityx;
        //totalVy = totalVy + velocityy;
        //totalVz = totalVz + velocityz;
        
        // use velocity^2 to calculate total kinetic energy
        totalV2 = totalV2 + velocityx*velocityx + velocityy*velocityy + velocityz*velocityz;

		// define new positions as current positions, current positions as previous positions
        positionPrevVector[a].x = positionVector[a].x;
        positionPrevVector[a].y = positionVector[a].y;
        positionPrevVector[a].z = positionVector[a].z;
        positionVector[a].x = boxObj.pbc(newX,BoxLength);
        positionVector[a].y = boxObj.pbc(newY,BoxLength);
        positionVector[a].z = boxObj.pbc(newZ,BoxLength);
    }
    
	// return total velocity^2
    return totalV2;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movq %rsi, %rbx
movsd %xmm2, (%rsp)
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rdi, %r14
leaq 0x60(%rsp), %rdi
movl $0x90, %edx
xorl %esi, %esi
callq 0x20a0
xorpd %xmm2, %xmm2
movsd 0x40(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x390b
movsd 0x20(%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x48(%rsp)
addsd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x18(%r14), %rax
xorpd %xmm0, %xmm0
movl $0x1, %ebp
xorl %r12d, %r12d
leaq 0xf(%rsp), %r15
movsd %xmm0, 0x58(%rsp)
movq 0x30(%r14), %rcx
movsd (%rax,%r12), %xmm1
movsd 0x8(%rax,%r12), %xmm0
movsd %xmm0, 0x18(%rsp)
addsd %xmm1, %xmm1
subsd (%rcx,%r12), %xmm1
movq (%rbx), %rdx
movsd (%rdx,%r12), %xmm0
movsd 0x48(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x8(%rcx,%r12), %xmm1
movsd %xmm1, 0x38(%rsp)
movsd 0x8(%rdx,%r12), %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x10(%rax,%r12), %xmm1
addsd %xmm1, %xmm1
subsd 0x10(%rcx,%r12), %xmm1
movsd %xmm1, 0x30(%rsp)
movsd 0x10(%rdx,%r12), %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x28(%rsp)
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c3e
movsd %xmm0, 0x50(%rsp)
movsd 0x18(%rsp), %xmm1
addsd %xmm1, %xmm1
subsd 0x38(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
mulsd 0x48(%rsp), %xmm0
addsd %xmm1, %xmm0
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c3e
movsd %xmm0, 0x18(%rsp)
movsd 0x28(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c3e
movsd %xmm0, 0x10(%rsp)
movq 0x30(%r14), %rax
movsd 0x50(%rsp), %xmm0
subsd (%rax,%r12), %xmm0
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c0c
movsd %xmm0, 0x38(%rsp)
movq 0x30(%r14), %rax
movsd 0x18(%rsp), %xmm0
subsd 0x8(%rax,%r12), %xmm0
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c0c
movsd %xmm0, 0x30(%rsp)
movq 0x30(%r14), %rax
movsd 0x10(%rsp), %xmm0
subsd 0x10(%rax,%r12), %xmm0
movq %r15, %rdi
movsd (%rsp), %xmm1
callq 0x4c0c
movsd %xmm0, 0x28(%rsp)
movq 0x18(%r14), %rax
movq 0x30(%r14), %rcx
movups (%rax,%r12), %xmm0
movups %xmm0, (%rcx,%r12)
movsd 0x10(%rax,%r12), %xmm0
movsd %xmm0, 0x10(%rcx,%r12)
movq %r15, %rdi
movsd 0x50(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x4c3e
movq 0x18(%r14), %rax
movsd %xmm0, (%rax,%r12)
movq %r15, %rdi
movsd 0x18(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x4c3e
movq 0x18(%r14), %rax
movsd %xmm0, 0x8(%rax,%r12)
movq %r15, %rdi
movsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x4c3e
movsd 0x20(%rsp), %xmm1
movsd 0x38(%rsp), %xmm3
divsd %xmm1, %xmm3
movsd 0x30(%rsp), %xmm4
divsd %xmm1, %xmm4
movsd 0x28(%rsp), %xmm2
divsd %xmm1, %xmm2
mulsd %xmm3, %xmm3
movsd 0x58(%rsp), %xmm1
addsd %xmm3, %xmm1
mulsd %xmm4, %xmm4
addsd %xmm1, %xmm4
mulsd %xmm2, %xmm2
addsd %xmm4, %xmm2
movq 0x18(%r14), %rax
movsd %xmm0, 0x10(%rax,%r12)
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
addq $0x18, %r12
incl %ebp
movsd 0x40(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm2, %xmm0
ja 0x36de
movapd %xmm2, %xmm0
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x31b2
movq %rbx, %rdi
callq 0x2200