1*2b89d87eSLeila Ghaffari // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2*2b89d87eSLeila Ghaffari // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3*2b89d87eSLeila Ghaffari // 4*2b89d87eSLeila Ghaffari // SPDX-License-Identifier: BSD-2-Clause 5*2b89d87eSLeila Ghaffari // 6*2b89d87eSLeila Ghaffari // This file is part of CEED: http://github.com/ceed 7*2b89d87eSLeila Ghaffari 8*2b89d87eSLeila Ghaffari /// @file 9*2b89d87eSLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation 10*2b89d87eSLeila Ghaffari 11*2b89d87eSLeila Ghaffari 12*2b89d87eSLeila Ghaffari #ifndef stabilization_h 13*2b89d87eSLeila Ghaffari #define stabilization_h 14*2b89d87eSLeila Ghaffari 15*2b89d87eSLeila Ghaffari #include "newtonian_state.h" 16*2b89d87eSLeila Ghaffari #include <ceed.h> 17*2b89d87eSLeila Ghaffari 18*2b89d87eSLeila Ghaffari // ***************************************************************************** 19*2b89d87eSLeila Ghaffari // Helper function for computing the variation in primitive variables, 20*2b89d87eSLeila Ghaffari // given Tau_d 21*2b89d87eSLeila Ghaffari // ***************************************************************************** 22*2b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], 23*2b89d87eSLeila Ghaffari CeedScalar dY[5]) { 24*2b89d87eSLeila Ghaffari dY[0] = Tau_d[0] * Y[0]; 25*2b89d87eSLeila Ghaffari dY[1] = Tau_d[1] * Y[1]; 26*2b89d87eSLeila Ghaffari dY[2] = Tau_d[1] * Y[2]; 27*2b89d87eSLeila Ghaffari dY[3] = Tau_d[1] * Y[3]; 28*2b89d87eSLeila Ghaffari dY[4] = Tau_d[2] * Y[4]; 29*2b89d87eSLeila Ghaffari } 30*2b89d87eSLeila Ghaffari 31*2b89d87eSLeila Ghaffari // ***************************************************************************** 32*2b89d87eSLeila Ghaffari // Helper functions for computing the stabilization terms 33*2b89d87eSLeila Ghaffari // ***************************************************************************** 34*2b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, 35*2b89d87eSLeila Ghaffari State s, CeedScalar Tau_d[3], CeedScalar R[5], const CeedScalar x[3], 36*2b89d87eSLeila Ghaffari CeedScalar stab[5][3]) { 37*2b89d87eSLeila Ghaffari CeedScalar dY[5]; 38*2b89d87eSLeila Ghaffari const CeedScalar dx_i[3] = {0}; 39*2b89d87eSLeila Ghaffari StateConservative dF[3]; 40*2b89d87eSLeila Ghaffari // Zero stab so all future terms can safely sum into it 41*2b89d87eSLeila Ghaffari for (CeedInt i=0; i<5; i++) 42*2b89d87eSLeila Ghaffari for (CeedInt j=0; j<3; j++) 43*2b89d87eSLeila Ghaffari stab[i][j] = 0; 44*2b89d87eSLeila Ghaffari dYFromTau(R, Tau_d, dY); 45*2b89d87eSLeila Ghaffari State ds = StateFromY_fwd(gas, s, dY, x, dx_i); 46*2b89d87eSLeila Ghaffari FluxInviscid_fwd(gas, s, ds, dF); 47*2b89d87eSLeila Ghaffari for (CeedInt i=0; i<3; i++) { 48*2b89d87eSLeila Ghaffari CeedScalar dF_i[5]; 49*2b89d87eSLeila Ghaffari UnpackState_U(dF[i], dF_i); 50*2b89d87eSLeila Ghaffari for (CeedInt j=0; j<5; j++) 51*2b89d87eSLeila Ghaffari stab[j][i] += dF_i[j]; 52*2b89d87eSLeila Ghaffari } 53*2b89d87eSLeila Ghaffari } 54*2b89d87eSLeila Ghaffari 55*2b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, 56*2b89d87eSLeila Ghaffari CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5], 57*2b89d87eSLeila Ghaffari const CeedScalar body_force[5], const CeedScalar x[3], CeedScalar stab[5][3]) { 58*2b89d87eSLeila Ghaffari // -- Stabilization method: none (Galerkin), SU, or SUPG 59*2b89d87eSLeila Ghaffari CeedScalar R[5] = {0}; 60*2b89d87eSLeila Ghaffari switch (gas->stabilization) { 61*2b89d87eSLeila Ghaffari case STAB_NONE: 62*2b89d87eSLeila Ghaffari break; 63*2b89d87eSLeila Ghaffari case STAB_SU: 64*2b89d87eSLeila Ghaffari FluxInviscidStrong(gas, s, ds, R); 65*2b89d87eSLeila Ghaffari break; 66*2b89d87eSLeila Ghaffari case STAB_SUPG: 67*2b89d87eSLeila Ghaffari FluxInviscidStrong(gas, s, ds, R); 68*2b89d87eSLeila Ghaffari for (CeedInt j=0; j<5; j++) R[j] += U_dot[j] - body_force[j]; 69*2b89d87eSLeila Ghaffari break; 70*2b89d87eSLeila Ghaffari } 71*2b89d87eSLeila Ghaffari StabilizationMatrix(gas, s, Tau_d, R, x, stab); 72*2b89d87eSLeila Ghaffari } 73*2b89d87eSLeila Ghaffari 74*2b89d87eSLeila Ghaffari // ***************************************************************************** 75*2b89d87eSLeila Ghaffari // Helper function for computing Tau elements (stabilization constant) 76*2b89d87eSLeila Ghaffari // Model from: 77*2b89d87eSLeila Ghaffari // PHASTA 78*2b89d87eSLeila Ghaffari // 79*2b89d87eSLeila Ghaffari // Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial) 80*2b89d87eSLeila Ghaffari // 81*2b89d87eSLeila Ghaffari // ***************************************************************************** 82*2b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, 83*2b89d87eSLeila Ghaffari const CeedScalar dXdx[3][3], 84*2b89d87eSLeila Ghaffari const CeedScalar dt, CeedScalar Tau_d[3]) { 85*2b89d87eSLeila Ghaffari // Context 86*2b89d87eSLeila Ghaffari const CeedScalar Ctau_t = gas->Ctau_t; 87*2b89d87eSLeila Ghaffari const CeedScalar Ctau_v = gas->Ctau_v; 88*2b89d87eSLeila Ghaffari const CeedScalar Ctau_C = gas->Ctau_C; 89*2b89d87eSLeila Ghaffari const CeedScalar Ctau_M = gas->Ctau_M; 90*2b89d87eSLeila Ghaffari const CeedScalar Ctau_E = gas->Ctau_E; 91*2b89d87eSLeila Ghaffari const CeedScalar cv = gas->cv; 92*2b89d87eSLeila Ghaffari const CeedScalar mu = gas->mu; 93*2b89d87eSLeila Ghaffari const CeedScalar u[3] = {s.Y.velocity[0], s.Y.velocity[1], s.Y.velocity[2]}; 94*2b89d87eSLeila Ghaffari const CeedScalar rho = s.U.density; 95*2b89d87eSLeila Ghaffari 96*2b89d87eSLeila Ghaffari CeedScalar gijd[6]; 97*2b89d87eSLeila Ghaffari CeedScalar tau; 98*2b89d87eSLeila Ghaffari CeedScalar dts; 99*2b89d87eSLeila Ghaffari CeedScalar fact; 100*2b89d87eSLeila Ghaffari 101*2b89d87eSLeila Ghaffari //*INDENT-OFF* 102*2b89d87eSLeila Ghaffari gijd[0] = dXdx[0][0] * dXdx[0][0] 103*2b89d87eSLeila Ghaffari + dXdx[1][0] * dXdx[1][0] 104*2b89d87eSLeila Ghaffari + dXdx[2][0] * dXdx[2][0]; 105*2b89d87eSLeila Ghaffari 106*2b89d87eSLeila Ghaffari gijd[1] = dXdx[0][0] * dXdx[0][1] 107*2b89d87eSLeila Ghaffari + dXdx[1][0] * dXdx[1][1] 108*2b89d87eSLeila Ghaffari + dXdx[2][0] * dXdx[2][1]; 109*2b89d87eSLeila Ghaffari 110*2b89d87eSLeila Ghaffari gijd[2] = dXdx[0][1] * dXdx[0][1] 111*2b89d87eSLeila Ghaffari + dXdx[1][1] * dXdx[1][1] 112*2b89d87eSLeila Ghaffari + dXdx[2][1] * dXdx[2][1]; 113*2b89d87eSLeila Ghaffari 114*2b89d87eSLeila Ghaffari gijd[3] = dXdx[0][0] * dXdx[0][2] 115*2b89d87eSLeila Ghaffari + dXdx[1][0] * dXdx[1][2] 116*2b89d87eSLeila Ghaffari + dXdx[2][0] * dXdx[2][2]; 117*2b89d87eSLeila Ghaffari 118*2b89d87eSLeila Ghaffari gijd[4] = dXdx[0][1] * dXdx[0][2] 119*2b89d87eSLeila Ghaffari + dXdx[1][1] * dXdx[1][2] 120*2b89d87eSLeila Ghaffari + dXdx[2][1] * dXdx[2][2]; 121*2b89d87eSLeila Ghaffari 122*2b89d87eSLeila Ghaffari gijd[5] = dXdx[0][2] * dXdx[0][2] 123*2b89d87eSLeila Ghaffari + dXdx[1][2] * dXdx[1][2] 124*2b89d87eSLeila Ghaffari + dXdx[2][2] * dXdx[2][2]; 125*2b89d87eSLeila Ghaffari //*INDENT-ON* 126*2b89d87eSLeila Ghaffari 127*2b89d87eSLeila Ghaffari dts = Ctau_t / dt ; 128*2b89d87eSLeila Ghaffari 129*2b89d87eSLeila Ghaffari tau = rho*rho*((4. * dts * dts) 130*2b89d87eSLeila Ghaffari + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3])) 131*2b89d87eSLeila Ghaffari + u[1] * ( u[1] * gijd[2] + 2. * u[2] * gijd[4]) 132*2b89d87eSLeila Ghaffari + u[2] * u[2] * gijd[5]) 133*2b89d87eSLeila Ghaffari + Ctau_v* mu * mu * 134*2b89d87eSLeila Ghaffari (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] + 135*2b89d87eSLeila Ghaffari + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4])); 136*2b89d87eSLeila Ghaffari 137*2b89d87eSLeila Ghaffari fact = sqrt(tau); 138*2b89d87eSLeila Ghaffari 139*2b89d87eSLeila Ghaffari Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125; 140*2b89d87eSLeila Ghaffari 141*2b89d87eSLeila Ghaffari Tau_d[1] = Ctau_M / fact; 142*2b89d87eSLeila Ghaffari Tau_d[2] = Ctau_E / ( fact * cv ); 143*2b89d87eSLeila Ghaffari 144*2b89d87eSLeila Ghaffari // consider putting back the way I initially had it Ctau_E * Tau_d[1] /cv 145*2b89d87eSLeila Ghaffari // to avoid a division if the compiler is smart enough to see that cv IS 146*2b89d87eSLeila Ghaffari // a constant that it could invert once for all elements 147*2b89d87eSLeila Ghaffari // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M 148*2b89d87eSLeila Ghaffari // OR we could absorb cv into Ctau_E but this puts more burden on user to 149*2b89d87eSLeila Ghaffari // know how to change constants with a change of fluid or units. Same for 150*2b89d87eSLeila Ghaffari // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T) 151*2b89d87eSLeila Ghaffari } 152*2b89d87eSLeila Ghaffari 153*2b89d87eSLeila Ghaffari // ***************************************************************************** 154*2b89d87eSLeila Ghaffari 155*2b89d87eSLeila Ghaffari #endif // stabilization_h 156