xref: /honee/qfunctions/newtonian.h (revision f0b653724bc7ff3458409f59c4d1e4b1ef161295)
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 
23c1a52365SJed Brown typedef struct {
24c1a52365SJed Brown   CeedScalar pressure;
25c1a52365SJed Brown   CeedScalar velocity[3];
26c1a52365SJed Brown   CeedScalar temperature;
27c1a52365SJed Brown } StatePrimitive;
28c1a52365SJed Brown 
29c1a52365SJed Brown typedef struct {
30c1a52365SJed Brown   CeedScalar density;
31c1a52365SJed Brown   CeedScalar momentum[3];
32c1a52365SJed Brown   CeedScalar E_total;
33c1a52365SJed Brown } StateConservative;
34c1a52365SJed Brown 
35c1a52365SJed Brown typedef struct {
36c1a52365SJed Brown   StateConservative U;
37c1a52365SJed Brown   StatePrimitive Y;
38c1a52365SJed Brown } State;
39c1a52365SJed Brown 
40c1a52365SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
41c1a52365SJed Brown   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
42c1a52365SJed Brown   StatePrimitive Y;
43c1a52365SJed Brown   for (int i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
44c1a52365SJed Brown   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
45c1a52365SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
46c1a52365SJed Brown   CeedScalar e_total = U.E_total / U.density;
47c1a52365SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
48c1a52365SJed Brown   Y.temperature = e_internal / gas->cv;
49c1a52365SJed Brown   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
50c1a52365SJed Brown   return Y;
51c1a52365SJed Brown }
52c1a52365SJed Brown 
53c1a52365SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
54c1a52365SJed Brown   NewtonianIdealGasContext gas, State s, StateConservative dU,
55c1a52365SJed Brown   const CeedScalar x[3], const CeedScalar dx[3]) {
56c1a52365SJed Brown   StatePrimitive dY;
57c1a52365SJed Brown   for (int i=0; i<3; i++) {
58c1a52365SJed Brown     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
59c1a52365SJed Brown   }
60c1a52365SJed Brown   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
61c1a52365SJed Brown   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
62c1a52365SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
63c1a52365SJed Brown   CeedScalar de_potential = -Dot3(gas->g, dx);
64c1a52365SJed Brown   CeedScalar e_total = s.U.E_total / s.U.density;
65c1a52365SJed Brown   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
66c1a52365SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
67c1a52365SJed Brown   CeedScalar de_internal = de_total - de_kinetic - de_potential;
68c1a52365SJed Brown   dY.temperature = de_internal / gas->cv;
69c1a52365SJed Brown   dY.pressure = (gas->cp / gas->cv - 1)
70c1a52365SJed Brown                 * (dU.density * e_internal + s.U.density * de_internal);
71c1a52365SJed Brown   return dY;
72c1a52365SJed Brown }
73c1a52365SJed Brown 
74c1a52365SJed Brown CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
75c1a52365SJed Brown                                        const CeedScalar U[5], const CeedScalar x[3]) {
76c1a52365SJed Brown   State s;
77c1a52365SJed Brown   s.U.density = U[0];
78c1a52365SJed Brown   s.U.momentum[0] = U[1];
79c1a52365SJed Brown   s.U.momentum[1] = U[2];
80c1a52365SJed Brown   s.U.momentum[2] = U[3];
81c1a52365SJed Brown   s.U.E_total = U[4];
82c1a52365SJed Brown   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
83c1a52365SJed Brown   return s;
84c1a52365SJed Brown }
85c1a52365SJed Brown 
862f7ce6c1SJed Brown CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
872f7ce6c1SJed Brown     State s, const CeedScalar dU[5],
882f7ce6c1SJed Brown     const CeedScalar x[3], const CeedScalar dx[3]) {
892f7ce6c1SJed Brown   State ds;
902f7ce6c1SJed Brown   ds.U.density = dU[0];
912f7ce6c1SJed Brown   ds.U.momentum[0] = dU[1];
922f7ce6c1SJed Brown   ds.U.momentum[1] = dU[2];
932f7ce6c1SJed Brown   ds.U.momentum[2] = dU[3];
942f7ce6c1SJed Brown   ds.U.E_total = dU[4];
952f7ce6c1SJed Brown   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
962f7ce6c1SJed Brown   return ds;
972f7ce6c1SJed Brown }
982f7ce6c1SJed Brown 
99c1a52365SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
100c1a52365SJed Brown                                         StateConservative Flux[3]) {
101c1a52365SJed Brown   for (int i=0; i<3; i++) {
102c1a52365SJed Brown     Flux[i].density = s.U.momentum[i];
103c1a52365SJed Brown     for (int j=0; j<3; j++)
104c1a52365SJed Brown       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
105c1a52365SJed Brown                             + s.Y.pressure * (i == j);
106c1a52365SJed Brown     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
107c1a52365SJed Brown   }
108c1a52365SJed Brown }
109c1a52365SJed Brown 
110c1a52365SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
111c1a52365SJed Brown     State s, State ds, StateConservative dFlux[3]) {
112c1a52365SJed Brown   for (int i=0; i<3; i++) {
113c1a52365SJed Brown     dFlux[i].density = ds.U.momentum[i];
114c1a52365SJed Brown     for (int j=0; j<3; j++)
115c1a52365SJed Brown       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
116c1a52365SJed Brown                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
117c1a52365SJed Brown     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
118c1a52365SJed Brown                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
119c1a52365SJed Brown   }
120c1a52365SJed Brown }
121c1a52365SJed Brown 
122c1a52365SJed Brown // Kelvin-Mandel notation
123c1a52365SJed Brown CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
124c1a52365SJed Brown                                         CeedScalar strain_rate[6]) {
125c1a52365SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
126c1a52365SJed Brown   strain_rate[0] = grad_s[0].Y.velocity[0];
127c1a52365SJed Brown   strain_rate[1] = grad_s[1].Y.velocity[1];
128c1a52365SJed Brown   strain_rate[2] = grad_s[2].Y.velocity[2];
129c1a52365SJed Brown   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
130c1a52365SJed Brown   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
131c1a52365SJed Brown   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
132c1a52365SJed Brown }
133c1a52365SJed Brown 
134c1a52365SJed Brown CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
135c1a52365SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
136c1a52365SJed Brown   A[0][0] = v[0];
137c1a52365SJed Brown   A[1][1] = v[1];
138c1a52365SJed Brown   A[2][2] = v[2];
139c1a52365SJed Brown   A[2][1] = A[1][2] = weight * v[3];
140c1a52365SJed Brown   A[2][0] = A[0][2] = weight * v[4];
141c1a52365SJed Brown   A[1][0] = A[0][1] = weight * v[5];
142c1a52365SJed Brown }
143c1a52365SJed Brown 
144c1a52365SJed Brown CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
145c1a52365SJed Brown     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
146c1a52365SJed Brown   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
147c1a52365SJed Brown   for (int i=0; i<6; i++) {
148c1a52365SJed Brown     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
149c1a52365SJed Brown   }
150c1a52365SJed Brown }
151c1a52365SJed Brown 
152c1a52365SJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
153c1a52365SJed Brown     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
154c1a52365SJed Brown     CeedScalar Fe[3]) {
155c1a52365SJed Brown   for (int i=0; i<3; i++) {
156c1a52365SJed Brown     Fe[i] = - Y.velocity[0] * stress[0][i]
157c1a52365SJed Brown             - Y.velocity[1] * stress[1][i]
158c1a52365SJed Brown             - Y.velocity[2] * stress[2][i]
159c1a52365SJed Brown             - gas->k * grad_s[i].Y.temperature;
160c1a52365SJed Brown   }
161c1a52365SJed Brown }
162c1a52365SJed Brown 
163*f0b65372SJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
164*f0b65372SJed Brown     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
165*f0b65372SJed Brown     const CeedScalar stress[3][3],
166*f0b65372SJed Brown     const CeedScalar dstress[3][3],
167*f0b65372SJed Brown     CeedScalar dFe[3]) {
168*f0b65372SJed Brown   for (int i=0; i<3; i++) {
169*f0b65372SJed Brown     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
170*f0b65372SJed Brown              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
171*f0b65372SJed Brown              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
172*f0b65372SJed Brown              - gas->k * grad_ds[i].Y.temperature;
173*f0b65372SJed Brown   }
174*f0b65372SJed Brown }
1753a8779fbSJames Wright // *****************************************************************************
1763a8779fbSJames Wright // Helper function for computing flux Jacobian
1773a8779fbSJames Wright // *****************************************************************************
1783a8779fbSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5],
1793a8779fbSJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
180bb8a0c61SJames Wright     const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) {
1813a8779fbSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
182bb8a0c61SJames Wright   CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
1833a8779fbSJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
1843a8779fbSJames Wright     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
185bb8a0c61SJames Wright       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) -
186bb8a0c61SJames Wright                       u[i]*u[j];
1873a8779fbSJames Wright       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
1883a8779fbSJames Wright         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
1893a8779fbSJames Wright         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
1903a8779fbSJames Wright                           ((i==k) ? u[j] : 0.) -
1913a8779fbSJames Wright                           ((i==j) ? u[k] : 0.) * (gamma-1.);
1923a8779fbSJames Wright         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
1933a8779fbSJames Wright                           (gamma-1.)*u[i]*u[k];
1943a8779fbSJames Wright       }
1953a8779fbSJames Wright       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
1963a8779fbSJames Wright     }
1973a8779fbSJames Wright     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
1983a8779fbSJames Wright     dF[i][4][4] = u[i] * gamma;
1993a8779fbSJames Wright   }
2003a8779fbSJames Wright }
2013a8779fbSJames Wright 
2023a8779fbSJames Wright // *****************************************************************************
203bb8a0c61SJames Wright // Helper function for computing flux Jacobian of Primitive variables
204bb8a0c61SJames Wright // *****************************************************************************
205bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5],
206bb8a0c61SJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
207bb8a0c61SJames Wright     const CeedScalar Rd, const CeedScalar cv) {
208bb8a0c61SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
209bb8a0c61SJames Wright   // TODO Add in gravity's contribution
210bb8a0c61SJames Wright 
211bb8a0c61SJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
212bb8a0c61SJames Wright   CeedScalar drdT = -rho / T;
213bb8a0c61SJames Wright   CeedScalar drdP = 1. / ( Rd * T);
214bb8a0c61SJames Wright   CeedScalar etot =  E / rho ;
215bb8a0c61SJames Wright   CeedScalar e2p  = drdP * etot + 1. ;
216bb8a0c61SJames Wright   CeedScalar e3p  = ( E  + rho * Rd * T );
217bb8a0c61SJames Wright   CeedScalar e4p  = drdT * etot + rho * cv ;
218bb8a0c61SJames Wright 
219bb8a0c61SJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
220bb8a0c61SJames Wright     for (CeedInt j=0; j<3; j++) { // j counts F^{m_j}
221bb8a0c61SJames Wright //        [row][col] of A_i
222bb8a0c61SJames Wright       dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p
223bb8a0c61SJames Wright       for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k
2242acc7cbcSKenneth E. Jansen         dF[i][0][k+1]   =  ((i==k) ? rho  : 0.);   // F^c wrt u_k
225bb8a0c61SJames Wright         dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) +  // F^m_j wrt u_k
226bb8a0c61SJames Wright                            ((i==k) ? u[j] : 0.) ) * rho;
227bb8a0c61SJames Wright         dF[i][4][k+1]   = rho * u[i] * u[k]
228bb8a0c61SJames Wright                           + ((i==k) ? e3p  : 0.) ; // F^e wrt u_k
229bb8a0c61SJames Wright       }
230bb8a0c61SJames Wright       dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T
231bb8a0c61SJames Wright     }
232bb8a0c61SJames Wright     dF[i][4][0] = u[i] * e2p; // F^e wrt p
233bb8a0c61SJames Wright     dF[i][4][4] = u[i] * e4p; // F^e wrt T
234bb8a0c61SJames Wright     dF[i][0][0] = u[i] * drdP; // F^c wrt p
235bb8a0c61SJames Wright     dF[i][0][4] = u[i] * drdT; // F^c wrt T
236bb8a0c61SJames Wright   }
237bb8a0c61SJames Wright }
238bb8a0c61SJames Wright 
239bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho,
240bb8a0c61SJames Wright     const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd,
241bb8a0c61SJames Wright     const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) {
242bb8a0c61SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2];
243bb8a0c61SJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
244bb8a0c61SJames Wright   CeedScalar drdT = -rho / T;
245bb8a0c61SJames Wright   CeedScalar drdP = 1. / ( Rd * T);
246bb8a0c61SJames Wright   dU[0] = drdP * dY[0] + drdT * dY[4];
247bb8a0c61SJames Wright   CeedScalar de_kinetic = 0;
248bb8a0c61SJames Wright   for (int i=0; i<3; i++) {
249bb8a0c61SJames Wright     dU[1+i] = dU[0] * u[i] + rho * dY[1+i];
250bb8a0c61SJames Wright     de_kinetic += u[i] * dY[1+i];
251bb8a0c61SJames Wright   }
252bb8a0c61SJames Wright   dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e
253bb8a0c61SJames Wright           + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2
254bb8a0c61SJames Wright }
255bb8a0c61SJames Wright 
256bb8a0c61SJames Wright // *****************************************************************************
257bb8a0c61SJames Wright // Helper function for computing Tau elements (stabilization constant)
258bb8a0c61SJames Wright //   Model from:
259bb8a0c61SJames Wright //     PHASTA
260bb8a0c61SJames Wright //
261bb8a0c61SJames Wright //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
262bb8a0c61SJames Wright //
263bb8a0c61SJames Wright // Where NOT UPDATED YET
264bb8a0c61SJames Wright // *****************************************************************************
265bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3],
266bb8a0c61SJames Wright                                         const CeedScalar dXdx[3][3], const CeedScalar u[3],
267bb8a0c61SJames Wright                                         const CeedScalar cv, const NewtonianIdealGasContext newt_ctx,
268bb8a0c61SJames Wright                                         const CeedScalar mu, const CeedScalar dt,
269bb8a0c61SJames Wright                                         const CeedScalar rho) {
270bb8a0c61SJames Wright   // Context
271bb8a0c61SJames Wright   const CeedScalar Ctau_t = newt_ctx->Ctau_t;
272bb8a0c61SJames Wright   const CeedScalar Ctau_v = newt_ctx->Ctau_v;
273bb8a0c61SJames Wright   const CeedScalar Ctau_C = newt_ctx->Ctau_C;
274bb8a0c61SJames Wright   const CeedScalar Ctau_M = newt_ctx->Ctau_M;
275bb8a0c61SJames Wright   const CeedScalar Ctau_E = newt_ctx->Ctau_E;
276bb8a0c61SJames Wright   CeedScalar gijd[6];
277bb8a0c61SJames Wright   CeedScalar tau;
278bb8a0c61SJames Wright   CeedScalar dts;
279bb8a0c61SJames Wright   CeedScalar fact;
280bb8a0c61SJames Wright 
281bb8a0c61SJames Wright   //*INDENT-OFF*
282bb8a0c61SJames Wright   gijd[0] =   dXdx[0][0] * dXdx[0][0]
283bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][0]
284bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][0];
285bb8a0c61SJames Wright 
286bb8a0c61SJames Wright   gijd[1] =   dXdx[0][0] * dXdx[0][1]
287bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][1]
288bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][1];
289bb8a0c61SJames Wright 
290bb8a0c61SJames Wright   gijd[2] =   dXdx[0][1] * dXdx[0][1]
291bb8a0c61SJames Wright             + dXdx[1][1] * dXdx[1][1]
292bb8a0c61SJames Wright             + dXdx[2][1] * dXdx[2][1];
293bb8a0c61SJames Wright 
294bb8a0c61SJames Wright   gijd[3] =   dXdx[0][0] * dXdx[0][2]
295bb8a0c61SJames Wright             + dXdx[1][0] * dXdx[1][2]
296bb8a0c61SJames Wright             + dXdx[2][0] * dXdx[2][2];
297bb8a0c61SJames Wright 
298bb8a0c61SJames Wright   gijd[4] =   dXdx[0][1] * dXdx[0][2]
299bb8a0c61SJames Wright             + dXdx[1][1] * dXdx[1][2]
300bb8a0c61SJames Wright             + dXdx[2][1] * dXdx[2][2];
301bb8a0c61SJames Wright 
302bb8a0c61SJames Wright   gijd[5] =   dXdx[0][2] * dXdx[0][2]
303bb8a0c61SJames Wright             + dXdx[1][2] * dXdx[1][2]
304bb8a0c61SJames Wright             + dXdx[2][2] * dXdx[2][2];
305bb8a0c61SJames Wright   //*INDENT-ON*
306bb8a0c61SJames Wright 
307bb8a0c61SJames Wright   dts = Ctau_t / dt ;
308bb8a0c61SJames Wright 
309bb8a0c61SJames Wright   tau = rho*rho*((4. * dts * dts)
310bb8a0c61SJames Wright                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
311bb8a0c61SJames Wright                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
312bb8a0c61SJames Wright                  + u[2] *   u[2] * gijd[5])
313bb8a0c61SJames Wright         + Ctau_v* mu * mu *
314bb8a0c61SJames Wright         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
315bb8a0c61SJames Wright          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
316bb8a0c61SJames Wright 
317bb8a0c61SJames Wright   fact=sqrt(tau);
318bb8a0c61SJames Wright 
319bb8a0c61SJames Wright   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
320bb8a0c61SJames Wright 
321bb8a0c61SJames Wright   Tau_d[1] = Ctau_M / fact;
322bb8a0c61SJames Wright   Tau_d[2] = Ctau_E / ( fact * cv );
323bb8a0c61SJames Wright 
324bb8a0c61SJames Wright // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
325bb8a0c61SJames Wright //  to avoid a division if the compiler is smart enough to see that cv IS
326bb8a0c61SJames Wright // a constant that it could invert once for all elements
327bb8a0c61SJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
328bb8a0c61SJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to
329bb8a0c61SJames Wright // know how to change constants with a change of fluid or units.  Same for
330bb8a0c61SJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
331bb8a0c61SJames Wright }
332bb8a0c61SJames Wright 
333bb8a0c61SJames Wright // *****************************************************************************
3343a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
3353a8779fbSJames Wright // *****************************************************************************
3363a8779fbSJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q,
3373a8779fbSJames Wright                                const CeedScalar *const *in, CeedScalar *const *out) {
3383a8779fbSJames Wright   // Inputs
3393a8779fbSJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3403a8779fbSJames Wright 
3413a8779fbSJames Wright   // Outputs
3423a8779fbSJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3433a8779fbSJames Wright 
344bb8a0c61SJames Wright   // Context
345bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
346bb8a0c61SJames Wright   const CeedScalar theta0    = context->theta0;
347bb8a0c61SJames Wright   const CeedScalar P0        = context->P0;
348bb8a0c61SJames Wright   const CeedScalar cv        = context->cv;
349bb8a0c61SJames Wright   const CeedScalar cp        = context->cp;
350bb8a0c61SJames Wright   const CeedScalar *g        = context->g;
351bb8a0c61SJames Wright   const CeedScalar Rd        = cp - cv;
352bb8a0c61SJames Wright 
3533a8779fbSJames Wright   // Quadrature Point Loop
3543a8779fbSJames Wright   CeedPragmaSIMD
3553a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
3563a8779fbSJames Wright     CeedScalar q[5] = {0.};
3573a8779fbSJames Wright 
3583a8779fbSJames Wright     // Setup
3593a8779fbSJames Wright     // -- Coordinates
360bb8a0c61SJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
361bb8a0c61SJames Wright     const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
3623a8779fbSJames Wright 
3633a8779fbSJames Wright     // -- Density
364bb8a0c61SJames Wright     const CeedScalar rho = P0 / (Rd*theta0);
3653a8779fbSJames Wright 
3663a8779fbSJames Wright     // Initial Conditions
3673a8779fbSJames Wright     q[0] = rho;
3683a8779fbSJames Wright     q[1] = 0.0;
3693a8779fbSJames Wright     q[2] = 0.0;
3703a8779fbSJames Wright     q[3] = 0.0;
371bb8a0c61SJames Wright     q[4] = rho * (cv*theta0 + e_potential);
3723a8779fbSJames Wright 
3733a8779fbSJames Wright     for (CeedInt j=0; j<5; j++)
3743a8779fbSJames Wright       q0[j][i] = q[j];
3753a8779fbSJames Wright   } // End of Quadrature Point Loop
3763a8779fbSJames Wright   return 0;
3773a8779fbSJames Wright }
3783a8779fbSJames Wright 
3793a8779fbSJames Wright // *****************************************************************************
3803a8779fbSJames Wright // This QFunction implements the following formulation of Navier-Stokes with
3813a8779fbSJames Wright //   explicit time stepping method
3823a8779fbSJames Wright //
3833a8779fbSJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
3843a8779fbSJames Wright //   variables of density, momentum density, and total energy density.
3853a8779fbSJames Wright //
3863a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
3873a8779fbSJames Wright //   rho - Mass Density
3883a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
3893a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
3903a8779fbSJames Wright //
3913a8779fbSJames Wright // Navier-Stokes Equations:
3923a8779fbSJames Wright //   drho/dt + div( U )                               = 0
3933a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
3943a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
3953a8779fbSJames Wright //
3963a8779fbSJames Wright // Viscous Stress:
3973a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
3983a8779fbSJames Wright //
3993a8779fbSJames Wright // Thermal Stress:
4003a8779fbSJames Wright //   Fe = u Fu + k grad( T )
401bb8a0c61SJames Wright // Equation of State
4023a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
4033a8779fbSJames Wright //
4043a8779fbSJames Wright // Stabilization:
4053a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
4063a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
4073a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
4083a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
4093a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
4103a8779fbSJames Wright //     TauE = TauM / (Ce cv)
4113a8779fbSJames Wright //
4123a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
4133a8779fbSJames Wright //
4143a8779fbSJames Wright // Constants:
4153a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
4163a8779fbSJames Wright //   mu              ,  Dynamic viscosity
4173a8779fbSJames Wright //   k               ,  Thermal conductivity
4183a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
4193a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
4203a8779fbSJames Wright //   g               ,  Gravity
4213a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
4223a8779fbSJames Wright //
4233a8779fbSJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
4243a8779fbSJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
4253a8779fbSJames Wright // int( gradv gradu )
4263a8779fbSJames Wright //
4273a8779fbSJames Wright // *****************************************************************************
428c1a52365SJed Brown CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q,
4293a8779fbSJames Wright                                       const CeedScalar *const *in, CeedScalar *const *out) {
4303a8779fbSJames Wright   // *INDENT-OFF*
4313a8779fbSJames Wright   // Inputs
4323a8779fbSJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
433752f40e3SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
4343a8779fbSJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
4353a8779fbSJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
4363a8779fbSJames Wright   // Outputs
4373a8779fbSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
438752f40e3SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4393a8779fbSJames Wright   // *INDENT-ON*
4403a8779fbSJames Wright 
4413a8779fbSJames Wright   // Context
4423a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
4433a8779fbSJames Wright   const CeedScalar mu     = context->mu;
4443a8779fbSJames Wright   const CeedScalar cv     = context->cv;
4453a8779fbSJames Wright   const CeedScalar cp     = context->cp;
446bb8a0c61SJames Wright   const CeedScalar *g     = context->g;
447bb8a0c61SJames Wright   const CeedScalar dt     = context->dt;
4483a8779fbSJames Wright   const CeedScalar gamma  = cp / cv;
449bb8a0c61SJames Wright   const CeedScalar Rd     = cp - cv;
4503a8779fbSJames Wright 
4513a8779fbSJames Wright   CeedPragmaSIMD
4523a8779fbSJames Wright   // Quadrature Point Loop
4533a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
454c1a52365SJed Brown     CeedScalar U[5];
455c1a52365SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
456c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
457c1a52365SJed Brown     State s = StateFromU(context, U, x_i);
458c1a52365SJed Brown 
4593a8779fbSJames Wright     // -- Interp-to-Interp q_data
4603a8779fbSJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
4613a8779fbSJames Wright     // -- Interp-to-Grad q_data
4623a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
4633a8779fbSJames Wright     // *INDENT-OFF*
4643a8779fbSJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
4653a8779fbSJames Wright                                     q_data[2][i],
4663a8779fbSJames Wright                                     q_data[3][i]},
4673a8779fbSJames Wright                                    {q_data[4][i],
4683a8779fbSJames Wright                                     q_data[5][i],
4693a8779fbSJames Wright                                     q_data[6][i]},
4703a8779fbSJames Wright                                    {q_data[7][i],
4713a8779fbSJames Wright                                     q_data[8][i],
4723a8779fbSJames Wright                                     q_data[9][i]}
4733a8779fbSJames Wright                                   };
4743a8779fbSJames Wright     // *INDENT-ON*
4753a8779fbSJames Wright 
476c1a52365SJed Brown     State grad_s[3];
477c1a52365SJed Brown     for (int j=0; j<3; j++) {
4782f7ce6c1SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
479752f40e3SJed Brown       for (int k=0; k<5; k++) dU[k] = Grad_q[0][k][i] * dXdx[0][j]
480752f40e3SJed Brown                                         + Grad_q[1][k][i] * dXdx[1][j]
481752f40e3SJed Brown                                         + Grad_q[2][k][i] * dXdx[2][j];
482c1a52365SJed Brown       dx_i[j] = 1.;
4832f7ce6c1SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
484c1a52365SJed Brown     }
485c1a52365SJed Brown 
486c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
487c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
488c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
489c1a52365SJed Brown     KMUnpack(kmstress, stress);
490c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
491c1a52365SJed Brown 
492c1a52365SJed Brown     StateConservative F_inviscid[3];
493c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
494c1a52365SJed Brown 
495c1a52365SJed Brown     // Total flux
496c1a52365SJed Brown     CeedScalar Flux[5][3];
497c1a52365SJed Brown     for (int j=0; j<3; j++) {
498c1a52365SJed Brown       Flux[0][j] = F_inviscid[j].density;
499c1a52365SJed Brown       for (int k=0; k<3; k++)
500c1a52365SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
501c1a52365SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
502c1a52365SJed Brown     }
503c1a52365SJed Brown 
504c1a52365SJed Brown     for (int j=0; j<3; j++) {
505c1a52365SJed Brown       for (int k=0; k<5; k++) {
506752f40e3SJed Brown         Grad_v[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
507c1a52365SJed Brown                                    dXdx[j][1] * Flux[k][1] +
508c1a52365SJed Brown                                    dXdx[j][2] * Flux[k][2]);
509c1a52365SJed Brown       }
510c1a52365SJed Brown     }
511c1a52365SJed Brown 
512c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
513c1a52365SJed Brown     for (int j=0; j<5; j++)
514c1a52365SJed Brown       v[j][i] = wdetJ * body_force[j];
5153a8779fbSJames Wright 
5163a8779fbSJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
517c1a52365SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
518c1a52365SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
519c1a52365SJed Brown                            gamma, g, x_i);
520c1a52365SJed Brown     CeedScalar grad_U[5][3];
5213a8779fbSJames Wright     for (int j=0; j<3; j++) {
522c1a52365SJed Brown       grad_U[0][j] = grad_s[j].U.density;
523c1a52365SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
524c1a52365SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
5253a8779fbSJames Wright     }
5263a8779fbSJames Wright 
5273a8779fbSJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
5283a8779fbSJames Wright     CeedScalar strong_conv[5] = {0};
5293a8779fbSJames Wright     for (int j=0; j<3; j++)
5303a8779fbSJames Wright       for (int k=0; k<5; k++)
5313a8779fbSJames Wright         for (int l=0; l<5; l++)
532c1a52365SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
5333a8779fbSJames Wright 
534bb8a0c61SJames Wright     // -- Stabilization method: none, SU, or SUPG
535bb8a0c61SJames Wright     CeedScalar stab[5][3] = {{0.}};
536bb8a0c61SJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
537bb8a0c61SJames Wright     CeedScalar Tau_d[3] = {0.};
5383a8779fbSJames Wright     switch (context->stabilization) {
5393a8779fbSJames Wright     case STAB_NONE:        // Galerkin
5403a8779fbSJames Wright       break;
5413a8779fbSJames Wright     case STAB_SU:        // SU
542c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
543bb8a0c61SJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
544bb8a0c61SJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
545bb8a0c61SJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
546bb8a0c61SJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
547bb8a0c61SJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
548c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
549c1a52365SJed Brown                                   tau_strong_conv,
550bb8a0c61SJames Wright                                   tau_strong_conv_conservative);
5513a8779fbSJames Wright       for (int j=0; j<3; j++)
5523a8779fbSJames Wright         for (int k=0; k<5; k++)
5533a8779fbSJames Wright           for (int l=0; l<5; l++)
554bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
5553a8779fbSJames Wright 
5563a8779fbSJames Wright       for (int j=0; j<5; j++)
5573a8779fbSJames Wright         for (int k=0; k<3; k++)
558752f40e3SJed Brown           Grad_v[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
5593a8779fbSJames Wright                                     stab[j][1] * dXdx[k][1] +
5603a8779fbSJames Wright                                     stab[j][2] * dXdx[k][2]);
5613a8779fbSJames Wright       break;
5623a8779fbSJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
5633a8779fbSJames Wright       break;
5643a8779fbSJames Wright     }
5653a8779fbSJames Wright 
5663a8779fbSJames Wright   } // End Quadrature Point Loop
5673a8779fbSJames Wright 
5683a8779fbSJames Wright   // Return
5693a8779fbSJames Wright   return 0;
5703a8779fbSJames Wright }
5713a8779fbSJames Wright 
5723a8779fbSJames Wright // *****************************************************************************
5733a8779fbSJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
5743a8779fbSJames Wright //   implicit time stepping method
5753a8779fbSJames Wright //
5763a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
5773a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
5783a8779fbSJames Wright //                                       (diffussive terms will be added later)
5793a8779fbSJames Wright //
5803a8779fbSJames Wright // *****************************************************************************
5813a8779fbSJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
5823a8779fbSJames Wright                                     const CeedScalar *const *in,
5833a8779fbSJames Wright                                     CeedScalar *const *out) {
5843a8779fbSJames Wright   // *INDENT-OFF*
5853a8779fbSJames Wright   // Inputs
5863a8779fbSJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
587752f40e3SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
5883a8779fbSJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
5893a8779fbSJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
5903a8779fbSJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
5913a8779fbSJames Wright   // Outputs
5923a8779fbSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
593752f40e3SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1],
594752f40e3SJed Brown              (*jac_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[2];
5953a8779fbSJames Wright   // *INDENT-ON*
5963a8779fbSJames Wright   // Context
5973a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
5983a8779fbSJames Wright   const CeedScalar mu     = context->mu;
5993a8779fbSJames Wright   const CeedScalar cv     = context->cv;
6003a8779fbSJames Wright   const CeedScalar cp     = context->cp;
601bb8a0c61SJames Wright   const CeedScalar *g     = context->g;
602bb8a0c61SJames Wright   const CeedScalar dt     = context->dt;
6033a8779fbSJames Wright   const CeedScalar gamma  = cp / cv;
604bb8a0c61SJames Wright   const CeedScalar Rd     = cp-cv;
6053a8779fbSJames Wright 
6063a8779fbSJames Wright   CeedPragmaSIMD
6073a8779fbSJames Wright   // Quadrature Point Loop
6083a8779fbSJames Wright   for (CeedInt i=0; i<Q; i++) {
609c1a52365SJed Brown     CeedScalar U[5];
610c1a52365SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
611c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
612c1a52365SJed Brown     State s = StateFromU(context, U, x_i);
613c1a52365SJed Brown 
6143a8779fbSJames Wright     // -- Interp-to-Interp q_data
6153a8779fbSJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
6163a8779fbSJames Wright     // -- Interp-to-Grad q_data
6173a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
6183a8779fbSJames Wright     // *INDENT-OFF*
6193a8779fbSJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
6203a8779fbSJames Wright                                     q_data[2][i],
6213a8779fbSJames Wright                                     q_data[3][i]},
6223a8779fbSJames Wright                                    {q_data[4][i],
6233a8779fbSJames Wright                                     q_data[5][i],
6243a8779fbSJames Wright                                     q_data[6][i]},
6253a8779fbSJames Wright                                    {q_data[7][i],
6263a8779fbSJames Wright                                     q_data[8][i],
6273a8779fbSJames Wright                                     q_data[9][i]}
6283a8779fbSJames Wright                                   };
6293a8779fbSJames Wright     // *INDENT-ON*
630c1a52365SJed Brown     State grad_s[3];
6313a8779fbSJames Wright     for (int j=0; j<3; j++) {
6322f7ce6c1SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
633752f40e3SJed Brown       for (int k=0; k<5; k++) dU[k] = Grad_q[0][k][i] * dXdx[0][j]
634752f40e3SJed Brown                                         + Grad_q[1][k][i] * dXdx[1][j]
635752f40e3SJed Brown                                         + Grad_q[2][k][i] * dXdx[2][j];
636c1a52365SJed Brown       dx_i[j] = 1.;
6372f7ce6c1SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
6383a8779fbSJames Wright     }
639c1a52365SJed Brown 
640c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
641c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
642c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
643c1a52365SJed Brown     KMUnpack(kmstress, stress);
644c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
645c1a52365SJed Brown 
646c1a52365SJed Brown     StateConservative F_inviscid[3];
647c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
648c1a52365SJed Brown 
649c1a52365SJed Brown 
650c1a52365SJed Brown     // Total flux
651c1a52365SJed Brown     CeedScalar Flux[5][3];
652c1a52365SJed Brown     for (int j=0; j<3; j++) {
653c1a52365SJed Brown       Flux[0][j] = F_inviscid[j].density;
6543a8779fbSJames Wright       for (int k=0; k<3; k++)
655c1a52365SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
656c1a52365SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
657c1a52365SJed Brown     }
658c1a52365SJed Brown 
659c1a52365SJed Brown     for (int j=0; j<3; j++) {
660c1a52365SJed Brown       for (int k=0; k<5; k++) {
661752f40e3SJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
662c1a52365SJed Brown                                     dXdx[j][1] * Flux[k][1] +
663c1a52365SJed Brown                                     dXdx[j][2] * Flux[k][2]);
664c1a52365SJed Brown       }
665c1a52365SJed Brown     }
666c1a52365SJed Brown 
667c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
668c1a52365SJed Brown     for (int j=0; j<5; j++)
669c1a52365SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
6703a8779fbSJames Wright 
6713a8779fbSJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
672c1a52365SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
673c1a52365SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
674c1a52365SJed Brown                            gamma, g, x_i);
675c1a52365SJed Brown     CeedScalar grad_U[5][3];
6763a8779fbSJames Wright     for (int j=0; j<3; j++) {
677c1a52365SJed Brown       grad_U[0][j] = grad_s[j].U.density;
678c1a52365SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
679c1a52365SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
6803a8779fbSJames Wright     }
681c1a52365SJed Brown 
6823a8779fbSJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
6833a8779fbSJames Wright     CeedScalar strong_conv[5] = {0};
6843a8779fbSJames Wright     for (int j=0; j<3; j++)
6853a8779fbSJames Wright       for (int k=0; k<5; k++)
6863a8779fbSJames Wright         for (int l=0; l<5; l++)
687c1a52365SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
6883a8779fbSJames Wright 
6893a8779fbSJames Wright     // Strong residual
6903a8779fbSJames Wright     CeedScalar strong_res[5];
6913a8779fbSJames Wright     for (int j=0; j<5; j++)
6923a8779fbSJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
6933a8779fbSJames Wright 
6943a8779fbSJames Wright     // -- Stabilization method: none, SU, or SUPG
695bb8a0c61SJames Wright     CeedScalar stab[5][3] = {{0.}};
696bb8a0c61SJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
697bb8a0c61SJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
698bb8a0c61SJames Wright     CeedScalar Tau_d[3] = {0.};
6993a8779fbSJames Wright     switch (context->stabilization) {
7003a8779fbSJames Wright     case STAB_NONE:        // Galerkin
7013a8779fbSJames Wright       break;
7023a8779fbSJames Wright     case STAB_SU:        // SU
703c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
704bb8a0c61SJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
705bb8a0c61SJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
706bb8a0c61SJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
707bb8a0c61SJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
708bb8a0c61SJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
709c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
710c1a52365SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
7113a8779fbSJames Wright       for (int j=0; j<3; j++)
7123a8779fbSJames Wright         for (int k=0; k<5; k++)
7133a8779fbSJames Wright           for (int l=0; l<5; l++)
714bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
7153a8779fbSJames Wright 
7163a8779fbSJames Wright       for (int j=0; j<5; j++)
7173a8779fbSJames Wright         for (int k=0; k<3; k++)
718752f40e3SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
7193a8779fbSJames Wright                                     stab[j][1] * dXdx[k][1] +
7203a8779fbSJames Wright                                     stab[j][2] * dXdx[k][2]);
7213a8779fbSJames Wright       break;
7223a8779fbSJames Wright     case STAB_SUPG:        // SUPG
723c1a52365SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
724bb8a0c61SJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
725bb8a0c61SJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
726bb8a0c61SJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
727bb8a0c61SJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
728bb8a0c61SJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
729bb8a0c61SJames Wright // Alternate route (useful later with primitive variable code)
730bb8a0c61SJames Wright // this function was verified against PHASTA for as IC that was as close as possible
731bb8a0c61SJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
732bb8a0c61SJames Wright // it has also been verified to compute a correct through the following
733bb8a0c61SJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
734bb8a0c61SJames Wright // applied in the triple loop below
735bb8a0c61SJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
736c1a52365SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
737c1a52365SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
7383a8779fbSJames Wright       for (int j=0; j<3; j++)
7393a8779fbSJames Wright         for (int k=0; k<5; k++)
7403a8779fbSJames Wright           for (int l=0; l<5; l++)
741bb8a0c61SJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
7423a8779fbSJames Wright 
7433a8779fbSJames Wright       for (int j=0; j<5; j++)
7443a8779fbSJames Wright         for (int k=0; k<3; k++)
745752f40e3SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
7463a8779fbSJames Wright                                     stab[j][1] * dXdx[k][1] +
7473a8779fbSJames Wright                                     stab[j][2] * dXdx[k][2]);
7483a8779fbSJames Wright       break;
7493a8779fbSJames Wright     }
750752f40e3SJed Brown     for (int j=0; j<5; j++) jac_data[j][i] = U[j];
751*f0b65372SJed Brown     for (int j=0; j<6; j++) jac_data[5+j][i] = kmstress[j];
752*f0b65372SJed Brown     for (int j=0; j<3; j++) jac_data[5+6+j][i] = Tau_d[j];
7533a8779fbSJames Wright 
7543a8779fbSJames Wright   } // End Quadrature Point Loop
7553a8779fbSJames Wright 
7563a8779fbSJames Wright   // Return
7573a8779fbSJames Wright   return 0;
7583a8779fbSJames Wright }
759*f0b65372SJed Brown 
760*f0b65372SJed Brown CEED_QFUNCTION(IJacobian_Newtonian)(void *ctx, CeedInt Q,
761*f0b65372SJed Brown                                     const CeedScalar *const *in,
762*f0b65372SJed Brown                                     CeedScalar *const *out) {
763*f0b65372SJed Brown   // *INDENT-OFF*
764*f0b65372SJed Brown   // Inputs
765*f0b65372SJed Brown   const CeedScalar (*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
766*f0b65372SJed Brown                    (*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
767*f0b65372SJed Brown                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
768*f0b65372SJed Brown                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
769*f0b65372SJed Brown                    (*jac_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
770*f0b65372SJed Brown   // Outputs
771*f0b65372SJed Brown   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
772*f0b65372SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
773*f0b65372SJed Brown   // *INDENT-ON*
774*f0b65372SJed Brown   // Context
775*f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
776*f0b65372SJed Brown   const CeedScalar *g = context->g;
777*f0b65372SJed Brown   const CeedScalar cp = context->cp;
778*f0b65372SJed Brown   const CeedScalar cv = context->cv;
779*f0b65372SJed Brown   const CeedScalar Rd = cp - cv;
780*f0b65372SJed Brown   const CeedScalar gamma = cp / cv;
781*f0b65372SJed Brown 
782*f0b65372SJed Brown   CeedPragmaSIMD
783*f0b65372SJed Brown   // Quadrature Point Loop
784*f0b65372SJed Brown   for (CeedInt i=0; i<Q; i++) {
785*f0b65372SJed Brown     // -- Interp-to-Interp q_data
786*f0b65372SJed Brown     const CeedScalar wdetJ      =   q_data[0][i];
787*f0b65372SJed Brown     // -- Interp-to-Grad q_data
788*f0b65372SJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
789*f0b65372SJed Brown     // *INDENT-OFF*
790*f0b65372SJed Brown     const CeedScalar dXdx[3][3] = {{q_data[1][i],
791*f0b65372SJed Brown                                     q_data[2][i],
792*f0b65372SJed Brown                                     q_data[3][i]},
793*f0b65372SJed Brown                                    {q_data[4][i],
794*f0b65372SJed Brown                                     q_data[5][i],
795*f0b65372SJed Brown                                     q_data[6][i]},
796*f0b65372SJed Brown                                    {q_data[7][i],
797*f0b65372SJed Brown                                     q_data[8][i],
798*f0b65372SJed Brown                                     q_data[9][i]}
799*f0b65372SJed Brown                                   };
800*f0b65372SJed Brown     // *INDENT-ON*
801*f0b65372SJed Brown 
802*f0b65372SJed Brown     CeedScalar U[5], kmstress[6], Tau_d[3] __attribute((unused));
803*f0b65372SJed Brown     for (int j=0; j<5; j++) U[j] = jac_data[j][i];
804*f0b65372SJed Brown     for (int j=0; j<6; j++) kmstress[j] = jac_data[5+j][i];
805*f0b65372SJed Brown     for (int j=0; j<3; j++) Tau_d[j] = jac_data[5+6+j][i];
806*f0b65372SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
807*f0b65372SJed Brown     State s = StateFromU(context, U, x_i);
808*f0b65372SJed Brown 
809*f0b65372SJed Brown     CeedScalar dU[5], dx0[3] = {0};
810*f0b65372SJed Brown     for (int j=0; j<5; j++) dU[j] = dq[j][i];
811*f0b65372SJed Brown     State ds = StateFromU_fwd(context, s, dU, x_i, dx0);
812*f0b65372SJed Brown 
813*f0b65372SJed Brown     State grad_ds[3];
814*f0b65372SJed Brown     for (int j=0; j<3; j++) {
815*f0b65372SJed Brown       CeedScalar dUj[5];
816*f0b65372SJed Brown       for (int k=0; k<5; k++) dUj[k] = Grad_dq[0][k][i] * dXdx[0][j]
817*f0b65372SJed Brown                                          + Grad_dq[1][k][i] * dXdx[1][j]
818*f0b65372SJed Brown                                          + Grad_dq[2][k][i] * dXdx[2][j];
819*f0b65372SJed Brown       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx0);
820*f0b65372SJed Brown     }
821*f0b65372SJed Brown 
822*f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
823*f0b65372SJed Brown     KMStrainRate(grad_ds, dstrain_rate);
824*f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
825*f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
826*f0b65372SJed Brown     KMUnpack(kmstress, stress);
827*f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
828*f0b65372SJed Brown 
829*f0b65372SJed Brown     StateConservative dF_inviscid[3];
830*f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
831*f0b65372SJed Brown 
832*f0b65372SJed Brown     // Total flux
833*f0b65372SJed Brown     CeedScalar dFlux[5][3];
834*f0b65372SJed Brown     for (int j=0; j<3; j++) {
835*f0b65372SJed Brown       dFlux[0][j] = dF_inviscid[j].density;
836*f0b65372SJed Brown       for (int k=0; k<3; k++)
837*f0b65372SJed Brown         dFlux[k+1][j] = dF_inviscid[j].momentum[k] - dstress[k][j];
838*f0b65372SJed Brown       dFlux[4][j] = dF_inviscid[j].E_total + dFe[j];
839*f0b65372SJed Brown     }
840*f0b65372SJed Brown 
841*f0b65372SJed Brown     for (int j=0; j<3; j++) {
842*f0b65372SJed Brown       for (int k=0; k<5; k++) {
843*f0b65372SJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * dFlux[k][0] +
844*f0b65372SJed Brown                                     dXdx[j][1] * dFlux[k][1] +
845*f0b65372SJed Brown                                     dXdx[j][2] * dFlux[k][2]);
846*f0b65372SJed Brown       }
847*f0b65372SJed Brown     }
848*f0b65372SJed Brown 
849*f0b65372SJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density *g[0], ds.U.density *g[1], ds.U.density *g[2], 0};
850*f0b65372SJed Brown     for (int j=0; j<5; j++)
851*f0b65372SJed Brown       v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
852*f0b65372SJed Brown 
853*f0b65372SJed Brown     if (1) {
854*f0b65372SJed Brown       CeedScalar jacob_F_conv[3][5][5] = {0};
855*f0b65372SJed Brown       computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
856*f0b65372SJed Brown                              gamma, g, x_i);
857*f0b65372SJed Brown       CeedScalar grad_dU[5][3];
858*f0b65372SJed Brown       for (int j=0; j<3; j++) {
859*f0b65372SJed Brown         grad_dU[0][j] = grad_ds[j].U.density;
860*f0b65372SJed Brown         for (int k=0; k<3; k++) grad_dU[k+1][j] = grad_ds[j].U.momentum[k];
861*f0b65372SJed Brown         grad_dU[4][j] = grad_ds[j].U.E_total;
862*f0b65372SJed Brown       }
863*f0b65372SJed Brown       CeedScalar dstrong_conv[5] = {0};
864*f0b65372SJed Brown       for (int j=0; j<3; j++)
865*f0b65372SJed Brown         for (int k=0; k<5; k++)
866*f0b65372SJed Brown           for (int l=0; l<5; l++)
867*f0b65372SJed Brown             dstrong_conv[k] += jacob_F_conv[j][k][l] * grad_dU[l][j];
868*f0b65372SJed Brown       CeedScalar dstrong_res[5];
869*f0b65372SJed Brown       for (int j=0; j<5; j++)
870*f0b65372SJed Brown         dstrong_res[j] = context->ijacobian_time_shift * dU[j] + dstrong_conv[j] -
871*f0b65372SJed Brown                          dbody_force[j];
872*f0b65372SJed Brown       CeedScalar dtau_strong_res[5] = {0.}, dtau_strong_res_conservative[5] = {0};
873*f0b65372SJed Brown       dtau_strong_res[0] = Tau_d[0] * dstrong_res[0];
874*f0b65372SJed Brown       dtau_strong_res[1] = Tau_d[1] * dstrong_res[1];
875*f0b65372SJed Brown       dtau_strong_res[2] = Tau_d[1] * dstrong_res[2];
876*f0b65372SJed Brown       dtau_strong_res[3] = Tau_d[1] * dstrong_res[3];
877*f0b65372SJed Brown       dtau_strong_res[4] = Tau_d[2] * dstrong_res[4];
878*f0b65372SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
879*f0b65372SJed Brown                                   dtau_strong_res, dtau_strong_res_conservative);
880*f0b65372SJed Brown       CeedScalar dstab[5][3] = {0};
881*f0b65372SJed Brown       for (int j=0; j<3; j++)
882*f0b65372SJed Brown         for (int k=0; k<5; k++)
883*f0b65372SJed Brown           for (int l=0; l<5; l++)
884*f0b65372SJed Brown             dstab[k][j] += jacob_F_conv[j][k][l] * dtau_strong_res_conservative[l];
885*f0b65372SJed Brown       for (int j=0; j<5; j++)
886*f0b65372SJed Brown         for (int k=0; k<3; k++)
887*f0b65372SJed Brown           Grad_v[k][j][i] += wdetJ*(dstab[j][0] * dXdx[k][0] +
888*f0b65372SJed Brown                                     dstab[j][1] * dXdx[k][1] +
889*f0b65372SJed Brown                                     dstab[j][2] * dXdx[k][2]);
890*f0b65372SJed Brown 
891*f0b65372SJed Brown     }
892*f0b65372SJed Brown   } // End Quadrature Point Loop
893*f0b65372SJed Brown   return 0;
894*f0b65372SJed Brown }
8953a8779fbSJames Wright // *****************************************************************************
8963a8779fbSJames Wright #endif // newtonian_h
897