// File: /workspace/github_cpp_makefile_star2_O2/suhasjains[P]AMR-Solver/src/poisson.cpp
// Function: amrsolver::jacobi_for_field(myOctree::Octree*, myOctree::Field*, double)
double jacobi_for_field(Octree* node, Field* f, double global_res) {

	double res, err;
	double dx, dy, dz;
	double ue, uw, un, us, ut, ub, up;
	double force = 1.0;
	res = 0.0;
	
	Field temp(nx_block+2*pad,ny_block+2*pad,nz_block+2*pad, "temp");
	//temp.set_field(0.0);	
	
	dx = node->get_block_data()->dx;
	dy = node->get_block_data()->dy;
	dz = node->get_block_data()->dz;
	
	
	//jocobi work - add here
	for(int i=pad; i<nx_block+pad; i++) {
		for(int j=pad; j<ny_block+pad; j++) {
			for(int k=pad; k<nz_block+pad; k++) {
					
				ue = f->val[i+1][j][k];		
				uw = f->val[i-1][j][k];		
				un = f->val[i][j+1][k];		
				us = f->val[i][j-1][k];		
				ut = f->val[i][j][k+1];		
				ub = f->val[i][j][k-1];		
				up = f->val[i][j][k];		
				
	
				if(i==pad)		temp.val[i][j][k] += (2.0*uw - up)/pow(dx,2); 	
				else			temp.val[i][j][k] += uw/pow(dx,2);	
				if(i==nx_block+pad-1)	temp.val[i][j][k] += (2.0*ue - up)/pow(dx,2); 	
				else			temp.val[i][j][k] += ue/pow(dx,2);	
				if(j==pad)		temp.val[i][j][k] += (2.0*us - up)/pow(dy,2); 	
				else			temp.val[i][j][k] += us/pow(dy,2);	
				if(j==ny_block+pad-1)	temp.val[i][j][k] += (2.0*un - up)/pow(dy,2); 	
				else			temp.val[i][j][k] += un/pow(dy,2);	
				if(k==pad)		temp.val[i][j][k] += (2.0*ub - up)/pow(dz,2); 	
				else			temp.val[i][j][k] += ub/pow(dz,2);	
				if(k==nz_block+pad-1)	temp.val[i][j][k] += (2.0*ut - up)/pow(dz,2); 	
				else			temp.val[i][j][k] += ut/pow(dz,2);	
	
				temp.val[i][j][k] -= force;
				temp.val[i][j][k] /= 2.0;
				temp.val[i][j][k] /= (1.0/pow(dx,2) + 1.0/pow(dy,2) + 1.0/pow(dz,2));
	
			//	std::cerr << temp.val[i][j][k] << std::endl;
	
				/*magnitude to be considered*/
				err = std::abs(temp.val[i][j][k] - f->val[i][j][k]);
				if(err>res) 	res = err;	
	
	
			}
		}
	}	
	
	//Transferring values from temp to field
	for(int i=pad; i<nx_block+pad; i++) {
		for(int j=pad; j<ny_block+pad; j++) {
			for(int k=pad; k<nz_block+pad; k++) {
	
				f->val[i][j][k] = temp.val[i][j][k];	
	
			}
		}
	}	
	
	
	if(res>global_res)	global_res = res;

	return global_res;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movsd %xmm0, -0x48(%rbp)
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x771f(%rip), %rax      # 0x131d4
movl (%rax), %eax
movq %rax, -0x30(%rbp)
leaq 0x770e(%rip), %rax      # 0x131d0
movl (%rax), %r15d
leaq 0x770c(%rip), %rax      # 0x131d8
movl (%rax), %r12d
leaq 0x7706(%rip), %rax      # 0x131dc
movl (%rax), %r13d
leaq 0x3b0f(%rip), %rsi      # 0xf5ef
leaq -0x88(%rbp), %rdi
leaq -0x31(%rbp), %rdx
callq 0x2d10
leal (%r13,%r15,2), %ecx
leal (%r12,%r15,2), %edx
movq -0x30(%rbp), %rax
leal (%rax,%r15,2), %esi
leaq -0xd8(%rbp), %rdi
leaq -0x88(%rbp), %r8
callq 0x62b4
leaq -0x88(%rbp), %rdi
callq 0x2100
movq %r14, %rdi
callq 0x3db0
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %r14, %rdi
callq 0x3db0
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r14, %rdi
callq 0x3db0
movsd 0x80(%rax), %xmm0
leaq 0x7675(%rip), %rax      # 0x131d0
movslq (%rax), %rax
leaq 0x766f(%rip), %rcx      # 0x131d4
movslq (%rcx), %r11
addq %rax, %r11
leaq 0x7666(%rip), %rcx      # 0x131d8
movslq (%rcx), %rdx
addq %rax, %rdx
movsd -0x30(%rbp), %xmm12
mulsd %xmm12, %xmm12
leal -0x1(%r11), %ecx
movsd -0x40(%rbp), %xmm14
mulsd %xmm14, %xmm14
movq %rdx, -0x60(%rbp)
decl %edx
mulsd %xmm0, %xmm0
movsd 0x3474(%rip), %xmm1     # 0xf018
movapd %xmm1, %xmm2
divsd %xmm12, %xmm2
movapd %xmm1, %xmm3
divsd %xmm14, %xmm3
addsd %xmm2, %xmm3
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
movslq %ecx, %rcx
movq %rcx, -0x68(%rbp)
movslq %edx, %rdi
leaq (,%rax,8), %r8
movq %rax, %rcx
negq %rcx
movq %rcx, -0x50(%rbp)
xorpd %xmm13, %xmm13
movsd 0x341d(%rip), %xmm2     # 0xf008
movsd 0x3595(%rip), %xmm3     # 0xf188
movapd 0x3575(%rip), %xmm4     # 0xf170
movq %rax, %r10
movq %r11, -0x58(%rbp)
cmpq %r11, %r10
jge 0xbdca
leaq 0x75ca(%rip), %rcx      # 0x131dc
movl (%rcx), %ecx
leal (%rcx,%rax), %edx
addl %eax, %ecx
decl %ecx
movslq %edx, %rdx
movq %rdx, -0x30(%rbp)
movslq %ecx, %rcx
addq -0x50(%rbp), %rcx
movq %rcx, -0x40(%rbp)
movq %rax, %r13
cmpq -0x60(%rbp), %r13
jge 0xbdbe
movq -0xc8(%rbp), %r12
xorl %r15d, %r15d
leaq (%rax,%r15), %rcx
cmpq -0x30(%rbp), %rcx
jge 0xbdb6
movq 0x10(%rbx), %rdx
movq -0x8(%rdx,%r10,8), %rsi
movq (%rdx,%r10,8), %rcx
movq (%rsi,%r13,8), %rsi
addq %r8, %rsi
movq (%rcx,%r13,8), %r9
addq %r8, %r9
movsd (%rsi,%r15,8), %xmm9
movsd (%r9,%r15,8), %xmm5
cmpq %rax, %r10
jne 0xbc88
addsd %xmm9, %xmm9
subsd %xmm5, %xmm9
movq 0x8(%rdx,%r10,8), %rdx
movq (%rdx,%r13,8), %rdx
addq %r8, %rdx
movsd (%rdx,%r15,8), %xmm10
cmpq -0x68(%rbp), %r10
jne 0xbcaa
addsd %xmm10, %xmm10
subsd %xmm5, %xmm10
movq 0x8(%rcx,%r13,8), %r14
addq %r8, %r14
movq -0x8(%rcx,%r13,8), %r11
addq %r8, %r11
movsd -0x8(%r9,%r15,8), %xmm7
movq (%r12,%r10,8), %rsi
movq (%rsi,%r13,8), %rdx
leaq (%rdx,%r8), %rcx
movsd (%r14,%r15,8), %xmm8
movsd (%r11,%r15,8), %xmm11
movsd 0x8(%r9,%r15,8), %xmm6
divsd %xmm12, %xmm9
addsd (%rcx,%r15,8), %xmm9
divsd %xmm12, %xmm10
addsd %xmm9, %xmm10
movsd %xmm10, (%rcx,%r15,8)
cmpq %rax, %r13
jne 0xbd24
addsd %xmm11, %xmm11
subsd %xmm5, %xmm11
divsd %xmm14, %xmm11
movq (%rsi,%rax,8), %r11
addq %r8, %r11
addsd (%r11,%r15,8), %xmm11
movsd %xmm11, (%r11,%r15,8)
jmp 0xbd34
divsd %xmm14, %xmm11
addsd %xmm10, %xmm11
movsd %xmm11, (%rcx,%r15,8)
cmpq %rdi, %r13
jne 0xbd47
addsd %xmm8, %xmm8
subsd %xmm5, %xmm8
movq (%rsi,%rdi,8), %rdx
divsd %xmm14, %xmm8
addq %r8, %rdx
addsd (%rdx,%r15,8), %xmm8
movsd %xmm8, (%rdx,%r15,8)
testq %r15, %r15
jne 0xbd68
addsd %xmm7, %xmm7
subsd %xmm5, %xmm7
divsd %xmm0, %xmm7
addsd (%rcx,%r15,8), %xmm7
cmpq %r15, -0x40(%rbp)
jne 0xbd80
addsd %xmm6, %xmm6
subsd %xmm5, %xmm6
divsd %xmm0, %xmm6
addsd %xmm7, %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
divsd %xmm1, %xmm6
movsd %xmm6, (%rcx,%r15,8)
subsd (%r9,%r15,8), %xmm6
andpd %xmm4, %xmm6
maxsd %xmm13, %xmm6
incq %r15
movapd %xmm6, %xmm13
jmp 0xbc44
incq %r13
jmp 0xbc30
incq %r10
movq -0x58(%rbp), %r11
jmp 0xbc02
leaq 0x7407(%rip), %rcx      # 0x131d8
movslq (%rcx), %rcx
addq %rax, %rcx
movq %rax, %rdx
cmpq %r11, %rdx
jge 0xbe32
leaq 0x73f6(%rip), %rsi      # 0x131dc
movslq (%rsi), %rsi
addq %rax, %rsi
movq %rax, %rdi
cmpq %rcx, %rdi
jge 0xbe2d
movq -0xc8(%rbp), %r8
movq %rax, %r9
cmpq %rsi, %r9
jge 0xbe28
movq (%r8,%rdx,8), %r10
movq (%r10,%rdi,8), %r10
movsd (%r10,%r9,8), %xmm0
movq 0x10(%rbx), %r10
movq (%r10,%rdx,8), %r10
movq (%r10,%rdi,8), %r10
movsd %xmm0, (%r10,%r9,8)
incq %r9
jmp 0xbdfe
incq %rdi
jmp 0xbdef
incq %rdx
jmp 0xbdda
maxsd -0x48(%rbp), %xmm13
movsd %xmm13, -0x30(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x65b0
movsd -0x30(%rbp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x2100
jmp 0xbe86
movq %rax, %rbx
jmp 0xbe86
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x65b0
movq %rbx, %rdi
callq 0x2320