xref: /honee/qfunctions/sgs_dd_model.h (revision 71c848e3cbb5937b366eb6209f12ca2bf020f463)
1 // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Structs and helper functions to evaluate data-driven subgrid-stress modeling
10 /// See 'Invariant data-driven subgrid stress modeling in the strain-rate eigenframe for large eddy simulation' 2022 and 'S-frame discrepancy
11 /// correction models for data-informed Reynolds stress closure' 2022
12 
13 #ifndef sgs_dd_model_h
14 #define sgs_dd_model_h
15 
16 #include <ceed.h>
17 
18 #include "newtonian_state.h"
19 #include "newtonian_types.h"
20 #include "sgs_dd_utils.h"
21 #include "utils.h"
22 #include "utils_eigensolver_jacobi.h"
23 
24 typedef struct SgsDDModelContext_ *SgsDDModelContext;
25 struct SgsDDModelContext_ {
26   CeedInt    num_inputs, num_outputs;
27   CeedInt    num_layers;
28   CeedInt    num_neurons;
29   CeedScalar alpha;
30 
31   struct NewtonianIdealGasContext_ gas;
32   struct {
33     size_t bias1, bias2;
34     size_t weight1, weight2;
35     size_t out_scaling;
36   } offsets;
37   size_t     total_bytes;
38   CeedScalar data[1];
39 };
40 
41 CEED_QFUNCTION_HELPER void LeakyReLU(CeedScalar *x, const CeedScalar alpha, const CeedInt N) {
42   for (CeedInt i = 0; i < N; i++) x[i] *= (x[i] < 0 ? alpha : 1.);
43 }
44 
45 CEED_QFUNCTION_HELPER void DataDrivenInference(const CeedScalar *inputs, CeedScalar *outputs, SgsDDModelContext sgsdd_ctx) {
46   const CeedInt     num_neurons = sgsdd_ctx->num_neurons;
47   const CeedInt     num_inputs  = sgsdd_ctx->num_inputs;
48   const CeedInt     num_outputs = sgsdd_ctx->num_outputs;
49   const CeedScalar  alpha       = sgsdd_ctx->alpha;
50   const CeedScalar *bias1       = &sgsdd_ctx->data[sgsdd_ctx->offsets.bias1];
51   const CeedScalar *bias2       = &sgsdd_ctx->data[sgsdd_ctx->offsets.bias2];
52   const CeedScalar *weight1     = &sgsdd_ctx->data[sgsdd_ctx->offsets.weight1];
53   const CeedScalar *weight2     = &sgsdd_ctx->data[sgsdd_ctx->offsets.weight2];
54   CeedScalar        V[20]       = {0.};
55 
56   CopyN(bias1, V, num_neurons);
57   MatVecNM(weight1, inputs, num_neurons, num_inputs, CEED_NOTRANSPOSE, V);
58   LeakyReLU(V, alpha, num_neurons);
59   CopyN(bias2, outputs, num_outputs);
60   MatVecNM(weight2, V, num_outputs, num_neurons, CEED_NOTRANSPOSE, outputs);
61 }
62 
63 CEED_QFUNCTION_HELPER void ComputeSgsDDAnisotropic(const CeedScalar grad_velo_aniso[3][3], const CeedScalar km_A_ij[6], const CeedScalar delta,
64                                                    const CeedScalar viscosity, CeedScalar kmsgs_stress[6], SgsDDModelContext sgsdd_ctx) {
65   CeedScalar inputs[6], grad_velo_magnitude, eigenvectors[3][3], sgs_sframe_sym[6] = {0.};
66   const CeedScalar(*new_bounds)[2] = (const CeedScalar(*)[2]) & sgsdd_ctx->data[sgsdd_ctx->offsets.out_scaling];
67 
68   ComputeSGS_DDAnisotropicInputs(grad_velo_aniso, km_A_ij, delta, viscosity, eigenvectors, inputs, &grad_velo_magnitude);
69   DataDrivenInference(inputs, sgs_sframe_sym, sgsdd_ctx);
70   ComputeSGS_DDAnisotropicOutputs(sgs_sframe_sym, delta, eigenvectors, new_bounds, grad_velo_magnitude, kmsgs_stress);
71 }
72 
73 // @brief Calculate subgrid stress at nodes using anisotropic data-driven model
74 CEED_QFUNCTION_HELPER int ComputeSgsDDAnisotropicNodal(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
75                                                        StateVariable state_var) {
76   const CeedScalar(*q)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[0];
77   const CeedScalar(*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[1];
78   const CeedScalar(*grad_velo)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[2];
79   const CeedScalar(*A_ij_delta)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[3];
80   const CeedScalar(*inv_multiplicity)         = (const CeedScalar(*))in[4];
81   CeedScalar(*v)[CEED_Q_VLA]                  = (CeedScalar(*)[CEED_Q_VLA])out[0];
82 
83   const SgsDDModelContext        sgsdd_ctx = (SgsDDModelContext)ctx;
84   const NewtonianIdealGasContext gas       = &sgsdd_ctx->gas;
85 
86   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
87     const CeedScalar qi[5]                 = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
88     const CeedScalar x_i[3]                = {x[0][i], x[1][i], x[2][i]};
89     const CeedScalar grad_velo_aniso[3][3] = {
90         {grad_velo[0][0][i], grad_velo[0][1][i], grad_velo[0][2][i]},
91         {grad_velo[1][0][i], grad_velo[1][1][i], grad_velo[1][2][i]},
92         {grad_velo[2][0][i], grad_velo[2][1][i], grad_velo[2][2][i]}
93     };
94     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]};
95     const CeedScalar delta      = A_ij_delta[6][i];
96     const State      s          = StateFromQ(gas, qi, x_i, state_var);
97     CeedScalar       km_sgs[6];
98 
99     ComputeSgsDDAnisotropic(grad_velo_aniso, km_A_ij, delta, gas->mu / s.U.density, km_sgs, sgsdd_ctx);
100 
101     for (int j = 0; j < 6; j++) v[j][i] = inv_multiplicity[i] * km_sgs[j];
102   }
103   return 0;
104 }
105 
106 CEED_QFUNCTION(ComputeSgsDDAnisotropicNodal_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
107   return ComputeSgsDDAnisotropicNodal(ctx, Q, in, out, STATEVAR_PRIMITIVE);
108 }
109 
110 CEED_QFUNCTION(ComputeSgsDDAnisotropicNodal_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
111   return ComputeSgsDDAnisotropicNodal(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
112 }
113 
114 // @brief Adds subgrid stress to residual (during IFunction evaluation)
115 CEED_QFUNCTION_HELPER int FluxSubgridStress(const StatePrimitive Y, const CeedScalar km_sgs[6], CeedScalar Flux[5][3]) {
116   CeedScalar sgs[3][3];
117 
118   KMUnpack(km_sgs, sgs);
119   for (CeedInt j = 0; j < 3; j++) {
120     Flux[0][j] = 0.;
121     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = sgs[k][j];
122     Flux[4][j] = Y.velocity[0] * sgs[0][j] + Y.velocity[1] * sgs[1][j] + Y.velocity[2] * sgs[2][j];
123   }
124   return 0;
125 }
126 
127 CEED_QFUNCTION_HELPER int IFunction_NodalSgs(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
128   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
129   const CeedScalar(*q_data)             = in[1];
130   const CeedScalar(*x)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[2];
131   const CeedScalar(*km_sgs)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
132   CeedScalar(*Grad_v)[5][CEED_Q_VLA]    = (CeedScalar(*)[5][CEED_Q_VLA])out[0];
133 
134   SgsDDModelContext        sgsdd_ctx = (SgsDDModelContext)ctx;
135   NewtonianIdealGasContext gas       = &sgsdd_ctx->gas;
136 
137   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
138     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
139     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
140     const State      s      = StateFromQ(gas, qi, x_i, state_var);
141 
142     CeedScalar wdetJ, dXdx[3][3];
143     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
144 
145     CeedScalar       Flux[5][3];
146     const CeedScalar km_sgs_i[6] = {km_sgs[0][i], km_sgs[1][i], km_sgs[2][i], km_sgs[3][i], km_sgs[4][i], km_sgs[5][i]};
147     FluxSubgridStress(s.Y, km_sgs_i, Flux);
148 
149     for (CeedInt k = 0; k < 3; k++) {
150       for (CeedInt j = 0; j < 5; j++) {
151         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
152       }
153     }
154   }
155   return 0;
156 }
157 
158 CEED_QFUNCTION(IFunction_NodalSgs_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
159   return IFunction_NodalSgs(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
160 }
161 
162 CEED_QFUNCTION(IFunction_NodalSgs_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
163   return IFunction_NodalSgs(ctx, Q, in, out, STATEVAR_PRIMITIVE);
164 }
165 
166 #endif  // sgs_dd_model_h
167