// File: /workspace/github_cpp_makefile_star2_O3/suhasjains[P]AMR-Solver/src/adapt.cpp
// Function: myOctree::set_refine_flag_based_on_gradient()
void set_refine_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) {

                if((*it)->get_block_data()->max_gradient > max_of_max_gradient*0.2) {
                        (*it)->set_to_refine_with_nesting();
			//printf("yes refine\n");
		}	
        }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
callq 0x2f30
leaq 0x1162c(%rip), %rax     # 0x15230
movq (%rax), %rbx
cmpq %rax, %rbx
je 0x3e35
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 0x1157e(%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 0xd4e7(%rip), %xmm4     # 0x11170
jge 0x3db0
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 0x3d9b
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 0x3d85
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 0x3d30
jmp 0x3d89
movapd %xmm1, %xmm7
incq %r11
movapd %xmm7, %xmm1
cmpq %rax, %r11
jne 0x3ce5
jmp 0x3d9f
movapd %xmm1, %xmm7
incq %r8
movapd %xmm7, %xmm1
cmpq -0x68(%rbp), %r8
jne 0x3cc8
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 0x111d1(%rip), %rbx     # 0x14fb0
jne 0x3c14
movapd %xmm1, -0x40(%rbp)
leaq 0x1143f(%rip), %rax     # 0x15230
movq (%rax), %rbx
cmpq %rax, %rbx
je 0x3e35
movapd -0x40(%rbp), %xmm0
mulsd 0xd37a(%rip), %xmm0     # 0x11180
movapd %xmm0, -0x40(%rbp)
movq 0x10(%rbx), %rdi
callq 0x418c
movsd 0x20(%rax), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x3e29
movq 0x10(%rbx), %rdi
callq 0x4300
movq (%rbx), %rbx
cmpq 0x1117d(%rip), %rbx     # 0x14fb0
jne 0x3e0b
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq