1493642f1SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2493642f1SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3493642f1SJames Wright // 4493642f1SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5493642f1SJames Wright // 6493642f1SJames Wright // This file is part of CEED: http://github.com/ceed 7493642f1SJames Wright 8493642f1SJames Wright /// @file 9493642f1SJames Wright /// Implementation of the Synthetic Turbulence Generation (STG) algorithm 10493642f1SJames Wright /// presented in Shur et al. 2014 11493642f1SJames Wright 12493642f1SJames Wright #include <stdlib.h> 13493642f1SJames Wright #include <math.h> 14493642f1SJames Wright #include <petsc.h> 15493642f1SJames Wright #include "../navierstokes.h" 16493642f1SJames Wright #include "stg_shur14.h" 17493642f1SJames Wright #include "../qfunctions/stg_shur14.h" 18493642f1SJames Wright 198085925cSJames Wright STGShur14Context global_stg_ctx; 208085925cSJames Wright 21493642f1SJames Wright /* 22493642f1SJames Wright * @brief Perform Cholesky decomposition on array of symmetric 3x3 matrices 23493642f1SJames Wright * 24493642f1SJames Wright * This assumes the input matrices are in order [11,22,33,12,13,23]. This 25493642f1SJames Wright * format is also used for the output. 26493642f1SJames Wright * 27493642f1SJames Wright * @param[in] comm MPI_Comm 28493642f1SJames Wright * @param[in] nprofs Number of matrices in Rij 29493642f1SJames Wright * @param[in] Rij Array of the symmetric matrices [6,nprofs] 30493642f1SJames Wright * @param[out] Cij Array of the Cholesky Decomposition matrices, [6,nprofs] 31493642f1SJames Wright */ 32493642f1SJames Wright PetscErrorCode CalcCholeskyDecomp(MPI_Comm comm, PetscInt nprofs, 33493642f1SJames Wright const CeedScalar Rij[6][nprofs], CeedScalar Cij[6][nprofs]) { 34493642f1SJames Wright PetscFunctionBeginUser; 35493642f1SJames Wright for (PetscInt i=0; i<nprofs; i++) { 36493642f1SJames Wright Cij[0][i] = sqrt(Rij[0][i]); 37493642f1SJames Wright Cij[3][i] = Rij[3][i] / Cij[0][i]; 38493642f1SJames Wright Cij[1][i] = sqrt(Rij[1][i] - pow(Cij[3][i], 2) ); 39493642f1SJames Wright Cij[4][i] = Rij[4][i] / Cij[0][i]; 40493642f1SJames Wright Cij[5][i] = (Rij[5][i] - Cij[3][i]*Cij[4][i]) / Cij[1][i]; 41493642f1SJames Wright Cij[2][i] = sqrt(Rij[2][i] - pow(Cij[4][i], 2) - pow(Cij[5][i], 2)); 42493642f1SJames Wright 43493642f1SJames Wright if (isnan(Cij[0][i]) || isnan(Cij[1][i]) || isnan(Cij[2][i])) 4445aa3cadSJeremy L Thompson SETERRQ(comm, -1, "Cholesky decomposition failed at profile point %" 4545aa3cadSJeremy L Thompson PetscInt_FMT ". Either STGInflow has non-SPD matrix or contains nan.", i+1); 46493642f1SJames Wright } 47493642f1SJames Wright PetscFunctionReturn(0); 48493642f1SJames Wright } 49493642f1SJames Wright 50493642f1SJames Wright 51493642f1SJames Wright /* 52493642f1SJames Wright * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer 53493642f1SJames Wright * 54493642f1SJames Wright * This function opens the file specified by `path` using `PetscFOpen` and 55493642f1SJames Wright * passes the file pointer in `fp`. It is not closed in this function, thus 56493642f1SJames Wright * `fp` must be closed sometime after this function has been called (using 57493642f1SJames Wright * `PetscFClose` for example). 58493642f1SJames Wright * 59493642f1SJames Wright * Assumes that the first line of the file has the number of rows and columns 60493642f1SJames Wright * as the only two entries, separated by a single space 61493642f1SJames Wright * 62493642f1SJames Wright * @param[in] comm MPI_Comm for the program 63493642f1SJames Wright * @param[in] path Path to the file 64493642f1SJames Wright * @param[in] char_array_len Length of the character array that should contain each line 65493642f1SJames Wright * @param[out] dims Dimensions of the file, taken from the first line of the file 66493642f1SJames Wright * @param[out] fp File pointer to the opened file 67493642f1SJames Wright */ 68493642f1SJames Wright static PetscErrorCode OpenPHASTADatFile(const MPI_Comm comm, 69493642f1SJames Wright const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len, 70493642f1SJames Wright PetscInt dims[2], FILE **fp) { 71493642f1SJames Wright PetscInt ndims; 72493642f1SJames Wright char line[char_array_len]; 73493642f1SJames Wright char **array; 74493642f1SJames Wright 75493642f1SJames Wright PetscFunctionBeginUser; 76*34b5deb1SJames Wright PetscCall(PetscFOpen(comm, path, "r", fp)); 77*34b5deb1SJames Wright PetscCall(PetscSynchronizedFGets(comm, *fp, char_array_len, line)); 78*34b5deb1SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 79493642f1SJames Wright if (ndims != 2) SETERRQ(comm, -1, 8045aa3cadSJeremy L Thompson "Found %" PetscInt_FMT" dimensions instead of 2 on the first line of %s", 81493642f1SJames Wright ndims, path); 82493642f1SJames Wright 83493642f1SJames Wright for (PetscInt i=0; i<ndims; i++) dims[i] = atoi(array[i]); 84*34b5deb1SJames Wright PetscCall(PetscStrToArrayDestroy(ndims, array)); 85493642f1SJames Wright PetscFunctionReturn(0); 86493642f1SJames Wright } 87493642f1SJames Wright 88493642f1SJames Wright /* 89493642f1SJames Wright * @brief Get the number of rows for the PHASTA file at path 90493642f1SJames Wright * 91493642f1SJames Wright * Assumes that the first line of the file has the number of rows and columns 92493642f1SJames Wright * as the only two entries, separated by a single space 93493642f1SJames Wright * 94493642f1SJames Wright * @param[in] comm MPI_Comm for the program 95493642f1SJames Wright * @param[in] path Path to the file 96493642f1SJames Wright * @param[out] nrows Number of rows 97493642f1SJames Wright */ 98493642f1SJames Wright static PetscErrorCode GetNRows(const MPI_Comm comm, 99493642f1SJames Wright const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) { 100493642f1SJames Wright const PetscInt char_array_len = 512; 101493642f1SJames Wright PetscInt dims[2]; 102493642f1SJames Wright FILE *fp; 103493642f1SJames Wright 104493642f1SJames Wright PetscFunctionBeginUser; 105*34b5deb1SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 106493642f1SJames Wright *nrows = dims[0]; 107*34b5deb1SJames Wright PetscCall(PetscFClose(comm, fp)); 108493642f1SJames Wright PetscFunctionReturn(0); 109493642f1SJames Wright } 110493642f1SJames Wright 111493642f1SJames Wright /* 112493642f1SJames Wright * @brief Read the STGInflow file and load the contents into stg_ctx 113493642f1SJames Wright * 114493642f1SJames Wright * Assumes that the first line of the file has the number of rows and columns 115493642f1SJames Wright * as the only two entries, separated by a single space. 116493642f1SJames Wright * Assumes there are 14 columns in the file 117493642f1SJames Wright * 118493642f1SJames Wright * Function calculates the Cholesky decomposition from the Reynolds stress 119493642f1SJames Wright * profile found in the file 120493642f1SJames Wright * 121493642f1SJames Wright * @param[in] comm MPI_Comm for the program 122493642f1SJames Wright * @param[in] path Path to the STGInflow.dat file 123493642f1SJames Wright * @param[inout] stg_ctx STGShur14Context where the data will be loaded into 124493642f1SJames Wright */ 125493642f1SJames Wright static PetscErrorCode ReadSTGInflow(const MPI_Comm comm, 126493642f1SJames Wright const char path[PETSC_MAX_PATH_LEN], STGShur14Context stg_ctx) { 127493642f1SJames Wright PetscInt ndims, dims[2]; 128493642f1SJames Wright FILE *fp; 129493642f1SJames Wright const PetscInt char_array_len=512; 130493642f1SJames Wright char line[char_array_len]; 131493642f1SJames Wright char **array; 132493642f1SJames Wright 133493642f1SJames Wright PetscFunctionBeginUser; 134493642f1SJames Wright 135*34b5deb1SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 136493642f1SJames Wright 137493642f1SJames Wright CeedScalar rij[6][stg_ctx->nprofs]; 138c77f3192SJames Wright CeedScalar *wall_dist = &stg_ctx->data[stg_ctx->offsets.wall_dist]; 139493642f1SJames Wright CeedScalar *eps = &stg_ctx->data[stg_ctx->offsets.eps]; 140493642f1SJames Wright CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt]; 141493642f1SJames Wright CeedScalar (*ubar)[stg_ctx->nprofs] = (CeedScalar (*)[stg_ctx->nprofs]) 142493642f1SJames Wright &stg_ctx->data[stg_ctx->offsets.ubar]; 143493642f1SJames Wright 144493642f1SJames Wright for (PetscInt i=0; i<stg_ctx->nprofs; i++) { 145*34b5deb1SJames Wright PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 146*34b5deb1SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 147493642f1SJames Wright if (ndims < dims[1]) SETERRQ(comm, -1, 14845aa3cadSJeremy L Thompson "Line %" PetscInt_FMT" of %s does not contain enough columns (%" 14945aa3cadSJeremy L Thompson PetscInt_FMT" instead of %" PetscInt_FMT")", i, 150493642f1SJames Wright path, ndims, dims[1]); 151493642f1SJames Wright 152c77f3192SJames Wright wall_dist[i] = (CeedScalar) atof(array[0]); 153493642f1SJames Wright ubar[0][i] = (CeedScalar) atof(array[1]); 154493642f1SJames Wright ubar[1][i] = (CeedScalar) atof(array[2]); 155493642f1SJames Wright ubar[2][i] = (CeedScalar) atof(array[3]); 156493642f1SJames Wright rij[0][i] = (CeedScalar) atof(array[4]); 157493642f1SJames Wright rij[1][i] = (CeedScalar) atof(array[5]); 158493642f1SJames Wright rij[2][i] = (CeedScalar) atof(array[6]); 159493642f1SJames Wright rij[3][i] = (CeedScalar) atof(array[7]); 160493642f1SJames Wright rij[4][i] = (CeedScalar) atof(array[8]); 161493642f1SJames Wright rij[5][i] = (CeedScalar) atof(array[9]); 162493642f1SJames Wright lt[i] = (CeedScalar) atof(array[12]); 163493642f1SJames Wright eps[i] = (CeedScalar) atof(array[13]); 164493642f1SJames Wright 165c77f3192SJames Wright if (wall_dist[i] < 0) SETERRQ(comm, -1, 166493642f1SJames Wright "Distance to wall in %s cannot be negative", path); 167493642f1SJames Wright if (lt[i] < 0) SETERRQ(comm, -1, 168493642f1SJames Wright "Turbulent length scale in %s cannot be negative", path); 169493642f1SJames Wright if (eps[i] < 0) SETERRQ(comm, -1, 170493642f1SJames Wright "Turbulent dissipation in %s cannot be negative", path); 171493642f1SJames Wright 172493642f1SJames Wright } 173493642f1SJames Wright CeedScalar (*cij)[stg_ctx->nprofs] = (CeedScalar (*)[stg_ctx->nprofs]) 174493642f1SJames Wright &stg_ctx->data[stg_ctx->offsets.cij]; 175*34b5deb1SJames Wright PetscCall(CalcCholeskyDecomp(comm, stg_ctx->nprofs, rij, cij)); 176*34b5deb1SJames Wright PetscCall(PetscFClose(comm, fp)); 177493642f1SJames Wright PetscFunctionReturn(0); 178493642f1SJames Wright } 179493642f1SJames Wright 180493642f1SJames Wright /* 181493642f1SJames Wright * @brief Read the STGRand file and load the contents into stg_ctx 182493642f1SJames Wright * 183493642f1SJames Wright * Assumes that the first line of the file has the number of rows and columns 184493642f1SJames Wright * as the only two entries, separated by a single space. 185493642f1SJames Wright * Assumes there are 7 columns in the file 186493642f1SJames Wright * 187493642f1SJames Wright * @param[in] comm MPI_Comm for the program 188493642f1SJames Wright * @param[in] path Path to the STGRand.dat file 189493642f1SJames Wright * @param[inout] stg_ctx STGShur14Context where the data will be loaded into 190493642f1SJames Wright */ 191493642f1SJames Wright static PetscErrorCode ReadSTGRand(const MPI_Comm comm, 192493642f1SJames Wright const char path[PETSC_MAX_PATH_LEN], 193493642f1SJames Wright STGShur14Context stg_ctx) { 194493642f1SJames Wright PetscInt ndims, dims[2]; 195493642f1SJames Wright FILE *fp; 196493642f1SJames Wright const PetscInt char_array_len = 512; 197493642f1SJames Wright char line[char_array_len]; 198493642f1SJames Wright char **array; 199493642f1SJames Wright 200493642f1SJames Wright PetscFunctionBeginUser; 201*34b5deb1SJames Wright PetscCall(OpenPHASTADatFile(comm, path, char_array_len, dims, &fp)); 202493642f1SJames Wright 203493642f1SJames Wright CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi]; 204493642f1SJames Wright CeedScalar (*d)[stg_ctx->nmodes] = (CeedScalar (*)[stg_ctx->nmodes]) 205493642f1SJames Wright &stg_ctx->data[stg_ctx->offsets.d]; 206493642f1SJames Wright CeedScalar (*sigma)[stg_ctx->nmodes] = (CeedScalar (*)[stg_ctx->nmodes]) 207493642f1SJames Wright &stg_ctx->data[stg_ctx->offsets.sigma]; 208493642f1SJames Wright 209493642f1SJames Wright for (PetscInt i=0; i<stg_ctx->nmodes; i++) { 210*34b5deb1SJames Wright PetscCall(PetscSynchronizedFGets(comm, fp, char_array_len, line)); 211*34b5deb1SJames Wright PetscCall(PetscStrToArray(line, ' ', &ndims, &array)); 212493642f1SJames Wright if (ndims < dims[1]) SETERRQ(comm, -1, 21345aa3cadSJeremy L Thompson "Line %" PetscInt_FMT" of %s does not contain enough columns (%" 21445aa3cadSJeremy L Thompson PetscInt_FMT" instead of %" PetscInt_FMT")", i, 215493642f1SJames Wright path, ndims, dims[1]); 216493642f1SJames Wright 217493642f1SJames Wright d[0][i] = (CeedScalar) atof(array[0]); 218493642f1SJames Wright d[1][i] = (CeedScalar) atof(array[1]); 219493642f1SJames Wright d[2][i] = (CeedScalar) atof(array[2]); 220493642f1SJames Wright phi[i] = (CeedScalar) atof(array[3]); 221493642f1SJames Wright sigma[0][i] = (CeedScalar) atof(array[4]); 222493642f1SJames Wright sigma[1][i] = (CeedScalar) atof(array[5]); 223493642f1SJames Wright sigma[2][i] = (CeedScalar) atof(array[6]); 224493642f1SJames Wright } 225*34b5deb1SJames Wright PetscCall(PetscFClose(comm, fp)); 226493642f1SJames Wright PetscFunctionReturn(0); 227493642f1SJames Wright } 228493642f1SJames Wright 229493642f1SJames Wright /* 230493642f1SJames Wright * @brief Read STG data from input paths and put in STGShur14Context 231493642f1SJames Wright * 232493642f1SJames Wright * Reads data from input paths and puts them into a STGShur14Context object. 233493642f1SJames Wright * Data stored initially in `*pstg_ctx` will be copied over to the new 234493642f1SJames Wright * STGShur14Context instance. 235493642f1SJames Wright * 236493642f1SJames Wright * @param[in] comm MPI_Comm for the program 237493642f1SJames Wright * @param[in] dm DM for the program 238493642f1SJames Wright * @param[in] stg_inflow_path Path to STGInflow.dat file 239493642f1SJames Wright * @param[in] stg_rand_path Path to STGRand.dat file 240493642f1SJames Wright * @param[inout] pstg_ctx Pointer to STGShur14Context where the data will be loaded into 241493642f1SJames Wright */ 242493642f1SJames Wright PetscErrorCode GetSTGContextData(const MPI_Comm comm, const DM dm, 243493642f1SJames Wright char stg_inflow_path[PETSC_MAX_PATH_LEN], 244493642f1SJames Wright char stg_rand_path[PETSC_MAX_PATH_LEN], 245e6098bcdSJames Wright STGShur14Context *pstg_ctx, 246e6098bcdSJames Wright const CeedScalar ynodes[]) { 247493642f1SJames Wright PetscInt nmodes, nprofs; 248493642f1SJames Wright STGShur14Context stg_ctx; 249493642f1SJames Wright PetscFunctionBeginUser; 250493642f1SJames Wright 251493642f1SJames Wright // Get options 252*34b5deb1SJames Wright PetscCall(GetNRows(comm, stg_rand_path, &nmodes)); 253*34b5deb1SJames Wright PetscCall(GetNRows(comm, stg_inflow_path, &nprofs)); 254493642f1SJames Wright if (nmodes > STG_NMODES_MAX) 25545aa3cadSJeremy L Thompson SETERRQ(comm, 1, "Number of wavemodes in %s (%" 25645aa3cadSJeremy L Thompson PetscInt_FMT") exceeds STG_NMODES_MAX (%" PetscInt_FMT"). " 257493642f1SJames Wright "Change size of STG_NMODES_MAX and recompile", stg_rand_path, nmodes, 258493642f1SJames Wright STG_NMODES_MAX); 259493642f1SJames Wright 260493642f1SJames Wright { 261493642f1SJames Wright STGShur14Context s; 262*34b5deb1SJames Wright PetscCall(PetscCalloc1(1, &s)); 263493642f1SJames Wright *s = **pstg_ctx; 264493642f1SJames Wright s->nmodes = nmodes; 265493642f1SJames Wright s->nprofs = nprofs; 266493642f1SJames Wright s->offsets.sigma = 0; 267493642f1SJames Wright s->offsets.d = nmodes*3; 268493642f1SJames Wright s->offsets.phi = s->offsets.d + nmodes*3; 269493642f1SJames Wright s->offsets.kappa = s->offsets.phi + nmodes; 270c77f3192SJames Wright s->offsets.wall_dist = s->offsets.kappa + nmodes; 271c77f3192SJames Wright s->offsets.ubar = s->offsets.wall_dist + nprofs; 272493642f1SJames Wright s->offsets.cij = s->offsets.ubar + nprofs*3; 273493642f1SJames Wright s->offsets.eps = s->offsets.cij + nprofs*6; 274493642f1SJames Wright s->offsets.lt = s->offsets.eps + nprofs; 275e6098bcdSJames Wright s->offsets.ynodes = s->offsets.lt + nprofs; 276e6098bcdSJames Wright PetscInt total_num_scalars = s->offsets.ynodes + s->nynodes; 277493642f1SJames Wright s->total_bytes = sizeof(*stg_ctx) + total_num_scalars*sizeof(stg_ctx->data[0]); 278*34b5deb1SJames Wright PetscCall(PetscMalloc(s->total_bytes, &stg_ctx)); 279493642f1SJames Wright *stg_ctx = *s; 280*34b5deb1SJames Wright PetscCall(PetscFree(s)); 281493642f1SJames Wright } 282493642f1SJames Wright 283*34b5deb1SJames Wright PetscCall(ReadSTGInflow(comm, stg_inflow_path, stg_ctx)); 284*34b5deb1SJames Wright PetscCall(ReadSTGRand(comm, stg_rand_path, stg_ctx)); 285493642f1SJames Wright 286e6098bcdSJames Wright if (stg_ctx->nynodes > 0) { 287e6098bcdSJames Wright CeedScalar *ynodes_ctx = &stg_ctx->data[stg_ctx->offsets.ynodes]; 288e6098bcdSJames Wright for (PetscInt i=0; i<stg_ctx->nynodes; i++) ynodes_ctx[i] = ynodes[i]; 289e6098bcdSJames Wright } 290e6098bcdSJames Wright 291493642f1SJames Wright // -- Calculate kappa 292493642f1SJames Wright { 293493642f1SJames Wright CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 294c77f3192SJames Wright CeedScalar *wall_dist = &stg_ctx->data[stg_ctx->offsets.wall_dist]; 295493642f1SJames Wright CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt]; 296493642f1SJames Wright CeedScalar le, le_max=0; 297493642f1SJames Wright 298493642f1SJames Wright CeedPragmaSIMD 299493642f1SJames Wright for (PetscInt i=0; i<stg_ctx->nprofs; i++) { 300c77f3192SJames Wright le = PetscMin(2*wall_dist[i], 3*lt[i]); 301493642f1SJames Wright if (le_max < le) le_max = le; 302493642f1SJames Wright } 303493642f1SJames Wright CeedScalar kmin = M_PI/le_max; 304493642f1SJames Wright 305493642f1SJames Wright CeedPragmaSIMD 306493642f1SJames Wright for (PetscInt i=0; i<stg_ctx->nmodes; i++) { 307493642f1SJames Wright kappa[i] = kmin*pow(stg_ctx->alpha, i); 308493642f1SJames Wright } 309493642f1SJames Wright } //end calculate kappa 310493642f1SJames Wright 311*34b5deb1SJames Wright PetscCall(PetscFree(*pstg_ctx)); 312493642f1SJames Wright *pstg_ctx = stg_ctx; 313493642f1SJames Wright PetscFunctionReturn(0); 314493642f1SJames Wright } 315493642f1SJames Wright 316493642f1SJames Wright PetscErrorCode SetupSTG(const MPI_Comm comm, const DM dm, ProblemData *problem, 317493642f1SJames Wright User user, const bool prescribe_T, 318e6098bcdSJames Wright const CeedScalar theta0, const CeedScalar P0, 319e6098bcdSJames Wright const CeedScalar ynodes[], const CeedInt nynodes) { 320493642f1SJames Wright char stg_inflow_path[PETSC_MAX_PATH_LEN] = "./STGInflow.dat"; 321493642f1SJames Wright char stg_rand_path[PETSC_MAX_PATH_LEN] = "./STGRand.dat"; 322e6098bcdSJames Wright PetscBool mean_only = PETSC_FALSE, 323d4e0f297SJames Wright use_stgstrong = PETSC_FALSE, 324d4e0f297SJames Wright use_fluctuating_IC = PETSC_FALSE; 325e6098bcdSJames Wright CeedScalar u0 = 0.0, 326e6098bcdSJames Wright alpha = 1.01; 327493642f1SJames Wright CeedQFunctionContext stg_context; 328493642f1SJames Wright NewtonianIdealGasContext newtonian_ig_ctx; 329493642f1SJames Wright PetscFunctionBeginUser; 330493642f1SJames Wright 331493642f1SJames Wright // Get options 332493642f1SJames Wright PetscOptionsBegin(comm, NULL, "STG Boundary Condition Options", NULL); 333*34b5deb1SJames Wright PetscCall(PetscOptionsString("-stg_inflow_path", "Path to STGInflow.dat", NULL, 334493642f1SJames Wright stg_inflow_path, stg_inflow_path, 335*34b5deb1SJames Wright sizeof(stg_inflow_path), NULL)); 336*34b5deb1SJames Wright PetscCall(PetscOptionsString("-stg_rand_path", "Path to STGInflow.dat", NULL, 337493642f1SJames Wright stg_rand_path,stg_rand_path, 338*34b5deb1SJames Wright sizeof(stg_rand_path), NULL)); 339*34b5deb1SJames Wright PetscCall(PetscOptionsReal("-stg_alpha", "Growth rate of the wavemodes", NULL, 340*34b5deb1SJames Wright alpha, &alpha, NULL)); 341*34b5deb1SJames Wright PetscCall(PetscOptionsReal("-stg_u0", "Advective velocity for the fluctuations", 342*34b5deb1SJames Wright NULL, u0, &u0, NULL)); 343*34b5deb1SJames Wright PetscCall(PetscOptionsBool("-stg_mean_only", "Only apply mean profile", 344*34b5deb1SJames Wright NULL, mean_only, &mean_only, NULL)); 345*34b5deb1SJames Wright PetscCall(PetscOptionsBool("-stg_strong", "Enforce STG inflow strongly", 346*34b5deb1SJames Wright NULL, use_stgstrong, &use_stgstrong, NULL)); 347d4e0f297SJames Wright PetscCall(PetscOptionsBool("-stg_fluctuating_IC", 348d4e0f297SJames Wright "\"Extrude\" the fluctuations through the domain as an initial condition", 349d4e0f297SJames Wright NULL, use_fluctuating_IC, &use_fluctuating_IC, NULL)); 350493642f1SJames Wright PetscOptionsEnd(); 351493642f1SJames Wright 352*34b5deb1SJames Wright PetscCall(PetscCalloc1(1, &global_stg_ctx)); 3538085925cSJames Wright global_stg_ctx->alpha = alpha; 3548085925cSJames Wright global_stg_ctx->u0 = u0; 3558085925cSJames Wright global_stg_ctx->is_implicit = user->phys->implicit; 3568085925cSJames Wright global_stg_ctx->prescribe_T = prescribe_T; 3578085925cSJames Wright global_stg_ctx->mean_only = mean_only; 358d4e0f297SJames Wright global_stg_ctx->use_fluctuating_IC = use_fluctuating_IC; 3598085925cSJames Wright global_stg_ctx->theta0 = theta0; 3608085925cSJames Wright global_stg_ctx->P0 = P0; 3618085925cSJames Wright global_stg_ctx->nynodes = nynodes; 362493642f1SJames Wright 363493642f1SJames Wright { 364493642f1SJames Wright // Calculate dx assuming constant spacing 365493642f1SJames Wright PetscReal domain_min[3], domain_max[3], domain_size[3]; 366*34b5deb1SJames Wright PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 367493642f1SJames Wright for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 368493642f1SJames Wright 369493642f1SJames Wright PetscInt nmax = 3, faces[3]; 370*34b5deb1SJames Wright PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", faces, 371*34b5deb1SJames Wright &nmax, NULL)); 3728085925cSJames Wright global_stg_ctx->dx = domain_size[0]/faces[0]; 3738085925cSJames Wright global_stg_ctx->dz = domain_size[2]/faces[2]; 374493642f1SJames Wright } 375493642f1SJames Wright 376493642f1SJames Wright CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, 377493642f1SJames Wright CEED_MEM_HOST, &newtonian_ig_ctx); 3788085925cSJames Wright global_stg_ctx->newtonian_ctx = *newtonian_ig_ctx; 379493642f1SJames Wright CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, 380493642f1SJames Wright &newtonian_ig_ctx); 381493642f1SJames Wright 382*34b5deb1SJames Wright PetscCall(GetSTGContextData(comm, dm, stg_inflow_path, stg_rand_path, 383*34b5deb1SJames Wright &global_stg_ctx, ynodes)); 384493642f1SJames Wright 385493642f1SJames Wright CeedQFunctionContextCreate(user->ceed, &stg_context); 386493642f1SJames Wright CeedQFunctionContextSetData(stg_context, CEED_MEM_HOST, 3878085925cSJames Wright CEED_USE_POINTER, global_stg_ctx->total_bytes, global_stg_ctx); 388493642f1SJames Wright CeedQFunctionContextSetDataDestroy(stg_context, CEED_MEM_HOST, 389493642f1SJames Wright FreeContextPetsc); 390493642f1SJames Wright CeedQFunctionContextRegisterDouble(stg_context, "solution time", 391493642f1SJames Wright offsetof(struct STGShur14Context_, time), 1, 3922c498363SJed Brown "Physical time of the solution"); 393493642f1SJames Wright 39443bff553SJames Wright CeedQFunctionContextDestroy(&problem->ics.qfunction_context); 39543bff553SJames Wright problem->ics.qfunction = ICsSTG; 39643bff553SJames Wright problem->ics.qfunction_loc = ICsSTG_loc; 39743bff553SJames Wright problem->ics.qfunction_context = stg_context; 39843bff553SJames Wright 399e6098bcdSJames Wright if (use_stgstrong) { 4008085925cSJames Wright // Use default boundary integral QF (BoundaryIntegral) in newtonian.h 4016d0190e2SJames Wright problem->use_dirichlet_ceed = PETSC_TRUE; 402d7244455SJames Wright problem->bc_from_ics = PETSC_FALSE; 403e6098bcdSJames Wright } else { 404493642f1SJames Wright problem->apply_inflow.qfunction = STGShur14_Inflow; 405493642f1SJames Wright problem->apply_inflow.qfunction_loc = STGShur14_Inflow_loc; 406a6e8f989SJames Wright problem->apply_inflow_jacobian.qfunction = STGShur14_Inflow_Jacobian; 407a6e8f989SJames Wright problem->apply_inflow_jacobian.qfunction_loc = STGShur14_Inflow_Jacobian_loc; 408a6e8f989SJames Wright CeedQFunctionContextReferenceCopy(stg_context, 409a6e8f989SJames Wright &problem->apply_inflow.qfunction_context); 410a6e8f989SJames Wright CeedQFunctionContextReferenceCopy(stg_context, 411a6e8f989SJames Wright &problem->apply_inflow_jacobian.qfunction_context); 412d7244455SJames Wright problem->bc_from_ics = PETSC_TRUE; 413e6098bcdSJames Wright } 414493642f1SJames Wright 415493642f1SJames Wright PetscFunctionReturn(0); 416493642f1SJames Wright } 417e6098bcdSJames Wright 418e6098bcdSJames Wright static inline PetscScalar FindDy(const PetscScalar ynodes[], 419e6098bcdSJames Wright const PetscInt nynodes, const PetscScalar y) { 420e6098bcdSJames Wright const PetscScalar half_mindy = 0.5 * (ynodes[1] - ynodes[0]); 421e6098bcdSJames Wright // ^^assuming min(dy) is first element off the wall 422e6098bcdSJames Wright PetscInt idx = -1; // Index 423e6098bcdSJames Wright 424e6098bcdSJames Wright for (PetscInt i=0; i<nynodes; i++) { 425e6098bcdSJames Wright if (y < ynodes[i] + half_mindy) { 426e6098bcdSJames Wright idx = i; break; 427e6098bcdSJames Wright } 428e6098bcdSJames Wright } 429e6098bcdSJames Wright if (idx == 0) return ynodes[1] - ynodes[0]; 430e6098bcdSJames Wright else if (idx == nynodes-1) return ynodes[nynodes-2] - ynodes[nynodes-1]; 431e6098bcdSJames Wright else return 0.5 * (ynodes[idx+1] - ynodes[idx-1]); 432e6098bcdSJames Wright } 433e6098bcdSJames Wright 434e6098bcdSJames Wright // Function passed to DMAddBoundary 4356d0190e2SJames Wright // NOTE: Not used in favor of QFunction-based method 436e6098bcdSJames Wright PetscErrorCode StrongSTGbcFunc(PetscInt dim, PetscReal time, 437e6098bcdSJames Wright const PetscReal x[], PetscInt Nc, PetscScalar bcval[], void *ctx) { 438e6098bcdSJames Wright PetscFunctionBeginUser; 439e6098bcdSJames Wright const STGShur14Context stg_ctx = (STGShur14Context) ctx; 440e6098bcdSJames Wright PetscScalar qn[stg_ctx->nmodes], u[3], ubar[3], cij[6], eps, lt; 441e6098bcdSJames Wright const bool mean_only = stg_ctx->mean_only; 442e6098bcdSJames Wright const PetscScalar dx = stg_ctx->dx; 443e6098bcdSJames Wright const PetscScalar dz = stg_ctx->dz; 444e6098bcdSJames Wright const PetscScalar mu = stg_ctx->newtonian_ctx.mu; 445e6098bcdSJames Wright const PetscScalar theta0 = stg_ctx->theta0; 446e6098bcdSJames Wright const PetscScalar P0 = stg_ctx->P0; 447e6098bcdSJames Wright const PetscScalar cv = stg_ctx->newtonian_ctx.cv; 448e6098bcdSJames Wright const PetscScalar cp = stg_ctx->newtonian_ctx.cp; 449e6098bcdSJames Wright const PetscScalar Rd = cp - cv; 450e6098bcdSJames Wright 451e6098bcdSJames Wright const CeedScalar rho = P0 / (Rd * theta0); 452e6098bcdSJames Wright InterpolateProfile(x[1], ubar, cij, &eps, <, stg_ctx); 453e6098bcdSJames Wright if (!mean_only) { 454e6098bcdSJames Wright const PetscInt nynodes = stg_ctx->nynodes; 455e6098bcdSJames Wright const PetscScalar *ynodes = &stg_ctx->data[stg_ctx->offsets.ynodes]; 456e6098bcdSJames Wright const PetscScalar h[3] = {dx, FindDy(ynodes, nynodes, x[1]), dz}; 457e6098bcdSJames Wright CalcSpectrum(x[1], eps, lt, h, mu/rho, qn, stg_ctx); 458e6098bcdSJames Wright STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx); 459e6098bcdSJames Wright } else { 460e6098bcdSJames Wright for (CeedInt j=0; j<3; j++) u[j] = ubar[j]; 461e6098bcdSJames Wright } 462e6098bcdSJames Wright 463e6098bcdSJames Wright bcval[0] = rho; 464e6098bcdSJames Wright bcval[1] = rho * u[0]; 465e6098bcdSJames Wright bcval[2] = rho * u[1]; 466e6098bcdSJames Wright bcval[3] = rho * u[2]; 467e6098bcdSJames Wright PetscFunctionReturn(0); 468e6098bcdSJames Wright } 469e6098bcdSJames Wright 470d374fb47SJames Wright PetscErrorCode SetupStrongSTG(DM dm, SimpleBC bc, ProblemData *problem, 471d374fb47SJames Wright Physics phys) { 472e6098bcdSJames Wright DMLabel label; 473e6098bcdSJames Wright PetscFunctionBeginUser; 474e6098bcdSJames Wright 475d374fb47SJames Wright PetscInt comps[5], num_comps=4; 4763636f6a4SJames Wright switch (phys->state_var) { 4773636f6a4SJames Wright case STATEVAR_CONSERVATIVE: 478d374fb47SJames Wright // {0,1,2,3} for rho, rho*u, rho*v, rho*w 479d374fb47SJames Wright for(int i=0; i<4; i++) comps[i] = i; 4803636f6a4SJames Wright break; 4813636f6a4SJames Wright 4823636f6a4SJames Wright case STATEVAR_PRIMITIVE: 4833636f6a4SJames Wright // {1,2,3,4} for u, v, w, T 4843636f6a4SJames Wright for(int i=0; i<4; i++) comps[i] = i+1; 4853636f6a4SJames Wright break; 486d374fb47SJames Wright } 487d374fb47SJames Wright 488*34b5deb1SJames Wright PetscCall(DMGetLabel(dm, "Face Sets", &label)); 489e6098bcdSJames Wright // Set wall BCs 490e6098bcdSJames Wright if (bc->num_inflow > 0) { 491*34b5deb1SJames Wright PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "STG", label, 492e6098bcdSJames Wright bc->num_inflow, bc->inflows, 0, num_comps, 493e6098bcdSJames Wright comps, (void(*)(void))StrongSTGbcFunc, 494*34b5deb1SJames Wright NULL, global_stg_ctx, NULL)); 495e6098bcdSJames Wright } 496e6098bcdSJames Wright 497e6098bcdSJames Wright PetscFunctionReturn(0); 498e6098bcdSJames Wright } 4996d0190e2SJames Wright 5006d0190e2SJames Wright PetscErrorCode SetupStrongSTG_QF(Ceed ceed, ProblemData *problem, 5019eeef72bSJames Wright CeedInt num_comp_x, CeedInt num_comp_q, CeedInt stg_data_size, 5029eeef72bSJames Wright CeedInt q_data_size_sur, CeedQFunction *pqf_strongbc) { 5036d0190e2SJames Wright 5046d0190e2SJames Wright CeedQFunction qf_strongbc; 5056d0190e2SJames Wright PetscFunctionBeginUser; 5066d0190e2SJames Wright CeedQFunctionCreateInterior(ceed, 1, STGShur14_Inflow_StrongQF, 5076d0190e2SJames Wright STGShur14_Inflow_StrongQF_loc, &qf_strongbc); 5086d0190e2SJames Wright CeedQFunctionAddInput(qf_strongbc, "surface qdata", q_data_size_sur, 5096d0190e2SJames Wright CEED_EVAL_NONE); 5106d0190e2SJames Wright CeedQFunctionAddInput(qf_strongbc, "x", num_comp_x, CEED_EVAL_NONE); 5116d0190e2SJames Wright CeedQFunctionAddInput(qf_strongbc, "scale", 1, CEED_EVAL_NONE); 5129eeef72bSJames Wright CeedQFunctionAddInput(qf_strongbc, "stg data", stg_data_size, CEED_EVAL_NONE); 5136d0190e2SJames Wright CeedQFunctionAddOutput(qf_strongbc, "q", num_comp_q, CEED_EVAL_NONE); 5146d0190e2SJames Wright 5156d0190e2SJames Wright CeedQFunctionSetContext(qf_strongbc, problem->ics.qfunction_context); 5166d0190e2SJames Wright *pqf_strongbc = qf_strongbc; 5176d0190e2SJames Wright PetscFunctionReturn(0); 5186d0190e2SJames Wright } 5199eeef72bSJames Wright 5209eeef72bSJames Wright PetscErrorCode SetupStrongSTG_PreProcessing(Ceed ceed, ProblemData *problem, 5219eeef72bSJames Wright CeedInt num_comp_x, CeedInt stg_data_size, CeedInt q_data_size_sur, 5229eeef72bSJames Wright CeedQFunction *pqf_strongbc) { 5239eeef72bSJames Wright 5249eeef72bSJames Wright CeedQFunction qf_strongbc; 5259eeef72bSJames Wright PetscFunctionBeginUser; 5269eeef72bSJames Wright CeedQFunctionCreateInterior(ceed, 1, Preprocess_STGShur14, 5279eeef72bSJames Wright Preprocess_STGShur14_loc, &qf_strongbc); 5289eeef72bSJames Wright CeedQFunctionAddInput(qf_strongbc, "surface qdata", q_data_size_sur, 5299eeef72bSJames Wright CEED_EVAL_NONE); 5309eeef72bSJames Wright CeedQFunctionAddInput(qf_strongbc, "x", num_comp_x, CEED_EVAL_NONE); 5319eeef72bSJames Wright CeedQFunctionAddOutput(qf_strongbc, "stg data", stg_data_size, CEED_EVAL_NONE); 5329eeef72bSJames Wright 5339eeef72bSJames Wright CeedQFunctionSetContext(qf_strongbc, problem->ics.qfunction_context); 5349eeef72bSJames Wright *pqf_strongbc = qf_strongbc; 5359eeef72bSJames Wright PetscFunctionReturn(0); 5369eeef72bSJames Wright } 537