// File: /workspace/github_cpp_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movq %rdx, 0xa8(%rsp)
movsd %xmm3, 0x80(%rsp)
movsd %xmm2, 0x48(%rsp)
movsd %xmm1, (%rsp)
movq %rdi, %rbx
movslq %esi, %r14
movq %r14, %rsi
callq 0x36a4
movq %rbx, 0x30(%rsp)
movq (%rbx), %rax
xorl %ecx, %ecx
testl %r14d, %r14d
movl $0x0, %ebp
cmovgl %r14d, %ebp
imulq $0x18, %rbp, %rdx
xorps %xmm0, %xmm0
cmpq %rcx, %rdx
je 0x3c27
movups %xmm0, (%rax,%rcx)
andq $0x0, 0x10(%rax,%rcx)
addq $0x18, %rcx
jmp 0x3c12
xorps %xmm0, %xmm0
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movaps %xmm0, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movsd (%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x2080
movapd %xmm0, 0x10(%rsp)
movq 0x30(%rsp), %rax
leaq 0x30(%rax), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4340
movq %r14, 0x70(%rsp)
movsd 0x48(%rsp), %xmm1
movapd 0x10(%rsp), %xmm0
cvttsd2si %xmm0, %r13d
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd %xmm1, %xmm1
movsd %xmm1, 0xd8(%rsp)
movsd (%rsp), %xmm1
movapd %xmm0, 0x50(%rsp)
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movl %r13d, %eax
imull %r13d, %eax
movl %eax, %ecx
movl %eax, 0x98(%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0xb0(%rsp)
imull %r13d, %ecx
movq %rcx, 0x28(%rsp)
movq %rbx, 0x78(%rsp)
movq (%rbx), %rbx
imulq $0x6, %rbp, %r14
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x3d92
movq 0xa8(%rsp), %rax
movq (%rax), %r12
movsd (%r12,%r15,4), %xmm0
movsd 0x8(%r12,%r15,4), %xmm1
movsd %xmm1, 0x38(%rsp)
divsd 0x10(%rsp), %xmm0
callq 0x21d0
movsd %xmm0, 0x88(%rsp)
movsd 0x38(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21d0
movsd %xmm0, 0x38(%rsp)
movsd 0x10(%r12,%r15,4), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x21d0
movsd 0x38(%rsp), %xmm1
mulsd 0x50(%rsp), %xmm1
mulsd 0xb0(%rsp), %xmm0
addsd %xmm1, %xmm0
addsd 0x88(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%rbx)
addq $0x6, %r15
addq $0x4, %rbx
jmp 0x3cf9
movq 0x30(%rsp), %rax
leaq 0x48(%rax), %rdi
movslq 0x28(%rsp), %rsi
movq %rdi, 0x40(%rsp)
callq 0x4340
movq 0x78(%rsp), %rbx
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %rax
leaq 0x60(%rax), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x4340
movq 0x40(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
testl %edx, %edx
cmovlel %ecx, %edx
movq %rdx, 0x28(%rsp)
cmpq %rcx, 0x28(%rsp)
je 0x3df1
orl $-0x1, (%rax,%rcx,4)
incq %rcx
jmp 0x3de1
movq (%r12), %rax
xorl %ecx, %ecx
cmpq %rcx, %rbp
je 0x3e05
orl $-0x1, (%rax,%rcx,4)
incq %rcx
jmp 0x3df7
movq 0x30(%rsp), %rax
leaq 0x78(%rax), %rdi
movq %rdi, 0x68(%rsp)
movq %r14, %rsi
callq 0x36a4
xorl %eax, %eax
testl %r13d, %r13d
movl $0x0, %ecx
cmovgl %r13d, %ecx
movl %r13d, %edx
negl %edx
xorpd %xmm0, %xmm0
xorl %esi, %esi
movsd (%rsp), %xmm1
cmpl %ecx, %esi
je 0x3ea7
xorl %edi, %edi
xorpd %xmm3, %xmm3
cmpl %r13d, %edi
je 0x3e9b
movq 0x68(%rsp), %r8
movq (%r8), %r8
movslq %eax, %r9
imulq $0x18, %r9, %r9
addq %r8, %r9
addq $0x10, %r9
xorpd %xmm2, %xmm2
xorl %r8d, %r8d
cmpl %r8d, %edx
je 0x3e8c
movsd %xmm2, -0x10(%r9)
movsd %xmm3, -0x8(%r9)
movsd %xmm0, (%r9)
addsd 0x118d(%rip), %xmm2     # 0x5010
decl %r8d
addq $0x18, %r9
jmp 0x3e65
subl %r8d, %eax
addsd 0x1179(%rip), %xmm3     # 0x5010
incl %edi
jmp 0x3e43
addsd 0x116d(%rip), %xmm0     # 0x5010
incl %esi
jmp 0x3e39
movq (%rbx), %rax
movq 0x40(%rsp), %rcx
movq (%rcx), %rcx
movq (%r12), %rdx
xorl %esi, %esi
cmpq %rsi, %rbp
je 0x3ed0
movl (%rax,%rsi,4), %edi
movl (%rcx,%rdi,4), %r8d
movl %r8d, (%rdx,%rsi,4)
movl %esi, (%rcx,%rdi,4)
incq %rsi
jmp 0x3eb8
movsd 0x80(%rsp), %xmm0
mulsd 0x138f(%rip), %xmm0     # 0x5270
movsd %xmm0, 0x80(%rsp)
leal -0x1(%r13), %eax
movl %eax, 0x8(%rsp)
xorl %ecx, %ecx
leaq 0xf(%rsp), %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movq %r13, 0xc8(%rsp)
cmpq 0x28(%rsp), %rcx
je 0x42f3
movq 0x68(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %rcx, %rdx
movq %rdx, 0x60(%rsp)
movsd (%rax,%rdx), %xmm0
movapd %xmm0, %xmm2
addsd 0x1360(%rip), %xmm2     # 0x5298
cvttsd2si %xmm2, %edx
leaq (,%rcx,4), %rsi
movq %rsi, 0xd0(%rsp)
movq %rcx, 0xc0(%rsp)
cvtsi2sd %edx, %xmm2
addsd 0x1340(%rip), %xmm0     # 0x52a0
ucomisd %xmm2, %xmm0
jbe 0x42eb
movq 0x60(%rsp), %rcx
movsd 0x8(%rax,%rcx), %xmm0
movapd %xmm0, %xmm2
addsd 0x1317(%rip), %xmm2     # 0x5298
cvttsd2si %xmm2, %esi
cmpl %r13d, %edx
movl $0x0, %edi
cmovll %edx, %edi
testl %edx, %edx
cmovsl 0x8(%rsp), %edi
movl %edi, 0x94(%rsp)
movl %edx, 0xa0(%rsp)
cvtsi2sd %esi, %xmm2
addq %rax, %rcx
addsd 0x12ec(%rip), %xmm0     # 0x52a0
ucomisd %xmm2, %xmm0
jbe 0x42d8
movsd 0x10(%rcx), %xmm0
addsd 0x12cd(%rip), %xmm0     # 0x5298
cvttsd2si %xmm0, %edi
cmpl %r13d, %esi
movl $0x0, %ecx
cmovll %esi, %ecx
testl %esi, %esi
cmovsl 0x8(%rsp), %ecx
imull %r13d, %ecx
addl 0x94(%rsp), %ecx
movl %ecx, 0x9c(%rsp)
movq 0x60(%rsp), %rcx
movl %esi, 0xa4(%rsp)
cvtsi2sd %edi, %xmm0
movsd 0x10(%rax,%rcx), %xmm2
addsd 0x128f(%rip), %xmm2     # 0x52a0
ucomisd %xmm0, %xmm2
jbe 0x42cb
cmpl %r13d, %edi
movl $0x0, %eax
cmovll %edi, %eax
testl %edi, %edi
cmovsl 0x8(%rsp), %eax
imull 0x98(%rsp), %eax
addl 0x9c(%rsp), %eax
movq 0x40(%rsp), %rcx
movq (%rcx), %r13
addq 0xd0(%rsp), %r13
cltq
shlq $0x2, %rax
movq %rax, 0x78(%rsp)
movl %edi, 0x70(%rsp)
movslq (%r13), %r13
testq %r13, %r13
js 0x42af
movq 0x40(%rsp), %rax
movq (%rax), %r15
addq 0x78(%rsp), %r15
imulq $0x18, %r13, %r14
movslq (%r15), %r15
testq %r15, %r15
js 0x4290
cmpl %r15d, %r13d
jge 0x4283
movq 0xa8(%rsp), %rax
movq (%rax), %rax
imulq $0x18, %r15, %rbx
movsd (%rax,%r14), %xmm0
movsd 0x8(%rax,%r14), %xmm2
movsd %xmm2, 0x10(%rsp)
subsd (%rax,%rbx), %xmm0
movsd 0x8(%rax,%rbx), %xmm2
movsd %xmm2, 0x88(%rsp)
movsd 0x10(%rax,%r14), %xmm2
subsd 0x10(%rax,%rbx), %xmm2
movsd %xmm2, 0x38(%rsp)
movq %rbp, %rdi
callq 0x452c
movapd %xmm0, 0xb0(%rsp)
movsd 0x10(%rsp), %xmm0
subsd 0x88(%rsp), %xmm0
movq %rbp, %rdi
movsd (%rsp), %xmm1
callq 0x452c
movapd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movsd 0x38(%rsp), %xmm0
movsd (%rsp), %xmm1
callq 0x452c
movapd 0x10(%rsp), %xmm7
movapd %xmm7, %xmm1
mulsd %xmm7, %xmm1
movapd 0xb0(%rsp), %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
addsd %xmm2, %xmm1
movsd 0xd8(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x427e
movsd 0x1111(%rip), %xmm2     # 0x5278
divsd %xmm1, %xmm2
movapd %xmm1, %xmm3
mulsd %xmm1, %xmm3
mulsd %xmm1, %xmm3
movsd 0xe91(%rip), %xmm1      # 0x5010
divsd %xmm3, %xmm1
movapd %xmm1, %xmm6
mulsd %xmm1, %xmm6
movapd %xmm1, %xmm3
mulsd 0x1099(%rip), %xmm3     # 0x5230
addsd %xmm6, %xmm3
mulsd %xmm2, %xmm3
movq 0x30(%rsp), %rax
movq (%rax), %rax
movupd (%rax,%r14), %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)
mulsd %xmm0, %xmm3
movsd 0x10(%rax,%r14), %xmm0
addsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%r14)
movupd (%rax,%rbx), %xmm0
subpd %xmm5, %xmm0
movupd %xmm0, (%rax,%rbx)
movsd 0x10(%rax,%rbx), %xmm0
subsd %xmm3, %xmm0
movsd %xmm0, 0x10(%rax,%rbx)
subsd %xmm1, %xmm6
mulsd 0x1079(%rip), %xmm6     # 0x5280
addsd 0x50(%rsp), %xmm6
movsd %xmm6, 0x10(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x1067(%rip), %xmm1     # 0x5288
callq 0x2200
movsd 0xde2(%rip), %xmm1      # 0x5010
divsd %xmm0, %xmm1
movsd %xmm1, 0x50(%rsp)
movsd 0x48(%rsp), %xmm0
movsd 0x104a(%rip), %xmm1     # 0x5290
callq 0x2200
movsd 0x1045(%rip), %xmm1     # 0x5298
divsd %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
mulsd 0xdbb(%rip), %xmm0      # 0x5020
addsd %xmm1, %xmm0
mulsd 0x80(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd (%rsp), %xmm1
shlq $0x2, %r15
addq (%r12), %r15
jmp 0x4079
shlq $0x2, %r13
addq (%r12), %r13
movl 0xa0(%rsp), %edx
movl 0xa4(%rsp), %esi
movl 0x70(%rsp), %edi
jmp 0x405b
incl %edi
movq 0x68(%rsp), %rax
movq (%rax), %rax
movq 0xc8(%rsp), %r13
movq 0x60(%rsp), %rcx
jmp 0x3fff
incl %esi
movsd 0x8(%rax,%rcx), %xmm0
jmp 0x3fa5
incl %edx
movsd (%rcx), %xmm0
movq 0xc0(%rsp), %rcx
jmp 0x3f54
incq %rcx
jmp 0x3f0b
leaq 0xe0(%rsp), %rdi
callq 0x2c04
movsd 0x50(%rsp), %xmm0
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x431a
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x2c04
movq %rbx, %rdi
callq 0x2210