// File: /workspace/github_cpp_makefile_star2_O3/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, 0xb8(%rsp)
movsd %xmm3, 0x78(%rsp)
movsd %xmm2, 0x58(%rsp)
movsd %xmm1, 0x38(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
movslq %esi, %r14
movq %r14, %rsi
callq 0x3dcc
movl %ebp, %r13d
movq %r14, 0x28(%rsp)
testl %r14d, %r14d
jle 0x434a
movq (%rbx), %rdi
leaq (,%r13,8), %rax
leaq (%rax,%rax,2), %rdx
xorl %esi, %esi
callq 0x20a0
movl %ebp, 0x4(%rsp)
movsd 0x38(%rsp), %xmm0
divsd 0x58(%rsp), %xmm0
callq 0x2080
movapd %xmm0, 0x10(%rsp)
cvttsd2si %xmm0, %eax
movl %eax, %ecx
imull %eax, %ecx
movl %ecx, 0x44(%rsp)
movq %rax, 0x48(%rsp)
imull %eax, %ecx
movl %ecx, 0x50(%rsp)
movq %rbx, 0xb0(%rsp)
leaq 0x30(%rbx), %r12
movq %r12, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x4a6a
testl %ebx, %ebx
jle 0x4474
cvttpd2dq 0x10(%rsp), %xmm0
cvtdq2pd %xmm0, %xmm0
movsd 0x38(%rsp), %xmm1
movapd %xmm0, 0x90(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdl 0x44(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xb8(%rsp), %rax
movq (%rax), %rbx
movq (%r12), %r14
addq $0x10, %rbx
xorl %r15d, %r15d
movsd -0x10(%rbx), %xmm0
movsd -0x8(%rbx), %xmm1
movsd %xmm1, 0xa0(%rsp)
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd %xmm0, 0x30(%rsp)
movsd 0xa0(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd %xmm0, 0xa0(%rsp)
movsd (%rbx), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21c0
movsd 0xa0(%rsp), %xmm1
mulsd 0x90(%rsp), %xmm1
mulsd 0x60(%rsp), %xmm0
addsd %xmm1, %xmm0
addsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r14,%r15,4)
incq %r15
addq $0x18, %rbx
cmpq %r15, %r13
jne 0x43ea
movq %r12, 0x10(%rsp)
movq 0xb0(%rsp), %rbx
leaq 0x48(%rbx), %r14
movl 0x50(%rsp), %r15d
movslq %r15d, %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4a6a
leaq 0x60(%rbx), %r12
movq %r12, %rdi
movq 0x28(%rsp), %rsi
callq 0x4a6a
movl %r15d, %r15d
testl %ebp, %ebp
jle 0x44c5
movq (%r14), %rdi
leaq (,%r15,4), %rdx
movl $0xff, %esi
callq 0x20a0
movq %r14, 0x70(%rsp)
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
movq 0x48(%rsp), %r14
jle 0x44ed
movq (%r12), %rdi
leaq (,%r13,4), %rdx
movl $0xff, %esi
callq 0x20a0
leaq 0x78(%rbx), %rdi
movq %rdi, 0x68(%rsp)
movq 0x28(%rsp), %rsi
callq 0x3dcc
testl %r14d, %r14d
jle 0x457c
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd 0xafd(%rip), %xmm1      # 0x5010
xorl %ecx, %ecx
movq 0x68(%rsp), %rdx
movq (%rdx), %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 0x453c
addsd %xmm1, %xmm2
incl %edi
addq %r8, %rcx
cmpl %r14d, %edi
jne 0x4527
addsd %xmm1, %xmm0
incl %eax
movslq %esi, %rcx
addq %r8, %rcx
cmpl %r14d, %eax
jne 0x4515
testl %ebp, %ebp
jle 0x45ac
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %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, %r13
jne 0x4596
cmpl $0x0, 0x50(%rsp)
jle 0x4a39
movsd 0x58(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
leal -0x1(%r14), %eax
movl %eax, 0x8(%rsp)
movsd 0x78(%rsp), %xmm0
mulsd 0xc93(%rip), %xmm0      # 0x5270
movsd %xmm0, 0x78(%rsp)
movq 0x68(%rsp), %rax
movq (%rax), %rax
xorl %esi, %esi
movsd 0xcab(%rip), %xmm3      # 0x52a0
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x70(%rsp), %rdx
movq %r15, 0xc0(%rsp)
leaq (%rsi,%rsi,2), %rdi
movsd (%rax,%rdi,8), %xmm1
movapd %xmm1, %xmm0
addsd 0xc77(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm2
cvtdq2pd %xmm2, %xmm2
addsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x4a23
cvttsd2si %xmm0, %ecx
movq %rsi, 0xc8(%rsp)
movq %rdi, 0xd0(%rsp)
movsd 0x8(%rax,%rdi,8), %xmm1
movapd %xmm1, %xmm0
addsd 0xc3b(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm2
cvtdq2pd %xmm2, %xmm2
addsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
movl %ecx, 0x80(%rsp)
jbe 0x4a00
cvttsd2si %xmm0, %r8d
cmpl 0x48(%rsp), %ecx
movl $0x0, %r9d
cmovll %ecx, %r9d
testl %ecx, %ecx
cmovsl 0x8(%rsp), %r9d
movl %r9d, 0x84(%rsp)
movsd 0x10(%rax,%rdi,8), %xmm1
movapd %xmm1, %xmm0
addsd 0xbe9(%rip), %xmm0      # 0x5298
cvttpd2dq %xmm0, %xmm2
cvtdq2pd %xmm2, %xmm2
addsd %xmm3, %xmm1
ucomisd %xmm2, %xmm1
movl %r8d, 0x88(%rsp)
jbe 0x49d9
cvttsd2si %xmm0, %ecx
movl %ecx, 0x4(%rsp)
movq 0x48(%rsp), %rcx
cmpl %ecx, %r8d
movl $0x0, %r9d
cmovll %r8d, %r9d
testl %r8d, %r8d
cmovsl 0x8(%rsp), %r9d
imull %ecx, %r9d
addl 0x84(%rsp), %r9d
movl %r9d, 0x8c(%rsp)
movq (%rdx), %rcx
movl (%rcx,%rsi,4), %r15d
testl %r15d, %r15d
js 0x49ac
movl 0x4(%rsp), %ecx
cmpl 0x48(%rsp), %ecx
movl $0x0, %eax
cmovll %ecx, %eax
testl %ecx, %ecx
cmovsl 0x8(%rsp), %eax
imull 0x44(%rsp), %eax
addl 0x8c(%rsp), %eax
cltq
movq %rax, 0x50(%rsp)
movq (%rdx), %rax
movq 0x50(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl %r15d, %ecx
movq %rcx, 0x60(%rsp)
testl %eax, %eax
js 0x4986
movl %eax, %r14d
cmpl %eax, %r15d
jge 0x496f
movq %r12, %rbp
movq 0xb8(%rsp), %rax
movq (%rax), %rax
movq 0x60(%rsp), %rcx
leaq (%rcx,%rcx,2), %r12
leaq (%r14,%r14,2), %rbx
movsd (%rax,%r12,8), %xmm0
movsd 0x8(%rax,%r12,8), %xmm2
subsd (%rax,%rbx,8), %xmm0
movsd 0x10(%rax,%r12,8), %xmm1
subsd 0x10(%rax,%rbx,8), %xmm1
movsd %xmm1, 0x30(%rsp)
subsd 0x8(%rax,%rbx,8), %xmm2
movsd %xmm2, 0x90(%rsp)
leaq 0xf(%rsp), %r13
movq %r13, %rdi
movsd 0x38(%rsp), %xmm1
callq 0x4c0c
movapd %xmm0, 0xa0(%rsp)
movq %r13, %rdi
movsd 0x90(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
callq 0x4c0c
movaps %xmm0, 0x90(%rsp)
movq %r13, %rdi
movsd 0x30(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
callq 0x4c0c
movapd 0x90(%rsp), %xmm7
movapd 0xa0(%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 0x28(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x4964
movsd 0xa2b(%rip), %xmm2      # 0x5278
divsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0x7ab(%rip), %xmm1      # 0x5010
divsd %xmm3, %xmm1
movapd %xmm1, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm3
mulsd 0x9b3(%rip), %xmm3      # 0x5230
addsd %xmm6, %xmm3
mulsd %xmm2, %xmm3
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movupd (%rax,%r12,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,%r12,8)
mulsd %xmm0, %xmm3
movsd 0x10(%rax,%r12,8), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%r12,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 0x990(%rip), %xmm6      # 0x5280
addsd 0x10(%rsp), %xmm6
movsd %xmm6, 0x30(%rsp)
movsd 0x58(%rsp), %xmm0
movsd 0x97e(%rip), %xmm1      # 0x5288
callq 0x21f0
movsd 0x6f9(%rip), %xmm1      # 0x5010
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movsd 0x58(%rsp), %xmm0
movsd 0x961(%rip), %xmm1      # 0x5290
callq 0x21f0
movsd 0x95c(%rip), %xmm1      # 0x5298
divsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd 0x6d2(%rip), %xmm0      # 0x5020
addsd %xmm1, %xmm0
mulsd 0x78(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %rbp, %r12
movsd 0x931(%rip), %xmm3      # 0x52a0
movq (%r12), %rcx
movl (%rcx,%r14,4), %eax
testl %eax, %eax
jns 0x4759
movq 0x70(%rsp), %rdx
jmp 0x498a
movq (%r12), %rcx
movq 0x60(%rsp), %rax
movl (%rcx,%rax,4), %r15d
testl %r15d, %r15d
jns 0x473e
movq 0x68(%rsp), %rax
movq (%rax), %rax
movq 0xc8(%rsp), %rsi
movl 0x4(%rsp), %ecx
incl %ecx
movl %ecx, 0x4(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movq 0xd0(%rsp), %rdi
movsd 0x10(%rax,%rdi,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
ja 0x4704
movl 0x88(%rsp), %r8d
incl %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8d, %xmm0
movsd 0x8(%rax,%rdi,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
ja 0x469d
movl 0x80(%rsp), %ecx
incl %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd (%rax,%rdi,8), %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
ja 0x464b
incq %rsi
movq 0xc0(%rsp), %r15
cmpq %r15, %rsi
jne 0x460c
jmp 0x4a43
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
nop