xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 39c69132bfa1e54ee170692e72f9f4b944109a99)
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 
163e334ad8fSJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
164e334ad8fSJed Brown     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
165e334ad8fSJed Brown     const CeedScalar stress[3][3],
166e334ad8fSJed Brown     const CeedScalar dstress[3][3],
167e334ad8fSJed Brown     CeedScalar dFe[3]) {
168e334ad8fSJed Brown   for (int i=0; i<3; i++) {
169e334ad8fSJed Brown     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
170e334ad8fSJed Brown              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
171e334ad8fSJed Brown              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
172e334ad8fSJed Brown              - gas->k * grad_ds[i].Y.temperature;
173e334ad8fSJed Brown   }
174e334ad8fSJed 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;
248ba6664aeSJames Wright   for (CeedInt 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];
4773c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
4786f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
479*39c69132SJed Brown       for (CeedInt k=0; k<5; k++)
480*39c69132SJed Brown         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
481*39c69132SJed Brown                 Grad_q[1][k][i] * dXdx[1][j] +
482*39c69132SJed Brown                 Grad_q[2][k][i] * dXdx[2][j];
4835c677226SJed Brown       dx_i[j] = 1.;
4846f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
4855c677226SJed Brown     }
4865c677226SJed Brown 
4875c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
4885c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
4895c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
4905c677226SJed Brown     KMUnpack(kmstress, stress);
4915c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
4925c677226SJed Brown 
4935c677226SJed Brown     StateConservative F_inviscid[3];
4945c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
4955c677226SJed Brown 
4965c677226SJed Brown     // Total flux
4975c677226SJed Brown     CeedScalar Flux[5][3];
4983c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
4995c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
5003c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++)
5015c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
5025c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
5035c677226SJed Brown     }
5045c677226SJed Brown 
5053c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
5063c4b7af6SJed Brown       for (CeedInt k=0; k<5; k++) {
507a3ae0734SJed Brown         Grad_v[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
5085c677226SJed Brown                                    dXdx[j][1] * Flux[k][1] +
5095c677226SJed Brown                                    dXdx[j][2] * Flux[k][2]);
5105c677226SJed Brown       }
5115c677226SJed Brown     }
5125c677226SJed Brown 
5135c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
5145c677226SJed Brown     for (int j=0; j<5; j++)
5155c677226SJed Brown       v[j][i] = wdetJ * body_force[j];
51688b783a1SJames Wright 
51788b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
5185c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
5195c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
5205c677226SJed Brown                            gamma, g, x_i);
5215c677226SJed Brown     CeedScalar grad_U[5][3];
522ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
5235c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
5243c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
5255c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
52688b783a1SJames Wright     }
52788b783a1SJames Wright 
52888b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
52988b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
530ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
531ba6664aeSJames Wright       for (CeedInt k=0; k<5; k++)
532ba6664aeSJames Wright         for (CeedInt l=0; l<5; l++)
5335c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
53488b783a1SJames Wright 
53588626eedSJames Wright     // -- Stabilization method: none, SU, or SUPG
53688626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
53788626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
53888626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
53988b783a1SJames Wright     switch (context->stabilization) {
54088b783a1SJames Wright     case STAB_NONE:        // Galerkin
54188b783a1SJames Wright       break;
54288b783a1SJames Wright     case STAB_SU:        // SU
5435c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
54488626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
54588626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
54688626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
54788626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
54888626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
5495c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
5505c677226SJed Brown                                   tau_strong_conv,
55188626eedSJames Wright                                   tau_strong_conv_conservative);
552ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
553ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
554ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
55588626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
55688b783a1SJames Wright 
557ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
558ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
559a3ae0734SJed Brown           Grad_v[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
56088b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
56188b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
56288b783a1SJames Wright       break;
56388b783a1SJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
56488b783a1SJames Wright       break;
56588b783a1SJames Wright     }
56688b783a1SJames Wright 
56788b783a1SJames Wright   } // End Quadrature Point Loop
56888b783a1SJames Wright 
56988b783a1SJames Wright   // Return
57088b783a1SJames Wright   return 0;
57188b783a1SJames Wright }
57288b783a1SJames Wright 
57388b783a1SJames Wright // *****************************************************************************
57488b783a1SJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
57588b783a1SJames Wright //   implicit time stepping method
57688b783a1SJames Wright //
57788b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
57888b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
57988b783a1SJames Wright //                                       (diffussive terms will be added later)
58088b783a1SJames Wright //
58188b783a1SJames Wright // *****************************************************************************
58288b783a1SJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
58388b783a1SJames Wright                                     const CeedScalar *const *in,
58488b783a1SJames Wright                                     CeedScalar *const *out) {
58588b783a1SJames Wright   // *INDENT-OFF*
58688b783a1SJames Wright   // Inputs
58788b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
588a3ae0734SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
58988b783a1SJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
59088b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
59188b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
59288b783a1SJames Wright   // Outputs
59388b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
594a3ae0734SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1],
595a3ae0734SJed Brown              (*jac_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[2];
59688b783a1SJames Wright   // *INDENT-ON*
59788b783a1SJames Wright   // Context
59888b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
59988b783a1SJames Wright   const CeedScalar mu     = context->mu;
60088b783a1SJames Wright   const CeedScalar cv     = context->cv;
60188b783a1SJames Wright   const CeedScalar cp     = context->cp;
60288626eedSJames Wright   const CeedScalar *g     = context->g;
60388626eedSJames Wright   const CeedScalar dt     = context->dt;
60488b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
60588626eedSJames Wright   const CeedScalar Rd     = cp-cv;
60688b783a1SJames Wright 
60788b783a1SJames Wright   CeedPragmaSIMD
60888b783a1SJames Wright   // Quadrature Point Loop
60988b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
6105c677226SJed Brown     CeedScalar U[5];
6113c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++) U[j] = q[j][i];
6125c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
6135c677226SJed Brown     State s = StateFromU(context, U, x_i);
6145c677226SJed Brown 
61588b783a1SJames Wright     // -- Interp-to-Interp q_data
61688b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
61788b783a1SJames Wright     // -- Interp-to-Grad q_data
61888b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
61988b783a1SJames Wright     // *INDENT-OFF*
62088b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
62188b783a1SJames Wright                                     q_data[2][i],
62288b783a1SJames Wright                                     q_data[3][i]},
62388b783a1SJames Wright                                    {q_data[4][i],
62488b783a1SJames Wright                                     q_data[5][i],
62588b783a1SJames Wright                                     q_data[6][i]},
62688b783a1SJames Wright                                    {q_data[7][i],
62788b783a1SJames Wright                                     q_data[8][i],
62888b783a1SJames Wright                                     q_data[9][i]}
62988b783a1SJames Wright                                   };
63088b783a1SJames Wright     // *INDENT-ON*
6315c677226SJed Brown     State grad_s[3];
632ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
6336f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
634*39c69132SJed Brown       for (CeedInt k=0; k<5; k++)
635*39c69132SJed Brown         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
636*39c69132SJed Brown                 Grad_q[1][k][i] * dXdx[1][j] +
637*39c69132SJed Brown                 Grad_q[2][k][i] * dXdx[2][j];
6385c677226SJed Brown       dx_i[j] = 1.;
6396f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
64088b783a1SJames Wright     }
6415c677226SJed Brown 
6425c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
6435c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
6445c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
6455c677226SJed Brown     KMUnpack(kmstress, stress);
6465c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
6475c677226SJed Brown 
6485c677226SJed Brown     StateConservative F_inviscid[3];
6495c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
6505c677226SJed Brown 
6515c677226SJed Brown 
6525c677226SJed Brown     // Total flux
6535c677226SJed Brown     CeedScalar Flux[5][3];
6543c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
6555c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
656ba6664aeSJames Wright       for (CeedInt k=0; k<3; k++)
6575c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
6585c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
6595c677226SJed Brown     }
6605c677226SJed Brown 
6613c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
6623c4b7af6SJed Brown       for (CeedInt k=0; k<5; k++) {
663a3ae0734SJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
6645c677226SJed Brown                                     dXdx[j][1] * Flux[k][1] +
6655c677226SJed Brown                                     dXdx[j][2] * Flux[k][2]);
6665c677226SJed Brown       }
6675c677226SJed Brown     }
6685c677226SJed Brown 
6695c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
6703c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++)
6715c677226SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
67288b783a1SJames Wright 
67388b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
6745c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
6755c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
6765c677226SJed Brown                            gamma, g, x_i);
6775c677226SJed Brown     CeedScalar grad_U[5][3];
678ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
6795c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
6803c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
6815c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
68288b783a1SJames Wright     }
6835c677226SJed Brown 
68488b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
68588b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
686ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
687ba6664aeSJames Wright       for (CeedInt k=0; k<5; k++)
688ba6664aeSJames Wright         for (CeedInt l=0; l<5; l++)
6895c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
69088b783a1SJames Wright 
69188b783a1SJames Wright     // Strong residual
69288b783a1SJames Wright     CeedScalar strong_res[5];
693ba6664aeSJames Wright     for (CeedInt j=0; j<5; j++)
69488b783a1SJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
69588b783a1SJames Wright 
69688b783a1SJames Wright     // -- Stabilization method: none, SU, or SUPG
69788626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
69888626eedSJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
69988626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
70088626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
70188b783a1SJames Wright     switch (context->stabilization) {
70288b783a1SJames Wright     case STAB_NONE:        // Galerkin
70388b783a1SJames Wright       break;
70488b783a1SJames Wright     case STAB_SU:        // SU
7055c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
70688626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
70788626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
70888626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
70988626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
71088626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
7115c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
7125c677226SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
713ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
714ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
715ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
71688626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
71788b783a1SJames Wright 
718ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
719ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
720a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
72188b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
72288b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
7233c4b7af6SJed Brown 
72488b783a1SJames Wright       break;
72588b783a1SJames Wright     case STAB_SUPG:        // SUPG
7265c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
72788626eedSJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
72888626eedSJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
72988626eedSJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
73088626eedSJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
73188626eedSJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
73288626eedSJames Wright // Alternate route (useful later with primitive variable code)
73388626eedSJames Wright // this function was verified against PHASTA for as IC that was as close as possible
73488626eedSJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
73588626eedSJames Wright // it has also been verified to compute a correct through the following
73688626eedSJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
73788626eedSJames Wright // applied in the triple loop below
73888626eedSJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
7395c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
7405c677226SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
741ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
742ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
743ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
74488626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
74588b783a1SJames Wright 
746ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
747ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
748a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
74988b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
75088b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
75188b783a1SJames Wright       break;
75288b783a1SJames Wright     }
7533c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++) jac_data[j][i] = U[j];
7543c4b7af6SJed Brown     for (CeedInt j=0; j<6; j++) jac_data[5+j][i] = kmstress[j];
7553c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) jac_data[5+6+j][i] = Tau_d[j];
75688b783a1SJames Wright 
75788b783a1SJames Wright   } // End Quadrature Point Loop
75888b783a1SJames Wright 
75988b783a1SJames Wright   // Return
76088b783a1SJames Wright   return 0;
76188b783a1SJames Wright }
762e334ad8fSJed Brown 
763e334ad8fSJed Brown CEED_QFUNCTION(IJacobian_Newtonian)(void *ctx, CeedInt Q,
764e334ad8fSJed Brown                                     const CeedScalar *const *in,
765e334ad8fSJed Brown                                     CeedScalar *const *out) {
766e334ad8fSJed Brown   // *INDENT-OFF*
767e334ad8fSJed Brown   // Inputs
768e334ad8fSJed Brown   const CeedScalar (*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
769e334ad8fSJed Brown                    (*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
770e334ad8fSJed Brown                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
771e334ad8fSJed Brown                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
772e334ad8fSJed Brown                    (*jac_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
773e334ad8fSJed Brown   // Outputs
774e334ad8fSJed Brown   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
775e334ad8fSJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
776e334ad8fSJed Brown   // *INDENT-ON*
777e334ad8fSJed Brown   // Context
778e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
779e334ad8fSJed Brown   const CeedScalar *g = context->g;
780e334ad8fSJed Brown   const CeedScalar cp = context->cp;
781e334ad8fSJed Brown   const CeedScalar cv = context->cv;
782e334ad8fSJed Brown   const CeedScalar Rd = cp - cv;
783e334ad8fSJed Brown   const CeedScalar gamma = cp / cv;
784e334ad8fSJed Brown 
785e334ad8fSJed Brown   CeedPragmaSIMD
786e334ad8fSJed Brown   // Quadrature Point Loop
787e334ad8fSJed Brown   for (CeedInt i=0; i<Q; i++) {
788e334ad8fSJed Brown     // -- Interp-to-Interp q_data
789e334ad8fSJed Brown     const CeedScalar wdetJ      =   q_data[0][i];
790e334ad8fSJed Brown     // -- Interp-to-Grad q_data
791e334ad8fSJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
792e334ad8fSJed Brown     // *INDENT-OFF*
793e334ad8fSJed Brown     const CeedScalar dXdx[3][3] = {{q_data[1][i],
794e334ad8fSJed Brown                                     q_data[2][i],
795e334ad8fSJed Brown                                     q_data[3][i]},
796e334ad8fSJed Brown                                    {q_data[4][i],
797e334ad8fSJed Brown                                     q_data[5][i],
798e334ad8fSJed Brown                                     q_data[6][i]},
799e334ad8fSJed Brown                                    {q_data[7][i],
800e334ad8fSJed Brown                                     q_data[8][i],
801e334ad8fSJed Brown                                     q_data[9][i]}
802e334ad8fSJed Brown                                   };
803e334ad8fSJed Brown     // *INDENT-ON*
804e334ad8fSJed Brown 
805e334ad8fSJed Brown     CeedScalar U[5], kmstress[6], Tau_d[3] __attribute((unused));
806e334ad8fSJed Brown     for (int j=0; j<5; j++) U[j] = jac_data[j][i];
807e334ad8fSJed Brown     for (int j=0; j<6; j++) kmstress[j] = jac_data[5+j][i];
808e334ad8fSJed Brown     for (int j=0; j<3; j++) Tau_d[j] = jac_data[5+6+j][i];
809e334ad8fSJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
810e334ad8fSJed Brown     State s = StateFromU(context, U, x_i);
811e334ad8fSJed Brown 
812e334ad8fSJed Brown     CeedScalar dU[5], dx0[3] = {0};
813e334ad8fSJed Brown     for (int j=0; j<5; j++) dU[j] = dq[j][i];
814e334ad8fSJed Brown     State ds = StateFromU_fwd(context, s, dU, x_i, dx0);
815e334ad8fSJed Brown 
816e334ad8fSJed Brown     State grad_ds[3];
817e334ad8fSJed Brown     for (int j=0; j<3; j++) {
818e334ad8fSJed Brown       CeedScalar dUj[5];
819e334ad8fSJed Brown       for (int k=0; k<5; k++) dUj[k] = Grad_dq[0][k][i] * dXdx[0][j]
820e334ad8fSJed Brown                                          + Grad_dq[1][k][i] * dXdx[1][j]
821e334ad8fSJed Brown                                          + Grad_dq[2][k][i] * dXdx[2][j];
822e334ad8fSJed Brown       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx0);
823e334ad8fSJed Brown     }
824e334ad8fSJed Brown 
825e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
826e334ad8fSJed Brown     KMStrainRate(grad_ds, dstrain_rate);
827e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
828e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
829e334ad8fSJed Brown     KMUnpack(kmstress, stress);
830e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
831e334ad8fSJed Brown 
832e334ad8fSJed Brown     StateConservative dF_inviscid[3];
833e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
834e334ad8fSJed Brown 
835e334ad8fSJed Brown     // Total flux
836e334ad8fSJed Brown     CeedScalar dFlux[5][3];
837e334ad8fSJed Brown     for (int j=0; j<3; j++) {
838e334ad8fSJed Brown       dFlux[0][j] = dF_inviscid[j].density;
839e334ad8fSJed Brown       for (int k=0; k<3; k++)
840e334ad8fSJed Brown         dFlux[k+1][j] = dF_inviscid[j].momentum[k] - dstress[k][j];
841e334ad8fSJed Brown       dFlux[4][j] = dF_inviscid[j].E_total + dFe[j];
842e334ad8fSJed Brown     }
843e334ad8fSJed Brown 
844e334ad8fSJed Brown     for (int j=0; j<3; j++) {
845e334ad8fSJed Brown       for (int k=0; k<5; k++) {
846e334ad8fSJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * dFlux[k][0] +
847e334ad8fSJed Brown                                     dXdx[j][1] * dFlux[k][1] +
848e334ad8fSJed Brown                                     dXdx[j][2] * dFlux[k][2]);
849e334ad8fSJed Brown       }
850e334ad8fSJed Brown     }
851e334ad8fSJed Brown 
852e334ad8fSJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density *g[0], ds.U.density *g[1], ds.U.density *g[2], 0};
853e334ad8fSJed Brown     for (int j=0; j<5; j++)
854e334ad8fSJed Brown       v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
855e334ad8fSJed Brown 
856e334ad8fSJed Brown     if (1) {
857e334ad8fSJed Brown       CeedScalar jacob_F_conv[3][5][5] = {0};
858e334ad8fSJed Brown       computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
859e334ad8fSJed Brown                              gamma, g, x_i);
860e334ad8fSJed Brown       CeedScalar grad_dU[5][3];
861e334ad8fSJed Brown       for (int j=0; j<3; j++) {
862e334ad8fSJed Brown         grad_dU[0][j] = grad_ds[j].U.density;
863e334ad8fSJed Brown         for (int k=0; k<3; k++) grad_dU[k+1][j] = grad_ds[j].U.momentum[k];
864e334ad8fSJed Brown         grad_dU[4][j] = grad_ds[j].U.E_total;
865e334ad8fSJed Brown       }
866e334ad8fSJed Brown       CeedScalar dstrong_conv[5] = {0};
867e334ad8fSJed Brown       for (int j=0; j<3; j++)
868e334ad8fSJed Brown         for (int k=0; k<5; k++)
869e334ad8fSJed Brown           for (int l=0; l<5; l++)
870e334ad8fSJed Brown             dstrong_conv[k] += jacob_F_conv[j][k][l] * grad_dU[l][j];
871e334ad8fSJed Brown       CeedScalar dstrong_res[5];
872e334ad8fSJed Brown       for (int j=0; j<5; j++)
873e334ad8fSJed Brown         dstrong_res[j] = context->ijacobian_time_shift * dU[j] + dstrong_conv[j] -
874e334ad8fSJed Brown                          dbody_force[j];
875e334ad8fSJed Brown       CeedScalar dtau_strong_res[5] = {0.}, dtau_strong_res_conservative[5] = {0};
876e334ad8fSJed Brown       dtau_strong_res[0] = Tau_d[0] * dstrong_res[0];
877e334ad8fSJed Brown       dtau_strong_res[1] = Tau_d[1] * dstrong_res[1];
878e334ad8fSJed Brown       dtau_strong_res[2] = Tau_d[1] * dstrong_res[2];
879e334ad8fSJed Brown       dtau_strong_res[3] = Tau_d[1] * dstrong_res[3];
880e334ad8fSJed Brown       dtau_strong_res[4] = Tau_d[2] * dstrong_res[4];
881e334ad8fSJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
882e334ad8fSJed Brown                                   dtau_strong_res, dtau_strong_res_conservative);
883e334ad8fSJed Brown       CeedScalar dstab[5][3] = {0};
884e334ad8fSJed Brown       for (int j=0; j<3; j++)
885e334ad8fSJed Brown         for (int k=0; k<5; k++)
886e334ad8fSJed Brown           for (int l=0; l<5; l++)
887e334ad8fSJed Brown             dstab[k][j] += jacob_F_conv[j][k][l] * dtau_strong_res_conservative[l];
888e334ad8fSJed Brown       for (int j=0; j<5; j++)
889e334ad8fSJed Brown         for (int k=0; k<3; k++)
890e334ad8fSJed Brown           Grad_v[k][j][i] += wdetJ*(dstab[j][0] * dXdx[k][0] +
891e334ad8fSJed Brown                                     dstab[j][1] * dXdx[k][1] +
892e334ad8fSJed Brown                                     dstab[j][2] * dXdx[k][2]);
893e334ad8fSJed Brown 
894e334ad8fSJed Brown     }
895e334ad8fSJed Brown   } // End Quadrature Point Loop
896e334ad8fSJed Brown   return 0;
897e334ad8fSJed Brown }
89888b783a1SJames Wright // *****************************************************************************
89988b783a1SJames Wright #endif // newtonian_h
900