xref: /libCEED/examples/fluids/problems/stg_shur14.c (revision ba6664ae303f5b2ef46b3df96973d9bdc665107c)
1*ba6664aeSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*ba6664aeSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*ba6664aeSJames Wright //
4*ba6664aeSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5*ba6664aeSJames Wright //
6*ba6664aeSJames Wright // This file is part of CEED:  http://github.com/ceed
7*ba6664aeSJames Wright 
8*ba6664aeSJames Wright /// @file
9*ba6664aeSJames Wright /// Implementation of the Synthetic Turbulence Generation (STG) algorithm
10*ba6664aeSJames Wright /// presented in Shur et al. 2014
11*ba6664aeSJames Wright 
12*ba6664aeSJames Wright #include <stdlib.h>
13*ba6664aeSJames Wright #include <math.h>
14*ba6664aeSJames Wright #include <petsc.h>
15*ba6664aeSJames Wright #include "../navierstokes.h"
16*ba6664aeSJames Wright #include "stg_shur14.h"
17*ba6664aeSJames Wright #include "../qfunctions/stg_shur14.h"
18*ba6664aeSJames Wright 
19*ba6664aeSJames Wright #ifndef M_PI
20*ba6664aeSJames Wright #define M_PI    3.14159265358979323846
21*ba6664aeSJames Wright #endif
22*ba6664aeSJames Wright 
23*ba6664aeSJames Wright /*
24*ba6664aeSJames Wright  * @brief Perform Cholesky decomposition on array of symmetric 3x3 matrices
25*ba6664aeSJames Wright  *
26*ba6664aeSJames Wright  * This assumes the input matrices are in order [11,22,33,12,13,23]. This
27*ba6664aeSJames Wright  * format is also used for the output.
28*ba6664aeSJames Wright  *
29*ba6664aeSJames Wright  * @param[in]  comm   MPI_Comm
30*ba6664aeSJames Wright  * @param[in]  nprofs Number of matrices in Rij
31*ba6664aeSJames Wright  * @param[in]  Rij    Array of the symmetric matrices [6,nprofs]
32*ba6664aeSJames Wright  * @param[out] Cij    Array of the Cholesky Decomposition matrices, [6,nprofs]
33*ba6664aeSJames Wright  */
34*ba6664aeSJames Wright PetscErrorCode CalcCholeskyDecomp(MPI_Comm comm, PetscInt nprofs,
35*ba6664aeSJames Wright                                   const CeedScalar Rij[6][nprofs], CeedScalar Cij[6][nprofs]) {
36*ba6664aeSJames Wright 
37*ba6664aeSJames Wright   PetscFunctionBeginUser;
38*ba6664aeSJames Wright   for (PetscInt i=0; i<nprofs; i++) {
39*ba6664aeSJames Wright     Cij[0][i] = sqrt(Rij[0][i]);
40*ba6664aeSJames Wright     Cij[3][i] = Rij[3][i] / Cij[0][i];
41*ba6664aeSJames Wright     Cij[1][i] = sqrt(Rij[1][i] - pow(Cij[3][i], 2) );
42*ba6664aeSJames Wright     Cij[4][i] = Rij[4][i] / Cij[0][i];
43*ba6664aeSJames Wright     Cij[5][i] = (Rij[5][i] - Cij[3][i]*Cij[4][i]) / Cij[1][i];
44*ba6664aeSJames Wright     Cij[2][i] = sqrt(Rij[2][i] - pow(Cij[4][i], 2) - pow(Cij[5][i], 2));
45*ba6664aeSJames Wright 
46*ba6664aeSJames Wright     if (isnan(Cij[0][i]) || isnan(Cij[1][i]) || isnan(Cij[2][i]))
47*ba6664aeSJames Wright       SETERRQ(comm, -1, "Cholesky decomposition failed at profile point %d. "
48*ba6664aeSJames Wright               "Either STGInflow has non-SPD matrix or contains nan.", i+1);
49*ba6664aeSJames Wright   }
50*ba6664aeSJames Wright   PetscFunctionReturn(0);
51*ba6664aeSJames Wright }
52*ba6664aeSJames Wright 
53*ba6664aeSJames Wright 
54*ba6664aeSJames Wright /*
55*ba6664aeSJames Wright  * @brief Open a PHASTA *.dat file, grabbing dimensions and file pointer
56*ba6664aeSJames Wright  *
57*ba6664aeSJames Wright  * This function opens the file specified by `path` using `PetscFOpen` and
58*ba6664aeSJames Wright  * passes the file pointer in `fp`. It is not closed in this function, thus
59*ba6664aeSJames Wright  * `fp` must be closed sometime after this function has been called (using
60*ba6664aeSJames Wright  * `PetscFClose` for example).
61*ba6664aeSJames Wright  *
62*ba6664aeSJames Wright  * Assumes that the first line of the file has the number of rows and columns
63*ba6664aeSJames Wright  * as the only two entries, separated by a single space
64*ba6664aeSJames Wright  *
65*ba6664aeSJames Wright  * @param[in] comm MPI_Comm for the program
66*ba6664aeSJames Wright  * @param[in] path Path to the file
67*ba6664aeSJames Wright  * @param[in] char_array_len Length of the character array that should contain each line
68*ba6664aeSJames Wright  * @param[out] dims Dimensions of the file, taken from the first line of the file
69*ba6664aeSJames Wright  * @param[out] fp File pointer to the opened file
70*ba6664aeSJames Wright  */
71*ba6664aeSJames Wright static PetscErrorCode OpenPHASTADatFile(const MPI_Comm comm,
72*ba6664aeSJames Wright                                         const char path[PETSC_MAX_PATH_LEN], const PetscInt char_array_len,
73*ba6664aeSJames Wright                                         PetscInt dims[2], FILE **fp) {
74*ba6664aeSJames Wright   PetscErrorCode ierr;
75*ba6664aeSJames Wright   PetscInt ndims;
76*ba6664aeSJames Wright   char line[char_array_len];
77*ba6664aeSJames Wright   char **array;
78*ba6664aeSJames Wright 
79*ba6664aeSJames Wright   PetscFunctionBeginUser;
80*ba6664aeSJames Wright   ierr = PetscFOpen(comm, path, "r", fp); CHKERRQ(ierr);
81*ba6664aeSJames Wright   ierr = PetscSynchronizedFGets(comm, *fp, char_array_len, line); CHKERRQ(ierr);
82*ba6664aeSJames Wright   ierr = PetscStrToArray(line, ' ', &ndims, &array); CHKERRQ(ierr);
83*ba6664aeSJames Wright   if (ndims != 2) SETERRQ(comm, -1,
84*ba6664aeSJames Wright                             "Found %d dimensions instead of 2 on the first line of %s",
85*ba6664aeSJames Wright                             ndims, path);
86*ba6664aeSJames Wright 
87*ba6664aeSJames Wright   for (PetscInt i=0; i<ndims; i++)  dims[i] = atoi(array[i]);
88*ba6664aeSJames Wright   ierr = PetscStrToArrayDestroy(ndims, array); CHKERRQ(ierr);
89*ba6664aeSJames Wright   PetscFunctionReturn(0);
90*ba6664aeSJames Wright }
91*ba6664aeSJames Wright 
92*ba6664aeSJames Wright /*
93*ba6664aeSJames Wright  * @brief Get the number of rows for the PHASTA file at path
94*ba6664aeSJames Wright  *
95*ba6664aeSJames Wright  * Assumes that the first line of the file has the number of rows and columns
96*ba6664aeSJames Wright  * as the only two entries, separated by a single space
97*ba6664aeSJames Wright  *
98*ba6664aeSJames Wright  * @param[in] comm MPI_Comm for the program
99*ba6664aeSJames Wright  * @param[in] path Path to the file
100*ba6664aeSJames Wright  * @param[out] nrows Number of rows
101*ba6664aeSJames Wright  */
102*ba6664aeSJames Wright static PetscErrorCode GetNRows(const MPI_Comm comm,
103*ba6664aeSJames Wright                                const char path[PETSC_MAX_PATH_LEN], PetscInt *nrows) {
104*ba6664aeSJames Wright   PetscErrorCode ierr;
105*ba6664aeSJames Wright   const PetscInt char_array_len = 512;
106*ba6664aeSJames Wright   PetscInt dims[2];
107*ba6664aeSJames Wright   FILE *fp;
108*ba6664aeSJames Wright 
109*ba6664aeSJames Wright   PetscFunctionBeginUser;
110*ba6664aeSJames Wright   ierr = OpenPHASTADatFile(comm, path, char_array_len, dims, &fp); CHKERRQ(ierr);
111*ba6664aeSJames Wright   *nrows = dims[0];
112*ba6664aeSJames Wright   ierr = PetscFClose(comm, fp); CHKERRQ(ierr);
113*ba6664aeSJames Wright   PetscFunctionReturn(0);
114*ba6664aeSJames Wright }
115*ba6664aeSJames Wright 
116*ba6664aeSJames Wright /*
117*ba6664aeSJames Wright  * @brief Read the STGInflow file and load the contents into stg_ctx
118*ba6664aeSJames Wright  *
119*ba6664aeSJames Wright  * Assumes that the first line of the file has the number of rows and columns
120*ba6664aeSJames Wright  * as the only two entries, separated by a single space.
121*ba6664aeSJames Wright  * Assumes there are 14 columns in the file
122*ba6664aeSJames Wright  *
123*ba6664aeSJames Wright  * Function calculates the Cholesky decomposition from the Reynolds stress
124*ba6664aeSJames Wright  * profile found in the file
125*ba6664aeSJames Wright  *
126*ba6664aeSJames Wright  * @param[in] comm MPI_Comm for the program
127*ba6664aeSJames Wright  * @param[in] path Path to the STGInflow.dat file
128*ba6664aeSJames Wright  * @param[inout] stg_ctx STGShur14Context where the data will be loaded into
129*ba6664aeSJames Wright  */
130*ba6664aeSJames Wright static PetscErrorCode ReadSTGInflow(const MPI_Comm comm,
131*ba6664aeSJames Wright                                     const char path[PETSC_MAX_PATH_LEN], STGShur14Context stg_ctx) {
132*ba6664aeSJames Wright   PetscErrorCode ierr;
133*ba6664aeSJames Wright   PetscInt ndims, dims[2];
134*ba6664aeSJames Wright   FILE *fp;
135*ba6664aeSJames Wright   const PetscInt char_array_len=512;
136*ba6664aeSJames Wright   char line[char_array_len];
137*ba6664aeSJames Wright   char **array;
138*ba6664aeSJames Wright 
139*ba6664aeSJames Wright   PetscFunctionBeginUser;
140*ba6664aeSJames Wright 
141*ba6664aeSJames Wright   ierr = OpenPHASTADatFile(comm, path, char_array_len, dims, &fp); CHKERRQ(ierr);
142*ba6664aeSJames Wright 
143*ba6664aeSJames Wright   CeedScalar rij[6][stg_ctx->nprofs];
144*ba6664aeSJames Wright   CeedScalar *prof_dw = &stg_ctx->data[stg_ctx->offsets.prof_dw];
145*ba6664aeSJames Wright   CeedScalar *eps = &stg_ctx->data[stg_ctx->offsets.eps];
146*ba6664aeSJames Wright   CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt];
147*ba6664aeSJames Wright   CeedScalar (*ubar)[stg_ctx->nprofs] = (CeedScalar (*)[stg_ctx->nprofs])
148*ba6664aeSJames Wright                                         &stg_ctx->data[stg_ctx->offsets.ubar];
149*ba6664aeSJames Wright 
150*ba6664aeSJames Wright   for (PetscInt i=0; i<stg_ctx->nprofs; i++) {
151*ba6664aeSJames Wright     ierr = PetscSynchronizedFGets(comm, fp, char_array_len, line); CHKERRQ(ierr);
152*ba6664aeSJames Wright     ierr = PetscStrToArray(line, ' ', &ndims, &array); CHKERRQ(ierr);
153*ba6664aeSJames Wright     if (ndims < dims[1]) SETERRQ(comm, -1,
154*ba6664aeSJames Wright                                    "Line %d of %s does not contain enough columns (%d instead of %d)", i,
155*ba6664aeSJames Wright                                    path, ndims, dims[1]);
156*ba6664aeSJames Wright 
157*ba6664aeSJames Wright     prof_dw[i] = (CeedScalar) atof(array[0]);
158*ba6664aeSJames Wright     ubar[0][i] = (CeedScalar) atof(array[1]);
159*ba6664aeSJames Wright     ubar[1][i] = (CeedScalar) atof(array[2]);
160*ba6664aeSJames Wright     ubar[2][i] = (CeedScalar) atof(array[3]);
161*ba6664aeSJames Wright     rij[0][i]  = (CeedScalar) atof(array[4]);
162*ba6664aeSJames Wright     rij[1][i]  = (CeedScalar) atof(array[5]);
163*ba6664aeSJames Wright     rij[2][i]  = (CeedScalar) atof(array[6]);
164*ba6664aeSJames Wright     rij[3][i]  = (CeedScalar) atof(array[7]);
165*ba6664aeSJames Wright     rij[4][i]  = (CeedScalar) atof(array[8]);
166*ba6664aeSJames Wright     rij[5][i]  = (CeedScalar) atof(array[9]);
167*ba6664aeSJames Wright     lt[i]      = (CeedScalar) atof(array[12]);
168*ba6664aeSJames Wright     eps[i]     = (CeedScalar) atof(array[13]);
169*ba6664aeSJames Wright 
170*ba6664aeSJames Wright     if (prof_dw[i] < 0) SETERRQ(comm, -1,
171*ba6664aeSJames Wright                                   "Distance to wall in %s cannot be negative", path);
172*ba6664aeSJames Wright     if (lt[i] < 0) SETERRQ(comm, -1,
173*ba6664aeSJames Wright                              "Turbulent length scale in %s cannot be negative", path);
174*ba6664aeSJames Wright     if (eps[i] < 0) SETERRQ(comm, -1,
175*ba6664aeSJames Wright                               "Turbulent dissipation in %s cannot be negative", path);
176*ba6664aeSJames Wright 
177*ba6664aeSJames Wright   }
178*ba6664aeSJames Wright   CeedScalar (*cij)[stg_ctx->nprofs]  = (CeedScalar (*)[stg_ctx->nprofs])
179*ba6664aeSJames Wright                                         &stg_ctx->data[stg_ctx->offsets.cij];
180*ba6664aeSJames Wright   ierr = CalcCholeskyDecomp(comm, stg_ctx->nprofs, rij, cij); CHKERRQ(ierr);
181*ba6664aeSJames Wright   ierr = PetscFClose(comm, fp); CHKERRQ(ierr);
182*ba6664aeSJames Wright   PetscFunctionReturn(0);
183*ba6664aeSJames Wright }
184*ba6664aeSJames Wright 
185*ba6664aeSJames Wright /*
186*ba6664aeSJames Wright  * @brief Read the STGRand file and load the contents into stg_ctx
187*ba6664aeSJames Wright  *
188*ba6664aeSJames Wright  * Assumes that the first line of the file has the number of rows and columns
189*ba6664aeSJames Wright  * as the only two entries, separated by a single space.
190*ba6664aeSJames Wright  * Assumes there are 7 columns in the file
191*ba6664aeSJames Wright  *
192*ba6664aeSJames Wright  * @param[in]    comm    MPI_Comm for the program
193*ba6664aeSJames Wright  * @param[in]    path    Path to the STGRand.dat file
194*ba6664aeSJames Wright  * @param[inout] stg_ctx STGShur14Context where the data will be loaded into
195*ba6664aeSJames Wright  */
196*ba6664aeSJames Wright static PetscErrorCode ReadSTGRand(const MPI_Comm comm,
197*ba6664aeSJames Wright                                   const char path[PETSC_MAX_PATH_LEN],
198*ba6664aeSJames Wright                                   STGShur14Context stg_ctx) {
199*ba6664aeSJames Wright 
200*ba6664aeSJames Wright   PetscErrorCode ierr;
201*ba6664aeSJames Wright   PetscInt ndims, dims[2];
202*ba6664aeSJames Wright   FILE *fp;
203*ba6664aeSJames Wright   const PetscInt char_array_len = 512;
204*ba6664aeSJames Wright   char line[char_array_len];
205*ba6664aeSJames Wright   char **array;
206*ba6664aeSJames Wright 
207*ba6664aeSJames Wright   PetscFunctionBeginUser;
208*ba6664aeSJames Wright   ierr = OpenPHASTADatFile(comm, path, char_array_len, dims, &fp); CHKERRQ(ierr);
209*ba6664aeSJames Wright 
210*ba6664aeSJames Wright   CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi];
211*ba6664aeSJames Wright   CeedScalar (*d)[stg_ctx->nmodes]     = (CeedScalar (*)[stg_ctx->nmodes])
212*ba6664aeSJames Wright                                          &stg_ctx->data[stg_ctx->offsets.d];
213*ba6664aeSJames Wright   CeedScalar (*sigma)[stg_ctx->nmodes] = (CeedScalar (*)[stg_ctx->nmodes])
214*ba6664aeSJames Wright                                          &stg_ctx->data[stg_ctx->offsets.sigma];
215*ba6664aeSJames Wright 
216*ba6664aeSJames Wright   for (PetscInt i=0; i<stg_ctx->nmodes; i++) {
217*ba6664aeSJames Wright     ierr = PetscSynchronizedFGets(comm, fp, char_array_len, line); CHKERRQ(ierr);
218*ba6664aeSJames Wright     ierr = PetscStrToArray(line, ' ', &ndims, &array); CHKERRQ(ierr);
219*ba6664aeSJames Wright     if (ndims < dims[1]) SETERRQ(comm, -1,
220*ba6664aeSJames Wright                                    "Line %d of %s does not contain enough columns (%d instead of %d)", i,
221*ba6664aeSJames Wright                                    path, ndims, dims[1]);
222*ba6664aeSJames Wright 
223*ba6664aeSJames Wright     d[0][i]     = (CeedScalar) atof(array[0]);
224*ba6664aeSJames Wright     d[1][i]     = (CeedScalar) atof(array[1]);
225*ba6664aeSJames Wright     d[2][i]     = (CeedScalar) atof(array[2]);
226*ba6664aeSJames Wright     phi[i]      = (CeedScalar) atof(array[3]);
227*ba6664aeSJames Wright     sigma[0][i] = (CeedScalar) atof(array[4]);
228*ba6664aeSJames Wright     sigma[1][i] = (CeedScalar) atof(array[5]);
229*ba6664aeSJames Wright     sigma[2][i] = (CeedScalar) atof(array[6]);
230*ba6664aeSJames Wright   }
231*ba6664aeSJames Wright   ierr = PetscFClose(comm, fp); CHKERRQ(ierr);
232*ba6664aeSJames Wright   PetscFunctionReturn(0);
233*ba6664aeSJames Wright }
234*ba6664aeSJames Wright 
235*ba6664aeSJames Wright /*
236*ba6664aeSJames Wright  * @brief Read STG data from input paths and put in STGShur14Context
237*ba6664aeSJames Wright  *
238*ba6664aeSJames Wright  * Reads data from input paths and puts them into a STGShur14Context object.
239*ba6664aeSJames Wright  * Data stored initially in `*pstg_ctx` will be copied over to the new
240*ba6664aeSJames Wright  * STGShur14Context instance.
241*ba6664aeSJames Wright  *
242*ba6664aeSJames Wright  * @param[in]    comm            MPI_Comm for the program
243*ba6664aeSJames Wright  * @param[in]    dm              DM for the program
244*ba6664aeSJames Wright  * @param[in]    stg_inflow_path Path to STGInflow.dat file
245*ba6664aeSJames Wright  * @param[in]    stg_rand_path   Path to STGRand.dat file
246*ba6664aeSJames Wright  * @param[inout] pstg_ctx        Pointer to STGShur14Context where the data will be loaded into
247*ba6664aeSJames Wright  */
248*ba6664aeSJames Wright PetscErrorCode GetSTGContextData(const MPI_Comm comm, const DM dm,
249*ba6664aeSJames Wright                                  char stg_inflow_path[PETSC_MAX_PATH_LEN],
250*ba6664aeSJames Wright                                  char stg_rand_path[PETSC_MAX_PATH_LEN],
251*ba6664aeSJames Wright                                  STGShur14Context *pstg_ctx) {
252*ba6664aeSJames Wright   PetscErrorCode ierr;
253*ba6664aeSJames Wright   PetscInt nmodes, nprofs;
254*ba6664aeSJames Wright   STGShur14Context stg_ctx;
255*ba6664aeSJames Wright   PetscFunctionBeginUser;
256*ba6664aeSJames Wright 
257*ba6664aeSJames Wright   // Get options
258*ba6664aeSJames Wright   ierr = GetNRows(comm, stg_rand_path, &nmodes); CHKERRQ(ierr);
259*ba6664aeSJames Wright   ierr = GetNRows(comm, stg_inflow_path, &nprofs); CHKERRQ(ierr);
260*ba6664aeSJames Wright   if (nmodes > STG_NMODES_MAX)
261*ba6664aeSJames Wright     SETERRQ(comm, 1, "Number of wavemodes in %s (%d) exceeds STG_NMODES_MAX (%d). "
262*ba6664aeSJames Wright             "Change size of STG_NMODES_MAX and recompile", stg_rand_path, nmodes,
263*ba6664aeSJames Wright             STG_NMODES_MAX);
264*ba6664aeSJames Wright 
265*ba6664aeSJames Wright   {
266*ba6664aeSJames Wright     STGShur14Context s;
267*ba6664aeSJames Wright     ierr = PetscCalloc1(1, &s); CHKERRQ(ierr);
268*ba6664aeSJames Wright     *s = **pstg_ctx;
269*ba6664aeSJames Wright     s->nmodes = nmodes;
270*ba6664aeSJames Wright     s->nprofs = nprofs;
271*ba6664aeSJames Wright     s->offsets.sigma   = 0;
272*ba6664aeSJames Wright     s->offsets.d       = nmodes*3;
273*ba6664aeSJames Wright     s->offsets.phi     = s->offsets.d       + nmodes*3;
274*ba6664aeSJames Wright     s->offsets.kappa   = s->offsets.phi     + nmodes;
275*ba6664aeSJames Wright     s->offsets.prof_dw = s->offsets.kappa   + nmodes;
276*ba6664aeSJames Wright     s->offsets.ubar    = s->offsets.prof_dw + nprofs;
277*ba6664aeSJames Wright     s->offsets.cij     = s->offsets.ubar    + nprofs*3;
278*ba6664aeSJames Wright     s->offsets.eps     = s->offsets.cij     + nprofs*6;
279*ba6664aeSJames Wright     s->offsets.lt      = s->offsets.eps     + nprofs;
280*ba6664aeSJames Wright     PetscInt total_num_scalars = s->offsets.lt + nprofs;
281*ba6664aeSJames Wright     s->total_bytes = sizeof(*stg_ctx) + total_num_scalars*sizeof(stg_ctx->data[0]);
282*ba6664aeSJames Wright     ierr = PetscMalloc(s->total_bytes, &stg_ctx); CHKERRQ(ierr);
283*ba6664aeSJames Wright     *stg_ctx = *s;
284*ba6664aeSJames Wright     ierr = PetscFree(s); CHKERRQ(ierr);
285*ba6664aeSJames Wright   }
286*ba6664aeSJames Wright 
287*ba6664aeSJames Wright   ierr = ReadSTGInflow(comm, stg_inflow_path, stg_ctx); CHKERRQ(ierr);
288*ba6664aeSJames Wright   ierr = ReadSTGRand(comm, stg_rand_path, stg_ctx); CHKERRQ(ierr);
289*ba6664aeSJames Wright 
290*ba6664aeSJames Wright   // -- Calculate kappa
291*ba6664aeSJames Wright   {
292*ba6664aeSJames Wright     CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa];
293*ba6664aeSJames Wright     CeedScalar *prof_dw = &stg_ctx->data[stg_ctx->offsets.prof_dw];
294*ba6664aeSJames Wright     CeedScalar *lt = &stg_ctx->data[stg_ctx->offsets.lt];
295*ba6664aeSJames Wright     CeedScalar le, le_max=0;
296*ba6664aeSJames Wright 
297*ba6664aeSJames Wright     CeedPragmaSIMD
298*ba6664aeSJames Wright     for (PetscInt i=0; i<stg_ctx->nprofs; i++) {
299*ba6664aeSJames Wright       le = PetscMin(2*prof_dw[i], 3*lt[i]);
300*ba6664aeSJames Wright       if (le_max < le) le_max = le;
301*ba6664aeSJames Wright     }
302*ba6664aeSJames Wright     CeedScalar kmin = M_PI/le_max;
303*ba6664aeSJames Wright 
304*ba6664aeSJames Wright     CeedPragmaSIMD
305*ba6664aeSJames Wright     for (PetscInt i=0; i<stg_ctx->nmodes; i++) {
306*ba6664aeSJames Wright       kappa[i] = kmin*pow(stg_ctx->alpha, i);
307*ba6664aeSJames Wright     }
308*ba6664aeSJames Wright   } //end calculate kappa
309*ba6664aeSJames Wright 
310*ba6664aeSJames Wright   *pstg_ctx = stg_ctx;
311*ba6664aeSJames Wright   PetscFunctionReturn(0);
312*ba6664aeSJames Wright }
313*ba6664aeSJames Wright 
314*ba6664aeSJames Wright PetscErrorCode SetupSTG(const MPI_Comm comm, const DM dm, ProblemData *problem,
315*ba6664aeSJames Wright                         User user, const bool prescribe_T,
316*ba6664aeSJames Wright                         const CeedScalar theta0, const CeedScalar P0) {
317*ba6664aeSJames Wright   PetscErrorCode ierr;
318*ba6664aeSJames Wright   char stg_inflow_path[PETSC_MAX_PATH_LEN] = "./STGInflow.dat";
319*ba6664aeSJames Wright   char stg_rand_path[PETSC_MAX_PATH_LEN] = "./STGRand.dat";
320*ba6664aeSJames Wright   PetscBool mean_only = PETSC_FALSE;
321*ba6664aeSJames Wright   CeedScalar u0=0.0, alpha=1.01;
322*ba6664aeSJames Wright   STGShur14Context stg_ctx;
323*ba6664aeSJames Wright   CeedQFunctionContext stg_context;
324*ba6664aeSJames Wright   NewtonianIdealGasContext newtonian_ig_ctx;
325*ba6664aeSJames Wright   PetscFunctionBeginUser;
326*ba6664aeSJames Wright 
327*ba6664aeSJames Wright   // Get options
328*ba6664aeSJames Wright   PetscOptionsBegin(comm, NULL, "STG Boundary Condition Options", NULL);
329*ba6664aeSJames Wright   ierr = PetscOptionsString("-stg_inflow_path", "Path to STGInflow.dat", NULL,
330*ba6664aeSJames Wright                             stg_inflow_path, stg_inflow_path,
331*ba6664aeSJames Wright                             sizeof(stg_inflow_path), NULL); CHKERRQ(ierr);
332*ba6664aeSJames Wright   ierr = PetscOptionsString("-stg_rand_path", "Path to STGInflow.dat", NULL,
333*ba6664aeSJames Wright                             stg_rand_path,stg_rand_path,
334*ba6664aeSJames Wright                             sizeof(stg_rand_path), NULL); CHKERRQ(ierr);
335*ba6664aeSJames Wright   ierr = PetscOptionsReal("-stg_alpha", "Growth rate of the wavemodes", NULL,
336*ba6664aeSJames Wright                           alpha, &alpha, NULL); CHKERRQ(ierr);
337*ba6664aeSJames Wright   ierr = PetscOptionsReal("-stg_u0", "Advective velocity for the fluctuations",
338*ba6664aeSJames Wright                           NULL, u0, &u0, NULL); CHKERRQ(ierr);
339*ba6664aeSJames Wright   ierr = PetscOptionsBool("-stg_mean_only", "Only apply mean profile",
340*ba6664aeSJames Wright                           NULL, mean_only, &mean_only, NULL); CHKERRQ(ierr);
341*ba6664aeSJames Wright   PetscOptionsEnd();
342*ba6664aeSJames Wright 
343*ba6664aeSJames Wright   ierr = PetscCalloc1(1, &stg_ctx); CHKERRQ(ierr);
344*ba6664aeSJames Wright   stg_ctx->alpha         = alpha;
345*ba6664aeSJames Wright   stg_ctx->u0            = u0;
346*ba6664aeSJames Wright   stg_ctx->is_implicit   = user->phys->implicit;
347*ba6664aeSJames Wright   stg_ctx->prescribe_T   = prescribe_T;
348*ba6664aeSJames Wright   stg_ctx->mean_only     = mean_only;
349*ba6664aeSJames Wright   stg_ctx->theta0        = theta0;
350*ba6664aeSJames Wright   stg_ctx->P0            = P0;
351*ba6664aeSJames Wright 
352*ba6664aeSJames Wright   {
353*ba6664aeSJames Wright     // Calculate dx assuming constant spacing
354*ba6664aeSJames Wright     PetscReal domain_min[3], domain_max[3], domain_size[3];
355*ba6664aeSJames Wright     ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
356*ba6664aeSJames Wright     for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
357*ba6664aeSJames Wright 
358*ba6664aeSJames Wright     PetscInt nmax = 3, faces[3];
359*ba6664aeSJames Wright     ierr = PetscOptionsGetIntArray(NULL, NULL, "-dm_plex_box_faces", faces, &nmax,
360*ba6664aeSJames Wright                                    NULL); CHKERRQ(ierr);
361*ba6664aeSJames Wright     stg_ctx->dx = domain_size[0]/faces[0];
362*ba6664aeSJames Wright   }
363*ba6664aeSJames Wright 
364*ba6664aeSJames Wright   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context,
365*ba6664aeSJames Wright                               CEED_MEM_HOST, &newtonian_ig_ctx);
366*ba6664aeSJames Wright   stg_ctx->newtonian_ctx = *newtonian_ig_ctx;
367*ba6664aeSJames Wright   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context,
368*ba6664aeSJames Wright                                   &newtonian_ig_ctx);
369*ba6664aeSJames Wright 
370*ba6664aeSJames Wright   ierr = GetSTGContextData(comm, dm, stg_inflow_path, stg_rand_path, &stg_ctx);
371*ba6664aeSJames Wright   CHKERRQ(ierr);
372*ba6664aeSJames Wright 
373*ba6664aeSJames Wright   CeedQFunctionContextDestroy(&problem->apply_inflow.qfunction_context);
374*ba6664aeSJames Wright   CeedQFunctionContextCreate(user->ceed, &stg_context);
375*ba6664aeSJames Wright   CeedQFunctionContextSetData(stg_context, CEED_MEM_HOST,
376*ba6664aeSJames Wright                               CEED_USE_POINTER, stg_ctx->total_bytes, stg_ctx);
377*ba6664aeSJames Wright   CeedQFunctionContextSetDataDestroy(stg_context, CEED_MEM_HOST,
378*ba6664aeSJames Wright                                      FreeContextPetsc);
379*ba6664aeSJames Wright   CeedQFunctionContextRegisterDouble(stg_context, "solution time",
380*ba6664aeSJames Wright                                      offsetof(struct STGShur14Context_, time), 1,
381*ba6664aeSJames Wright                                      "Phyiscal time of the solution");
382*ba6664aeSJames Wright 
383*ba6664aeSJames Wright   problem->apply_inflow.qfunction         = STGShur14_Inflow;
384*ba6664aeSJames Wright   problem->apply_inflow.qfunction_loc     = STGShur14_Inflow_loc;
385*ba6664aeSJames Wright   problem->apply_inflow.qfunction_context = stg_context;
386*ba6664aeSJames Wright 
387*ba6664aeSJames Wright   PetscFunctionReturn(0);
388*ba6664aeSJames Wright }
389