// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movsd %xmm0, -0x60(%rbp)
movq %rsi, -0x50(%rbp)
movq %rdi, %rbx
leaq 0x77aa(%rip), %rdx      # 0x151ec
leaq 0x779f(%rip), %rax      # 0x151e8
movl (%rax), %r15d
leaq 0x779d(%rip), %rcx      # 0x151f0
leaq 0x779a(%rip), %rax      # 0x151f4
leaq -0xa8(%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 0x3b91(%rip), %rsi      # 0x1160f
leaq 0x3b8e(%rip), %rdx      # 0x11613
leaq -0xb8(%rbp), %r14
movq %r14, %rdi
callq 0x2e92
leaq -0x108(%rbp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movq %r14, %r8
callq 0x697c
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0xdace
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x418c
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %rbx, %rdi
callq 0x418c
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x418c
leaq 0x76eb(%rip), %rcx      # 0x151ec
movslq (%rcx), %rsi
testq %rsi, %rsi
jle 0xde70
leaq 0x76d4(%rip), %rcx      # 0x151e8
movslq (%rcx), %r12
movq %rsi, -0x58(%rbp)
addq %r12, %rsi
movsd 0x80(%rax), %xmm0
leaq 0x76c3(%rip), %rax      # 0x151f0
movl (%rax), %edi
leal (%rdi,%r12), %eax
leaq 0x76ba(%rip), %rcx      # 0x151f4
movl (%rcx), %r8d
leal (%r8,%r12), %ecx
movsd -0x40(%rbp), %xmm13
mulsd %xmm13, %xmm13
movq %rsi, -0x48(%rbp)
leal -0x1(%rsi), %edx
movsd -0x30(%rbp), %xmm14
mulsd %xmm14, %xmm14
movq %rdi, -0x68(%rbp)
leal -0x1(%rdi,%r12), %esi
mulsd %xmm0, %xmm0
movq %r8, -0x80(%rbp)
leal -0x1(%r8,%r12), %edi
movsd 0x349c(%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, -0x30(%rbp)
cltq
movq %rax, -0x78(%rbp)
movslq %edx, %rax
movq %rax, -0x98(%rbp)
movslq %esi, %r11
movslq %edi, %rax
leaq (,%r12,8), %r15
subq %r12, %rax
movq %rax, -0x90(%rbp)
xorpd %xmm6, %xmm6
movsd 0x3433(%rip), %xmm2     # 0x11008
movsd 0x35ab(%rip), %xmm3     # 0x11188
movapd 0x358b(%rip), %xmm4     # 0x11170
movq %r12, %rsi
cmpl $0x0, -0x68(%rbp)
jle 0xdde3
movq -0xf8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %r12, %rcx
movapd %xmm6, %xmm5
cmpl $0x0, -0x80(%rbp)
jle 0xddce
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax,%rsi,8), %rdx
movq -0x8(%rax,%rsi,8), %rdi
movq (%rax,%rsi,8), %r10
movq -0xf8(%rbp), %rax
movq -0x70(%rbp), %r8
movq (%r8,%rsi,8), %r8
movq (%rax,%rsi,8), %rax
movq %rax, -0x40(%rbp)
movq (%rax,%rcx,8), %rax
movq %rax, -0x88(%rbp)
leaq (%rax,%r15), %rbx
movq (%rdx,%rcx,8), %r13
addq %r15, %r13
movq (%rdi,%rcx,8), %rdi
addq %r15, %rdi
movq 0x8(%r10,%rcx,8), %r9
addq %r15, %r9
movq -0x8(%r10,%rcx,8), %rax
addq %r15, %rax
movq (%r8,%rcx,8), %r8
addq %r15, %r8
movq (%r10,%rcx,8), %r10
addq %r15, %r10
xorl %r14d, %r14d
movsd (%rdi,%r14,8), %xmm10
movsd (%r10,%r14,8), %xmm6
cmpq %r12, %rsi
jne 0xdc94
addsd %xmm10, %xmm10
subsd %xmm6, %xmm10
movsd (%r13,%r14,8), %xmm11
cmpq -0x98(%rbp), %rsi
jne 0xdcae
addsd %xmm11, %xmm11
subsd %xmm6, %xmm11
movsd -0x8(%r10,%r14,8), %xmm7
movsd (%r9,%r14,8), %xmm9
movsd (%rax,%r14,8), %xmm12
movsd 0x8(%r10,%r14,8), %xmm8
divsd %xmm13, %xmm10
addsd (%rbx,%r14,8), %xmm10
divsd %xmm13, %xmm11
addsd %xmm10, %xmm11
movsd %xmm11, (%rbx,%r14,8)
cmpq %r12, %rcx
jne 0xdd10
addsd %xmm12, %xmm12
subsd %xmm6, %xmm12
divsd %xmm14, %xmm12
movq -0x40(%rbp), %rdx
movq (%rdx,%r12,8), %rdx
addq %r15, %rdx
addsd (%rdx,%r14,8), %xmm12
movsd %xmm12, (%rdx,%r14,8)
jmp 0xdd20
divsd %xmm14, %xmm12
addsd %xmm11, %xmm12
movsd %xmm12, (%rbx,%r14,8)
cmpq %r11, %rcx
jne 0xdd39
addsd %xmm9, %xmm9
subsd %xmm6, %xmm9
movq -0x40(%rbp), %rdx
movq (%rdx,%r11,8), %rdx
jmp 0xdd40
movq -0x88(%rbp), %rdx
divsd %xmm14, %xmm9
addq %r15, %rdx
addsd (%rdx,%r14,8), %xmm9
movsd %xmm9, (%rdx,%r14,8)
testq %r14, %r14
jne 0xdd61
addsd %xmm7, %xmm7
subsd %xmm6, %xmm7
divsd %xmm0, %xmm7
addsd (%rbx,%r14,8), %xmm7
cmpq %r14, -0x90(%rbp)
jne 0xdd7e
addsd %xmm8, %xmm8
subsd %xmm6, %xmm8
divsd %xmm0, %xmm8
addsd %xmm7, %xmm8
movsd %xmm8, (%rbx,%r14,8)
movsd (%r8,%r14,8), %xmm6
addsd %xmm2, %xmm6
mulsd %xmm3, %xmm6
divsd %xmm1, %xmm6
movsd %xmm6, (%r8,%r14,8)
subsd (%r10,%r14,8), %xmm6
andpd %xmm4, %xmm6
maxsd %xmm5, %xmm6
leaq (%r12,%r14), %rdx
incq %rdx
incq %r14
movapd %xmm6, %xmm5
cmpq -0x30(%rbp), %rdx
jl 0xdc79
jmp 0xddd2
movapd %xmm5, %xmm6
incq %rcx
movapd %xmm6, %xmm5
cmpq -0x78(%rbp), %rcx
jl 0xdc04
incq %rsi
cmpq -0x48(%rbp), %rsi
jl 0xdbe8
cmpl $0x0, -0x58(%rbp)
jle 0xde74
leaq 0x73f3(%rip), %rax      # 0x151f0
movl (%rax), %eax
leal (%rax,%r12), %esi
leaq 0x73ea(%rip), %rcx      # 0x151f4
movl (%rcx), %ecx
leal (%rcx,%r12), %edx
movslq %edx, %rdx
movslq %esi, %rsi
movslq -0x48(%rbp), %rdi
movq %r12, %r8
testl %eax, %eax
jle 0xde66
movq -0xf8(%rbp), %r9
movq %r12, %r10
testl %ecx, %ecx
jle 0xde5e
movq (%r9,%r8,8), %r11
movq (%r11,%r10,8), %r11
movq -0x50(%rbp), %rbx
movq 0x10(%rbx), %rbx
movq (%rbx,%r8,8), %rbx
movq (%rbx,%r10,8), %rbx
movq %r12, %r14
movsd (%r11,%r14,8), %xmm0
movsd %xmm0, (%rbx,%r14,8)
incq %r14
cmpq %rdx, %r14
jl 0xde4a
incq %r10
cmpq %rsi, %r10
jl 0xde2b
incq %r8
cmpq %rdi, %r8
jl 0xde1d
jmp 0xde74
xorpd %xmm6, %xmm6
maxsd -0x60(%rbp), %xmm6
movapd %xmm6, -0x40(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x6caa
movaps -0x40(%rbp), %xmm0
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0xded6
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x2210
jmp 0xded6
movq %rax, %r14
leaq -0x108(%rbp), %rdi
callq 0x6caa
movq %r14, %rdi
callq 0x2350