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

	double res, err;
	double dx, dy, dz;
	double ue, uw, un, us, ut, ub, up;
	double prev_val, curr_val;
	double force = 100.0;
	double relax = 1.4;
	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++) {
			

				if((i+j+k)%2==0) {

					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));
	
					prev_val = f->val[i][j][k];

					f->val[i][j][k] = (1.0 - relax) * f->val[i][j][k] + relax * temp.val[i][j][k];
					
					curr_val = f->val[i][j][k];
	
					/*magnitude to be considered*/
					err = std::abs(curr_val - prev_val);
					if(err>res) 	res = err;	
				
				}	
			}
		}
	}	
	
	
	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 $0xc8, %rsp
movsd %xmm0, -0x48(%rbp)
movq %rsi, -0x78(%rbp)
movq %rdi, %r14
leaq 0x70bc(%rip), %rax      # 0x131d4
movl (%rax), %ebx
leaq 0x70af(%rip), %rax      # 0x131d0
movl (%rax), %r15d
leaq 0x70ad(%rip), %rax      # 0x131d8
movl (%rax), %r12d
leaq 0x70a7(%rip), %rax      # 0x131dc
movl (%rax), %r13d
leaq 0x34b0(%rip), %rsi      # 0xf5ef
leaq -0x98(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x2d10
leal (%r13,%r15,2), %ecx
leal (%r12,%r15,2), %edx
leal (%rbx,%r15,2), %esi
leaq -0xe8(%rbp), %rdi
leaq -0x98(%rbp), %r8
callq 0x62b4
leaq -0x98(%rbp), %rdi
callq 0x2100
movq %r14, %rdi
callq 0x3db0
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq %r14, %rdi
callq 0x3db0
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %r14, %rdi
callq 0x3db0
movsd 0x80(%rax), %xmm0
leaq 0x701a(%rip), %rax      # 0x131d0
movslq (%rax), %r9
leaq 0x7014(%rip), %rax      # 0x131d4
movslq (%rax), %rax
addq %r9, %rax
leaq 0x700b(%rip), %rcx      # 0x131d8
movslq (%rcx), %rcx
addq %r9, %rcx
movsd -0x38(%rbp), %xmm14
mulsd %xmm14, %xmm14
movq %rax, -0x58(%rbp)
decl %eax
movsd -0x40(%rbp), %xmm15
mulsd %xmm15, %xmm15
movq %rcx, -0x68(%rbp)
leal -0x1(%rcx), %ecx
mulsd %xmm0, %xmm0
movsd 0x2e16(%rip), %xmm1     # 0xf018
movapd %xmm1, %xmm2
divsd %xmm14, %xmm2
movapd %xmm1, %xmm3
divsd %xmm15, %xmm3
addsd %xmm2, %xmm3
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
cltq
movq %rax, -0x70(%rbp)
movslq %ecx, %rdi
leaq (,%r9,8), %r8
movq %r9, %rax
negq %rax
movq %rax, -0x50(%rbp)
leaq (%r9,%r9,2), %r13
xorpd %xmm2, %xmm2
movsd 0x2f3d(%rip), %xmm3     # 0xf188
movsd 0x336d(%rip), %xmm4     # 0xf5c0
movsd 0x336d(%rip), %xmm5     # 0xf5c8
movapd 0x2f0d(%rip), %xmm6     # 0xf170
movq %r9, %r11
cmpq -0x58(%rbp), %r11
jge 0xc47a
leaq 0x6f65(%rip), %rax      # 0x131dc
movl (%rax), %eax
leal (%rax,%r9), %ecx
addl %r9d, %eax
decl %eax
movslq %ecx, %r14
cltq
addq -0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq %r13, -0x60(%rbp)
movq %r9, %rax
cmpq -0x68(%rbp), %rax
jge 0xc46b
movq -0xd8(%rbp), %rcx
movq %rcx, -0x38(%rbp)
xorl %r15d, %r15d
leaq (%r9,%r15), %rdx
cmpq %r14, %rdx
jge 0xc460
leal (%r15,%r13), %edx
testb $0x1, %dl
jne 0xc458
movq -0x78(%rbp), %rcx
movq 0x10(%rcx), %rsi
movq -0x8(%rsi,%r11,8), %r10
movq (%rsi,%r11,8), %rdx
movq (%r10,%rax,8), %rbx
addq %r8, %rbx
movq (%rdx,%rax,8), %r10
addq %r8, %r10
movsd (%rbx,%r15,8), %xmm11
movsd (%r10,%r15,8), %xmm7
cmpq %r9, %r11
jne 0xc302
addsd %xmm11, %xmm11
subsd %xmm7, %xmm11
movq 0x8(%rsi,%r11,8), %rsi
movq (%rsi,%rax,8), %rsi
addq %r8, %rsi
movsd (%rsi,%r15,8), %xmm12
cmpq -0x70(%rbp), %r11
jne 0xc324
addsd %xmm12, %xmm12
subsd %xmm7, %xmm12
movq 0x8(%rdx,%rax,8), %r12
addq %r8, %r12
movq -0x8(%rdx,%rax,8), %rcx
addq %r8, %rcx
movsd -0x8(%r10,%r15,8), %xmm9
movq -0x38(%rbp), %rdx
movq (%rdx,%r11,8), %rsi
movq (%rsi,%rax,8), %rbx
leaq (%rbx,%r8), %rdx
movsd (%r12,%r15,8), %xmm10
movsd (%rcx,%r15,8), %xmm13
movsd 0x8(%r10,%r15,8), %xmm8
divsd %xmm14, %xmm11
addsd (%rdx,%r15,8), %xmm11
divsd %xmm14, %xmm12
addsd %xmm11, %xmm12
movsd %xmm12, (%rdx,%r15,8)
cmpq %r9, %rax
jne 0xc3a2
addsd %xmm13, %xmm13
subsd %xmm7, %xmm13
divsd %xmm15, %xmm13
movq (%rsi,%r9,8), %rcx
addq %r8, %rcx
addsd (%rcx,%r15,8), %xmm13
movsd %xmm13, (%rcx,%r15,8)
jmp 0xc3b2
divsd %xmm15, %xmm13
addsd %xmm12, %xmm13
movsd %xmm13, (%rdx,%r15,8)
cmpq %rdi, %rax
jne 0xc3c5
addsd %xmm10, %xmm10
subsd %xmm7, %xmm10
movq (%rsi,%rdi,8), %rbx
divsd %xmm15, %xmm10
addq %r8, %rbx
addsd (%rbx,%r15,8), %xmm10
movsd %xmm10, (%rbx,%r15,8)
testq %r15, %r15
jne 0xc3e8
addsd %xmm9, %xmm9
subsd %xmm7, %xmm9
divsd %xmm0, %xmm9
addsd (%rdx,%r15,8), %xmm9
cmpq %r15, -0x40(%rbp)
jne 0xc403
addsd %xmm8, %xmm8
subsd %xmm7, %xmm8
divsd %xmm0, %xmm8
addsd %xmm9, %xmm8
addsd 0x31a2(%rip), %xmm8     # 0xf5b8
mulsd %xmm3, %xmm8
divsd %xmm1, %xmm8
movsd %xmm8, (%rdx,%r15,8)
movsd (%r10,%r15,8), %xmm9
mulsd %xmm4, %xmm8
movapd %xmm9, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm8, %xmm7
movsd %xmm7, (%r10,%r15,8)
subsd %xmm9, %xmm7
andpd %xmm6, %xmm7
ucomisd %xmm2, %xmm7
jbe 0xc458
movapd %xmm7, %xmm2
incq %r15
jmp 0xc2ae
incq %rax
incq %r13
jmp 0xc296
incq %r11
movq -0x60(%rbp), %r13
incq %r13
jmp 0xc266
maxsd -0x48(%rbp), %xmm2
movsd %xmm2, -0x38(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x65b0
movsd -0x38(%rbp), %xmm0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x2100
jmp 0xc4cc
movq %rax, %rbx
jmp 0xc4cc
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x65b0
movq %rbx, %rdi
callq 0x2320