// File: /workspace/github_cpp_makefile_star2_O1/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, 0x28(%rsp)
movsd %xmm1, 0x30(%rsp)
movsd %xmm0, 0x10(%rsp)
movl %esi, %r14d
movq %rdi, %r15
movslq %esi, %r12
movq %r12, %rsi
callq 0x3e7c
movq %r14, 0x18(%rsp)
movl %r14d, %r13d
testl %r12d, %r12d
jle 0x4092
movq (%r15), %rdi
leaq (,%r13,8), %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x20a0
movq %r15, 0x68(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0xd0(%rsp)
movapd %xmm0, 0xc0(%rsp)
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
cmpl $0x2, 0x18(%rsp)
movsd 0x10(%rsp), %xmm1
jl 0x4362
movsd 0x30(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x18(%rsp), %rax
decl %eax
movq %rax, 0x18(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x115a(%rip), %xmm0     # 0x5270
movsd %xmm0, 0x28(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movl $0x1, %ebp
movl $0x28, %r15d
xorl %eax, %eax
leaq 0xf(%rsp), %r12
movq %r13, 0x50(%rsp)
leaq (%rax,%rax,2), %r14
incq %rax
movq %rax, 0x58(%rsp)
movq %r15, 0x60(%rsp)
movq (%rbx), %rax
movsd (%rax,%r14,8), %xmm0
movsd 0x8(%rax,%r14,8), %xmm2
movsd %xmm2, 0x40(%rsp)
subsd -0x10(%rax,%r15), %xmm0
movsd -0x8(%rax,%r15), %xmm2
movsd %xmm2, 0x38(%rsp)
movsd 0x10(%rax,%r14,8), %xmm2
subsd (%rax,%r15), %xmm2
movsd %xmm2, 0x78(%rsp)
movq %r12, %rdi
callq 0x4cf4
movapd %xmm0, 0xe0(%rsp)
movsd 0x40(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
movq %r12, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x4cf4
movapd %xmm0, 0x40(%rsp)
movq %r12, %rdi
movsd 0x78(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
callq 0x4cf4
movapd 0x40(%rsp), %xmm7
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd 0xe0(%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 0x432b
movsd 0x1061(%rip), %xmm2     # 0x5278
divsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0xde1(%rip), %xmm1      # 0x5010
divsd %xmm3, %xmm1
movapd %xmm1, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm3
mulsd 0xfe9(%rip), %xmm3      # 0x5230
addsd %xmm6, %xmm3
mulsd %xmm2, %xmm3
movq 0x68(%rsp), %rax
movq (%rax), %rax
movupd (%rax,%r14,8), %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,%r14,8)
mulsd %xmm0, %xmm3
movsd 0x10(%rax,%r14,8), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%r14,8)
movupd -0x10(%rax,%r15), %xmm0
subpd %xmm5, %xmm0
movupd %xmm0, -0x10(%rax,%r15)
movsd (%rax,%r15), %xmm0
subsd %xmm3, %xmm0
movsd %xmm0, (%rax,%r15)
subsd %xmm1, %xmm6
mulsd 0xfc5(%rip), %xmm6      # 0x5280
addsd 0x20(%rsp), %xmm6
movsd %xmm6, 0x40(%rsp)
movsd 0x30(%rsp), %xmm0
movsd 0xfb3(%rip), %xmm1      # 0x5288
callq 0x21f0
movsd 0xd2e(%rip), %xmm1      # 0x5010
divsd %xmm0, %xmm1
mulsd 0xd32(%rip), %xmm1      # 0x5020
movsd %xmm1, 0x38(%rsp)
movsd 0x30(%rsp), %xmm0
movsd 0xf8e(%rip), %xmm1      # 0x5290
callq 0x21f0
movsd 0xf89(%rip), %xmm1      # 0x5298
divsd %xmm0, %xmm1
addsd 0x38(%rsp), %xmm1
mulsd 0x28(%rsp), %xmm1
addsd 0x40(%rsp), %xmm1
movsd %xmm1, 0x20(%rsp)
movsd 0x10(%rsp), %xmm1
addq $0x18, %r15
decq %r13
cmpq %r13, %rbp
jne 0x414e
incq %rbp
movq 0x60(%rsp), %r15
addq $0x18, %r15
movq 0x58(%rsp), %rax
cmpq 0x18(%rsp), %rax
movq 0x50(%rsp), %r13
jne 0x413d
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x437f
movq 0xd8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x439c
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movq 0x98(%rsp), %rdi
testq %rdi, %rdi
je 0x43b9
movq 0xa8(%rsp), %rsi
subq %rdi, %rsi
callq 0x2150
movsd 0x20(%rsp), %xmm0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x3224
movq %rbx, %rdi
callq 0x2200
nop