162b7942eSJames Wright // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors. 262b7942eSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 362b7942eSJames Wright // 462b7942eSJames Wright // SPDX-License-Identifier: BSD-2-Clause 562b7942eSJames Wright // 662b7942eSJames Wright // This file is part of CEED: http://github.com/ceed 762b7942eSJames Wright 862b7942eSJames Wright /// @file 962b7942eSJames Wright /// Structs and helper functions for data-driven subgrid-stress modeling 10*3fc405b4SJames Wright /// See 'Invariant data-driven subgrid stress modeling in the strain-rate eigenframe for large eddy simulation' 2022 and 'S-frame discrepancy 11*3fc405b4SJames Wright /// correction models for data-informed Reynolds stress closure' 2022 1262b7942eSJames Wright 1362b7942eSJames Wright #ifndef sgs_dd_model_h 1462b7942eSJames Wright #define sgs_dd_model_h 1562b7942eSJames Wright 1662b7942eSJames Wright #include <ceed.h> 1762b7942eSJames Wright 18*3fc405b4SJames Wright #include "newtonian_state.h" 19*3fc405b4SJames Wright #include "utils.h" 20*3fc405b4SJames Wright #include "utils_eigensolver_jacobi.h" 21*3fc405b4SJames Wright 2262b7942eSJames Wright typedef struct SGS_DD_ModelContext_ *SGS_DDModelContext; 2362b7942eSJames Wright struct SGS_DD_ModelContext_ { 2462b7942eSJames Wright CeedInt num_inputs, num_outputs; 2562b7942eSJames Wright CeedInt num_layers; 2662b7942eSJames Wright CeedInt num_neurons; 2762b7942eSJames Wright CeedScalar alpha; 2862b7942eSJames Wright 2962b7942eSJames Wright struct { 3062b7942eSJames Wright size_t bias1, bias2; 3162b7942eSJames Wright size_t weight1, weight2; 3262b7942eSJames Wright size_t out_scaling; 3362b7942eSJames Wright } offsets; 3462b7942eSJames Wright size_t total_bytes; 3562b7942eSJames Wright CeedScalar data[1]; 3662b7942eSJames Wright }; 3762b7942eSJames Wright 38*3fc405b4SJames Wright // @brief Calculate the inverse of the multiplicity, reducing to a single component 39*3fc405b4SJames Wright CEED_QFUNCTION(InverseMultiplicity)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 40*3fc405b4SJames Wright const CeedScalar(*multiplicity)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 41*3fc405b4SJames Wright CeedScalar(*inv_multiplicity) = (CeedScalar(*))out[0]; 42*3fc405b4SJames Wright 43*3fc405b4SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) inv_multiplicity[i] = 1.0 / multiplicity[0][i]; 44*3fc405b4SJames Wright return 0; 45*3fc405b4SJames Wright } 46*3fc405b4SJames Wright 47*3fc405b4SJames Wright // @brief Calculate Frobenius norm of velocity gradient from eigenframe quantities 48*3fc405b4SJames Wright CEED_QFUNCTION_HELPER CeedScalar VelocityGradientMagnitude(const CeedScalar strain_sframe[3], const CeedScalar vorticity_sframe[3]) { 49*3fc405b4SJames Wright return sqrt(Dot3(strain_sframe, strain_sframe) + 0.5 * Dot3(vorticity_sframe, vorticity_sframe)); 50*3fc405b4SJames Wright }; 51*3fc405b4SJames Wright 52*3fc405b4SJames Wright // @brief Denormalize outputs using min-max (de-)normalization 53*3fc405b4SJames Wright CEED_QFUNCTION_HELPER void DenormalizeDDOutputs(CeedScalar output[6], const CeedScalar new_bounds[6][2], const CeedScalar old_bounds[6][2]) { 54*3fc405b4SJames Wright CeedScalar bounds_ratio; 55*3fc405b4SJames Wright for (int i = 0; i < 6; i++) { 56*3fc405b4SJames Wright bounds_ratio = (new_bounds[i][1] - new_bounds[i][0]) / (old_bounds[i][1] - old_bounds[i][0]); 57*3fc405b4SJames Wright output[i] = bounds_ratio * (output[i] - old_bounds[i][1]) + new_bounds[i][1]; 58*3fc405b4SJames Wright } 59*3fc405b4SJames Wright } 60*3fc405b4SJames Wright 61*3fc405b4SJames Wright // @brief Change the order of basis vectors so that they align with vector and obey right-hand rule 62*3fc405b4SJames 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 63*3fc405b4SJames Wright // The basis vectors are assumed to form the rows of the basis matrix. 64*3fc405b4SJames Wright CEED_QFUNCTION_HELPER void OrientBasisWithVector(CeedScalar basis[3][3], const CeedScalar vector[3]) { 65*3fc405b4SJames Wright CeedScalar alignment[3] = {0.}, cross[3]; 66*3fc405b4SJames Wright 67*3fc405b4SJames Wright MatVec3(basis, vector, CEED_NOTRANSPOSE, alignment); 68*3fc405b4SJames Wright 69*3fc405b4SJames Wright if (alignment[0] < 0) ScaleN(basis[0], -1, 3); 70*3fc405b4SJames Wright if (alignment[2] < 0) ScaleN(basis[2], -1, 3); 71*3fc405b4SJames Wright 72*3fc405b4SJames Wright Cross3(basis[2], basis[0], cross); 73*3fc405b4SJames Wright CeedScalar basis_1_orientation = Dot3(cross, basis[1]); 74*3fc405b4SJames Wright if (basis_1_orientation < 0) ScaleN(basis[1], -1, 3); 75*3fc405b4SJames Wright } 76*3fc405b4SJames Wright 77*3fc405b4SJames Wright CEED_QFUNCTION_HELPER void LeakyReLU(CeedScalar *x, const CeedScalar alpha, const CeedInt N) { 78*3fc405b4SJames Wright for (CeedInt i = 0; i < N; i++) x[i] *= (x[i] < 0 ? alpha : 1.); 79*3fc405b4SJames Wright } 80*3fc405b4SJames Wright 81*3fc405b4SJames Wright CEED_QFUNCTION_HELPER void DataDrivenInference(const CeedScalar *inputs, CeedScalar *outputs, SGS_DDModelContext sgsdd_ctx) { 82*3fc405b4SJames Wright const CeedInt num_neurons = sgsdd_ctx->num_neurons; 83*3fc405b4SJames Wright const CeedInt num_inputs = sgsdd_ctx->num_inputs; 84*3fc405b4SJames Wright const CeedInt num_outputs = sgsdd_ctx->num_outputs; 85*3fc405b4SJames Wright const CeedScalar alpha = sgsdd_ctx->alpha; 86*3fc405b4SJames Wright const CeedScalar *bias1 = &sgsdd_ctx->data[sgsdd_ctx->offsets.bias1]; 87*3fc405b4SJames Wright const CeedScalar *bias2 = &sgsdd_ctx->data[sgsdd_ctx->offsets.bias2]; 88*3fc405b4SJames Wright const CeedScalar *weight1 = &sgsdd_ctx->data[sgsdd_ctx->offsets.weight1]; 89*3fc405b4SJames Wright const CeedScalar *weight2 = &sgsdd_ctx->data[sgsdd_ctx->offsets.weight2]; 90*3fc405b4SJames Wright CeedScalar V[20] = {0.}; 91*3fc405b4SJames Wright 92*3fc405b4SJames Wright CopyN(bias1, V, num_neurons); 93*3fc405b4SJames Wright MatVecNM(weight1, inputs, num_neurons, num_inputs, CEED_NOTRANSPOSE, V); 94*3fc405b4SJames Wright LeakyReLU(V, alpha, num_neurons); 95*3fc405b4SJames Wright CopyN(bias2, outputs, num_outputs); 96*3fc405b4SJames Wright MatVecNM(weight2, V, num_outputs, num_neurons, CEED_NOTRANSPOSE, outputs); 97*3fc405b4SJames Wright } 98*3fc405b4SJames Wright 99*3fc405b4SJames Wright CEED_QFUNCTION_HELPER void ComputeSGS_DDAnisotropic(const CeedScalar grad_velo_aniso[3][3], const CeedScalar km_A_ij[6], const CeedScalar delta, 100*3fc405b4SJames Wright const CeedScalar viscosity, CeedScalar kmsgs_stress[6], SGS_DDModelContext sgsdd_ctx) { 101*3fc405b4SJames Wright CeedScalar strain_sframe[3] = {0.}, vorticity_sframe[3] = {0.}, eigenvectors[3][3]; 102*3fc405b4SJames Wright CeedScalar A_ij[3][3] = {{0.}}, grad_velo_iso[3][3] = {{0.}}; 103*3fc405b4SJames Wright 104*3fc405b4SJames Wright // -- Unpack anisotropy tensor 105*3fc405b4SJames Wright KMUnpack(km_A_ij, A_ij); 106*3fc405b4SJames Wright 107*3fc405b4SJames Wright // -- Transform physical, anisotropic velocity gradient to isotropic 108*3fc405b4SJames Wright MatMat3(grad_velo_aniso, A_ij, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, grad_velo_iso); 109*3fc405b4SJames Wright 110*3fc405b4SJames Wright { // -- Get Eigenframe 111*3fc405b4SJames Wright CeedScalar kmstrain_iso[6], strain_iso[3][3]; 112*3fc405b4SJames Wright CeedInt work_vector[3] = {0}; 113*3fc405b4SJames Wright KMStrainRate(grad_velo_iso, kmstrain_iso); 114*3fc405b4SJames Wright KMUnpack(kmstrain_iso, strain_iso); 115*3fc405b4SJames Wright Diagonalize3(strain_iso, strain_sframe, eigenvectors, work_vector, SORT_DECREASING_EVALS, true, 5); 116*3fc405b4SJames Wright } 117*3fc405b4SJames Wright 118*3fc405b4SJames Wright { // -- Get vorticity in S-frame 119*3fc405b4SJames Wright CeedScalar rotation_iso[3][3]; 120*3fc405b4SJames Wright RotationRate(grad_velo_iso, rotation_iso); 121*3fc405b4SJames Wright CeedScalar vorticity_iso[3] = {-2 * rotation_iso[1][2], 2 * rotation_iso[0][2], -2 * rotation_iso[0][1]}; 122*3fc405b4SJames Wright OrientBasisWithVector(eigenvectors, vorticity_iso); 123*3fc405b4SJames Wright MatVec3(eigenvectors, vorticity_iso, CEED_NOTRANSPOSE, vorticity_sframe); 124*3fc405b4SJames Wright } 125*3fc405b4SJames Wright 126*3fc405b4SJames Wright // -- Setup DD model inputs 127*3fc405b4SJames Wright const CeedScalar grad_velo_magnitude = VelocityGradientMagnitude(strain_sframe, vorticity_sframe); 128*3fc405b4SJames Wright CeedScalar inputs[6] = {strain_sframe[0], strain_sframe[1], strain_sframe[2], vorticity_sframe[0], vorticity_sframe[1], viscosity / Square(delta)}; 129*3fc405b4SJames Wright ScaleN(inputs, 1 / (grad_velo_magnitude + CEED_EPSILON), 6); 130*3fc405b4SJames Wright 131*3fc405b4SJames Wright CeedScalar sgs_sframe_sym[6] = {0.}; 132*3fc405b4SJames Wright DataDrivenInference(inputs, sgs_sframe_sym, sgsdd_ctx); 133*3fc405b4SJames Wright 134*3fc405b4SJames Wright CeedScalar old_bounds[6][2] = {{0}}; 135*3fc405b4SJames Wright for (int j = 0; j < 6; j++) old_bounds[j][1] = 1; 136*3fc405b4SJames Wright const CeedScalar(*new_bounds)[2] = (const CeedScalar(*)[2]) & sgsdd_ctx->data[sgsdd_ctx->offsets.out_scaling]; 137*3fc405b4SJames Wright DenormalizeDDOutputs(sgs_sframe_sym, new_bounds, old_bounds); 138*3fc405b4SJames Wright 139*3fc405b4SJames Wright // Re-dimensionalize sgs_stress 140*3fc405b4SJames Wright ScaleN(sgs_sframe_sym, Square(delta) * Square(grad_velo_magnitude), 6); 141*3fc405b4SJames Wright 142*3fc405b4SJames Wright CeedScalar sgs_stress[3][3] = {{0.}}; 143*3fc405b4SJames Wright { // Rotate SGS Stress back to physical frame, SGS_physical = E^T SGS_sframe E 144*3fc405b4SJames Wright CeedScalar Evec_sgs[3][3] = {{0.}}; 145*3fc405b4SJames Wright const CeedScalar sgs_sframe[3][3] = { 146*3fc405b4SJames Wright {sgs_sframe_sym[0], sgs_sframe_sym[3], sgs_sframe_sym[4]}, 147*3fc405b4SJames Wright {sgs_sframe_sym[3], sgs_sframe_sym[1], sgs_sframe_sym[5]}, 148*3fc405b4SJames Wright {sgs_sframe_sym[4], sgs_sframe_sym[5], sgs_sframe_sym[2]}, 149*3fc405b4SJames Wright }; 150*3fc405b4SJames Wright MatMat3(eigenvectors, sgs_sframe, CEED_TRANSPOSE, CEED_NOTRANSPOSE, Evec_sgs); 151*3fc405b4SJames Wright MatMat3(Evec_sgs, eigenvectors, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, sgs_stress); 152*3fc405b4SJames Wright } 153*3fc405b4SJames Wright 154*3fc405b4SJames Wright KMPack(sgs_stress, kmsgs_stress); 155*3fc405b4SJames Wright } 156*3fc405b4SJames Wright 15762b7942eSJames Wright #endif // sgs_dd_model_h 158