xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 0fcbc436b6a2f4df9fd6067c55fef5898978bc44)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
388b783a1SJames Wright //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
588b783a1SJames Wright //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
788b783a1SJames Wright 
888b783a1SJames Wright /// @file
988b783a1SJames Wright /// Operator for Navier-Stokes example using PETSc
1088b783a1SJames Wright #include <ceed.h>
11c9c2c079SJeremy L Thompson #include <math.h>
12738af36cSAdelekeBankole #include <stdlib.h>
132b730f8bSJeremy L Thompson 
14c6e8c570SJames Wright #include "newtonian_state.h"
15c9c2c079SJeremy L Thompson #include "newtonian_types.h"
162b89d87eSLeila Ghaffari #include "stabilization.h"
17c9c2c079SJeremy L Thompson #include "utils.h"
1888626eedSJames Wright 
191d2a9659SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma, CeedScalar damp_Y[5],
20530ad8c4SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
21530ad8c4SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
223bd61617SKenneth E. Jansen   State damp_s = StateFromY_fwd(context, s, damp_Y);
23530ad8c4SKenneth E. Jansen 
24530ad8c4SKenneth E. Jansen   CeedScalar U[5];
25530ad8c4SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
26530ad8c4SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
27530ad8c4SKenneth E. Jansen }
28530ad8c4SKenneth E. Jansen 
2988626eedSJames Wright // *****************************************************************************
3088b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
3188b783a1SJames Wright // *****************************************************************************
32be91e165SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3388b783a1SJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3488b783a1SJames Wright 
3588626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
3688626eedSJames Wright 
372b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3888b783a1SJames Wright     CeedScalar q[5] = {0.};
393bd61617SKenneth E. Jansen     State      s    = StateFromPrimitive(&context->gas, context->reference);
40be91e165SJames Wright     StateToQ(&context->gas, s, q, state_var);
412b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
42f0b01153SJames Wright   }
4388b783a1SJames Wright   return 0;
4488b783a1SJames Wright }
4588b783a1SJames Wright 
462b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
47be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
48d310b3d3SAdeleke O. Bankole }
49d310b3d3SAdeleke O. Bankole CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
50be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
51dc805cc4SLeila Ghaffari }
52dc805cc4SLeila Ghaffari 
53*0fcbc436SJames Wright CEED_QFUNCTION_HELPER void MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
54*0fcbc436SJames Wright                                                   StateVariable state_var) {
55*0fcbc436SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
56*0fcbc436SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
57*0fcbc436SJames Wright   const CeedScalar(*q_data)            = in[2];
58*0fcbc436SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
59*0fcbc436SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
60*0fcbc436SJames Wright 
61*0fcbc436SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
62*0fcbc436SJames Wright 
63*0fcbc436SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
64*0fcbc436SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
65*0fcbc436SJames Wright     const CeedScalar qi_dot[5] = {q_dot[0][i], q_dot[1][i], q_dot[2][i], q_dot[3][i], q_dot[4][i]};
66*0fcbc436SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
67*0fcbc436SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
68*0fcbc436SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
69*0fcbc436SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
70*0fcbc436SJames Wright 
71*0fcbc436SJames Wright     // Standard mass matrix term
72*0fcbc436SJames Wright     for (CeedInt f = 0; f < 5; f++) {
73*0fcbc436SJames Wright       v[f][i] = wdetJ * qi_dot[f];
74*0fcbc436SJames Wright     }
75*0fcbc436SJames Wright 
76*0fcbc436SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
77*0fcbc436SJames Wright     State      grad_s[3] = {{{0.}}};
78*0fcbc436SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, U_dot[5];
79*0fcbc436SJames Wright     UnpackState_U(s_dot.U, U_dot);
80*0fcbc436SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
81*0fcbc436SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
82*0fcbc436SJames Wright 
83*0fcbc436SJames Wright     // Stabilized mass term
84*0fcbc436SJames Wright     for (CeedInt j = 0; j < 5; j++) {
85*0fcbc436SJames Wright       for (CeedInt k = 0; k < 3; k++) {
86*0fcbc436SJames 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]);
87*0fcbc436SJames Wright       }
88*0fcbc436SJames Wright     }
89*0fcbc436SJames Wright   }
90*0fcbc436SJames Wright }
91*0fcbc436SJames Wright 
92*0fcbc436SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
93*0fcbc436SJames Wright   MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
94*0fcbc436SJames Wright   return 0;
95*0fcbc436SJames Wright }
96*0fcbc436SJames Wright 
97dc805cc4SLeila Ghaffari // *****************************************************************************
98ea61e9acSJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
9988b783a1SJames Wright //
100ea61e9acSJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
10188b783a1SJames Wright //
10288b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
10388b783a1SJames Wright //   rho - Mass Density
10488b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
10588b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
10688b783a1SJames Wright //
10788b783a1SJames Wright // Navier-Stokes Equations:
10888b783a1SJames Wright //   drho/dt + div( U )                               = 0
10988b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
11088b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
11188b783a1SJames Wright //
11288b783a1SJames Wright // Viscous Stress:
11388b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
11488b783a1SJames Wright //
11588b783a1SJames Wright // Thermal Stress:
11688b783a1SJames Wright //   Fe = u Fu + k grad( T )
11788626eedSJames Wright // Equation of State
11888b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
11988b783a1SJames Wright //
12088b783a1SJames Wright // Stabilization:
12188b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
12288b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
12388b783a1SJames Wright //     gij = dXi/dX * dXi/dX
12488b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
12588b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
12688b783a1SJames Wright //     TauE = TauM / (Ce cv)
12788b783a1SJames Wright //
12888b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
12988b783a1SJames Wright //
13088b783a1SJames Wright // Constants:
13188b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
13288b783a1SJames Wright //   mu              ,  Dynamic viscosity
13388b783a1SJames Wright //   k               ,  Thermal conductivity
13488b783a1SJames Wright //   cv              ,  Specific heat, constant volume
13588b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
13688b783a1SJames Wright //   g               ,  Gravity
13788b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
13888b783a1SJames Wright //
139ea61e9acSJeremy 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
140ea61e9acSJeremy L Thompson // gradu )
14188b783a1SJames Wright // *****************************************************************************
1422b730f8bSJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
14346603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1449b6a821dSJames Wright   const CeedScalar(*Grad_q)          = in[1];
145f3e15844SJames Wright   const CeedScalar(*q_data)          = in[2];
14646603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
14746603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
14888b783a1SJames Wright 
14988b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
15088626eedSJames Wright   const CeedScalar        *g       = context->g;
15188626eedSJames Wright   const CeedScalar         dt      = context->dt;
15288b783a1SJames Wright 
15346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
154f3e15844SJames Wright     CeedScalar U[5], wdetJ, dXdx[3][3];
1555c677226SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
156f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
157f3e15844SJames Wright     StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
1583bd61617SKenneth E. Jansen     State s = StateFromU(context, U);
1595c677226SJed Brown 
1605c677226SJed Brown     State grad_s[3];
1613bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
1625c677226SJed Brown 
1635c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
164d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1655c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
1665c677226SJed Brown     KMUnpack(kmstress, stress);
1675c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
1685c677226SJed Brown 
1695c677226SJed Brown     StateConservative F_inviscid[3];
1705c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
1715c677226SJed Brown 
1725c677226SJed Brown     // Total flux
1735c677226SJed Brown     CeedScalar Flux[5][3];
1742b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
1755c677226SJed Brown 
1767b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
1777b69c783SJames 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]);
1782b730f8bSJeremy L Thompson     }
1795c677226SJed Brown 
180858ec087SKenneth E. Jansen     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], Dot3(s.U.momentum, g)};
1812b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
18288b783a1SJames Wright 
1832b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1842b89d87eSLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
1852b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1863bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
18788b783a1SJames Wright 
1882b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1892b730f8bSJeremy 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]);
1902b730f8bSJeremy L Thompson     }
191f0b01153SJames Wright   }
19288b783a1SJames Wright   return 0;
19388b783a1SJames Wright }
19488b783a1SJames Wright 
19588b783a1SJames Wright // *****************************************************************************
196ea61e9acSJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
19788b783a1SJames Wright //
19888b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
19988b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
200ea61e9acSJeremy L Thompson //                                       (diffusive terms will be added later)
20188b783a1SJames Wright // *****************************************************************************
202be91e165SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
20346603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2049b6a821dSJames Wright   const CeedScalar(*Grad_q)            = in[1];
20546603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
206f3e15844SJames Wright   const CeedScalar(*q_data)            = in[3];
20746603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
20846603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
20946603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
210f3e15844SJames Wright   CeedScalar(*jac_data)                = out[2];
21146603fc5SJames Wright 
21288b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
21388626eedSJames Wright   const CeedScalar        *g       = context->g;
21488626eedSJames Wright   const CeedScalar         dt      = context->dt;
215530ad8c4SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
21688b783a1SJames Wright 
21746603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
21846603fc5SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2195c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
2203bd61617SKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
2215c677226SJed Brown 
222f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
223f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
2245c677226SJed Brown     State grad_s[3];
2253bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
2265c677226SJed Brown 
2275c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
228d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
2295c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
2305c677226SJed Brown     KMUnpack(kmstress, stress);
2315c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
2325c677226SJed Brown 
2335c677226SJed Brown     StateConservative F_inviscid[3];
2345c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
2355c677226SJed Brown 
2365c677226SJed Brown     // Total flux
2375c677226SJed Brown     CeedScalar Flux[5][3];
2382b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
2395c677226SJed Brown 
2407b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
2417b69c783SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2427b69c783SJames 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]);
24346603fc5SJames Wright       }
2442b730f8bSJeremy L Thompson     }
2455c677226SJed Brown 
246858ec087SKenneth E. Jansen     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], Dot3(s.U.momentum, g)};
24788b783a1SJames Wright 
2482b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
2493bd61617SKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
2503d02368aSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
2513bd61617SKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
2523d02368aSJames Wright     UnpackState_U(s_dot.U, U_dot);
2533d02368aSJames Wright 
2542b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
255530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
2561d2a9659SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
2571d2a9659SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
258530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
2591d2a9659SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
260530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
261530ad8c4SKenneth E. Jansen     }
262530ad8c4SKenneth E. Jansen 
2632b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2643bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
26588b783a1SJames Wright 
2662b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
26746603fc5SJames Wright       for (CeedInt k = 0; k < 3; k++) {
26846603fc5SJames 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]);
26946603fc5SJames Wright       }
2702b730f8bSJeremy L Thompson     }
271f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
272f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
273f3e15844SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
274f0b01153SJames Wright   }
27588b783a1SJames Wright   return 0;
27688b783a1SJames Wright }
277e334ad8fSJed Brown 
2782b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
279be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2803d02368aSJames Wright }
2813d02368aSJames Wright 
2822b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
283be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2843d02368aSJames Wright }
2853d02368aSJames Wright 
286dc805cc4SLeila Ghaffari // *****************************************************************************
287ea61e9acSJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
288dc805cc4SLeila Ghaffari // *****************************************************************************
289be91e165SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
29046603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2919b6a821dSJames Wright   const CeedScalar(*Grad_dq)         = in[1];
292f3e15844SJames Wright   const CeedScalar(*q_data)          = in[2];
2931d2a9659SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
29446603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
29546603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
29646603fc5SJames Wright 
297e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
298e334ad8fSJed Brown   const CeedScalar        *g       = context->g;
299e334ad8fSJed Brown 
30046603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
301f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
302f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
303e334ad8fSJed Brown 
304c98a0616SJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
305f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
306f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
307f3e15844SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
3083bd61617SKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
309e334ad8fSJed Brown 
3103bd61617SKenneth E. Jansen     CeedScalar dqi[5];
3113d02368aSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3123bd61617SKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
313e334ad8fSJed Brown 
314e334ad8fSJed Brown     State grad_ds[3];
3153bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
316e334ad8fSJed Brown 
317e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
318d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
319e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
320e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
321e334ad8fSJed Brown     KMUnpack(kmstress, stress);
322e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
323e334ad8fSJed Brown 
324e334ad8fSJed Brown     StateConservative dF_inviscid[3];
325e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
326e334ad8fSJed Brown 
327e334ad8fSJed Brown     // Total flux
328e334ad8fSJed Brown     CeedScalar dFlux[5][3];
3292b89d87eSLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
330e334ad8fSJed Brown 
33151b00d91SJames Wright     for (int j = 0; j < 5; j++) {
33251b00d91SJames 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]);
3332b730f8bSJeremy L Thompson     }
334e334ad8fSJed Brown 
335858ec087SKenneth E. Jansen     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], Dot3(ds.U.momentum, g)};
3363d02368aSJames Wright     CeedScalar       dU[5]          = {0.};
3373d02368aSJames Wright     UnpackState_U(ds.U, dU);
3382b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
339e334ad8fSJed Brown 
340530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
3411d2a9659SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
342530ad8c4SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
343530ad8c4SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
3441d2a9659SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
345530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
346530ad8c4SKenneth E. Jansen     }
347530ad8c4SKenneth E. Jansen 
3482b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
3492b89d87eSLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
3502b89d87eSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3513bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, dstab);
3522b89d87eSLeila Ghaffari 
3532b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) {
3542b730f8bSJeremy 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]);
3552b730f8bSJeremy L Thompson     }
356f0b01153SJames Wright   }
357e334ad8fSJed Brown   return 0;
358e334ad8fSJed Brown }
35965dd5cafSJames Wright 
3602b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
361be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3623d02368aSJames Wright }
3633d02368aSJames Wright 
3642b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
365be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3663d02368aSJames Wright }
3673d02368aSJames Wright 
3682b89d87eSLeila Ghaffari // *****************************************************************************
36965dd5cafSJames Wright // Compute boundary integral (ie. for strongly set inflows)
3702b89d87eSLeila Ghaffari // *****************************************************************************
371be91e165SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
372f21e6b1cSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
37346603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3749b6a821dSJames Wright   const CeedScalar(*Grad_q)              = in[1];
375f3e15844SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
37646603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
377f21e6b1cSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
37865dd5cafSJames Wright 
3792c4e60d7SJames Wright   const bool is_implicit = context->is_implicit;
38065dd5cafSJames Wright 
3812b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
382efe9d856SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3833bd61617SKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
38465dd5cafSJames Wright 
385f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
386f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
387f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
38865dd5cafSJames Wright 
3892c4e60d7SJames Wright     State grad_s[3];
3903bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
39165dd5cafSJames Wright 
3922c4e60d7SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
393d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
3942c4e60d7SJames Wright     NewtonianStress(context, strain_rate, kmstress);
3952c4e60d7SJames Wright     KMUnpack(kmstress, stress);
3962c4e60d7SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
3972c4e60d7SJames Wright 
3982c4e60d7SJames Wright     StateConservative F_inviscid[3];
3992c4e60d7SJames Wright     FluxInviscid(context, s, F_inviscid);
4002c4e60d7SJames Wright 
4015bce47c7SJames Wright     CeedScalar Flux[5];
4025bce47c7SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
4032c4e60d7SJames Wright 
4045bce47c7SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
40565dd5cafSJames Wright 
406f21e6b1cSJames Wright     if (is_implicit) {
407f3e15844SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
408f3e15844SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
40965dd5cafSJames Wright     }
410f21e6b1cSJames Wright   }
41165dd5cafSJames Wright   return 0;
41265dd5cafSJames Wright }
41365dd5cafSJames Wright 
4142b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
415be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
41620840d50SJames Wright }
41720840d50SJames Wright 
4182b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
419be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
42020840d50SJames Wright }
42120840d50SJames Wright 
4222b89d87eSLeila Ghaffari // *****************************************************************************
423b55ac660SJames Wright // Jacobian for "set nothing" boundary integral
4242b89d87eSLeila Ghaffari // *****************************************************************************
4252b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
426be91e165SJames Wright                                                     StateVariable state_var) {
42746603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4289b6a821dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
429f3e15844SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
430c1d93bc4SKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
431b55ac660SJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
432b55ac660SJames Wright 
433b55ac660SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
434f3e15844SJames Wright   const bool                     is_implicit = context->is_implicit;
435b55ac660SJames Wright 
43646603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
437f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
438f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
439f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
440b55ac660SJames Wright 
4413bd61617SKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
442f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
443f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
444efe9d856SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
44557e55a1cSJames Wright 
4463bd61617SKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
4473bd61617SKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
448b55ac660SJames Wright 
449b55ac660SJames Wright     State grad_ds[3];
4503bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
451b55ac660SJames Wright 
452b55ac660SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
453d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
454b55ac660SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
455b55ac660SJames Wright     KMUnpack(dkmstress, dstress);
456b55ac660SJames Wright     KMUnpack(kmstress, stress);
457b55ac660SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
458b55ac660SJames Wright 
459b55ac660SJames Wright     StateConservative dF_inviscid[3];
460b55ac660SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
461b55ac660SJames Wright 
4625bce47c7SJames Wright     CeedScalar dFlux[5];
4635bce47c7SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
464b55ac660SJames Wright 
4655bce47c7SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
4664c0e8230SJames Wright   }
467b55ac660SJames Wright   return 0;
468b55ac660SJames Wright }
469b55ac660SJames Wright 
4702b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
471be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
47220840d50SJames Wright }
47320840d50SJames Wright 
4742b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
475be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
47620840d50SJames Wright }
477