// File: /workspace/github_cpp_makefile_star2_O2/shuwenyue[P]MD_code_cell_list/force.cpp
// Function: force::getForce(int, double, double, double, std::vector<vec3<double>, std::allocator<vec3<double>>>)
double force::getForce(int NParticles, double BoxLength, double cutoff, double density, vector<double3> coord)
{
    
	// define force vector initialized to 0
    forceVector.resize(NParticles);
    for (int f=0; f < NParticles; f++)
    {
        forceVector[f].x = 0;
        forceVector[f].y = 0;
        forceVector[f].z = 0;
    }

	// define initial energy to 0
	double energy = 0;

	// define objects to use functions in other classes 
	particle particleObj;
	box boxObj;

	// calculations for 'long range interaction' (mean field energy outside of cutoff), to be used in total Energy calculation
    double rcutoff6i = 1/(cutoff*cutoff*cutoff*cutoff*cutoff*cutoff); // 1/(cutoff^6) 
    double rcutoff12i = rcutoff6i*rcutoff6i; // 1/(cutoff^12)

	// define length variables
	double xlength, ylength, zlength, r2;

	// loop through every interaction of particle i and j, without double counting for each pair
	for (int i = 0; i < (NParticles-1); i++)
    {
        for (int j = i+1; j < NParticles; j++)
        {
            xlength = coord[i].x - coord[j].x; // interaction length between particle i and j in the x direction
            ylength = coord[i].y - coord[j].y; // interaction length between particle i and j in the y direction
            zlength = coord[i].z - coord[j].z; // interaction length between particle i and j in the z direction

			// apply periodic boundary conditions on interaction lengths
            xlength = boxObj.boundaries(xlength, BoxLength);
            ylength = boxObj.boundaries(ylength, BoxLength);
            zlength = boxObj.boundaries(zlength, BoxLength);

            // calculate radius^2
            r2 = (xlength*xlength) + (ylength*ylength) + (zlength*zlength);

			// define formulas for LJ force calculation 
			// LJ force formula is force = (48/cutoff^2)*(1/cutoff^12 - 1/cutoff^6) - we can break this down to multiple components
            double r6i = 1/(r2*r2*r2);
            double r12i = r6i*r6i;
            double lj = (48/r2)*(r12i-0.5*r6i);	// lj = force
           
			// if r < cutoff radius, then add explicitly calculated interaction force to cumulative force
			// otherwise, this interaction is outside of cutoff radius and not included in calculation
            if (r2 < (cutoff*cutoff))
            {
				// update force
                forceVector[i].x = forceVector[i].x + lj*xlength;
                forceVector[i].y = forceVector[i].y + lj*ylength;
                forceVector[i].z = forceVector[i].z + lj*zlength;
                
                forceVector[j].x = forceVector[j].x - lj*xlength;
                forceVector[j].y = forceVector[j].y - lj*ylength;
                forceVector[j].z = forceVector[j].z - lj*zlength;
               
				// interactions energy outside of cutoff distance represented by in 'long range correction' (8/3*pi*density...)
				// energy = energy + LJ energy of current interaction long range correction
				energy = energy + 4*(r12i-r6i) - (8/3)*3.14159265*density*((1/3)*(1/pow(cutoff,9.0))-(1/pow(cutoff,6.0)));
				
            }
        }
    }

	// returns total energy 
	return energy;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdx, %rbx
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movslq %esi, %r15
movq %r15, %rsi
callq 0x36a4
movq %r14, 0x68(%rsp)
movq (%r14), %rax
xorl %ecx, %ecx
testl %r15d, %r15d
movl $0x0, %edx
cmovgl %ebp, %edx
imulq $0x18, %rdx, %rdx
xorps %xmm0, %xmm0
cmpq %rcx, %rdx
je 0x38c3
movups %xmm0, (%rax,%rcx)
andq $0x0, 0x10(%rax,%rcx)
addq $0x18, %rcx
jmp 0x38ae
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x18(%rsp), %xmm0
mulsd 0x195c(%rip), %xmm0     # 0x5270
movsd %xmm0, 0x18(%rsp)
cmpl $0x2, %ebp
pushq $0x1
popq %r12
movl %r12d, %eax
cmovgel %ebp, %eax
decl %eax
movq %rax, 0x50(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
pushq $0x28
popq %r14
xorl %eax, %eax
leaq 0xf(%rsp), %r15
movsd 0x10(%rsp), %xmm1
cmpq 0x50(%rsp), %rax
je 0x3b7d
imulq $0x18, %rax, %r13
incq %rax
movq %rax, 0x58(%rsp)
movl %r12d, 0x3c(%rsp)
movq %r14, 0x60(%rsp)
cmpl %ebp, %r12d
jge 0x3b62
movq (%rbx), %rax
movsd (%rax,%r13), %xmm0
movsd 0x8(%rax,%r13), %xmm2
movsd %xmm2, 0x40(%rsp)
subsd -0x10(%rax,%r14), %xmm0
movsd -0x8(%rax,%r14), %xmm2
movsd %xmm2, 0x30(%rsp)
movsd 0x10(%rax,%r13), %xmm2
subsd (%rax,%r14), %xmm2
movsd %xmm2, 0x78(%rsp)
movq %r15, %rdi
callq 0x452c
movapd %xmm0, 0x80(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movq %r15, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x452c
movapd %xmm0, 0x40(%rsp)
movq %r15, %rdi
movsd 0x78(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x452c
movapd 0x40(%rsp), %xmm7
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd 0x80(%rsp), %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movsd 0x70(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x3b50
movsd 0x183c(%rip), %xmm2     # 0x5278
divsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0x15bc(%rip), %xmm1     # 0x5010
divsd %xmm3, %xmm1
movapd %xmm1, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm3
mulsd 0x17c4(%rip), %xmm3     # 0x5230
addsd %xmm6, %xmm3
mulsd %xmm2, %xmm3
movq 0x68(%rsp), %rax
movq (%rax), %rax
movupd (%rax,%r13), %xmm2
movapd %xmm3, %xmm4
unpcklpd %xmm3, %xmm4            # xmm4 = xmm4[0],xmm3[0]
unpcklpd %xmm7, %xmm5            # xmm5 = xmm5[0],xmm7[0]
mulpd %xmm4, %xmm5
addpd %xmm5, %xmm2
movupd %xmm2, (%rax,%r13)
mulsd %xmm0, %xmm3
movsd 0x10(%rax,%r13), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%r13)
movupd -0x10(%rax,%r14), %xmm0
subpd %xmm5, %xmm0
movupd %xmm0, -0x10(%rax,%r14)
movsd (%rax,%r14), %xmm0
subsd %xmm3, %xmm0
movsd %xmm0, (%rax,%r14)
subsd %xmm1, %xmm6
mulsd 0x17a0(%rip), %xmm6     # 0x5280
addsd 0x20(%rsp), %xmm6
movsd %xmm6, 0x40(%rsp)
movsd 0x28(%rsp), %xmm0
movsd 0x178e(%rip), %xmm1     # 0x5288
callq 0x2200
movsd 0x1509(%rip), %xmm1     # 0x5010
divsd %xmm0, %xmm1
mulsd 0x150d(%rip), %xmm1     # 0x5020
movsd %xmm1, 0x30(%rsp)
movsd 0x28(%rsp), %xmm0
movsd 0x1769(%rip), %xmm1     # 0x5290
callq 0x2200
movsd 0x1764(%rip), %xmm1     # 0x5298
divsd %xmm0, %xmm1
addsd 0x30(%rsp), %xmm1
mulsd 0x18(%rsp), %xmm1
addsd 0x40(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x10(%rsp), %xmm1
addq $0x18, %r14
incl %r12d
jmp 0x396a
movq 0x60(%rsp), %r14
addq $0x18, %r14
movl 0x3c(%rsp), %r12d
incl %r12d
movq 0x58(%rsp), %rax
jmp 0x3949
leaq 0x90(%rsp), %rdi
callq 0x2c04
movsd 0x20(%rsp), %xmm0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x2c04
movq %rbx, %rdi
callq 0x2210