// File: /workspace/github_cpp_makefile_star2_O3/jamesliu516[P]amrdg2d/spatial_discretization.cpp
// Function: spatial_discretize_v(bodygridclass*)
void spatial_discretize_v(OctCell* un)
{
   double tmp92=0.0;
   double tmp93=0.0;
   double tmp94=0.0;  
   double tmpx=0.0, tmpy=0.0;
   
   double uapp[4][3][3]={0.0};
   double dx,dy, dxpdy;
   dx=un->dx;
   dy=un->dy;
   dxpdy=dx*dy;
   
   for(short j=0; j<4; j++) {
      for(short imm=0; imm<3;imm++) {
	   for(short jmm=0; jmm<3; jmm++) {
	       uapp[j][jmm][imm]=approx_sol(gspt[jmm], gspt[imm], un->dof[j], nDOF);  //u(x,y)
	    }
	}
   }
	
  double pre[3][3]={0.0};
  double pre1[3][3]={0.0};	
  	
  for(short imm=0; imm<3;imm++) {
     for(short jmm=0; jmm<3; jmm++) {
	      pre[jmm][imm]=GAM11 *(uapp[3][jmm][imm]-0.5*(uapp[1][jmm][imm]*uapp[1][jmm][imm]/uapp[0][jmm][imm]
	          + uapp[2][jmm][imm]*uapp[2][jmm][imm]/uapp[0][jmm][imm]));	          
     }
  }
          
  double flxfunx[4][3][3]={0.0};
  double flxfuny[4][3][3]={0.0};
  
  for(short imm=0; imm<3;imm++) {
     for(short jmm=0; jmm<3; jmm++) {

	         flxfunx[0][jmm][imm]=uapp[1][jmm][imm]; 
	         flxfuny[0][jmm][imm]=uapp[2][jmm][imm]; 	
	                  
	         flxfunx[1][jmm][imm]=uapp[1][jmm][imm]*uapp[1][jmm][imm]/uapp[0][jmm][imm] + pre[jmm][imm];

	         flxfunx[2][jmm][imm] = flxfuny[1][jmm][imm] = uapp[1][jmm][imm]*uapp[2][jmm][imm]/uapp[0][jmm][imm];
	         
	         flxfuny[2][jmm][imm]=uapp[2][jmm][imm]*uapp[2][jmm][imm]/uapp[0][jmm][imm] + pre[jmm][imm];	
	         
	         flxfunx[3][jmm][imm] = (uapp[3][jmm][imm] + pre[jmm][imm])* uapp[1][jmm][imm]/uapp[0][jmm][imm];
	         flxfuny[3][jmm][imm] = (uapp[3][jmm][imm] + pre[jmm][imm])* uapp[2][jmm][imm]/uapp[0][jmm][imm];
	           	         	              	     
	      }
  }
  
//体积积分项
     
// euler equation for aerodynamics 
//uapp[j][jmm][imm]
    for(short j=0; j<4;++j) {
       tmp92=0.0;
	tmp93=0.0;
	tmp94=0.0;	
	tmpx=tmpy=0.0;	
	
//base function: phi_i(x)  &  psi_j(y)
	for(short imm=0; imm < 3;imm++) {
	   for(short jmm=0; jmm < 3; jmm++) {
	      tmp92 += flxfunx[j][jmm][imm]*wei[imm]*wei[jmm];
	      
	      tmp93 += flxfuny[j][jmm][imm]*wei[imm]*wei[jmm];
	      }
	 }	 

	 tmp92 *=  0.25*dxpdy;
	 tmp93 *=  0.25*dxpdy;	 
	       
        un->residual[j][1] -= 2.0/dx * tmp92;
        un->residual[j][2] -= 2.0/dy * tmp93;
        
        tmp92=tmp93=0.0;
  
  //base function: phi_i(x)*psi_j(y), phi_i(x)^2-1.0/3.0, &  psi_j(y)^2-1.0/3
     if(nDOF==6) {
	 for(short imm=0; imm<3;imm++) {
	   for(short jmm=0; jmm<3; jmm++) {
	      tmp93 += flxfuny[j][jmm][imm] *gspt[jmm]*wei[imm]*wei[jmm];   //fy*x 
	      
	      tmp94 += flxfunx[j][jmm][imm] *gspt[imm]*wei[imm]*wei[jmm];   //fx*y 
	      
	      tmpx += flxfunx[j][jmm][imm] *gspt[jmm]*wei[imm]*wei[jmm];  //fx*x
	      tmpy += flxfuny[j][jmm][imm] *gspt[imm]*wei[imm]*wei[jmm];	//fy*y      
	      }
	  }

	  tmp94 *= 0.25*dxpdy;
	  tmp93 *= 0.25*dxpdy;
	  tmpx *= 0.25*dxpdy;
	  tmpy *= 0.25*dxpdy;

	  un->residual[j][3] -= 2.0/dx * tmp94 + 2.0/dy * tmp93;
	
	  un->residual[j][4] -= 4.0/dx * tmpx;
	  un->residual[j][5] -= 4.0/dy * tmpy;  
	  	
       }
    } //end for
//=====
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
movq %rdi, %rbx
leaq 0x2d0(%rsp), %r13
xorl %r15d, %r15d
movl $0x120, %edx            # imm = 0x120
movq %r13, %rdi
xorl %esi, %esi
callq 0x3120
movups 0x288(%rbx), %xmm0
movaps %xmm0, 0x30(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x30(%rbx), %rax
movq %rax, 0x10(%rsp)
leaq 0xd153(%rip), %r12      # 0x1d830
movq %r15, 0x18(%rsp)
leaq (%r15,%r15,2), %r15
shlq $0x4, %r15
addq 0x10(%rsp), %r15
movq %r13, 0x20(%rsp)
xorl %ebp, %ebp
movsd (%r12,%rbp,8), %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %ebx, %ebx
movq %r13, %r14
movsd (%rbx,%r12), %xmm0
movsd 0x28(%rsp), %xmm1
movq %r15, %rdi
movl $0x6, %esi
callq 0xd904
movsd %xmm0, (%r14)
addq $0x18, %r14
addq $0x8, %rbx
cmpq $0x18, %rbx
jne 0x10707
incq %rbp
addq $0x8, %r13
cmpq $0x3, %rbp
jne 0x106f6
movq 0x18(%rsp), %r15
incq %r15
movq 0x20(%rsp), %r13
addq $0x48, %r13
cmpq $0x4, %r15
jne 0x106dd
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq $0x0, 0x40(%rax)
leaq 0x3a8(%rsp), %rcx
xorl %edx, %edx
movsd 0xc9b8(%rip), %xmm0     # 0x1d140
movsd 0xca18(%rip), %xmm1     # 0x1d1a8
movq 0x8(%rsp), %rbx
movq $-0x48, %rsi
movsd -0x90(%rcx,%rsi), %xmm2
movsd -0x48(%rcx,%rsi), %xmm3
movhpd (%rcx,%rsi), %xmm3      # xmm3 = xmm3[0],mem[0]
mulpd %xmm3, %xmm3
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
divpd %xmm2, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
addsd %xmm3, %xmm2
mulsd %xmm0, %xmm2
addsd 0x48(%rcx,%rsi), %xmm2
mulsd %xmm1, %xmm2
movsd %xmm2, 0x48(%rax,%rsi)
addq $0x18, %rsi
jne 0x1079c
incq %rdx
addq $0x8, %rax
addq $0x8, %rcx
cmpq $0x3, %rdx
jne 0x10795
leaq 0x1b0(%rsp), %r14
xorl %r13d, %r13d
movl $0x120, %edx            # imm = 0x120
movq %r14, %rdi
xorl %esi, %esi
callq 0x3120
leaq 0x90(%rsp), %r15
movl $0x120, %edx            # imm = 0x120
movq %r15, %rdi
xorl %esi, %esi
callq 0x3120
leaq 0x2d0(%rsp), %rax
leaq 0x40(%rsp), %rcx
xorl %edx, %edx
movsd 0x48(%rax,%rdx), %xmm0
movsd %xmm0, (%r14,%rdx)
movsd 0x90(%rax,%rdx), %xmm1
movsd %xmm1, (%r15,%rdx)
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
movsd (%rax,%rdx), %xmm3
divsd %xmm3, %xmm2
movsd (%rcx,%rdx), %xmm4
addsd %xmm4, %xmm2
movsd %xmm2, 0x48(%r14,%rdx)
movapd %xmm0, %xmm2
mulsd %xmm1, %xmm2
divsd %xmm3, %xmm2
movsd %xmm2, 0x48(%r15,%rdx)
movsd %xmm2, 0x90(%r14,%rdx)
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
divsd %xmm3, %xmm2
addsd %xmm4, %xmm2
movsd %xmm2, 0x90(%r15,%rdx)
addsd 0xd8(%rax,%rdx), %xmm4
mulsd %xmm4, %xmm0
divsd %xmm3, %xmm0
movsd %xmm0, 0xd8(%r14,%rdx)
mulsd %xmm1, %xmm4
divsd %xmm3, %xmm4
movsd %xmm4, 0xd8(%r15,%rdx)
addq $0x18, %rdx
cmpq $0x48, %rdx
jne 0x10833
incq %r13
addq $0x8, %r15
addq $0x8, %r14
addq $0x8, %rax
addq $0x8, %rcx
cmpq $0x3, %r13
jne 0x10831
movapd 0x30(%rsp), %xmm4
movapd %xmm4, %xmm0
unpckhpd %xmm4, %xmm0            # xmm0 = xmm0[1],xmm4[1]
mulsd %xmm4, %xmm0
mulsd 0xcea8(%rip), %xmm0     # 0x1d7c0
addq $0x2a0, %rbx            # imm = 0x2A0
movapd 0xcec9(%rip), %xmm1     # 0x1d7f0
divpd %xmm4, %xmm1
movapd 0xc76d(%rip), %xmm2     # 0x1d0a0
xorpd %xmm1, %xmm2
movapd 0xcec1(%rip), %xmm3     # 0x1d800
divpd %xmm4, %xmm3
movapd %xmm0, %xmm4
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
movapd %xmm1, %xmm5
unpckhpd %xmm1, %xmm5            # xmm5 = xmm5[1],xmm1[1]
xorl %eax, %eax
leaq 0xceb4(%rip), %rcx      # 0x1d810
xorl %edx, %edx
xorpd %xmm6, %xmm6
movq %rax, %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm7
unpcklpd %xmm7, %xmm7            # xmm7 = xmm7[0,0]
xorl %r8d, %r8d
movq %rsi, %r9
movsd 0x1b0(%rsp,%r9), %xmm8
movhpd 0x90(%rsp,%r9), %xmm8   # xmm8 = xmm8[0],mem[0]
mulpd %xmm7, %xmm8
movsd (%r8,%rcx), %xmm9
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
mulpd %xmm8, %xmm9
addpd %xmm9, %xmm6
addq $0x18, %r9
addq $0x8, %r8
cmpq $0x18, %r8
jne 0x10976
incq %rdi
addq $0x8, %rsi
cmpq $0x3, %rdi
jne 0x10967
mulpd %xmm4, %xmm6
mulpd %xmm2, %xmm6
leaq (%rdx,%rdx,2), %rdi
shlq $0x4, %rdi
leaq (%rbx,%rdi), %rsi
movupd 0x8(%rbx,%rdi), %xmm7
addpd %xmm6, %xmm7
movupd %xmm7, 0x8(%rbx,%rdi)
xorpd %xmm6, %xmm6
movq %rax, %rdi
xorl %r8d, %r8d
xorpd %xmm7, %xmm7
movsd (%rcx,%r8,8), %xmm8
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
movhpd (%r12,%r8,8), %xmm9     # xmm9 = xmm9[0],mem[0]
xorl %r9d, %r9d
movq %rdi, %r10
movsd 0x1b0(%rsp,%r10), %xmm10
movsd 0x90(%rsp,%r10), %xmm11
movsd (%r9,%r12), %xmm12
movapd %xmm9, %xmm13
movsd %xmm12, %xmm13          # xmm13 = xmm12[0],xmm13[1]
unpcklpd %xmm11, %xmm12          # xmm12 = xmm12[0],xmm11[0]
unpcklpd %xmm10, %xmm11          # xmm11 = xmm11[0],xmm10[0]
mulpd %xmm11, %xmm13
mulpd %xmm8, %xmm13
movsd (%r9,%rcx), %xmm11
unpcklpd %xmm11, %xmm11          # xmm11 = xmm11[0,0]
mulpd %xmm11, %xmm13
addpd %xmm13, %xmm7
shufpd $0x2, %xmm9, %xmm10     # xmm10 = xmm10[0],xmm9[1]
mulpd %xmm12, %xmm10
mulpd %xmm8, %xmm10
mulpd %xmm11, %xmm10
addpd %xmm10, %xmm6
addq $0x18, %r10
addq $0x8, %r9
cmpq $0x18, %r9
jne 0x10a08
incq %r8
addq $0x8, %rdi
cmpq $0x3, %r8
jne 0x109f1
movapd %xmm0, %xmm8
mulsd %xmm7, %xmm8
unpckhpd %xmm7, %xmm7            # xmm7 = xmm7[1,1]
mulsd %xmm0, %xmm7
mulpd %xmm4, %xmm6
mulpd %xmm3, %xmm6
mulsd %xmm5, %xmm8
mulsd %xmm1, %xmm7
addsd %xmm8, %xmm7
movsd 0x18(%rsi), %xmm8
subsd %xmm7, %xmm8
movsd %xmm8, 0x18(%rsi)
movupd 0x20(%rsi), %xmm7
addpd %xmm6, %xmm7
movupd %xmm7, 0x20(%rsi)
incq %rdx
addq $0x48, %rax
cmpq $0x4, %rdx
jne 0x1095e
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq