1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 33a8779fbSJames Wright // 4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 53a8779fbSJames Wright // 6727da7e7SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 73a8779fbSJames Wright 83a8779fbSJames Wright /// @file 93a8779fbSJames Wright /// Operator for Navier-Stokes example using PETSc 103a8779fbSJames Wright 113a8779fbSJames Wright 123a8779fbSJames Wright #ifndef newtonian_h 133a8779fbSJames Wright #define newtonian_h 143a8779fbSJames Wright 153a8779fbSJames Wright #include <math.h> 163a8779fbSJames Wright #include <ceed.h> 1715a3537eSJed Brown #include "newtonian_types.h" 183a8779fbSJames Wright 193a8779fbSJames Wright #ifndef M_PI 203a8779fbSJames Wright #define M_PI 3.14159265358979323846 213a8779fbSJames Wright #endif 223a8779fbSJames Wright 233a8779fbSJames Wright // ***************************************************************************** 243a8779fbSJames Wright // Helper function for computing flux Jacobian 253a8779fbSJames Wright // ***************************************************************************** 263a8779fbSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5], 273a8779fbSJames Wright const CeedScalar rho, const CeedScalar u[3], const CeedScalar E, 28bb8a0c61SJames Wright const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) { 293a8779fbSJames Wright CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square 30bb8a0c61SJames Wright CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]); 313a8779fbSJames Wright for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions 323a8779fbSJames Wright for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix 33bb8a0c61SJames Wright dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) - 34bb8a0c61SJames Wright u[i]*u[j]; 353a8779fbSJames Wright for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix 363a8779fbSJames Wright dF[i][0][k+1] = ((i==k) ? 1. : 0.); 373a8779fbSJames Wright dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) + 383a8779fbSJames Wright ((i==k) ? u[j] : 0.) - 393a8779fbSJames Wright ((i==j) ? u[k] : 0.) * (gamma-1.); 403a8779fbSJames Wright dF[i][4][k+1] = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) - 413a8779fbSJames Wright (gamma-1.)*u[i]*u[k]; 423a8779fbSJames Wright } 433a8779fbSJames Wright dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.); 443a8779fbSJames Wright } 453a8779fbSJames Wright dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho); 463a8779fbSJames Wright dF[i][4][4] = u[i] * gamma; 473a8779fbSJames Wright } 483a8779fbSJames Wright } 493a8779fbSJames Wright 503a8779fbSJames Wright // ***************************************************************************** 51bb8a0c61SJames Wright // Helper function for computing flux Jacobian of Primitive variables 52bb8a0c61SJames Wright // ***************************************************************************** 53bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5], 54bb8a0c61SJames Wright const CeedScalar rho, const CeedScalar u[3], const CeedScalar E, 55bb8a0c61SJames Wright const CeedScalar Rd, const CeedScalar cv) { 56bb8a0c61SJames Wright CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square 57bb8a0c61SJames Wright // TODO Add in gravity's contribution 58bb8a0c61SJames Wright 59bb8a0c61SJames Wright CeedScalar T = ( E / rho - u_sq / 2. ) / cv; 60bb8a0c61SJames Wright CeedScalar drdT = -rho / T; 61bb8a0c61SJames Wright CeedScalar drdP = 1. / ( Rd * T); 62bb8a0c61SJames Wright CeedScalar etot = E / rho ; 63bb8a0c61SJames Wright CeedScalar e2p = drdP * etot + 1. ; 64bb8a0c61SJames Wright CeedScalar e3p = ( E + rho * Rd * T ); 65bb8a0c61SJames Wright CeedScalar e4p = drdT * etot + rho * cv ; 66bb8a0c61SJames Wright 67bb8a0c61SJames Wright for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions 68bb8a0c61SJames Wright for (CeedInt j=0; j<3; j++) { // j counts F^{m_j} 69bb8a0c61SJames Wright // [row][col] of A_i 70bb8a0c61SJames Wright dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p 71bb8a0c61SJames Wright for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k 722acc7cbcSKenneth E. Jansen dF[i][0][k+1] = ((i==k) ? rho : 0.); // F^c wrt u_k 73bb8a0c61SJames Wright dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) + // F^m_j wrt u_k 74bb8a0c61SJames Wright ((i==k) ? u[j] : 0.) ) * rho; 75bb8a0c61SJames Wright dF[i][4][k+1] = rho * u[i] * u[k] 76bb8a0c61SJames Wright + ((i==k) ? e3p : 0.) ; // F^e wrt u_k 77bb8a0c61SJames Wright } 78bb8a0c61SJames Wright dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T 79bb8a0c61SJames Wright } 80bb8a0c61SJames Wright dF[i][4][0] = u[i] * e2p; // F^e wrt p 81bb8a0c61SJames Wright dF[i][4][4] = u[i] * e4p; // F^e wrt T 82bb8a0c61SJames Wright dF[i][0][0] = u[i] * drdP; // F^c wrt p 83bb8a0c61SJames Wright dF[i][0][4] = u[i] * drdT; // F^c wrt T 84bb8a0c61SJames Wright } 85bb8a0c61SJames Wright } 86bb8a0c61SJames Wright 87bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho, 88bb8a0c61SJames Wright const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd, 89bb8a0c61SJames Wright const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) { 90bb8a0c61SJames Wright CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; 91bb8a0c61SJames Wright CeedScalar T = ( E / rho - u_sq / 2. ) / cv; 92bb8a0c61SJames Wright CeedScalar drdT = -rho / T; 93bb8a0c61SJames Wright CeedScalar drdP = 1. / ( Rd * T); 94bb8a0c61SJames Wright dU[0] = drdP * dY[0] + drdT * dY[4]; 95bb8a0c61SJames Wright CeedScalar de_kinetic = 0; 96*493642f1SJames Wright for (CeedInt i=0; i<3; i++) { 97bb8a0c61SJames Wright dU[1+i] = dU[0] * u[i] + rho * dY[1+i]; 98bb8a0c61SJames Wright de_kinetic += u[i] * dY[1+i]; 99bb8a0c61SJames Wright } 100bb8a0c61SJames Wright dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e 101bb8a0c61SJames Wright + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2 102bb8a0c61SJames Wright } 103bb8a0c61SJames Wright 104bb8a0c61SJames Wright // ***************************************************************************** 105bb8a0c61SJames Wright // Helper function for computing Tau elements (stabilization constant) 106bb8a0c61SJames Wright // Model from: 107bb8a0c61SJames Wright // PHASTA 108bb8a0c61SJames Wright // 109bb8a0c61SJames Wright // Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial) 110bb8a0c61SJames Wright // 111bb8a0c61SJames Wright // Where NOT UPDATED YET 112bb8a0c61SJames Wright // ***************************************************************************** 113bb8a0c61SJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3], 114bb8a0c61SJames Wright const CeedScalar dXdx[3][3], const CeedScalar u[3], 115bb8a0c61SJames Wright const CeedScalar cv, const NewtonianIdealGasContext newt_ctx, 116bb8a0c61SJames Wright const CeedScalar mu, const CeedScalar dt, 117bb8a0c61SJames Wright const CeedScalar rho) { 118bb8a0c61SJames Wright // Context 119bb8a0c61SJames Wright const CeedScalar Ctau_t = newt_ctx->Ctau_t; 120bb8a0c61SJames Wright const CeedScalar Ctau_v = newt_ctx->Ctau_v; 121bb8a0c61SJames Wright const CeedScalar Ctau_C = newt_ctx->Ctau_C; 122bb8a0c61SJames Wright const CeedScalar Ctau_M = newt_ctx->Ctau_M; 123bb8a0c61SJames Wright const CeedScalar Ctau_E = newt_ctx->Ctau_E; 124bb8a0c61SJames Wright CeedScalar gijd[6]; 125bb8a0c61SJames Wright CeedScalar tau; 126bb8a0c61SJames Wright CeedScalar dts; 127bb8a0c61SJames Wright CeedScalar fact; 128bb8a0c61SJames Wright 129bb8a0c61SJames Wright //*INDENT-OFF* 130bb8a0c61SJames Wright gijd[0] = dXdx[0][0] * dXdx[0][0] 131bb8a0c61SJames Wright + dXdx[1][0] * dXdx[1][0] 132bb8a0c61SJames Wright + dXdx[2][0] * dXdx[2][0]; 133bb8a0c61SJames Wright 134bb8a0c61SJames Wright gijd[1] = dXdx[0][0] * dXdx[0][1] 135bb8a0c61SJames Wright + dXdx[1][0] * dXdx[1][1] 136bb8a0c61SJames Wright + dXdx[2][0] * dXdx[2][1]; 137bb8a0c61SJames Wright 138bb8a0c61SJames Wright gijd[2] = dXdx[0][1] * dXdx[0][1] 139bb8a0c61SJames Wright + dXdx[1][1] * dXdx[1][1] 140bb8a0c61SJames Wright + dXdx[2][1] * dXdx[2][1]; 141bb8a0c61SJames Wright 142bb8a0c61SJames Wright gijd[3] = dXdx[0][0] * dXdx[0][2] 143bb8a0c61SJames Wright + dXdx[1][0] * dXdx[1][2] 144bb8a0c61SJames Wright + dXdx[2][0] * dXdx[2][2]; 145bb8a0c61SJames Wright 146bb8a0c61SJames Wright gijd[4] = dXdx[0][1] * dXdx[0][2] 147bb8a0c61SJames Wright + dXdx[1][1] * dXdx[1][2] 148bb8a0c61SJames Wright + dXdx[2][1] * dXdx[2][2]; 149bb8a0c61SJames Wright 150bb8a0c61SJames Wright gijd[5] = dXdx[0][2] * dXdx[0][2] 151bb8a0c61SJames Wright + dXdx[1][2] * dXdx[1][2] 152bb8a0c61SJames Wright + dXdx[2][2] * dXdx[2][2]; 153bb8a0c61SJames Wright //*INDENT-ON* 154bb8a0c61SJames Wright 155bb8a0c61SJames Wright dts = Ctau_t / dt ; 156bb8a0c61SJames Wright 157bb8a0c61SJames Wright tau = rho*rho*((4. * dts * dts) 158bb8a0c61SJames Wright + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3])) 159bb8a0c61SJames Wright + u[1] * ( u[1] * gijd[2] + 2. * u[2] * gijd[4]) 160bb8a0c61SJames Wright + u[2] * u[2] * gijd[5]) 161bb8a0c61SJames Wright + Ctau_v* mu * mu * 162bb8a0c61SJames Wright (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] + 163bb8a0c61SJames Wright + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4])); 164bb8a0c61SJames Wright 165bb8a0c61SJames Wright fact=sqrt(tau); 166bb8a0c61SJames Wright 167bb8a0c61SJames Wright Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125; 168bb8a0c61SJames Wright 169bb8a0c61SJames Wright Tau_d[1] = Ctau_M / fact; 170bb8a0c61SJames Wright Tau_d[2] = Ctau_E / ( fact * cv ); 171bb8a0c61SJames Wright 172bb8a0c61SJames Wright // consider putting back the way I initially had it Ctau_E * Tau_d[1] /cv 173bb8a0c61SJames Wright // to avoid a division if the compiler is smart enough to see that cv IS 174bb8a0c61SJames Wright // a constant that it could invert once for all elements 175bb8a0c61SJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M 176bb8a0c61SJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to 177bb8a0c61SJames Wright // know how to change constants with a change of fluid or units. Same for 178bb8a0c61SJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T) 179bb8a0c61SJames Wright } 180bb8a0c61SJames Wright 181bb8a0c61SJames Wright // ***************************************************************************** 1823a8779fbSJames Wright // Helper function for computing Tau elements (stabilization constant) 1833a8779fbSJames Wright // Model from: 1843a8779fbSJames Wright // Stabilized Methods for Compressible Flows, Hughes et al 2010 1853a8779fbSJames Wright // 1863a8779fbSJames Wright // Spatial criterion #2 - Tau is a 3x3 diagonal matrix 1873a8779fbSJames Wright // Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum) 1883a8779fbSJames Wright // 1893a8779fbSJames Wright // Where 1903a8779fbSJames Wright // c_tau = stabilization constant (0.5 is reported as "optimal") 1913a8779fbSJames Wright // h[i] = 2 length(dxdX[i]) 1923a8779fbSJames Wright // Pe = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity ) 1933a8779fbSJames Wright // Xi(Pe) = coth Pe - 1. / Pe (1. at large local Peclet number ) 1943a8779fbSJames Wright // rho(A[i]) = spectral radius of the convective flux Jacobian i, 1953a8779fbSJames Wright // wave speed in direction i 1963a8779fbSJames Wright // ***************************************************************************** 1973a8779fbSJames Wright CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3], 1983a8779fbSJames Wright const CeedScalar dXdx[3][3], const CeedScalar u[3], 199bb8a0c61SJames Wright /* const CeedScalar sound_speed, const CeedScalar c_tau) { */ 200bb8a0c61SJames Wright const CeedScalar sound_speed, const CeedScalar c_tau, 201bb8a0c61SJames Wright const CeedScalar viscosity) { 202bb8a0c61SJames Wright const CeedScalar mag_u_visc = sqrt(u[0]*u[0] +u[1]*u[1] +u[2]*u[2]) / 203bb8a0c61SJames Wright (2*viscosity); 204*493642f1SJames Wright for (CeedInt i=0; i<3; i++) { 2053a8779fbSJames Wright // length of element in direction i 2063a8779fbSJames Wright CeedScalar h = 2 / sqrt(dXdx[0][i]*dXdx[0][i] + dXdx[1][i]*dXdx[1][i] + 2073a8779fbSJames Wright dXdx[2][i]*dXdx[2][i]); 208bb8a0c61SJames Wright CeedScalar Pe = mag_u_visc*h; 209bb8a0c61SJames Wright CeedScalar Xi = 1/tanh(Pe) - 1/Pe; 2103a8779fbSJames Wright // fastest wave in direction i 2113a8779fbSJames Wright CeedScalar fastest_wave = fabs(u[i]) + sound_speed; 212bb8a0c61SJames Wright Tau_x[i] = c_tau * h * Xi / fastest_wave; 2133a8779fbSJames Wright } 2143a8779fbSJames Wright } 2153a8779fbSJames Wright 2163a8779fbSJames Wright // ***************************************************************************** 2173a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems 2183a8779fbSJames Wright // ***************************************************************************** 2193a8779fbSJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q, 2203a8779fbSJames Wright const CeedScalar *const *in, CeedScalar *const *out) { 2213a8779fbSJames Wright // Inputs 2223a8779fbSJames Wright const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 2233a8779fbSJames Wright 2243a8779fbSJames Wright // Outputs 2253a8779fbSJames Wright CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 2263a8779fbSJames Wright 227bb8a0c61SJames Wright // Context 228bb8a0c61SJames Wright const SetupContext context = (SetupContext)ctx; 229bb8a0c61SJames Wright const CeedScalar theta0 = context->theta0; 230bb8a0c61SJames Wright const CeedScalar P0 = context->P0; 231bb8a0c61SJames Wright const CeedScalar cv = context->cv; 232bb8a0c61SJames Wright const CeedScalar cp = context->cp; 233bb8a0c61SJames Wright const CeedScalar *g = context->g; 234bb8a0c61SJames Wright const CeedScalar Rd = cp - cv; 235bb8a0c61SJames Wright 2363a8779fbSJames Wright // Quadrature Point Loop 2373a8779fbSJames Wright CeedPragmaSIMD 2383a8779fbSJames Wright for (CeedInt i=0; i<Q; i++) { 2393a8779fbSJames Wright CeedScalar q[5] = {0.}; 2403a8779fbSJames Wright 2413a8779fbSJames Wright // Setup 2423a8779fbSJames Wright // -- Coordinates 243bb8a0c61SJames Wright const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]}; 244bb8a0c61SJames Wright const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]); 2453a8779fbSJames Wright 2463a8779fbSJames Wright // -- Density 247bb8a0c61SJames Wright const CeedScalar rho = P0 / (Rd*theta0); 2483a8779fbSJames Wright 2493a8779fbSJames Wright // Initial Conditions 2503a8779fbSJames Wright q[0] = rho; 2513a8779fbSJames Wright q[1] = 0.0; 2523a8779fbSJames Wright q[2] = 0.0; 2533a8779fbSJames Wright q[3] = 0.0; 254bb8a0c61SJames Wright q[4] = rho * (cv*theta0 + e_potential); 2553a8779fbSJames Wright 2563a8779fbSJames Wright for (CeedInt j=0; j<5; j++) 2573a8779fbSJames Wright q0[j][i] = q[j]; 2583a8779fbSJames Wright } // End of Quadrature Point Loop 2593a8779fbSJames Wright return 0; 2603a8779fbSJames Wright } 2613a8779fbSJames Wright 2623a8779fbSJames Wright // ***************************************************************************** 2633a8779fbSJames Wright // This QFunction implements the following formulation of Navier-Stokes with 2643a8779fbSJames Wright // explicit time stepping method 2653a8779fbSJames Wright // 2663a8779fbSJames Wright // This is 3D compressible Navier-Stokes in conservation form with state 2673a8779fbSJames Wright // variables of density, momentum density, and total energy density. 2683a8779fbSJames Wright // 2693a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E ) 2703a8779fbSJames Wright // rho - Mass Density 2713a8779fbSJames Wright // Ui - Momentum Density, Ui = rho ui 2723a8779fbSJames Wright // E - Total Energy Density, E = rho (cv T + (u u)/2 + g z) 2733a8779fbSJames Wright // 2743a8779fbSJames Wright // Navier-Stokes Equations: 2753a8779fbSJames Wright // drho/dt + div( U ) = 0 2763a8779fbSJames Wright // dU/dt + div( rho (u x u) + P I3 ) + rho g khat = div( Fu ) 2773a8779fbSJames Wright // dE/dt + div( (E + P) u ) = div( Fe ) 2783a8779fbSJames Wright // 2793a8779fbSJames Wright // Viscous Stress: 2803a8779fbSJames Wright // Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3) 2813a8779fbSJames Wright // 2823a8779fbSJames Wright // Thermal Stress: 2833a8779fbSJames Wright // Fe = u Fu + k grad( T ) 284bb8a0c61SJames Wright // Equation of State 2853a8779fbSJames Wright // P = (gamma - 1) (E - rho (u u) / 2 - rho g z) 2863a8779fbSJames Wright // 2873a8779fbSJames Wright // Stabilization: 2883a8779fbSJames Wright // Tau = diag(TauC, TauM, TauM, TauM, TauE) 2893a8779fbSJames Wright // f1 = rho sqrt(ui uj gij) 2903a8779fbSJames Wright // gij = dXi/dX * dXi/dX 2913a8779fbSJames Wright // TauC = Cc f1 / (8 gii) 2923a8779fbSJames Wright // TauM = min( 1 , 1 / f1 ) 2933a8779fbSJames Wright // TauE = TauM / (Ce cv) 2943a8779fbSJames Wright // 2953a8779fbSJames Wright // SU = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) ) 2963a8779fbSJames Wright // 2973a8779fbSJames Wright // Constants: 2983a8779fbSJames Wright // lambda = - 2 / 3, From Stokes hypothesis 2993a8779fbSJames Wright // mu , Dynamic viscosity 3003a8779fbSJames Wright // k , Thermal conductivity 3013a8779fbSJames Wright // cv , Specific heat, constant volume 3023a8779fbSJames Wright // cp , Specific heat, constant pressure 3033a8779fbSJames Wright // g , Gravity 3043a8779fbSJames Wright // gamma = cp / cv, Specific heat ratio 3053a8779fbSJames Wright // 3063a8779fbSJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and 3073a8779fbSJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form: 3083a8779fbSJames Wright // int( gradv gradu ) 3093a8779fbSJames Wright // 3103a8779fbSJames Wright // ***************************************************************************** 3113a8779fbSJames Wright CEED_QFUNCTION(Newtonian)(void *ctx, CeedInt Q, 3123a8779fbSJames Wright const CeedScalar *const *in, CeedScalar *const *out) { 3133a8779fbSJames Wright // *INDENT-OFF* 3143a8779fbSJames Wright // Inputs 3153a8779fbSJames Wright const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 3163a8779fbSJames Wright (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 3173a8779fbSJames Wright (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], 3183a8779fbSJames Wright (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 3193a8779fbSJames Wright // Outputs 3203a8779fbSJames Wright CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], 3213a8779fbSJames Wright (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 3223a8779fbSJames Wright // *INDENT-ON* 3233a8779fbSJames Wright 3243a8779fbSJames Wright // Context 3253a8779fbSJames Wright NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx; 3263a8779fbSJames Wright const CeedScalar lambda = context->lambda; 3273a8779fbSJames Wright const CeedScalar mu = context->mu; 3283a8779fbSJames Wright const CeedScalar k = context->k; 3293a8779fbSJames Wright const CeedScalar cv = context->cv; 3303a8779fbSJames Wright const CeedScalar cp = context->cp; 331bb8a0c61SJames Wright const CeedScalar *g = context->g; 332bb8a0c61SJames Wright const CeedScalar dt = context->dt; 3333a8779fbSJames Wright const CeedScalar gamma = cp / cv; 334bb8a0c61SJames Wright const CeedScalar Rd = cp - cv; 3353a8779fbSJames Wright 3363a8779fbSJames Wright CeedPragmaSIMD 3373a8779fbSJames Wright // Quadrature Point Loop 3383a8779fbSJames Wright for (CeedInt i=0; i<Q; i++) { 3393a8779fbSJames Wright // *INDENT-OFF* 3403a8779fbSJames Wright // Setup 3413a8779fbSJames Wright // -- Interp in 3423a8779fbSJames Wright const CeedScalar rho = q[0][i]; 3433a8779fbSJames Wright const CeedScalar u[3] = {q[1][i] / rho, 3443a8779fbSJames Wright q[2][i] / rho, 3453a8779fbSJames Wright q[3][i] / rho 3463a8779fbSJames Wright }; 3473a8779fbSJames Wright const CeedScalar E = q[4][i]; 3483a8779fbSJames Wright // -- Grad in 3493a8779fbSJames Wright const CeedScalar drho[3] = {dq[0][0][i], 3503a8779fbSJames Wright dq[1][0][i], 3513a8779fbSJames Wright dq[2][0][i] 3523a8779fbSJames Wright }; 3533a8779fbSJames Wright const CeedScalar dU[3][3] = {{dq[0][1][i], 3543a8779fbSJames Wright dq[1][1][i], 3553a8779fbSJames Wright dq[2][1][i]}, 3563a8779fbSJames Wright {dq[0][2][i], 3573a8779fbSJames Wright dq[1][2][i], 3583a8779fbSJames Wright dq[2][2][i]}, 3593a8779fbSJames Wright {dq[0][3][i], 3603a8779fbSJames Wright dq[1][3][i], 3613a8779fbSJames Wright dq[2][3][i]} 3623a8779fbSJames Wright }; 3633a8779fbSJames Wright const CeedScalar dE[3] = {dq[0][4][i], 3643a8779fbSJames Wright dq[1][4][i], 3653a8779fbSJames Wright dq[2][4][i] 3663a8779fbSJames Wright }; 3673a8779fbSJames Wright // -- Interp-to-Interp q_data 3683a8779fbSJames Wright const CeedScalar wdetJ = q_data[0][i]; 3693a8779fbSJames Wright // -- Interp-to-Grad q_data 3703a8779fbSJames Wright // ---- Inverse of change of coordinate matrix: X_i,j 3713a8779fbSJames Wright // *INDENT-OFF* 3723a8779fbSJames Wright const CeedScalar dXdx[3][3] = {{q_data[1][i], 3733a8779fbSJames Wright q_data[2][i], 3743a8779fbSJames Wright q_data[3][i]}, 3753a8779fbSJames Wright {q_data[4][i], 3763a8779fbSJames Wright q_data[5][i], 3773a8779fbSJames Wright q_data[6][i]}, 3783a8779fbSJames Wright {q_data[7][i], 3793a8779fbSJames Wright q_data[8][i], 3803a8779fbSJames Wright q_data[9][i]} 3813a8779fbSJames Wright }; 382bb8a0c61SJames Wright const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]}; 3833a8779fbSJames Wright // *INDENT-ON* 3843a8779fbSJames Wright // -- Grad-to-Grad q_data 3853a8779fbSJames Wright // dU/dx 3863a8779fbSJames Wright CeedScalar du[3][3] = {{0}}; 3873a8779fbSJames Wright CeedScalar drhodx[3] = {0}; 3883a8779fbSJames Wright CeedScalar dEdx[3] = {0}; 3893a8779fbSJames Wright CeedScalar dUdx[3][3] = {{0}}; 3903a8779fbSJames Wright CeedScalar dXdxdXdxT[3][3] = {{0}}; 391*493642f1SJames Wright for (CeedInt j=0; j<3; j++) { 392*493642f1SJames Wright for (CeedInt k=0; k<3; k++) { 3933a8779fbSJames Wright du[j][k] = (dU[j][k] - drho[k]*u[j]) / rho; 3943a8779fbSJames Wright drhodx[j] += drho[k] * dXdx[k][j]; 3953a8779fbSJames Wright dEdx[j] += dE[k] * dXdx[k][j]; 396*493642f1SJames Wright for (CeedInt l=0; l<3; l++) { 3973a8779fbSJames Wright dUdx[j][k] += dU[j][l] * dXdx[l][k]; 3983a8779fbSJames Wright dXdxdXdxT[j][k] += dXdx[j][l]*dXdx[k][l]; //dXdx_j,k * dXdx_k,j 3993a8779fbSJames Wright } 4003a8779fbSJames Wright } 4013a8779fbSJames Wright } 4023a8779fbSJames Wright CeedScalar dudx[3][3] = {{0}}; 403*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 404*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 405*493642f1SJames Wright for (CeedInt l=0; l<3; l++) 4063a8779fbSJames Wright dudx[j][k] += du[j][l] * dXdx[l][k]; 4073a8779fbSJames Wright // -- grad_T 4083a8779fbSJames Wright const CeedScalar grad_T[3] = {(dEdx[0]/rho - E*drhodx[0]/(rho*rho) - /* *NOPAD* */ 409bb8a0c61SJames Wright (u[0]*dudx[0][0] + u[1]*dudx[1][0] + u[2]*dudx[2][0]) + g[0])/cv, 4103a8779fbSJames Wright (dEdx[1]/rho - E*drhodx[1]/(rho*rho) - /* *NOPAD* */ 411bb8a0c61SJames Wright (u[0]*dudx[0][1] + u[1]*dudx[1][1] + u[2]*dudx[2][1]) + g[1])/cv, 4123a8779fbSJames Wright (dEdx[2]/rho - E*drhodx[2]/(rho*rho) - /* *NOPAD* */ 413bb8a0c61SJames Wright (u[0]*dudx[0][2] + u[1]*dudx[1][2] + u[2]*dudx[2][2]) + g[2])/cv 4143a8779fbSJames Wright }; 4153a8779fbSJames Wright 4163a8779fbSJames Wright // -- Fuvisc 4173a8779fbSJames Wright // ---- Symmetric 3x3 matrix 4183a8779fbSJames Wright const CeedScalar Fu[6] = {mu*(dudx[0][0] * (2 + lambda) + /* *NOPAD* */ 4193a8779fbSJames Wright lambda * (dudx[1][1] + dudx[2][2])), 4203a8779fbSJames Wright mu*(dudx[0][1] + dudx[1][0]), /* *NOPAD* */ 4213a8779fbSJames Wright mu*(dudx[0][2] + dudx[2][0]), /* *NOPAD* */ 4223a8779fbSJames Wright mu*(dudx[1][1] * (2 + lambda) + /* *NOPAD* */ 4233a8779fbSJames Wright lambda * (dudx[0][0] + dudx[2][2])), 4243a8779fbSJames Wright mu*(dudx[1][2] + dudx[2][1]), /* *NOPAD* */ 4253a8779fbSJames Wright mu*(dudx[2][2] * (2 + lambda) + /* *NOPAD* */ 4263a8779fbSJames Wright lambda * (dudx[0][0] + dudx[1][1])) 4273a8779fbSJames Wright }; 4283a8779fbSJames Wright // -- Fevisc 4293a8779fbSJames Wright const CeedScalar Fe[3] = {u[0]*Fu[0] + u[1]*Fu[1] + u[2]*Fu[2] + /* *NOPAD* */ 4303a8779fbSJames Wright k*grad_T[0], /* *NOPAD* */ 4313a8779fbSJames Wright u[0]*Fu[1] + u[1]*Fu[3] + u[2]*Fu[4] + /* *NOPAD* */ 4323a8779fbSJames Wright k*grad_T[1], /* *NOPAD* */ 4333a8779fbSJames Wright u[0]*Fu[2] + u[1]*Fu[4] + u[2]*Fu[5] + /* *NOPAD* */ 4343a8779fbSJames Wright k*grad_T[2] /* *NOPAD* */ 4353a8779fbSJames Wright }; 4363a8779fbSJames Wright // Pressure 4373a8779fbSJames Wright const CeedScalar 4383a8779fbSJames Wright E_kinetic = 0.5 * rho * (u[0]*u[0] + u[1]*u[1] + u[2]*u[2]), 439bb8a0c61SJames Wright E_potential = -rho*(g[0]*x_i[0] + g[1]*x_i[1] + g[2]*x_i[2]), 4403a8779fbSJames Wright E_internal = E - E_kinetic - E_potential, 4413a8779fbSJames Wright P = E_internal * (gamma - 1.); // P = pressure 4423a8779fbSJames Wright 4433a8779fbSJames Wright // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction 4443a8779fbSJames Wright CeedScalar jacob_F_conv[3][5][5] = {{{0.}}}; 445bb8a0c61SJames Wright computeFluxJacobian_NS(jacob_F_conv, rho, u, E, gamma, g, x_i); 4463a8779fbSJames Wright 4473a8779fbSJames Wright // dqdx collects drhodx, dUdx and dEdx in one vector 4483a8779fbSJames Wright CeedScalar dqdx[5][3]; 449*493642f1SJames Wright for (CeedInt j=0; j<3; j++) { 4503a8779fbSJames Wright dqdx[0][j] = drhodx[j]; 4513a8779fbSJames Wright dqdx[4][j] = dEdx[j]; 452*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 4533a8779fbSJames Wright dqdx[k+1][j] = dUdx[k][j]; 4543a8779fbSJames Wright } 4553a8779fbSJames Wright 4563a8779fbSJames Wright // strong_conv = dF/dq * dq/dx (Strong convection) 4573a8779fbSJames Wright CeedScalar strong_conv[5] = {0}; 458*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 459*493642f1SJames Wright for (CeedInt k=0; k<5; k++) 460*493642f1SJames Wright for (CeedInt l=0; l<5; l++) 4613a8779fbSJames Wright strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j]; 4623a8779fbSJames Wright 4633a8779fbSJames Wright // Body force 464bb8a0c61SJames Wright const CeedScalar body_force[5] = {0, rho *g[0], rho *g[1], rho *g[2], 0}; 4653a8779fbSJames Wright 4663a8779fbSJames Wright // The Physics 4673a8779fbSJames Wright // Zero dv so all future terms can safely sum into it 468*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 469*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 4703a8779fbSJames Wright dv[k][j][i] = 0; 4713a8779fbSJames Wright 4723a8779fbSJames Wright // -- Density 4733a8779fbSJames Wright // ---- u rho 474*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 4753a8779fbSJames Wright dv[j][0][i] += wdetJ*(rho*u[0]*dXdx[j][0] + rho*u[1]*dXdx[j][1] + 4763a8779fbSJames Wright rho*u[2]*dXdx[j][2]); 4773a8779fbSJames Wright // -- Momentum 4783a8779fbSJames Wright // ---- rho (u x u) + P I3 479*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 480*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 4813a8779fbSJames Wright dv[k][j+1][i] += wdetJ*((rho*u[j]*u[0] + (j==0?P:0))*dXdx[k][0] + 4823a8779fbSJames Wright (rho*u[j]*u[1] + (j==1?P:0))*dXdx[k][1] + 4833a8779fbSJames Wright (rho*u[j]*u[2] + (j==2?P:0))*dXdx[k][2]); 4843a8779fbSJames Wright // ---- Fuvisc 4853a8779fbSJames Wright const CeedInt Fuviscidx[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}}; // symmetric matrix indices 486*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 487*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 4883a8779fbSJames Wright dv[k][j+1][i] -= wdetJ*(Fu[Fuviscidx[j][0]]*dXdx[k][0] + 4893a8779fbSJames Wright Fu[Fuviscidx[j][1]]*dXdx[k][1] + 4903a8779fbSJames Wright Fu[Fuviscidx[j][2]]*dXdx[k][2]); 4913a8779fbSJames Wright // -- Total Energy Density 4923a8779fbSJames Wright // ---- (E + P) u 493*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 4943a8779fbSJames Wright dv[j][4][i] += wdetJ * (E + P) * (u[0]*dXdx[j][0] + u[1]*dXdx[j][1] + 4953a8779fbSJames Wright u[2]*dXdx[j][2]); 4963a8779fbSJames Wright // ---- Fevisc 497*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 4983a8779fbSJames Wright dv[j][4][i] -= wdetJ * (Fe[0]*dXdx[j][0] + Fe[1]*dXdx[j][1] + 4993a8779fbSJames Wright Fe[2]*dXdx[j][2]); 5003a8779fbSJames Wright // Body Force 501*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 5023a8779fbSJames Wright v[j][i] = wdetJ * body_force[j]; 5033a8779fbSJames Wright 504bb8a0c61SJames Wright // Spatial Stabilization 505bb8a0c61SJames Wright // -- Not used in favor of diagonal tau. Kept for future testing 506bb8a0c61SJames Wright // const CeedScalar sound_speed = sqrt(gamma * P / rho); 507bb8a0c61SJames Wright // CeedScalar Tau_x[3] = {0.}; 508bb8a0c61SJames Wright // Tau_spatial(Tau_x, dXdx, u, sound_speed, context->c_tau, mu); 5093a8779fbSJames Wright 510bb8a0c61SJames Wright // -- Stabilization method: none, SU, or SUPG 511bb8a0c61SJames Wright CeedScalar stab[5][3] = {{0.}}; 512bb8a0c61SJames Wright CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0}; 513bb8a0c61SJames Wright CeedScalar Tau_d[3] = {0.}; 5143a8779fbSJames Wright switch (context->stabilization) { 5153a8779fbSJames Wright case STAB_NONE: // Galerkin 5163a8779fbSJames Wright break; 5173a8779fbSJames Wright case STAB_SU: // SU 518bb8a0c61SJames Wright Tau_diagPrim(Tau_d, dXdx, u, cv, context, mu, dt, rho); 519bb8a0c61SJames Wright tau_strong_conv[0] = Tau_d[0] * strong_conv[0]; 520bb8a0c61SJames Wright tau_strong_conv[1] = Tau_d[1] * strong_conv[1]; 521bb8a0c61SJames Wright tau_strong_conv[2] = Tau_d[1] * strong_conv[2]; 522bb8a0c61SJames Wright tau_strong_conv[3] = Tau_d[1] * strong_conv[3]; 523bb8a0c61SJames Wright tau_strong_conv[4] = Tau_d[2] * strong_conv[4]; 524bb8a0c61SJames Wright PrimitiveToConservative_fwd(rho, u, E, Rd, cv, tau_strong_conv, 525bb8a0c61SJames Wright tau_strong_conv_conservative); 526*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 527*493642f1SJames Wright for (CeedInt k=0; k<5; k++) 528*493642f1SJames Wright for (CeedInt l=0; l<5; l++) 529bb8a0c61SJames Wright stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l]; 5303a8779fbSJames Wright 531*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 532*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 5333a8779fbSJames Wright dv[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] + 5343a8779fbSJames Wright stab[j][1] * dXdx[k][1] + 5353a8779fbSJames Wright stab[j][2] * dXdx[k][2]); 5363a8779fbSJames Wright break; 5373a8779fbSJames Wright case STAB_SUPG: // SUPG is not implemented for explicit scheme 5383a8779fbSJames Wright break; 5393a8779fbSJames Wright } 5403a8779fbSJames Wright 5413a8779fbSJames Wright } // End Quadrature Point Loop 5423a8779fbSJames Wright 5433a8779fbSJames Wright // Return 5443a8779fbSJames Wright return 0; 5453a8779fbSJames Wright } 5463a8779fbSJames Wright 5473a8779fbSJames Wright // ***************************************************************************** 5483a8779fbSJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with 5493a8779fbSJames Wright // implicit time stepping method 5503a8779fbSJames Wright // 5513a8779fbSJames Wright // SU = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) ) 5523a8779fbSJames Wright // SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) ) 5533a8779fbSJames Wright // (diffussive terms will be added later) 5543a8779fbSJames Wright // 5553a8779fbSJames Wright // ***************************************************************************** 5563a8779fbSJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q, 5573a8779fbSJames Wright const CeedScalar *const *in, 5583a8779fbSJames Wright CeedScalar *const *out) { 5593a8779fbSJames Wright // *INDENT-OFF* 5603a8779fbSJames Wright // Inputs 5613a8779fbSJames Wright const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 5623a8779fbSJames Wright (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 5633a8779fbSJames Wright (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], 5643a8779fbSJames Wright (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3], 5653a8779fbSJames Wright (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4]; 5663a8779fbSJames Wright // Outputs 5673a8779fbSJames Wright CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], 5683a8779fbSJames Wright (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 5693a8779fbSJames Wright // *INDENT-ON* 5703a8779fbSJames Wright // Context 5713a8779fbSJames Wright NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx; 5723a8779fbSJames Wright const CeedScalar lambda = context->lambda; 5733a8779fbSJames Wright const CeedScalar mu = context->mu; 5743a8779fbSJames Wright const CeedScalar k = context->k; 5753a8779fbSJames Wright const CeedScalar cv = context->cv; 5763a8779fbSJames Wright const CeedScalar cp = context->cp; 577bb8a0c61SJames Wright const CeedScalar *g = context->g; 578bb8a0c61SJames Wright const CeedScalar dt = context->dt; 5793a8779fbSJames Wright const CeedScalar gamma = cp / cv; 580bb8a0c61SJames Wright const CeedScalar Rd = cp-cv; 5813a8779fbSJames Wright 5823a8779fbSJames Wright CeedPragmaSIMD 5833a8779fbSJames Wright // Quadrature Point Loop 5843a8779fbSJames Wright for (CeedInt i=0; i<Q; i++) { 5853a8779fbSJames Wright // Setup 5863a8779fbSJames Wright // -- Interp in 5873a8779fbSJames Wright const CeedScalar rho = q[0][i]; 5883a8779fbSJames Wright const CeedScalar u[3] = {q[1][i] / rho, 5893a8779fbSJames Wright q[2][i] / rho, 5903a8779fbSJames Wright q[3][i] / rho 5913a8779fbSJames Wright }; 5923a8779fbSJames Wright const CeedScalar E = q[4][i]; 5933a8779fbSJames Wright // -- Grad in 5943a8779fbSJames Wright const CeedScalar drho[3] = {dq[0][0][i], 5953a8779fbSJames Wright dq[1][0][i], 5963a8779fbSJames Wright dq[2][0][i] 5973a8779fbSJames Wright }; 5983a8779fbSJames Wright // *INDENT-OFF* 5993a8779fbSJames Wright const CeedScalar dU[3][3] = {{dq[0][1][i], 6003a8779fbSJames Wright dq[1][1][i], 6013a8779fbSJames Wright dq[2][1][i]}, 6023a8779fbSJames Wright {dq[0][2][i], 6033a8779fbSJames Wright dq[1][2][i], 6043a8779fbSJames Wright dq[2][2][i]}, 6053a8779fbSJames Wright {dq[0][3][i], 6063a8779fbSJames Wright dq[1][3][i], 6073a8779fbSJames Wright dq[2][3][i]} 6083a8779fbSJames Wright }; 6093a8779fbSJames Wright // *INDENT-ON* 6103a8779fbSJames Wright const CeedScalar dE[3] = {dq[0][4][i], 6113a8779fbSJames Wright dq[1][4][i], 6123a8779fbSJames Wright dq[2][4][i] 6133a8779fbSJames Wright }; 6143a8779fbSJames Wright // -- Interp-to-Interp q_data 6153a8779fbSJames Wright const CeedScalar wdetJ = q_data[0][i]; 6163a8779fbSJames Wright // -- Interp-to-Grad q_data 6173a8779fbSJames Wright // ---- Inverse of change of coordinate matrix: X_i,j 6183a8779fbSJames Wright // *INDENT-OFF* 6193a8779fbSJames Wright const CeedScalar dXdx[3][3] = {{q_data[1][i], 6203a8779fbSJames Wright q_data[2][i], 6213a8779fbSJames Wright q_data[3][i]}, 6223a8779fbSJames Wright {q_data[4][i], 6233a8779fbSJames Wright q_data[5][i], 6243a8779fbSJames Wright q_data[6][i]}, 6253a8779fbSJames Wright {q_data[7][i], 6263a8779fbSJames Wright q_data[8][i], 6273a8779fbSJames Wright q_data[9][i]} 6283a8779fbSJames Wright }; 629bb8a0c61SJames Wright const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]}; 6303a8779fbSJames Wright // *INDENT-ON* 6313a8779fbSJames Wright // -- Grad-to-Grad q_data 6323a8779fbSJames Wright // dU/dx 6333a8779fbSJames Wright CeedScalar du[3][3] = {{0}}; 6343a8779fbSJames Wright CeedScalar drhodx[3] = {0}; 6353a8779fbSJames Wright CeedScalar dEdx[3] = {0}; 6363a8779fbSJames Wright CeedScalar dUdx[3][3] = {{0}}; 6373a8779fbSJames Wright CeedScalar dXdxdXdxT[3][3] = {{0}}; 638*493642f1SJames Wright for (CeedInt j=0; j<3; j++) { 639*493642f1SJames Wright for (CeedInt k=0; k<3; k++) { 6403a8779fbSJames Wright du[j][k] = (dU[j][k] - drho[k]*u[j]) / rho; 6413a8779fbSJames Wright drhodx[j] += drho[k] * dXdx[k][j]; 6423a8779fbSJames Wright dEdx[j] += dE[k] * dXdx[k][j]; 643*493642f1SJames Wright for (CeedInt l=0; l<3; l++) { 6443a8779fbSJames Wright dUdx[j][k] += dU[j][l] * dXdx[l][k]; 6453a8779fbSJames Wright dXdxdXdxT[j][k] += dXdx[j][l]*dXdx[k][l]; //dXdx_j,k * dXdx_k,j 6463a8779fbSJames Wright } 6473a8779fbSJames Wright } 6483a8779fbSJames Wright } 6493a8779fbSJames Wright CeedScalar dudx[3][3] = {{0}}; 650*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 651*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 652*493642f1SJames Wright for (CeedInt l=0; l<3; l++) 6533a8779fbSJames Wright dudx[j][k] += du[j][l] * dXdx[l][k]; 6543a8779fbSJames Wright // -- grad_T 6553a8779fbSJames Wright const CeedScalar grad_T[3] = {(dEdx[0]/rho - E*drhodx[0]/(rho*rho) - /* *NOPAD* */ 656bb8a0c61SJames Wright (u[0]*dudx[0][0] + u[1]*dudx[1][0] + u[2]*dudx[2][0]) + g[0])/cv, 6573a8779fbSJames Wright (dEdx[1]/rho - E*drhodx[1]/(rho*rho) - /* *NOPAD* */ 658bb8a0c61SJames Wright (u[0]*dudx[0][1] + u[1]*dudx[1][1] + u[2]*dudx[2][1]) + g[1])/cv, 6593a8779fbSJames Wright (dEdx[2]/rho - E*drhodx[2]/(rho*rho) - /* *NOPAD* */ 660bb8a0c61SJames Wright (u[0]*dudx[0][2] + u[1]*dudx[1][2] + u[2]*dudx[2][2]) + g[2])/cv 6613a8779fbSJames Wright }; 6623a8779fbSJames Wright // -- Fuvisc 6633a8779fbSJames Wright // ---- Symmetric 3x3 matrix 6643a8779fbSJames Wright const CeedScalar Fu[6] = {mu*(dudx[0][0] * (2 + lambda) + /* *NOPAD* */ 6653a8779fbSJames Wright lambda * (dudx[1][1] + dudx[2][2])), 6663a8779fbSJames Wright mu*(dudx[0][1] + dudx[1][0]), /* *NOPAD* */ 6673a8779fbSJames Wright mu*(dudx[0][2] + dudx[2][0]), /* *NOPAD* */ 6683a8779fbSJames Wright mu*(dudx[1][1] * (2 + lambda) + /* *NOPAD* */ 6693a8779fbSJames Wright lambda * (dudx[0][0] + dudx[2][2])), 6703a8779fbSJames Wright mu*(dudx[1][2] + dudx[2][1]), /* *NOPAD* */ 6713a8779fbSJames Wright mu*(dudx[2][2] * (2 + lambda) + /* *NOPAD* */ 6723a8779fbSJames Wright lambda * (dudx[0][0] + dudx[1][1])) 6733a8779fbSJames Wright }; 6743a8779fbSJames Wright // -- Fevisc 6753a8779fbSJames Wright const CeedScalar Fe[3] = {u[0]*Fu[0] + u[1]*Fu[1] + u[2]*Fu[2] + /* *NOPAD* */ 6763a8779fbSJames Wright k*grad_T[0], /* *NOPAD* */ 6773a8779fbSJames Wright u[0]*Fu[1] + u[1]*Fu[3] + u[2]*Fu[4] + /* *NOPAD* */ 6783a8779fbSJames Wright k*grad_T[1], /* *NOPAD* */ 6793a8779fbSJames Wright u[0]*Fu[2] + u[1]*Fu[4] + u[2]*Fu[5] + /* *NOPAD* */ 6803a8779fbSJames Wright k*grad_T[2] /* *NOPAD* */ 6813a8779fbSJames Wright }; 6823a8779fbSJames Wright // Pressure 6833a8779fbSJames Wright const CeedScalar 6843a8779fbSJames Wright E_kinetic = 0.5 * rho * (u[0]*u[0] + u[1]*u[1] + u[2]*u[2]), 685bb8a0c61SJames Wright E_potential = -rho*(g[0]*x_i[0] + g[1]*x_i[1] + g[2]*x_i[2]), 6863a8779fbSJames Wright E_internal = E - E_kinetic - E_potential, 6873a8779fbSJames Wright P = E_internal * (gamma - 1.); // P = pressure 6883a8779fbSJames Wright 6893a8779fbSJames Wright // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction 6903a8779fbSJames Wright CeedScalar jacob_F_conv[3][5][5] = {{{0.}}}; 691bb8a0c61SJames Wright computeFluxJacobian_NS(jacob_F_conv, rho, u, E, gamma, g, x_i); 6923a8779fbSJames Wright 6933a8779fbSJames Wright // dqdx collects drhodx, dUdx and dEdx in one vector 6943a8779fbSJames Wright CeedScalar dqdx[5][3]; 695*493642f1SJames Wright for (CeedInt j=0; j<3; j++) { 6963a8779fbSJames Wright dqdx[0][j] = drhodx[j]; 6973a8779fbSJames Wright dqdx[4][j] = dEdx[j]; 698*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 6993a8779fbSJames Wright dqdx[k+1][j] = dUdx[k][j]; 7003a8779fbSJames Wright } 7013a8779fbSJames Wright // strong_conv = dF/dq * dq/dx (Strong convection) 7023a8779fbSJames Wright CeedScalar strong_conv[5] = {0}; 703*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 704*493642f1SJames Wright for (CeedInt k=0; k<5; k++) 705*493642f1SJames Wright for (CeedInt l=0; l<5; l++) 7063a8779fbSJames Wright strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j]; 7073a8779fbSJames Wright 7083a8779fbSJames Wright // Body force 709bb8a0c61SJames Wright const CeedScalar body_force[5] = {0, rho *g[0], rho *g[1], rho *g[2], 0}; 7103a8779fbSJames Wright 7113a8779fbSJames Wright // Strong residual 7123a8779fbSJames Wright CeedScalar strong_res[5]; 713*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 7143a8779fbSJames Wright strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j]; 7153a8779fbSJames Wright 7163a8779fbSJames Wright // The Physics 7173a8779fbSJames Wright //-----mass matrix 718*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 7193a8779fbSJames Wright v[j][i] = wdetJ*q_dot[j][i]; 7203a8779fbSJames Wright 7213a8779fbSJames Wright // Zero dv so all future terms can safely sum into it 722*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 723*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 7243a8779fbSJames Wright dv[k][j][i] = 0; 7253a8779fbSJames Wright 7263a8779fbSJames Wright // -- Density 7273a8779fbSJames Wright // ---- u rho 728*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 7293a8779fbSJames Wright dv[j][0][i] -= wdetJ*(rho*u[0]*dXdx[j][0] + rho*u[1]*dXdx[j][1] + 7303a8779fbSJames Wright rho*u[2]*dXdx[j][2]); 7313a8779fbSJames Wright // -- Momentum 7323a8779fbSJames Wright // ---- rho (u x u) + P I3 733*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 734*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 7353a8779fbSJames Wright dv[k][j+1][i] -= wdetJ*((rho*u[j]*u[0] + (j==0?P:0))*dXdx[k][0] + 7363a8779fbSJames Wright (rho*u[j]*u[1] + (j==1?P:0))*dXdx[k][1] + 7373a8779fbSJames Wright (rho*u[j]*u[2] + (j==2?P:0))*dXdx[k][2]); 7383a8779fbSJames Wright // ---- Fuvisc 7393a8779fbSJames Wright const CeedInt Fuviscidx[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}}; // symmetric matrix indices 740*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 741*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 7423a8779fbSJames Wright dv[k][j+1][i] += wdetJ*(Fu[Fuviscidx[j][0]]*dXdx[k][0] + 7433a8779fbSJames Wright Fu[Fuviscidx[j][1]]*dXdx[k][1] + 7443a8779fbSJames Wright Fu[Fuviscidx[j][2]]*dXdx[k][2]); 7453a8779fbSJames Wright // -- Total Energy Density 7463a8779fbSJames Wright // ---- (E + P) u 747*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 7483a8779fbSJames Wright dv[j][4][i] -= wdetJ * (E + P) * (u[0]*dXdx[j][0] + u[1]*dXdx[j][1] + 7493a8779fbSJames Wright u[2]*dXdx[j][2]); 7503a8779fbSJames Wright // ---- Fevisc 751*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 7523a8779fbSJames Wright dv[j][4][i] += wdetJ * (Fe[0]*dXdx[j][0] + Fe[1]*dXdx[j][1] + 7533a8779fbSJames Wright Fe[2]*dXdx[j][2]); 7543a8779fbSJames Wright // Body Force 755*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 7563a8779fbSJames Wright v[j][i] -= wdetJ*body_force[j]; 7573a8779fbSJames Wright 758bb8a0c61SJames Wright // Spatial Stabilization 759bb8a0c61SJames Wright // -- Not used in favor of diagonal tau. Kept for future testing 760bb8a0c61SJames Wright // const CeedScalar sound_speed = sqrt(gamma * P / rho); 761bb8a0c61SJames Wright // CeedScalar Tau_x[3] = {0.}; 762bb8a0c61SJames Wright // Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau, mu); 7633a8779fbSJames Wright 7643a8779fbSJames Wright // -- Stabilization method: none, SU, or SUPG 765bb8a0c61SJames Wright CeedScalar stab[5][3] = {{0.}}; 766bb8a0c61SJames Wright CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0}; 767bb8a0c61SJames Wright CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0}; 768bb8a0c61SJames Wright CeedScalar Tau_d[3] = {0.}; 7693a8779fbSJames Wright switch (context->stabilization) { 7703a8779fbSJames Wright case STAB_NONE: // Galerkin 7713a8779fbSJames Wright break; 7723a8779fbSJames Wright case STAB_SU: // SU 773bb8a0c61SJames Wright Tau_diagPrim(Tau_d, dXdx, u, cv, context, mu, dt, rho); 774bb8a0c61SJames Wright tau_strong_conv[0] = Tau_d[0] * strong_conv[0]; 775bb8a0c61SJames Wright tau_strong_conv[1] = Tau_d[1] * strong_conv[1]; 776bb8a0c61SJames Wright tau_strong_conv[2] = Tau_d[1] * strong_conv[2]; 777bb8a0c61SJames Wright tau_strong_conv[3] = Tau_d[1] * strong_conv[3]; 778bb8a0c61SJames Wright tau_strong_conv[4] = Tau_d[2] * strong_conv[4]; 779bb8a0c61SJames Wright PrimitiveToConservative_fwd(rho, u, E, Rd, cv, tau_strong_conv, 780bb8a0c61SJames Wright tau_strong_conv_conservative); 781*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 782*493642f1SJames Wright for (CeedInt k=0; k<5; k++) 783*493642f1SJames Wright for (CeedInt l=0; l<5; l++) 784bb8a0c61SJames Wright stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l]; 7853a8779fbSJames Wright 786*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 787*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 7883a8779fbSJames Wright dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] + 7893a8779fbSJames Wright stab[j][1] * dXdx[k][1] + 7903a8779fbSJames Wright stab[j][2] * dXdx[k][2]); 7913a8779fbSJames Wright break; 7923a8779fbSJames Wright case STAB_SUPG: // SUPG 793bb8a0c61SJames Wright Tau_diagPrim(Tau_d, dXdx, u, cv, context, mu, dt, rho); 794bb8a0c61SJames Wright tau_strong_res[0] = Tau_d[0] * strong_res[0]; 795bb8a0c61SJames Wright tau_strong_res[1] = Tau_d[1] * strong_res[1]; 796bb8a0c61SJames Wright tau_strong_res[2] = Tau_d[1] * strong_res[2]; 797bb8a0c61SJames Wright tau_strong_res[3] = Tau_d[1] * strong_res[3]; 798bb8a0c61SJames Wright tau_strong_res[4] = Tau_d[2] * strong_res[4]; 799bb8a0c61SJames Wright // Alternate route (useful later with primitive variable code) 800bb8a0c61SJames Wright // this function was verified against PHASTA for as IC that was as close as possible 801bb8a0c61SJames Wright // computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv); 802bb8a0c61SJames Wright // it has also been verified to compute a correct through the following 803bb8a0c61SJames Wright // stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive 804bb8a0c61SJames Wright // applied in the triple loop below 805bb8a0c61SJames Wright // However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz 806bb8a0c61SJames Wright PrimitiveToConservative_fwd(rho, u, E, Rd, cv, tau_strong_res, 807bb8a0c61SJames Wright tau_strong_res_conservative); 808*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 809*493642f1SJames Wright for (CeedInt k=0; k<5; k++) 810*493642f1SJames Wright for (CeedInt l=0; l<5; l++) 811bb8a0c61SJames Wright stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l]; 8123a8779fbSJames Wright 813*493642f1SJames Wright for (CeedInt j=0; j<5; j++) 814*493642f1SJames Wright for (CeedInt k=0; k<3; k++) 8153a8779fbSJames Wright dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] + 8163a8779fbSJames Wright stab[j][1] * dXdx[k][1] + 8173a8779fbSJames Wright stab[j][2] * dXdx[k][2]); 8183a8779fbSJames Wright break; 8193a8779fbSJames Wright } 8203a8779fbSJames Wright 8213a8779fbSJames Wright } // End Quadrature Point Loop 8223a8779fbSJames Wright 8233a8779fbSJames Wright // Return 8243a8779fbSJames Wright return 0; 8253a8779fbSJames Wright } 8263a8779fbSJames Wright // ***************************************************************************** 8273a8779fbSJames Wright #endif // newtonian_h 828