// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x20, %rsp
callq 0x2dec
xorpd %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
leaq 0xf8d7(%rip), %rbx      # 0x13210
leaq 0xf890(%rip), %r14      # 0x131d0
movq %rbx, %r15
movq (%r15), %r15
cmpq %rbx, %r15
je 0x3aa5
movq 0x10(%r15), %rdi
callq 0x3db0
movq 0x8(%rax), %r12
movq 0x10(%r15), %rdi
callq 0x3db0
movsd 0x70(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movq 0x10(%r15), %rdi
callq 0x3db0
movsd 0x78(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x10(%r15), %rdi
callq 0x3db0
movsd -0x38(%rbp), %xmm6
movsd -0x30(%rbp), %xmm5
movapd 0xb7d3(%rip), %xmm4     # 0xf170
movsd 0x80(%rax), %xmm0
movslq (%r14), %rax
movslq (%r12), %rcx
subq %rax, %rcx
addsd %xmm5, %xmm5
addsd %xmm6, %xmm6
addsd %xmm0, %xmm0
xorpd %xmm7, %xmm7
movq %rax, %rdx
cmpq %rcx, %rdx
jge 0x3a7e
movslq 0x4(%r12), %rsi
subq %rax, %rsi
movq %rax, %rdi
cmpq %rsi, %rdi
jge 0x3a76
movslq 0x8(%r12), %r8
subq %rax, %r8
movq %rax, %r9
cmpq %r8, %r9
jge 0x3a6e
movq 0x10(%r12), %r10
movq 0x8(%r10,%rdx,8), %r11
movq (%r11,%rdi,8), %r11
movsd (%r11,%r9,8), %xmm1
movq -0x8(%r10,%rdx,8), %r11
movq (%r10,%rdx,8), %r10
movq (%r11,%rdi,8), %r11
subsd (%r11,%r9,8), %xmm1
divsd %xmm5, %xmm1
movq 0x8(%r10,%rdi,8), %r11
movsd (%r11,%r9,8), %xmm2
movq -0x8(%r10,%rdi,8), %r11
movq (%r10,%rdi,8), %r10
subsd (%r11,%r9,8), %xmm2
divsd %xmm6, %xmm2
movsd 0x8(%r10,%r9,8), %xmm3
subsd -0x8(%r10,%r9,8), %xmm3
incq %r9
divsd %xmm0, %xmm3
andpd %xmm4, %xmm1
andpd %xmm4, %xmm2
addsd %xmm1, %xmm2
andpd %xmm4, %xmm3
addsd %xmm2, %xmm3
maxsd %xmm7, %xmm3
movapd %xmm3, %xmm7
jmp 0x39ea
incq %rdi
jmp 0x39d6
incq %rdx
jmp 0x39c2
movq 0x10(%r15), %rdi
movsd %xmm7, -0x30(%rbp)
callq 0x3db0
movsd -0x30(%rbp), %xmm0
movsd %xmm0, 0x20(%rax)
maxsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x3943
movsd -0x28(%rbp), %xmm0
mulsd 0xb6ce(%rip), %xmm0     # 0xf180
movsd %xmm0, -0x28(%rbp)
leaq 0xf752(%rip), %rbx      # 0x13210
movq %rbx, %r14
movq (%r14), %r14
cmpq %rbx, %r14
je 0x3ae9
movq 0x10(%r14), %rdi
callq 0x3db0
movsd 0x20(%rax), %xmm0
ucomisd -0x28(%rbp), %xmm0
jbe 0x3ac1
movq 0x10(%r14), %rdi
callq 0x3ef6
jmp 0x3ac1
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq