// File: 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
subq $0xa0, %rsp
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
callq 0x4930
leaq 0x1fe71(%rip), %rdi     # 0x262b0
callq 0x4490
movq %rax, -0x50(%rbp)
leaq 0x1fe61(%rip), %rdi     # 0x262b0
callq 0x44c0
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x646e
jmp 0x6765
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 0x1fd7e(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x60(%rbp), %rcx
movl (%rcx), %ecx
leaq 0x1fd69(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x6714
leaq 0x1fd58(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq -0x60(%rbp), %rcx
movl 0x4(%rcx), %ecx
leaq 0x1fd42(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x6704
leaq 0x1fd31(%rip), %rax     # 0x26268
movl (%rax), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x60(%rbp), %rcx
movl 0x8(%rcx), %ecx
leaq 0x1fd1b(%rip), %rdx     # 0x26268
subl (%rdx), %ecx
cmpl %ecx, %eax
jge 0x66f4
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 0x1abbd(%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 0x1ab5b(%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 0x1aaf9(%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 0x66e4
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x66e6
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x653c
jmp 0x66f6
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x6515
jmp 0x6706
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x64ef
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 0x6755
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x6757
leaq -0x50(%rbp), %rdi
callq 0x4550
jmp 0x6458
leaq 0x1fb44(%rip), %rdi     # 0x262b0
callq 0x4490
movq %rax, -0x78(%rbp)
leaq 0x1fb34(%rip), %rdi     # 0x262b0
callq 0x44c0
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x44f0
testb $0x1, %al
jne 0x6798
jmp 0x67df
leaq -0x78(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x6de0
movsd 0x20(%rax), %xmm0
movsd 0x1a9aa(%rip), %xmm1    # 0x21160
mulsd -0x48(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x67d2
leaq -0x78(%rbp), %rdi
callq 0x4520
movq (%rax), %rdi
callq 0x7070
jmp 0x67d4
leaq -0x78(%rbp), %rdi
callq 0x4550
jmp 0x6785
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)