xref: /libCEED/examples/fluids/qfunctions/stabilization.h (revision c9c2c07970382857cc7b4a28d359710237b91a3e)
12b89d87eSLeila Ghaffari // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
22b89d87eSLeila Ghaffari // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
32b89d87eSLeila Ghaffari //
42b89d87eSLeila Ghaffari // SPDX-License-Identifier: BSD-2-Clause
52b89d87eSLeila Ghaffari //
62b89d87eSLeila Ghaffari // This file is part of CEED:  http://github.com/ceed
72b89d87eSLeila Ghaffari 
82b89d87eSLeila Ghaffari /// @file
92b89d87eSLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation
102b89d87eSLeila Ghaffari 
112b89d87eSLeila Ghaffari 
122b89d87eSLeila Ghaffari #ifndef stabilization_h
132b89d87eSLeila Ghaffari #define stabilization_h
142b89d87eSLeila Ghaffari 
152b89d87eSLeila Ghaffari #include <ceed.h>
16*c9c2c079SJeremy L Thompson #include "newtonian_state.h"
172b89d87eSLeila Ghaffari 
182b89d87eSLeila Ghaffari // *****************************************************************************
192b89d87eSLeila Ghaffari // Helper function for computing the variation in primitive variables,
202b89d87eSLeila Ghaffari //   given Tau_d
212b89d87eSLeila Ghaffari // *****************************************************************************
222b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3],
232b89d87eSLeila Ghaffari                                      CeedScalar dY[5]) {
242b89d87eSLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
252b89d87eSLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
262b89d87eSLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
272b89d87eSLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
282b89d87eSLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
292b89d87eSLeila Ghaffari }
302b89d87eSLeila Ghaffari 
312b89d87eSLeila Ghaffari // *****************************************************************************
322b89d87eSLeila Ghaffari // Helper functions for computing the stabilization terms
332b89d87eSLeila Ghaffari // *****************************************************************************
342b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas,
352b89d87eSLeila Ghaffari     State s, CeedScalar Tau_d[3], CeedScalar R[5], const CeedScalar x[3],
362b89d87eSLeila Ghaffari     CeedScalar stab[5][3]) {
372b89d87eSLeila Ghaffari   CeedScalar dY[5];
382b89d87eSLeila Ghaffari   const CeedScalar dx_i[3] = {0};
392b89d87eSLeila Ghaffari   StateConservative dF[3];
402b89d87eSLeila Ghaffari   // Zero stab so all future terms can safely sum into it
412b89d87eSLeila Ghaffari   for (CeedInt i=0; i<5; i++)
422b89d87eSLeila Ghaffari     for (CeedInt j=0; j<3; j++)
432b89d87eSLeila Ghaffari       stab[i][j] = 0;
442b89d87eSLeila Ghaffari   dYFromTau(R, Tau_d, dY);
452b89d87eSLeila Ghaffari   State ds = StateFromY_fwd(gas, s, dY, x, dx_i);
462b89d87eSLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
472b89d87eSLeila Ghaffari   for (CeedInt i=0; i<3; i++) {
482b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
492b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
502b89d87eSLeila Ghaffari     for (CeedInt j=0; j<5; j++)
512b89d87eSLeila Ghaffari       stab[j][i] += dF_i[j];
522b89d87eSLeila Ghaffari   }
532b89d87eSLeila Ghaffari }
542b89d87eSLeila Ghaffari 
552b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s,
562b89d87eSLeila Ghaffari     CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
572b89d87eSLeila Ghaffari     const CeedScalar body_force[5], const CeedScalar x[3], CeedScalar stab[5][3]) {
582b89d87eSLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
592b89d87eSLeila Ghaffari   CeedScalar R[5] = {0};
602b89d87eSLeila Ghaffari   switch (gas->stabilization) {
612b89d87eSLeila Ghaffari   case STAB_NONE:
622b89d87eSLeila Ghaffari     break;
632b89d87eSLeila Ghaffari   case STAB_SU:
642b89d87eSLeila Ghaffari     FluxInviscidStrong(gas, s, ds, R);
652b89d87eSLeila Ghaffari     break;
662b89d87eSLeila Ghaffari   case STAB_SUPG:
672b89d87eSLeila Ghaffari     FluxInviscidStrong(gas, s, ds, R);
682b89d87eSLeila Ghaffari     for (CeedInt j=0; j<5; j++) R[j] += U_dot[j] - body_force[j];
692b89d87eSLeila Ghaffari     break;
702b89d87eSLeila Ghaffari   }
712b89d87eSLeila Ghaffari   StabilizationMatrix(gas, s, Tau_d, R, x, stab);
722b89d87eSLeila Ghaffari }
732b89d87eSLeila Ghaffari 
742b89d87eSLeila Ghaffari // *****************************************************************************
752b89d87eSLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
762b89d87eSLeila Ghaffari //   Model from:
772b89d87eSLeila Ghaffari //     PHASTA
782b89d87eSLeila Ghaffari //
792b89d87eSLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
802b89d87eSLeila Ghaffari //
812b89d87eSLeila Ghaffari // *****************************************************************************
822b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s,
832b89d87eSLeila Ghaffari                                         const CeedScalar dXdx[3][3],
842b89d87eSLeila Ghaffari                                         const CeedScalar dt, CeedScalar Tau_d[3]) {
852b89d87eSLeila Ghaffari   // Context
862b89d87eSLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
872b89d87eSLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
882b89d87eSLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
892b89d87eSLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
902b89d87eSLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
912b89d87eSLeila Ghaffari   const CeedScalar cv = gas->cv;
922b89d87eSLeila Ghaffari   const CeedScalar mu = gas->mu;
932b89d87eSLeila Ghaffari   const CeedScalar u[3] = {s.Y.velocity[0], s.Y.velocity[1], s.Y.velocity[2]};
942b89d87eSLeila Ghaffari   const CeedScalar rho = s.U.density;
952b89d87eSLeila Ghaffari 
962b89d87eSLeila Ghaffari   CeedScalar gijd[6];
972b89d87eSLeila Ghaffari   CeedScalar tau;
982b89d87eSLeila Ghaffari   CeedScalar dts;
992b89d87eSLeila Ghaffari   CeedScalar fact;
1002b89d87eSLeila Ghaffari 
1012b89d87eSLeila Ghaffari   //*INDENT-OFF*
1022b89d87eSLeila Ghaffari   gijd[0] =   dXdx[0][0] * dXdx[0][0]
1032b89d87eSLeila Ghaffari             + dXdx[1][0] * dXdx[1][0]
1042b89d87eSLeila Ghaffari             + dXdx[2][0] * dXdx[2][0];
1052b89d87eSLeila Ghaffari 
1062b89d87eSLeila Ghaffari   gijd[1] =   dXdx[0][0] * dXdx[0][1]
1072b89d87eSLeila Ghaffari             + dXdx[1][0] * dXdx[1][1]
1082b89d87eSLeila Ghaffari             + dXdx[2][0] * dXdx[2][1];
1092b89d87eSLeila Ghaffari 
1102b89d87eSLeila Ghaffari   gijd[2] =   dXdx[0][1] * dXdx[0][1]
1112b89d87eSLeila Ghaffari             + dXdx[1][1] * dXdx[1][1]
1122b89d87eSLeila Ghaffari             + dXdx[2][1] * dXdx[2][1];
1132b89d87eSLeila Ghaffari 
1142b89d87eSLeila Ghaffari   gijd[3] =   dXdx[0][0] * dXdx[0][2]
1152b89d87eSLeila Ghaffari             + dXdx[1][0] * dXdx[1][2]
1162b89d87eSLeila Ghaffari             + dXdx[2][0] * dXdx[2][2];
1172b89d87eSLeila Ghaffari 
1182b89d87eSLeila Ghaffari   gijd[4] =   dXdx[0][1] * dXdx[0][2]
1192b89d87eSLeila Ghaffari             + dXdx[1][1] * dXdx[1][2]
1202b89d87eSLeila Ghaffari             + dXdx[2][1] * dXdx[2][2];
1212b89d87eSLeila Ghaffari 
1222b89d87eSLeila Ghaffari   gijd[5] =   dXdx[0][2] * dXdx[0][2]
1232b89d87eSLeila Ghaffari             + dXdx[1][2] * dXdx[1][2]
1242b89d87eSLeila Ghaffari             + dXdx[2][2] * dXdx[2][2];
1252b89d87eSLeila Ghaffari   //*INDENT-ON*
1262b89d87eSLeila Ghaffari 
1272b89d87eSLeila Ghaffari   dts = Ctau_t / dt ;
1282b89d87eSLeila Ghaffari 
1292b89d87eSLeila Ghaffari   tau = rho*rho*((4. * dts * dts)
1302b89d87eSLeila Ghaffari                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
1312b89d87eSLeila Ghaffari                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
1322b89d87eSLeila Ghaffari                  + u[2] *   u[2] * gijd[5])
1332b89d87eSLeila Ghaffari         + Ctau_v* mu * mu *
1342b89d87eSLeila Ghaffari         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
1352b89d87eSLeila Ghaffari          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
1362b89d87eSLeila Ghaffari 
1372b89d87eSLeila Ghaffari   fact = sqrt(tau);
1382b89d87eSLeila Ghaffari 
1392b89d87eSLeila Ghaffari   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
1402b89d87eSLeila Ghaffari 
1412b89d87eSLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
1422b89d87eSLeila Ghaffari   Tau_d[2] = Ctau_E / ( fact * cv );
1432b89d87eSLeila Ghaffari 
1442b89d87eSLeila Ghaffari   // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
1452b89d87eSLeila Ghaffari   //  to avoid a division if the compiler is smart enough to see that cv IS
1462b89d87eSLeila Ghaffari   // a constant that it could invert once for all elements
1472b89d87eSLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
1482b89d87eSLeila Ghaffari   // OR we could absorb cv into Ctau_E but this puts more burden on user to
1492b89d87eSLeila Ghaffari   // know how to change constants with a change of fluid or units.  Same for
1502b89d87eSLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
1512b89d87eSLeila Ghaffari }
1522b89d87eSLeila Ghaffari 
1532b89d87eSLeila Ghaffari // *****************************************************************************
1542b89d87eSLeila Ghaffari 
1552b89d87eSLeila Ghaffari #endif // stabilization_h
156