1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3952746efSJames Wright
4952746efSJames Wright /// @file
5952746efSJames Wright /// Structs and helper functions for data-driven subgrid-stress modeling
6952746efSJames Wright /// See 'Invariant data-driven subgrid stress modeling in the strain-rate eigenframe for large eddy simulation' 2022 and 'S-frame discrepancy
7952746efSJames Wright /// correction models for data-informed Reynolds stress closure' 2022
8c7ece6efSJeremy L Thompson #pragma once
9952746efSJames Wright
10*3e17a7a1SJames Wright #include <ceed/types.h>
11952746efSJames Wright
12952746efSJames Wright #include "newtonian_state.h"
13952746efSJames Wright #include "newtonian_types.h"
14952746efSJames Wright #include "utils.h"
15952746efSJames Wright #include "utils_eigensolver_jacobi.h"
16952746efSJames Wright
17952746efSJames Wright // @brief Calculate Frobenius norm of velocity gradient from eigenframe quantities
VelocityGradientMagnitude(const CeedScalar strain_sframe[3],const CeedScalar vorticity_sframe[3])18952746efSJames Wright CEED_QFUNCTION_HELPER CeedScalar VelocityGradientMagnitude(const CeedScalar strain_sframe[3], const CeedScalar vorticity_sframe[3]) {
19952746efSJames Wright return sqrt(Dot3(strain_sframe, strain_sframe) + 0.5 * Dot3(vorticity_sframe, vorticity_sframe));
20952746efSJames Wright };
21952746efSJames Wright
22952746efSJames Wright // @brief Change the order of basis vectors so that they align with vector and obey right-hand rule
23952746efSJames 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
24952746efSJames Wright // The basis vectors are assumed to form the rows of the basis matrix.
OrientBasisWithVector(CeedScalar basis[3][3],const CeedScalar vector[3])25952746efSJames Wright CEED_QFUNCTION_HELPER void OrientBasisWithVector(CeedScalar basis[3][3], const CeedScalar vector[3]) {
26952746efSJames Wright CeedScalar alignment[3] = {0.}, cross[3];
27952746efSJames Wright
28952746efSJames Wright MatVec3(basis, vector, CEED_NOTRANSPOSE, alignment);
29952746efSJames Wright
30952746efSJames Wright if (alignment[0] < 0) ScaleN(basis[0], -1, 3);
31952746efSJames Wright if (alignment[2] < 0) ScaleN(basis[2], -1, 3);
32952746efSJames Wright
33952746efSJames Wright Cross3(basis[2], basis[0], cross);
34952746efSJames Wright CeedScalar basis_1_orientation = Dot3(cross, basis[1]);
35952746efSJames Wright if (basis_1_orientation < 0) ScaleN(basis[1], -1, 3);
36952746efSJames Wright }
37952746efSJames Wright
382679b3cdSJames Wright // @brief Denormalize outputs using min-max (de-)normalization
DenormalizeDDOutputs(CeedScalar output[6],const CeedScalar new_bounds[6][2],const CeedScalar old_bounds[6][2])39be75532aSJames Wright CEED_QFUNCTION_HELPER void DenormalizeDDOutputs(CeedScalar output[6], const CeedScalar new_bounds[6][2], const CeedScalar old_bounds[6][2]) {
402679b3cdSJames Wright CeedScalar bounds_ratio;
412679b3cdSJames Wright for (int i = 0; i < 6; i++) {
422679b3cdSJames Wright bounds_ratio = (new_bounds[i][1] - new_bounds[i][0]) / (old_bounds[i][1] - old_bounds[i][0]);
432679b3cdSJames Wright output[i] = bounds_ratio * (output[i] - old_bounds[i][1]) + new_bounds[i][1];
442679b3cdSJames Wright }
452679b3cdSJames Wright }
462679b3cdSJames Wright
47952746efSJames Wright /**
48952746efSJames Wright * @brief Compute model inputs for anisotropic data-driven model
49952746efSJames Wright *
50952746efSJames Wright * @param[in] grad_velo_aniso Gradient of velocity in physical (anisotropic) coordinates
51952746efSJames Wright * @param[in] km_A_ij Anisotropy tensor, in Kelvin-Mandel notation
52952746efSJames Wright * @param[in] delta Length used to create anisotropy tensor
53952746efSJames Wright * @param[in] viscosity Kinematic viscosity
54952746efSJames Wright * @param[out] eigenvectors Eigenvectors of the (anisotropic) velocity gradient
55952746efSJames Wright * @param[out] inputs Data-driven model inputs
56952746efSJames Wright * @param[out] grad_velo_magnitude Frobenius norm of the velocity gradient
57952746efSJames Wright */
ComputeSgsDDInputs(const CeedScalar grad_velo_aniso[3][3],const CeedScalar km_A_ij[6],const CeedScalar delta,const CeedScalar viscosity,CeedScalar eigenvectors[3][3],CeedScalar inputs[6],CeedScalar * grad_velo_magnitude)58ad494f68SJames Wright CEED_QFUNCTION_HELPER void ComputeSgsDDInputs(const CeedScalar grad_velo_aniso[3][3], const CeedScalar km_A_ij[6], const CeedScalar delta,
59952746efSJames Wright const CeedScalar viscosity, CeedScalar eigenvectors[3][3], CeedScalar inputs[6],
60952746efSJames Wright CeedScalar *grad_velo_magnitude) {
61952746efSJames Wright CeedScalar strain_sframe[3] = {0.}, vorticity_sframe[3] = {0.};
62952746efSJames Wright CeedScalar A_ij[3][3] = {{0.}}, grad_velo_iso[3][3] = {{0.}};
63952746efSJames Wright
64952746efSJames Wright // -- Transform physical, anisotropic velocity gradient to isotropic
65952746efSJames Wright KMUnpack(km_A_ij, A_ij);
66952746efSJames Wright MatMat3(grad_velo_aniso, A_ij, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, grad_velo_iso);
67952746efSJames Wright
68952746efSJames Wright { // -- Get Eigenframe
69952746efSJames Wright CeedScalar kmstrain_iso[6], strain_iso[3][3];
70952746efSJames Wright CeedInt work_vector[3] = {0};
71952746efSJames Wright KMStrainRate(grad_velo_iso, kmstrain_iso);
72952746efSJames Wright KMUnpack(kmstrain_iso, strain_iso);
73952746efSJames Wright Diagonalize3(strain_iso, strain_sframe, eigenvectors, work_vector, SORT_DECREASING_EVALS, true, 5);
74952746efSJames Wright }
75952746efSJames Wright
76952746efSJames Wright { // -- Get vorticity in S-frame
77952746efSJames Wright CeedScalar rotation_iso[3][3];
78952746efSJames Wright RotationRate(grad_velo_iso, rotation_iso);
79952746efSJames Wright CeedScalar vorticity_iso[3] = {-2 * rotation_iso[1][2], 2 * rotation_iso[0][2], -2 * rotation_iso[0][1]};
80952746efSJames Wright OrientBasisWithVector(eigenvectors, vorticity_iso);
81952746efSJames Wright MatVec3(eigenvectors, vorticity_iso, CEED_NOTRANSPOSE, vorticity_sframe);
82952746efSJames Wright }
83952746efSJames Wright
84952746efSJames Wright // -- Calculate DD model inputs
85952746efSJames Wright *grad_velo_magnitude = VelocityGradientMagnitude(strain_sframe, vorticity_sframe);
86952746efSJames Wright inputs[0] = strain_sframe[0];
87952746efSJames Wright inputs[1] = strain_sframe[1];
88952746efSJames Wright inputs[2] = strain_sframe[2];
89952746efSJames Wright inputs[3] = vorticity_sframe[0];
90952746efSJames Wright inputs[4] = vorticity_sframe[1];
91952746efSJames Wright inputs[5] = viscosity / Square(delta);
92952746efSJames Wright ScaleN(inputs, 1 / (*grad_velo_magnitude + CEED_EPSILON), 6);
93952746efSJames Wright }
94952746efSJames Wright
952679b3cdSJames Wright /**
962679b3cdSJames Wright * @brief Compute the physical SGS stresses from the neural-network output
972679b3cdSJames Wright *
982679b3cdSJames Wright * @param[in,out] outputs Outputs from the neural-network
992679b3cdSJames Wright * @param[in] delta Length used to create anisotropy tensor
1002679b3cdSJames Wright * @param[in] eigenvectors Eigenvectors of the (anisotropic) velocity gradient
1012679b3cdSJames Wright * @param[in] new_bounds Bounds used for min-max de-normalization
1022679b3cdSJames Wright * @param[in] grad_velo_magnitude Magnitude of the velocity gradient
1032679b3cdSJames Wright * @param[out] kmsgs_stress Physical SGS stresses in Kelvin-Mandel notation
1042679b3cdSJames Wright */
ComputeSgsDDOutputs(CeedScalar outputs[6],const CeedScalar delta,const CeedScalar eigenvectors[3][3],const CeedScalar new_bounds[6][2],const CeedScalar grad_velo_magnitude,CeedScalar kmsgs_stress[6])105ad494f68SJames Wright CEED_QFUNCTION_HELPER void ComputeSgsDDOutputs(CeedScalar outputs[6], const CeedScalar delta, const CeedScalar eigenvectors[3][3],
106ad494f68SJames Wright const CeedScalar new_bounds[6][2], const CeedScalar grad_velo_magnitude, CeedScalar kmsgs_stress[6]) {
1072679b3cdSJames Wright CeedScalar old_bounds[6][2] = {{0}};
1082679b3cdSJames Wright for (int j = 0; j < 6; j++) old_bounds[j][1] = 1;
1092679b3cdSJames Wright DenormalizeDDOutputs(outputs, new_bounds, old_bounds);
1102679b3cdSJames Wright
1112679b3cdSJames Wright // Re-dimensionalize sgs_stress
1122679b3cdSJames Wright ScaleN(outputs, Square(delta) * Square(grad_velo_magnitude), 6);
1132679b3cdSJames Wright
1142679b3cdSJames Wright CeedScalar sgs_stress[3][3] = {{0.}};
1152679b3cdSJames Wright { // Rotate SGS Stress back to physical frame, SGS_physical = E^T SGS_sframe E
1162679b3cdSJames Wright CeedScalar Evec_sgs[3][3] = {{0.}};
1172679b3cdSJames Wright const CeedScalar sgs_sframe[3][3] = {
1182679b3cdSJames Wright {outputs[0], outputs[3], outputs[4]},
1192679b3cdSJames Wright {outputs[3], outputs[1], outputs[5]},
1202679b3cdSJames Wright {outputs[4], outputs[5], outputs[2]},
1212679b3cdSJames Wright };
1222679b3cdSJames Wright MatMat3(eigenvectors, sgs_sframe, CEED_TRANSPOSE, CEED_NOTRANSPOSE, Evec_sgs);
1232679b3cdSJames Wright MatMat3(Evec_sgs, eigenvectors, CEED_NOTRANSPOSE, CEED_NOTRANSPOSE, sgs_stress);
1242679b3cdSJames Wright }
1252679b3cdSJames Wright
1262679b3cdSJames Wright KMPack(sgs_stress, kmsgs_stress);
1272679b3cdSJames Wright }
128