xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision e334ad8fca0b75b5d678f79cfd10f4127eb21e08)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
388b783a1SJames Wright //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
588b783a1SJames Wright //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
788b783a1SJames Wright 
888b783a1SJames Wright /// @file
988b783a1SJames Wright /// Operator for Navier-Stokes example using PETSc
1088b783a1SJames Wright 
1188b783a1SJames Wright 
1288b783a1SJames Wright #ifndef newtonian_h
1388b783a1SJames Wright #define newtonian_h
1488b783a1SJames Wright 
1588b783a1SJames Wright #include <math.h>
1688b783a1SJames Wright #include <ceed.h>
17841e4c73SJed Brown #include "newtonian_types.h"
1888b783a1SJames Wright 
1988b783a1SJames Wright #ifndef M_PI
2088b783a1SJames Wright #define M_PI    3.14159265358979323846
2188b783a1SJames Wright #endif
2288b783a1SJames Wright 
235c677226SJed Brown typedef struct {
245c677226SJed Brown   CeedScalar pressure;
255c677226SJed Brown   CeedScalar velocity[3];
265c677226SJed Brown   CeedScalar temperature;
275c677226SJed Brown } StatePrimitive;
285c677226SJed Brown 
295c677226SJed Brown typedef struct {
305c677226SJed Brown   CeedScalar density;
315c677226SJed Brown   CeedScalar momentum[3];
325c677226SJed Brown   CeedScalar E_total;
335c677226SJed Brown } StateConservative;
345c677226SJed Brown 
355c677226SJed Brown typedef struct {
365c677226SJed Brown   StateConservative U;
375c677226SJed Brown   StatePrimitive Y;
385c677226SJed Brown } State;
395c677226SJed Brown 
405c677226SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
415c677226SJed Brown   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
425c677226SJed Brown   StatePrimitive Y;
435c677226SJed Brown   for (int i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
445c677226SJed Brown   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
455c677226SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
465c677226SJed Brown   CeedScalar e_total = U.E_total / U.density;
475c677226SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
485c677226SJed Brown   Y.temperature = e_internal / gas->cv;
495c677226SJed Brown   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
505c677226SJed Brown   return Y;
515c677226SJed Brown }
525c677226SJed Brown 
535c677226SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
545c677226SJed Brown   NewtonianIdealGasContext gas, State s, StateConservative dU,
555c677226SJed Brown   const CeedScalar x[3], const CeedScalar dx[3]) {
565c677226SJed Brown   StatePrimitive dY;
575c677226SJed Brown   for (int i=0; i<3; i++) {
585c677226SJed Brown     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
595c677226SJed Brown   }
605c677226SJed Brown   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
615c677226SJed Brown   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
625c677226SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
635c677226SJed Brown   CeedScalar de_potential = -Dot3(gas->g, dx);
645c677226SJed Brown   CeedScalar e_total = s.U.E_total / s.U.density;
655c677226SJed Brown   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
665c677226SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
675c677226SJed Brown   CeedScalar de_internal = de_total - de_kinetic - de_potential;
685c677226SJed Brown   dY.temperature = de_internal / gas->cv;
695c677226SJed Brown   dY.pressure = (gas->cp / gas->cv - 1)
705c677226SJed Brown                 * (dU.density * e_internal + s.U.density * de_internal);
715c677226SJed Brown   return dY;
725c677226SJed Brown }
735c677226SJed Brown 
745c677226SJed Brown CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
755c677226SJed Brown                                        const CeedScalar U[5], const CeedScalar x[3]) {
765c677226SJed Brown   State s;
775c677226SJed Brown   s.U.density = U[0];
785c677226SJed Brown   s.U.momentum[0] = U[1];
795c677226SJed Brown   s.U.momentum[1] = U[2];
805c677226SJed Brown   s.U.momentum[2] = U[3];
815c677226SJed Brown   s.U.E_total = U[4];
825c677226SJed Brown   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
835c677226SJed Brown   return s;
845c677226SJed Brown }
855c677226SJed Brown 
866f00d0e6SJed Brown CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
876f00d0e6SJed Brown     State s, const CeedScalar dU[5],
886f00d0e6SJed Brown     const CeedScalar x[3], const CeedScalar dx[3]) {
896f00d0e6SJed Brown   State ds;
906f00d0e6SJed Brown   ds.U.density = dU[0];
916f00d0e6SJed Brown   ds.U.momentum[0] = dU[1];
926f00d0e6SJed Brown   ds.U.momentum[1] = dU[2];
936f00d0e6SJed Brown   ds.U.momentum[2] = dU[3];
946f00d0e6SJed Brown   ds.U.E_total = dU[4];
956f00d0e6SJed Brown   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
966f00d0e6SJed Brown   return ds;
976f00d0e6SJed Brown }
986f00d0e6SJed Brown 
995c677226SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
1005c677226SJed Brown                                         StateConservative Flux[3]) {
1015c677226SJed Brown   for (int i=0; i<3; i++) {
1025c677226SJed Brown     Flux[i].density = s.U.momentum[i];
1035c677226SJed Brown     for (int j=0; j<3; j++)
1045c677226SJed Brown       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
1055c677226SJed Brown                             + s.Y.pressure * (i == j);
1065c677226SJed Brown     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
1075c677226SJed Brown   }
1085c677226SJed Brown }
1095c677226SJed Brown 
1105c677226SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
1115c677226SJed Brown     State s, State ds, StateConservative dFlux[3]) {
1125c677226SJed Brown   for (int i=0; i<3; i++) {
1135c677226SJed Brown     dFlux[i].density = ds.U.momentum[i];
1145c677226SJed Brown     for (int j=0; j<3; j++)
1155c677226SJed Brown       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
1165c677226SJed Brown                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
1175c677226SJed Brown     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
1185c677226SJed Brown                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
1195c677226SJed Brown   }
1205c677226SJed Brown }
1215c677226SJed Brown 
1225c677226SJed Brown // Kelvin-Mandel notation
1235c677226SJed Brown CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
1245c677226SJed Brown                                         CeedScalar strain_rate[6]) {
1255c677226SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
1265c677226SJed Brown   strain_rate[0] = grad_s[0].Y.velocity[0];
1275c677226SJed Brown   strain_rate[1] = grad_s[1].Y.velocity[1];
1285c677226SJed Brown   strain_rate[2] = grad_s[2].Y.velocity[2];
1295c677226SJed Brown   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
1305c677226SJed Brown   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
1315c677226SJed Brown   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
1325c677226SJed Brown }
1335c677226SJed Brown 
1345c677226SJed Brown CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
1355c677226SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
1365c677226SJed Brown   A[0][0] = v[0];
1375c677226SJed Brown   A[1][1] = v[1];
1385c677226SJed Brown   A[2][2] = v[2];
1395c677226SJed Brown   A[2][1] = A[1][2] = weight * v[3];
1405c677226SJed Brown   A[2][0] = A[0][2] = weight * v[4];
1415c677226SJed Brown   A[1][0] = A[0][1] = weight * v[5];
1425c677226SJed Brown }
1435c677226SJed Brown 
1445c677226SJed Brown CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
1455c677226SJed Brown     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
1465c677226SJed Brown   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
1475c677226SJed Brown   for (int i=0; i<6; i++) {
1485c677226SJed Brown     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
1495c677226SJed Brown   }
1505c677226SJed Brown }
1515c677226SJed Brown 
1525c677226SJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
1535c677226SJed Brown     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
1545c677226SJed Brown     CeedScalar Fe[3]) {
1555c677226SJed Brown   for (int i=0; i<3; i++) {
1565c677226SJed Brown     Fe[i] = - Y.velocity[0] * stress[0][i]
1575c677226SJed Brown             - Y.velocity[1] * stress[1][i]
1585c677226SJed Brown             - Y.velocity[2] * stress[2][i]
1595c677226SJed Brown             - gas->k * grad_s[i].Y.temperature;
1605c677226SJed Brown   }
1615c677226SJed Brown }
1625c677226SJed Brown 
163*e334ad8fSJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
164*e334ad8fSJed Brown     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
165*e334ad8fSJed Brown     const CeedScalar stress[3][3],
166*e334ad8fSJed Brown     const CeedScalar dstress[3][3],
167*e334ad8fSJed Brown     CeedScalar dFe[3]) {
168*e334ad8fSJed Brown   for (int i=0; i<3; i++) {
169*e334ad8fSJed Brown     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
170*e334ad8fSJed Brown              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
171*e334ad8fSJed Brown              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
172*e334ad8fSJed Brown              - gas->k * grad_ds[i].Y.temperature;
173*e334ad8fSJed Brown   }
174*e334ad8fSJed Brown }
17588b783a1SJames Wright // *****************************************************************************
17688b783a1SJames Wright // Helper function for computing flux Jacobian
17788b783a1SJames Wright // *****************************************************************************
17888b783a1SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5],
17988b783a1SJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
18088626eedSJames Wright     const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) {
18188b783a1SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
18288626eedSJames Wright   CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
18388b783a1SJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
18488b783a1SJames Wright     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
18588626eedSJames Wright       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) -
18688626eedSJames Wright                       u[i]*u[j];
18788b783a1SJames Wright       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
18888b783a1SJames Wright         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
18988b783a1SJames Wright         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
19088b783a1SJames Wright                           ((i==k) ? u[j] : 0.) -
19188b783a1SJames Wright                           ((i==j) ? u[k] : 0.) * (gamma-1.);
19288b783a1SJames Wright         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
19388b783a1SJames Wright                           (gamma-1.)*u[i]*u[k];
19488b783a1SJames Wright       }
19588b783a1SJames Wright       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
19688b783a1SJames Wright     }
19788b783a1SJames Wright     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
19888b783a1SJames Wright     dF[i][4][4] = u[i] * gamma;
19988b783a1SJames Wright   }
20088b783a1SJames Wright }
20188b783a1SJames Wright 
20288b783a1SJames Wright // *****************************************************************************
20388626eedSJames Wright // Helper function for computing flux Jacobian of Primitive variables
20488626eedSJames Wright // *****************************************************************************
20588626eedSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5],
20688626eedSJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
20788626eedSJames Wright     const CeedScalar Rd, const CeedScalar cv) {
20888626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
20988626eedSJames Wright   // TODO Add in gravity's contribution
21088626eedSJames Wright 
21188626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
21288626eedSJames Wright   CeedScalar drdT = -rho / T;
21388626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
21488626eedSJames Wright   CeedScalar etot =  E / rho ;
21588626eedSJames Wright   CeedScalar e2p  = drdP * etot + 1. ;
21688626eedSJames Wright   CeedScalar e3p  = ( E  + rho * Rd * T );
21788626eedSJames Wright   CeedScalar e4p  = drdT * etot + rho * cv ;
21888626eedSJames Wright 
21988626eedSJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
22088626eedSJames Wright     for (CeedInt j=0; j<3; j++) { // j counts F^{m_j}
22188626eedSJames Wright //        [row][col] of A_i
22288626eedSJames Wright       dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p
22388626eedSJames Wright       for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k
224871db79fSKenneth E. Jansen         dF[i][0][k+1]   =  ((i==k) ? rho  : 0.);   // F^c wrt u_k
22588626eedSJames Wright         dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) +  // F^m_j wrt u_k
22688626eedSJames Wright                            ((i==k) ? u[j] : 0.) ) * rho;
22788626eedSJames Wright         dF[i][4][k+1]   = rho * u[i] * u[k]
22888626eedSJames Wright                           + ((i==k) ? e3p  : 0.) ; // F^e wrt u_k
22988626eedSJames Wright       }
23088626eedSJames Wright       dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T
23188626eedSJames Wright     }
23288626eedSJames Wright     dF[i][4][0] = u[i] * e2p; // F^e wrt p
23388626eedSJames Wright     dF[i][4][4] = u[i] * e4p; // F^e wrt T
23488626eedSJames Wright     dF[i][0][0] = u[i] * drdP; // F^c wrt p
23588626eedSJames Wright     dF[i][0][4] = u[i] * drdT; // F^c wrt T
23688626eedSJames Wright   }
23788626eedSJames Wright }
23888626eedSJames Wright 
23988626eedSJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho,
24088626eedSJames Wright     const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd,
24188626eedSJames Wright     const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) {
24288626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2];
24388626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
24488626eedSJames Wright   CeedScalar drdT = -rho / T;
24588626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
24688626eedSJames Wright   dU[0] = drdP * dY[0] + drdT * dY[4];
24788626eedSJames Wright   CeedScalar de_kinetic = 0;
24888626eedSJames Wright   for (int i=0; i<3; i++) {
24988626eedSJames Wright     dU[1+i] = dU[0] * u[i] + rho * dY[1+i];
25088626eedSJames Wright     de_kinetic += u[i] * dY[1+i];
25188626eedSJames Wright   }
25288626eedSJames Wright   dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e
25388626eedSJames Wright           + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2
25488626eedSJames Wright }
25588626eedSJames Wright 
25688626eedSJames Wright // *****************************************************************************
25788626eedSJames Wright // Helper function for computing Tau elements (stabilization constant)
25888626eedSJames Wright //   Model from:
25988626eedSJames Wright //     PHASTA
26088626eedSJames Wright //
26188626eedSJames Wright //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
26288626eedSJames Wright //
26388626eedSJames Wright // Where NOT UPDATED YET
26488626eedSJames Wright // *****************************************************************************
26588626eedSJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3],
26688626eedSJames Wright                                         const CeedScalar dXdx[3][3], const CeedScalar u[3],
26788626eedSJames Wright                                         const CeedScalar cv, const NewtonianIdealGasContext newt_ctx,
26888626eedSJames Wright                                         const CeedScalar mu, const CeedScalar dt,
26988626eedSJames Wright                                         const CeedScalar rho) {
27088626eedSJames Wright   // Context
27188626eedSJames Wright   const CeedScalar Ctau_t = newt_ctx->Ctau_t;
27288626eedSJames Wright   const CeedScalar Ctau_v = newt_ctx->Ctau_v;
27388626eedSJames Wright   const CeedScalar Ctau_C = newt_ctx->Ctau_C;
27488626eedSJames Wright   const CeedScalar Ctau_M = newt_ctx->Ctau_M;
27588626eedSJames Wright   const CeedScalar Ctau_E = newt_ctx->Ctau_E;
27688626eedSJames Wright   CeedScalar gijd[6];
27788626eedSJames Wright   CeedScalar tau;
27888626eedSJames Wright   CeedScalar dts;
27988626eedSJames Wright   CeedScalar fact;
28088626eedSJames Wright 
28188626eedSJames Wright   //*INDENT-OFF*
28288626eedSJames Wright   gijd[0] =   dXdx[0][0] * dXdx[0][0]
28388626eedSJames Wright             + dXdx[1][0] * dXdx[1][0]
28488626eedSJames Wright             + dXdx[2][0] * dXdx[2][0];
28588626eedSJames Wright 
28688626eedSJames Wright   gijd[1] =   dXdx[0][0] * dXdx[0][1]
28788626eedSJames Wright             + dXdx[1][0] * dXdx[1][1]
28888626eedSJames Wright             + dXdx[2][0] * dXdx[2][1];
28988626eedSJames Wright 
29088626eedSJames Wright   gijd[2] =   dXdx[0][1] * dXdx[0][1]
29188626eedSJames Wright             + dXdx[1][1] * dXdx[1][1]
29288626eedSJames Wright             + dXdx[2][1] * dXdx[2][1];
29388626eedSJames Wright 
29488626eedSJames Wright   gijd[3] =   dXdx[0][0] * dXdx[0][2]
29588626eedSJames Wright             + dXdx[1][0] * dXdx[1][2]
29688626eedSJames Wright             + dXdx[2][0] * dXdx[2][2];
29788626eedSJames Wright 
29888626eedSJames Wright   gijd[4] =   dXdx[0][1] * dXdx[0][2]
29988626eedSJames Wright             + dXdx[1][1] * dXdx[1][2]
30088626eedSJames Wright             + dXdx[2][1] * dXdx[2][2];
30188626eedSJames Wright 
30288626eedSJames Wright   gijd[5] =   dXdx[0][2] * dXdx[0][2]
30388626eedSJames Wright             + dXdx[1][2] * dXdx[1][2]
30488626eedSJames Wright             + dXdx[2][2] * dXdx[2][2];
30588626eedSJames Wright   //*INDENT-ON*
30688626eedSJames Wright 
30788626eedSJames Wright   dts = Ctau_t / dt ;
30888626eedSJames Wright 
30988626eedSJames Wright   tau = rho*rho*((4. * dts * dts)
31088626eedSJames Wright                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
31188626eedSJames Wright                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
31288626eedSJames Wright                  + u[2] *   u[2] * gijd[5])
31388626eedSJames Wright         + Ctau_v* mu * mu *
31488626eedSJames Wright         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
31588626eedSJames Wright          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
31688626eedSJames Wright 
31788626eedSJames Wright   fact=sqrt(tau);
31888626eedSJames Wright 
31988626eedSJames Wright   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
32088626eedSJames Wright 
32188626eedSJames Wright   Tau_d[1] = Ctau_M / fact;
32288626eedSJames Wright   Tau_d[2] = Ctau_E / ( fact * cv );
32388626eedSJames Wright 
32488626eedSJames Wright // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
32588626eedSJames Wright //  to avoid a division if the compiler is smart enough to see that cv IS
32688626eedSJames Wright // a constant that it could invert once for all elements
32788626eedSJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
32888626eedSJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to
32988626eedSJames Wright // know how to change constants with a change of fluid or units.  Same for
33088626eedSJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
33188626eedSJames Wright }
33288626eedSJames Wright 
33388626eedSJames Wright // *****************************************************************************
33488b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
33588b783a1SJames Wright // *****************************************************************************
33688b783a1SJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q,
33788b783a1SJames Wright                                const CeedScalar *const *in, CeedScalar *const *out) {
33888b783a1SJames Wright   // Inputs
33988b783a1SJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
34088b783a1SJames Wright 
34188b783a1SJames Wright   // Outputs
34288b783a1SJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
34388b783a1SJames Wright 
34488626eedSJames Wright   // Context
34588626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
34688626eedSJames Wright   const CeedScalar theta0    = context->theta0;
34788626eedSJames Wright   const CeedScalar P0        = context->P0;
34888626eedSJames Wright   const CeedScalar cv        = context->cv;
34988626eedSJames Wright   const CeedScalar cp        = context->cp;
35088626eedSJames Wright   const CeedScalar *g        = context->g;
35188626eedSJames Wright   const CeedScalar Rd        = cp - cv;
35288626eedSJames Wright 
35388b783a1SJames Wright   // Quadrature Point Loop
35488b783a1SJames Wright   CeedPragmaSIMD
35588b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
35688b783a1SJames Wright     CeedScalar q[5] = {0.};
35788b783a1SJames Wright 
35888b783a1SJames Wright     // Setup
35988b783a1SJames Wright     // -- Coordinates
36088626eedSJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
36188626eedSJames Wright     const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
36288b783a1SJames Wright 
36388b783a1SJames Wright     // -- Density
36488626eedSJames Wright     const CeedScalar rho = P0 / (Rd*theta0);
36588b783a1SJames Wright 
36688b783a1SJames Wright     // Initial Conditions
36788b783a1SJames Wright     q[0] = rho;
36888b783a1SJames Wright     q[1] = 0.0;
36988b783a1SJames Wright     q[2] = 0.0;
37088b783a1SJames Wright     q[3] = 0.0;
37188626eedSJames Wright     q[4] = rho * (cv*theta0 + e_potential);
37288b783a1SJames Wright 
37388b783a1SJames Wright     for (CeedInt j=0; j<5; j++)
37488b783a1SJames Wright       q0[j][i] = q[j];
37588b783a1SJames Wright   } // End of Quadrature Point Loop
37688b783a1SJames Wright   return 0;
37788b783a1SJames Wright }
37888b783a1SJames Wright 
37988b783a1SJames Wright // *****************************************************************************
38088b783a1SJames Wright // This QFunction implements the following formulation of Navier-Stokes with
38188b783a1SJames Wright //   explicit time stepping method
38288b783a1SJames Wright //
38388b783a1SJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
38488b783a1SJames Wright //   variables of density, momentum density, and total energy density.
38588b783a1SJames Wright //
38688b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
38788b783a1SJames Wright //   rho - Mass Density
38888b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
38988b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
39088b783a1SJames Wright //
39188b783a1SJames Wright // Navier-Stokes Equations:
39288b783a1SJames Wright //   drho/dt + div( U )                               = 0
39388b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
39488b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
39588b783a1SJames Wright //
39688b783a1SJames Wright // Viscous Stress:
39788b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
39888b783a1SJames Wright //
39988b783a1SJames Wright // Thermal Stress:
40088b783a1SJames Wright //   Fe = u Fu + k grad( T )
40188626eedSJames Wright // Equation of State
40288b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
40388b783a1SJames Wright //
40488b783a1SJames Wright // Stabilization:
40588b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
40688b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
40788b783a1SJames Wright //     gij = dXi/dX * dXi/dX
40888b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
40988b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
41088b783a1SJames Wright //     TauE = TauM / (Ce cv)
41188b783a1SJames Wright //
41288b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
41388b783a1SJames Wright //
41488b783a1SJames Wright // Constants:
41588b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
41688b783a1SJames Wright //   mu              ,  Dynamic viscosity
41788b783a1SJames Wright //   k               ,  Thermal conductivity
41888b783a1SJames Wright //   cv              ,  Specific heat, constant volume
41988b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
42088b783a1SJames Wright //   g               ,  Gravity
42188b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
42288b783a1SJames Wright //
42388b783a1SJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
42488b783a1SJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
42588b783a1SJames Wright // int( gradv gradu )
42688b783a1SJames Wright //
42788b783a1SJames Wright // *****************************************************************************
4285c677226SJed Brown CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q,
42988b783a1SJames Wright                                       const CeedScalar *const *in, CeedScalar *const *out) {
43088b783a1SJames Wright   // *INDENT-OFF*
43188b783a1SJames Wright   // Inputs
43288b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
433a3ae0734SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
43488b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
43588b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
43688b783a1SJames Wright   // Outputs
43788b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
438a3ae0734SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
43988b783a1SJames Wright   // *INDENT-ON*
44088b783a1SJames Wright 
44188b783a1SJames Wright   // Context
44288b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
44388b783a1SJames Wright   const CeedScalar mu     = context->mu;
44488b783a1SJames Wright   const CeedScalar cv     = context->cv;
44588b783a1SJames Wright   const CeedScalar cp     = context->cp;
44688626eedSJames Wright   const CeedScalar *g     = context->g;
44788626eedSJames Wright   const CeedScalar dt     = context->dt;
44888b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
44988626eedSJames Wright   const CeedScalar Rd     = cp - cv;
45088b783a1SJames Wright 
45188b783a1SJames Wright   CeedPragmaSIMD
45288b783a1SJames Wright   // Quadrature Point Loop
45388b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
4545c677226SJed Brown     CeedScalar U[5];
4555c677226SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
4565c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
4575c677226SJed Brown     State s = StateFromU(context, U, x_i);
4585c677226SJed Brown 
45988b783a1SJames Wright     // -- Interp-to-Interp q_data
46088b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
46188b783a1SJames Wright     // -- Interp-to-Grad q_data
46288b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
46388b783a1SJames Wright     // *INDENT-OFF*
46488b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
46588b783a1SJames Wright                                     q_data[2][i],
46688b783a1SJames Wright                                     q_data[3][i]},
46788b783a1SJames Wright                                    {q_data[4][i],
46888b783a1SJames Wright                                     q_data[5][i],
46988b783a1SJames Wright                                     q_data[6][i]},
47088b783a1SJames Wright                                    {q_data[7][i],
47188b783a1SJames Wright                                     q_data[8][i],
47288b783a1SJames Wright                                     q_data[9][i]}
47388b783a1SJames Wright                                   };
47488b783a1SJames Wright     // *INDENT-ON*
47588b783a1SJames Wright 
4765c677226SJed Brown     State grad_s[3];
4775c677226SJed Brown     for (int j=0; j<3; j++) {
4786f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
479a3ae0734SJed Brown       for (int k=0; k<5; k++) dU[k] = Grad_q[0][k][i] * dXdx[0][j]
480a3ae0734SJed Brown                                         + Grad_q[1][k][i] * dXdx[1][j]
481a3ae0734SJed Brown                                         + Grad_q[2][k][i] * dXdx[2][j];
4825c677226SJed Brown       dx_i[j] = 1.;
4836f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
4845c677226SJed Brown     }
4855c677226SJed Brown 
4865c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
4875c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
4885c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
4895c677226SJed Brown     KMUnpack(kmstress, stress);
4905c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
4915c677226SJed Brown 
4925c677226SJed Brown     StateConservative F_inviscid[3];
4935c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
4945c677226SJed Brown 
4955c677226SJed Brown     // Total flux
4965c677226SJed Brown     CeedScalar Flux[5][3];
4975c677226SJed Brown     for (int j=0; j<3; j++) {
4985c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
4995c677226SJed Brown       for (int k=0; k<3; k++)
5005c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
5015c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
5025c677226SJed Brown     }
5035c677226SJed Brown 
5045c677226SJed Brown     for (int j=0; j<3; j++) {
5055c677226SJed Brown       for (int k=0; k<5; k++) {
506a3ae0734SJed Brown         Grad_v[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
5075c677226SJed Brown                                    dXdx[j][1] * Flux[k][1] +
5085c677226SJed Brown                                    dXdx[j][2] * Flux[k][2]);
5095c677226SJed Brown       }
5105c677226SJed Brown     }
5115c677226SJed Brown 
5125c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
5135c677226SJed Brown     for (int j=0; j<5; j++)
5145c677226SJed Brown       v[j][i] = wdetJ * body_force[j];
51588b783a1SJames Wright 
51688b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
5175c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
5185c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
5195c677226SJed Brown                            gamma, g, x_i);
5205c677226SJed Brown     CeedScalar grad_U[5][3];
52188b783a1SJames Wright     for (int j=0; j<3; j++) {
5225c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
5235c677226SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
5245c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
52588b783a1SJames Wright     }
52688b783a1SJames Wright 
52788b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
52888b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
52988b783a1SJames Wright     for (int j=0; j<3; j++)
53088b783a1SJames Wright       for (int k=0; k<5; k++)
53188b783a1SJames Wright         for (int l=0; l<5; l++)
5325c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
53388b783a1SJames Wright 
53488626eedSJames Wright     // -- Stabilization method: none, SU, or SUPG
53588626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
53688626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
53788626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
53888b783a1SJames Wright     switch (context->stabilization) {
53988b783a1SJames Wright     case STAB_NONE:        // Galerkin
54088b783a1SJames Wright       break;
54188b783a1SJames Wright     case STAB_SU:        // SU
5425c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
54388626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
54488626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
54588626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
54688626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
54788626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
5485c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
5495c677226SJed Brown                                   tau_strong_conv,
55088626eedSJames Wright                                   tau_strong_conv_conservative);
55188b783a1SJames Wright       for (int j=0; j<3; j++)
55288b783a1SJames Wright         for (int k=0; k<5; k++)
55388b783a1SJames Wright           for (int l=0; l<5; l++)
55488626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
55588b783a1SJames Wright 
55688b783a1SJames Wright       for (int j=0; j<5; j++)
55788b783a1SJames Wright         for (int k=0; k<3; k++)
558a3ae0734SJed Brown           Grad_v[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
55988b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
56088b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
56188b783a1SJames Wright       break;
56288b783a1SJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
56388b783a1SJames Wright       break;
56488b783a1SJames Wright     }
56588b783a1SJames Wright 
56688b783a1SJames Wright   } // End Quadrature Point Loop
56788b783a1SJames Wright 
56888b783a1SJames Wright   // Return
56988b783a1SJames Wright   return 0;
57088b783a1SJames Wright }
57188b783a1SJames Wright 
57288b783a1SJames Wright // *****************************************************************************
57388b783a1SJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
57488b783a1SJames Wright //   implicit time stepping method
57588b783a1SJames Wright //
57688b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
57788b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
57888b783a1SJames Wright //                                       (diffussive terms will be added later)
57988b783a1SJames Wright //
58088b783a1SJames Wright // *****************************************************************************
58188b783a1SJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
58288b783a1SJames Wright                                     const CeedScalar *const *in,
58388b783a1SJames Wright                                     CeedScalar *const *out) {
58488b783a1SJames Wright   // *INDENT-OFF*
58588b783a1SJames Wright   // Inputs
58688b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
587a3ae0734SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
58888b783a1SJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
58988b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
59088b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
59188b783a1SJames Wright   // Outputs
59288b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
593a3ae0734SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1],
594a3ae0734SJed Brown              (*jac_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[2];
59588b783a1SJames Wright   // *INDENT-ON*
59688b783a1SJames Wright   // Context
59788b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
59888b783a1SJames Wright   const CeedScalar mu     = context->mu;
59988b783a1SJames Wright   const CeedScalar cv     = context->cv;
60088b783a1SJames Wright   const CeedScalar cp     = context->cp;
60188626eedSJames Wright   const CeedScalar *g     = context->g;
60288626eedSJames Wright   const CeedScalar dt     = context->dt;
60388b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
60488626eedSJames Wright   const CeedScalar Rd     = cp-cv;
60588b783a1SJames Wright 
60688b783a1SJames Wright   CeedPragmaSIMD
60788b783a1SJames Wright   // Quadrature Point Loop
60888b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
6095c677226SJed Brown     CeedScalar U[5];
6105c677226SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
6115c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
6125c677226SJed Brown     State s = StateFromU(context, U, x_i);
6135c677226SJed Brown 
61488b783a1SJames Wright     // -- Interp-to-Interp q_data
61588b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
61688b783a1SJames Wright     // -- Interp-to-Grad q_data
61788b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
61888b783a1SJames Wright     // *INDENT-OFF*
61988b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
62088b783a1SJames Wright                                     q_data[2][i],
62188b783a1SJames Wright                                     q_data[3][i]},
62288b783a1SJames Wright                                    {q_data[4][i],
62388b783a1SJames Wright                                     q_data[5][i],
62488b783a1SJames Wright                                     q_data[6][i]},
62588b783a1SJames Wright                                    {q_data[7][i],
62688b783a1SJames Wright                                     q_data[8][i],
62788b783a1SJames Wright                                     q_data[9][i]}
62888b783a1SJames Wright                                   };
62988b783a1SJames Wright     // *INDENT-ON*
6305c677226SJed Brown     State grad_s[3];
63188b783a1SJames Wright     for (int j=0; j<3; j++) {
6326f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
633a3ae0734SJed Brown       for (int k=0; k<5; k++) dU[k] = Grad_q[0][k][i] * dXdx[0][j]
634a3ae0734SJed Brown                                         + Grad_q[1][k][i] * dXdx[1][j]
635a3ae0734SJed Brown                                         + Grad_q[2][k][i] * dXdx[2][j];
6365c677226SJed Brown       dx_i[j] = 1.;
6376f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
63888b783a1SJames Wright     }
6395c677226SJed Brown 
6405c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
6415c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
6425c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
6435c677226SJed Brown     KMUnpack(kmstress, stress);
6445c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
6455c677226SJed Brown 
6465c677226SJed Brown     StateConservative F_inviscid[3];
6475c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
6485c677226SJed Brown 
6495c677226SJed Brown 
6505c677226SJed Brown     // Total flux
6515c677226SJed Brown     CeedScalar Flux[5][3];
6525c677226SJed Brown     for (int j=0; j<3; j++) {
6535c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
65488b783a1SJames Wright       for (int k=0; k<3; k++)
6555c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
6565c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
6575c677226SJed Brown     }
6585c677226SJed Brown 
6595c677226SJed Brown     for (int j=0; j<3; j++) {
6605c677226SJed Brown       for (int k=0; k<5; k++) {
661a3ae0734SJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
6625c677226SJed Brown                                     dXdx[j][1] * Flux[k][1] +
6635c677226SJed Brown                                     dXdx[j][2] * Flux[k][2]);
6645c677226SJed Brown       }
6655c677226SJed Brown     }
6665c677226SJed Brown 
6675c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
6685c677226SJed Brown     for (int j=0; j<5; j++)
6695c677226SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
67088b783a1SJames Wright 
67188b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
6725c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
6735c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
6745c677226SJed Brown                            gamma, g, x_i);
6755c677226SJed Brown     CeedScalar grad_U[5][3];
67688b783a1SJames Wright     for (int j=0; j<3; j++) {
6775c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
6785c677226SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
6795c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
68088b783a1SJames Wright     }
6815c677226SJed Brown 
68288b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
68388b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
68488b783a1SJames Wright     for (int j=0; j<3; j++)
68588b783a1SJames Wright       for (int k=0; k<5; k++)
68688b783a1SJames Wright         for (int l=0; l<5; l++)
6875c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
68888b783a1SJames Wright 
68988b783a1SJames Wright     // Strong residual
69088b783a1SJames Wright     CeedScalar strong_res[5];
69188b783a1SJames Wright     for (int j=0; j<5; j++)
69288b783a1SJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
69388b783a1SJames Wright 
69488b783a1SJames Wright     // -- Stabilization method: none, SU, or SUPG
69588626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
69688626eedSJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
69788626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
69888626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
69988b783a1SJames Wright     switch (context->stabilization) {
70088b783a1SJames Wright     case STAB_NONE:        // Galerkin
70188b783a1SJames Wright       break;
70288b783a1SJames Wright     case STAB_SU:        // SU
7035c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
70488626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
70588626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
70688626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
70788626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
70888626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
7095c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
7105c677226SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
71188b783a1SJames Wright       for (int j=0; j<3; j++)
71288b783a1SJames Wright         for (int k=0; k<5; k++)
71388b783a1SJames Wright           for (int l=0; l<5; l++)
71488626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
71588b783a1SJames Wright 
71688b783a1SJames Wright       for (int j=0; j<5; j++)
71788b783a1SJames Wright         for (int k=0; k<3; k++)
718a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
71988b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
72088b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
72188b783a1SJames Wright       break;
72288b783a1SJames Wright     case STAB_SUPG:        // SUPG
7235c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
72488626eedSJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
72588626eedSJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
72688626eedSJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
72788626eedSJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
72888626eedSJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
72988626eedSJames Wright // Alternate route (useful later with primitive variable code)
73088626eedSJames Wright // this function was verified against PHASTA for as IC that was as close as possible
73188626eedSJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
73288626eedSJames Wright // it has also been verified to compute a correct through the following
73388626eedSJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
73488626eedSJames Wright // applied in the triple loop below
73588626eedSJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
7365c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
7375c677226SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
73888b783a1SJames Wright       for (int j=0; j<3; j++)
73988b783a1SJames Wright         for (int k=0; k<5; k++)
74088b783a1SJames Wright           for (int l=0; l<5; l++)
74188626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
74288b783a1SJames Wright 
74388b783a1SJames Wright       for (int j=0; j<5; j++)
74488b783a1SJames Wright         for (int k=0; k<3; k++)
745a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
74688b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
74788b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
74888b783a1SJames Wright       break;
74988b783a1SJames Wright     }
750a3ae0734SJed Brown     for (int j=0; j<5; j++) jac_data[j][i] = U[j];
751*e334ad8fSJed Brown     for (int j=0; j<6; j++) jac_data[5+j][i] = kmstress[j];
752*e334ad8fSJed Brown     for (int j=0; j<3; j++) jac_data[5+6+j][i] = Tau_d[j];
75388b783a1SJames Wright 
75488b783a1SJames Wright   } // End Quadrature Point Loop
75588b783a1SJames Wright 
75688b783a1SJames Wright   // Return
75788b783a1SJames Wright   return 0;
75888b783a1SJames Wright }
759*e334ad8fSJed Brown 
760*e334ad8fSJed Brown CEED_QFUNCTION(IJacobian_Newtonian)(void *ctx, CeedInt Q,
761*e334ad8fSJed Brown                                     const CeedScalar *const *in,
762*e334ad8fSJed Brown                                     CeedScalar *const *out) {
763*e334ad8fSJed Brown   // *INDENT-OFF*
764*e334ad8fSJed Brown   // Inputs
765*e334ad8fSJed Brown   const CeedScalar (*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
766*e334ad8fSJed Brown                    (*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
767*e334ad8fSJed Brown                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
768*e334ad8fSJed Brown                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
769*e334ad8fSJed Brown                    (*jac_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
770*e334ad8fSJed Brown   // Outputs
771*e334ad8fSJed Brown   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
772*e334ad8fSJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
773*e334ad8fSJed Brown   // *INDENT-ON*
774*e334ad8fSJed Brown   // Context
775*e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
776*e334ad8fSJed Brown   const CeedScalar *g = context->g;
777*e334ad8fSJed Brown   const CeedScalar cp = context->cp;
778*e334ad8fSJed Brown   const CeedScalar cv = context->cv;
779*e334ad8fSJed Brown   const CeedScalar Rd = cp - cv;
780*e334ad8fSJed Brown   const CeedScalar gamma = cp / cv;
781*e334ad8fSJed Brown 
782*e334ad8fSJed Brown   CeedPragmaSIMD
783*e334ad8fSJed Brown   // Quadrature Point Loop
784*e334ad8fSJed Brown   for (CeedInt i=0; i<Q; i++) {
785*e334ad8fSJed Brown     // -- Interp-to-Interp q_data
786*e334ad8fSJed Brown     const CeedScalar wdetJ      =   q_data[0][i];
787*e334ad8fSJed Brown     // -- Interp-to-Grad q_data
788*e334ad8fSJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
789*e334ad8fSJed Brown     // *INDENT-OFF*
790*e334ad8fSJed Brown     const CeedScalar dXdx[3][3] = {{q_data[1][i],
791*e334ad8fSJed Brown                                     q_data[2][i],
792*e334ad8fSJed Brown                                     q_data[3][i]},
793*e334ad8fSJed Brown                                    {q_data[4][i],
794*e334ad8fSJed Brown                                     q_data[5][i],
795*e334ad8fSJed Brown                                     q_data[6][i]},
796*e334ad8fSJed Brown                                    {q_data[7][i],
797*e334ad8fSJed Brown                                     q_data[8][i],
798*e334ad8fSJed Brown                                     q_data[9][i]}
799*e334ad8fSJed Brown                                   };
800*e334ad8fSJed Brown     // *INDENT-ON*
801*e334ad8fSJed Brown 
802*e334ad8fSJed Brown     CeedScalar U[5], kmstress[6], Tau_d[3] __attribute((unused));
803*e334ad8fSJed Brown     for (int j=0; j<5; j++) U[j] = jac_data[j][i];
804*e334ad8fSJed Brown     for (int j=0; j<6; j++) kmstress[j] = jac_data[5+j][i];
805*e334ad8fSJed Brown     for (int j=0; j<3; j++) Tau_d[j] = jac_data[5+6+j][i];
806*e334ad8fSJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
807*e334ad8fSJed Brown     State s = StateFromU(context, U, x_i);
808*e334ad8fSJed Brown 
809*e334ad8fSJed Brown     CeedScalar dU[5], dx0[3] = {0};
810*e334ad8fSJed Brown     for (int j=0; j<5; j++) dU[j] = dq[j][i];
811*e334ad8fSJed Brown     State ds = StateFromU_fwd(context, s, dU, x_i, dx0);
812*e334ad8fSJed Brown 
813*e334ad8fSJed Brown     State grad_ds[3];
814*e334ad8fSJed Brown     for (int j=0; j<3; j++) {
815*e334ad8fSJed Brown       CeedScalar dUj[5];
816*e334ad8fSJed Brown       for (int k=0; k<5; k++) dUj[k] = Grad_dq[0][k][i] * dXdx[0][j]
817*e334ad8fSJed Brown                                          + Grad_dq[1][k][i] * dXdx[1][j]
818*e334ad8fSJed Brown                                          + Grad_dq[2][k][i] * dXdx[2][j];
819*e334ad8fSJed Brown       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx0);
820*e334ad8fSJed Brown     }
821*e334ad8fSJed Brown 
822*e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
823*e334ad8fSJed Brown     KMStrainRate(grad_ds, dstrain_rate);
824*e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
825*e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
826*e334ad8fSJed Brown     KMUnpack(kmstress, stress);
827*e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
828*e334ad8fSJed Brown 
829*e334ad8fSJed Brown     StateConservative dF_inviscid[3];
830*e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
831*e334ad8fSJed Brown 
832*e334ad8fSJed Brown     // Total flux
833*e334ad8fSJed Brown     CeedScalar dFlux[5][3];
834*e334ad8fSJed Brown     for (int j=0; j<3; j++) {
835*e334ad8fSJed Brown       dFlux[0][j] = dF_inviscid[j].density;
836*e334ad8fSJed Brown       for (int k=0; k<3; k++)
837*e334ad8fSJed Brown         dFlux[k+1][j] = dF_inviscid[j].momentum[k] - dstress[k][j];
838*e334ad8fSJed Brown       dFlux[4][j] = dF_inviscid[j].E_total + dFe[j];
839*e334ad8fSJed Brown     }
840*e334ad8fSJed Brown 
841*e334ad8fSJed Brown     for (int j=0; j<3; j++) {
842*e334ad8fSJed Brown       for (int k=0; k<5; k++) {
843*e334ad8fSJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * dFlux[k][0] +
844*e334ad8fSJed Brown                                     dXdx[j][1] * dFlux[k][1] +
845*e334ad8fSJed Brown                                     dXdx[j][2] * dFlux[k][2]);
846*e334ad8fSJed Brown       }
847*e334ad8fSJed Brown     }
848*e334ad8fSJed Brown 
849*e334ad8fSJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density *g[0], ds.U.density *g[1], ds.U.density *g[2], 0};
850*e334ad8fSJed Brown     for (int j=0; j<5; j++)
851*e334ad8fSJed Brown       v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
852*e334ad8fSJed Brown 
853*e334ad8fSJed Brown     if (1) {
854*e334ad8fSJed Brown       CeedScalar jacob_F_conv[3][5][5] = {0};
855*e334ad8fSJed Brown       computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
856*e334ad8fSJed Brown                              gamma, g, x_i);
857*e334ad8fSJed Brown       CeedScalar grad_dU[5][3];
858*e334ad8fSJed Brown       for (int j=0; j<3; j++) {
859*e334ad8fSJed Brown         grad_dU[0][j] = grad_ds[j].U.density;
860*e334ad8fSJed Brown         for (int k=0; k<3; k++) grad_dU[k+1][j] = grad_ds[j].U.momentum[k];
861*e334ad8fSJed Brown         grad_dU[4][j] = grad_ds[j].U.E_total;
862*e334ad8fSJed Brown       }
863*e334ad8fSJed Brown       CeedScalar dstrong_conv[5] = {0};
864*e334ad8fSJed Brown       for (int j=0; j<3; j++)
865*e334ad8fSJed Brown         for (int k=0; k<5; k++)
866*e334ad8fSJed Brown           for (int l=0; l<5; l++)
867*e334ad8fSJed Brown             dstrong_conv[k] += jacob_F_conv[j][k][l] * grad_dU[l][j];
868*e334ad8fSJed Brown       CeedScalar dstrong_res[5];
869*e334ad8fSJed Brown       for (int j=0; j<5; j++)
870*e334ad8fSJed Brown         dstrong_res[j] = context->ijacobian_time_shift * dU[j] + dstrong_conv[j] -
871*e334ad8fSJed Brown                          dbody_force[j];
872*e334ad8fSJed Brown       CeedScalar dtau_strong_res[5] = {0.}, dtau_strong_res_conservative[5] = {0};
873*e334ad8fSJed Brown       dtau_strong_res[0] = Tau_d[0] * dstrong_res[0];
874*e334ad8fSJed Brown       dtau_strong_res[1] = Tau_d[1] * dstrong_res[1];
875*e334ad8fSJed Brown       dtau_strong_res[2] = Tau_d[1] * dstrong_res[2];
876*e334ad8fSJed Brown       dtau_strong_res[3] = Tau_d[1] * dstrong_res[3];
877*e334ad8fSJed Brown       dtau_strong_res[4] = Tau_d[2] * dstrong_res[4];
878*e334ad8fSJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
879*e334ad8fSJed Brown                                   dtau_strong_res, dtau_strong_res_conservative);
880*e334ad8fSJed Brown       CeedScalar dstab[5][3] = {0};
881*e334ad8fSJed Brown       for (int j=0; j<3; j++)
882*e334ad8fSJed Brown         for (int k=0; k<5; k++)
883*e334ad8fSJed Brown           for (int l=0; l<5; l++)
884*e334ad8fSJed Brown             dstab[k][j] += jacob_F_conv[j][k][l] * dtau_strong_res_conservative[l];
885*e334ad8fSJed Brown       for (int j=0; j<5; j++)
886*e334ad8fSJed Brown         for (int k=0; k<3; k++)
887*e334ad8fSJed Brown           Grad_v[k][j][i] += wdetJ*(dstab[j][0] * dXdx[k][0] +
888*e334ad8fSJed Brown                                     dstab[j][1] * dXdx[k][1] +
889*e334ad8fSJed Brown                                     dstab[j][2] * dXdx[k][2]);
890*e334ad8fSJed Brown 
891*e334ad8fSJed Brown     }
892*e334ad8fSJed Brown   } // End Quadrature Point Loop
893*e334ad8fSJed Brown   return 0;
894*e334ad8fSJed Brown }
89588b783a1SJames Wright // *****************************************************************************
89688b783a1SJames Wright #endif // newtonian_h
897