// File: /workspace/github_cpp_makefile_star2_O3/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, -0x40(%rbp)
movq %rsi, -0x78(%rbp)
movq %rdi, %rbx
leaq 0x6f11(%rip), %rdx      # 0x151ec
leaq 0x6f06(%rip), %rax      # 0x151e8
movl (%rax), %r15d
leaq 0x6f04(%rip), %rcx      # 0x151f0
leaq 0x6f01(%rip), %rax      # 0x151f4
leaq -0x88(%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 0x32f8(%rip), %rsi      # 0x1160f
leaq 0x32f5(%rip), %rdx      # 0x11613
leaq -0x98(%rbp), %r14
movq %r14, %rdi
callq 0x2e92
leaq -0xe8(%rbp), %rdi
movl %r15d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movq %r14, %r8
callq 0x697c
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0xe367
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2210
movq %rbx, %rdi
callq 0x418c
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq %rbx, %rdi
callq 0x418c
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq %rbx, %rdi
callq 0x418c
leaq 0x6e52(%rip), %rcx      # 0x151ec
movl (%rcx), %ecx
testl %ecx, %ecx
jle 0xe683
leaq 0x6e3d(%rip), %rdx      # 0x151e8
movslq (%rdx), %r8
movsd 0x80(%rax), %xmm0
leaq 0x6e33(%rip), %rax      # 0x151f0
movl (%rax), %esi
leal (%rsi,%r8), %eax
leaq 0x6e2a(%rip), %rdx      # 0x151f4
movl (%rdx), %r10d
leal (%r10,%r8), %edx
movsd -0x30(%rbp), %xmm14
mulsd %xmm14, %xmm14
movl %ecx, %edi
addl %r8d, %edi
addl %r8d, %ecx
decl %ecx
movsd -0x38(%rbp), %xmm15
mulsd %xmm15, %xmm15
movq %rsi, -0x50(%rbp)
leal -0x1(%rsi,%r8), %r9d
mulsd %xmm0, %xmm0
movq %r10, -0x68(%rbp)
leal -0x1(%r10,%r8), %r11d
movsd 0x2c09(%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, -0x60(%rbp)
movslq %edi, %rax
movq %rax, -0x48(%rbp)
movslq %ecx, %rax
movq %rax, -0x38(%rbp)
movslq %r9d, %r10
movslq %r11d, %rax
leaq (,%r8,8), %r14
subq %r8, %rax
movq %rax, -0x70(%rbp)
leaq (%r8,%r8,2), %r13
xorpd %xmm2, %xmm2
movsd 0x2d1f(%rip), %xmm3     # 0x11188
movsd 0x316f(%rip), %xmm4     # 0x115e0
movsd 0x316f(%rip), %xmm5     # 0x115e8
movapd 0x2cef(%rip), %xmm6     # 0x11170
movq %r8, %r12
movq %r13, -0x58(%rbp)
movq %r8, %rcx
cmpl $0x0, -0x50(%rbp)
jle 0xe66d
cmpl $0x0, -0x68(%rbp)
jle 0xe65d
movq -0xd8(%rbp), %rax
movq %rax, -0x30(%rbp)
xorl %r15d, %r15d
leal (%r15,%r13), %edx
testb $0x1, %dl
jne 0xe64a
movq -0x78(%rbp), %rax
movq 0x10(%rax), %r9
movq -0x8(%r9,%r12,8), %rdx
movq (%r9,%r12,8), %rdi
movq (%rdx,%rcx,8), %rbx
addq %r14, %rbx
movq (%rdi,%rcx,8), %rdx
addq %r14, %rdx
movsd (%rbx,%r15,8), %xmm11
movsd (%rdx,%r15,8), %xmm7
cmpq %r8, %r12
jne 0xe4f4
addsd %xmm11, %xmm11
subsd %xmm7, %xmm11
movq 0x8(%r9,%r12,8), %r9
movq (%r9,%rcx,8), %r9
addq %r14, %r9
movsd (%r9,%r15,8), %xmm12
cmpq -0x38(%rbp), %r12
jne 0xe516
addsd %xmm12, %xmm12
subsd %xmm7, %xmm12
movq 0x8(%rdi,%rcx,8), %r11
addq %r14, %r11
movq -0x8(%rdi,%rcx,8), %rax
addq %r14, %rax
movsd -0x8(%rdx,%r15,8), %xmm9
movq -0x30(%rbp), %rdi
movq (%rdi,%r12,8), %r9
movq (%r9,%rcx,8), %rbx
leaq (%rbx,%r14), %rdi
movsd (%r11,%r15,8), %xmm10
movsd (%rax,%r15,8), %xmm13
movsd 0x8(%rdx,%r15,8), %xmm8
divsd %xmm14, %xmm11
addsd (%rdi,%r15,8), %xmm11
divsd %xmm14, %xmm12
addsd %xmm11, %xmm12
movsd %xmm12, (%rdi,%r15,8)
cmpq %r8, %rcx
jne 0xe594
addsd %xmm13, %xmm13
subsd %xmm7, %xmm13
divsd %xmm15, %xmm13
movq (%r9,%r8,8), %rax
addq %r14, %rax
addsd (%rax,%r15,8), %xmm13
movsd %xmm13, (%rax,%r15,8)
jmp 0xe5a4
divsd %xmm15, %xmm13
addsd %xmm12, %xmm13
movsd %xmm13, (%rdi,%r15,8)
cmpq %r10, %rcx
jne 0xe5b7
addsd %xmm10, %xmm10
subsd %xmm7, %xmm10
movq (%r9,%r10,8), %rbx
divsd %xmm15, %xmm10
addq %r14, %rbx
addsd (%rbx,%r15,8), %xmm10
movsd %xmm10, (%rbx,%r15,8)
testq %r15, %r15
jne 0xe5da
addsd %xmm9, %xmm9
subsd %xmm7, %xmm9
divsd %xmm0, %xmm9
addsd (%rdi,%r15,8), %xmm9
cmpq %r15, -0x70(%rbp)
jne 0xe5f5
addsd %xmm8, %xmm8
subsd %xmm7, %xmm8
divsd %xmm0, %xmm8
addsd %xmm9, %xmm8
addsd 0x2fd0(%rip), %xmm8     # 0x115d8
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 0xe64a
movapd %xmm7, %xmm2
leaq (%r8,%r15), %rax
incq %rax
incq %r15
cmpq %rsi, %rax
jl 0xe4ad
incq %rcx
incq %r13
cmpq -0x60(%rbp), %rcx
jl 0xe495
incq %r12
movq -0x58(%rbp), %r13
incq %r13
cmpq -0x48(%rbp), %r12
jl 0xe484
jmp 0xe687
xorpd %xmm2, %xmm2
maxsd -0x40(%rbp), %xmm2
movsd %xmm2, -0x30(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x6caa
movsd -0x30(%rbp), %xmm0
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movq -0x98(%rbp), %rdi
leaq -0x88(%rbp), %rax
cmpq %rax, %rdi
je 0xe6ea
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x2210
jmp 0xe6ea
movq %rax, %r14
leaq -0xe8(%rbp), %rdi
callq 0x6caa
movq %r14, %rdi
callq 0x2350