xref: /libCEED/examples/fluids/qfunctions/stabilization.h (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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 #ifndef stabilization_h
122b89d87eSLeila Ghaffari #define stabilization_h
132b89d87eSLeila Ghaffari 
142b89d87eSLeila Ghaffari #include <ceed.h>
15*2b730f8bSJeremy L Thompson 
16c9c2c079SJeremy 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 // *****************************************************************************
22*2b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], CeedScalar dY[5]) {
232b89d87eSLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
242b89d87eSLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
252b89d87eSLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
262b89d87eSLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
272b89d87eSLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
282b89d87eSLeila Ghaffari }
292b89d87eSLeila Ghaffari 
302b89d87eSLeila Ghaffari // *****************************************************************************
312b89d87eSLeila Ghaffari // Helper functions for computing the stabilization terms
322b89d87eSLeila Ghaffari // *****************************************************************************
33*2b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar R[5], const CeedScalar x[3],
342b89d87eSLeila Ghaffari                                                CeedScalar stab[5][3]) {
352b89d87eSLeila Ghaffari   CeedScalar        dY[5];
362b89d87eSLeila Ghaffari   const CeedScalar  dx_i[3] = {0};
372b89d87eSLeila Ghaffari   StateConservative dF[3];
382b89d87eSLeila Ghaffari   // Zero stab so all future terms can safely sum into it
39*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 5; i++) {
40*2b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
41*2b730f8bSJeremy L Thompson   }
422b89d87eSLeila Ghaffari   dYFromTau(R, Tau_d, dY);
432b89d87eSLeila Ghaffari   State ds = StateFromY_fwd(gas, s, dY, x, dx_i);
442b89d87eSLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
452b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
462b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
472b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
48*2b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
492b89d87eSLeila Ghaffari   }
502b89d87eSLeila Ghaffari }
512b89d87eSLeila Ghaffari 
52*2b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
532b89d87eSLeila Ghaffari                                          const CeedScalar body_force[5], const CeedScalar x[3], CeedScalar stab[5][3]) {
542b89d87eSLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
552b89d87eSLeila Ghaffari   CeedScalar R[5] = {0};
562b89d87eSLeila Ghaffari   switch (gas->stabilization) {
572b89d87eSLeila Ghaffari     case STAB_NONE:
582b89d87eSLeila Ghaffari       break;
592b89d87eSLeila Ghaffari     case STAB_SU:
602b89d87eSLeila Ghaffari       FluxInviscidStrong(gas, s, ds, R);
612b89d87eSLeila Ghaffari       break;
622b89d87eSLeila Ghaffari     case STAB_SUPG:
632b89d87eSLeila Ghaffari       FluxInviscidStrong(gas, s, ds, R);
642b89d87eSLeila Ghaffari       for (CeedInt j = 0; j < 5; j++) R[j] += U_dot[j] - body_force[j];
652b89d87eSLeila Ghaffari       break;
662b89d87eSLeila Ghaffari   }
672b89d87eSLeila Ghaffari   StabilizationMatrix(gas, s, Tau_d, R, x, stab);
682b89d87eSLeila Ghaffari }
692b89d87eSLeila Ghaffari 
702b89d87eSLeila Ghaffari // *****************************************************************************
712b89d87eSLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
722b89d87eSLeila Ghaffari //   Model from:
732b89d87eSLeila Ghaffari //     PHASTA
742b89d87eSLeila Ghaffari //
752b89d87eSLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
762b89d87eSLeila Ghaffari //
772b89d87eSLeila Ghaffari // *****************************************************************************
78*2b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
79*2b730f8bSJeremy L Thompson                                         CeedScalar Tau_d[3]) {
802b89d87eSLeila Ghaffari   // Context
812b89d87eSLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
822b89d87eSLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
832b89d87eSLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
842b89d87eSLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
852b89d87eSLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
862b89d87eSLeila Ghaffari   const CeedScalar cv     = gas->cv;
872b89d87eSLeila Ghaffari   const CeedScalar mu     = gas->mu;
882b89d87eSLeila Ghaffari   const CeedScalar u[3]   = {s.Y.velocity[0], s.Y.velocity[1], s.Y.velocity[2]};
892b89d87eSLeila Ghaffari   const CeedScalar rho    = s.U.density;
902b89d87eSLeila Ghaffari 
912b89d87eSLeila Ghaffari   CeedScalar gijd[6];
922b89d87eSLeila Ghaffari   CeedScalar tau;
932b89d87eSLeila Ghaffari   CeedScalar dts;
942b89d87eSLeila Ghaffari   CeedScalar fact;
952b89d87eSLeila Ghaffari 
962b89d87eSLeila Ghaffari   //*INDENT-OFF*
97*2b730f8bSJeremy L Thompson   gijd[0] = dXdx[0][0] * dXdx[0][0] + dXdx[1][0] * dXdx[1][0] + dXdx[2][0] * dXdx[2][0];
982b89d87eSLeila Ghaffari 
99*2b730f8bSJeremy L Thompson   gijd[1] = dXdx[0][0] * dXdx[0][1] + dXdx[1][0] * dXdx[1][1] + dXdx[2][0] * dXdx[2][1];
1002b89d87eSLeila Ghaffari 
101*2b730f8bSJeremy L Thompson   gijd[2] = dXdx[0][1] * dXdx[0][1] + dXdx[1][1] * dXdx[1][1] + dXdx[2][1] * dXdx[2][1];
1022b89d87eSLeila Ghaffari 
103*2b730f8bSJeremy L Thompson   gijd[3] = dXdx[0][0] * dXdx[0][2] + dXdx[1][0] * dXdx[1][2] + dXdx[2][0] * dXdx[2][2];
1042b89d87eSLeila Ghaffari 
105*2b730f8bSJeremy L Thompson   gijd[4] = dXdx[0][1] * dXdx[0][2] + dXdx[1][1] * dXdx[1][2] + dXdx[2][1] * dXdx[2][2];
1062b89d87eSLeila Ghaffari 
107*2b730f8bSJeremy L Thompson   gijd[5] = dXdx[0][2] * dXdx[0][2] + dXdx[1][2] * dXdx[1][2] + dXdx[2][2] * dXdx[2][2];
1082b89d87eSLeila Ghaffari   //*INDENT-ON*
1092b89d87eSLeila Ghaffari 
1102b89d87eSLeila Ghaffari   dts = Ctau_t / dt;
1112b89d87eSLeila Ghaffari 
112*2b730f8bSJeremy L Thompson   tau = rho * rho *
113*2b730f8bSJeremy L Thompson             ((4. * dts * dts) + u[0] * (u[0] * gijd[0] + 2. * (u[1] * gijd[1] + u[2] * gijd[3])) + u[1] * (u[1] * gijd[2] + 2. * u[2] * gijd[4]) +
114*2b730f8bSJeremy L Thompson              u[2] * u[2] * gijd[5]) +
115*2b730f8bSJeremy L Thompson         Ctau_v * mu * mu *
116*2b730f8bSJeremy L Thompson             (gijd[0] * gijd[0] + gijd[2] * gijd[2] + gijd[5] * gijd[5] + +2. * (gijd[1] * gijd[1] + gijd[3] * gijd[3] + gijd[4] * gijd[4]));
1172b89d87eSLeila Ghaffari 
1182b89d87eSLeila Ghaffari   fact = sqrt(tau);
1192b89d87eSLeila Ghaffari 
1202b89d87eSLeila Ghaffari   Tau_d[0] = Ctau_C * fact / (rho * (gijd[0] + gijd[2] + gijd[5])) * 0.125;
1212b89d87eSLeila Ghaffari 
1222b89d87eSLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
1232b89d87eSLeila Ghaffari   Tau_d[2] = Ctau_E / (fact * cv);
1242b89d87eSLeila Ghaffari 
1252b89d87eSLeila Ghaffari   // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
1262b89d87eSLeila Ghaffari   //  to avoid a division if the compiler is smart enough to see that cv IS
1272b89d87eSLeila Ghaffari   // a constant that it could invert once for all elements
1282b89d87eSLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
1292b89d87eSLeila Ghaffari   // OR we could absorb cv into Ctau_E but this puts more burden on user to
1302b89d87eSLeila Ghaffari   // know how to change constants with a change of fluid or units.  Same for
1312b89d87eSLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
1322b89d87eSLeila Ghaffari }
1332b89d87eSLeila Ghaffari 
1342b89d87eSLeila Ghaffari // *****************************************************************************
1352b89d87eSLeila Ghaffari 
1362b89d87eSLeila Ghaffari #endif  // stabilization_h
137