// File: 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:
subq $0x198, %rsp            # imm = 0x198
movq %rdx, 0x80(%rsp)
movq %rdi, 0x190(%rsp)
movl %esi, 0x18c(%rsp)
movsd %xmm0, 0x180(%rsp)
movsd %xmm1, 0x178(%rsp)
movsd %xmm2, 0x170(%rsp)
movq %rdx, 0x168(%rsp)
movq 0x190(%rsp), %rdi
movq %rdi, 0x88(%rsp)
movslq 0x18c(%rsp), %rsi
callq 0x5ca0
movl $0x0, 0x164(%rsp)
movl 0x164(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jge 0x66e2
movq 0x88(%rsp), %rdi
movslq 0x164(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movslq 0x164(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
xorps %xmm0, %xmm0
movsd %xmm0, 0x8(%rax)
movslq 0x164(%rsp), %rsi
callq 0x3350
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movl 0x164(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x164(%rsp)
jmp 0x6669
xorps %xmm0, %xmm0
movsd %xmm0, 0x158(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x3110
movsd 0x178(%rsp), %xmm1
mulsd 0x178(%rsp), %xmm1
mulsd 0x178(%rsp), %xmm1
mulsd 0x178(%rsp), %xmm1
mulsd 0x178(%rsp), %xmm1
mulsd 0x178(%rsp), %xmm1
movsd 0x28d7(%rip), %xmm0     # 0x9010
divsd %xmm1, %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
mulsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl 0x18c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x6dd6
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
cmpl 0x18c(%rsp), %eax
jge 0x6dbe
movq 0x80(%rsp), %rdi
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x80(%rsp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x60(%rsp), %xmm0
movq 0x80(%rsp), %rdi
movsd (%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x80(%rsp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x68(%rsp), %xmm0
movq 0x80(%rsp), %rdi
movsd 0x8(%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x80(%rsp), %rdi
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x70(%rsp), %xmm0
movsd 0x10(%rax), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
leaq 0xf7(%rsp), %rdi
callq 0x8900
movsd %xmm0, 0x78(%rsp)
jmp 0x68b6
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
leaq 0xf7(%rsp), %rdi
callq 0x8900
movsd %xmm0, 0x58(%rsp)
jmp 0x68ec
movsd 0x58(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
leaq 0xf7(%rsp), %rdi
callq 0x8900
movsd %xmm0, 0x50(%rsp)
jmp 0x6922
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xd8(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm2
movsd 0xd0(%rsp), %xmm0
mulsd 0xd0(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm1
mulsd 0xc0(%rsp), %xmm1
mulsd 0xc0(%rsp), %xmm1
movsd 0x266d(%rip), %xmm0     # 0x9010
divsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
mulsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x288d(%rip), %xmm0     # 0x9260
divsd 0xc0(%rsp), %xmm0
movsd 0x98(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
movsd 0x2862(%rip), %xmm3     # 0x9258
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0xc0(%rsp), %xmm1
movsd 0x178(%rsp), %xmm0
mulsd 0x178(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6da6
movq 0x88(%rsp), %rdi
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd (%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xd8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movsd 0x8(%rsp), %xmm0
movq 0x88(%rsp), %rdi
movsd %xmm0, (%rax)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd 0x8(%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movsd 0x10(%rsp), %xmm0
movq 0x88(%rsp), %rdi
movsd %xmm0, 0x8(%rax)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd 0x10(%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movslq 0xbc(%rsp), %rsi
callq 0x3350
movsd 0x18(%rsp), %xmm0
movq 0x88(%rsp), %rdi
movsd %xmm0, 0x10(%rax)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd (%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xd8(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x20(%rsp), %xmm0
movq 0x88(%rsp), %rdi
movsd %xmm0, (%rax)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd 0x8(%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x28(%rsp), %xmm0
movq 0x88(%rsp), %rdi
movsd %xmm0, 0x8(%rax)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movq 0x88(%rsp), %rdi
movsd 0x10(%rax), %xmm1
movsd 0x90(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movslq 0xb8(%rsp), %rsi
callq 0x3350
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movsd 0x158(%rsp), %xmm1
movsd 0x98(%rsp), %xmm0
subsd 0xa0(%rsp), %xmm0
movsd 0x25d0(%rip), %xmm2     # 0x9280
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x25b2(%rip), %xmm0     # 0x9278
mulsd 0x170(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x178(%rsp), %xmm0
movsd 0x258a(%rip), %xmm1     # 0x9270
callq 0x21e0
movaps %xmm0, %xmm1
movsd 0x231a(%rip), %xmm0     # 0x9010
divsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x178(%rsp), %xmm0
movsd 0x2557(%rip), %xmm1     # 0x9268
callq 0x21e0
movsd 0x38(%rsp), %xmm2
movsd 0x40(%rsp), %xmm1
movaps %xmm0, %xmm4
movsd 0x48(%rsp), %xmm0
movsd 0x22dd(%rip), %xmm3     # 0x9010
divsd %xmm4, %xmm3
movq %xmm3, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm3
xorps %xmm4, %xmm4
mulsd %xmm4, %xmm2
addsd %xmm3, %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x158(%rsp)
jmp 0x6da6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x33d0
jmp 0x6dfe
jmp 0x6da8
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x6796
jmp 0x6dc0
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x676c
movsd 0x158(%rsp), %xmm0
movsd %xmm0, (%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x33d0
movsd (%rsp), %xmm0
addq $0x198, %rsp            # imm = 0x198
retq
movq 0xb0(%rsp), %rdi
callq 0x21f0
nopl (%rax,%rax)