// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movsd %xmm0, -0x48(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdi, %rbx
leaq 0x6f55(%rip), %rcx      # 0x151e4
leaq 0x6f4a(%rip), %rax      # 0x151e0
movl (%rax), %r15d
leaq 0x6f48(%rip), %rax      # 0x151e8
leaq 0x6f45(%rip), %rdx      # 0x151ec
leaq -0x78(%rbp), %rsi
movq %rsi, -0x10(%rsi)
addl %r15d, %r15d
movl (%rdx), %r12d
addl %r15d, %r12d
movl (%rax), %r13d
addl %r15d, %r13d
addl (%rcx), %r15d
leaq 0x3317(%rip), %rsi      # 0x115df
leaq 0x3314(%rip), %rdx      # 0x115e3
leaq -0x88(%rbp), %r14
movq %r14, %rdi
callq 0x2e8e
leaq -0xd8(%rbp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movq %r14, %r8
callq 0x68cc
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xe312
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x4064
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq %rbx, %rdi
callq 0x4064
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x4064
leaq 0x6e9f(%rip), %rcx      # 0x151e4
movl (%rcx), %ecx
testl %ecx, %ecx
jle 0xe65c
leaq 0x6e8a(%rip), %rdx      # 0x151e0
movslq (%rdx), %r8
movsd 0x80(%rax), %xmm0
leaq 0x6e80(%rip), %rax      # 0x151e8
movl (%rax), %esi
leal (%rsi,%r8), %eax
leaq 0x6e77(%rip), %rdx      # 0x151ec
movl (%rdx), %r9d
leal (%r9,%r8), %edx
movsd -0x38(%rbp), %xmm14
mulsd %xmm14, %xmm14
movl %ecx, %edi
addl %r8d, %edi
addl %r8d, %ecx
decl %ecx
movsd -0x30(%rbp), %xmm15
mulsd %xmm15, %xmm15
movq %rsi, -0x58(%rbp)
leal -0x1(%rsi,%r8), %r10d
mulsd %xmm0, %xmm0
movq %r9, -0x30(%rbp)
leal -0x1(%r9,%r8), %r11d
movsd 0x2c5e(%rip), %xmm1     # 0x11018
movapd %xmm1, %xmm2
divsd %xmm14, %xmm2
movapd %xmm1, %xmm3
divsd %xmm15, %xmm3
addsd %xmm2, %xmm3
divsd %xmm0, %xmm1
addsd %xmm3, %xmm1
movslq %edx, %rsi
cltq
movq %rax, -0x68(%rbp)
movslq %edi, %rax
movq %rax, -0x50(%rbp)
movslq %ecx, %rax
movq %rax, -0x38(%rbp)
movslq %r10d, %r10
movslq %r11d, %r11
leaq (,%r8,8), %r14
subq %r8, %r11
leaq (%r8,%r8,2), %r13
xorpd %xmm2, %xmm2
movsd 0x2d78(%rip), %xmm3     # 0x11188
movsd 0x3198(%rip), %xmm4     # 0x115b0
movsd 0x3198(%rip), %xmm5     # 0x115b8
movapd 0x2d48(%rip), %xmm6     # 0x11170
movq %r8, %r12
movq %r13, -0x60(%rbp)
movq %r8, %rcx
cmpl $0x0, -0x58(%rbp)
jle 0xe646
cmpl $0x0, -0x30(%rbp)
jle 0xe636
movq -0xc8(%rbp), %rax
xorl %r15d, %r15d
leal (%r15,%r13), %edx
testb $0x1, %dl
jne 0xe623
movq -0x40(%rbp), %rdx
movq 0x10(%rdx), %rbx
movq -0x8(%rbx,%r12,8), %rdx
movq (%rbx,%r12,8), %rdi
movq (%rdx,%rcx,8), %r9
addq %r14, %r9
movq (%rdi,%rcx,8), %rdx
addq %r14, %rdx
movsd (%r9,%r15,8), %xmm12
movsd (%rdx,%r15,8), %xmm7
cmpq %r8, %r12
jne 0xe497
addsd %xmm12, %xmm12
subsd %xmm7, %xmm12
divsd %xmm14, %xmm12
movq 0x8(%rbx,%r12,8), %r9
movq (%r9,%rcx,8), %r9
addq %r14, %r9
movq 0x8(%rdi,%rcx,8), %rbx
addq %r14, %rbx
movq -0x8(%rdi,%rcx,8), %rdi
addq %r14, %rdi
movsd (%r9,%r15,8), %xmm13
movsd (%rbx,%r15,8), %xmm10
movsd (%rdi,%r15,8), %xmm11
movsd 0x8(%rdx,%r15,8), %xmm8
movsd -0x8(%rdx,%r15,8), %xmm9
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm12
movsd %xmm12, (%rdi,%r15,8)
cmpq -0x38(%rbp), %r12
jne 0xe4ff
addsd %xmm13, %xmm13
subsd %xmm7, %xmm13
divsd %xmm14, %xmm13
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm13
movsd %xmm13, (%rdi,%r15,8)
cmpq %r8, %rcx
jne 0xe52a
addsd %xmm11, %xmm11
subsd %xmm7, %xmm11
divsd %xmm15, %xmm11
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm11
movsd %xmm11, (%rdi,%r15,8)
cmpq %r10, %rcx
jne 0xe555
addsd %xmm10, %xmm10
subsd %xmm7, %xmm10
divsd %xmm15, %xmm10
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm10
movsd %xmm10, (%rdi,%r15,8)
testq %r15, %r15
jne 0xe580
addsd %xmm9, %xmm9
subsd %xmm7, %xmm9
divsd %xmm0, %xmm9
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm9
movsd %xmm9, (%rdi,%r15,8)
cmpq %r15, %r11
jne 0xe5ab
addsd %xmm8, %xmm8
subsd %xmm7, %xmm8
divsd %xmm0, %xmm8
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
addsd (%rdi,%r15,8), %xmm8
movsd %xmm8, (%rdi,%r15,8)
movq (%rax,%r12,8), %rdi
movq (%rdi,%rcx,8), %rdi
addq %r14, %rdi
movsd (%rdi,%r15,8), %xmm8
addsd 0x2fc7(%rip), %xmm8     # 0x115a8
mulsd %xmm3, %xmm8
divsd %xmm1, %xmm8
movsd %xmm8, (%rdi,%r15,8)
movsd (%rdx,%r15,8), %xmm9
mulsd %xmm4, %xmm8
movapd %xmm9, %xmm7
mulsd %xmm5, %xmm7
addsd %xmm8, %xmm7
movsd %xmm7, (%rdx,%r15,8)
subsd %xmm9, %xmm7
andpd %xmm6, %xmm7
ucomisd %xmm2, %xmm7
jbe 0xe623
movapd %xmm7, %xmm2
leaq (%r8,%r15), %rdx
incq %rdx
incq %r15
cmpq %rsi, %rdx
jl 0xe450
incq %rcx
incq %r13
cmpq -0x68(%rbp), %rcx
jl 0xe43c
incq %r12
movq -0x60(%rbp), %r13
incq %r13
cmpq -0x50(%rbp), %r12
jl 0xe42b
jmp 0xe660
xorpd %xmm2, %xmm2
maxsd -0x48(%rbp), %xmm2
movsd %xmm2, -0x40(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x6c20
movsd -0x40(%rbp), %xmm0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0xe6bd
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0xe6bd
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x6c20
movq %rbx, %rdi
callq 0x2340