void CPrimalGrid::SetCoord_CG(su2double **val_coord) {
	unsigned short iDim, iNode, NodeFace, iFace;
	
  AD::StartPreacc();
  AD::SetPreaccIn(val_coord, GetnNodes(), nDim);

	for (iDim = 0; iDim < nDim; iDim++) {
		Coord_CG[iDim] = 0.0;
		for (iNode = 0; iNode < GetnNodes();  iNode++)
			Coord_CG[iDim] += val_coord[iNode][iDim]/su2double(GetnNodes());
	}
	
	for (iFace = 0; iFace < GetnFaces();  iFace++)
		for (iDim = 0; iDim < nDim; iDim++) {
			Coord_FaceElems_CG[iFace][iDim] = 0.0;
			for (iNode = 0; iNode < GetnNodesFace(iFace); iNode++) {
				NodeFace = GetFaces(iFace, iNode);
				Coord_FaceElems_CG[iFace][iDim] += val_coord[NodeFace][iDim]/su2double(GetnNodesFace(iFace));
			}
		}

  AD::SetPreaccOut(Coord_CG, nDim);
  AD::SetPreaccOut(Coord_FaceElems_CG, GetnFaces(), nDim);
  AD::EndPreacc();

}
void CCentLaxArtComp_Flow::ComputeResidual(su2double *val_residual, su2double **val_Jacobian_i, su2double **val_Jacobian_j,
                                           CConfig *config) {
  
  su2double U_i[4] = {0.0,0.0,0.0,0.0}, U_j[4] = {0.0,0.0,0.0,0.0};

  /*--- Conservative variables at point i and j ---*/
  
  Pressure_i =    V_i[0];       Pressure_j = V_j[0];
  DensityInc_i =  V_i[nDim+1];  DensityInc_j = V_j[nDim+1];
  BetaInc2_i =    V_i[nDim+2];  BetaInc2_j = V_j[nDim+2];
  sq_vel_i = 0.0; sq_vel_j = 0.0;
  for (iDim = 0; iDim < nDim; iDim++) {
    Velocity_i[iDim] = V_i[iDim+1];
    Velocity_j[iDim] = V_j[iDim+1];
    sq_vel_i += 0.5*Velocity_i[iDim]*Velocity_i[iDim];
    sq_vel_j += 0.5*Velocity_j[iDim]*Velocity_j[iDim];
  }
  
  /*--- Recompute conservative variables ---*/

  U_i[0] = Pressure_i; U_j[0] = Pressure_j;
  for (iDim = 0; iDim < nDim; iDim++) {
    U_i[iDim+1] = DensityInc_i*Velocity_i[iDim]; U_j[iDim+1] = DensityInc_j*Velocity_j[iDim];
  }
  
  /*--- Compute mean values of the variables ---*/
  
  MeanDensity = 0.5*(DensityInc_i+DensityInc_j);
  MeanPressure = 0.5*(Pressure_i+Pressure_j);
  MeanBetaInc2 = 0.5*(BetaInc2_i+BetaInc2_j);
  for (iDim = 0; iDim < nDim; iDim++)
    MeanVelocity[iDim] =  0.5*(Velocity_i[iDim]+Velocity_j[iDim]);
  
  /*--- Get projected flux tensor ---*/
  
  GetInviscidArtCompProjFlux(&MeanDensity, MeanVelocity, &MeanPressure, &MeanBetaInc2, Normal, ProjFlux);
  
  /*--- Compute inviscid residual ---*/
  
  for (iVar = 0; iVar < nVar; iVar++)
    val_residual[iVar] = ProjFlux[iVar];
  
  /*--- Jacobians of the inviscid flux ---*/
  
  if (implicit) {
    GetInviscidArtCompProjJac(&MeanDensity, MeanVelocity, &MeanBetaInc2, Normal, 0.5, val_Jacobian_i);
    for (iVar = 0; iVar < nVar; iVar++)
      for (jVar = 0; jVar < nVar; jVar++)
        val_Jacobian_j[iVar][jVar] = val_Jacobian_i[iVar][jVar];
  }
  
  /*--- Computes differences btw. conservative variables ---*/
  
  for (iVar = 0; iVar < nVar; iVar++)
    Diff_U[iVar] = U_i[iVar]-U_j[iVar];
  
  /*--- Compute the local espectral radius and the stretching factor ---*/
  
  ProjVelocity_i = 0; ProjVelocity_j = 0; Area = 0.0;
  for (iDim = 0; iDim < nDim; iDim++) {
    ProjVelocity_i += Velocity_i[iDim]*Normal[iDim];
    ProjVelocity_j += Velocity_j[iDim]*Normal[iDim];
    Area += Normal[iDim]*Normal[iDim];
  }
  Area = sqrt(Area);
  
  SoundSpeed_i = sqrt(ProjVelocity_i*ProjVelocity_i + (BetaInc2_i/DensityInc_i)*Area*Area);
  SoundSpeed_j = sqrt(ProjVelocity_j*ProjVelocity_j + (BetaInc2_j/DensityInc_j)*Area*Area);
  
  Local_Lambda_i = fabs(ProjVelocity_i)+SoundSpeed_i;
  Local_Lambda_j = fabs(ProjVelocity_j)+SoundSpeed_j;
  MeanLambda = 0.5*(Local_Lambda_i + Local_Lambda_j);
  
  Phi_i = pow(Lambda_i/(4.0*MeanLambda), Param_p);
  Phi_j = pow(Lambda_j/(4.0*MeanLambda), Param_p);
  StretchingFactor = 4.0*Phi_i*Phi_j/(Phi_i+Phi_j);
  
  sc0 = 3.0*(su2double(Neighbor_i)+su2double(Neighbor_j))/(su2double(Neighbor_i)*su2double(Neighbor_j));
  Epsilon_0 = Param_Kappa_0*sc0*su2double(nDim)/3.0;
  
  /*--- Compute viscous part of the residual ---*/
  for (iVar = 0; iVar < nVar; iVar++)
    val_residual[iVar] += Epsilon_0*Diff_U[iVar]*StretchingFactor*MeanLambda;
  
  if (implicit) {
    for (iVar = 0; iVar < nVar; iVar++) {
      val_Jacobian_i[iVar][iVar] += Epsilon_0*StretchingFactor*MeanLambda;
      val_Jacobian_j[iVar][iVar] -= Epsilon_0*StretchingFactor*MeanLambda;
    }
  }
  
}
void CCentJST_LinFlow::ComputeResidual (su2double *val_resconv, su2double *val_resvisc, su2double **val_Jacobian_i,
                                    su2double **val_Jacobian_j, CConfig *config) {
  
	/*--- Mean Values of the linealized variables ---*/
	MeanDeltaRho =  0.5*(DeltaU_i[0]+DeltaU_j[0]);
	for (iDim = 0; iDim < nDim; iDim++)
		MeanDeltaVel[iDim] =  0.5*(DeltaU_i[iDim+1]+DeltaU_j[iDim+1]);
	MeanDeltaE =  0.5*(DeltaU_i[nVar-1]+DeltaU_j[nVar-1]);
  
	/*--- Values of the flow variables at point i ---*/
	Density_i = U_i[0];
	ProjVelocity_i = 0; Area = 0;
	sq_vel = 0;
	for (iDim = 0; iDim < nDim; iDim++) {
		Velocity_i[iDim] = U_i[iDim+1] / Density_i;
		ProjVelocity_i += Velocity_i[iDim]*Normal[iDim];
		sq_vel += Velocity_i[iDim]*Velocity_i[iDim];
		Area += Normal[iDim]*Normal[iDim];
	}
	Area = sqrt(Area);
	DensityEnergy_i = U_i[nDim+1];
	Energy_i = DensityEnergy_i / Density_i;
	SoundSpeed_i = sqrt(Gamma*Gamma_Minus_One*(Energy_i-0.5*sq_vel));
	Pressure_i = (SoundSpeed_i * SoundSpeed_i * Density_i) / Gamma;
	Enthalpy_i = (DensityEnergy_i + Pressure_i) / Density_i;
  
	/*--- Values of the flow variables at point j ---*/
	Density_j = U_j[0];
	ProjVelocity_j = 0;
	sq_vel = 0;
	for (iDim = 0; iDim < nDim; iDim++) {
		Velocity_j[iDim] = U_j[iDim+1] / Density_j;
		ProjVelocity_j += Velocity_j[iDim]*Normal[iDim];
		sq_vel += Velocity_j[iDim]*Velocity_j[iDim];
	}
	DensityEnergy_j = U_j[nDim+1];
	Energy_j = DensityEnergy_j / Density_j;
	SoundSpeed_j = sqrt(Gamma*Gamma_Minus_One*(Energy_j-0.5*sq_vel));
	Pressure_j = (SoundSpeed_j * SoundSpeed_j * Density_j) / Gamma;
	Enthalpy_j = (DensityEnergy_j + Pressure_j) / Density_j;
  
	/*--- Mean values the flow variables ---*/
	MeanDensity = 0.5*(Density_i + Density_j);
	for (iDim = 0; iDim < nDim; iDim++) MeanVelocity[iDim] = 0.5*(Velocity_j[iDim] + Velocity_i[iDim]);
	MeanPressure = 0.5*(Pressure_i + Pressure_j);
	MeanEnthalpy = 0.5*(Enthalpy_j+Enthalpy_i);
	MeanEnergy = 0.5*(Energy_j+Energy_i);
  
	/*--- Compute projected inviscid Jacobian (scale = 0.5 because val_resconv ~ 0.5*(fc_i+fc_j)*Normal) ---*/
	GetInviscidProjJac(Velocity_i, &Energy_i, Normal, 0.5, Jacobian_i);
	GetInviscidProjJac(Velocity_j, &Energy_j, Normal, 0.5, Jacobian_j);
  
	/*--- Compute inviscid flux $Jacobian x DeltaU$ ---*/
  
	for (iVar = 0; iVar < nVar; iVar++) {
		val_resconv[iVar] = 0.0;
		for (jVar = 0; jVar < nVar; jVar++)
			val_resconv[iVar] += Jacobian_i[iVar][jVar] * DeltaU_i[jVar] + Jacobian_j[iVar][jVar] * DeltaU_j[jVar];
	}
  
	/*--- Computes differences btw. variables and Laplacians ---*/
	for (iVar = 0; iVar < nVar; iVar++)
		Diff_Lapl[iVar] = Und_Lapl_i[iVar]-Und_Lapl_j[iVar];
  
	/*--- Calcula el radio espectral local, Factor de stretching factor ---*/
	Local_Lambda_i = (fabs(ProjVelocity_i)+SoundSpeed_i*Area);
	Local_Lambda_j = (fabs(ProjVelocity_j)+SoundSpeed_j*Area);
	MeanLambda = 0.5*(Local_Lambda_i+Local_Lambda_j);
  
	Phi_i = pow(0.5*max(0.0,(Lambda_i - MeanLambda)/(MeanLambda)), Param_p);
	Phi_j = pow(0.5*max(0.0,(Lambda_j - MeanLambda)/(MeanLambda)), Param_p);
	StretchingFactor = 4.0*Phi_i*Phi_j/(Phi_i+Phi_j);
  
	sc4 = 9.0/(su2double(Neighbor_i*(1+Neighbor_i))) + 9.0/(su2double(Neighbor_j*(1+Neighbor_j)));
  
	Epsilon_4 = Param_Kappa_4*sc4;
  
	for (iVar = 0; iVar < nVar; iVar++)
		val_resvisc[iVar] = -Epsilon_4*Diff_Lapl[iVar]*StretchingFactor*MeanLambda;
  
}