xref: /libCEED/examples/fluids/qfunctions/stabilization.h (revision c0b5abf0f23b15c4f0ada76f8abe9f8d2b6fa247)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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
10*c0b5abf0SJeremy L Thompson #include <ceed/types.h>
112b730f8bSJeremy L Thompson 
12c9c2c079SJeremy L Thompson #include "newtonian_state.h"
132b89d87eSLeila Ghaffari 
142b89d87eSLeila Ghaffari // *****************************************************************************
15ea61e9acSJeremy L Thompson // Helper function for computing the variation in primitive variables, given Tau_d
162b89d87eSLeila Ghaffari // *****************************************************************************
172b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], CeedScalar dY[5]) {
182b89d87eSLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
192b89d87eSLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
202b89d87eSLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
212b89d87eSLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
222b89d87eSLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
232b89d87eSLeila Ghaffari }
242b89d87eSLeila Ghaffari 
252b89d87eSLeila Ghaffari // *****************************************************************************
262b89d87eSLeila Ghaffari // Helper functions for computing the stabilization terms
272b89d87eSLeila Ghaffari // *****************************************************************************
288c3b32bfSJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar strong_residual[5],
298c3b32bfSJames Wright                                                CeedScalar stab[5][3]) {
302b89d87eSLeila Ghaffari   CeedScalar        dY[5];
312b89d87eSLeila Ghaffari   StateConservative dF[3];
322b89d87eSLeila Ghaffari   // Zero stab so all future terms can safely sum into it
332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 5; i++) {
342b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
352b730f8bSJeremy L Thompson   }
368c3b32bfSJames Wright   dYFromTau(strong_residual, Tau_d, dY);
373bd61617SKenneth E. Jansen   State ds = StateFromY_fwd(gas, s, dY);
382b89d87eSLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
392b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
402b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
412b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
422b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
432b89d87eSLeila Ghaffari   }
442b89d87eSLeila Ghaffari }
452b89d87eSLeila Ghaffari 
462b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
473bd61617SKenneth E. Jansen                                          const CeedScalar body_force[5], CeedScalar stab[5][3]) {
482b89d87eSLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
498c3b32bfSJames Wright   CeedScalar strong_residual[5] = {0};
502b89d87eSLeila Ghaffari   switch (gas->stabilization) {
512b89d87eSLeila Ghaffari     case STAB_NONE:
522b89d87eSLeila Ghaffari       break;
532b89d87eSLeila Ghaffari     case STAB_SU:
548c3b32bfSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
552b89d87eSLeila Ghaffari       break;
562b89d87eSLeila Ghaffari     case STAB_SUPG:
578c3b32bfSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
588c3b32bfSJames Wright       for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j];
592b89d87eSLeila Ghaffari       break;
602b89d87eSLeila Ghaffari   }
618c3b32bfSJames Wright   StabilizationMatrix(gas, s, Tau_d, strong_residual, stab);
622b89d87eSLeila Ghaffari }
632b89d87eSLeila Ghaffari 
642b89d87eSLeila Ghaffari // *****************************************************************************
652b89d87eSLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
662b89d87eSLeila Ghaffari //   Model from:
672b89d87eSLeila Ghaffari //     PHASTA
682b89d87eSLeila Ghaffari //
692b89d87eSLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
702b89d87eSLeila Ghaffari // *****************************************************************************
712b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
722b730f8bSJeremy L Thompson                                         CeedScalar Tau_d[3]) {
732b89d87eSLeila Ghaffari   // Context
742b89d87eSLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
752b89d87eSLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
762b89d87eSLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
772b89d87eSLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
782b89d87eSLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
792b89d87eSLeila Ghaffari   const CeedScalar cv     = gas->cv;
802b89d87eSLeila Ghaffari   const CeedScalar mu     = gas->mu;
812b89d87eSLeila Ghaffari   const CeedScalar rho    = s.U.density;
822b89d87eSLeila Ghaffari 
832b89d87eSLeila Ghaffari   CeedScalar tau;
842b89d87eSLeila Ghaffari   CeedScalar dts;
852b89d87eSLeila Ghaffari   CeedScalar fact;
862b89d87eSLeila Ghaffari 
87b9b033b3SJames Wright   CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term;
88b9b033b3SJames Wright   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
892b89d87eSLeila Ghaffari 
902b89d87eSLeila Ghaffari   dts = Ctau_t / dt;
912b89d87eSLeila Ghaffari 
92b9b033b3SJames Wright   {  // u_i g_ij u_j
93b9b033b3SJames Wright     CeedScalar gij_uj[3] = {0.};
94b9b033b3SJames Wright     MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj);
95b9b033b3SJames Wright     velocity_term = Dot3(s.Y.velocity, gij_uj);
96b9b033b3SJames Wright   }
97b9b033b3SJames Wright 
98b9b033b3SJames Wright   tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9);
992b89d87eSLeila Ghaffari 
1002b89d87eSLeila Ghaffari   fact = sqrt(tau);
1012b89d87eSLeila Ghaffari 
102b9b033b3SJames Wright   Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125;
1032b89d87eSLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
1042b89d87eSLeila Ghaffari   Tau_d[2] = Ctau_E / (fact * cv);
1052b89d87eSLeila Ghaffari 
106ea61e9acSJeremy L Thompson   // consider putting back the way I initially had it
107ea61e9acSJeremy L Thompson   // Ctau_E * Tau_d[1] /cv to avoid a division if the compiler is smart enough to see that cv IS a constant that it could invert once for all elements
1082b89d87eSLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
109ea61e9acSJeremy L Thompson   // OR we could absorb cv into Ctau_E but this puts more burden on user to know how to change constants with a change of fluid or units.  Same for
1102b89d87eSLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
1112b89d87eSLeila Ghaffari }
112