xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 5c677226cd04abd1d571aee943392fe51960a7dc)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 
1188b783a1SJames Wright 
1288b783a1SJames Wright #ifndef newtonian_h
1388b783a1SJames Wright #define newtonian_h
1488b783a1SJames Wright 
1588b783a1SJames Wright #include <math.h>
1688b783a1SJames Wright #include <ceed.h>
17841e4c73SJed Brown #include "newtonian_types.h"
1888b783a1SJames Wright 
1988b783a1SJames Wright #ifndef M_PI
2088b783a1SJames Wright #define M_PI    3.14159265358979323846
2188b783a1SJames Wright #endif
2288b783a1SJames Wright 
23*5c677226SJed Brown typedef struct {
24*5c677226SJed Brown   CeedScalar pressure;
25*5c677226SJed Brown   CeedScalar velocity[3];
26*5c677226SJed Brown   CeedScalar temperature;
27*5c677226SJed Brown } StatePrimitive;
28*5c677226SJed Brown 
29*5c677226SJed Brown typedef struct {
30*5c677226SJed Brown   CeedScalar density;
31*5c677226SJed Brown   CeedScalar momentum[3];
32*5c677226SJed Brown   CeedScalar E_total;
33*5c677226SJed Brown } StateConservative;
34*5c677226SJed Brown 
35*5c677226SJed Brown typedef struct {
36*5c677226SJed Brown   StateConservative U;
37*5c677226SJed Brown   StatePrimitive Y;
38*5c677226SJed Brown } State;
39*5c677226SJed Brown 
40*5c677226SJed Brown CEED_QFUNCTION_HELPER CeedScalar Dot3(const CeedScalar u[3],
41*5c677226SJed Brown                                       const CeedScalar v[3]) {
42*5c677226SJed Brown   return u[0]*v[0] + u[1]*v[1] + u[2]*v[2];
43*5c677226SJed Brown }
44*5c677226SJed Brown 
45*5c677226SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
46*5c677226SJed Brown   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
47*5c677226SJed Brown   StatePrimitive Y;
48*5c677226SJed Brown   for (int i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
49*5c677226SJed Brown   CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity);
50*5c677226SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
51*5c677226SJed Brown   CeedScalar e_total = U.E_total / U.density;
52*5c677226SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
53*5c677226SJed Brown   Y.temperature = e_internal / gas->cv;
54*5c677226SJed Brown   Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal;
55*5c677226SJed Brown   return Y;
56*5c677226SJed Brown }
57*5c677226SJed Brown 
58*5c677226SJed Brown CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
59*5c677226SJed Brown   NewtonianIdealGasContext gas, State s, StateConservative dU,
60*5c677226SJed Brown   const CeedScalar x[3], const CeedScalar dx[3]) {
61*5c677226SJed Brown   StatePrimitive dY;
62*5c677226SJed Brown   for (int i=0; i<3; i++) {
63*5c677226SJed Brown     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
64*5c677226SJed Brown   }
65*5c677226SJed Brown   CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity);
66*5c677226SJed Brown   CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
67*5c677226SJed Brown   CeedScalar e_potential = -Dot3(gas->g, x);
68*5c677226SJed Brown   CeedScalar de_potential = -Dot3(gas->g, dx);
69*5c677226SJed Brown   CeedScalar e_total = s.U.E_total / s.U.density;
70*5c677226SJed Brown   CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density;
71*5c677226SJed Brown   CeedScalar e_internal = e_total - e_kinetic - e_potential;
72*5c677226SJed Brown   CeedScalar de_internal = de_total - de_kinetic - de_potential;
73*5c677226SJed Brown   dY.temperature = de_internal / gas->cv;
74*5c677226SJed Brown   dY.pressure = (gas->cp / gas->cv - 1)
75*5c677226SJed Brown                 * (dU.density * e_internal + s.U.density * de_internal);
76*5c677226SJed Brown   return dY;
77*5c677226SJed Brown }
78*5c677226SJed Brown 
79*5c677226SJed Brown CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
80*5c677226SJed Brown                                        const CeedScalar U[5], const CeedScalar x[3]) {
81*5c677226SJed Brown   State s;
82*5c677226SJed Brown   s.U.density = U[0];
83*5c677226SJed Brown   s.U.momentum[0] = U[1];
84*5c677226SJed Brown   s.U.momentum[1] = U[2];
85*5c677226SJed Brown   s.U.momentum[2] = U[3];
86*5c677226SJed Brown   s.U.E_total = U[4];
87*5c677226SJed Brown   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
88*5c677226SJed Brown   return s;
89*5c677226SJed Brown }
90*5c677226SJed Brown 
91*5c677226SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
92*5c677226SJed Brown                                         StateConservative Flux[3]) {
93*5c677226SJed Brown   for (int i=0; i<3; i++) {
94*5c677226SJed Brown     Flux[i].density = s.U.momentum[i];
95*5c677226SJed Brown     for (int j=0; j<3; j++)
96*5c677226SJed Brown       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
97*5c677226SJed Brown                             + s.Y.pressure * (i == j);
98*5c677226SJed Brown     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
99*5c677226SJed Brown   }
100*5c677226SJed Brown }
101*5c677226SJed Brown 
102*5c677226SJed Brown CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
103*5c677226SJed Brown     State s, State ds, StateConservative dFlux[3]) {
104*5c677226SJed Brown   for (int i=0; i<3; i++) {
105*5c677226SJed Brown     dFlux[i].density = ds.U.momentum[i];
106*5c677226SJed Brown     for (int j=0; j<3; j++)
107*5c677226SJed Brown       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
108*5c677226SJed Brown                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
109*5c677226SJed Brown     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
110*5c677226SJed Brown                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
111*5c677226SJed Brown   }
112*5c677226SJed Brown }
113*5c677226SJed Brown 
114*5c677226SJed Brown // Kelvin-Mandel notation
115*5c677226SJed Brown CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
116*5c677226SJed Brown                                         CeedScalar strain_rate[6]) {
117*5c677226SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
118*5c677226SJed Brown   strain_rate[0] = grad_s[0].Y.velocity[0];
119*5c677226SJed Brown   strain_rate[1] = grad_s[1].Y.velocity[1];
120*5c677226SJed Brown   strain_rate[2] = grad_s[2].Y.velocity[2];
121*5c677226SJed Brown   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
122*5c677226SJed Brown   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
123*5c677226SJed Brown   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
124*5c677226SJed Brown }
125*5c677226SJed Brown 
126*5c677226SJed Brown CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) {
127*5c677226SJed Brown   const CeedScalar weight = 1 / sqrt(2.);
128*5c677226SJed Brown   A[0][0] = v[0];
129*5c677226SJed Brown   A[1][1] = v[1];
130*5c677226SJed Brown   A[2][2] = v[2];
131*5c677226SJed Brown   A[2][1] = A[1][2] = weight * v[3];
132*5c677226SJed Brown   A[2][0] = A[0][2] = weight * v[4];
133*5c677226SJed Brown   A[1][0] = A[0][1] = weight * v[5];
134*5c677226SJed Brown }
135*5c677226SJed Brown 
136*5c677226SJed Brown CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
137*5c677226SJed Brown     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
138*5c677226SJed Brown   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
139*5c677226SJed Brown   for (int i=0; i<6; i++) {
140*5c677226SJed Brown     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
141*5c677226SJed Brown   }
142*5c677226SJed Brown }
143*5c677226SJed Brown 
144*5c677226SJed Brown CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
145*5c677226SJed Brown     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
146*5c677226SJed Brown     CeedScalar Fe[3]) {
147*5c677226SJed Brown   for (int i=0; i<3; i++) {
148*5c677226SJed Brown     Fe[i] = - Y.velocity[0] * stress[0][i]
149*5c677226SJed Brown             - Y.velocity[1] * stress[1][i]
150*5c677226SJed Brown             - Y.velocity[2] * stress[2][i]
151*5c677226SJed Brown             - gas->k * grad_s[i].Y.temperature;
152*5c677226SJed Brown   }
153*5c677226SJed Brown }
154*5c677226SJed Brown 
15588b783a1SJames Wright // *****************************************************************************
15688b783a1SJames Wright // Helper function for computing flux Jacobian
15788b783a1SJames Wright // *****************************************************************************
15888b783a1SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5],
15988b783a1SJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
16088626eedSJames Wright     const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) {
16188b783a1SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
16288626eedSJames Wright   CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
16388b783a1SJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
16488b783a1SJames Wright     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
16588626eedSJames Wright       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) -
16688626eedSJames Wright                       u[i]*u[j];
16788b783a1SJames Wright       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
16888b783a1SJames Wright         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
16988b783a1SJames Wright         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
17088b783a1SJames Wright                           ((i==k) ? u[j] : 0.) -
17188b783a1SJames Wright                           ((i==j) ? u[k] : 0.) * (gamma-1.);
17288b783a1SJames Wright         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
17388b783a1SJames Wright                           (gamma-1.)*u[i]*u[k];
17488b783a1SJames Wright       }
17588b783a1SJames Wright       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
17688b783a1SJames Wright     }
17788b783a1SJames Wright     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
17888b783a1SJames Wright     dF[i][4][4] = u[i] * gamma;
17988b783a1SJames Wright   }
18088b783a1SJames Wright }
18188b783a1SJames Wright 
18288b783a1SJames Wright // *****************************************************************************
18388626eedSJames Wright // Helper function for computing flux Jacobian of Primitive variables
18488626eedSJames Wright // *****************************************************************************
18588626eedSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5],
18688626eedSJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
18788626eedSJames Wright     const CeedScalar Rd, const CeedScalar cv) {
18888626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
18988626eedSJames Wright   // TODO Add in gravity's contribution
19088626eedSJames Wright 
19188626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
19288626eedSJames Wright   CeedScalar drdT = -rho / T;
19388626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
19488626eedSJames Wright   CeedScalar etot =  E / rho ;
19588626eedSJames Wright   CeedScalar e2p  = drdP * etot + 1. ;
19688626eedSJames Wright   CeedScalar e3p  = ( E  + rho * Rd * T );
19788626eedSJames Wright   CeedScalar e4p  = drdT * etot + rho * cv ;
19888626eedSJames Wright 
19988626eedSJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
20088626eedSJames Wright     for (CeedInt j=0; j<3; j++) { // j counts F^{m_j}
20188626eedSJames Wright //        [row][col] of A_i
20288626eedSJames Wright       dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p
20388626eedSJames Wright       for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k
204871db79fSKenneth E. Jansen         dF[i][0][k+1]   =  ((i==k) ? rho  : 0.);   // F^c wrt u_k
20588626eedSJames Wright         dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) +  // F^m_j wrt u_k
20688626eedSJames Wright                            ((i==k) ? u[j] : 0.) ) * rho;
20788626eedSJames Wright         dF[i][4][k+1]   = rho * u[i] * u[k]
20888626eedSJames Wright                           + ((i==k) ? e3p  : 0.) ; // F^e wrt u_k
20988626eedSJames Wright       }
21088626eedSJames Wright       dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T
21188626eedSJames Wright     }
21288626eedSJames Wright     dF[i][4][0] = u[i] * e2p; // F^e wrt p
21388626eedSJames Wright     dF[i][4][4] = u[i] * e4p; // F^e wrt T
21488626eedSJames Wright     dF[i][0][0] = u[i] * drdP; // F^c wrt p
21588626eedSJames Wright     dF[i][0][4] = u[i] * drdT; // F^c wrt T
21688626eedSJames Wright   }
21788626eedSJames Wright }
21888626eedSJames Wright 
21988626eedSJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho,
22088626eedSJames Wright     const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd,
22188626eedSJames Wright     const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) {
22288626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2];
22388626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
22488626eedSJames Wright   CeedScalar drdT = -rho / T;
22588626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
22688626eedSJames Wright   dU[0] = drdP * dY[0] + drdT * dY[4];
22788626eedSJames Wright   CeedScalar de_kinetic = 0;
22888626eedSJames Wright   for (int i=0; i<3; i++) {
22988626eedSJames Wright     dU[1+i] = dU[0] * u[i] + rho * dY[1+i];
23088626eedSJames Wright     de_kinetic += u[i] * dY[1+i];
23188626eedSJames Wright   }
23288626eedSJames Wright   dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e
23388626eedSJames Wright           + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2
23488626eedSJames Wright }
23588626eedSJames Wright 
23688626eedSJames Wright // *****************************************************************************
23788626eedSJames Wright // Helper function for computing Tau elements (stabilization constant)
23888626eedSJames Wright //   Model from:
23988626eedSJames Wright //     PHASTA
24088626eedSJames Wright //
24188626eedSJames Wright //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
24288626eedSJames Wright //
24388626eedSJames Wright // Where NOT UPDATED YET
24488626eedSJames Wright // *****************************************************************************
24588626eedSJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3],
24688626eedSJames Wright                                         const CeedScalar dXdx[3][3], const CeedScalar u[3],
24788626eedSJames Wright                                         const CeedScalar cv, const NewtonianIdealGasContext newt_ctx,
24888626eedSJames Wright                                         const CeedScalar mu, const CeedScalar dt,
24988626eedSJames Wright                                         const CeedScalar rho) {
25088626eedSJames Wright   // Context
25188626eedSJames Wright   const CeedScalar Ctau_t = newt_ctx->Ctau_t;
25288626eedSJames Wright   const CeedScalar Ctau_v = newt_ctx->Ctau_v;
25388626eedSJames Wright   const CeedScalar Ctau_C = newt_ctx->Ctau_C;
25488626eedSJames Wright   const CeedScalar Ctau_M = newt_ctx->Ctau_M;
25588626eedSJames Wright   const CeedScalar Ctau_E = newt_ctx->Ctau_E;
25688626eedSJames Wright   CeedScalar gijd[6];
25788626eedSJames Wright   CeedScalar tau;
25888626eedSJames Wright   CeedScalar dts;
25988626eedSJames Wright   CeedScalar fact;
26088626eedSJames Wright 
26188626eedSJames Wright   //*INDENT-OFF*
26288626eedSJames Wright   gijd[0] =   dXdx[0][0] * dXdx[0][0]
26388626eedSJames Wright             + dXdx[1][0] * dXdx[1][0]
26488626eedSJames Wright             + dXdx[2][0] * dXdx[2][0];
26588626eedSJames Wright 
26688626eedSJames Wright   gijd[1] =   dXdx[0][0] * dXdx[0][1]
26788626eedSJames Wright             + dXdx[1][0] * dXdx[1][1]
26888626eedSJames Wright             + dXdx[2][0] * dXdx[2][1];
26988626eedSJames Wright 
27088626eedSJames Wright   gijd[2] =   dXdx[0][1] * dXdx[0][1]
27188626eedSJames Wright             + dXdx[1][1] * dXdx[1][1]
27288626eedSJames Wright             + dXdx[2][1] * dXdx[2][1];
27388626eedSJames Wright 
27488626eedSJames Wright   gijd[3] =   dXdx[0][0] * dXdx[0][2]
27588626eedSJames Wright             + dXdx[1][0] * dXdx[1][2]
27688626eedSJames Wright             + dXdx[2][0] * dXdx[2][2];
27788626eedSJames Wright 
27888626eedSJames Wright   gijd[4] =   dXdx[0][1] * dXdx[0][2]
27988626eedSJames Wright             + dXdx[1][1] * dXdx[1][2]
28088626eedSJames Wright             + dXdx[2][1] * dXdx[2][2];
28188626eedSJames Wright 
28288626eedSJames Wright   gijd[5] =   dXdx[0][2] * dXdx[0][2]
28388626eedSJames Wright             + dXdx[1][2] * dXdx[1][2]
28488626eedSJames Wright             + dXdx[2][2] * dXdx[2][2];
28588626eedSJames Wright   //*INDENT-ON*
28688626eedSJames Wright 
28788626eedSJames Wright   dts = Ctau_t / dt ;
28888626eedSJames Wright 
28988626eedSJames Wright   tau = rho*rho*((4. * dts * dts)
29088626eedSJames Wright                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
29188626eedSJames Wright                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
29288626eedSJames Wright                  + u[2] *   u[2] * gijd[5])
29388626eedSJames Wright         + Ctau_v* mu * mu *
29488626eedSJames Wright         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
29588626eedSJames Wright          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
29688626eedSJames Wright 
29788626eedSJames Wright   fact=sqrt(tau);
29888626eedSJames Wright 
29988626eedSJames Wright   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
30088626eedSJames Wright 
30188626eedSJames Wright   Tau_d[1] = Ctau_M / fact;
30288626eedSJames Wright   Tau_d[2] = Ctau_E / ( fact * cv );
30388626eedSJames Wright 
30488626eedSJames Wright // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
30588626eedSJames Wright //  to avoid a division if the compiler is smart enough to see that cv IS
30688626eedSJames Wright // a constant that it could invert once for all elements
30788626eedSJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
30888626eedSJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to
30988626eedSJames Wright // know how to change constants with a change of fluid or units.  Same for
31088626eedSJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
31188626eedSJames Wright }
31288626eedSJames Wright 
31388626eedSJames Wright // *****************************************************************************
31488b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
31588b783a1SJames Wright // *****************************************************************************
31688b783a1SJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q,
31788b783a1SJames Wright                                const CeedScalar *const *in, CeedScalar *const *out) {
31888b783a1SJames Wright   // Inputs
31988b783a1SJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
32088b783a1SJames Wright 
32188b783a1SJames Wright   // Outputs
32288b783a1SJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
32388b783a1SJames Wright 
32488626eedSJames Wright   // Context
32588626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
32688626eedSJames Wright   const CeedScalar theta0    = context->theta0;
32788626eedSJames Wright   const CeedScalar P0        = context->P0;
32888626eedSJames Wright   const CeedScalar cv        = context->cv;
32988626eedSJames Wright   const CeedScalar cp        = context->cp;
33088626eedSJames Wright   const CeedScalar *g        = context->g;
33188626eedSJames Wright   const CeedScalar Rd        = cp - cv;
33288626eedSJames Wright 
33388b783a1SJames Wright   // Quadrature Point Loop
33488b783a1SJames Wright   CeedPragmaSIMD
33588b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
33688b783a1SJames Wright     CeedScalar q[5] = {0.};
33788b783a1SJames Wright 
33888b783a1SJames Wright     // Setup
33988b783a1SJames Wright     // -- Coordinates
34088626eedSJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
34188626eedSJames Wright     const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
34288b783a1SJames Wright 
34388b783a1SJames Wright     // -- Density
34488626eedSJames Wright     const CeedScalar rho = P0 / (Rd*theta0);
34588b783a1SJames Wright 
34688b783a1SJames Wright     // Initial Conditions
34788b783a1SJames Wright     q[0] = rho;
34888b783a1SJames Wright     q[1] = 0.0;
34988b783a1SJames Wright     q[2] = 0.0;
35088b783a1SJames Wright     q[3] = 0.0;
35188626eedSJames Wright     q[4] = rho * (cv*theta0 + e_potential);
35288b783a1SJames Wright 
35388b783a1SJames Wright     for (CeedInt j=0; j<5; j++)
35488b783a1SJames Wright       q0[j][i] = q[j];
35588b783a1SJames Wright   } // End of Quadrature Point Loop
35688b783a1SJames Wright   return 0;
35788b783a1SJames Wright }
35888b783a1SJames Wright 
35988b783a1SJames Wright // *****************************************************************************
36088b783a1SJames Wright // This QFunction implements the following formulation of Navier-Stokes with
36188b783a1SJames Wright //   explicit time stepping method
36288b783a1SJames Wright //
36388b783a1SJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
36488b783a1SJames Wright //   variables of density, momentum density, and total energy density.
36588b783a1SJames Wright //
36688b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
36788b783a1SJames Wright //   rho - Mass Density
36888b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
36988b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
37088b783a1SJames Wright //
37188b783a1SJames Wright // Navier-Stokes Equations:
37288b783a1SJames Wright //   drho/dt + div( U )                               = 0
37388b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
37488b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
37588b783a1SJames Wright //
37688b783a1SJames Wright // Viscous Stress:
37788b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
37888b783a1SJames Wright //
37988b783a1SJames Wright // Thermal Stress:
38088b783a1SJames Wright //   Fe = u Fu + k grad( T )
38188626eedSJames Wright // Equation of State
38288b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
38388b783a1SJames Wright //
38488b783a1SJames Wright // Stabilization:
38588b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
38688b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
38788b783a1SJames Wright //     gij = dXi/dX * dXi/dX
38888b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
38988b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
39088b783a1SJames Wright //     TauE = TauM / (Ce cv)
39188b783a1SJames Wright //
39288b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
39388b783a1SJames Wright //
39488b783a1SJames Wright // Constants:
39588b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
39688b783a1SJames Wright //   mu              ,  Dynamic viscosity
39788b783a1SJames Wright //   k               ,  Thermal conductivity
39888b783a1SJames Wright //   cv              ,  Specific heat, constant volume
39988b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
40088b783a1SJames Wright //   g               ,  Gravity
40188b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
40288b783a1SJames Wright //
40388b783a1SJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
40488b783a1SJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
40588b783a1SJames Wright // int( gradv gradu )
40688b783a1SJames Wright //
40788b783a1SJames Wright // *****************************************************************************
408*5c677226SJed Brown CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q,
40988b783a1SJames Wright                                       const CeedScalar *const *in, CeedScalar *const *out) {
41088b783a1SJames Wright   // *INDENT-OFF*
41188b783a1SJames Wright   // Inputs
41288b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
41388b783a1SJames Wright                    (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
41488b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
41588b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
41688b783a1SJames Wright   // Outputs
41788b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
41888b783a1SJames Wright              (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
41988b783a1SJames Wright   // *INDENT-ON*
42088b783a1SJames Wright 
42188b783a1SJames Wright   // Context
42288b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
42388b783a1SJames Wright   const CeedScalar mu     = context->mu;
42488b783a1SJames Wright   const CeedScalar cv     = context->cv;
42588b783a1SJames Wright   const CeedScalar cp     = context->cp;
42688626eedSJames Wright   const CeedScalar *g     = context->g;
42788626eedSJames Wright   const CeedScalar dt     = context->dt;
42888b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
42988626eedSJames Wright   const CeedScalar Rd     = cp - cv;
43088b783a1SJames Wright 
43188b783a1SJames Wright   CeedPragmaSIMD
43288b783a1SJames Wright   // Quadrature Point Loop
43388b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
434*5c677226SJed Brown     CeedScalar U[5];
435*5c677226SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
436*5c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
437*5c677226SJed Brown     State s = StateFromU(context, U, x_i);
438*5c677226SJed Brown 
43988b783a1SJames Wright     // -- Interp-to-Interp q_data
44088b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
44188b783a1SJames Wright     // -- Interp-to-Grad q_data
44288b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
44388b783a1SJames Wright     // *INDENT-OFF*
44488b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
44588b783a1SJames Wright                                     q_data[2][i],
44688b783a1SJames Wright                                     q_data[3][i]},
44788b783a1SJames Wright                                    {q_data[4][i],
44888b783a1SJames Wright                                     q_data[5][i],
44988b783a1SJames Wright                                     q_data[6][i]},
45088b783a1SJames Wright                                    {q_data[7][i],
45188b783a1SJames Wright                                     q_data[8][i],
45288b783a1SJames Wright                                     q_data[9][i]}
45388b783a1SJames Wright                                   };
45488b783a1SJames Wright     // *INDENT-ON*
45588b783a1SJames Wright 
456*5c677226SJed Brown     State grad_s[3];
457*5c677226SJed Brown     for (int j=0; j<3; j++) {
458*5c677226SJed Brown       CeedScalar dx_i[3] = {0};
459*5c677226SJed Brown       grad_s[j].U.density = dq[0][0][i] * dXdx[0][j]
460*5c677226SJed Brown                             + dq[1][0][i] * dXdx[1][j] + dq[2][0][i] * dXdx[2][j];
461*5c677226SJed Brown       for (int k=0; k<3; k++) grad_s[j].U.momentum[k] = dq[0][k+1][i] * dXdx[0][j]
462*5c677226SJed Brown             + dq[1][k+1][i] * dXdx[1][j] + dq[2][k+1][i] * dXdx[2][j];
463*5c677226SJed Brown       grad_s[j].U.E_total = dq[0][4][i] * dXdx[0][j] + dq[1][4][i] * dXdx[1][j] +
464*5c677226SJed Brown                             dq[2][4][i] * dXdx[2][j];
465*5c677226SJed Brown       dx_i[j] = 1.;
466*5c677226SJed Brown       grad_s[j].Y = StatePrimitiveFromConservative_fwd(context, s, grad_s[j].U,
467*5c677226SJed Brown                     x_i, dx_i);
468*5c677226SJed Brown     }
469*5c677226SJed Brown 
470*5c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
471*5c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
472*5c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
473*5c677226SJed Brown     KMUnpack(kmstress, stress);
474*5c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
475*5c677226SJed Brown 
476*5c677226SJed Brown     StateConservative F_inviscid[3];
477*5c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
478*5c677226SJed Brown 
479*5c677226SJed Brown     // Total flux
480*5c677226SJed Brown     CeedScalar Flux[5][3];
481*5c677226SJed Brown     for (int j=0; j<3; j++) {
482*5c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
483*5c677226SJed Brown       for (int k=0; k<3; k++)
484*5c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
485*5c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
486*5c677226SJed Brown     }
487*5c677226SJed Brown 
488*5c677226SJed Brown     for (int j=0; j<3; j++) {
489*5c677226SJed Brown       for (int k=0; k<5; k++) {
490*5c677226SJed Brown         dv[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
491*5c677226SJed Brown                                dXdx[j][1] * Flux[k][1] +
492*5c677226SJed Brown                                dXdx[j][2] * Flux[k][2]);
493*5c677226SJed Brown       }
494*5c677226SJed Brown     }
495*5c677226SJed Brown 
496*5c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
497*5c677226SJed Brown     for (int j=0; j<5; j++)
498*5c677226SJed Brown       v[j][i] = wdetJ * body_force[j];
49988b783a1SJames Wright 
50088b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
501*5c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
502*5c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
503*5c677226SJed Brown                            gamma, g, x_i);
504*5c677226SJed Brown     CeedScalar grad_U[5][3];
50588b783a1SJames Wright     for (int j=0; j<3; j++) {
506*5c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
507*5c677226SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
508*5c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
50988b783a1SJames Wright     }
51088b783a1SJames Wright 
51188b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
51288b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
51388b783a1SJames Wright     for (int j=0; j<3; j++)
51488b783a1SJames Wright       for (int k=0; k<5; k++)
51588b783a1SJames Wright         for (int l=0; l<5; l++)
516*5c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
51788b783a1SJames Wright 
51888626eedSJames Wright     // -- Stabilization method: none, SU, or SUPG
51988626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
52088626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
52188626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
52288b783a1SJames Wright     switch (context->stabilization) {
52388b783a1SJames Wright     case STAB_NONE:        // Galerkin
52488b783a1SJames Wright       break;
52588b783a1SJames Wright     case STAB_SU:        // SU
526*5c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
52788626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
52888626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
52988626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
53088626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
53188626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
532*5c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
533*5c677226SJed Brown                                   tau_strong_conv,
53488626eedSJames Wright                                   tau_strong_conv_conservative);
53588b783a1SJames Wright       for (int j=0; j<3; j++)
53688b783a1SJames Wright         for (int k=0; k<5; k++)
53788b783a1SJames Wright           for (int l=0; l<5; l++)
53888626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
53988b783a1SJames Wright 
54088b783a1SJames Wright       for (int j=0; j<5; j++)
54188b783a1SJames Wright         for (int k=0; k<3; k++)
54288b783a1SJames Wright           dv[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
54388b783a1SJames Wright                                 stab[j][1] * dXdx[k][1] +
54488b783a1SJames Wright                                 stab[j][2] * dXdx[k][2]);
54588b783a1SJames Wright       break;
54688b783a1SJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
54788b783a1SJames Wright       break;
54888b783a1SJames Wright     }
54988b783a1SJames Wright 
55088b783a1SJames Wright   } // End Quadrature Point Loop
55188b783a1SJames Wright 
55288b783a1SJames Wright   // Return
55388b783a1SJames Wright   return 0;
55488b783a1SJames Wright }
55588b783a1SJames Wright 
55688b783a1SJames Wright // *****************************************************************************
55788b783a1SJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
55888b783a1SJames Wright //   implicit time stepping method
55988b783a1SJames Wright //
56088b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
56188b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
56288b783a1SJames Wright //                                       (diffussive terms will be added later)
56388b783a1SJames Wright //
56488b783a1SJames Wright // *****************************************************************************
56588b783a1SJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
56688b783a1SJames Wright                                     const CeedScalar *const *in,
56788b783a1SJames Wright                                     CeedScalar *const *out) {
56888b783a1SJames Wright   // *INDENT-OFF*
56988b783a1SJames Wright   // Inputs
57088b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
57188b783a1SJames Wright                    (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
57288b783a1SJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
57388b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
57488b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
57588b783a1SJames Wright   // Outputs
57688b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
57788b783a1SJames Wright              (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
57888b783a1SJames Wright   // *INDENT-ON*
57988b783a1SJames Wright   // Context
58088b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
58188b783a1SJames Wright   const CeedScalar mu     = context->mu;
58288b783a1SJames Wright   const CeedScalar cv     = context->cv;
58388b783a1SJames Wright   const CeedScalar cp     = context->cp;
58488626eedSJames Wright   const CeedScalar *g     = context->g;
58588626eedSJames Wright   const CeedScalar dt     = context->dt;
58688b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
58788626eedSJames Wright   const CeedScalar Rd     = cp-cv;
58888b783a1SJames Wright 
58988b783a1SJames Wright   CeedPragmaSIMD
59088b783a1SJames Wright   // Quadrature Point Loop
59188b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
592*5c677226SJed Brown     CeedScalar U[5];
593*5c677226SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
594*5c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
595*5c677226SJed Brown     State s = StateFromU(context, U, x_i);
596*5c677226SJed Brown 
59788b783a1SJames Wright     // -- Interp-to-Interp q_data
59888b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
59988b783a1SJames Wright     // -- Interp-to-Grad q_data
60088b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
60188b783a1SJames Wright     // *INDENT-OFF*
60288b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
60388b783a1SJames Wright                                     q_data[2][i],
60488b783a1SJames Wright                                     q_data[3][i]},
60588b783a1SJames Wright                                    {q_data[4][i],
60688b783a1SJames Wright                                     q_data[5][i],
60788b783a1SJames Wright                                     q_data[6][i]},
60888b783a1SJames Wright                                    {q_data[7][i],
60988b783a1SJames Wright                                     q_data[8][i],
61088b783a1SJames Wright                                     q_data[9][i]}
61188b783a1SJames Wright                                   };
61288b783a1SJames Wright     // *INDENT-ON*
613*5c677226SJed Brown     State grad_s[3];
61488b783a1SJames Wright     for (int j=0; j<3; j++) {
615*5c677226SJed Brown       CeedScalar dx_i[3];
616*5c677226SJed Brown       grad_s[j].U.density = dq[0][0][i] * dXdx[0][j]
617*5c677226SJed Brown                             + dq[1][0][i] * dXdx[1][j] + dq[2][0][i] * dXdx[2][j];
618*5c677226SJed Brown       for (int k=0; k<3; k++) grad_s[j].U.momentum[k] = dq[0][k+1][i] * dXdx[0][j]
619*5c677226SJed Brown             + dq[1][k+1][i] * dXdx[1][j] + dq[2][k+1][i] * dXdx[2][j];
620*5c677226SJed Brown       grad_s[j].U.E_total = dq[0][4][i] * dXdx[0][j] + dq[1][4][i] * dXdx[1][j] +
621*5c677226SJed Brown                             dq[2][4][i] * dXdx[2][j];
622*5c677226SJed Brown       dx_i[j] = 1.;
623*5c677226SJed Brown       grad_s[j].Y = StatePrimitiveFromConservative_fwd(context, s, grad_s[j].U,
624*5c677226SJed Brown                     x_i, dx_i);
62588b783a1SJames Wright     }
626*5c677226SJed Brown 
627*5c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
628*5c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
629*5c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
630*5c677226SJed Brown     KMUnpack(kmstress, stress);
631*5c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
632*5c677226SJed Brown 
633*5c677226SJed Brown     StateConservative F_inviscid[3];
634*5c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
635*5c677226SJed Brown 
636*5c677226SJed Brown 
637*5c677226SJed Brown     // Total flux
638*5c677226SJed Brown     CeedScalar Flux[5][3];
639*5c677226SJed Brown     for (int j=0; j<3; j++) {
640*5c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
64188b783a1SJames Wright       for (int k=0; k<3; k++)
642*5c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
643*5c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
644*5c677226SJed Brown     }
645*5c677226SJed Brown 
646*5c677226SJed Brown     for (int j=0; j<3; j++) {
647*5c677226SJed Brown       for (int k=0; k<5; k++) {
648*5c677226SJed Brown         dv[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
649*5c677226SJed Brown                                 dXdx[j][1] * Flux[k][1] +
650*5c677226SJed Brown                                 dXdx[j][2] * Flux[k][2]);
651*5c677226SJed Brown       }
652*5c677226SJed Brown     }
653*5c677226SJed Brown 
654*5c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
655*5c677226SJed Brown     for (int j=0; j<5; j++)
656*5c677226SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
65788b783a1SJames Wright 
65888b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
659*5c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
660*5c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
661*5c677226SJed Brown                            gamma, g, x_i);
662*5c677226SJed Brown     CeedScalar grad_U[5][3];
66388b783a1SJames Wright     for (int j=0; j<3; j++) {
664*5c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
665*5c677226SJed Brown       for (int k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
666*5c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
66788b783a1SJames Wright     }
668*5c677226SJed Brown 
66988b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
67088b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
67188b783a1SJames Wright     for (int j=0; j<3; j++)
67288b783a1SJames Wright       for (int k=0; k<5; k++)
67388b783a1SJames Wright         for (int l=0; l<5; l++)
674*5c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
67588b783a1SJames Wright 
67688b783a1SJames Wright     // Strong residual
67788b783a1SJames Wright     CeedScalar strong_res[5];
67888b783a1SJames Wright     for (int j=0; j<5; j++)
67988b783a1SJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
68088b783a1SJames Wright 
68188b783a1SJames Wright     // -- Stabilization method: none, SU, or SUPG
68288626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
68388626eedSJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
68488626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
68588626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
68688b783a1SJames Wright     switch (context->stabilization) {
68788b783a1SJames Wright     case STAB_NONE:        // Galerkin
68888b783a1SJames Wright       break;
68988b783a1SJames Wright     case STAB_SU:        // SU
690*5c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
69188626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
69288626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
69388626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
69488626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
69588626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
696*5c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
697*5c677226SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
69888b783a1SJames Wright       for (int j=0; j<3; j++)
69988b783a1SJames Wright         for (int k=0; k<5; k++)
70088b783a1SJames Wright           for (int l=0; l<5; l++)
70188626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
70288b783a1SJames Wright 
70388b783a1SJames Wright       for (int j=0; j<5; j++)
70488b783a1SJames Wright         for (int k=0; k<3; k++)
70588b783a1SJames Wright           dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
70688b783a1SJames Wright                                 stab[j][1] * dXdx[k][1] +
70788b783a1SJames Wright                                 stab[j][2] * dXdx[k][2]);
70888b783a1SJames Wright       break;
70988b783a1SJames Wright     case STAB_SUPG:        // SUPG
710*5c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
71188626eedSJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
71288626eedSJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
71388626eedSJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
71488626eedSJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
71588626eedSJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
71688626eedSJames Wright // Alternate route (useful later with primitive variable code)
71788626eedSJames Wright // this function was verified against PHASTA for as IC that was as close as possible
71888626eedSJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
71988626eedSJames Wright // it has also been verified to compute a correct through the following
72088626eedSJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
72188626eedSJames Wright // applied in the triple loop below
72288626eedSJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
723*5c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
724*5c677226SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
72588b783a1SJames Wright       for (int j=0; j<3; j++)
72688b783a1SJames Wright         for (int k=0; k<5; k++)
72788b783a1SJames Wright           for (int l=0; l<5; l++)
72888626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
72988b783a1SJames Wright 
73088b783a1SJames Wright       for (int j=0; j<5; j++)
73188b783a1SJames Wright         for (int k=0; k<3; k++)
73288b783a1SJames Wright           dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
73388b783a1SJames Wright                                 stab[j][1] * dXdx[k][1] +
73488b783a1SJames Wright                                 stab[j][2] * dXdx[k][2]);
73588b783a1SJames Wright       break;
73688b783a1SJames Wright     }
73788b783a1SJames Wright 
73888b783a1SJames Wright   } // End Quadrature Point Loop
73988b783a1SJames Wright 
74088b783a1SJames Wright   // Return
74188b783a1SJames Wright   return 0;
74288b783a1SJames Wright }
74388b783a1SJames Wright // *****************************************************************************
74488b783a1SJames Wright #endif // newtonian_h
745