1ba6664aeSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2ba6664aeSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3ba6664aeSJames Wright // 4ba6664aeSJames Wright // SPDX-License-Identifier: BSD-2-Clause 5ba6664aeSJames Wright // 6ba6664aeSJames Wright // This file is part of CEED: http://github.com/ceed 7ba6664aeSJames Wright 8ba6664aeSJames Wright /// @file 9ba6664aeSJames Wright /// Implementation of the Synthetic Turbulence Generation (STG) algorithm 10ba6664aeSJames Wright /// presented in Shur et al. 2014 11ba6664aeSJames Wright 122b730f8bSJeremy L Thompson #include "stg_shur14.h" 132b730f8bSJeremy L Thompson 14ba6664aeSJames Wright #include <math.h> 15ba6664aeSJames Wright #include <petsc.h> 162b730f8bSJeremy L Thompson #include <stdlib.h> 172b730f8bSJeremy L Thompson 18ba6664aeSJames Wright #include "../navierstokes.h" 19ba6664aeSJames Wright #include "../qfunctions/stg_shur14.h" 20ba6664aeSJames Wright 2165dd5cafSJames Wright STGShur14Context global_stg_ctx; 2265dd5cafSJames Wright 23ba6664aeSJames Wright /* 24ba6664aeSJames Wright * @brief Perform Cholesky decomposition on array of symmetric 3x3 matrices 25ba6664aeSJames Wright * 26*ea61e9acSJeremy L Thompson * This assumes the input matrices are in order [11,22,33,12,13,23]. 27*ea61e9acSJeremy L Thompson * This format is also used for the output. 28ba6664aeSJames Wright * 29ba6664aeSJames Wright * @param[in] comm MPI_Comm 30ba6664aeSJames Wright * @param[in] nprofs Number of matrices in Rij 31ba6664aeSJames Wright * @param[in] Rij Array of the symmetric matrices [6,nprofs] 32ba6664aeSJames Wright * @param[out] Cij Array of the Cholesky Decomposition matrices, [6,nprofs] 33ba6664aeSJames Wright */ 342b730f8bSJeremy L Thompson PetscErrorCode CalcCholeskyDecomp(MPI_Comm comm, PetscInt nprofs, const CeedScalar Rij[6][nprofs], CeedScalar Cij[6][nprofs]) { 35ba6664aeSJames Wright PetscFunctionBeginUser; 36ba6664aeSJames Wright for (PetscInt i = 0; i < nprofs; i++) { 37ba6664aeSJames Wright Cij[0][i] = sqrt(Rij[0][i]); 38ba6664aeSJames Wright Cij[3][i] = Rij[3][i] / Cij[0][i]; 39ba6664aeSJames Wright Cij[1][i] = sqrt(Rij[1][i] - pow(Cij[3][i], 2)); 40ba6664aeSJames Wright Cij[4][i] = Rij[4][i] / Cij[0][i]; 41ba6664aeSJames Wright Cij[5][i] = (Rij[5][i] - Cij[3][i] * Cij[4][i]) / Cij[1][i]; 42ba6664aeSJames Wright Cij[2][i] = sqrt(Rij[2][i] - pow(Cij[4][i], 2) - pow(Cij[5][i], 2)); 43ba6664aeSJames Wright 442b730f8bSJeremy L Thompson if (isnan(Cij[0][i]) || isnan(Cij[1][i]) || isnan(Cij[2][i])) { 452b730f8bSJeremy L Thompson SETERRQ(comm, -1, "Cholesky decomposition failed at profile point %" PetscInt_FMT ". Either STGInflow has non-SPD matrix or contains nan.", 462b730f8bSJeremy L Thompson i + 1); 472b730f8bSJeremy L Thompson } 48ba6664aeSJames Wright } 49ba6664aeSJames Wright PetscFunctionReturn(0); 50ba6664aeSJames Wright } 51ba6664aeSJames Wright 52ba6664aeSJames Wright /* 53ba6664aeSJames Wright * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer 54ba6664aeSJames Wright * 55*ea61e9acSJeremy L Thompson * This function opens the file specified by `path` using `PetscFOpen` and passes the file pointer in `fp`. 56*ea61e9acSJeremy L Thompson * It is not closed in this function, thus `fp` must be closed sometime after this function has been called (using `PetscFClose` for example). 57ba6664aeSJames Wright * 58*ea61e9acSJeremy L Thompson * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space. 59ba6664aeSJames Wright * 60ba6664aeSJames Wright * @param[in] comm MPI_Comm for the program 61ba6664aeSJames Wright * @param[in] path Path to the file 62ba6664aeSJames Wright * @param[in] char_array_len Length of the character array that should contain each line 63ba6664aeSJames Wright * @param[out] dims Dimensions of the file, taken from the first line of the file 64ba6664aeSJames Wright * @param[out] fp File pointer to the opened file 65ba6664aeSJames Wright */ 662b730f8bSJeremy L Thompson static PetscErrorCode OpenPHASTADatFile(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, PetscInt dims[2], 672b730f8bSJeremy L Thompson FILE **fp) { 68ba6664aeSJames Wright PetscInt ndims; 69ba6664aeSJames Wright char line[char_array_len]; 70ba6664aeSJames Wright char **array; 71ba6664aeSJames Wright 72ba6664aeSJames Wright PetscFunctionBeginUser; 7324941a69SJames Wright PetscCall(PetscFOpen(comm, path, "r", fp)); 7424941a69SJames Wright PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line)); 7524941a69SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 762b730f8bSJeremy L Thompson if (ndims != 2) { 772b730f8bSJeremy L Thompson SETERRQ(comm, -1, "Found %" PetscInt_FMT " dimensions instead of 2 on the first line of %s", ndims, path); 782b730f8bSJeremy L Thompson } 79ba6664aeSJames Wright 80ba6664aeSJames Wright for (PetscInt i = 0; i < ndims; i++) dims[i] = atoi(array[i]); 8124941a69SJames Wright PetscCall(PetscStrToArrayDestroy(ndims, array)); 822b730f8bSJeremy L Thompson 83ba6664aeSJames Wright PetscFunctionReturn(0); 84ba6664aeSJames Wright } 85ba6664aeSJames Wright 86ba6664aeSJames Wright /* 87*ea61e9acSJeremy L Thompson * @brief Get the number of rows for the PHASTA file at path. 88ba6664aeSJames Wright * 89*ea61e9acSJeremy L Thompson * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space. 90ba6664aeSJames Wright * 91ba6664aeSJames Wright * @param[in] comm MPI_Comm for the program 92ba6664aeSJames Wright * @param[in] path Path to the file 93ba6664aeSJames Wright * @param[out] nrows Number of rows 94ba6664aeSJames Wright */ 952b730f8bSJeremy L Thompson static PetscErrorCode GetNRows(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) { 96ba6664aeSJames Wright const PetscInt char_array_len = 512; 97ba6664aeSJames Wright PetscInt dims[2]; 98ba6664aeSJames Wright FILE *fp; 99ba6664aeSJames Wright 100ba6664aeSJames Wright PetscFunctionBeginUser; 10124941a69SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 102ba6664aeSJames Wright *nrows = dims[0]; 10324941a69SJames Wright PetscCall(PetscFClose(comm, fp)); 1042b730f8bSJeremy L Thompson 105ba6664aeSJames Wright PetscFunctionReturn(0); 106ba6664aeSJames Wright } 107ba6664aeSJames Wright 108ba6664aeSJames Wright /* 109ba6664aeSJames Wright * @brief Read the STGInflow file and load the contents into stg_ctx 110ba6664aeSJames Wright * 111*ea61e9acSJeremy L Thompson * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space. 112*ea61e9acSJeremy L Thompson * Assumes there are 14 columns in the file. 113ba6664aeSJames Wright * 114*ea61e9acSJeremy L Thompson * Function calculates the Cholesky decomposition from the Reynolds stress profile found in the file. 115ba6664aeSJames Wright * 116ba6664aeSJames Wright * @param[in] comm MPI_Comm for the program 117ba6664aeSJames Wright * @param[in] path Path to the STGInflow.dat file 118*ea61e9acSJeremy L Thompson * @param[in,out] stg_ctx STGShur14Context where the data will be loaded into 119ba6664aeSJames Wright */ 1202b730f8bSJeremy L Thompson static PetscErrorCode ReadSTGInflow(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], STGShur14Context stg_ctx) { 121ba6664aeSJames Wright PetscInt ndims, dims[2]; 122ba6664aeSJames Wright FILE *fp; 123ba6664aeSJames Wright const PetscInt char_array_len = 512; 124ba6664aeSJames Wright char line[char_array_len]; 125ba6664aeSJames Wright char **array; 126ba6664aeSJames Wright 127ba6664aeSJames Wright PetscFunctionBeginUser; 128ba6664aeSJames Wright 12924941a69SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 130ba6664aeSJames Wright 131ba6664aeSJames Wright CeedScalar rij[6][stg_ctx->nprofs]; 132175f00a6SJames Wright CeedScalar *wall_dist = &stg_ctx->data[stg_ctx->offsets.wall_dist]; 133ba6664aeSJames Wright CeedScalar *eps = &stg_ctx->data[stg_ctx->offsets.eps]; 134ba6664aeSJames Wright CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt]; 1352b730f8bSJeremy L Thompson CeedScalar(*ubar)[stg_ctx->nprofs] = (CeedScalar(*)[stg_ctx->nprofs]) & stg_ctx->data[stg_ctx->offsets.ubar]; 136ba6664aeSJames Wright 137ba6664aeSJames Wright for (PetscInt i = 0; i < stg_ctx->nprofs; i++) { 13824941a69SJames Wright PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 13924941a69SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 1402b730f8bSJeremy L Thompson if (ndims < dims[1]) { 1412b730f8bSJeremy L Thompson SETERRQ(comm, -1, "Line %" PetscInt_FMT " of %s does not contain enough columns (%" PetscInt_FMT " instead of %" PetscInt_FMT ")", i, path, 1422b730f8bSJeremy L Thompson ndims, dims[1]); 1432b730f8bSJeremy L Thompson } 144ba6664aeSJames Wright 145175f00a6SJames Wright wall_dist[i] = (CeedScalar)atof(array[0]); 146ba6664aeSJames Wright ubar[0][i] = (CeedScalar)atof(array[1]); 147ba6664aeSJames Wright ubar[1][i] = (CeedScalar)atof(array[2]); 148ba6664aeSJames Wright ubar[2][i] = (CeedScalar)atof(array[3]); 149ba6664aeSJames Wright rij[0][i] = (CeedScalar)atof(array[4]); 150ba6664aeSJames Wright rij[1][i] = (CeedScalar)atof(array[5]); 151ba6664aeSJames Wright rij[2][i] = (CeedScalar)atof(array[6]); 152ba6664aeSJames Wright rij[3][i] = (CeedScalar)atof(array[7]); 153ba6664aeSJames Wright rij[4][i] = (CeedScalar)atof(array[8]); 154ba6664aeSJames Wright rij[5][i] = (CeedScalar)atof(array[9]); 155ba6664aeSJames Wright lt[i] = (CeedScalar)atof(array[12]); 156ba6664aeSJames Wright eps[i] = (CeedScalar)atof(array[13]); 157ba6664aeSJames Wright 1582b730f8bSJeremy L Thompson if (wall_dist[i] < 0) SETERRQ(comm, -1, "Distance to wall in %s cannot be negative", path); 1592b730f8bSJeremy L Thompson if (lt[i] < 0) SETERRQ(comm, -1, "Turbulent length scale in %s cannot be negative", path); 1602b730f8bSJeremy L Thompson if (eps[i] < 0) SETERRQ(comm, -1, "Turbulent dissipation in %s cannot be negative", path); 161ba6664aeSJames Wright } 1622b730f8bSJeremy L Thompson CeedScalar(*cij)[stg_ctx->nprofs] = (CeedScalar(*)[stg_ctx->nprofs]) & stg_ctx->data[stg_ctx->offsets.cij]; 16324941a69SJames Wright PetscCall(CalcCholeskyDecomp(comm, stg_ctx->nprofs, rij, cij)); 16424941a69SJames Wright PetscCall(PetscFClose(comm, fp)); 1652b730f8bSJeremy L Thompson 166ba6664aeSJames Wright PetscFunctionReturn(0); 167ba6664aeSJames Wright } 168ba6664aeSJames Wright 169ba6664aeSJames Wright /* 170ba6664aeSJames Wright * @brief Read the STGRand file and load the contents into stg_ctx 171ba6664aeSJames Wright * 172*ea61e9acSJeremy L Thompson * Assumes that the first line of the file has the number of rows and columns as the only two entries, separated by a single space. 173*ea61e9acSJeremy L Thompson * Assumes there are 7 columns in the file. 174ba6664aeSJames Wright * 175ba6664aeSJames Wright * @param[in] comm MPI_Comm for the program 176ba6664aeSJames Wright * @param[in] path Path to the STGRand.dat file 177*ea61e9acSJeremy L Thompson * @param[in,out] stg_ctx STGShur14Context where the data will be loaded into 178ba6664aeSJames Wright */ 1792b730f8bSJeremy L Thompson static PetscErrorCode ReadSTGRand(const MPI_Comm comm, const char path[PETSC_MAX_PATH_LEN], STGShur14Context stg_ctx) { 180ba6664aeSJames Wright PetscInt ndims, dims[2]; 181ba6664aeSJames Wright FILE *fp; 182ba6664aeSJames Wright const PetscInt char_array_len = 512; 183ba6664aeSJames Wright char line[char_array_len]; 184ba6664aeSJames Wright char **array; 185ba6664aeSJames Wright 186ba6664aeSJames Wright PetscFunctionBeginUser; 18724941a69SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 188ba6664aeSJames Wright 189ba6664aeSJames Wright CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi]; 1902b730f8bSJeremy L Thompson CeedScalar(*d)[stg_ctx->nmodes] = (CeedScalar(*)[stg_ctx->nmodes]) & stg_ctx->data[stg_ctx->offsets.d]; 1912b730f8bSJeremy L Thompson CeedScalar(*sigma)[stg_ctx->nmodes] = (CeedScalar(*)[stg_ctx->nmodes]) & stg_ctx->data[stg_ctx->offsets.sigma]; 192ba6664aeSJames Wright 193ba6664aeSJames Wright for (PetscInt i = 0; i < stg_ctx->nmodes; i++) { 19424941a69SJames Wright PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 19524941a69SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 1962b730f8bSJeremy L Thompson if (ndims < dims[1]) { 1972b730f8bSJeremy L Thompson SETERRQ(comm, -1, "Line %" PetscInt_FMT " of %s does not contain enough columns (%" PetscInt_FMT " instead of %" PetscInt_FMT ")", i, path, 1982b730f8bSJeremy L Thompson ndims, dims[1]); 1992b730f8bSJeremy L Thompson } 200ba6664aeSJames Wright 201ba6664aeSJames Wright d[0][i] = (CeedScalar)atof(array[0]); 202ba6664aeSJames Wright d[1][i] = (CeedScalar)atof(array[1]); 203ba6664aeSJames Wright d[2][i] = (CeedScalar)atof(array[2]); 204ba6664aeSJames Wright phi[i] = (CeedScalar)atof(array[3]); 205ba6664aeSJames Wright sigma[0][i] = (CeedScalar)atof(array[4]); 206ba6664aeSJames Wright sigma[1][i] = (CeedScalar)atof(array[5]); 207ba6664aeSJames Wright sigma[2][i] = (CeedScalar)atof(array[6]); 208ba6664aeSJames Wright } 20924941a69SJames Wright PetscCall(PetscFClose(comm, fp)); 2102b730f8bSJeremy L Thompson 211ba6664aeSJames Wright PetscFunctionReturn(0); 212ba6664aeSJames Wright } 213ba6664aeSJames Wright 214ba6664aeSJames Wright /* 215ba6664aeSJames Wright * @brief Read STG data from input paths and put in STGShur14Context 216ba6664aeSJames Wright * 217ba6664aeSJames Wright * Reads data from input paths and puts them into a STGShur14Context object. 218*ea61e9acSJeremy L Thompson * Data stored initially in `*pstg_ctx` will be copied over to the new STGShur14Context instance. 219ba6664aeSJames Wright * 220ba6664aeSJames Wright * @param[in] comm MPI_Comm for the program 221ba6664aeSJames Wright * @param[in] dm DM for the program 222ba6664aeSJames Wright * @param[in] stg_inflow_path Path to STGInflow.dat file 223ba6664aeSJames Wright * @param[in] stg_rand_path Path to STGRand.dat file 224*ea61e9acSJeremy L Thompson * @param[in,out] pstg_ctx Pointer to STGShur14Context where the data will be loaded into 225ba6664aeSJames Wright */ 2262b730f8bSJeremy L Thompson PetscErrorCode GetSTGContextData(const MPI_Comm comm, const DM dm, char stg_inflow_path[PETSC_MAX_PATH_LEN], char stg_rand_path[PETSC_MAX_PATH_LEN], 2272b730f8bSJeremy L Thompson STGShur14Context *pstg_ctx, const CeedScalar ynodes[]) { 228ba6664aeSJames Wright PetscInt nmodes, nprofs; 229ba6664aeSJames Wright STGShur14Context stg_ctx; 230ba6664aeSJames Wright PetscFunctionBeginUser; 231ba6664aeSJames Wright 232ba6664aeSJames Wright // Get options 23324941a69SJames Wright PetscCall(GetNRows(comm, stg_rand_path, &nmodes)); 23424941a69SJames Wright PetscCall(GetNRows(comm, stg_inflow_path, &nprofs)); 235ba6664aeSJames Wright if (nmodes > STG_NMODES_MAX) 2362b730f8bSJeremy L Thompson SETERRQ(comm, 1, 2372b730f8bSJeremy L Thompson "Number of wavemodes in %s (%" PetscInt_FMT ") exceeds STG_NMODES_MAX (%" PetscInt_FMT 2382b730f8bSJeremy L Thompson "). " 2392b730f8bSJeremy L Thompson "Change size of STG_NMODES_MAX and recompile", 2402b730f8bSJeremy L Thompson stg_rand_path, nmodes, STG_NMODES_MAX); 241ba6664aeSJames Wright 242ba6664aeSJames Wright { 243ba6664aeSJames Wright STGShur14Context s; 24424941a69SJames Wright PetscCall(PetscCalloc1(1, &s)); 245ba6664aeSJames Wright *s = **pstg_ctx; 246ba6664aeSJames Wright s->nmodes = nmodes; 247ba6664aeSJames Wright s->nprofs = nprofs; 248ba6664aeSJames Wright s->offsets.sigma = 0; 249ba6664aeSJames Wright s->offsets.d = nmodes * 3; 250ba6664aeSJames Wright s->offsets.phi = s->offsets.d + nmodes * 3; 251ba6664aeSJames Wright s->offsets.kappa = s->offsets.phi + nmodes; 252175f00a6SJames Wright s->offsets.wall_dist = s->offsets.kappa + nmodes; 253175f00a6SJames Wright s->offsets.ubar = s->offsets.wall_dist + nprofs; 254ba6664aeSJames Wright s->offsets.cij = s->offsets.ubar + nprofs * 3; 255ba6664aeSJames Wright s->offsets.eps = s->offsets.cij + nprofs * 6; 256ba6664aeSJames Wright s->offsets.lt = s->offsets.eps + nprofs; 2574e139266SJames Wright s->offsets.ynodes = s->offsets.lt + nprofs; 2584e139266SJames Wright PetscInt total_num_scalars = s->offsets.ynodes + s->nynodes; 259ba6664aeSJames Wright s->total_bytes = sizeof(*stg_ctx) + total_num_scalars * sizeof(stg_ctx->data[0]); 26024941a69SJames Wright PetscCall(PetscMalloc(s->total_bytes, &stg_ctx)); 261ba6664aeSJames Wright *stg_ctx = *s; 26224941a69SJames Wright PetscCall(PetscFree(s)); 263ba6664aeSJames Wright } 264ba6664aeSJames Wright 26524941a69SJames Wright PetscCall(ReadSTGInflow(comm, stg_inflow_path, stg_ctx)); 26624941a69SJames Wright PetscCall(ReadSTGRand(comm, stg_rand_path, stg_ctx)); 267ba6664aeSJames Wright 2684e139266SJames Wright if (stg_ctx->nynodes > 0) { 2694e139266SJames Wright CeedScalar *ynodes_ctx = &stg_ctx->data[stg_ctx->offsets.ynodes]; 2704e139266SJames Wright for (PetscInt i = 0; i < stg_ctx->nynodes; i++) ynodes_ctx[i] = ynodes[i]; 2714e139266SJames Wright } 2724e139266SJames Wright 273ba6664aeSJames Wright // -- Calculate kappa 274ba6664aeSJames Wright { 275ba6664aeSJames Wright CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 276175f00a6SJames Wright CeedScalar *wall_dist = &stg_ctx->data[stg_ctx->offsets.wall_dist]; 277ba6664aeSJames Wright CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt]; 278ba6664aeSJames Wright CeedScalar le, le_max = 0; 279ba6664aeSJames Wright 2802b730f8bSJeremy L Thompson CeedPragmaSIMD for (PetscInt i = 0; i < stg_ctx->nprofs; i++) { 281175f00a6SJames Wright le = PetscMin(2 * wall_dist[i], 3 * lt[i]); 282ba6664aeSJames Wright if (le_max < le) le_max = le; 283ba6664aeSJames Wright } 284ba6664aeSJames Wright CeedScalar kmin = M_PI / le_max; 285ba6664aeSJames Wright 2862b730f8bSJeremy L Thompson CeedPragmaSIMD for (PetscInt i = 0; i < stg_ctx->nmodes; i++) { kappa[i] = kmin * pow(stg_ctx->alpha, i); } 287ba6664aeSJames Wright } // end calculate kappa 288ba6664aeSJames Wright 28924941a69SJames Wright PetscCall(PetscFree(*pstg_ctx)); 290ba6664aeSJames Wright *pstg_ctx = stg_ctx; 291ba6664aeSJames Wright PetscFunctionReturn(0); 292ba6664aeSJames Wright } 293ba6664aeSJames Wright 2942b730f8bSJeremy L Thompson PetscErrorCode SetupSTG(const MPI_Comm comm, const DM dm, ProblemData *problem, User user, const bool prescribe_T, const CeedScalar theta0, 2952b730f8bSJeremy L Thompson const CeedScalar P0, const CeedScalar ynodes[], const CeedInt nynodes) { 296ba6664aeSJames Wright char stg_inflow_path[PETSC_MAX_PATH_LEN] = "./STGInflow.dat"; 297ba6664aeSJames Wright char stg_rand_path[PETSC_MAX_PATH_LEN] = "./STGRand.dat"; 2982b730f8bSJeremy L Thompson PetscBool mean_only = PETSC_FALSE, use_stgstrong = PETSC_FALSE, use_fluctuating_IC = PETSC_FALSE; 2992b730f8bSJeremy L Thompson CeedScalar u0 = 0.0, alpha = 1.01; 300ba6664aeSJames Wright CeedQFunctionContext stg_context; 301ba6664aeSJames Wright NewtonianIdealGasContext newtonian_ig_ctx; 302ba6664aeSJames Wright PetscFunctionBeginUser; 303ba6664aeSJames Wright 304ba6664aeSJames Wright // Get options 305ba6664aeSJames Wright PetscOptionsBegin(comm, NULL, "STG Boundary Condition Options", NULL); 3062b730f8bSJeremy L Thompson PetscCall(PetscOptionsString("-stg_inflow_path", "Path to STGInflow.dat", NULL, stg_inflow_path, stg_inflow_path, sizeof(stg_inflow_path), NULL)); 3072b730f8bSJeremy L Thompson PetscCall(PetscOptionsString("-stg_rand_path", "Path to STGInflow.dat", NULL, stg_rand_path, stg_rand_path, sizeof(stg_rand_path), NULL)); 3082b730f8bSJeremy L Thompson PetscCall(PetscOptionsReal("-stg_alpha", "Growth rate of the wavemodes", NULL, alpha, &alpha, NULL)); 3092b730f8bSJeremy L Thompson PetscCall(PetscOptionsReal("-stg_u0", "Advective velocity for the fluctuations", NULL, u0, &u0, NULL)); 3102b730f8bSJeremy L Thompson PetscCall(PetscOptionsBool("-stg_mean_only", "Only apply mean profile", NULL, mean_only, &mean_only, NULL)); 3112b730f8bSJeremy L Thompson PetscCall(PetscOptionsBool("-stg_strong", "Enforce STG inflow strongly", NULL, use_stgstrong, &use_stgstrong, NULL)); 3122b730f8bSJeremy L Thompson PetscCall(PetscOptionsBool("-stg_fluctuating_IC", "\"Extrude\" the fluctuations through the domain as an initial condition", NULL, 3132b730f8bSJeremy L Thompson use_fluctuating_IC, &use_fluctuating_IC, NULL)); 314ba6664aeSJames Wright PetscOptionsEnd(); 315ba6664aeSJames Wright 31624941a69SJames Wright PetscCall(PetscCalloc1(1, &global_stg_ctx)); 31765dd5cafSJames Wright global_stg_ctx->alpha = alpha; 31865dd5cafSJames Wright global_stg_ctx->u0 = u0; 31965dd5cafSJames Wright global_stg_ctx->is_implicit = user->phys->implicit; 32065dd5cafSJames Wright global_stg_ctx->prescribe_T = prescribe_T; 32165dd5cafSJames Wright global_stg_ctx->mean_only = mean_only; 32289060322SJames Wright global_stg_ctx->use_fluctuating_IC = use_fluctuating_IC; 32365dd5cafSJames Wright global_stg_ctx->theta0 = theta0; 32465dd5cafSJames Wright global_stg_ctx->P0 = P0; 32565dd5cafSJames Wright global_stg_ctx->nynodes = nynodes; 326ba6664aeSJames Wright 327ba6664aeSJames Wright { 328ba6664aeSJames Wright // Calculate dx assuming constant spacing 329ba6664aeSJames Wright PetscReal domain_min[3], domain_max[3], domain_size[3]; 33024941a69SJames Wright PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 331ba6664aeSJames Wright for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 332ba6664aeSJames Wright 333ba6664aeSJames Wright PetscInt nmax = 3, faces[3]; 3342b730f8bSJeremy L Thompson PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", faces, &nmax, NULL)); 33565dd5cafSJames Wright global_stg_ctx->dx = domain_size[0] / faces[0]; 33665dd5cafSJames Wright global_stg_ctx->dz = domain_size[2] / faces[2]; 337ba6664aeSJames Wright } 338ba6664aeSJames Wright 3392b730f8bSJeremy L Thompson CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ig_ctx); 34065dd5cafSJames Wright global_stg_ctx->newtonian_ctx = *newtonian_ig_ctx; 3412b730f8bSJeremy L Thompson CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ig_ctx); 342ba6664aeSJames Wright 3432b730f8bSJeremy L Thompson PetscCall(GetSTGContextData(comm, dm, stg_inflow_path, stg_rand_path, &global_stg_ctx, ynodes)); 344ba6664aeSJames Wright 345ba6664aeSJames Wright CeedQFunctionContextCreate(user->ceed, &stg_context); 3462b730f8bSJeremy L Thompson CeedQFunctionContextSetData(stg_context, CEED_MEM_HOST, CEED_USE_POINTER, global_stg_ctx->total_bytes, global_stg_ctx); 3472b730f8bSJeremy L Thompson CeedQFunctionContextSetDataDestroy(stg_context, CEED_MEM_HOST, FreeContextPetsc); 3482b730f8bSJeremy L Thompson CeedQFunctionContextRegisterDouble(stg_context, "solution time", offsetof(struct STGShur14Context_, time), 1, "Physical time of the solution"); 349ba6664aeSJames Wright 350b77c53c9SJames Wright CeedQFunctionContextDestroy(&problem->ics.qfunction_context); 351b77c53c9SJames Wright problem->ics.qfunction = ICsSTG; 352b77c53c9SJames Wright problem->ics.qfunction_loc = ICsSTG_loc; 353b77c53c9SJames Wright problem->ics.qfunction_context = stg_context; 354b77c53c9SJames Wright 3554e139266SJames Wright if (use_stgstrong) { 35665dd5cafSJames Wright // Use default boundary integral QF (BoundaryIntegral) in newtonian.h 357dada6cc0SJames Wright problem->use_dirichlet_ceed = PETSC_TRUE; 35836b31e27SJames Wright problem->bc_from_ics = PETSC_FALSE; 3594e139266SJames Wright } else { 360ba6664aeSJames Wright problem->apply_inflow.qfunction = STGShur14_Inflow; 361ba6664aeSJames Wright problem->apply_inflow.qfunction_loc = STGShur14_Inflow_loc; 3624dbab5e5SJames Wright problem->apply_inflow_jacobian.qfunction = STGShur14_Inflow_Jacobian; 3634dbab5e5SJames Wright problem->apply_inflow_jacobian.qfunction_loc = STGShur14_Inflow_Jacobian_loc; 3642b730f8bSJeremy L Thompson CeedQFunctionContextReferenceCopy(stg_context, &problem->apply_inflow.qfunction_context); 3652b730f8bSJeremy L Thompson CeedQFunctionContextReferenceCopy(stg_context, &problem->apply_inflow_jacobian.qfunction_context); 36636b31e27SJames Wright problem->bc_from_ics = PETSC_TRUE; 3674e139266SJames Wright } 368ba6664aeSJames Wright 369ba6664aeSJames Wright PetscFunctionReturn(0); 370ba6664aeSJames Wright } 3714e139266SJames Wright 3722b730f8bSJeremy L Thompson static inline PetscScalar FindDy(const PetscScalar ynodes[], const PetscInt nynodes, const PetscScalar y) { 3734e139266SJames Wright const PetscScalar half_mindy = 0.5 * (ynodes[1] - ynodes[0]); 3744e139266SJames Wright // ^^assuming min(dy) is first element off the wall 3754e139266SJames Wright PetscInt idx = -1; // Index 3764e139266SJames Wright 3774e139266SJames Wright for (PetscInt i = 0; i < nynodes; i++) { 3784e139266SJames Wright if (y < ynodes[i] + half_mindy) { 3792b730f8bSJeremy L Thompson idx = i; 3802b730f8bSJeremy L Thompson break; 3814e139266SJames Wright } 3824e139266SJames Wright } 3834e139266SJames Wright if (idx == 0) return ynodes[1] - ynodes[0]; 3844e139266SJames Wright else if (idx == nynodes - 1) return ynodes[nynodes - 2] - ynodes[nynodes - 1]; 3854e139266SJames Wright else return 0.5 * (ynodes[idx + 1] - ynodes[idx - 1]); 3864e139266SJames Wright } 3874e139266SJames Wright 3884e139266SJames Wright // Function passed to DMAddBoundary 389dada6cc0SJames Wright // NOTE: Not used in favor of QFunction-based method 3902b730f8bSJeremy L Thompson PetscErrorCode StrongSTGbcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[], void *ctx) { 3914e139266SJames Wright PetscFunctionBeginUser; 3922b730f8bSJeremy L Thompson 3934e139266SJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 3944e139266SJames Wright PetscScalar qn[stg_ctx->nmodes], u[3], ubar[3], cij[6], eps, lt; 3954e139266SJames Wright const bool mean_only = stg_ctx->mean_only; 3964e139266SJames Wright const PetscScalar dx = stg_ctx->dx; 3974e139266SJames Wright const PetscScalar dz = stg_ctx->dz; 3984e139266SJames Wright const PetscScalar mu = stg_ctx->newtonian_ctx.mu; 3994e139266SJames Wright const PetscScalar theta0 = stg_ctx->theta0; 4004e139266SJames Wright const PetscScalar P0 = stg_ctx->P0; 4014e139266SJames Wright const PetscScalar cv = stg_ctx->newtonian_ctx.cv; 4024e139266SJames Wright const PetscScalar cp = stg_ctx->newtonian_ctx.cp; 4034e139266SJames Wright const PetscScalar Rd = cp - cv; 4044e139266SJames Wright 4054e139266SJames Wright const CeedScalar rho = P0 / (Rd * theta0); 4064e139266SJames Wright InterpolateProfile(x[1], ubar, cij, &eps, <, stg_ctx); 4074e139266SJames Wright if (!mean_only) { 4084e139266SJames Wright const PetscInt nynodes = stg_ctx->nynodes; 4094e139266SJames Wright const PetscScalar *ynodes = &stg_ctx->data[stg_ctx->offsets.ynodes]; 4104e139266SJames Wright const PetscScalar h[3] = {dx, FindDy(ynodes, nynodes, x[1]), dz}; 4114e139266SJames Wright CalcSpectrum(x[1], eps, lt, h, mu / rho, qn, stg_ctx); 4124e139266SJames Wright STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx); 4134e139266SJames Wright } else { 4144e139266SJames Wright for (CeedInt j = 0; j < 3; j++) u[j] = ubar[j]; 4154e139266SJames Wright } 4164e139266SJames Wright 4174e139266SJames Wright bcval[0] = rho; 4184e139266SJames Wright bcval[1] = rho * u[0]; 4194e139266SJames Wright bcval[2] = rho * u[1]; 4204e139266SJames Wright bcval[3] = rho * u[2]; 4214e139266SJames Wright PetscFunctionReturn(0); 4224e139266SJames Wright } 4234e139266SJames Wright 4242b730f8bSJeremy L Thompson PetscErrorCode SetupStrongSTG(DM dm, SimpleBC bc, ProblemData *problem, Physics phys) { 4254e139266SJames Wright DMLabel label; 4264e139266SJames Wright PetscFunctionBeginUser; 4274e139266SJames Wright 428192a7459SJames Wright PetscInt comps[5], num_comps = 4; 42997baf651SJames Wright switch (phys->state_var) { 43097baf651SJames Wright case STATEVAR_CONSERVATIVE: 431192a7459SJames Wright // {0,1,2,3} for rho, rho*u, rho*v, rho*w 432192a7459SJames Wright for (int i = 0; i < 4; i++) comps[i] = i; 43397baf651SJames Wright break; 43497baf651SJames Wright 43597baf651SJames Wright case STATEVAR_PRIMITIVE: 43697baf651SJames Wright // {1,2,3,4} for u, v, w, T 43797baf651SJames Wright for (int i = 0; i < 4; i++) comps[i] = i + 1; 43897baf651SJames Wright break; 439192a7459SJames Wright } 440192a7459SJames Wright 44124941a69SJames Wright PetscCall(DMGetLabel(dm, "Face Sets", &label)); 4424e139266SJames Wright // Set wall BCs 4434e139266SJames Wright if (bc->num_inflow > 0) { 4442b730f8bSJeremy L Thompson PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "STG", label, bc->num_inflow, bc->inflows, 0, num_comps, comps, (void (*)(void))StrongSTGbcFunc, 44524941a69SJames Wright NULL, global_stg_ctx, NULL)); 4464e139266SJames Wright } 4474e139266SJames Wright 4484e139266SJames Wright PetscFunctionReturn(0); 4494e139266SJames Wright } 450dada6cc0SJames Wright 4512b730f8bSJeremy L Thompson PetscErrorCode SetupStrongSTG_QF(Ceed ceed, ProblemData *problem, CeedInt num_comp_x, CeedInt num_comp_q, CeedInt stg_data_size, 4525dc40723SJames Wright CeedInt q_data_size_sur, CeedQFunction *pqf_strongbc) { 453dada6cc0SJames Wright CeedQFunction qf_strongbc; 454dada6cc0SJames Wright PetscFunctionBeginUser; 4552b730f8bSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, STGShur14_Inflow_StrongQF, STGShur14_Inflow_StrongQF_loc, &qf_strongbc); 4562b730f8bSJeremy L Thompson CeedQFunctionAddInput(qf_strongbc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE); 457dada6cc0SJames Wright CeedQFunctionAddInput(qf_strongbc, "x", num_comp_x, CEED_EVAL_NONE); 458dada6cc0SJames Wright CeedQFunctionAddInput(qf_strongbc, "scale", 1, CEED_EVAL_NONE); 4595dc40723SJames Wright CeedQFunctionAddInput(qf_strongbc, "stg data", stg_data_size, CEED_EVAL_NONE); 460dada6cc0SJames Wright CeedQFunctionAddOutput(qf_strongbc, "q", num_comp_q, CEED_EVAL_NONE); 461dada6cc0SJames Wright 462dada6cc0SJames Wright CeedQFunctionSetContext(qf_strongbc, problem->ics.qfunction_context); 463dada6cc0SJames Wright *pqf_strongbc = qf_strongbc; 464dada6cc0SJames Wright PetscFunctionReturn(0); 465dada6cc0SJames Wright } 4665dc40723SJames Wright 4672b730f8bSJeremy L Thompson PetscErrorCode SetupStrongSTG_PreProcessing(Ceed ceed, ProblemData *problem, CeedInt num_comp_x, CeedInt stg_data_size, CeedInt q_data_size_sur, 4685dc40723SJames Wright CeedQFunction *pqf_strongbc) { 4695dc40723SJames Wright CeedQFunction qf_strongbc; 4705dc40723SJames Wright PetscFunctionBeginUser; 4712b730f8bSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, Preprocess_STGShur14, Preprocess_STGShur14_loc, &qf_strongbc); 4722b730f8bSJeremy L Thompson CeedQFunctionAddInput(qf_strongbc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE); 4735dc40723SJames Wright CeedQFunctionAddInput(qf_strongbc, "x", num_comp_x, CEED_EVAL_NONE); 4745dc40723SJames Wright CeedQFunctionAddOutput(qf_strongbc, "stg data", stg_data_size, CEED_EVAL_NONE); 4755dc40723SJames Wright 4765dc40723SJames Wright CeedQFunctionSetContext(qf_strongbc, problem->ics.qfunction_context); 4775dc40723SJames Wright *pqf_strongbc = qf_strongbc; 4785dc40723SJames Wright PetscFunctionReturn(0); 4795dc40723SJames Wright } 480