1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3d1b9ef12SLeila Ghaffari 4d1b9ef12SLeila Ghaffari /// @file 5d1b9ef12SLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation 63e17a7a1SJames Wright #include <ceed/types.h> 72b916ea7SJeremy L Thompson 8d0cce58aSJeremy L Thompson #include "newtonian_state.h" 9d1b9ef12SLeila Ghaffari 10d1b9ef12SLeila Ghaffari // ***************************************************************************** 1104e40bb6SJeremy L Thompson // Helper function for computing the variation in primitive variables, given Tau_d 12d1b9ef12SLeila Ghaffari // ***************************************************************************** 137e084dbeSJames Wright CEED_QFUNCTION_HELPER void dYFromTau(const CeedScalar Y[5], const CeedScalar Tau_d[3], CeedScalar dY[5]) { 14d1b9ef12SLeila Ghaffari dY[0] = Tau_d[0] * Y[0]; 15d1b9ef12SLeila Ghaffari dY[1] = Tau_d[1] * Y[1]; 16d1b9ef12SLeila Ghaffari dY[2] = Tau_d[1] * Y[2]; 17d1b9ef12SLeila Ghaffari dY[3] = Tau_d[1] * Y[3]; 18d1b9ef12SLeila Ghaffari dY[4] = Tau_d[2] * Y[4]; 19d1b9ef12SLeila Ghaffari } 20d1b9ef12SLeila Ghaffari 21d1b9ef12SLeila Ghaffari // ***************************************************************************** 22d1b9ef12SLeila Ghaffari // Helper functions for computing the stabilization terms 23d1b9ef12SLeila Ghaffari // ***************************************************************************** 24*cde3d787SJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(const NewtonianIGProperties gas, const State s, const CeedScalar Tau_d[3], 257e084dbeSJames Wright const CeedScalar strong_residual[5], CeedScalar stab[5][3]) { 26d1b9ef12SLeila Ghaffari CeedScalar dY[5]; 27d1b9ef12SLeila Ghaffari StateConservative dF[3]; 28d1b9ef12SLeila Ghaffari // Zero stab so all future terms can safely sum into it 294383298cSJames Wright SetValueN((CeedScalar *)stab, 0, 15); 3051d861cbSJames Wright dYFromTau(strong_residual, Tau_d, dY); 31edcfef1bSKenneth E. Jansen State ds = StateFromY_fwd(gas, s, dY); 32d1b9ef12SLeila Ghaffari FluxInviscid_fwd(gas, s, ds, dF); 33d1b9ef12SLeila Ghaffari for (CeedInt i = 0; i < 3; i++) { 34d1b9ef12SLeila Ghaffari CeedScalar dF_i[5]; 35d1b9ef12SLeila Ghaffari UnpackState_U(dF[i], dF_i); 362b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j]; 37d1b9ef12SLeila Ghaffari } 38d1b9ef12SLeila Ghaffari } 39d1b9ef12SLeila Ghaffari 40*cde3d787SJames Wright CEED_QFUNCTION_HELPER void Stabilization(StabilizationType stab_type, const NewtonianIGProperties gas, const State s, const CeedScalar Tau_d[3], 41*cde3d787SJames Wright const State ds[3], const CeedScalar U_dot[5], const CeedScalar body_force[5], const CeedScalar divFdiff[5], 427e084dbeSJames Wright CeedScalar stab[5][3]) { 43d1b9ef12SLeila Ghaffari // -- Stabilization method: none (Galerkin), SU, or SUPG 4451d861cbSJames Wright CeedScalar strong_residual[5] = {0}; 45*cde3d787SJames Wright switch (stab_type) { 46d1b9ef12SLeila Ghaffari case STAB_NONE: 47d1b9ef12SLeila Ghaffari break; 48d1b9ef12SLeila Ghaffari case STAB_SU: 4951d861cbSJames Wright FluxInviscidStrong(gas, s, ds, strong_residual); 50d1b9ef12SLeila Ghaffari break; 51d1b9ef12SLeila Ghaffari case STAB_SUPG: 5251d861cbSJames Wright FluxInviscidStrong(gas, s, ds, strong_residual); 538c85b835SJames Wright for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j] + divFdiff[j]; 54d1b9ef12SLeila Ghaffari break; 55d1b9ef12SLeila Ghaffari } 5651d861cbSJames Wright StabilizationMatrix(gas, s, Tau_d, strong_residual, stab); 57d1b9ef12SLeila Ghaffari } 58d1b9ef12SLeila Ghaffari 59d1b9ef12SLeila Ghaffari // ***************************************************************************** 60d1b9ef12SLeila Ghaffari // Helper function for computing Tau elements (stabilization constant) 61d1b9ef12SLeila Ghaffari // Model from: 62d1b9ef12SLeila Ghaffari // PHASTA 63d1b9ef12SLeila Ghaffari // 64d1b9ef12SLeila Ghaffari // Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial) 65d1b9ef12SLeila Ghaffari // ***************************************************************************** 66*cde3d787SJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(TauDiagCoefficients tau_coeffs, NewtonianIGProperties gas, State s, const CeedScalar dXdx[3][3], 67*cde3d787SJames Wright const CeedScalar dt, CeedScalar Tau_d[3]) { 68d1b9ef12SLeila Ghaffari // Context 69*cde3d787SJames Wright const CeedScalar Ctau_t = tau_coeffs.Ctau_t; 70*cde3d787SJames Wright const CeedScalar Ctau_v = tau_coeffs.Ctau_v; 71*cde3d787SJames Wright const CeedScalar Ctau_C = tau_coeffs.Ctau_C; 72*cde3d787SJames Wright const CeedScalar Ctau_M = tau_coeffs.Ctau_M; 73*cde3d787SJames Wright const CeedScalar Ctau_E = tau_coeffs.Ctau_E; 74*cde3d787SJames Wright const CeedScalar cv = gas.cv; 75*cde3d787SJames Wright const CeedScalar mu = gas.mu; 76d1b9ef12SLeila Ghaffari const CeedScalar rho = s.U.density; 77d1b9ef12SLeila Ghaffari 78d1b9ef12SLeila Ghaffari CeedScalar tau; 79d1b9ef12SLeila Ghaffari CeedScalar dts; 80d1b9ef12SLeila Ghaffari CeedScalar fact; 81d1b9ef12SLeila Ghaffari 82d5eb0e14SJames Wright CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term; 83d5eb0e14SJames Wright MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat); 84d1b9ef12SLeila Ghaffari 85d1b9ef12SLeila Ghaffari dts = Ctau_t / dt; 86d1b9ef12SLeila Ghaffari 87d5eb0e14SJames Wright { // u_i g_ij u_j 88d5eb0e14SJames Wright CeedScalar gij_uj[3] = {0.}; 89d5eb0e14SJames Wright MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj); 90d5eb0e14SJames Wright velocity_term = Dot3(s.Y.velocity, gij_uj); 91d5eb0e14SJames Wright } 92d5eb0e14SJames Wright 93d5eb0e14SJames Wright tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9); 94d1b9ef12SLeila Ghaffari 95d1b9ef12SLeila Ghaffari fact = sqrt(tau); 96d1b9ef12SLeila Ghaffari 97d5eb0e14SJames Wright Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125; 98d1b9ef12SLeila Ghaffari Tau_d[1] = Ctau_M / fact; 99d1b9ef12SLeila Ghaffari Tau_d[2] = Ctau_E / (fact * cv); 100d1b9ef12SLeila Ghaffari 10104e40bb6SJeremy L Thompson // consider putting back the way I initially had it 10204e40bb6SJeremy 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 103d1b9ef12SLeila Ghaffari // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M 10404e40bb6SJeremy 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 105d1b9ef12SLeila Ghaffari // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T) 106d1b9ef12SLeila Ghaffari } 107