xref: /libCEED/examples/fluids/qfunctions/turb_spanstats.h (revision b7d6643930f63dbc81dde690883cc613b302935e)
1 // Copyright (c) 2017-2023, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed.h>
9 
10 #include "newtonian_state.h"
11 #include "utils.h"
12 
13 CEED_QFUNCTION_HELPER int ChildStatsCollection(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateFromQi_t StateFromQi,
14                                                StateFromQi_fwd_t StateFromQi_fwd) {
15   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
16   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
17   const CeedScalar(*x)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[2];
18   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
19 
20   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
21 
22   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
23     const CeedScalar wdetJ = q_data[0][i];
24 
25     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
26     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
27     const State      s      = StateFromQi(context, qi, x_i);
28 
29     v[0][i]  = wdetJ * s.U.density;
30     v[1][i]  = wdetJ * s.Y.pressure;
31     v[2][i]  = wdetJ * Square(s.Y.pressure);
32     v[3][i]  = wdetJ * s.Y.pressure * s.Y.velocity[0];
33     v[4][i]  = wdetJ * s.Y.pressure * s.Y.velocity[1];
34     v[5][i]  = wdetJ * s.Y.pressure * s.Y.velocity[2];
35     v[6][i]  = wdetJ * s.U.density * s.Y.temperature;
36     v[7][i]  = wdetJ * s.U.density * s.Y.temperature * s.Y.velocity[0];
37     v[8][i]  = wdetJ * s.U.density * s.Y.temperature * s.Y.velocity[1];
38     v[9][i]  = wdetJ * s.U.density * s.Y.temperature * s.Y.velocity[2];
39     v[10][i] = wdetJ * s.U.momentum[0];
40     v[11][i] = wdetJ * s.U.momentum[1];
41     v[12][i] = wdetJ * s.U.momentum[2];
42     v[13][i] = wdetJ * s.U.momentum[0] * s.Y.velocity[0];
43     v[14][i] = wdetJ * s.U.momentum[1] * s.Y.velocity[1];
44     v[15][i] = wdetJ * s.U.momentum[2] * s.Y.velocity[2];
45     v[16][i] = wdetJ * s.U.momentum[1] * s.Y.velocity[2];
46     v[17][i] = wdetJ * s.U.momentum[0] * s.Y.velocity[2];
47     v[18][i] = wdetJ * s.U.momentum[0] * s.Y.velocity[1];
48     v[19][i] = wdetJ * s.Y.velocity[0];
49     v[20][i] = wdetJ * s.Y.velocity[1];
50     v[21][i] = wdetJ * s.Y.velocity[2];
51   }
52   return 0;
53 }
54 
55 CEED_QFUNCTION(ChildStatsCollection_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56   return ChildStatsCollection(ctx, Q, in, out, StateFromU, StateFromU_fwd);
57 }
58 
59 CEED_QFUNCTION(ChildStatsCollection_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60   return ChildStatsCollection(ctx, Q, in, out, StateFromY, StateFromY_fwd);
61 }
62 
63 // QFunctions for testing
64 CEED_QFUNCTION_HELPER CeedScalar ChildStatsCollectionTest_Exact(const CeedScalar x_i[3]) { return x_i[0] + Square(x_i[1]); }
65 
66 CEED_QFUNCTION(ChildStatsCollectionMMSTest)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
67   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
68   const CeedScalar(*x)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[2];
69   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
70 
71   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
72   const CeedScalar         t       = context->time;
73 
74   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
75     const CeedScalar wdetJ  = q_data[0][i];
76     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
77 
78     // set spanwise domain to [0,1] and integrate from t \in [0,1] to recover exact solution
79     v[0][i] = wdetJ * (ChildStatsCollectionTest_Exact(x_i) + t - 0.5) * 4 * Cube(x_i[2]);
80     for (int j = 1; j < 22; j++) v[j][i] = 0;
81   }
82   return 0;
83 }
84 
85 CEED_QFUNCTION(ChildStatsCollectionMMSTest_Error)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
86   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
87   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
88   const CeedScalar(*x)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[2];
89   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
90 
91   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
92     const CeedScalar wdetJ  = q_data[0][i];
93     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
94 
95     v[0][i] = wdetJ * Square(ChildStatsCollectionTest_Exact(x_i) - q[0][i]);
96   }
97   return 0;
98 }
99