// File: /workspace/github_cpp_makefile_star2_O1/shuwenyue[P]MD_code_cell_list/force.cpp
// Function: force::getForcecellist(double, int, double, double, double, std::vector<vec3<double>, std::allocator<vec3<double>>>)
double force::getForcecellist(double t, 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;

	// build Cell List, determine number of cells long on each side of box (each cell length > cutoff)
	int CellsPerSide = round(BoxLength/cutoff);

	// length of each cell
	double cellLength = BoxLength/CellsPerSide;

	// total number of cells
	int Ncells = CellsPerSide*CellsPerSide*CellsPerSide;
	
	// ParticleCell vector contains the cell number location for each particle
	ParticleCell.resize(NParticles);	
	for (int a=0; a<NParticles; a++)
	{
		// convert position coordinates to cell coordinates
		double cx = floor(coord[a].x/cellLength); 
		double cy = floor(coord[a].y/cellLength);
		double cz = floor(coord[a].z/cellLength);
		ParticleCell[a] = CellsPerSide*CellsPerSide*cz+cy*CellsPerSide+cx; // algorithm to determine cell number location of a particle
	}
	
	// create head vector and linked list and initialize to -1 (NULL)
	HeadList.resize(Ncells);
	LinkedList.resize(NParticles);
	for (int m=0; m<Ncells; m++)
	{
		HeadList[m] = -1;
	}
	for (int m=0; m<NParticles; m++)
	{
		LinkedList[m] = -1;
	}
		
	// create cell coordinate matrix: NParticlesx3 matrix of cell coordinate for each particle
	CellCoord.resize(NParticles);
	int cellCounter = 0;
	for (int z = 0; z < CellsPerSide ; z++)
	{
		for (int y = 0; y < CellsPerSide ; y++)
		{
			for(int x = 0; x < CellsPerSide ; x++)
			{
				CellCoord[cellCounter].x = x;
				CellCoord[cellCounter].y = y;
				CellCoord[cellCounter].z = z;
				cellCounter = cellCounter + 1;
			}
		}
	}

	//loop over particle list and assign values to headlist and linked list
	for (int i = 0; i<NParticles; i++)
	{
		double cellnumber = ParticleCell[i];
		LinkedList[i]=HeadList[cellnumber];
		HeadList[cellnumber] = i;
	}


	// loop over all cells for interactions, where any 1 cell is designated 'center cell' for surrounding interactions
	for (int cell = 0; cell < Ncells; cell++)
	{
		// loop over all 26 neighbors of 'center cell' and 'center cell' itself (27 boxes total)
		for (int cx1 = CellCoord[cell].x-1; cx1 < CellCoord[cell].x+2; cx1++)
		{
			for (int cy1 = CellCoord[cell].y-1; cy1 < CellCoord[cell].y+2; cy1++)
			{
				for (int cz1 = CellCoord[cell].z-1; cz1 < CellCoord[cell].z+2; cz1++)
				{

					// periodic boundary conditions (correct cell locations of neighbors)
					int cx11 = cx1;
					int cy11 = cy1;
					int cz11 = cz1;

					if (cx1 < 0)
					{
						cx11 = CellsPerSide - 1;
					}
					else if (cx1 > (CellsPerSide-1))
					{
						cx11 = 0;
					}
					if (cy1 < 0)
					{
						cy11 = CellsPerSide - 1;
					}
					else if (cy1 > (CellsPerSide-1))
					{
						cy11 = 0;
					}
					if (cz1 < 0)
					{
						cz11 = CellsPerSide - 1;
					}
					else if(cz1 > (CellsPerSide-1))
					{
						cz11 = 0;
					}
					
					// use algorithm to determine cell number of neighbor cells
					int neighborCell = CellsPerSide*CellsPerSide*cz11+cy11*CellsPerSide+cx11;

					// define atom i as HeadList value of 'center cell'
					int i = HeadList[cell];
					
					// scan for atom i in center cell (cell)
					int counter = 0;
					while (i > -1) // while HeadList vector space is null
					{
						counter = counter + 1;

						// scan atom j in cell neighbor (neighborCell)
						int j = HeadList[neighborCell];
						
						while (j > -1) // while LinkedList vector space is null
						{
						
							if (i < j) // avoid double counting of each pair
							{
            					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);	// used for derivation of energy = 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)));
								}
							}
							j = LinkedList[j];
						}
						i = LinkedList[i];
					}
				}
			}
		}
	}
	// returns total energy
	return energy;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, 0xa8(%rsp)
