xref: /honee/qfunctions/newtonian.h (revision c1a523651d358ec8468adfdea34f8ec8b13ac445)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33a8779fbSJames Wright //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53a8779fbSJames Wright //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73a8779fbSJames Wright 
83a8779fbSJames Wright /// @file
93a8779fbSJames Wright /// Operator for Navier-Stokes example using PETSc
103a8779fbSJames Wright 
113a8779fbSJames Wright 
123a8779fbSJames Wright #ifndef newtonian_h
133a8779fbSJames Wright #define newtonian_h
143a8779fbSJames Wright 
153a8779fbSJames Wright #include <math.h>
163a8779fbSJames Wright #include <ceed.h>
1715a3537eSJed Brown #include "newtonian_types.h"
183a8779fbSJames Wright 
193a8779fbSJames Wright #ifndef M_PI
203a8779fbSJames Wright #define M_PI    3.14159265358979323846
213a8779fbSJames Wright #endif
223a8779fbSJames Wright 
23*c1a52365SJed Brown typedef struct {
24*c1a52365SJed Brown   CeedScalar pressure;
25*c1a52365SJed Brown   CeedScalar velocity[3];
26*c1a52365SJed Brown   CeedScalar temperature;
27*c1a52365SJed Brown } StatePrimitive;
28*c1a52365SJed Brown 
29*c1a52365SJed Brown typedef struct {
30*c1a52365SJed Brown   CeedScalar density;
31*c1a52365SJed Brown   CeedScalar momentum[3];
32*c1a52365SJed Brown   CeedScalar E_total;
33*c1a52365SJed Brown } StateConservative;
34*c1a52365SJed Brown 
35*c1a52365SJed Brown typedef struct {
36*c1a52365SJed Brown   StateConservative U;
37*c1a52365SJed Brown   StatePrimitive Y;
38*c1a52365SJed Brown } State;
39*c1a52365SJed Brown 
40*c1a52365SJed Brown CEED_QFUNCTION_HELPER CeedScalar Dot3(const CeedScalar u[3],
41*c1a52365SJed Brown                                       const CeedScalar v[3]) {
42*c1a52365SJed Brown   return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
43*c1a52365SJed Brown }
44*c1a52365SJed Brown 
45*c1a52365SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
46*c1a52365SJed Brown   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
47*c1a52365SJed Brown   StatePrimitive Y;
48*c1a52365SJed Brown   for (int i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
49*c1a52365SJed Brown   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
50*c1a52365SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
51*c1a52365SJed Brown   CeedScalar e_total = U.E_total / U.density;
52*c1a52365SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
53*c1a52365SJed Brown   Y.temperature = e_internal / gas->cv;
54*c1a52365SJed Brown   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
55*c1a52365SJed Brown   return Y;
56*c1a52365SJed Brown }
57*c1a52365SJed Brown 
58*c1a52365SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
59*c1a52365SJed Brown   NewtonianIdealGasContext gas, State s, StateConservative dU,
60*c1a52365SJed Brown   const CeedScalar x[3], const CeedScalar dx[3]) {
61*c1a52365SJed Brown   StatePrimitive dY;
62*c1a52365SJed Brown   for (int i=0; i<3; i++) {
63*c1a52365SJed Brown     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
64*c1a52365SJed Brown   }
65*c1a52365SJed Brown   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
66*c1a52365SJed Brown   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
67*c1a52365SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
68*c1a52365SJed Brown   CeedScalar de_potential = -Dot3(gas->g, dx);
69*c1a52365SJed Brown   CeedScalar e_total = s.U.E_total / s.U.density;
70*c1a52365SJed Brown   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
71*c1a52365SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
72*c1a52365SJed Brown   CeedScalar de_internal = de_total - de_kinetic - de_potential;
73*c1a52365SJed Brown   dY.temperature = de_internal / gas->cv;
74*c1a52365SJed Brown   dY.pressure = (gas->cp / gas->cv - 1)
75*c1a52365SJed Brown                 * (dU.density * e_internal + s.U.density * de_internal);
76*c1a52365SJed Brown   return dY;
77*c1a52365SJed Brown }
78*c1a52365SJed Brown 
79*c1a52365SJed Brown CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
80*c1a52365SJed Brown                                        const CeedScalar U[5], const CeedScalar x[3]) {
81*c1a52365SJed Brown   State s;
82*c1a52365SJed Brown   s.U.density = U[0];
83*c1a52365SJed Brown   s.U.momentum[0] = U[1];
84*c1a52365SJed Brown   s.U.momentum[1] = U[2];
85*c1a52365SJed Brown   s.U.momentum[2] = U[3];
86*c1a52365SJed Brown   s.U.E_total = U[4];
87*c1a52365SJed Brown   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
88*c1a52365SJed Brown   return s;
89*c1a52365SJed Brown }
90*c1a52365SJed Brown 
91*c1a52365SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
92*c1a52365SJed Brown                                         StateConservative Flux[3]) {
93*c1a52365SJed Brown   for (int i=0; i<3; i++) {
94*c1a52365SJed Brown     Flux[i].density = s.U.momentum[i];
95*c1a52365SJed Brown     for (int j=0; j<3; j++)
96*c1a52365SJed Brown       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
97*c1a52365SJed Brown                             + s.Y.pressure * (i == j);
98*c1a52365SJed Brown     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
99*c1a52365SJed Brown   }
100*c1a52365SJed Brown }
101*c1a52365SJed Brown 
102*c1a52365SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
103*c1a52365SJed Brown     State s, State ds, StateConservative dFlux[3]) {
104*c1a52365SJed Brown   for (int i=0; i<3; i++) {
105*c1a52365SJed Brown     dFlux[i].density = ds.U.momentum[i];
106*c1a52365SJed Brown     for (int j=0; j<3; j++)
107*c1a52365SJed Brown       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
108*c1a52365SJed Brown                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
109*c1a52365SJed Brown     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
110*c1a52365SJed Brown                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
111*c1a52365SJed Brown   }
112*c1a52365SJed Brown }
113*c1a52365SJed Brown 
114*c1a52365SJed Brown // Kelvin-Mandel notation
115*c1a52365SJed Brown CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
116*c1a52365SJed Brown                                         CeedScalar strain_rate[6]) {
117*c1a52365SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
118*c1a52365SJed Brown   strain_rate[0] = grad_s[0].Y.velocity[0];
119*c1a52365SJed Brown   strain_rate[1] = grad_s[1].Y.velocity[1];
120*c1a52365SJed Brown   strain_rate[2] = grad_s[2].Y.velocity[2];
121*c1a52365SJed Brown   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
122*c1a52365SJed Brown   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
123*c1a52365SJed Brown   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
124*c1a52365SJed Brown }
125*c1a52365SJed Brown 
126*c1a52365SJed Brown CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
127*c1a52365SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
128*c1a52365SJed Brown   A[0][0] = v[0];
129*c1a52365SJed Brown   A[1][1] = v[1];
130*c1a52365SJed Brown   A[2][2] = v[2];
131*c1a52365SJed Brown   A[2][1] = A[1][2] = weight * v[3];
132*c1a52365SJed Brown   A[2][0] = A[0][2] = weight * v[4];
133*c1a52365SJed Brown   A[1][0] = A[0][1] = weight * v[5];
134*c1a52365SJed Brown }
135*c1a52365SJed Brown 
136*c1a52365SJed Brown CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
137*c1a52365SJed Brown     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
138*c1a52365SJed Brown   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
139*c1a52365SJed Brown   for (int i=0; i<6; i++) {
140*c1a52365SJed Brown     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
141*c1a52365SJed Brown   }
142*c1a52365SJed Brown }
143*c1a52365SJed Brown 
144*c1a52365SJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
145*c1a52365SJed Brown     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
146*c1a52365SJed Brown     CeedScalar Fe[3]) {
147*c1a52365SJed Brown   for (int i=0; i<3; i++) {
148*c1a52365SJed Brown     Fe[i] = - Y.velocity[0] * stress[0][i]
149*c1a52365SJed Brown             - Y.velocity[1] * stress[1][i]
150*c1a52365SJed Brown             - Y.velocity[2] * stress[2][i]
151*c1a52365SJed Brown             - gas->k * grad_s[i].Y.temperature;
152*c1a52365SJed Brown   }
153*c1a52365SJed Brown }
154*c1a52365SJed Brown 
1553a8779fbSJames Wright // *****************************************************************************
1563a8779fbSJames Wright // Helper function for computing flux Jacobian
1573a8779fbSJames Wright // *****************************************************************************
1583a8779fbSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5],
1593a8779fbSJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
160bb8a0c61SJames Wright     const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) {
1613a8779fbSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
162bb8a0c61SJames Wright   CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
1633a8779fbSJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
1643a8779fbSJames Wright     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
165bb8a0c61SJames Wright       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) -
166bb8a0c61SJames Wright                       u[i]*u[j];
1673a8779fbSJames Wright       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
1683a8779fbSJames Wright         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
1693a8779fbSJames Wright         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
1703a8779fbSJames Wright                           ((i==k) ? u[j] : 0.) -
1713a8779fbSJames Wright                           ((i==j) ? u[k] : 0.) * (gamma-1.);
1723a8779fbSJames Wright         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
1733a8779fbSJames Wright                           (gamma-1.)*u[i]*u[k];
1743a8779fbSJames Wright       }
1753a8779fbSJames Wright       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
1763a8779fbSJames Wright     }
1773a8779fbSJames Wright     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
1783a8779fbSJames Wright     dF[i][4][4] = u[i] * gamma;
1793a8779fbSJames Wright   }
1803a8779fbSJames Wright }
1813a8779fbSJames Wright 
1823a8779fbSJames Wright // *****************************************************************************
183bb8a0c61SJames Wright // Helper function for computing flux Jacobian of Primitive variables
184bb8a0c61SJames Wright // *****************************************************************************
185bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5],
186bb8a0c61SJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
187bb8a0c61SJames Wright     const CeedScalar Rd, const CeedScalar cv) {
188bb8a0c61SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
189bb8a0c61SJames Wright   // TODO Add in gravity's contribution
190bb8a0c61SJames Wright 
191bb8a0c61SJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
192bb8a0c61SJames Wright   CeedScalar drdT = -rho / T;
193bb8a0c61SJames Wright   CeedScalar drdP = 1. / ( Rd * T);
194bb8a0c61SJames Wright   CeedScalar etot =  E / rho ;
195bb8a0c61SJames Wright   CeedScalar e2p  = drdP * etot + 1. ;
196bb8a0c61SJames Wright   CeedScalar e3p  = ( E  + rho * Rd * T );
197bb8a0c61SJames Wright   CeedScalar e4p  = drdT * etot + rho * cv ;
198bb8a0c61SJames Wright 
199bb8a0c61SJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
200bb8a0c61SJames Wright     for (CeedInt j=0; j<3; j++) { // j counts F^{m_j}
201bb8a0c61SJames Wright //        [row][col] of A_i
202bb8a0c61SJames Wright       dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p
203bb8a0c61SJames Wright       for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k
2042acc7cbcSKenneth E. Jansen         dF[i][0][k+1]   =  ((i==k) ? rho  : 0.);   // F^c wrt u_k
205bb8a0c61SJames Wright         dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) +  // F^m_j wrt u_k
206bb8a0c61SJames Wright                            ((i==k) ? u[j] : 0.) ) * rho;
207bb8a0c61SJames Wright         dF[i][4][k+1]   = rho * u[i] * u[k]
208bb8a0c61SJames Wright                           + ((i==k) ? e3p  : 0.) ; // F^e wrt u_k
209bb8a0c61SJames Wright       }
210bb8a0c61SJames Wright       dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T
211bb8a0c61SJames Wright     }
212bb8a0c61SJames Wright     dF[i][4][0] = u[i] * e2p; // F^e wrt p
213bb8a0c61SJames Wright     dF[i][4][4] = u[i] * e4p; // F^e wrt T
214bb8a0c61SJames Wright     dF[i][0][0] = u[i] * drdP; // F^c wrt p
215bb8a0c61SJames Wright     dF[i][0][4] = u[i] * drdT; // F^c wrt T
216bb8a0c61SJames Wright   }
217bb8a0c61SJames Wright }
218bb8a0c61SJames Wright 
219bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho,
220bb8a0c61SJames Wright     const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd,
221bb8a0c61SJames Wright     const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) {
222bb8a0c61SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2];
223bb8a0c61SJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
224bb8a0c61SJames Wright   CeedScalar drdT = -rho / T;
225bb8a0c61SJames Wright   CeedScalar drdP = 1. / ( Rd * T);
226bb8a0c61SJames Wright   dU[0] = drdP * dY[0] + drdT * dY[4];
227bb8a0c61SJames Wright   CeedScalar de_kinetic = 0;
228bb8a0c61SJames Wright   for (int i=0; i<3; i++) {
229bb8a0c61SJames Wright     dU[1+i] = dU[0] * u[i] + rho * dY[1+i];
230bb8a0c61SJames Wright     de_kinetic += u[i] * dY[1+i];
231bb8a0c61SJames Wright   }
232bb8a0c61SJames Wright   dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e
233bb8a0c61SJames Wright           + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2
234bb8a0c61SJames Wright }
235bb8a0c61SJames Wright 
236bb8a0c61SJames Wright // *****************************************************************************
237bb8a0c61SJames Wright // Helper function for computing Tau elements (stabilization constant)
238bb8a0c61SJames Wright //   Model from:
239bb8a0c61SJames Wright //     PHASTA
240bb8a0c61SJames Wright //
241bb8a0c61SJames Wright //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
242bb8a0c61SJames Wright //
243bb8a0c61SJames Wright // Where NOT UPDATED YET
244bb8a0c61SJames Wright // *****************************************************************************
245bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3],
246bb8a0c61SJames Wright                                         const CeedScalar dXdx[3][3], const CeedScalar u[3],
247bb8a0c61SJames Wright                                         const CeedScalar cv, const NewtonianIdealGasContext newt_ctx,
248bb8a0c61SJames Wright                                         const CeedScalar mu, const CeedScalar dt,
249bb8a0c61SJames Wright                                         const CeedScalar rho) {
250bb8a0c61SJames Wright   // Context
251bb8a0c61SJames Wright   const CeedScalar Ctau_t = newt_ctx->Ctau_t;
252bb8a0c61SJames Wright   const CeedScalar Ctau_v = newt_ctx->Ctau_v;
253bb8a0c61SJames Wright   const CeedScalar Ctau_C = newt_ctx->Ctau_C;
254bb8a0c61SJames Wright   const CeedScalar Ctau_M = newt_ctx->Ctau_M;
255bb8a0c61SJames Wright   const CeedScalar Ctau_E = newt_ctx->Ctau_E;
256bb8a0c61SJames Wright   CeedScalar gijd[6];
257bb8a0c61SJames Wright   CeedScalar tau;
258bb8a0c61SJames Wright   CeedScalar dts;
259bb8a0c61SJames Wright   CeedScalar fact;
260bb8a0c61SJames Wright 
261bb8a0c61SJames Wright   //*INDENT-OFF*
262bb8a0c61SJames Wright   gijd[0] =   dXdx[0][0] * dXdx[0][0]
263bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][0]
264bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][0];
265bb8a0c61SJames Wright 
266bb8a0c61SJames Wright   gijd[1] =   dXdx[0][0] * dXdx[0][1]
267bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][1]
268bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][1];
269bb8a0c61SJames Wright 
270bb8a0c61SJames Wright   gijd[2] =   dXdx[0][1] * dXdx[0][1]
271bb8a0c61SJames Wright             + dXdx[1][1] * dXdx[1][1]
272bb8a0c61SJames Wright             + dXdx[2][1] * dXdx[2][1];
273bb8a0c61SJames Wright 
274bb8a0c61SJames Wright   gijd[3] =   dXdx[0][0] * dXdx[0][2]
275bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][2]
276bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][2];
277bb8a0c61SJames Wright 
278bb8a0c61SJames Wright   gijd[4] =   dXdx[0][1] * dXdx[0][2]
279bb8a0c61SJames Wright             + dXdx[1][1] * dXdx[1][2]
280bb8a0c61SJames Wright             + dXdx[2][1] * dXdx[2][2];
281bb8a0c61SJames Wright 
282bb8a0c61SJames Wright   gijd[5] =   dXdx[0][2] * dXdx[0][2]
283bb8a0c61SJames Wright             + dXdx[1][2] * dXdx[1][2]
284bb8a0c61SJames Wright             + dXdx[2][2] * dXdx[2][2];
285bb8a0c61SJames Wright   //*INDENT-ON*
286bb8a0c61SJames Wright 
287bb8a0c61SJames Wright   dts = Ctau_t / dt ;
288bb8a0c61SJames Wright 
289bb8a0c61SJames Wright   tau = rho*rho*((4. * dts * dts)
290bb8a0c61SJames Wright                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
291bb8a0c61SJames Wright                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
292bb8a0c61SJames Wright                  + u[2] *   u[2] * gijd[5])
293bb8a0c61SJames Wright         + Ctau_v* mu * mu *
294bb8a0c61SJames Wright         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
295bb8a0c61SJames Wright          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
296bb8a0c61SJames Wright 
297bb8a0c61SJames Wright   fact=sqrt(tau);
298bb8a0c61SJames Wright 
299bb8a0c61SJames Wright   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
300bb8a0c61SJames Wright 
301bb8a0c61SJames Wright   Tau_d[1] = Ctau_M / fact;
302bb8a0c61SJames Wright   Tau_d[2] = Ctau_E / ( fact * cv );
303bb8a0c61SJames Wright 
304bb8a0c61SJames Wright // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
305bb8a0c61SJames Wright //  to avoid a division if the compiler is smart enough to see that cv IS
306bb8a0c61SJames Wright // a constant that it could invert once for all elements
307bb8a0c61SJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
308bb8a0c61SJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to
309bb8a0c61SJames Wright // know how to change constants with a change of fluid or units.  Same for
310bb8a0c61SJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
311bb8a0c61SJames Wright }
312bb8a0c61SJames Wright 
313bb8a0c61SJames Wright // *****************************************************************************
3143a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
3153a8779fbSJames Wright // *****************************************************************************
3163a8779fbSJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q,
3173a8779fbSJames Wright                                const CeedScalar *const *in, CeedScalar *const *out) {
3183a8779fbSJames Wright   // Inputs
3193a8779fbSJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3203a8779fbSJames Wright 
3213a8779fbSJames Wright   // Outputs
3223a8779fbSJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3233a8779fbSJames Wright 
324bb8a0c61SJames Wright   // Context
325bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
326bb8a0c61SJames Wright   const CeedScalar theta0    = context->theta0;
327bb8a0c61SJames Wright   const CeedScalar P0        = context->P0;
328bb8a0c61SJames Wright   const CeedScalar cv        = context->cv;
329bb8a0c61SJames Wright   const CeedScalar cp        = context->cp;
330bb8a0c61SJames Wright   const CeedScalar *g        = context->g;
331bb8a0c61SJames Wright   const CeedScalar Rd        = cp - cv;
332bb8a0c61SJames Wright 
3333a8779fbSJames Wright   // Quadrature Point Loop
3343a8779fbSJames Wright   CeedPragmaSIMD
3353a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
3363a8779fbSJames Wright     CeedScalar q[5] = {0.};
3373a8779fbSJames Wright 
3383a8779fbSJames Wright     // Setup
3393a8779fbSJames Wright     // -- Coordinates
340bb8a0c61SJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
341bb8a0c61SJames Wright     const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
3423a8779fbSJames Wright 
3433a8779fbSJames Wright     // -- Density
344bb8a0c61SJames Wright     const CeedScalar rho = P0 / (Rd*theta0);
3453a8779fbSJames Wright 
3463a8779fbSJames Wright     // Initial Conditions
3473a8779fbSJames Wright     q[0] = rho;
3483a8779fbSJames Wright     q[1] = 0.0;
3493a8779fbSJames Wright     q[2] = 0.0;
3503a8779fbSJames Wright     q[3] = 0.0;
351bb8a0c61SJames Wright     q[4] = rho * (cv*theta0 + e_potential);
3523a8779fbSJames Wright 
3533a8779fbSJames Wright     for (CeedInt j=0; j<5; j++)
3543a8779fbSJames Wright       q0[j][i] = q[j];
3553a8779fbSJames Wright   } // End of Quadrature Point Loop
3563a8779fbSJames Wright   return 0;
3573a8779fbSJames Wright }
3583a8779fbSJames Wright 
3593a8779fbSJames Wright // *****************************************************************************
3603a8779fbSJames Wright // This QFunction implements the following formulation of Navier-Stokes with
3613a8779fbSJames Wright //   explicit time stepping method
3623a8779fbSJames Wright //
3633a8779fbSJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
3643a8779fbSJames Wright //   variables of density, momentum density, and total energy density.
3653a8779fbSJames Wright //
3663a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
3673a8779fbSJames Wright //   rho - Mass Density
3683a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
3693a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
3703a8779fbSJames Wright //
3713a8779fbSJames Wright // Navier-Stokes Equations:
3723a8779fbSJames Wright //   drho/dt + div( U )                               = 0
3733a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
3743a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
3753a8779fbSJames Wright //
3763a8779fbSJames Wright // Viscous Stress:
3773a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
3783a8779fbSJames Wright //
3793a8779fbSJames Wright // Thermal Stress:
3803a8779fbSJames Wright //   Fe = u Fu + k grad( T )
381bb8a0c61SJames Wright // Equation of State
3823a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
3833a8779fbSJames Wright //
3843a8779fbSJames Wright // Stabilization:
3853a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
3863a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
3873a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
3883a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
3893a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
3903a8779fbSJames Wright //     TauE = TauM / (Ce cv)
3913a8779fbSJames Wright //
3923a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
3933a8779fbSJames Wright //
3943a8779fbSJames Wright // Constants:
3953a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
3963a8779fbSJames Wright //   mu              ,  Dynamic viscosity
3973a8779fbSJames Wright //   k               ,  Thermal conductivity
3983a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
3993a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
4003a8779fbSJames Wright //   g               ,  Gravity
4013a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
4023a8779fbSJames Wright //
4033a8779fbSJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
4043a8779fbSJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
4053a8779fbSJames Wright // int( gradv gradu )
4063a8779fbSJames Wright //
4073a8779fbSJames Wright // *****************************************************************************
408*c1a52365SJed Brown CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q,
4093a8779fbSJames Wright                                       const CeedScalar *const *in, CeedScalar *const *out) {
4103a8779fbSJames Wright   // *INDENT-OFF*
4113a8779fbSJames Wright   // Inputs
4123a8779fbSJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
4133a8779fbSJames Wright                    (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
4143a8779fbSJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
4153a8779fbSJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
4163a8779fbSJames Wright   // Outputs
4173a8779fbSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
4183a8779fbSJames Wright              (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4193a8779fbSJames Wright   // *INDENT-ON*
4203a8779fbSJames Wright 
4213a8779fbSJames Wright   // Context
4223a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
4233a8779fbSJames Wright   const CeedScalar mu     = context->mu;
4243a8779fbSJames Wright   const CeedScalar cv     = context->cv;
4253a8779fbSJames Wright   const CeedScalar cp     = context->cp;
426bb8a0c61SJames Wright   const CeedScalar *g     = context->g;
427bb8a0c61SJames Wright   const CeedScalar dt     = context->dt;
4283a8779fbSJames Wright   const CeedScalar gamma  = cp / cv;
429bb8a0c61SJames Wright   const CeedScalar Rd     = cp - cv;
4303a8779fbSJames Wright 
4313a8779fbSJames Wright   CeedPragmaSIMD
4323a8779fbSJames Wright   // Quadrature Point Loop
4333a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
434*c1a52365SJed Brown     CeedScalar U[5];
435*c1a52365SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
436*c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
437*c1a52365SJed Brown     State s = StateFromU(context, U, x_i);
438*c1a52365SJed Brown 
4393a8779fbSJames Wright     // -- Interp-to-Interp q_data
4403a8779fbSJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
4413a8779fbSJames Wright     // -- Interp-to-Grad q_data
4423a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
4433a8779fbSJames Wright     // *INDENT-OFF*
4443a8779fbSJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
4453a8779fbSJames Wright                                     q_data[2][i],
4463a8779fbSJames Wright                                     q_data[3][i]},
4473a8779fbSJames Wright                                    {q_data[4][i],
4483a8779fbSJames Wright                                     q_data[5][i],
4493a8779fbSJames Wright                                     q_data[6][i]},
4503a8779fbSJames Wright                                    {q_data[7][i],
4513a8779fbSJames Wright                                     q_data[8][i],
4523a8779fbSJames Wright                                     q_data[9][i]}
4533a8779fbSJames Wright                                   };
4543a8779fbSJames Wright     // *INDENT-ON*
4553a8779fbSJames Wright 
456*c1a52365SJed Brown     State grad_s[3];
457*c1a52365SJed Brown     for (int j=0; j<3; j++) {
458*c1a52365SJed Brown       CeedScalar dx_i[3] = {0};
459*c1a52365SJed Brown       grad_s[j].U.density = dq[0][0][i] * dXdx[0][j]
460*c1a52365SJed Brown                             + dq[1][0][i] * dXdx[1][j] + dq[2][0][i] * dXdx[2][j];
461*c1a52365SJed Brown       for (int k=0; k<3; k++) grad_s[j].U.momentum[k] = dq[0][k+1][i] * dXdx[0][j]
462*c1a52365SJed Brown             + dq[1][k+1][i] * dXdx[1][j] + dq[2][k+1][i] * dXdx[2][j];
463*c1a52365SJed Brown       grad_s[j].U.E_total = dq[0][4][i] * dXdx[0][j] + dq[1][4][i] * dXdx[1][j] +
464*c1a52365SJed Brown                             dq[2][4][i] * dXdx[2][j];
465*c1a52365SJed Brown       dx_i[j] = 1.;
466*c1a52365SJed Brown       grad_s[j].Y = StatePrimitiveFromConservative_fwd(context, s, grad_s[j].U,
467*c1a52365SJed Brown                     x_i, dx_i);
468*c1a52365SJed Brown     }
469*c1a52365SJed Brown 
470*c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
471*c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
472*c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
473*c1a52365SJed Brown     KMUnpack(kmstress, stress);
474*c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
475*c1a52365SJed Brown 
476*c1a52365SJed Brown     StateConservative F_inviscid[3];
477*c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
478*c1a52365SJed Brown 
479*c1a52365SJed Brown     // Total flux
480*c1a52365SJed Brown     CeedScalar Flux[5][3];
481*c1a52365SJed Brown     for (int j=0; j<3; j++) {
482*c1a52365SJed Brown       Flux[0][j] = F_inviscid[j].density;
483*c1a52365SJed Brown       for (int k=0; k<3; k++)
484*c1a52365SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
485*c1a52365SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
486*c1a52365SJed Brown     }
487*c1a52365SJed Brown 
488*c1a52365SJed Brown     for (int j=0; j<3; j++) {
489*c1a52365SJed Brown       for (int k=0; k<5; k++) {
490*c1a52365SJed Brown         dv[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
491*c1a52365SJed Brown                                dXdx[j][1] * Flux[k][1] +
492*c1a52365SJed Brown                                dXdx[j][2] * Flux[k][2]);
493*c1a52365SJed Brown       }
494*c1a52365SJed Brown     }
495*c1a52365SJed Brown 
496*c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
497*c1a52365SJed Brown     for (int j=0; j<5; j++)
498*c1a52365SJed Brown       v[j][i] = wdetJ * body_force[j];
4993a8779fbSJames Wright 
5003a8779fbSJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
501*c1a52365SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
502*c1a52365SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
503*c1a52365SJed Brown                            gamma, g, x_i);
504*c1a52365SJed Brown     CeedScalar grad_U[5][3];
5053a8779fbSJames Wright     for (int j=0; j<3; j++) {
506*c1a52365SJed Brown       grad_U[0][j] = grad_s[j].U.density;
507*c1a52365SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
508*c1a52365SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
5093a8779fbSJames Wright     }
5103a8779fbSJames Wright 
5113a8779fbSJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
5123a8779fbSJames Wright     CeedScalar strong_conv[5] = {0};
5133a8779fbSJames Wright     for (int j=0; j<3; j++)
5143a8779fbSJames Wright       for (int k=0; k<5; k++)
5153a8779fbSJames Wright         for (int l=0; l<5; l++)
516*c1a52365SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
5173a8779fbSJames Wright 
518bb8a0c61SJames Wright     // -- Stabilization method: none, SU, or SUPG
519bb8a0c61SJames Wright     CeedScalar stab[5][3] = {{0.}};
520bb8a0c61SJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
521bb8a0c61SJames Wright     CeedScalar Tau_d[3] = {0.};
5223a8779fbSJames Wright     switch (context->stabilization) {
5233a8779fbSJames Wright     case STAB_NONE:        // Galerkin
5243a8779fbSJames Wright       break;
5253a8779fbSJames Wright     case STAB_SU:        // SU
526*c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
527bb8a0c61SJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
528bb8a0c61SJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
529bb8a0c61SJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
530bb8a0c61SJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
531bb8a0c61SJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
532*c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
533*c1a52365SJed Brown                                   tau_strong_conv,
534bb8a0c61SJames Wright                                   tau_strong_conv_conservative);
5353a8779fbSJames Wright       for (int j=0; j<3; j++)
5363a8779fbSJames Wright         for (int k=0; k<5; k++)
5373a8779fbSJames Wright           for (int l=0; l<5; l++)
538bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
5393a8779fbSJames Wright 
5403a8779fbSJames Wright       for (int j=0; j<5; j++)
5413a8779fbSJames Wright         for (int k=0; k<3; k++)
5423a8779fbSJames Wright           dv[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
5433a8779fbSJames Wright                                 stab[j][1] * dXdx[k][1] +
5443a8779fbSJames Wright                                 stab[j][2] * dXdx[k][2]);
5453a8779fbSJames Wright       break;
5463a8779fbSJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
5473a8779fbSJames Wright       break;
5483a8779fbSJames Wright     }
5493a8779fbSJames Wright 
5503a8779fbSJames Wright   } // End Quadrature Point Loop
5513a8779fbSJames Wright 
5523a8779fbSJames Wright   // Return
5533a8779fbSJames Wright   return 0;
5543a8779fbSJames Wright }
5553a8779fbSJames Wright 
5563a8779fbSJames Wright // *****************************************************************************
5573a8779fbSJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
5583a8779fbSJames Wright //   implicit time stepping method
5593a8779fbSJames Wright //
5603a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
5613a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
5623a8779fbSJames Wright //                                       (diffussive terms will be added later)
5633a8779fbSJames Wright //
5643a8779fbSJames Wright // *****************************************************************************
5653a8779fbSJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
5663a8779fbSJames Wright                                     const CeedScalar *const *in,
5673a8779fbSJames Wright                                     CeedScalar *const *out) {
5683a8779fbSJames Wright   // *INDENT-OFF*
5693a8779fbSJames Wright   // Inputs
5703a8779fbSJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
5713a8779fbSJames Wright                    (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
5723a8779fbSJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
5733a8779fbSJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
5743a8779fbSJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
5753a8779fbSJames Wright   // Outputs
5763a8779fbSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
5773a8779fbSJames Wright              (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
5783a8779fbSJames Wright   // *INDENT-ON*
5793a8779fbSJames Wright   // Context
5803a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
5813a8779fbSJames Wright   const CeedScalar mu     = context->mu;
5823a8779fbSJames Wright   const CeedScalar cv     = context->cv;
5833a8779fbSJames Wright   const CeedScalar cp     = context->cp;
584bb8a0c61SJames Wright   const CeedScalar *g     = context->g;
585bb8a0c61SJames Wright   const CeedScalar dt     = context->dt;
5863a8779fbSJames Wright   const CeedScalar gamma  = cp / cv;
587bb8a0c61SJames Wright   const CeedScalar Rd     = cp-cv;
5883a8779fbSJames Wright 
5893a8779fbSJames Wright   CeedPragmaSIMD
5903a8779fbSJames Wright   // Quadrature Point Loop
5913a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
592*c1a52365SJed Brown     CeedScalar U[5];
593*c1a52365SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
594*c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
595*c1a52365SJed Brown     State s = StateFromU(context, U, x_i);
596*c1a52365SJed Brown 
5973a8779fbSJames Wright     // -- Interp-to-Interp q_data
5983a8779fbSJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
5993a8779fbSJames Wright     // -- Interp-to-Grad q_data
6003a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
6013a8779fbSJames Wright     // *INDENT-OFF*
6023a8779fbSJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
6033a8779fbSJames Wright                                     q_data[2][i],
6043a8779fbSJames Wright                                     q_data[3][i]},
6053a8779fbSJames Wright                                    {q_data[4][i],
6063a8779fbSJames Wright                                     q_data[5][i],
6073a8779fbSJames Wright                                     q_data[6][i]},
6083a8779fbSJames Wright                                    {q_data[7][i],
6093a8779fbSJames Wright                                     q_data[8][i],
6103a8779fbSJames Wright                                     q_data[9][i]}
6113a8779fbSJames Wright                                   };
6123a8779fbSJames Wright     // *INDENT-ON*
613*c1a52365SJed Brown     State grad_s[3];
6143a8779fbSJames Wright     for (int j=0; j<3; j++) {
615*c1a52365SJed Brown       CeedScalar dx_i[3];
616*c1a52365SJed Brown       grad_s[j].U.density = dq[0][0][i] * dXdx[0][j]
617*c1a52365SJed Brown                             + dq[1][0][i] * dXdx[1][j] + dq[2][0][i] * dXdx[2][j];
618*c1a52365SJed Brown       for (int k=0; k<3; k++) grad_s[j].U.momentum[k] = dq[0][k+1][i] * dXdx[0][j]
619*c1a52365SJed Brown             + dq[1][k+1][i] * dXdx[1][j] + dq[2][k+1][i] * dXdx[2][j];
620*c1a52365SJed Brown       grad_s[j].U.E_total = dq[0][4][i] * dXdx[0][j] + dq[1][4][i] * dXdx[1][j] +
621*c1a52365SJed Brown                             dq[2][4][i] * dXdx[2][j];
622*c1a52365SJed Brown       dx_i[j] = 1.;
623*c1a52365SJed Brown       grad_s[j].Y = StatePrimitiveFromConservative_fwd(context, s, grad_s[j].U,
624*c1a52365SJed Brown                     x_i, dx_i);
6253a8779fbSJames Wright     }
626*c1a52365SJed Brown 
627*c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
628*c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
629*c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
630*c1a52365SJed Brown     KMUnpack(kmstress, stress);
631*c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
632*c1a52365SJed Brown 
633*c1a52365SJed Brown     StateConservative F_inviscid[3];
634*c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
635*c1a52365SJed Brown 
636*c1a52365SJed Brown 
637*c1a52365SJed Brown     // Total flux
638*c1a52365SJed Brown     CeedScalar Flux[5][3];
639*c1a52365SJed Brown     for (int j=0; j<3; j++) {
640*c1a52365SJed Brown       Flux[0][j] = F_inviscid[j].density;
6413a8779fbSJames Wright       for (int k=0; k<3; k++)
642*c1a52365SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
643*c1a52365SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
644*c1a52365SJed Brown     }
645*c1a52365SJed Brown 
646*c1a52365SJed Brown     for (int j=0; j<3; j++) {
647*c1a52365SJed Brown       for (int k=0; k<5; k++) {
648*c1a52365SJed Brown         dv[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
649*c1a52365SJed Brown                                 dXdx[j][1] * Flux[k][1] +
650*c1a52365SJed Brown                                 dXdx[j][2] * Flux[k][2]);
651*c1a52365SJed Brown       }
652*c1a52365SJed Brown     }
653*c1a52365SJed Brown 
654*c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
655*c1a52365SJed Brown     for (int j=0; j<5; j++)
656*c1a52365SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
6573a8779fbSJames Wright 
6583a8779fbSJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
659*c1a52365SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
660*c1a52365SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
661*c1a52365SJed Brown                            gamma, g, x_i);
662*c1a52365SJed Brown     CeedScalar grad_U[5][3];
6633a8779fbSJames Wright     for (int j=0; j<3; j++) {
664*c1a52365SJed Brown       grad_U[0][j] = grad_s[j].U.density;
665*c1a52365SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
666*c1a52365SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
6673a8779fbSJames Wright     }
668*c1a52365SJed Brown 
6693a8779fbSJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
6703a8779fbSJames Wright     CeedScalar strong_conv[5] = {0};
6713a8779fbSJames Wright     for (int j=0; j<3; j++)
6723a8779fbSJames Wright       for (int k=0; k<5; k++)
6733a8779fbSJames Wright         for (int l=0; l<5; l++)
674*c1a52365SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
6753a8779fbSJames Wright 
6763a8779fbSJames Wright     // Strong residual
6773a8779fbSJames Wright     CeedScalar strong_res[5];
6783a8779fbSJames Wright     for (int j=0; j<5; j++)
6793a8779fbSJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
6803a8779fbSJames Wright 
6813a8779fbSJames Wright     // -- Stabilization method: none, SU, or SUPG
682bb8a0c61SJames Wright     CeedScalar stab[5][3] = {{0.}};
683bb8a0c61SJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
684bb8a0c61SJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
685bb8a0c61SJames Wright     CeedScalar Tau_d[3] = {0.};
6863a8779fbSJames Wright     switch (context->stabilization) {
6873a8779fbSJames Wright     case STAB_NONE:        // Galerkin
6883a8779fbSJames Wright       break;
6893a8779fbSJames Wright     case STAB_SU:        // SU
690*c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
691bb8a0c61SJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
692bb8a0c61SJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
693bb8a0c61SJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
694bb8a0c61SJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
695bb8a0c61SJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
696*c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
697*c1a52365SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
6983a8779fbSJames Wright       for (int j=0; j<3; j++)
6993a8779fbSJames Wright         for (int k=0; k<5; k++)
7003a8779fbSJames Wright           for (int l=0; l<5; l++)
701bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
7023a8779fbSJames Wright 
7033a8779fbSJames Wright       for (int j=0; j<5; j++)
7043a8779fbSJames Wright         for (int k=0; k<3; k++)
7053a8779fbSJames Wright           dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
7063a8779fbSJames Wright                                 stab[j][1] * dXdx[k][1] +
7073a8779fbSJames Wright                                 stab[j][2] * dXdx[k][2]);
7083a8779fbSJames Wright       break;
7093a8779fbSJames Wright     case STAB_SUPG:        // SUPG
710*c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
711bb8a0c61SJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
712bb8a0c61SJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
713bb8a0c61SJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
714bb8a0c61SJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
715bb8a0c61SJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
716bb8a0c61SJames Wright // Alternate route (useful later with primitive variable code)
717bb8a0c61SJames Wright // this function was verified against PHASTA for as IC that was as close as possible
718bb8a0c61SJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
719bb8a0c61SJames Wright // it has also been verified to compute a correct through the following
720bb8a0c61SJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
721bb8a0c61SJames Wright // applied in the triple loop below
722bb8a0c61SJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
723*c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
724*c1a52365SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
7253a8779fbSJames Wright       for (int j=0; j<3; j++)
7263a8779fbSJames Wright         for (int k=0; k<5; k++)
7273a8779fbSJames Wright           for (int l=0; l<5; l++)
728bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
7293a8779fbSJames Wright 
7303a8779fbSJames Wright       for (int j=0; j<5; j++)
7313a8779fbSJames Wright         for (int k=0; k<3; k++)
7323a8779fbSJames Wright           dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
7333a8779fbSJames Wright                                 stab[j][1] * dXdx[k][1] +
7343a8779fbSJames Wright                                 stab[j][2] * dXdx[k][2]);
7353a8779fbSJames Wright       break;
7363a8779fbSJames Wright     }
7373a8779fbSJames Wright 
7383a8779fbSJames Wright   } // End Quadrature Point Loop
7393a8779fbSJames Wright 
7403a8779fbSJames Wright   // Return
7413a8779fbSJames Wright   return 0;
7423a8779fbSJames Wright }
7433a8779fbSJames Wright // *****************************************************************************
7443a8779fbSJames Wright #endif // newtonian_h
745