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