movsd %xmm3, 0x60(%rsp)
movsd %xmm2, 0x48(%rsp)
movsd %xmm1, 0x30(%rsp)
movl %esi, %r14d
movq %rdi, %rbx
movslq %esi, %r15
movq %r15, %rsi
callq 0x3e7c
movl %r14d, %ebp
movq %r15, 0x20(%rsp)
testl %r15d, %r15d
jle 0x4449
movq (%rbx), %rdi
leaq (,%rbp,8), %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x20a0
movl %r14d, 0x8(%rsp)
movsd 0x30(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x2080
movapd %xmm0, 0x10(%rsp)
cvttsd2si %xmm0, %eax
movl %eax, %ecx
imull %eax, %ecx
movl %ecx, 0x3c(%rsp)
movq %rax, 0x50(%rsp)
imull %eax, %ecx
movl %ecx, 0x40(%rsp)
movq %rbx, 0xa0(%rsp)
leaq 0x30(%rbx), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x4b58
testl %ebx, %ebx
jle 0x4574
cvttpd2dq 0x10(%rsp), %xmm0
cvtdq2pd %xmm0, %xmm0
movsd 0x30(%rsp), %xmm1
movapd %xmm0, 0x80(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x3c(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xa8(%rsp), %rax
movq (%rax), %rbx
movq (%r12), %r14
addq $0x10, %rbx
xorl %r15d, %r15d
movsd -0x10(%rbx), %xmm0
movsd -0x8(%rbx), %xmm1
movsd %xmm1, 0x90(%rsp)
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd %xmm0, 0x28(%rsp)
movsd 0x90(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd %xmm0, 0x90(%rsp)
movsd (%rbx), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd 0x90(%rsp), %xmm1
mulsd 0x80(%rsp), %xmm1
mulsd 0x68(%rsp), %xmm0
addsd %xmm1, %xmm0
addsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r14,%r15,4)
incq %r15
addq $0x18, %rbx
cmpq %r15, %rbp
jne 0x44ea
movq %r12, 0x10(%rsp)
movq 0xa0(%rsp), %rbx
leaq 0x48(%rbx), %r15
movl 0x40(%rsp), %r14d
movslq %r14d, %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x4b58
leaq 0x60(%rbx), %r12
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x4b58
movl %r14d, %r14d
testl %r13d, %r13d
jle 0x45c6
movq (%r15), %rdi
leaq (,%r14,4), %rdx
movl $0xff, %esi
callq 0x20a0
movq %r14, 0xb0(%rsp)
movl 0x8(%rsp), %r13d
testl %r13d, %r13d
movq 0x50(%rsp), %r14
jle 0x45f3
movq (%r12), %rdi
leaq (,%rbp,4), %rdx
movl $0xff, %esi
callq 0x20a0
addq $0x78, %rbx
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
callq 0x3e7c
testl %r14d, %r14d
jle 0x467b
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0x9f9(%rip), %xmm1      # 0x5010
xorl %ecx, %ecx
movq (%rbx), %rdx
addq $0x10, %rdx
xorpd %xmm2, %xmm2
xorl %edi, %edi
movq %rcx, %rsi
movslq %esi, %rcx
leaq (%rcx,%rcx,2), %r8
leaq (%rdx,%r8,8), %r9
xorpd %xmm3, %xmm3
xorl %r8d, %r8d
movsd %xmm3, -0x10(%r9)
movsd %xmm2, -0x8(%r9)
movsd %xmm0, (%r9)
addsd %xmm1, %xmm3
addq $0x18, %r9
incq %r8
cmpl %r8d, %r14d
jne 0x463b
addsd %xmm1, %xmm2
incl %edi
addq %r8, %rcx
cmpl %r14d, %edi
jne 0x4626
addsd %xmm1, %xmm0
incl %eax
movslq %esi, %rcx
addq %r8, %rcx
cmpl %r14d, %eax
jne 0x4619
testl %r13d, %r13d
jle 0x46a7
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%r15), %rcx
movq (%r12), %rdx
xorl %esi, %esi
movl (%rax,%rsi,4), %edi
movl (%rcx,%rdi,4), %r8d
movl %r8d, (%rdx,%rsi,4)
movl %esi, (%rcx,%rdi,4)
incq %rsi
cmpq %rsi, %rbp
jne 0x4691
cmpl $0x0, 0x40(%rsp)
jle 0x4b28
movsd 0x48(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x4(%rsp)
movsd 0x60(%rsp), %xmm0
mulsd 0xb98(%rip), %xmm0      # 0x5270
movsd %xmm0, 0x60(%rsp)
xorl %edx, %edx
movsd 0xbb8(%rip), %xmm3      # 0x52a0
leaq 0xf(%rsp), %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r15, 0xd0(%rsp)
movq %rbx, 0xc0(%rsp)
movq (%rbx), %rax
leaq (%rdx,%rdx,2), %rcx
movq %rcx, 0x58(%rsp)
movsd (%rax,%rcx,8), %xmm1
movapd %xmm1, %xmm0
addsd 0xb74(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm2
cvtdq2pd %xmm2, %xmm2
addsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x4b15
cvttsd2si %xmm0, %esi
movq %rdx, 0xc8(%rsp)
leaq (,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movsd 0x8(%rax,%rcx), %xmm0
addsd 0xb38(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm1
movq (%rbx), %rax
movq %rcx, 0xb8(%rsp)
movsd 0x8(%rax,%rcx), %xmm2
addsd %xmm3, %xmm2
ucomisd %xmm1, %xmm2
movl %esi, 0x70(%rsp)
jbe 0x4aed
cvttsd2si %xmm0, %edi
cmpl %r14d, %esi
movl $0x0, %ecx
cmovll %esi, %ecx
testl %esi, %esi
cmovsl 0x4(%rsp), %ecx
movl %ecx, 0x74(%rsp)
movq 0xb8(%rsp), %rcx
movsd 0x10(%rax,%rcx), %xmm0
addsd 0xadd(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm1
cvtdq2pd %xmm1, %xmm1
movq (%rbx), %rax
movsd 0x10(%rax,%rcx), %xmm2
addsd %xmm3, %xmm2
ucomisd %xmm1, %xmm2
movl %edi, 0x78(%rsp)
jbe 0x4abf
cvttsd2si %xmm0, %esi
cmpl %r14d, %edi
movl $0x0, %eax
cmovll %edi, %eax
testl %edi, %edi
cmovsl 0x4(%rsp), %eax
imull %r14d, %eax
addl 0x74(%rsp), %eax
movl %eax, 0x7c(%rsp)
movq (%r15), %rax
movl (%rax,%rdx,4), %r13d
testl %r13d, %r13d
movl %esi, 0x8(%rsp)
js 0x4a86
cmpl 0x50(%rsp), %esi
movl $0x0, %eax
cmovll %esi, %eax
testl %esi, %esi
cmovsl 0x4(%rsp), %eax
imull 0x3c(%rsp), %eax
addl 0x7c(%rsp), %eax
cltq
movq %rax, 0x40(%rsp)
movq (%r15), %rax
movq 0x40(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %r13d, %ecx
testl %eax, %eax
movq %rcx, 0x20(%rsp)
js 0x4a68
leaq (%rcx,%rcx,2), %r14
movl %eax, %r15d
cmpl %eax, %r13d
jge 0x4a58
movq 0xa8(%rsp), %rax
movq (%rax), %rax
leaq (%r15,%r15,2), %rbx
movsd (%rax,%r14,8), %xmm0
movsd 0x8(%rax,%r14,8), %xmm2
subsd (%rax,%rbx,8), %xmm0
movsd 0x10(%rax,%r14,8), %xmm1
subsd 0x10(%rax,%rbx,8), %xmm1
movsd %xmm1, 0x28(%rsp)
subsd 0x8(%rax,%rbx,8), %xmm2
movsd %xmm2, 0x80(%rsp)
movq %rbp, %rdi
movsd 0x30(%rsp), %xmm1
callq 0x4cf4
movapd %xmm0, 0x90(%rsp)
movq %rbp, %rdi
movsd 0x80(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x4cf4
movaps %xmm0, 0x80(%rsp)
movq %rbp, %rdi
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
callq 0x4cf4
movapd 0x80(%rsp), %xmm7
movapd 0x90(%rsp), %xmm5
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movsd 0x68(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x4a50
movsd 0x93f(%rip), %xmm2      # 0x5278
divsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0x6bf(%rip), %xmm1      # 0x5010
divsd %xmm3, %xmm1
movapd %xmm1, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm3
mulsd 0x8c7(%rip), %xmm3      # 0x5230
addsd %xmm6, %xmm3
mulsd %xmm2, %xmm3
movq 0xa0(%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 (%rax,%rbx,8), %xmm0
subpd %xmm5, %xmm0
movupd %xmm0, (%rax,%rbx,8)
movsd 0x10(%rax,%rbx,8), %xmm0
subsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%rbx,8)
subsd %xmm1, %xmm6
mulsd 0x8a4(%rip), %xmm6      # 0x5280
addsd 0x10(%rsp), %xmm6
movsd %xmm6, 0x28(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x892(%rip), %xmm1      # 0x5288
callq 0x21f0
movsd 0x60d(%rip), %xmm1      # 0x5010
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x875(%rip), %xmm1      # 0x5290
callq 0x21f0
movsd 0x870(%rip), %xmm1      # 0x5298
divsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd 0x5e6(%rip), %xmm0      # 0x5020
addsd %xmm1, %xmm0
mulsd 0x60(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x848(%rip), %xmm3      # 0x52a0
movq (%r12), %rax
movl (%rax,%r15,4), %eax
testl %eax, %eax
jns 0x4856
movq (%r12), %rax
movq 0x20(%rsp), %rcx
movl (%rax,%rcx,4), %r13d
testl %r13d, %r13d
movq 0xd0(%rsp), %r15
jns 0x4837
movl 0x8(%rsp), %esi
incl %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movq 0xc0(%rsp), %rbx
movq (%rbx), %rax
movq 0x58(%rsp), %rcx
movsd 0x10(%rax,%rcx,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
movq 0xc8(%rsp), %rdx
ja 0x4800
movl 0x78(%rsp), %edi
incl %edi
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movq (%rbx), %rax
movq 0x58(%rsp), %rcx
movsd 0x8(%rax,%rcx,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
movq 0x50(%rsp), %r14
ja 0x47a5
movl 0x70(%rsp), %esi
incl %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
movq (%rbx), %rax
movq 0x58(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4746
incq %rdx
cmpq 0xb0(%rsp), %rdx
jne 0x4707
jmp 0x4b32
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq