// File: /workspace/github_cpp_makefile_star2_O3/suhasjains[P]AMR-Solver/src/octree.cpp
// Function: myOctree::Octree::refine()
void Octree::refine() {
    
    int i, j, k;
    double xmin, xmax, ymin, ymax, zmin, zmax;
    double lev = this->level + 1;
    
    //creating children nodes
    for(k=0; k<2; k++) {
        for(j=0; j<2; j++) {
            for(i=0; i<2; i++) {
                
                if(i==0) {
                    xmin = this->x_min;
                    xmax = this->x_centre;
                }
                else {
                    xmin = this->x_centre;
                    xmax = this->x_max;
                }
                
                if(j==0) {
                    ymin = this->y_min;
                    ymax = this->y_centre;
                }
                else {
                    ymin = this->y_centre;
                    ymax = this->y_max;
                }
                
                if(k==0) {
                    zmin = this->z_min;
                    zmax = this->z_centre;
                }
                else {
                    zmin = this->z_centre;
                    zmax = this->z_max;
                }

                
                //creating new child object
                Octree child(xmin, xmax, ymin, ymax, zmin, zmax, lev);
                
                //deleting the pushed node to the list
                nodes.pop_back();
                
                //assigning parent to the child
                child.parent = this;
                
                //invoking copy constructor
                this->children[i][j][k] = new Octree(child);
                //*(this->children[i][j][k]) = child;


            }
        }
    }
    
	//setting neighbours to children - this could be redundant - yet to test
    	for(k=0; k<2; k++) {
        	for(j=0; j<2; j++) {
            		for(i=0; i<2; i++) {

				if(i==0) {
					this->children[i][j][k]->neighbour[XDIR][RIGHT] = this->children[i+1][j][k];
					if(this->neighbour[XDIR][LEFT] != NULL) 	this->children[i][j][k]->neighbour[XDIR][LEFT] = this->neighbour[XDIR][LEFT]->children[i+1][j][k];
				}	
				if(j==0) {
					this->children[i][j][k]->neighbour[YDIR][RIGHT] = this->children[i][j+1][k];
					if(this->neighbour[YDIR][LEFT] != NULL)	this->children[i][j][k]->neighbour[YDIR][LEFT] = this->neighbour[YDIR][LEFT]->children[i][j+1][k];
				}	
				if(k==0) {
					this->children[i][j][k]->neighbour[ZDIR][RIGHT] = this->children[i][j][k+1];
					if(this->neighbour[ZDIR][LEFT] != NULL)	this->children[i][j][k]->neighbour[ZDIR][LEFT] = this->neighbour[ZDIR][LEFT]->children[i][j][k+1];
				}	
				if(i==1) {
					this->children[i][j][k]->neighbour[XDIR][LEFT] = this->children[i-1][j][k];
					if(this->neighbour[XDIR][RIGHT] != NULL)	this->children[i][j][k]->neighbour[XDIR][RIGHT] = this->neighbour[XDIR][RIGHT]->children[i-1][j][k];
				}	
				if(j==1) {
					this->children[i][j][k]->neighbour[YDIR][LEFT] = this->children[i][j-1][k];
					if(this->neighbour[YDIR][RIGHT] != NULL)	this->children[i][j][k]->neighbour[YDIR][RIGHT] = this->neighbour[YDIR][RIGHT]->children[i][j-1][k];
				}	
				if(k==1) {
					this->children[i][j][k]->neighbour[ZDIR][LEFT] = this->children[i][j][k-1];
					if(this->neighbour[ZDIR][RIGHT] != NULL)	this->children[i][j][k]->neighbour[ZDIR][RIGHT] = this->neighbour[ZDIR][RIGHT]->children[i][j][k-1];
				}	
			}
		}	
	}		

	//setting boundaries to children
    	for(k=0; k<2; k++) {
        	for(j=0; j<2; j++) {
            		for(i=0; i<2; i++) {

				if(i==0) {
					this->children[i][j][k]->bc[XDIR][RIGHT] = NONE;
					this->children[i][j][k]->bc[XDIR][LEFT] = this->bc[XDIR][LEFT];
				}	
				if(j==0) {
					this->children[i][j][k]->bc[YDIR][RIGHT] = NONE;
					this->children[i][j][k]->bc[YDIR][LEFT] = this->bc[YDIR][LEFT];
				}	
				if(k==0) {
					this->children[i][j][k]->bc[ZDIR][RIGHT] = NONE;
					this->children[i][j][k]->bc[ZDIR][LEFT] = this->bc[ZDIR][LEFT];
				}	
				if(i==1) {
					this->children[i][j][k]->bc[XDIR][LEFT] = NONE;
					this->children[i][j][k]->bc[XDIR][RIGHT] = this->bc[XDIR][RIGHT];
				}	
				if(j==1) {
					this->children[i][j][k]->bc[YDIR][LEFT] = NONE;
					this->children[i][j][k]->bc[YDIR][RIGHT] = this->bc[YDIR][RIGHT];
				}	
				if(k==1) {
					this->children[i][j][k]->bc[ZDIR][LEFT] = NONE;
					this->children[i][j][k]->bc[ZDIR][RIGHT] = this->bc[ZDIR][RIGHT];
				}	
			}
		}	
	}		

	//setting boundary conditions to children fields 
    	for(k=0; k<2; k++) {
        	for(j=0; j<2; j++) {
            		for(i=0; i<2; i++) {
		
				for(int l = 0; l<scalar_fields.size() ; l++) {
					for (int m=0; m<3; m++) {
						for (int n=0; n<2; n++) {
					
							if(this->children[i][j][k]->bc[m][n] == BOUNDARY) {
								this->children[i][j][k]->get_block_data()->scalarfields[l]->bc[m][n] = \
								this->get_block_data()->scalarfields[l]->bc[m][n];  			
							}
							
							if(this->children[i][j][k]->bc[m][n] == NONE) {
								this->children[i][j][k]->get_block_data()->scalarfields[l]->bc[m][n] = none;
							}
							
							if(this->children[i][j][k]->bc[m][n] == MPI_BOUNDARY) {
								this->children[i][j][k]->get_block_data()->scalarfields[l]->bc[m][n] = mpi_boundary;
							}
						}	
					}
				}
				
				for(int l = 0; l<vector_fields.size() ; l++) {
					for (int m=0; m<3; m++) {
						for (int n=0; n<2; n++) {
					
							if(this->children[i][j][k]->bc[m][n] == BOUNDARY) {
								this->children[i][j][k]->get_block_data()->vectorfields[l]->xbc[m][n] = \
								this->get_block_data()->vectorfields[l]->xbc[m][n];  			
								this->children[i][j][k]->get_block_data()->vectorfields[l]->ybc[m][n] = \
								this->get_block_data()->vectorfields[l]->ybc[m][n];  			
								this->children[i][j][k]->get_block_data()->vectorfields[l]->zbc[m][n] = \
								this->get_block_data()->vectorfields[l]->zbc[m][n];  			
							}
							
							if(this->children[i][j][k]->bc[m][n] == NONE) {
								this->children[i][j][k]->get_block_data()->vectorfields[l]->xbc[m][n] = none;
								this->children[i][j][k]->get_block_data()->vectorfields[l]->ybc[m][n] = none;
								this->children[i][j][k]->get_block_data()->vectorfields[l]->zbc[m][n] = none;
							}
							
							if(this->children[i][j][k]->bc[m][n] == MPI_BOUNDARY) {
								this->children[i][j][k]->get_block_data()->vectorfields[l]->xbc[m][n] = mpi_boundary;
								this->children[i][j][k]->get_block_data()->vectorfields[l]->ybc[m][n] = mpi_boundary;
								this->children[i][j][k]->get_block_data()->vectorfields[l]->zbc[m][n] = mpi_boundary;
							}
						}	
					}
				}
			}
		}	
	}

	//Bilinear interpolation to fill ghost cells of children nodes


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdi, %rbx
movl 0xf0(%rdi), %eax
incl %eax
movl %eax, -0x58(%rbp)
leaq 0xa0(%rdi), %rax
movq %rax, -0x30(%rbp)
movb $0x1, %al
movl %eax, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
leaq -0x170(%rbp), %r15
movb $0x1, %al
movl %eax, -0x38(%rbp)
xorl %eax, %eax
movb $0x1, %r14b
shlq $0x4, %rax
movq %rax, -0x50(%rbp)
xorl %r13d, %r13d
movsd 0x50(%rbx), %xmm0
movaps %xmm0, %xmm1
testb $0x1, %r14b
jne 0x446b
movsd 0x70(%rbx), %xmm1
je 0x4472
movsd 0x68(%rbx), %xmm0
movsd 0x58(%rbx), %xmm2
movaps %xmm2, %xmm3
testb $0x1, -0x38(%rbp)
jne 0x4488
movsd 0x80(%rbx), %xmm3
je 0x448f
movsd 0x78(%rbx), %xmm2
movsd 0x60(%rbx), %xmm4
movaps %xmm4, %xmm5
testb $0x1, -0x40(%rbp)
jne 0x44a5
movsd 0x90(%rbx), %xmm5
je 0x44af
movsd 0x88(%rbx), %xmm4
movq %r15, %rdi
movl -0x58(%rbp), %esi
callq 0x4cf0
leaq 0x10d57(%rip), %rax     # 0x15218
movq 0x8(%rax), %r12
decq 0x10(%rax)
movq %r12, %rdi
callq 0x2320
movl $0x18, %esi
movq %r12, %rdi
callq 0x2210
movq %rbx, -0x88(%rbp)
movl $0xf8, %edi
callq 0x2200
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x4b4e
shlq $0x5, %r13
addq -0x30(%rbp), %r13
addq -0x50(%rbp), %r13
movq -0x48(%rbp), %rax
movq %r12, (%r13,%rax,8)
movq %r15, %rdi
callq 0x4b20
movl $0x1, %r13d
testb $0x1, %r14b
movl $0x0, %r14d
jne 0x4458
movl $0x1, %eax
testb $0x1, -0x38(%rbp)
movl $0x0, -0x38(%rbp)
jne 0x444a
movq %rax, -0x48(%rbp)
testb $0x1, -0x40(%rbp)
movl $0x0, -0x40(%rbp)
jne 0x4443
leaq 0xc0(%rbx), %rax
movq %rax, -0x40(%rbp)
movb $0x1, %al
movl %eax, -0x38(%rbp)
movq $0x0, -0x50(%rbp)
xorl %esi, %esi
xorl %edi, %edi
movb $0x1, %r8b
xorl %r9d, %r9d
shlq $0x4, %r9
movq -0x40(%rbp), %rax
addq %r9, %rax
leaq (%rax,%rsi,8), %r10
movq -0x30(%rbp), %rax
addq %r9, %rax
leaq (%rax,%rsi,8), %r11
movb $0x1, %r14b
xorl %r12d, %r12d
xorl %eax, %eax
testb $0x1, %r14b
je 0x45ca
movq (%r10), %rcx
movq (%r11), %r15
movq %rcx, 0x8(%r15)
movq 0x10(%rbx), %rcx
testq %rcx, %rcx
je 0x45ca
addq %r9, %rcx
movq 0xc0(%rcx,%rsi,8), %rcx
movq (%r11), %r15
movq %rcx, 0x10(%r15)
testb $0x1, %r8b
je 0x4608
movq %rax, %r13
shlq $0x5, %r13
movq -0x30(%rbp), %rcx
leaq (%rcx,%r13), %r15
movq (%r15,%rsi,8), %rcx
movq 0x10(%r15,%rsi,8), %rdx
movq %rdx, 0x18(%rcx)
movq 0x20(%rbx), %rcx
testq %rcx, %rcx
je 0x4608
addq %r13, %rcx
movq 0xb0(%rcx,%rsi,8), %rcx
movq (%r15,%rsi,8), %rdx
movq %rcx, 0x20(%rdx)
testb $0x1, -0x38(%rbp)
je 0x4649
movq %rax, %r15
shlq $0x5, %r15
movq -0x30(%rbp), %rcx
leaq (%rcx,%r15), %r13
movq (%r9,%r13), %rcx
movq 0x8(%r9,%r13), %rdx
movq %rdx, 0x28(%rcx)
movq 0x30(%rbx), %rcx
testq %rcx, %rcx
je 0x4649
addq %r9, %r13
addq %r15, %rcx
movq 0xa8(%r9,%rcx), %rcx
movq (%r13), %rdx
movq %rcx, 0x30(%rdx)
testb $0x1, %r12b
je 0x4674
movq (%r11), %rcx
movq (%r10), %rdx
movq %rcx, 0x10(%rdx)
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x4674
addq %r9, %rcx
movq 0xa0(%rcx,%rsi,8), %rcx
movq (%r10), %rdx
movq %rcx, 0x8(%rdx)
testb $0x1, %dil
je 0x46b3
movq %rax, %r12
shlq $0x5, %r12
movq -0x30(%rbp), %rcx
leaq (%rcx,%r12), %r15
movq (%r15,%rsi,8), %rcx
movq 0x10(%r15,%rsi,8), %rdx
movq %rcx, 0x20(%rdx)
movq 0x18(%rbx), %rcx
testq %rcx, %rcx
je 0x46b3
addq %r12, %rcx
movq 0xa0(%rcx,%rsi,8), %rcx
movq 0x10(%r15,%rsi,8), %rdx
movq %rcx, 0x18(%rdx)
testb $0x1, -0x50(%rbp)
je 0x46f2
shlq $0x5, %rax
movq -0x30(%rbp), %rcx
leaq (%rcx,%rax), %r12
movq (%r9,%r12), %rcx
movq 0x8(%r9,%r12), %rdx
movq %rcx, 0x30(%rdx)
movq 0x28(%rbx), %rcx
testq %rcx, %rcx
je 0x46f2
addq %r9, %r12
addq %rax, %rcx
movq 0xa0(%r9,%rcx), %rax
movq 0x8(%r12), %rcx
movq %rax, 0x28(%rcx)
movl $0x1, %eax
movb $0x1, %r12b
testb $0x1, %r14b
movl $0x0, %r14d
jne 0x459f
movl $0x1, %r9d
movb $0x1, %dil
testb $0x1, %r8b
movl $0x0, %r8d
jne 0x457d
movl $0x1, %esi
movb $0x1, %al
movq %rax, -0x50(%rbp)
testb $0x1, -0x38(%rbp)
movl $0x0, -0x38(%rbp)
jne 0x4575
movb $0x1, %cl
xorl %edx, %edx
xorl %esi, %esi
movq -0x30(%rbp), %rax
leaq (%rax,%rsi,8), %rax
movq %rax, -0x38(%rbp)
xorl %r8d, %r8d
movb $0x1, %r9b
xorl %r10d, %r10d
shlq $0x4, %r10
movq -0x30(%rbp), %rax
addq %r10, %rax
leaq (%rax,%rsi,8), %r11
movq -0x40(%rbp), %rax
addq %r10, %rax
leaq (%rax,%rsi,8), %r14
movb $0x1, %r15b
xorl %r13d, %r13d
xorl %r12d, %r12d
testb $0x1, %r15b
je 0x4793
movq (%r11), %rax
movl $0x0, 0x38(%rax)
movl 0x3c(%rbx), %edi
movl %edi, 0x3c(%rax)
testb $0x1, %r9b
je 0x47b5
movq %r12, %rax
shlq $0x5, %rax
movq -0x38(%rbp), %rdi
movq (%rdi,%rax), %rax
movl $0x0, 0x40(%rax)
movl 0x44(%rbx), %edi
movl %edi, 0x44(%rax)
testb $0x1, %cl
je 0x47d6
movq %r12, %rax
shlq $0x5, %rax
addq -0x30(%rbp), %rax
movq (%r10,%rax), %rax
movl $0x0, 0x48(%rax)
movl 0x4c(%rbx), %edi
movl %edi, 0x4c(%rax)
testb $0x1, %r13b
je 0x47ec
movq (%r14), %rax
movl $0x0, 0x3c(%rax)
movl 0x38(%rbx), %edi
movl %edi, 0x38(%rax)
testb $0x1, %r8b
je 0x480f
movq %r12, %rax
shlq $0x5, %rax
addq -0x30(%rbp), %rax
movq 0x10(%rax,%rsi,8), %rax
movl $0x0, 0x44(%rax)
movl 0x40(%rbx), %edi
movl %edi, 0x40(%rax)
testb $0x1, %dl
je 0x482e
shlq $0x5, %r12
addq -0x30(%rbp), %r12
movq 0x8(%r10,%r12), %rax
movl $0x0, 0x4c(%rax)
movl 0x48(%rbx), %edi
movl %edi, 0x48(%rax)
movl $0x1, %r12d
movb $0x1, %r13b
testb $0x1, %r15b
movl $0x0, %r15d
jne 0x477d
movl $0x1, %r10d
movb $0x1, %r8b
testb $0x1, %r9b
movl $0x0, %r9d
jne 0x475a
movl $0x1, %esi
movb $0x1, %dl
testb $0x1, %cl
movl $0x0, %ecx
jne 0x4745
leaq 0x10dbc(%rip), %rax     # 0x15638
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
movq %rax, %rdx
movq %rcx, -0x58(%rbp)
subq %rcx, %rdx
sarq $0x5, %rdx
leaq 0x10db4(%rip), %rax     # 0x15650
movq (%rax), %rcx
movq 0x8(%rax), %r8
movq %r8, -0x68(%rbp)
movq %rcx, -0x70(%rbp)
subq %rcx, %r8
sarq $0x5, %r8
cmpq $0x1, %rdx
adcq $0x0, %rdx
movq %rdx, -0x38(%rbp)
cmpq $0x1, %r8
adcq $0x0, %r8
movb $0x1, %al
movq $0x0, -0x48(%rbp)
movl %eax, -0x5c(%rbp)
movb $0x1, %al
xorl %ecx, %ecx
movl %eax, -0x60(%rbp)
movb $0x1, %al
shlq $0x4, %rcx
movq %rcx, -0x40(%rbp)
xorl %edx, %edx
movl %eax, -0x50(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x58(%rbp), %rax
je 0x499d
movq %rdx, %rax
shlq $0x5, %rax
addq -0x30(%rbp), %rax
addq -0x40(%rbp), %rax
movq -0x48(%rbp), %rcx
movq (%rax,%rcx,8), %r9
leaq 0x38(%r9), %r11
xorl %eax, %eax
xorl %ecx, %ecx
movq 0xe0(%rbx), %rsi
movb $0x1, %dil
leaq (%r11,%rcx,8), %r13
xorl %r10d, %r10d
movl (%r13,%r10,4), %r14d
testl %r14d, %r14d
je 0x495a
cmpl $0x2, %r14d
je 0x4954
cmpl $0x1, %r14d
jne 0x4976
movq 0x10(%rsi), %r14
leaq (,%rcx,8), %r15
addq (%r14,%rax,8), %r15
movl 0x38(%r15,%r10,4), %r14d
jmp 0x495a
movl $0x3, %r14d
movq 0xe0(%r9), %r15
movq 0x10(%r15), %r15
leaq (,%rcx,8), %r12
addq (%r15,%rax,8), %r12
movl %r14d, 0x38(%r12,%r10,4)
movl $0x1, %r10d
testb $0x1, %dil
movl $0x0, %edi
jne 0x4927
incq %rcx
cmpq $0x3, %rcx
jne 0x4916
incq %rax
cmpq -0x38(%rbp), %rax
jne 0x4914
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x4aaa
shlq $0x5, %rdx
addq -0x30(%rbp), %rdx
addq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rax
movq (%rdx,%rax,8), %r12
leaq 0x38(%r12), %r13
xorl %r9d, %r9d
xorl %r11d, %r11d
movq 0xe0(%rbx), %rax
movb $0x1, %cl
leaq (,%r11,8), %rsi
addq %r13, %rsi
xorl %edi, %edi
movl (%rsi,%rdi,4), %r10d
testl %r10d, %r10d
je 0x4a54
cmpl $0x2, %r10d
je 0x4a38
cmpl $0x1, %r10d
jne 0x4a7e
movq 0x18(%rax), %r10
leaq (,%r11,8), %r14
addq (%r10,%r9,8), %r14
movl 0x48(%r14,%rdi,4), %r15d
movq 0xe0(%r12), %r10
movq 0x18(%r10), %r10
movq (%r10,%r9,8), %r10
leaq (%r10,%r11,8), %rdx
movl %r15d, 0x48(%rdx,%rdi,4)
movl 0x60(%r14,%rdi,4), %r15d
movl %r15d, 0x60(%rdx,%rdi,4)
movl 0x78(%r14,%rdi,4), %r14d
jmp 0x4a75
movq 0xe0(%r12), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%r9,8), %r10
leaq (%r10,%r11,8), %rdx
movl $0x3, %r14d
jmp 0x4a6b
movq 0xe0(%r12), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%r9,8), %r10
leaq (%r10,%r11,8), %rdx
xorl %r14d, %r14d
movl %r14d, 0x48(%rdx,%rdi,4)
movl %r14d, 0x60(%rdx,%rdi,4)
leaq (%r10,%r11,8), %rdx
movl %r14d, 0x78(%rdx,%rdi,4)
movl $0x1, %edi
testb $0x1, %cl
movl $0x0, %ecx
jne 0x49e0
incq %r11
cmpq $0x3, %r11
jne 0x49ca
incq %r9
cmpq %r8, %r9
jne 0x49c7
movl $0x1, %edx
testb $0x1, -0x50(%rbp)
movl $0x0, %eax
jne 0x48e6
movl $0x1, %ecx
testb $0x1, -0x60(%rbp)
jne 0x48d7
movl $0x1, %eax
movq %rax, -0x48(%rbp)
testb $0x1, -0x5c(%rbp)
movl $0x0, %eax
jne 0x48d0
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xf8, %esi
movq %r12, %rdi
callq 0x2210
jmp 0x4b0c
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
callq 0x4b20
movq %rbx, %rdi
callq 0x2350