// File: /workspace/github_cpp_makefile_star2_O3/suhasjains[P]AMR-Solver/src/adapt.cpp
// Function: myOctree::set_coarsen_flag_based_on_gradient()
void set_coarsen_flag_based_on_gradient() {

        double x_grad, y_grad, z_grad;
        double dx, dy, dz;
        double mg;
        double tg;
        double max_of_max_gradient = 0.0;


        create_list_of_leaf_nodes();

        //calculation of max_total_gradient
        for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {

                //field based on which gradient is to calculated
                Field* f = (*it)->get_block_data()->field;
                dx = (*it)->get_block_data()->dx;
                dy = (*it)->get_block_data()->dy;
                dz = (*it)->get_block_data()->dz;

                mg = 0.0;

                for(int i=pad;i<(f->Nx-pad);i++) {
                        for(int j=pad;j<(f->Ny-pad);j++) {
                                for(int k=pad;k<(f->Nz-pad);k++) {

                                        //central difference gradient calculation 
                                        x_grad = (f->val[i+1][j][k] - f->val[i-1][j][k]) / (2.0*dx);
                                        y_grad = (f->val[i][j+1][k] - f->val[i][j-1][k]) / (2.0*dy);
                                        z_grad = (f->val[i][j][k+1] - f->val[i][j][k-1]) / (2.0*dz);

                                        tg = std::abs(x_grad) + std::abs(y_grad) + std::abs(z_grad);
                                        if(tg>mg)       mg = tg;
                                        //if((*it)->get_block_data()->total_gradient!=0.0)      printf("Yes not zero\n");                              
                                }
                        }
                }

                (*it)->get_block_data()->max_gradient = mg;
                if(mg>max_of_max_gradient)      max_of_max_gradient = mg;
        }

        //setting flags
        for (std::list<Octree*>::iterator it = leaf_nodes.begin(), end = leaf_nodes.end(); it != end; ++it) {

		//printf("mg mmg = %lf %lf\n",(*it)->get_block_data()->max_gradient,max_of_max_gradient);

		
		//Not set to coarsen if it is a root node
                if((*it)->get_block_data()->max_gradient < max_of_max_gradient*0.2 && (!(*it)->isRootNode())) {
                        (*it)->set_to_coarsen_with_nesting();
			//printf("yes coarsen\n");
		}	
        }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
callq 0x2f30
leaq 0x113cf(%rip), %rax     # 0x15230
movq (%rax), %rbx
cmpq %rax, %rbx
je 0x409f
xorpd %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq 0x10(%rbx), %rdi
callq 0x418c
movq 0x8(%rax), %r14
movq 0x10(%rbx), %rdi
callq 0x418c
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq 0x10(%rbx), %rdi
callq 0x418c
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq %rbx, -0x60(%rbp)
movq 0x10(%rbx), %rdi
callq 0x418c
movsd -0x50(%rbp), %xmm6
movsd -0x48(%rbp), %xmm5
leaq 0x11321(%rip), %rcx     # 0x151e8
movslq (%rcx), %rcx
movq %r14, -0x40(%rbp)
movslq (%r14), %rdx
subq %rcx, %rdx
xorpd %xmm7, %xmm7
movq %rdx, -0x68(%rbp)
cmpl %edx, %ecx
movapd 0xd28a(%rip), %xmm4     # 0x11170
jge 0x400d
movsd 0x80(%rax), %xmm0
movq -0x40(%rbp), %rax
movslq 0x4(%rax), %rax
subq %rcx, %rax
addsd %xmm5, %xmm5
addsd %xmm6, %xmm6
addsd %xmm0, %xmm0
leaq (,%rcx,8), %rsi
leaq (%rcx,%rcx), %rdx
negq %rdx
movq %rdx, -0x48(%rbp)
xorpd %xmm1, %xmm1
movq %rcx, %r8
cmpl %eax, %ecx
jge 0x3ff8
movq -0x40(%rbp), %rdx
movslq 0x8(%rdx), %r9
movl %r9d, %r10d
subl %ecx, %r10d
addq -0x48(%rbp), %r9
movq %rcx, %r11
cmpl %r10d, %ecx
jge 0x3fe2
movq -0x40(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq 0x8(%rdx,%r8,8), %rdi
movq -0x8(%rdx,%r8,8), %rbx
movq (%rdx,%r8,8), %rdx
movq (%rdx,%r11,8), %r12
movq (%rdi,%r11,8), %r13
addq %rsi, %r13
movq (%rbx,%r11,8), %r15
addq %rsi, %r15
movq 0x8(%rdx,%r11,8), %rbx
addq %rsi, %rbx
movq -0x8(%rdx,%r11,8), %r14
addq %rsi, %r14
leaq (%r12,%rsi), %rdi
addq $0x8, %rdi
xorl %edx, %edx
movsd (%r13,%rdx,8), %xmm2
subsd (%r15,%rdx,8), %xmm2
divsd %xmm5, %xmm2
movsd (%rbx,%rdx,8), %xmm3
subsd (%r14,%rdx,8), %xmm3
divsd %xmm6, %xmm3
movsd (%rdi,%rdx,8), %xmm7
subsd -0x10(%rdi,%rdx,8), %xmm7
divsd %xmm0, %xmm7
andpd %xmm4, %xmm2
andpd %xmm4, %xmm3
addsd %xmm2, %xmm3
andpd %xmm4, %xmm7
addsd %xmm3, %xmm7
maxsd %xmm1, %xmm7
incq %rdx
movapd %xmm7, %xmm1
cmpq %rdx, %r9
jne 0x3f8d
jmp 0x3fe6
movapd %xmm1, %xmm7
incq %r11
movapd %xmm7, %xmm1
cmpq %rax, %r11
jne 0x3f42
jmp 0x3ffc
movapd %xmm1, %xmm7
incq %r8
movapd %xmm7, %xmm1
cmpq -0x68(%rbp), %r8
jne 0x3f25
movapd %xmm7, -0x40(%rbp)
movq -0x60(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0x418c
movapd -0x40(%rbp), %xmm1
movsd %xmm1, 0x20(%rax)
maxsd -0x58(%rbp), %xmm1
movq (%rbx), %rbx
movapd %xmm1, %xmm0
cmpq 0x10f74(%rip), %rbx     # 0x14fb0
jne 0x3e71
movapd %xmm1, -0x40(%rbp)
leaq 0x111e2(%rip), %rax     # 0x15230
movq (%rax), %rbx
cmpq %rax, %rbx
je 0x409f
movapd -0x40(%rbp), %xmm0
mulsd 0xd11d(%rip), %xmm0     # 0x11180
movapd %xmm0, -0x40(%rbp)
movq 0x10(%rbx), %rdi
callq 0x418c
movapd -0x40(%rbp), %xmm0
ucomisd 0x20(%rax), %xmm0
jbe 0x4093
movq 0x10(%rbx), %rdi
callq 0x417a
testb %al, %al
jne 0x4093
movq 0x10(%rbx), %rdi
callq 0x43aa
movq (%rbx), %rbx
cmpq 0x10f13(%rip), %rbx     # 0x14fb0
jne 0x4068
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq