// File: /workspace/github_cpp_makefile_star2_O2/suhasjains[P]AMR-Solver/src/poisson.cpp
// Function: amrsolver::gauss_seidel(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void gauss_seidel(int level, std::string name) {

	myOctree::create_lists_of_level_nodes();

	if(myOctree::level_nodes[level].empty()) {
        	std::cerr << "No blocks in level " << level << std::endl;
	 	exit(1);
	}

	std::cerr << "Solving" << std::endl;

	double global_res = 1.0;

	long int loop = 0;

	//iteration loop
	while(loop <200 ) {
	//while(global_res > pow(10,-7)) {

		global_res = 0.0;
		
		//exchange ghost values
		exchange_ghost_val(level, name);

		//loop over all the nodes
		for (std::list<Octree*>::iterator it = level_nodes[level].begin(), end = level_nodes[level].end(); it != end; ++it) {
	
			for(int l = 0; l<myOctree::scalar_fields.size() ; l++) {
	
				Field *f = (*it)->get_block_data()->scalarfields[l];
				if( f->name == name ) {
					
					global_res = gauss_seidel_red(*it, f, global_res);	

				}	
			}
		}

		//exchange ghost values
		exchange_ghost_val(level, name);
		
		//loop over all the nodes
		for (std::list<Octree*>::iterator it = level_nodes[level].begin(), end = level_nodes[level].end(); it != end; ++it) {
	
			for(int l = 0; l<myOctree::scalar_fields.size() ; l++) {
	
				Field *f = (*it)->get_block_data()->scalarfields[l];
				if( f->name == name ) {
					
					global_res = gauss_seidel_black(*it, f, global_res);	

				}	
			}
		}

		std::cout << "Residual " << global_res << std::endl; 

		loop++;

	}	
		std::cout << "Residual " << global_res << std::endl; 
		std::cout << "No of loops " << loop << std::endl;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, %r14
movl %edi, %ebx
callq 0x2e8e
movl %ebx, -0x34(%rbp)
movslq %ebx, %rax
imulq $0x18, %rax, %rax
leaq 0x7386(%rip), %rcx      # 0x13240
leaq (%rcx,%rax), %rdx
movq %rdx, -0x40(%rbp)
cmpq %rdx, (%rcx,%rax)
je 0xc0ae
movq 0x711d(%rip), %rdi      # 0x12ff0
leaq 0x3700(%rip), %rsi      # 0xf5da
callq 0x21d0
movq %rax, %rdi
callq 0x2110
movsd 0x3129(%rip), %xmm0     # 0xf018
movsd %xmm0, -0x30(%rbp)
xorl %eax, %eax
leaq -0x68(%rbp), %rbx
leaq 0x7717(%rip), %r15      # 0x13618
cmpq $0xc8, %rax
je 0xc050
movq %rax, -0x48(%rbp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2270
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x6c98
movq %rbx, %rdi
callq 0x2100
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x40(%rbp), %r12
movq (%r12), %r12
cmpq -0x40(%rbp), %r12
je 0xbf95
xorl %ebx, %ebx
movq 0x8(%r15), %rax
subq (%r15), %rax
sarq $0x5, %rax
cmpq %rbx, %rax
jbe 0xbf3b
movq 0x10(%r12), %rdi
callq 0x3db0
movq 0x10(%rax), %rax
movq (%rax,%rbx,8), %r13
leaq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0x6c56
testb %al, %al
je 0xbf90
movq 0x10(%r12), %rdi
movq %r13, %rsi
movsd -0x30(%rbp), %xmm0
callq 0xc0f1
movsd %xmm0, -0x30(%rbp)
incq %rbx
jmp 0xbf47
leaq -0x88(%rbp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2270
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x6c98
movq %rbx, %rdi
callq 0x2100
movq -0x40(%rbp), %r12
movq (%r12), %r12
cmpq -0x40(%rbp), %r12
je 0xc018
xorl %ebx, %ebx
movq 0x8(%r15), %rax
subq (%r15), %rax
sarq $0x5, %rax
cmpq %rbx, %rax
jbe 0xbfbe
movq 0x10(%r12), %rdi
callq 0x3db0
movq 0x10(%rax), %rax
movq (%rax,%rbx,8), %r13
leaq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0x6c56
testb %al, %al
je 0xc013
movq 0x10(%r12), %rdi
movq %r13, %rsi
movsd -0x30(%rbp), %xmm0
callq 0xc4d4
movsd %xmm0, -0x30(%rbp)
incq %rbx
jmp 0xbfca
movq 0x6fb1(%rip), %rdi      # 0x12fd0
leaq 0x35aa(%rip), %rsi      # 0xf5d0
callq 0x21d0
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x2110
movq -0x48(%rbp), %rax
incq %rax
leaq -0x68(%rbp), %rbx
jmp 0xbf01
movq 0x6f79(%rip), %rbx      # 0x12fd0
leaq 0x3572(%rip), %rsi      # 0xf5d0
movq %rbx, %rdi
callq 0x21d0
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22e0
movq %rax, %rdi
callq 0x2110
leaq 0x3560(%rip), %rsi      # 0xf5e2
movq %rbx, %rdi
callq 0x21d0
movl $0xc8, %esi
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x2110
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x6f3b(%rip), %rdi      # 0x12ff0
leaq 0x353f(%rip), %rsi      # 0xf5fb
callq 0x21d0
movq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x2310
movq %rax, %rdi
callq 0x2110
pushq $0x1
popq %rdi
callq 0x2220
jmp 0xc0de
movq %rbx, %rdi
movq %rax, %rbx
callq 0x2100
movq %rbx, %rdi
callq 0x2320