xref: /honee/qfunctions/spanstats/cflpe.h (revision b30619f6337e4aeddfabbcf7d00859553914ef7b)
1*b30619f6SJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2025, HONEE contributors.
2*b30619f6SJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3*b30619f6SJames Wright #include <ceed/types.h>
4*b30619f6SJames Wright 
5*b30619f6SJames Wright #include "../newtonian_state.h"
6*b30619f6SJames Wright #include "../numerics.h"
7*b30619f6SJames Wright #include "../utils.h"
8*b30619f6SJames Wright 
9*b30619f6SJames Wright typedef struct CflPe_SpanStatsContext_ *CflPe_SpanStatsContext;
10*b30619f6SJames Wright struct CflPe_SpanStatsContext_ {
11*b30619f6SJames Wright   CeedScalar                       solution_time;
12*b30619f6SJames Wright   CeedScalar                       previous_time;
13*b30619f6SJames Wright   CeedScalar                       diffusion_coeff;
14*b30619f6SJames Wright   CeedScalar                       timestep;
15*b30619f6SJames Wright   struct NewtonianIdealGasContext_ gas;
16*b30619f6SJames Wright };
17*b30619f6SJames Wright 
18*b30619f6SJames Wright CEED_QFUNCTION_HELPER int ChildStatsCollection_CflPe(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
19*b30619f6SJames Wright                                                      StateVariable state_var, CeedInt dim) {
20*b30619f6SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
21*b30619f6SJames Wright   const CeedScalar(*q_data)        = in[1];
22*b30619f6SJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
23*b30619f6SJames Wright 
24*b30619f6SJames Wright   CflPe_SpanStatsContext   context = (CflPe_SpanStatsContext)ctx;
25*b30619f6SJames Wright   NewtonianIdealGasContext gas     = &context->gas;
26*b30619f6SJames Wright   CeedScalar               delta_t = context->solution_time - context->previous_time;
27*b30619f6SJames Wright 
28*b30619f6SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
29*b30619f6SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
30*b30619f6SJames Wright     const State      s     = StateFromQ(gas, qi, state_var);
31*b30619f6SJames Wright     CeedScalar       Pe, cfl, wdetJ;
32*b30619f6SJames Wright 
33*b30619f6SJames Wright     switch (dim) {
34*b30619f6SJames Wright       case 2: {
35*b30619f6SJames Wright         CeedScalar dXdx[2][2], gijd_mat[2][2] = {{0.}};
36*b30619f6SJames Wright 
37*b30619f6SJames Wright         QdataUnpack_2D(Q, i, q_data, &wdetJ, dXdx);
38*b30619f6SJames Wright         wdetJ = wdetJ * delta_t;
39*b30619f6SJames Wright 
40*b30619f6SJames Wright         MatMat2(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
41*b30619f6SJames Wright         // (1/2)^2 to account for reference element size; for length 1 square/cube element, gij should be identity matrix
42*b30619f6SJames Wright         ScaleN((CeedScalar *)gijd_mat, 0.25, Square(dim));
43*b30619f6SJames Wright 
44*b30619f6SJames Wright         cfl = CalculateCFL_2D(s.Y.velocity, context->timestep, gijd_mat);
45*b30619f6SJames Wright         Pe  = CalculatePe_2D(s.Y.velocity, context->diffusion_coeff, gijd_mat);
46*b30619f6SJames Wright       } break;
47*b30619f6SJames Wright       case 3: {
48*b30619f6SJames Wright         CeedScalar dXdx[3][3], gijd_mat[3][3] = {{0.}};
49*b30619f6SJames Wright 
50*b30619f6SJames Wright         QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
51*b30619f6SJames Wright         wdetJ = wdetJ * delta_t;
52*b30619f6SJames Wright 
53*b30619f6SJames Wright         MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
54*b30619f6SJames Wright         // (1/2)^2 to account for reference element size; for length 1 square/cube element, gij should be identity matrix
55*b30619f6SJames Wright         ScaleN((CeedScalar *)gijd_mat, 0.25, Square(dim));
56*b30619f6SJames Wright 
57*b30619f6SJames Wright         cfl = CalculateCFL_3D(s.Y.velocity, context->timestep, gijd_mat);
58*b30619f6SJames Wright         Pe  = CalculatePe_3D(s.Y.velocity, context->diffusion_coeff, gijd_mat);
59*b30619f6SJames Wright       } break;
60*b30619f6SJames Wright     }
61*b30619f6SJames Wright 
62*b30619f6SJames Wright     v[0][i] = wdetJ * cfl;
63*b30619f6SJames Wright     v[1][i] = wdetJ * Square(cfl);
64*b30619f6SJames Wright     v[2][i] = wdetJ * Cube(cfl);
65*b30619f6SJames Wright     v[3][i] = wdetJ * Pe;
66*b30619f6SJames Wright     v[4][i] = wdetJ * Square(Pe);
67*b30619f6SJames Wright     v[5][i] = wdetJ * Cube(Pe);
68*b30619f6SJames Wright   }
69*b30619f6SJames Wright   return 0;
70*b30619f6SJames Wright }
71*b30619f6SJames Wright 
72*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_3D_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
73*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_CONSERVATIVE, 3);
74*b30619f6SJames Wright }
75*b30619f6SJames Wright 
76*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_3D_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
77*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_PRIMITIVE, 3);
78*b30619f6SJames Wright }
79*b30619f6SJames Wright 
80*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_3D_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
81*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_ENTROPY, 3);
82*b30619f6SJames Wright }
83*b30619f6SJames Wright 
84*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_2D_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
85*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_CONSERVATIVE, 2);
86*b30619f6SJames Wright }
87*b30619f6SJames Wright 
88*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_2D_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
89*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_PRIMITIVE, 2);
90*b30619f6SJames Wright }
91*b30619f6SJames Wright 
92*b30619f6SJames Wright CEED_QFUNCTION(ChildStatsCollection_2D_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
93*b30619f6SJames Wright   return ChildStatsCollection_CflPe(ctx, Q, in, out, STATEVAR_ENTROPY, 2);
94*b30619f6SJames Wright }
95