// File: /workspace/github_cpp_makefile_star2_O2/bauerj4[P]Riemann1D/src/main.cpp
// Function: main
int main(int argc, char * argv[])
{
  
  char * PATH = argv[1];
  vector<double> testVector(3,2.0);
  // Primative should invert conservative
  testVector = primativeTo1DConservative(testVector);
  printf("The conserved test components are: [%10.10f, %10.10f, %10.10f]\n", testVector[0],testVector[1], testVector[2]);
  //
  testVector = conservativeTo1DPrimative(testVector);
  printf("The primative components are: [%10.10f, %10.10f, %10.10f]\n", testVector[0],testVector[1], testVector[2]);
  
  
  // Get context.
  
  context_t RiemannContext;
  readContext(PATH, RiemannContext);
  printContext(RiemannContext);
  
  // Initialize simulation
  mesh_t mesh;
  printf("Constructing mesh...\n");
  ConstructMesh(mesh, RiemannContext);
  printf("Mesh constructed.\n");
  
  // Add more ghost cells for higher order
  /*
    if (RiemannContext.SOLUTION_METHOD == "HLLC_FLUX_SUPERBEE" || RiemannContext.SOLUTION_METHOD == "HLLC_FLUX_SUPERBEE")
    {
    double positions[mesh.NCells + 2];
    double pressures[mesh.NCells + 2];
    double velocities[mesh.NCells + 2];
    double denisities[mesh.NCells + 2];
    mesh.NCells = mesh.NCells + 2;
    for (int i = 0; i < mesh.NCells; i++)/ 	
    {
    if i == mesh.NCells - 1)
    {
    positions[i] = positions[i - 1];
    pressures[i] = pressures[i - 1];
    velocities[i] = velocities[i - 1];
    }
    }
    
    }
   */
  
  int snapshot_number = 0;
  PrintDataToFile(mesh, RiemannContext, snapshot_number);
  //return 1;
  // Declare flux vector
  vector<vector<double> > fluxes(mesh.NCells - 3, vector<double>(3,0.));
  
  if(RiemannContext.SOLUTION_METHOD == "HLLC")
    {
      fluxes.push_back(vector<double>(3,0.));
      fluxes.push_back(vector<double>(3,0.));
    }
  
  vector<vector<double> > primatives(mesh.NCells,vector<double> (3,0.));
  vector<vector<double> > conserved(mesh.NCells,vector<double> (3,0.));
  
  for (int i = 0; i < mesh.NCells; i++)
    {
      primatives[i][0] = mesh.FirstDensityElement[i];
      primatives[i][1] = mesh.FirstVelocityElement[i];
      primatives[i][2] = mesh.FirstPressureElement[i];
      //printf("%10.10f, %10.10f, %10.10f", primatives[i][0], primatives[i][1], primatives[i][2]);
    }
  
  for (int i = 0; i < mesh.NCells; i++)
    {
      conserved[i] = primativeTo1DConservative(primatives[i]);
    }
  
  double currentTime = 0;
  double SMAX;
  int iteration;
  // For time, do:
  while (currentTime < RiemannContext.EVOLVE_TIME)
    {
      // Compute fluxes with TRANSMISSIVE BCs
      if (RiemannContext.SOLUTION_METHOD == "HLLC")
 	{
 	  HLLC_FLUX(mesh, fluxes, SMAX);
 	}
      
      if (RiemannContext.SOLUTION_METHOD=="HLLC_FLUX_SUPERBEE")
 	{
 	  int success = 1;
 	  success = HLLC_FLUX_SUPERBEE(mesh, fluxes, RiemannContext, SMAX);
 	  printf("Flux calculation finished.\n");
 	  if (success != 0)
 	    {
 	      printf("RIEMANN SOLVER RETURNED WITH ERROR.\n");	      
PrintDataToFile(mesh, RiemannContext, snapshot_number);
 	      break;
 	    }
 	  printf("FLUX RETURN CODE %d\n",success);
 	}

      if (RiemannContext.SOLUTION_METHOD=="HLLC_FLUX_MINBEE")
        {
          int success = 1;
          success = HLLC_FLUX_MINBEE(mesh, fluxes, RiemannContext, SMAX);
          printf("Flux calculation finished.\n");
          if (success != 0)
            {
              printf("RIEMANN SOLVER RETURNED WITH ERROR.\n");
	      PrintDataToFile(mesh, RiemannContext, snapshot_number);
              break;
            }
          printf("FLUX RETURN CODE %d\n",success);
	}

       // Update mesh
      //vector<vector<double> > test(mesh.NCells - 1, vector<double>(3,0.));
       printf("Updating Mesh.\n");
      
      int success = FVUpdate(conserved, fluxes, mesh, RiemannContext, currentTime, SMAX, iteration);
      if (success != 0)
	{
	  printf("RIEMANN SOLVER RETURNED WITH ERROR.\n");
	  PrintDataToFile(mesh, RiemannContext, snapshot_number);
	  break;
	}


      for (int i = 0; i < mesh.NCells; i++)
	{
	  primatives[i] = conservativeTo1DPrimative(conserved[i]);
	  //printf("%10.10f, %10.10f, %10.10f", primatives[i][0], primatives[i][1], primatives[i][2]);
	  mesh.FirstDensityElement[i] = primatives[i][0];
	  mesh.FirstVelocityElement[i] = primatives[i][1];
	  mesh.FirstPressureElement[i] = primatives[i][2];
	}
      SMAX=0;

    }

  // Convert back to primatives
  /*
  for (int i = 0; i < mesh.NCells; i++)
    {
      primatives[i] = conservativeTo1DPrimative(conserved[i]);
      mesh.FirstDensityElement[i] = primatives[i][0];
      mesh.FirstVelocityElement[i] = primatives[i][1];
      mesh.FirstPressureElement[i] = primatives[i][2];
    }
  */


  // Write output.

  
  PrintDataToFile(mesh, RiemannContext, snapshot_number);

  return 0;
  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq 0x8(%rsi), %rbx
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
leaq 0xe8(%rsp), %rdx
movq %rax, (%rdx)
leaq 0xa0(%rsp), %r14
pushq $0x3
popq %rsi
leaq 0x40(%rsp), %rcx
movq %r14, %rdi
callq 0x7c0c
leaq 0xd0(%rsp), %rdi
movq %r14, %rsi
callq 0x7cb4
leaq 0xe8(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x32f1
leaq 0xa0(%rsp), %r14
leaq 0xe8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x810c
movq %r15, %rdi
callq 0x7f02
leaq 0xd0(%rsp), %rdi
callq 0x7f02
movq (%r14), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
leaq 0x1c4b(%rip), %rdi      # 0xa636
movb $0x3, %al
callq 0x2030
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x7cb4
leaq 0xe8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x33d5
leaq 0xa0(%rsp), %r14
leaq 0xe8(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x810c
movq %r15, %rdi
callq 0x7f02
leaq 0xb8(%rsp), %rdi
callq 0x7f02
movq (%r14), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movsd 0x10(%rax), %xmm2
leaq 0x1c17(%rip), %rdi      # 0xa676
movb $0x3, %al
callq 0x2030
leaq 0x158(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x178(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdi
callq 0x25b4
leaq 0xe8(%rsp), %rdi
callq 0x29b5
leaq 0x1c19(%rip), %rdi      # 0xa6cb
callq 0x22b0
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x8434
leaq 0x1c10(%rip), %rdi      # 0xa6e0
callq 0x22b0
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorl %edx, %edx
callq 0x884b
movslq 0x40(%rsp), %rbx
leaq 0x28(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x70(%rsp), %rcx
callq 0x7c0c
addq $-0x3, %rbx
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movq %rbx, %rsi
callq 0x7dcc
leaq 0x148(%rsp), %rbx
leaq 0x8(%rsp), %rdi
callq 0x7f02
leaq 0x1b71(%rip), %rsi      # 0xa6b1
movq %rbx, %rdi
callq 0x2b6b
testb %al, %al
je 0x8bba
leaq 0x28(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x70(%rsp), %rcx
callq 0x7c0c
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x90bc
leaq 0x8(%rsp), %rdi
callq 0x7f02
leaq 0x28(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x70(%rsp), %rcx
callq 0x7c0c
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x90bc
leaq 0x8(%rsp), %rdi
callq 0x7f02
movslq 0x40(%rsp), %rbx
leaq 0x70(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x28(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0x20(%rsp), %rcx
callq 0x7c0c
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %rsp, %rcx
movq %rbx, %rsi
callq 0x7dcc
leaq 0x28(%rsp), %rdi
callq 0x7f02
movslq 0x40(%rsp), %rbx
leaq 0x20(%rsp), %rdx
andq $0x0, (%rdx)
leaq 0x70(%rsp), %rdi
pushq $0x3
popq %rsi
movq %rsp, %rcx
callq 0x7c0c
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rdx
leaq 0x6c(%rsp), %rcx
movq %rbx, %rsi
callq 0x7dcc
leaq 0x70(%rsp), %rdi
callq 0x7f02
movl 0x40(%rsp), %eax
movq 0x50(%rsp), %rcx
movq 0x8(%rsp), %rdx
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdi
xorl %r8d, %r8d
testl %eax, %eax
movl $0x0, %r9d
cmovgl %eax, %r9d
cmpq %r8, %r9
je 0x8c93
movsd (%rcx,%r8,8), %xmm0
movq (%rdx), %r10
movsd %xmm0, (%r10)
movsd (%rsi,%r8,8), %xmm0
movsd %xmm0, 0x8(%r10)
movsd (%rdi,%r8,8), %xmm0
movsd %xmm0, 0x10(%r10)
incq %r8
addq $0x18, %rdx
jmp 0x8c5f
xorl %r15d, %r15d
leaq 0x1a0(%rsp), %rbx
leaq 0x70(%rsp), %r14
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x8cf5
movq 0x8(%rsp), %rsi
addq %r15, %rsi
movq %rbx, %rdi
callq 0x7cb4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x32f1
movq 0x28(%rsp), %rdi
addq %r15, %rdi
movq %r14, %rsi
callq 0x810c
movq %r14, %rdi
callq 0x7f02
movq %rbx, %rdi
callq 0x7f02
incq %r12
movl 0x40(%rsp), %eax
addq $0x18, %r15
jmp 0x8ca6
leaq 0x20(%rsp), %rax
andq $0x0, (%rax)
xorpd %xmm0, %xmm0
leaq 0x40(%rsp), %r12
leaq 0x88(%rsp), %r13
leaq 0x188(%rsp), %r14
leaq 0x70(%rsp), %rbx
movsd 0x140(%rsp), %xmm1
ucomisd %xmm0, %xmm1
leaq 0x148(%rsp), %r15
jbe 0x8f33
movq %r15, %rdi
leaq 0x1970(%rip), %rsi      # 0xa6b1
callq 0x2b6b
testb %al, %al
je 0x8d58
movq %r12, %rdi
movq %r13, %rsi
movq %rsp, %rdx
callq 0x34b2
movq %r15, %rdi
leaq 0x14e0(%rip), %rsi      # 0xa242
callq 0x2b6b
testb %al, %al
je 0x8da7
movq %r12, %rdi
movq %r13, %rsi
leaq 0xe8(%rsp), %rdx
movq %rsp, %rcx
callq 0x4023
movl %eax, %ebp
leaq 0x1968(%rip), %rdi      # 0xa6f2
callq 0x22b0
testl %ebp, %ebp
jne 0x8ef1
leaq 0x1918(%rip), %rdi      # 0xa6b6
xorl %esi, %esi
xorl %eax, %eax
callq 0x2030
movq %r15, %rdi
leaq 0x14a4(%rip), %rsi      # 0xa255
callq 0x2b6b
testb %al, %al
je 0x8df6
movq %r12, %rdi
movq %r13, %rsi
leaq 0xe8(%rsp), %rdx
movq %rsp, %rcx
callq 0x5e17
movl %eax, %ebp
leaq 0x1919(%rip), %rdi      # 0xa6f2
callq 0x22b0
testl %ebp, %ebp
jne 0x8f13
leaq 0x18c9(%rip), %rdi      # 0xa6b6
xorl %esi, %esi
xorl %eax, %eax
callq 0x2030
leaq 0x1910(%rip), %rdi      # 0xa70d
callq 0x22b0
movsd (%rsp), %xmm0
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
leaq 0xe8(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x6c(%rsp), %r9
callq 0x2e70
testl %eax, %eax
jne 0x8ecf
xorl %ebp, %ebp
xorl %r15d, %r15d
movslq 0x40(%rsp), %rax
cmpq %rax, %r15
jge 0x8ebf
movq 0x28(%rsp), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x7cb4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x33d5
movq 0x8(%rsp), %rdi
addq %rbp, %rdi
movq %rbx, %rsi
callq 0x810c
movq %rbx, %rdi
callq 0x7f02
movq %r14, %rdi
callq 0x7f02
movq 0x8(%rsp), %rax
movq (%rax,%rbp), %rax
movsd (%rax), %xmm0
movq 0x50(%rsp), %rcx
movsd %xmm0, (%rcx,%r15,8)
movsd 0x8(%rax), %xmm0
movq 0x60(%rsp), %rcx
movsd %xmm0, (%rcx,%r15,8)
movsd 0x10(%rax), %xmm0
movq 0x58(%rsp), %rax
movsd %xmm0, (%rax,%r15,8)
incq %r15
addq $0x18, %rbp
jmp 0x8e36
andq $0x0, (%rsp)
movsd 0x20(%rsp), %xmm0
jmp 0x8d1c
leaq 0x1846(%rip), %rdi      # 0xa71c
callq 0x22b0
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorl %edx, %edx
callq 0x884b
jmp 0x8f33
leaq 0x1824(%rip), %rdi      # 0xa71c
callq 0x22b0
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorl %edx, %edx
callq 0x884b
jmp 0x8f33
leaq 0x1802(%rip), %rdi      # 0xa71c
callq 0x22b0
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorl %edx, %edx
callq 0x884b
leaq 0x40(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
xorl %edx, %edx
callq 0x884b
leaq 0x28(%rsp), %rdi
callq 0x7eb4
leaq 0x8(%rsp), %rdi
callq 0x7eb4
leaq 0x88(%rsp), %rdi
callq 0x7eb4
leaq 0xe8(%rsp), %rdi
callq 0x909e
leaq 0xa0(%rsp), %rdi
callq 0x7f02
xorl %eax, %eax
addq $0x1b8, %rsp            # imm = 0x1B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9057
jmp 0x9057
jmp 0x9057
jmp 0x8fa9
jmp 0x8fe6
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x8fdc
jmp 0x8fe6
jmp 0x9057
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x7f02
jmp 0x9064
movq %rax, %rbx
jmp 0x9064
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7f02
jmp 0x906e
movq %rax, %rbx
jmp 0x906e
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x7f02
jmp 0x907b
jmp 0x9029
jmp 0x9029
jmp 0x9029
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x901d
jmp 0x9024
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x7f02
jmp 0x9088
movq %rax, %rbx
jmp 0x9088
movq %rax, %rbx
jmp 0x907b
jmp 0x9057
jmp 0x9057
jmp 0x9057
jmp 0x9057
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
jmp 0x9050
jmp 0x9057
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x7f02
jmp 0x905a
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x7eb4
leaq 0x8(%rsp), %rdi
callq 0x7eb4
leaq 0x88(%rsp), %rdi
callq 0x7eb4
leaq 0xe8(%rsp), %rdi
callq 0x909e
leaq 0xa0(%rsp), %rdi
callq 0x7f02
movq %rbx, %rdi
callq 0x2320
nop