// File: 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
subq $0xf0, %rsp
movq %rsi, -0xb8(%rbp)
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x4a50
movslq -0x4(%rbp), %rax
leaq 0xe007(%rip), %rdi      # 0x262e0
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x12570
testb $0x1, %al
jne 0x182eb
jmp 0x18322
movq 0xdcfe(%rip), %rdi      # 0x25ff0
leaq 0x92a6(%rip), %rsi      # 0x2159f
callq 0x3240
movq %rax, %rdi
movl -0x4(%rbp), %esi
callq 0x3400
movq %rax, %rdi
movq 0xdcad(%rip), %rsi      # 0x25fc0
callq 0x3270
movl $0x1, %edi
callq 0x32e0
movq 0xdcc7(%rip), %rdi      # 0x25ff0
leaq 0x924e(%rip), %rsi      # 0x2157e
callq 0x3240
movq %rax, %rdi
movq 0xdc81(%rip), %rsi      # 0x25fc0
callq 0x3270
movsd 0x8cc4(%rip), %xmm0     # 0x21010
movsd %xmm0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
cmpq $0xc8, -0x20(%rbp)
jge 0x18685
movq -0xb8(%rbp), %rsi
movq $0x0, -0x18(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0xc4(%rbp)
leaq -0x40(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x3120
movl -0xc4(%rbp), %edi
movq -0xc0(%rbp), %rsi
callq 0xd220
jmp 0x183a3
leaq -0x40(%rbp), %rdi
callq 0x3180
movslq -0x4(%rbp), %rax
leaq 0xdf29(%rip), %rdi      # 0x262e0
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x4490
movq %rax, -0x58(%rbp)
movslq -0x4(%rbp), %rax
leaq 0xdf0e(%rip), %rdi      # 0x262e0
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x44c0
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x183f8
jmp 0x184bf
movl $0x0, -0x64(%rbp)
movslq -0x64(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq 0xe2a7(%rip), %rdi      # 0x266b8
callq 0x5b00
movq %rax, %rcx
movq -0xd0(%rbp), %rax
cmpq %rcx, %rax
jae 0x184af
leaq -0x58(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movq -0xb8(%rbp), %rsi
movq 0x10(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
addq $0x18, %rdi
callq 0xd110
testb $0x1, %al
jne 0x18464
jmp 0x1849f
leaq -0x58(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
movq -0x70(%rbp), %rsi
movsd -0x18(%rbp), %xmm0
callq 0x18700
movsd %xmm0, -0x18(%rbp)
jmp 0x1849f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3180
jmp 0x186eb
jmp 0x184a1
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x183ff
jmp 0x184b1
leaq -0x58(%rbp), %rdi
callq 0x4550
jmp 0x183e2
movq -0xb8(%rbp), %rsi
movl -0x4(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0x90(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x3120
movl -0xdc(%rbp), %edi
movq -0xd8(%rbp), %rsi
callq 0xd220
jmp 0x184f6
leaq -0x90(%rbp), %rdi
callq 0x3180
movslq -0x4(%rbp), %rax
leaq 0xddd3(%rip), %rdi      # 0x262e0
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x4490
movq %rax, -0x98(%rbp)
movslq -0x4(%rbp), %rax
leaq 0xddb5(%rip), %rdi      # 0x262e0
imulq $0x18, %rax, %rax
addq %rax, %rdi
callq 0x44c0
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x1855a
jmp 0x18645
movl $0x0, -0xa4(%rbp)
movslq -0xa4(%rbp), %rax
movq %rax, -0xe8(%rbp)
leaq 0xe13f(%rip), %rdi      # 0x266b8
callq 0x5b00
movq %rax, %rcx
movq -0xe8(%rbp), %rax
cmpq %rcx, %rax
jae 0x18632
leaq -0x98(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movq -0xb8(%rbp), %rsi
movq 0x10(%rax), %rax
movslq -0xa4(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rdi
addq $0x18, %rdi
callq 0xd110
testb $0x1, %al
jne 0x185d8
jmp 0x1861c
leaq -0x98(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
movq -0xb0(%rbp), %rsi
movsd -0x18(%rbp), %xmm0
callq 0x193a0
movsd %xmm0, -0x18(%rbp)
jmp 0x1861c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3180
jmp 0x186eb
jmp 0x1861e
movl -0xa4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa4(%rbp)
jmp 0x18564
jmp 0x18634
leaq -0x98(%rbp), %rdi
callq 0x4550
jmp 0x1853e
movq 0xd984(%rip), %rdi      # 0x25fd0
leaq 0x8f21(%rip), %rsi      # 0x21574
callq 0x3240
movq %rax, %rdi
movsd -0x18(%rbp), %xmm0
callq 0x3480
movq %rax, %rdi
movq 0xd951(%rip), %rsi      # 0x25fc0
callq 0x3270
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x18359
movq 0xd944(%rip), %rdi      # 0x25fd0
leaq 0x8ee1(%rip), %rsi      # 0x21574
callq 0x3240
movq %rax, %rdi
movsd -0x18(%rbp), %xmm0
callq 0x3480
movq %rax, %rdi
movq 0xd911(%rip), %rsi      # 0x25fc0
callq 0x3270
movq 0xd915(%rip), %rdi      # 0x25fd0
leaq 0x8ec4(%rip), %rsi      # 0x21586
callq 0x3240
movq %rax, %rdi
movq -0x20(%rbp), %rsi
callq 0x3460
movq %rax, %rdi
movq 0xd8e3(%rip), %rsi      # 0x25fc0
callq 0x3270
addq $0xf0, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x3420
nopw %cs:(%rax,%rax)