// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movsd %xmm0, -0x58(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, %rbx
leaq 0x7793(%rip), %rdx      # 0x151e4
leaq 0x7788(%rip), %rax      # 0x151e0
movl (%rax), %r15d
leaq 0x7786(%rip), %rcx      # 0x151e8
leaq 0x7783(%rip), %rax      # 0x151ec
leaq -0x98(%rbp), %rsi
movq %rsi, -0x10(%rsi)
addl %r15d, %r15d
movl (%rax), %r12d
addl %r15d, %r12d
movl (%rcx), %r13d
addl %r15d, %r13d
addl (%rdx), %r15d
leaq 0x3b52(%rip), %rsi      # 0x115df
leaq 0x3b4f(%rip), %rdx      # 0x115e3
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
callq 0x2e8e
leaq -0xf8(%rbp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movq %r14, %r8
callq 0x68cc
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xdadd
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x4064
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %rbx, %rdi
callq 0x4064
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x4064
leaq 0x76d0(%rip), %rcx      # 0x151e0
movslq (%rcx), %rsi
leaq 0x76ca(%rip), %rcx      # 0x151e4
movslq (%rcx), %rdx
leaq (%rdx,%rsi), %r9
movq %rdx, -0x50(%rbp)
testq %rdx, %rdx
jle 0xddd8
movsd 0x80(%rax), %xmm0
leaq 0x76ab(%rip), %rax      # 0x151e8
movl (%rax), %edi
leal (%rdi,%rsi), %eax
leaq 0x76a3(%rip), %rcx      # 0x151ec
movl (%rcx), %r8d
leal (%r8,%rsi), %ecx
movsd -0x40(%rbp), %xmm13
mulsd %xmm13, %xmm13
leal -0x1(%r9), %edx
movsd -0x30(%rbp), %xmm14
mulsd %xmm14, %xmm14
movq %rdi, -0x60(%rbp)
leal -0x1(%rdi,%rsi), %edi
mulsd %xmm0, %xmm0
movq %r8, -0x88(%rbp)
leal -0x1(%r8,%rsi), %r8d
movsd 0x348e(%rip), %xmm1     # 0x11018
movapd %xmm1, %xmm2
divsd %xmm13, %xmm2
movapd %xmm1, %xmm3
divsd %xmm14, %xmm3
addsd %xmm2, %xmm3
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
movslq %ecx, %rcx
movq %rcx, -0x40(%rbp)
cltq
movq %rax, -0x80(%rbp)
movslq %edx, %rax
movq %rax, -0x30(%rbp)
movslq %edi, %r11
movslq %r8d, %r14
leaq (,%rsi,8), %rax
movq %rax, -0x70(%rbp)
subq %rsi, %r14
xorpd %xmm5, %xmm5
movsd 0x342b(%rip), %xmm2     # 0x11008
movsd 0x35a3(%rip), %xmm3     # 0x11188
movapd 0x3583(%rip), %xmm4     # 0x11170
movq %rsi, %r12
movq %r9, -0x68(%rbp)
cmpl $0x0, -0x60(%rbp)
jle 0xddbe
movq -0xe8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq %rsi, %rcx
cmpl $0x0, -0x88(%rbp)
jle 0xdda7
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax,%r12,8), %rdi
movq -0x8(%rax,%r12,8), %r8
movq (%rax,%r12,8), %r15
movq -0xe8(%rbp), %rax
movq -0x78(%rbp), %rdx
movq (%rdx,%r12,8), %r9
movq (%rax,%r12,8), %rax
movq (%rax,%rcx,8), %rdx
movq -0x70(%rbp), %r10
addq %r10, %rdx
movq (%rdi,%rcx,8), %rax
addq %r10, %rax
movq (%r8,%rcx,8), %rbx
addq %r10, %rbx
movq 0x8(%r15,%rcx,8), %r13
addq %r10, %r13
movq -0x8(%r15,%rcx,8), %rdi
addq %r10, %rdi
movq (%r9,%rcx,8), %r9
addq %r10, %r9
movq (%r15,%rcx,8), %r15
addq %r10, %r15
xorl %r8d, %r8d
movsd (%rbx,%r8,8), %xmm12
movsd (%r15,%r8,8), %xmm6
cmpq %rsi, %r12
jne 0xdc97
addsd %xmm12, %xmm12
subsd %xmm6, %xmm12
divsd %xmm13, %xmm12
movsd (%rax,%r8,8), %xmm11
movsd (%r13,%r8,8), %xmm9
movsd (%rdi,%r8,8), %xmm10
movsd 0x8(%r15,%r8,8), %xmm7
movsd -0x8(%r15,%r8,8), %xmm8
addsd (%rdx,%r8,8), %xmm12
movsd %xmm12, (%rdx,%r8,8)
cmpq -0x30(%rbp), %r12
jne 0xdcd9
addsd %xmm11, %xmm11
subsd %xmm6, %xmm11
divsd %xmm13, %xmm11
addsd (%rdx,%r8,8), %xmm11
movsd %xmm11, (%rdx,%r8,8)
cmpq %rsi, %rcx
jne 0xdcf9
addsd %xmm10, %xmm10
subsd %xmm6, %xmm10
divsd %xmm14, %xmm10
addsd (%rdx,%r8,8), %xmm10
movsd %xmm10, (%rdx,%r8,8)
cmpq %r11, %rcx
jne 0xdd19
addsd %xmm9, %xmm9
subsd %xmm6, %xmm9
divsd %xmm14, %xmm9
addsd (%rdx,%r8,8), %xmm9
movsd %xmm9, (%rdx,%r8,8)
testq %r8, %r8
jne 0xdd39
addsd %xmm8, %xmm8
subsd %xmm6, %xmm8
divsd %xmm0, %xmm8
addsd (%rdx,%r8,8), %xmm8
movsd %xmm8, (%rdx,%r8,8)
cmpq %r8, %r14
jne 0xdd57
addsd %xmm7, %xmm7
subsd %xmm6, %xmm7
divsd %xmm0, %xmm7
addsd (%rdx,%r8,8), %xmm7
movsd %xmm7, (%rdx,%r8,8)
movsd (%r9,%r8,8), %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
divsd %xmm1, %xmm6
movsd %xmm6, (%r9,%r8,8)
subsd (%r15,%r8,8), %xmm6
andpd %xmm4, %xmm6
maxsd %xmm5, %xmm6
leaq (%rsi,%r8), %r10
incq %r10
incq %r8
movapd %xmm6, %xmm5
cmpq -0x40(%rbp), %r10
jl 0xdc7c
jmp 0xddab
movapd %xmm5, %xmm6
incq %rcx
movapd %xmm6, %xmm5
cmpq -0x80(%rbp), %rcx
jl 0xdc0c
jmp 0xddc2
movapd %xmm5, %xmm6
incq %r12
movapd %xmm6, %xmm5
movq -0x68(%rbp), %r9
cmpq %r9, %r12
jl 0xdbf4
jmp 0xdddc
xorpd %xmm6, %xmm6
cmpl $0x0, -0x50(%rbp)
jle 0xde58
leaq 0x73ff(%rip), %rax      # 0x151e8
movl (%rax), %eax
leal (%rax,%rsi), %edi
leaq 0x73f7(%rip), %rcx      # 0x151ec
movl (%rcx), %ecx
movl %esi, %edx
addl %ecx, %edx
movslq %edx, %rdx
movslq %edi, %rdi
movslq %r9d, %r8
movq %rsi, %r9
testl %eax, %eax
jle 0xde50
movq -0xe8(%rbp), %r10
movq %rsi, %r11
testl %ecx, %ecx
jle 0xde48
movq (%r10,%r9,8), %rbx
movq (%rbx,%r11,8), %rbx
movq -0x48(%rbp), %r14
movq 0x10(%r14), %r14
movq (%r14,%r9,8), %r14
movq (%r14,%r11,8), %r14
movq %rsi, %r15
movsd (%rbx,%r15,8), %xmm0
movsd %xmm0, (%r14,%r15,8)
incq %r15
cmpq %rdx, %r15
jl 0xde34
incq %r11
cmpq %rdi, %r11
jl 0xde15
incq %r9
cmpq %r8, %r9
jl 0xde07
maxsd -0x58(%rbp), %xmm6
movapd %xmm6, -0x40(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x6c20
movaps -0x40(%rbp), %xmm0
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xdeba
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0xdeba
movq %rax, %r14
leaq -0xf8(%rbp), %rdi
callq 0x6c20
movq %r14, %rdi
callq 0x2340