xref: /honee/qfunctions/sgs_dd_utils.h (revision 952746efd8add44fb80eb89215420d2db713796f)
1*952746efSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2*952746efSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*952746efSJames Wright //
4*952746efSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5*952746efSJames Wright //
6*952746efSJames Wright // This file is part of CEED:  http://github.com/ceed
7*952746efSJames Wright 
8*952746efSJames Wright /// @file
9*952746efSJames Wright /// Structs and helper functions for data-driven subgrid-stress modeling
10*952746efSJames Wright /// See 'Invariant data-driven subgrid stress modeling in the strain-rate eigenframe for large eddy simulation' 2022 and 'S-frame discrepancy
11*952746efSJames Wright /// correction models for data-informed Reynolds stress closure' 2022
12*952746efSJames Wright 
13*952746efSJames Wright #ifndef sgs_dd_utils_h
14*952746efSJames Wright #define sgs_dd_utils_h
15*952746efSJames Wright 
16*952746efSJames Wright #include <ceed.h>
17*952746efSJames Wright 
18*952746efSJames Wright #include "newtonian_state.h"
19*952746efSJames Wright #include "newtonian_types.h"
20*952746efSJames Wright #include "utils.h"
21*952746efSJames Wright #include "utils_eigensolver_jacobi.h"
22*952746efSJames Wright 
23*952746efSJames Wright // @brief Calculate the inverse of the multiplicity, reducing to a single component
24*952746efSJames Wright CEED_QFUNCTION(InverseMultiplicity)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
25*952746efSJames Wright   const CeedScalar(*multiplicity)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
26*952746efSJames Wright   CeedScalar(*inv_multiplicity)               = (CeedScalar(*))out[0];
27*952746efSJames Wright 
28*952746efSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) inv_multiplicity[i] = 1.0 / multiplicity[0][i];
29*952746efSJames Wright   return 0;
30*952746efSJames Wright }
31*952746efSJames Wright 
32*952746efSJames Wright // @brief Calculate Frobenius norm of velocity gradient from eigenframe quantities
33*952746efSJames Wright CEED_QFUNCTION_HELPER CeedScalar VelocityGradientMagnitude(const CeedScalar strain_sframe[3], const CeedScalar vorticity_sframe[3]) {
34*952746efSJames Wright   return sqrt(Dot3(strain_sframe, strain_sframe) + 0.5 * Dot3(vorticity_sframe, vorticity_sframe));
35*952746efSJames Wright };
36*952746efSJames Wright 
37*952746efSJames Wright // @brief Change the order of basis vectors so that they align with vector and obey right-hand rule
38*952746efSJames Wright // @details The e_1 and e_3 basis vectors are the closest aligned to the vector. The e_2 is set via  e_3 x e_1
39*952746efSJames Wright // The basis vectors are assumed to form the rows of the basis matrix.
40*952746efSJames Wright CEED_QFUNCTION_HELPER void OrientBasisWithVector(CeedScalar basis[3][3], const CeedScalar vector[3]) {
41*952746efSJames Wright   CeedScalar alignment[3] = {0.}, cross[3];
42*952746efSJames Wright 
43*952746efSJames Wright   MatVec3(basis, vector, CEED_NOTRANSPOSE, alignment);
44*952746efSJames Wright 
45*952746efSJames Wright   if (alignment[0] < 0) ScaleN(basis[0], -1, 3);
46*952746efSJames Wright   if (alignment[2] < 0) ScaleN(basis[2], -1, 3);
47*952746efSJames Wright 
48*952746efSJames Wright   Cross3(basis[2], basis[0], cross);
49*952746efSJames Wright   CeedScalar basis_1_orientation = Dot3(cross, basis[1]);
50*952746efSJames Wright   if (basis_1_orientation < 0) ScaleN(basis[1], -1, 3);
51*952746efSJames Wright }
52*952746efSJames Wright 
53*952746efSJames Wright /**
54*952746efSJames Wright  * @brief Compute model inputs for anisotropic data-driven model
55*952746efSJames Wright  *
56*952746efSJames Wright  * @param[in]  grad_velo_aniso     Gradient of velocity in physical (anisotropic) coordinates
57*952746efSJames Wright  * @param[in]  km_A_ij             Anisotropy tensor, in Kelvin-Mandel notation
58*952746efSJames Wright  * @param[in]  delta               Length used to create anisotropy tensor
59*952746efSJames Wright  * @param[in]  viscosity           Kinematic viscosity
60*952746efSJames Wright  * @param[out] eigenvectors        Eigenvectors of the (anisotropic) velocity gradient
61*952746efSJames Wright  * @param[out] inputs              Data-driven model inputs
62*952746efSJames Wright  * @param[out] grad_velo_magnitude Frobenius norm of the velocity gradient
63*952746efSJames Wright  */
64*952746efSJames Wright CEED_QFUNCTION_HELPER void ComputeSGS_DDAnisotropicInputs(const CeedScalar grad_velo_aniso[3][3], const CeedScalar km_A_ij[6], const CeedScalar delta,
65*952746efSJames Wright                                                           const CeedScalar viscosity, CeedScalar eigenvectors[3][3], CeedScalar inputs[6],
66*952746efSJames Wright                                                           CeedScalar *grad_velo_magnitude) {
67*952746efSJames Wright   CeedScalar strain_sframe[3] = {0.}, vorticity_sframe[3] = {0.};
68*952746efSJames Wright   CeedScalar A_ij[3][3] = {{0.}}, grad_velo_iso[3][3] = {{0.}};
69*952746efSJames Wright 
70*952746efSJames Wright   // -- Transform physical, anisotropic velocity gradient to isotropic
71*952746efSJames Wright   KMUnpack(km_A_ij, A_ij);
72*952746efSJames Wright   MatMat3(grad_velo_aniso, A_ij, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, grad_velo_iso);
73*952746efSJames Wright 
74*952746efSJames Wright   {  // -- Get Eigenframe
75*952746efSJames Wright     CeedScalar kmstrain_iso[6], strain_iso[3][3];
76*952746efSJames Wright     CeedInt    work_vector[3] = {0};
77*952746efSJames Wright     KMStrainRate(grad_velo_iso, kmstrain_iso);
78*952746efSJames Wright     KMUnpack(kmstrain_iso, strain_iso);
79*952746efSJames Wright     Diagonalize3(strain_iso, strain_sframe, eigenvectors, work_vector, SORT_DECREASING_EVALS, true, 5);
80*952746efSJames Wright   }
81*952746efSJames Wright 
82*952746efSJames Wright   {  // -- Get vorticity in S-frame
83*952746efSJames Wright     CeedScalar rotation_iso[3][3];
84*952746efSJames Wright     RotationRate(grad_velo_iso, rotation_iso);
85*952746efSJames Wright     CeedScalar vorticity_iso[3] = {-2 * rotation_iso[1][2], 2 * rotation_iso[0][2], -2 * rotation_iso[0][1]};
86*952746efSJames Wright     OrientBasisWithVector(eigenvectors, vorticity_iso);
87*952746efSJames Wright     MatVec3(eigenvectors, vorticity_iso, CEED_NOTRANSPOSE, vorticity_sframe);
88*952746efSJames Wright   }
89*952746efSJames Wright 
90*952746efSJames Wright   // -- Calculate DD model inputs
91*952746efSJames Wright   *grad_velo_magnitude = VelocityGradientMagnitude(strain_sframe, vorticity_sframe);
92*952746efSJames Wright   inputs[0]            = strain_sframe[0];
93*952746efSJames Wright   inputs[1]            = strain_sframe[1];
94*952746efSJames Wright   inputs[2]            = strain_sframe[2];
95*952746efSJames Wright   inputs[3]            = vorticity_sframe[0];
96*952746efSJames Wright   inputs[4]            = vorticity_sframe[1];
97*952746efSJames Wright   inputs[5]            = viscosity / Square(delta);
98*952746efSJames Wright   ScaleN(inputs, 1 / (*grad_velo_magnitude + CEED_EPSILON), 6);
99*952746efSJames Wright }
100*952746efSJames Wright 
101*952746efSJames Wright #endif  // sgs_dd_utils_h
102