xref: /honee/qfunctions/newtonian.h (revision 87bd45e78bb728c6bc53784bced060f00de3988d)
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 #ifndef newtonian_h
123a8779fbSJames Wright #define newtonian_h
133a8779fbSJames Wright 
143a8779fbSJames Wright #include <ceed.h>
15d0cce58aSJeremy L Thompson #include <math.h>
167b530f2aSAdelekeBankole #include <stdlib.h>
172b916ea7SJeremy L Thompson 
18475b2820SJames Wright #include "newtonian_state.h"
19d0cce58aSJeremy L Thompson #include "newtonian_types.h"
20d1b9ef12SLeila Ghaffari #include "stabilization.h"
21d0cce58aSJeremy L Thompson #include "utils.h"
22bb8a0c61SJames Wright 
23e7754af5SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar x_i[3], CeedScalar damp_Y[5],
24e7754af5SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
25e7754af5SKenneth E. Jansen   const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26e7754af5SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
27e7754af5SKenneth E. Jansen   CeedScalar dx_i[3] = {0};
28e7754af5SKenneth E. Jansen   State      damp_s  = StateFromY_fwd(context, s, damp_Y, x_i, dx_i);
29e7754af5SKenneth E. Jansen 
30e7754af5SKenneth E. Jansen   CeedScalar U[5];
31e7754af5SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
32e7754af5SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
33e7754af5SKenneth E. Jansen }
34e7754af5SKenneth E. Jansen 
35bb8a0c61SJames Wright // *****************************************************************************
363a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
373a8779fbSJames Wright // *****************************************************************************
388fff8293SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
393a8779fbSJames Wright   // Inputs
403a8779fbSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
413a8779fbSJames Wright 
423a8779fbSJames Wright   // Outputs
433a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
443a8779fbSJames Wright 
45bb8a0c61SJames Wright   // Context
46bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
47bb8a0c61SJames Wright 
483a8779fbSJames Wright   // Quadrature Point Loop
492b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
50b8fb7609SAdeleke O. Bankole     CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
513a8779fbSJames Wright     CeedScalar q[5] = {0.};
52b8fb7609SAdeleke O. Bankole     State      s    = StateFromPrimitive(&context->gas, context->reference, x);
538fff8293SJames Wright     StateToQ(&context->gas, s, q, state_var);
542b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
553a8779fbSJames Wright   }  // End of Quadrature Point Loop
563a8779fbSJames Wright   return 0;
573a8779fbSJames Wright }
583a8779fbSJames Wright 
592b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
608fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
61b8fb7609SAdeleke O. Bankole }
62b8fb7609SAdeleke O. Bankole CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
638fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
64cbe60e31SLeila Ghaffari }
65cbe60e31SLeila Ghaffari 
66cbe60e31SLeila Ghaffari // *****************************************************************************
6704e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
683a8779fbSJames Wright //
6904e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
703a8779fbSJames Wright //
713a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
723a8779fbSJames Wright //   rho - Mass Density
733a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
743a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
753a8779fbSJames Wright //
763a8779fbSJames Wright // Navier-Stokes Equations:
773a8779fbSJames Wright //   drho/dt + div( U )                               = 0
783a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
793a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
803a8779fbSJames Wright //
813a8779fbSJames Wright // Viscous Stress:
823a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
833a8779fbSJames Wright //
843a8779fbSJames Wright // Thermal Stress:
853a8779fbSJames Wright //   Fe = u Fu + k grad( T )
86bb8a0c61SJames Wright // Equation of State
873a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
883a8779fbSJames Wright //
893a8779fbSJames Wright // Stabilization:
903a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
913a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
923a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
933a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
943a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
953a8779fbSJames Wright //     TauE = TauM / (Ce cv)
963a8779fbSJames Wright //
973a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
983a8779fbSJames Wright //
993a8779fbSJames Wright // Constants:
1003a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1013a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1023a8779fbSJames Wright //   k               ,  Thermal conductivity
1033a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1043a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1053a8779fbSJames Wright //   g               ,  Gravity
1063a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1073a8779fbSJames Wright //
10804e40bb6SJeremy L Thompson // We require the product of the inverse of the Jacobian (dXdx_j,k) and its transpose (dXdx_k,j) to properly compute integrals of the form: int( gradv
10904e40bb6SJeremy L Thompson // gradu )
1103a8779fbSJames Wright // *****************************************************************************
1112b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1123a8779fbSJames Wright   // Inputs
1133d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
114*87bd45e7SJames Wright   const CeedScalar(*Grad_q)        = in[1];
115ade49511SJames Wright   const CeedScalar(*q_data)        = in[2];
1163d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
1173d65b166SJames Wright 
1183a8779fbSJames Wright   // Outputs
1193d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1203d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1213a8779fbSJames Wright 
1223a8779fbSJames Wright   // Context
1233a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
124bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
125bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
1263a8779fbSJames Wright 
1273a8779fbSJames Wright   // Quadrature Point Loop
1283d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
129ade49511SJames Wright     CeedScalar U[5], wdetJ, dXdx[3][3];
130c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
131ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
132ade49511SJames Wright     StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
133c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
134c1a52365SJed Brown     State            s      = StateFromU(context, U, x_i);
135c1a52365SJed Brown 
136c1a52365SJed Brown     State grad_s[3];
137*87bd45e7SJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, STATEVAR_CONSERVATIVE, Grad_q, dXdx, false, grad_s);
138c1a52365SJed Brown 
139c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
14040a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
141c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
142c1a52365SJed Brown     KMUnpack(kmstress, stress);
143c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
144c1a52365SJed Brown 
145c1a52365SJed Brown     StateConservative F_inviscid[3];
146c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
147c1a52365SJed Brown 
148c1a52365SJed Brown     // Total flux
149c1a52365SJed Brown     CeedScalar Flux[5][3];
150d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
151c1a52365SJed Brown 
1527523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
1537523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) Grad_v[k][j][i] = wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
1542b916ea7SJeremy L Thompson     }
155c1a52365SJed Brown 
156c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
1572b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1583a8779fbSJames Wright 
159d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
160d1b9ef12SLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
161d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
162d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
1633a8779fbSJames Wright 
1642b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1652b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) Grad_v[k][j][i] -= wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
1662b916ea7SJeremy L Thompson     }
1673a8779fbSJames Wright   }  // End Quadrature Point Loop
1683a8779fbSJames Wright 
1693a8779fbSJames Wright   // Return
1703a8779fbSJames Wright   return 0;
1713a8779fbSJames Wright }
1723a8779fbSJames Wright 
1733a8779fbSJames Wright // *****************************************************************************
17404e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
1753a8779fbSJames Wright //
1763a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1773a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
17804e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
1793a8779fbSJames Wright // *****************************************************************************
1808fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
1813a8779fbSJames Wright   // Inputs
1823d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
183*87bd45e7SJames Wright   const CeedScalar(*Grad_q)            = in[1];
1843d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
185ade49511SJames Wright   const CeedScalar(*q_data)            = in[3];
1863d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
1873d65b166SJames Wright 
1883a8779fbSJames Wright   // Outputs
1893d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1903d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
191ade49511SJames Wright   CeedScalar(*jac_data)              = out[2];
1923d65b166SJames Wright 
1933a8779fbSJames Wright   // Context
1943a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
195bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
196bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
197e7754af5SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
1983a8779fbSJames Wright 
1993a8779fbSJames Wright   // Quadrature Point Loop
2003d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2013d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
202c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
2038fff8293SJames Wright     const State      s      = StateFromQ(context, qi, x_i, state_var);
204c1a52365SJed Brown 
205ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
206ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
207c1a52365SJed Brown     State grad_s[3];
208*87bd45e7SJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
209c1a52365SJed Brown 
210c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
21140a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
212c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
213c1a52365SJed Brown     KMUnpack(kmstress, stress);
214c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
215c1a52365SJed Brown 
216c1a52365SJed Brown     StateConservative F_inviscid[3];
217c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
218c1a52365SJed Brown 
219c1a52365SJed Brown     // Total flux
220c1a52365SJed Brown     CeedScalar Flux[5][3];
221d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
222c1a52365SJed Brown 
2237523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2247523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
2257523f6aaSJames Wright         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
2263d65b166SJames Wright       }
2272b916ea7SJeremy L Thompson     }
228c1a52365SJed Brown 
229c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
2303a8779fbSJames Wright 
231d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
23276555becSJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
23376555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
2348fff8293SJames Wright     State s_dot = StateFromQ_fwd(context, s, qi_dot, x_i, dx0, state_var);
23576555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
23676555becSJames Wright 
2372b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
238e7754af5SKenneth E. Jansen     if (context->idl_enable) {
239e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
240e7754af5SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
241e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
242e7754af5SKenneth E. Jansen     }
243e7754af5SKenneth E. Jansen 
244d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
245d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
2463a8779fbSJames Wright 
2472b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2483d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2493d65b166SJames Wright         Grad_v[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
2503d65b166SJames Wright       }
2512b916ea7SJeremy L Thompson     }
252ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
253ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
254ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
2553a8779fbSJames Wright 
2563a8779fbSJames Wright   }  // End Quadrature Point Loop
2573a8779fbSJames Wright 
2583a8779fbSJames Wright   // Return
2593a8779fbSJames Wright   return 0;
2603a8779fbSJames Wright }
261f0b65372SJed Brown 
2622b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2638fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
26476555becSJames Wright }
26576555becSJames Wright 
2662b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2678fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
26876555becSJames Wright }
26976555becSJames Wright 
270cbe60e31SLeila Ghaffari // *****************************************************************************
27104e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
272cbe60e31SLeila Ghaffari // *****************************************************************************
2738fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
274f0b65372SJed Brown   // Inputs
2753d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
276*87bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
277ade49511SJames Wright   const CeedScalar(*q_data)         = in[2];
2783d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
279ade49511SJames Wright   const CeedScalar(*jac_data)       = in[4];
2803d65b166SJames Wright 
281f0b65372SJed Brown   // Outputs
2823d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2833d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2843d65b166SJames Wright 
285f0b65372SJed Brown   // Context
286f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
287f0b65372SJed Brown   const CeedScalar        *g       = context->g;
288f0b65372SJed Brown 
289f0b65372SJed Brown   // Quadrature Point Loop
2903d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
291ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
292ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
293f0b65372SJed Brown 
2948789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
295ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
296ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
297ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
298f0b65372SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
2998fff8293SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
300f0b65372SJed Brown 
30176555becSJames Wright     CeedScalar dqi[5], dx0[3] = {0};
30276555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3038fff8293SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx0, state_var);
304f0b65372SJed Brown 
305f0b65372SJed Brown     State grad_ds[3];
306*87bd45e7SJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, true, grad_ds);
307f0b65372SJed Brown 
308f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
30940a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
310f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
311f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
312f0b65372SJed Brown     KMUnpack(kmstress, stress);
313f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
314f0b65372SJed Brown 
315f0b65372SJed Brown     StateConservative dF_inviscid[3];
316f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
317f0b65372SJed Brown 
318f0b65372SJed Brown     // Total flux
319f0b65372SJed Brown     CeedScalar dFlux[5][3];
320d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
321f0b65372SJed Brown 
32222387d3aSJames Wright     for (int j = 0; j < 5; j++) {
32322387d3aSJames Wright       for (int k = 0; k < 3; k++) Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * dFlux[j][0] + dXdx[k][1] * dFlux[j][1] + dXdx[k][2] * dFlux[j][2]);
3242b916ea7SJeremy L Thompson     }
325f0b65372SJed Brown 
326f0b65372SJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
32776555becSJames Wright     CeedScalar       dU[5]          = {0.};
32876555becSJames Wright     UnpackState_U(ds.U, dU);
3292b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
330f0b65372SJed Brown 
331e7754af5SKenneth E. Jansen     if (context->idl_enable) {
332e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
333e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
334e7754af5SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
335e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
336e7754af5SKenneth E. Jansen     }
337e7754af5SKenneth E. Jansen 
338d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
339d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
340d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
341d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
342d1b9ef12SLeila Ghaffari 
3432b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3442b916ea7SJeremy L Thompson       for (int k = 0; k < 3; k++) Grad_v[k][j][i] += wdetJ * (dstab[j][0] * dXdx[k][0] + dstab[j][1] * dXdx[k][1] + dstab[j][2] * dXdx[k][2]);
3452b916ea7SJeremy L Thompson     }
346f0b65372SJed Brown   }  // End Quadrature Point Loop
347f0b65372SJed Brown   return 0;
348f0b65372SJed Brown }
3498085925cSJames Wright 
3502b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3518fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
35276555becSJames Wright }
35376555becSJames Wright 
3542b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3558fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
35676555becSJames Wright }
35776555becSJames Wright 
358d1b9ef12SLeila Ghaffari // *****************************************************************************
3598085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
360d1b9ef12SLeila Ghaffari // *****************************************************************************
3618fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3623d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
363*87bd45e7SJames Wright   const CeedScalar(*Grad_q)        = in[1];
364ade49511SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
3653d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
3668085925cSJames Wright 
3673d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
368ade49511SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
3698085925cSJames Wright 
370d3b25f3aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
371d3b25f3aSJames Wright   const bool                     is_implicit = context->is_implicit;
3728085925cSJames Wright 
3732b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
374d3b25f3aSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
37541e73928SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3768fff8293SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
3778085925cSJames Wright 
378ade49511SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
379ade49511SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
380ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3818085925cSJames Wright 
382d3b25f3aSJames Wright     State grad_s[3];
383*87bd45e7SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
3848085925cSJames Wright 
385d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
38640a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
387d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
388d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
389d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
390d3b25f3aSJames Wright 
391d3b25f3aSJames Wright     StateConservative F_inviscid[3];
392d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
393d3b25f3aSJames Wright 
394c5740391SJames Wright     CeedScalar Flux[5];
395c5740391SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
396d3b25f3aSJames Wright 
397c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
3988085925cSJames Wright 
399ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
400ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4018085925cSJames Wright   }
4028085925cSJames Wright   return 0;
4038085925cSJames Wright }
4048085925cSJames Wright 
4052b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4068fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
407d4559bbeSJames Wright }
408d4559bbeSJames Wright 
4092b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4108fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
411d4559bbeSJames Wright }
412d4559bbeSJames Wright 
413d1b9ef12SLeila Ghaffari // *****************************************************************************
41468ae065aSJames Wright // Jacobian for "set nothing" boundary integral
415d1b9ef12SLeila Ghaffari // *****************************************************************************
4162b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4178fff8293SJames Wright                                                     StateVariable state_var) {
41868ae065aSJames Wright   // Inputs
4193d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
420*87bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
421ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
4223d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
423ade49511SJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
4243d65b166SJames Wright 
42568ae065aSJames Wright   // Outputs
42668ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
42768ae065aSJames Wright 
42868ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
429ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
43068ae065aSJames Wright 
43168ae065aSJames Wright   // Quadrature Point Loop
4323d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
43368ae065aSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
434ade49511SJames Wright     CeedScalar       wdetJb, dXdx[2][3], norm[3];
435ade49511SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
436ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
43768ae065aSJames Wright 
43841e73928SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
439ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
440ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
44141e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4423934e2b1SJames Wright 
4438fff8293SJames Wright     State s  = StateFromQ(context, qi, x_i, state_var);
4448fff8293SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
44568ae065aSJames Wright 
44668ae065aSJames Wright     State grad_ds[3];
447*87bd45e7SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, false, grad_ds);
44868ae065aSJames Wright 
44968ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
45040a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
45168ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
45268ae065aSJames Wright     KMUnpack(dkmstress, dstress);
45368ae065aSJames Wright     KMUnpack(kmstress, stress);
45468ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
45568ae065aSJames Wright 
45668ae065aSJames Wright     StateConservative dF_inviscid[3];
45768ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
45868ae065aSJames Wright 
459c5740391SJames Wright     CeedScalar dFlux[5];
460c5740391SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
46168ae065aSJames Wright 
462c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
46368ae065aSJames Wright   }  // End Quadrature Point Loop
46468ae065aSJames Wright   return 0;
46568ae065aSJames Wright }
46668ae065aSJames Wright 
4672b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4688fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
469d4559bbeSJames Wright }
470d4559bbeSJames Wright 
4712b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4728fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
473d4559bbeSJames Wright }
474d4559bbeSJames Wright 
4753a8779fbSJames Wright #endif  // newtonian_h
476