// File: /workspace/github_cpp_makefile_star2_O2/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 $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
movl 0xf0(%rdi), %eax
incl %eax
movl %eax, -0x50(%rbp)
leaq 0xa0(%rdi), %rax
xorl %ecx, %ecx
leaq -0x160(%rbp), %r15
movq %rax, -0x40(%rbp)
cmpq $0x2, %rcx
je 0x40fb
movq %rax, -0x48(%rbp)
movq %rax, -0x30(%rbp)
xorl %r12d, %r12d
movq %rcx, -0x38(%rbp)
cmpq $0x2, %r12
je 0x40eb
xorl %r14d, %r14d
cmpq $0x40, %r14
je 0x40de
movsd 0x50(%rbx), %xmm0
movaps %xmm0, %xmm1
testq %r14, %r14
je 0x404c
movsd 0x70(%rbx), %xmm1
jne 0x4053
movsd 0x68(%rbx), %xmm0
movsd 0x58(%rbx), %xmm2
movaps %xmm2, %xmm3
testq %r12, %r12
je 0x4068
movsd 0x80(%rbx), %xmm3
jne 0x406f
movsd 0x78(%rbx), %xmm2
movsd 0x60(%rbx), %xmm4
movaps %xmm4, %xmm5
testq %rcx, %rcx
je 0x4084
movsd 0x90(%rbx), %xmm5
jne 0x408e
movsd 0x88(%rbx), %xmm4
movq %r15, %rdi
movl -0x50(%rbp), %esi
callq 0x47cc
leaq 0xf158(%rip), %rdi      # 0x131f8
callq 0x369a
movq %rbx, -0x78(%rbp)
movl $0xf8, %edi
callq 0x21e0
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x4620
movq -0x30(%rbp), %rax
movq %r13, (%rax,%r14)
movq %r15, %rdi
callq 0x45f2
addq $0x20, %r14
movq -0x38(%rbp), %rcx
jmp 0x4030
incq %r12
addq $0x10, -0x30(%rbp)
jmp 0x4023
incq %rcx
movq -0x48(%rbp), %rax
addq $0x8, %rax
jmp 0x400a
leaq 0xc0(%rbx), %r15
leaq 0xb0(%rbx), %rcx
leaq 0xa8(%rbx), %rax
movq %rax, -0x38(%rbp)
xorl %esi, %esi
xorl %edi, %edi
movq %r15, -0x30(%rbp)
cmpq $0x2, %rdi
je 0x42a7
xorl %r8d, %r8d
movq -0x38(%rbp), %r9
xorl %r10d, %r10d
cmpq $0x2, %r10
je 0x4297
movq %r10, %r11
shlq $0x4, %r11
leaq (%r15,%r11), %rdx
leaq (%rdx,%rdi,8), %r14
movq -0x40(%rbp), %rax
leaq (%rax,%r11), %rdx
leaq (%rdx,%rdi,8), %r15
xorl %r12d, %r12d
xorl %r13d, %r13d
testq %r13, %r13
je 0x416b
cmpq $0x2, %r13
jne 0x4190
jmp 0x4283
movq (%r14), %rdx
movq (%r15), %rax
movq %rdx, 0x8(%rax)
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0x4190
addq %r11, %rdx
movq 0xc0(%rdx,%rdi,8), %rax
movq (%r15), %rdx
movq %rax, 0x10(%rdx)
testq %r10, %r10
jne 0x41bf
movq -0x10(%rcx,%r12), %rax
movq (%rcx,%r12), %rdx
movq %rdx, 0x18(%rax)
movq 0x20(%rbx), %rdx
testq %rdx, %rdx
je 0x41bf
addq %rsi, %rdx
movq 0xb0(%r12,%rdx), %rax
movq -0x10(%rcx,%r12), %rdx
movq %rax, 0x20(%rdx)
testq %rdi, %rdi
jne 0x41ee
movq -0x8(%r9,%r12), %rax
movq (%r9,%r12), %rdx
movq %rdx, 0x28(%rax)
movq 0x30(%rbx), %rdx
testq %rdx, %rdx
je 0x41ee
addq %r8, %rdx
movq 0xa8(%r12,%rdx), %rax
movq -0x8(%r9,%r12), %rdx
movq %rax, 0x30(%rdx)
cmpq $0x20, %r12
jne 0x4219
movq (%r15), %rax
movq (%r14), %rdx
movq %rax, 0x10(%rdx)
movq 0x8(%rbx), %rdx
testq %rdx, %rdx
je 0x4219
addq %r11, %rdx
movq 0xa0(%rdx,%rdi,8), %rax
movq (%r14), %rdx
movq %rax, 0x8(%rdx)
cmpq $0x1, %r10
jne 0x4248
movq -0x10(%rcx,%r12), %rax
movq (%rcx,%r12), %rdx
movq %rax, 0x20(%rdx)
movq 0x18(%rbx), %rdx
testq %rdx, %rdx
je 0x4248
addq %rsi, %rdx
movq 0xa0(%r12,%rdx), %rax
movq (%rcx,%r12), %rdx
movq %rax, 0x18(%rdx)
cmpq $0x1, %rdi
jne 0x4277
movq -0x8(%r9,%r12), %rax
movq (%r9,%r12), %rdx
movq %rax, 0x30(%rdx)
movq 0x28(%rbx), %rdx
testq %rdx, %rdx
je 0x4277
addq %r8, %rdx
movq 0xa0(%r12,%rdx), %rax
movq (%r9,%r12), %rdx
movq %rax, 0x28(%rdx)
incq %r13
addq $0x20, %r12
jmp 0x415b
incq %r10
addq $0x10, %r9
addq $0x10, %r8
movq -0x30(%rbp), %r15
jmp 0x4130
incq %rdi
addq $0x8, %rcx
addq $0x8, %rsi
jmp 0x411c
movl $0xb0, %ecx
xorl %edx, %edx
cmpq $0x2, %rdx
je 0x4398
movl $0xa8, %esi
xorl %edi, %edi
cmpq $0x2, %rdi
je 0x438c
movq %rdi, %rax
shlq $0x4, %rax
movq -0x40(%rbp), %r8
addq %rax, %r8
leaq (%r8,%rdx,8), %r8
addq %r15, %rax
leaq (%rax,%rdx,8), %r9
movq %rbx, %r10
xorl %r11d, %r11d
testq %r11, %r11
je 0x42f8
cmpq $0x2, %r11
jne 0x4307
jmp 0x4380
movq (%r8), %rax
andl $0x0, 0x38(%rax)
movl 0x3c(%rbx), %r14d
movl %r14d, 0x3c(%rax)
testq %rdi, %rdi
jne 0x431d
movq -0x10(%r10,%rcx), %rax
andl $0x0, 0x40(%rax)
movl 0x44(%rbx), %r14d
movl %r14d, 0x44(%rax)
testq %rdx, %rdx
jne 0x4333
movq -0x8(%r10,%rsi), %rax
andl $0x0, 0x48(%rax)
movl 0x4c(%rbx), %r14d
movl %r14d, 0x4c(%rax)
cmpq $0x1, %r11
jne 0x4348
movq (%r9), %rax
andl $0x0, 0x3c(%rax)
movl 0x38(%rbx), %r14d
movl %r14d, 0x38(%rax)
cmpq $0x1, %rdi
jne 0x435e
movq (%r10,%rcx), %rax
andl $0x0, 0x44(%rax)
movl 0x40(%rbx), %r14d
movl %r14d, 0x40(%rax)
cmpq $0x1, %rdx
jne 0x4374
movq (%r10,%rsi), %rax
andl $0x0, 0x4c(%rax)
movl 0x48(%rbx), %r14d
movl %r14d, 0x48(%rax)
incq %r11
addq $0x20, %r10
jmp 0x42e8
incq %rdi
addq $0x10, %rsi
jmp 0x42bf
incq %rdx
addq $0x8, %rcx
jmp 0x42ae
leaq 0xf279(%rip), %rax      # 0x13618
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x5, %rcx
movq %rcx, -0x50(%rbp)
leaq 0xf27b(%rip), %rax      # 0x13630
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x5, %rcx
movq %rcx, -0x48(%rbp)
xorl %ecx, %ecx
pushq $0x3
popq %rax
pushq $0x2
popq %r8
cmpq $0x2, %rcx
je 0x45b7
xorl %edx, %edx
movq %rcx, -0x58(%rbp)
cmpq $0x2, %rdx
je 0x45af
movq %rdx, %rsi
shlq $0x4, %rsi
movq %rsi, -0x60(%rbp)
xorl %esi, %esi
movq %rdx, -0x68(%rbp)
cmpq $0x2, %rsi
je 0x45a7
movq %rsi, %rdx
shlq $0x5, %rdx
addq -0x40(%rbp), %rdx
addq -0x60(%rbp), %rdx
leaq (%rdx,%rcx,8), %rcx
movq %rcx, -0x38(%rbp)
xorl %edx, %edx
movq %rsi, -0x30(%rbp)
cmpq -0x50(%rbp), %rdx
je 0x4499
movq -0x38(%rbp), %rcx
movq (%rcx), %r9
pushq $0x38
popq %r10
xorl %r11d, %r11d
cmpq $0x3, %r11
je 0x4490
movq 0xe0(%rbx), %r15
movq %r8, %r12
movq %r10, %r13
subq $0x1, %r12
jb 0x4487
movl (%r9,%r13), %edi
testl %edi, %edi
je 0x446e
cmpl $0x2, %edi
je 0x446c
cmpl $0x1, %edi
jne 0x4481
movq 0x10(%r15), %rsi
movq (%rsi,%rdx,8), %rsi
movl (%rsi,%r13), %edi
jmp 0x446e
movl %eax, %edi
movq 0xe0(%r9), %rsi
movq 0x10(%rsi), %rsi
movq (%rsi,%rdx,8), %rsi
movl %edi, (%rsi,%r13)
addq $0x4, %r13
jmp 0x4446
incq %r11
addq $0x8, %r10
jmp 0x4433
incq %rdx
movq -0x30(%rbp), %rsi
jmp 0x441f
xorl %r15d, %r15d
cmpq -0x48(%rbp), %r15
je 0x4597
movq -0x38(%rbp), %rcx
movq (%rcx), %r12
leaq 0x38(%r12), %r13
xorl %r9d, %r9d
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x458b
movq 0xe0(%rbx), %r10
xorl %r11d, %r11d
cmpq $0x8, %r11
je 0x457b
movl (%r13,%r11), %esi
testl %esi, %esi
je 0x4548
cmpl $0x2, %esi
je 0x4526
cmpl $0x1, %esi
jne 0x4572
movq 0x18(%r10), %rsi
movq (%rsi,%r15,8), %rsi
addq %r9, %rsi
movl 0x48(%r11,%rsi), %ecx
movq 0xe0(%r12), %rdi
movq 0x18(%rdi), %rdi
movq (%rdi,%r15,8), %rdi
leaq (%rdi,%r9), %r14
movl %ecx, 0x48(%r11,%r14)
movl 0x60(%r11,%rsi), %ecx
movl %ecx, 0x60(%r11,%r14)
movl 0x78(%r11,%rsi), %esi
jmp 0x456a
movq 0xe0(%r12), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%r15,8), %rdi
leaq (%rdi,%r9), %rcx
movl %eax, 0x48(%r11,%rcx)
movl %eax, 0x60(%r11,%rcx)
movl %eax, %esi
jmp 0x456a
movq 0xe0(%r12), %rcx
movq 0x18(%rcx), %rcx
movq (%rcx,%r15,8), %rdi
leaq (%rdi,%r9), %rcx
andl $0x0, 0x48(%r11,%rcx)
andl $0x0, 0x60(%r11,%rcx)
xorl %esi, %esi
addq %r9, %rdi
movl %esi, 0x78(%r11,%rdi)
addq $0x4, %r11
jmp 0x44cb
incq %rdx
addq $0x8, %r9
addq $0x8, %r13
jmp 0x44b7
incq %r15
movq -0x30(%rbp), %rsi
jmp 0x449c
incq %rsi
movq -0x58(%rbp), %rcx
movq -0x68(%rbp), %rdx
jmp 0x43f8
incq %rdx
jmp 0x43dd
incq %rcx
jmp 0x43cd
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x45de
movq %rax, %rbx
movl $0xf8, %esi
movq %r13, %rdi
callq 0x21f0
leaq -0x160(%rbp), %rdi
callq 0x45f2
movq %rbx, %rdi
callq 0x2320