// File: /workspace/github_cpp_makefile_star2_O1/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 0x2f28
leaq 0x114af(%rip), %r14     # 0x15220
movq (%r14), %rbx
xorpd %xmm0, %xmm0
cmpq %r14, %rbx
je 0x3f64
movsd %xmm0, -0x58(%rbp)
movq 0x10(%rbx), %rdi
callq 0x4064
movq 0x8(%rax), %r14
movq 0x10(%rbx), %rdi
callq 0x4064
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x48(%rbp)
movq 0x10(%rbx), %rdi
callq 0x4064
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq %rbx, -0x60(%rbp)
movq 0x10(%rbx), %rdi
callq 0x4064
movsd -0x50(%rbp), %xmm6
movsd -0x48(%rbp), %xmm5
leaq 0x11409(%rip), %rcx     # 0x151e0
movslq (%rcx), %rcx
movq %r14, -0x40(%rbp)
movslq (%r14), %rdx
subq %rcx, %rdx
xorpd %xmm7, %xmm7
movq %rdx, -0x68(%rbp)
cmpl %edx, %ecx
movapd 0xd37a(%rip), %xmm4     # 0x11170
jge 0x3f1d
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 0x3f08
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 0x3ef2
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 0x3e9d
jmp 0x3ef6
movapd %xmm1, %xmm7
incq %r11
movapd %xmm7, %xmm1
cmpq %rax, %r11
jne 0x3e52
jmp 0x3f0c
movapd %xmm1, %xmm7
incq %r8
movapd %xmm7, %xmm1
cmpq -0x68(%rbp), %r8
jne 0x3e35
movapd %xmm7, -0x40(%rbp)
movq -0x60(%rbp), %rbx
movq 0x10(%rbx), %rdi
callq 0x4064
movapd -0x40(%rbp), %xmm1
movsd %xmm1, 0x20(%rax)
maxsd -0x58(%rbp), %xmm1
movq (%rbx), %rbx
movapd %xmm1, %xmm0
leaq 0x112d4(%rip), %r14     # 0x15220
cmpq %r14, %rbx
jne 0x3d81
mulsd 0xd223(%rip), %xmm1     # 0x11180
movapd %xmm1, -0x40(%rbp)
jmp 0x3f69
movapd %xmm0, -0x40(%rbp)
movq (%r14), %rbx
cmpq %r14, %rbx
je 0x3fa1
movq 0x10(%rbx), %rdi
callq 0x4064
movapd -0x40(%rbp), %xmm0
ucomisd 0x20(%rax), %xmm0
jbe 0x3f9c
movq 0x10(%rbx), %rdi
callq 0x4052
testb %al, %al
jne 0x3f9c
movq 0x10(%rbx), %rdi
callq 0x423c
movq (%rbx), %rbx
jmp 0x3f6c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq