// File: 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
subq $0xa0, %rsp
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x4930
leaq 0x1faa1(%rip), %rdi     # 0x262b0
callq 0x4490
movq %rax, -0x50(%rbp)
leaq 0x1fa91(%rip), %rdi     # 0x262b0
callq 0x44c0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x683e
jmp 0x6b35
leaq -0x50(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movq 0x8(%rax), %rax
movq %rax, -0x60(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x20(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd 0x80(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
leaq 0x1f9ae(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x60(%rbp), %rcx
movl (%rcx), %ecx
leaq 0x1f999(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x6ae4
leaq 0x1f988(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x60(%rbp), %rcx
movl 0x4(%rcx), %ecx
leaq 0x1f972(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x6ad4
leaq 0x1f961(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x60(%rbp), %rcx
movl 0x8(%rcx), %ecx
leaq 0x1f94b(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x6ac4
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x64(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x64(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd 0x1a7ed(%rip), %xmm1    # 0x21168
mulsd -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x68(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x68(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x6c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movsd 0x1a78b(%rip), %xmm1    # 0x21168
mulsd -0x28(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x10(%rbp)
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x6c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x68(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x6c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
subsd (%rax,%rcx,8), %xmm0
movsd 0x1a729(%rip), %xmm1    # 0x21168
mulsd -0x30(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x18(%rbp)
movsd -0x8(%rbp), %xmm0
callq 0x6d50
movsd %xmm0, -0x90(%rbp)
movsd -0x10(%rbp), %xmm0
callq 0x6d50
movaps %xmm0, %xmm1
movsd -0x90(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
movsd -0x18(%rbp), %xmm0
callq 0x6d50
movaps %xmm0, %xmm1
movsd -0x88(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
ucomisd -0x38(%rbp), %xmm0
jbe 0x6ab4
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x6ab6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x690c
jmp 0x6ac6
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x68e5
jmp 0x6ad6
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x68bf
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd -0x98(%rbp), %xmm0
movsd %xmm0, 0x20(%rax)
movsd -0x38(%rbp), %xmm0
ucomisd -0x48(%rbp), %xmm0
jbe 0x6b25
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x6b27
leaq -0x50(%rbp), %rdi
callq 0x4550
jmp 0x6828
leaq 0x1f774(%rip), %rdi     # 0x262b0
callq 0x4490
movq %rax, -0x78(%rbp)
leaq 0x1f764(%rip), %rdi     # 0x262b0
callq 0x44c0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x6b68
jmp 0x6bc4
leaq -0x78(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd 0x20(%rax), %xmm1
movsd 0x1a5da(%rip), %xmm0    # 0x21160
mulsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6bb7
leaq -0x78(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6dc0
testb $0x1, %al
jne 0x6bb7
leaq -0x78(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x7170
jmp 0x6bb9
leaq -0x78(%rbp), %rdi
callq 0x4550
jmp 0x6b55
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)