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 6*3e17a7a1SJames 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 // ***************************************************************************** 132b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], 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 // ***************************************************************************** 2451d861cbSJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar strong_residual[5], 2551d861cbSJames Wright 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 292b916ea7SJeremy L Thompson for (CeedInt i = 0; i < 5; i++) { 302b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0; 312b916ea7SJeremy L Thompson } 3251d861cbSJames Wright dYFromTau(strong_residual, Tau_d, dY); 33edcfef1bSKenneth E. Jansen State ds = StateFromY_fwd(gas, s, dY); 34d1b9ef12SLeila Ghaffari FluxInviscid_fwd(gas, s, ds, dF); 35d1b9ef12SLeila Ghaffari for (CeedInt i = 0; i < 3; i++) { 36d1b9ef12SLeila Ghaffari CeedScalar dF_i[5]; 37d1b9ef12SLeila Ghaffari UnpackState_U(dF[i], dF_i); 382b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j]; 39d1b9ef12SLeila Ghaffari } 40d1b9ef12SLeila Ghaffari } 41d1b9ef12SLeila Ghaffari 422b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5], 438c85b835SJames Wright const CeedScalar body_force[5], const CeedScalar divFdiff[5], CeedScalar stab[5][3]) { 44d1b9ef12SLeila Ghaffari // -- Stabilization method: none (Galerkin), SU, or SUPG 4551d861cbSJames Wright CeedScalar strong_residual[5] = {0}; 46d1b9ef12SLeila Ghaffari switch (gas->stabilization) { 47d1b9ef12SLeila Ghaffari case STAB_NONE: 48d1b9ef12SLeila Ghaffari break; 49d1b9ef12SLeila Ghaffari case STAB_SU: 5051d861cbSJames Wright FluxInviscidStrong(gas, s, ds, strong_residual); 51d1b9ef12SLeila Ghaffari break; 52d1b9ef12SLeila Ghaffari case STAB_SUPG: 5351d861cbSJames Wright FluxInviscidStrong(gas, s, ds, strong_residual); 548c85b835SJames Wright for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j] + divFdiff[j]; 55d1b9ef12SLeila Ghaffari break; 56d1b9ef12SLeila Ghaffari } 5751d861cbSJames Wright StabilizationMatrix(gas, s, Tau_d, strong_residual, stab); 58d1b9ef12SLeila Ghaffari } 59d1b9ef12SLeila Ghaffari 60d1b9ef12SLeila Ghaffari // ***************************************************************************** 61d1b9ef12SLeila Ghaffari // Helper function for computing Tau elements (stabilization constant) 62d1b9ef12SLeila Ghaffari // Model from: 63d1b9ef12SLeila Ghaffari // PHASTA 64d1b9ef12SLeila Ghaffari // 65d1b9ef12SLeila Ghaffari // Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial) 66d1b9ef12SLeila Ghaffari // ***************************************************************************** 672b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt, 682b916ea7SJeremy L Thompson CeedScalar Tau_d[3]) { 69d1b9ef12SLeila Ghaffari // Context 70d1b9ef12SLeila Ghaffari const CeedScalar Ctau_t = gas->Ctau_t; 71d1b9ef12SLeila Ghaffari const CeedScalar Ctau_v = gas->Ctau_v; 72d1b9ef12SLeila Ghaffari const CeedScalar Ctau_C = gas->Ctau_C; 73d1b9ef12SLeila Ghaffari const CeedScalar Ctau_M = gas->Ctau_M; 74d1b9ef12SLeila Ghaffari const CeedScalar Ctau_E = gas->Ctau_E; 75d1b9ef12SLeila Ghaffari const CeedScalar cv = gas->cv; 76d1b9ef12SLeila Ghaffari const CeedScalar mu = gas->mu; 77d1b9ef12SLeila Ghaffari const CeedScalar rho = s.U.density; 78d1b9ef12SLeila Ghaffari 79d1b9ef12SLeila Ghaffari CeedScalar tau; 80d1b9ef12SLeila Ghaffari CeedScalar dts; 81d1b9ef12SLeila Ghaffari CeedScalar fact; 82d1b9ef12SLeila Ghaffari 83d5eb0e14SJames Wright CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term; 84d5eb0e14SJames Wright MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat); 85d1b9ef12SLeila Ghaffari 86d1b9ef12SLeila Ghaffari dts = Ctau_t / dt; 87d1b9ef12SLeila Ghaffari 88d5eb0e14SJames Wright { // u_i g_ij u_j 89d5eb0e14SJames Wright CeedScalar gij_uj[3] = {0.}; 90d5eb0e14SJames Wright MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj); 91d5eb0e14SJames Wright velocity_term = Dot3(s.Y.velocity, gij_uj); 92d5eb0e14SJames Wright } 93d5eb0e14SJames Wright 94d5eb0e14SJames Wright tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9); 95d1b9ef12SLeila Ghaffari 96d1b9ef12SLeila Ghaffari fact = sqrt(tau); 97d1b9ef12SLeila Ghaffari 98d5eb0e14SJames Wright Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125; 99d1b9ef12SLeila Ghaffari Tau_d[1] = Ctau_M / fact; 100d1b9ef12SLeila Ghaffari Tau_d[2] = Ctau_E / (fact * cv); 101d1b9ef12SLeila Ghaffari 10204e40bb6SJeremy L Thompson // consider putting back the way I initially had it 10304e40bb6SJeremy 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 104d1b9ef12SLeila Ghaffari // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M 10504e40bb6SJeremy 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 106d1b9ef12SLeila Ghaffari // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T) 107d1b9ef12SLeila Ghaffari } 108