xref: /honee/qfunctions/sgs_dd_training.h (revision c4bd9656417ac488adba016fe5c2e786414c4baf)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Structs and helper functions for training data-driven subgrid-stress models
6 /// See 'Invariant data-driven subgrid stress modeling in the strain-rate eigenframe for large eddy simulation' 2022 and 'S-frame discrepancy
7 /// correction models for data-informed Reynolds stress closure' 2022
8 #include <ceed/types.h>
9 
10 #include "differential_filter_enums.h"
11 #include "newtonian_state.h"
12 #include "newtonian_types.h"
13 #include "sgs_dd_utils.h"
14 #include "utils.h"
15 #include "utils_eigensolver_jacobi.h"
16 
17 typedef struct SGS_DD_TrainingContext_ *SGS_DDTrainingContext;
18 struct SGS_DD_TrainingContext_ {
19   struct NewtonianIdealGasContext_ newt_ctx;
20 };
21 
22 // @brief Calculate Data-Driven SGS model training data at nodes
23 CEED_QFUNCTION_HELPER int ComputeSGS_DDAnisotropicTrainingDataNodal(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
24                                                                     StateVariable state_var) {
25   const CeedScalar(*q)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[0];
26   const CeedScalar(*velo_prod)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[1];
27   const CeedScalar(*grad_velo)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[2];
28   const CeedScalar(*A_ij_delta)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[3];
29   const CeedScalar(*inv_multiplicity)         = (const CeedScalar(*))in[4];
30   CeedScalar(*v)[CEED_Q_VLA]                  = (CeedScalar(*)[CEED_Q_VLA])out[0];
31 
32   const SGS_DDTrainingContext sgsdd_ctx = (SGS_DDTrainingContext)ctx;
33   const NewtonianIGProperties gas       = sgsdd_ctx->newt_ctx.gas;
34 
35   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
36     const CeedScalar qi[5]                 = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
37     const CeedScalar grad_velo_aniso[3][3] = {
38         {grad_velo[0][0][i], grad_velo[0][1][i], grad_velo[0][2][i]},
39         {grad_velo[1][0][i], grad_velo[1][1][i], grad_velo[1][2][i]},
40         {grad_velo[2][0][i], grad_velo[2][1][i], grad_velo[2][2][i]}
41     };
42     const CeedScalar km_A_ij[6] = {A_ij_delta[0][i], A_ij_delta[1][i], A_ij_delta[2][i], A_ij_delta[3][i], A_ij_delta[4][i], A_ij_delta[5][i]};
43     const CeedScalar delta      = A_ij_delta[6][i];
44     const State      s          = StateFromQ(gas, qi, state_var);
45     CeedScalar       inputs[6];
46     CeedScalar       eigenvectors[3][3], grad_velo_magnitude;  // dummy variables, don't actually use them
47 
48     ComputeSgsDDInputs(grad_velo_aniso, km_A_ij, delta, gas.mu / s.U.density, eigenvectors, inputs, &grad_velo_magnitude);
49 
50     for (int j = 0; j < 6; j++) v[j][i] = inv_multiplicity[i] * inputs[j];
51 
52     v[0 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_XX][i] - Square(s.Y.velocity[0])) * inv_multiplicity[i];
53     v[1 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_YY][i] - Square(s.Y.velocity[1])) * inv_multiplicity[i];
54     v[2 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_ZZ][i] - Square(s.Y.velocity[2])) * inv_multiplicity[i];
55     v[3 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_YZ][i] - s.Y.velocity[1] * s.Y.velocity[2]) * inv_multiplicity[i];
56     v[4 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_XZ][i] - s.Y.velocity[0] * s.Y.velocity[2]) * inv_multiplicity[i];
57     v[5 + 6][i] = (velo_prod[DIFF_FILTER_VELOCITY_SQUARED_XY][i] - s.Y.velocity[0] * s.Y.velocity[1]) * inv_multiplicity[i];
58   }
59   return 0;
60 }
61 
62 CEED_QFUNCTION(ComputeSGS_DDAnisotropicTrainingDataNodal_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
63   return ComputeSGS_DDAnisotropicTrainingDataNodal(ctx, Q, in, out, STATEVAR_PRIMITIVE);
64 }
65