// File: /workspace/github_cpp_makefile_star2_O1/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, -0x48(%rbp)
leaq 0xa0(%rdi), %r14
movq $0x0, -0x30(%rbp)
leaq -0x170(%rbp), %r15
movq %r14, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
xorl %r13d, %r13d
movsd 0x50(%rbx), %xmm0
movaps %xmm0, %xmm1
testq %r13, %r13
je 0x42db
movsd 0x70(%rbx), %xmm1
jne 0x42e2
movsd 0x68(%rbx), %xmm0
movsd 0x58(%rbx), %xmm2
movaps %xmm2, %xmm3
cmpq $0x0, -0x38(%rbp)
je 0x42f9
movsd 0x80(%rbx), %xmm3
jne 0x4300
movsd 0x78(%rbx), %xmm2
movsd 0x60(%rbx), %xmm4
movaps %xmm4, %xmm5
cmpq $0x0, -0x30(%rbp)
je 0x4317
movsd 0x90(%rbx), %xmm5
jne 0x4321
movsd 0x88(%rbx), %xmm4
movq %r15, %rdi
movl -0x48(%rbp), %esi
callq 0x4ade
leaq 0x10ed5(%rip), %rax     # 0x15208
movq 0x8(%rax), %r12
decq 0x10(%rax)
movq %r12, %rdi
callq 0x2310
movl $0x18, %esi
movq %r12, %rdi
callq 0x2200
movq %rbx, -0x88(%rbp)
movl $0xf8, %edi
callq 0x21f0
movq %rax, %r12
movq %rax, %rdi
movq %r15, %rsi
callq 0x4916
movq %r12, (%r14,%r13)
movq %r15, %rdi
callq 0x48e2
addq $0x20, %r13
cmpq $0x20, %r13
je 0x42c9
movq -0x38(%rbp), %rcx
leaq 0x1(%rcx), %rax
addq $0x10, %r14
testq %rcx, %rcx
movq %rax, -0x38(%rbp)
je 0x42c6
movq -0x30(%rbp), %rcx
leaq 0x1(%rcx), %rax
movq -0x40(%rbp), %r14
addq $0x8, %r14
testq %rcx, %rcx
movq %rax, -0x30(%rbp)
je 0x42ba
leaq 0xa0(%rbx), %rax
movq %rax, -0x30(%rbp)
leaq 0xc0(%rbx), %rcx
leaq 0xa8(%rbx), %rax
movq %rax, -0x38(%rbp)
leaq 0xb0(%rbx), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq -0x38(%rbp), %r10
xorl %r11d, %r11d
movq %r11, %r14
shlq $0x4, %r14
leaq (%rcx,%r14), %rax
leaq (%rax,%r8,8), %r15
movq -0x30(%rbp), %rax
addq %r14, %rax
leaq (%rax,%r8,8), %r12
xorl %r13d, %r13d
testq %r13, %r13
jne 0x443b
movq (%r15), %rax
movq (%r12), %rdx
movq %rax, 0x8(%rdx)
movq 0x10(%rbx), %rax
testq %rax, %rax
je 0x443b
addq %r14, %rax
movq 0xc0(%rax,%r8,8), %rax
movq (%r12), %rdx
movq %rax, 0x10(%rdx)
testq %r11, %r11
jne 0x446a
movq -0x10(%rsi,%r13), %rax
movq (%rsi,%r13), %rdx
movq %rdx, 0x18(%rax)
movq 0x20(%rbx), %rax
testq %rax, %rax
je 0x446a
addq %rdi, %rax
movq 0xb0(%r13,%rax), %rax
movq -0x10(%rsi,%r13), %rdx
movq %rax, 0x20(%rdx)
testq %r8, %r8
jne 0x4499
movq -0x8(%r10,%r13), %rax
movq (%r10,%r13), %rdx
movq %rdx, 0x28(%rax)
movq 0x30(%rbx), %rax
testq %rax, %rax
je 0x4499
addq %r9, %rax
movq 0xa8(%r13,%rax), %rax
movq -0x8(%r10,%r13), %rdx
movq %rax, 0x30(%rdx)
cmpq $0x20, %r13
jne 0x44c5
movq (%r12), %rax
movq (%r15), %rdx
movq %rax, 0x10(%rdx)
movq 0x8(%rbx), %rax
testq %rax, %rax
je 0x44c5
addq %r14, %rax
movq 0xa0(%rax,%r8,8), %rax
movq (%r15), %rdx
movq %rax, 0x8(%rdx)
cmpq $0x1, %r11
jne 0x44f4
movq -0x10(%rsi,%r13), %rax
movq (%rsi,%r13), %rdx
movq %rax, 0x20(%rdx)
movq 0x18(%rbx), %rax
testq %rax, %rax
je 0x44f4
addq %rdi, %rax
movq 0xa0(%r13,%rax), %rax
movq (%rsi,%r13), %rdx
movq %rax, 0x18(%rdx)
cmpq $0x1, %r8
jne 0x4523
movq -0x8(%r10,%r13), %rax
movq (%r10,%r13), %rdx
movq %rax, 0x30(%rdx)
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x4523
addq %r9, %rax
movq 0xa0(%r13,%rax), %rax
movq (%r10,%r13), %rdx
movq %rax, 0x28(%rdx)
addq $0x20, %r13
cmpq $0x20, %r13
je 0x440f
leaq 0x1(%r11), %rax
addq $0x10, %r10
addq $0x10, %r9
testq %r11, %r11
movq %rax, %r11
je 0x43f2
leaq 0x1(%r8), %rax
addq $0x8, %rdi
addq $0x8, %rsi
testq %r8, %r8
movq %rax, %r8
je 0x43e8
leaq 0xa8(%rbx), %rdx
leaq 0xb0(%rbx), %rsi
xorl %edi, %edi
movq %rdx, %rax
xorl %r8d, %r8d
movq %r8, %r10
shlq $0x4, %r10
movq -0x30(%rbp), %r9
addq %r10, %r9
leaq (%r9,%rdi,8), %r9
addq %rcx, %r10
leaq (%r10,%rdi,8), %r10
xorl %r11d, %r11d
testq %r11, %r11
jne 0x45ab
movq (%r9), %r14
movl $0x0, 0x38(%r14)
movl 0x3c(%rbx), %r15d
movl %r15d, 0x3c(%r14)
testq %r8, %r8
jne 0x45c5
movq -0x10(%rsi,%r11), %r14
movl $0x0, 0x40(%r14)
movl 0x44(%rbx), %r15d
movl %r15d, 0x44(%r14)
testq %rdi, %rdi
jne 0x45df
movq -0x8(%rax,%r11), %r14
movl $0x0, 0x48(%r14)
movl 0x4c(%rbx), %r15d
movl %r15d, 0x4c(%r14)
cmpq $0x20, %r11
jne 0x45f8
movq (%r10), %r14
movl $0x0, 0x3c(%r14)
movl 0x38(%rbx), %r15d
movl %r15d, 0x38(%r14)
cmpq $0x1, %r8
jne 0x4612
movq (%rsi,%r11), %r14
movl $0x0, 0x44(%r14)
movl 0x40(%rbx), %r15d
movl %r15d, 0x40(%r14)
cmpq $0x1, %rdi
jne 0x462c
movq (%rax,%r11), %r14
movl $0x0, 0x4c(%r14)
movl 0x48(%rbx), %r15d
movl %r15d, 0x48(%r14)
addq $0x20, %r11
cmpq $0x20, %r11
je 0x4593
leaq 0x1(%r8), %r9
addq $0x10, %rax
testq %r8, %r8
movq %r9, %r8
je 0x4577
leaq 0x1(%rdi), %rax
addq $0x8, %rsi
testq %rdi, %rdi
movq %rax, %rdi
je 0x4571
leaq 0x10fbf(%rip), %rax     # 0x15628
movq (%rax), %rcx
movq 0x8(%rax), %rax
movq %rax, -0x70(%rbp)
movq %rax, %rsi
movq %rcx, -0x78(%rbp)
subq %rcx, %rsi
sarq $0x5, %rsi
leaq 0x10fb7(%rip), %rax     # 0x15640
movq (%rax), %rcx
movq 0x8(%rax), %r9
movq %r9, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
subq %rcx, %r9
sarq $0x5, %r9
cmpq $0x1, %rsi
adcq $0x0, %rsi
cmpq $0x1, %r9
adcq $0x0, %r9
movq $0x0, -0x40(%rbp)
movq %rsi, -0x68(%rbp)
xorl %eax, %eax
movq %rax, -0x50(%rbp)
shlq $0x4, %rax
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
movq -0x70(%rbp), %rax
cmpq -0x78(%rbp), %rax
je 0x4764
movq %r15, %rax
shlq $0x5, %rax
addq -0x30(%rbp), %rax
addq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %rax
xorl %ecx, %ecx
movl $0x38, %edx
xorl %edi, %edi
movq 0xe0(%rbx), %r8
movl $0x1, %r11d
movq %rdx, %r12
movl (%rax,%r12), %r10d
testl %r10d, %r10d
je 0x4733
cmpl $0x2, %r10d
je 0x472d
cmpl $0x1, %r10d
jne 0x4746
movq 0x10(%r8), %r10
movq (%r10,%rcx,8), %r10
movl (%r10,%r12), %r10d
jmp 0x4733
movl $0x3, %r10d
movq 0xe0(%rax), %r14
movq 0x10(%r14), %r14
movq (%r14,%rcx,8), %r14
movl %r10d, (%r14,%r12)
addq $0x4, %r12
decq %r11
je 0x470a
incq %rdi
addq $0x8, %rdx
cmpq $0x3, %rdi
jne 0x46fa
incq %rcx
cmpq %rsi, %rcx
jne 0x46f3
movq %r15, -0x38(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x60(%rbp), %rax
je 0x486b
movq -0x38(%rbp), %rax
shlq $0x5, %rax
addq -0x30(%rbp), %rax
addq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
movq (%rax,%rcx,8), %r12
leaq 0x38(%r12), %rax
xorl %r11d, %r11d
movq %rax, %r13
xorl %ecx, %ecx
xorl %edx, %edx
movq 0xe0(%rbx), %rdi
xorl %r8d, %r8d
movl (%r13,%r8), %r10d
testl %r10d, %r10d
je 0x4813
cmpl $0x2, %r10d
je 0x47f7
cmpl $0x1, %r10d
jne 0x483c
movq 0x18(%rdi), %r10
movq (%r10,%r11,8), %r14
addq %rcx, %r14
movl 0x48(%r8,%r14), %r15d
movq 0xe0(%r12), %r10
movq 0x18(%r10), %r10
movq (%r10,%r11,8), %r10
leaq (%r10,%rcx), %rsi
movl %r15d, 0x48(%r8,%rsi)
movl 0x60(%r8,%r14), %r15d
movl %r15d, 0x60(%r8,%rsi)
movl 0x78(%r8,%r14), %r14d
jmp 0x4834
movq 0xe0(%r12), %rsi
movq 0x18(%rsi), %rsi
movq (%rsi,%r11,8), %r10
leaq (%r10,%rcx), %rsi
movl $0x3, %r14d
jmp 0x482a
movq 0xe0(%r12), %rsi
movq 0x18(%rsi), %rsi
movq (%rsi,%r11,8), %r10
leaq (%r10,%rcx), %rsi
xorl %r14d, %r14d
movl %r14d, 0x48(%r8,%rsi)
movl %r14d, 0x60(%r8,%rsi)
addq %rcx, %r10
movl %r14d, 0x78(%r8,%r10)
addq $0x4, %r8
cmpq $0x4, %r8
je 0x47a7
incq %rdx
addq $0x8, %rcx
addq $0x8, %r13
cmpq $0x3, %rdx
jne 0x479d
incq %r11
cmpq %r9, %r11
jne 0x4796
movq -0x38(%rbp), %rcx
leaq 0x1(%rcx), %r15
testq %rcx, %rcx
movq -0x68(%rbp), %rsi
je 0x46cc
movq -0x50(%rbp), %rcx
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
je 0x46bd
movq -0x40(%rbp), %rcx
leaq 0x1(%rcx), %rax
testq %rcx, %rcx
movq %rax, -0x40(%rbp)
je 0x46bb
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 0x2200
jmp 0x48cd
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
callq 0x48e2
movq %rbx, %rdi
callq 0x2340
nop