xref: /honee/qfunctions/newtonian.h (revision 7523f6aaeed819b409cd8065700bd29f6cf77260)
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];
1143d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
1153d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])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++) {
129c1a52365SJed Brown     CeedScalar U[5];
130c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
131c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
132c1a52365SJed Brown     State            s      = StateFromU(context, U, x_i);
133c1a52365SJed Brown 
1343a8779fbSJames Wright     // -- Interp-to-Interp q_data
1353a8779fbSJames Wright     const CeedScalar wdetJ = q_data[0][i];
1363a8779fbSJames Wright     // -- Interp-to-Grad q_data
1373a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
1382b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
1392b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
14034ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
14134ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
1423a8779fbSJames Wright     };
143c1a52365SJed Brown     State grad_s[3];
144*7523f6aaSJames Wright     for (CeedInt k = 0; k < 3; k++) {
1452f7ce6c1SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
146*7523f6aaSJames Wright       for (CeedInt j = 0; j < 5; j++) dU[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k] + Grad_q[2][j][i] * dXdx[2][k];
147*7523f6aaSJames Wright       dx_i[k]   = 1.;
148*7523f6aaSJames Wright       grad_s[k] = StateFromU_fwd(context, s, dU, x_i, dx_i);
149c1a52365SJed Brown     }
150c1a52365SJed Brown 
151c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
15240a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
153c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
154c1a52365SJed Brown     KMUnpack(kmstress, stress);
155c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
156c1a52365SJed Brown 
157c1a52365SJed Brown     StateConservative F_inviscid[3];
158c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
159c1a52365SJed Brown 
160c1a52365SJed Brown     // Total flux
161c1a52365SJed Brown     CeedScalar Flux[5][3];
162d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
163c1a52365SJed Brown 
164*7523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
165*7523f6aaSJames 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]);
1662b916ea7SJeremy L Thompson     }
167c1a52365SJed Brown 
168c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
1692b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1703a8779fbSJames Wright 
171d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
172d1b9ef12SLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
173d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
174d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
1753a8779fbSJames Wright 
1762b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1772b916ea7SJeremy 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]);
1782b916ea7SJeremy L Thompson     }
1793a8779fbSJames Wright   }  // End Quadrature Point Loop
1803a8779fbSJames Wright 
1813a8779fbSJames Wright   // Return
1823a8779fbSJames Wright   return 0;
1833a8779fbSJames Wright }
1843a8779fbSJames Wright 
1853a8779fbSJames Wright // *****************************************************************************
18604e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
1873a8779fbSJames Wright //
1883a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1893a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
19004e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
1913a8779fbSJames Wright // *****************************************************************************
1928fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
1933a8779fbSJames Wright   // Inputs
1943d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1953d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
1963d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
1973d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[3];
1983d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[4];
1993d65b166SJames Wright 
2003a8779fbSJames Wright   // Outputs
2013d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2023d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2033d65b166SJames Wright   CeedScalar(*jac_data)[CEED_Q_VLA]  = (CeedScalar(*)[CEED_Q_VLA])out[2];
2043d65b166SJames Wright 
2053a8779fbSJames Wright   // Context
2063a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
207bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
208bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
209e7754af5SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
2103a8779fbSJames Wright 
2113a8779fbSJames Wright   // Quadrature Point Loop
2123d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2133d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
214c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
2158fff8293SJames Wright     const State      s      = StateFromQ(context, qi, x_i, state_var);
216c1a52365SJed Brown 
2173a8779fbSJames Wright     // -- Interp-to-Interp q_data
2183a8779fbSJames Wright     const CeedScalar wdetJ = q_data[0][i];
2193a8779fbSJames Wright     // -- Interp-to-Grad q_data
2203a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
2212b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2222b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
22334ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
22434ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
2253a8779fbSJames Wright     };
226c1a52365SJed Brown     State grad_s[3];
227*7523f6aaSJames Wright     for (CeedInt k = 0; k < 3; k++) {
22876555becSJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
229*7523f6aaSJames Wright       for (CeedInt j = 0; j < 5; j++) {
230*7523f6aaSJames Wright         dqi[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k] + Grad_q[2][j][i] * dXdx[2][k];
2313d65b166SJames Wright       }
232*7523f6aaSJames Wright       dx_i[k]   = 1.;
233*7523f6aaSJames Wright       grad_s[k] = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
2343a8779fbSJames Wright     }
235c1a52365SJed Brown 
236c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
23740a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
238c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
239c1a52365SJed Brown     KMUnpack(kmstress, stress);
240c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
241c1a52365SJed Brown 
242c1a52365SJed Brown     StateConservative F_inviscid[3];
243c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
244c1a52365SJed Brown 
245c1a52365SJed Brown     // Total flux
246c1a52365SJed Brown     CeedScalar Flux[5][3];
247d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
248c1a52365SJed Brown 
249*7523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
250*7523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
251*7523f6aaSJames 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]);
2523d65b166SJames Wright       }
2532b916ea7SJeremy L Thompson     }
254c1a52365SJed Brown 
255c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
2563a8779fbSJames Wright 
257d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
25876555becSJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
25976555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
2608fff8293SJames Wright     State s_dot = StateFromQ_fwd(context, s, qi_dot, x_i, dx0, state_var);
26176555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
26276555becSJames Wright 
2632b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
264e7754af5SKenneth E. Jansen     if (context->idl_enable) {
265e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
266e7754af5SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
267e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
268e7754af5SKenneth E. Jansen     }
269e7754af5SKenneth E. Jansen 
270d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
271d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
2723a8779fbSJames Wright 
2732b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2743d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2753d65b166SJames 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]);
2763d65b166SJames Wright       }
2772b916ea7SJeremy L Thompson     }
27876555becSJames Wright     for (CeedInt j = 0; j < 5; j++) jac_data[j][i] = qi[j];
279eef2387dSJed Brown     for (CeedInt j = 0; j < 6; j++) jac_data[5 + j][i] = kmstress[j];
280eef2387dSJed Brown     for (CeedInt j = 0; j < 3; j++) jac_data[5 + 6 + j][i] = Tau_d[j];
2813a8779fbSJames Wright 
2823a8779fbSJames Wright   }  // End Quadrature Point Loop
2833a8779fbSJames Wright 
2843a8779fbSJames Wright   // Return
2853a8779fbSJames Wright   return 0;
2863a8779fbSJames Wright }
287f0b65372SJed Brown 
2882b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2898fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
29076555becSJames Wright }
29176555becSJames Wright 
2922b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2938fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
29476555becSJames Wright }
29576555becSJames Wright 
296cbe60e31SLeila Ghaffari // *****************************************************************************
29704e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
298cbe60e31SLeila Ghaffari // *****************************************************************************
2998fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
300f0b65372SJed Brown   // Inputs
3013d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3023d65b166SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
3033d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
3043d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
3053d65b166SJames Wright   const CeedScalar(*jac_data)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[4];
3063d65b166SJames Wright 
307f0b65372SJed Brown   // Outputs
3083d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3093d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3103d65b166SJames Wright 
311f0b65372SJed Brown   // Context
312f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
313f0b65372SJed Brown   const CeedScalar        *g       = context->g;
314f0b65372SJed Brown 
315f0b65372SJed Brown   // Quadrature Point Loop
3163d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
317f0b65372SJed Brown     // -- Interp-to-Interp q_data
318f0b65372SJed Brown     const CeedScalar wdetJ = q_data[0][i];
319f0b65372SJed Brown     // -- Interp-to-Grad q_data
320f0b65372SJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
3212b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
3222b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
32334ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
32434ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
325f0b65372SJed Brown     };
326f0b65372SJed Brown 
3278789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
32876555becSJames Wright     for (int j = 0; j < 5; j++) qi[j] = jac_data[j][i];
329f0b65372SJed Brown     for (int j = 0; j < 6; j++) kmstress[j] = jac_data[5 + j][i];
330f0b65372SJed Brown     for (int j = 0; j < 3; j++) Tau_d[j] = jac_data[5 + 6 + j][i];
331f0b65372SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
3328fff8293SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
333f0b65372SJed Brown 
33476555becSJames Wright     CeedScalar dqi[5], dx0[3] = {0};
33576555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3368fff8293SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx0, state_var);
337f0b65372SJed Brown 
338f0b65372SJed Brown     State grad_ds[3];
339*7523f6aaSJames Wright     for (int k = 0; k < 3; k++) {
34076555becSJames Wright       CeedScalar dqi_j[5];
341*7523f6aaSJames Wright       for (int j = 0; j < 5; j++) dqi_j[j] = Grad_dq[0][j][i] * dXdx[0][k] + Grad_dq[1][j][i] * dXdx[1][k] + Grad_dq[2][j][i] * dXdx[2][k];
342*7523f6aaSJames Wright       grad_ds[k] = StateFromQ_fwd(context, s, dqi_j, x_i, dx0, state_var);
343f0b65372SJed Brown     }
344f0b65372SJed Brown 
345f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
34640a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
347f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
348f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
349f0b65372SJed Brown     KMUnpack(kmstress, stress);
350f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
351f0b65372SJed Brown 
352f0b65372SJed Brown     StateConservative dF_inviscid[3];
353f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
354f0b65372SJed Brown 
355f0b65372SJed Brown     // Total flux
356f0b65372SJed Brown     CeedScalar dFlux[5][3];
357d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
358f0b65372SJed Brown 
35922387d3aSJames Wright     for (int j = 0; j < 5; j++) {
36022387d3aSJames 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]);
3612b916ea7SJeremy L Thompson     }
362f0b65372SJed Brown 
363f0b65372SJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
36476555becSJames Wright     CeedScalar       dU[5]          = {0.};
36576555becSJames Wright     UnpackState_U(ds.U, dU);
3662b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
367f0b65372SJed Brown 
368e7754af5SKenneth E. Jansen     if (context->idl_enable) {
369e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
370e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
371e7754af5SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
372e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
373e7754af5SKenneth E. Jansen     }
374e7754af5SKenneth E. Jansen 
375d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
376d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
377d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
378d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
379d1b9ef12SLeila Ghaffari 
3802b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3812b916ea7SJeremy 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]);
3822b916ea7SJeremy L Thompson     }
383f0b65372SJed Brown   }  // End Quadrature Point Loop
384f0b65372SJed Brown   return 0;
385f0b65372SJed Brown }
3868085925cSJames Wright 
3872b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3888fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
38976555becSJames Wright }
39076555becSJames Wright 
3912b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3928fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
39376555becSJames Wright }
39476555becSJames Wright 
395d1b9ef12SLeila Ghaffari // *****************************************************************************
3968085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
397d1b9ef12SLeila Ghaffari // *****************************************************************************
3988fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3993d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4003d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
4013d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
4023d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
4038085925cSJames Wright 
4043d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
4053d65b166SJames Wright   CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
4068085925cSJames Wright 
407d3b25f3aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
408d3b25f3aSJames Wright   const bool                     is_implicit = context->is_implicit;
4098085925cSJames Wright 
4102b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
411d3b25f3aSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
41241e73928SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
4138fff8293SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
4148085925cSJames Wright 
4158085925cSJames Wright     const CeedScalar wdetJb = (is_implicit ? -1. : 1.) * q_data_sur[0][i];
416c5740391SJames Wright     // ---- Normal vector
4172b916ea7SJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
4188085925cSJames Wright 
419d3b25f3aSJames Wright     const CeedScalar dXdx[2][3] = {
420d3b25f3aSJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
421d3b25f3aSJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
422d3b25f3aSJames Wright     };
4238085925cSJames Wright 
424d3b25f3aSJames Wright     State grad_s[3];
425*7523f6aaSJames Wright     for (CeedInt k = 0; k < 3; k++) {
42641e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
427*7523f6aaSJames Wright       for (CeedInt j = 0; j < 5; j++) dqi[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k];
428*7523f6aaSJames Wright       dx_i[k]   = 1.;
429*7523f6aaSJames Wright       grad_s[k] = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
430d3b25f3aSJames Wright     }
4318085925cSJames Wright 
432d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
43340a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
434d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
435d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
436d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
437d3b25f3aSJames Wright 
438d3b25f3aSJames Wright     StateConservative F_inviscid[3];
439d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
440d3b25f3aSJames Wright 
441c5740391SJames Wright     CeedScalar Flux[5];
442c5740391SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
443d3b25f3aSJames Wright 
444c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4458085925cSJames Wright 
446c5740391SJames Wright     for (int j = 0; j < 5; j++) jac_data_sur[j][i] = qi[j];
44768ae065aSJames Wright     for (int j = 0; j < 6; j++) jac_data_sur[5 + j][i] = kmstress[j];
4488085925cSJames Wright   }
4498085925cSJames Wright   return 0;
4508085925cSJames Wright }
4518085925cSJames Wright 
4522b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4538fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
454d4559bbeSJames Wright }
455d4559bbeSJames Wright 
4562b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4578fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
458d4559bbeSJames Wright }
459d4559bbeSJames Wright 
460d1b9ef12SLeila Ghaffari // *****************************************************************************
46168ae065aSJames Wright // Jacobian for "set nothing" boundary integral
462d1b9ef12SLeila Ghaffari // *****************************************************************************
4632b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4648fff8293SJames Wright                                                     StateVariable state_var) {
46568ae065aSJames Wright   // Inputs
4663d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4673d65b166SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA]   = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
4683d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2];
4693d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[3];
4703d65b166SJames Wright   const CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
4713d65b166SJames Wright 
47268ae065aSJames Wright   // Outputs
47368ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
47468ae065aSJames Wright 
47568ae065aSJames Wright   const NewtonianIdealGasContext context  = (NewtonianIdealGasContext)ctx;
47668ae065aSJames Wright   const bool                     implicit = context->is_implicit;
47768ae065aSJames Wright 
47868ae065aSJames Wright   // Quadrature Point Loop
4793d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
48068ae065aSJames Wright     const CeedScalar x_i[3]     = {x[0][i], x[1][i], x[2][i]};
48168ae065aSJames Wright     const CeedScalar wdetJb     = (implicit ? -1. : 1.) * q_data_sur[0][i];
4822b916ea7SJeremy L Thompson     const CeedScalar norm[3]    = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
48368ae065aSJames Wright     const CeedScalar dXdx[2][3] = {
48468ae065aSJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
48568ae065aSJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
48668ae065aSJames Wright     };
48768ae065aSJames Wright 
48841e73928SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
48941e73928SJames Wright     for (int j = 0; j < 5; j++) qi[j] = jac_data_sur[j][i];
49068ae065aSJames Wright     for (int j = 0; j < 6; j++) kmstress[j] = jac_data_sur[5 + j][i];
49141e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4923934e2b1SJames Wright 
4938fff8293SJames Wright     State s  = StateFromQ(context, qi, x_i, state_var);
4948fff8293SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
49568ae065aSJames Wright 
49668ae065aSJames Wright     State grad_ds[3];
497*7523f6aaSJames Wright     for (CeedInt k = 0; k < 3; k++) {
49841e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi_j[5];
499*7523f6aaSJames Wright       for (CeedInt j = 0; j < 5; j++) dqi_j[j] = Grad_dq[0][j][i] * dXdx[0][k] + Grad_dq[1][j][i] * dXdx[1][k];
500*7523f6aaSJames Wright       dx_i[k]    = 1.;
501*7523f6aaSJames Wright       grad_ds[k] = StateFromQ_fwd(context, s, dqi_j, x_i, dx_i, state_var);
50268ae065aSJames Wright     }
50368ae065aSJames Wright 
50468ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
50540a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
50668ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
50768ae065aSJames Wright     KMUnpack(dkmstress, dstress);
50868ae065aSJames Wright     KMUnpack(kmstress, stress);
50968ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
51068ae065aSJames Wright 
51168ae065aSJames Wright     StateConservative dF_inviscid[3];
51268ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
51368ae065aSJames Wright 
514c5740391SJames Wright     CeedScalar dFlux[5];
515c5740391SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
51668ae065aSJames Wright 
517c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
51868ae065aSJames Wright   }  // End Quadrature Point Loop
51968ae065aSJames Wright   return 0;
52068ae065aSJames Wright }
52168ae065aSJames Wright 
5222b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5238fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
524d4559bbeSJames Wright }
525d4559bbeSJames Wright 
5262b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5278fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
528d4559bbeSJames Wright }
529d4559bbeSJames Wright 
5303a8779fbSJames Wright #endif  // newtonian_h
531