xref: /honee/qfunctions/advection.h (revision dc936754fc0ae21fa21dd641901ba00fc24c3769)
1*dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Advection initial condition and operator for Navier-Stokes example using PETSc
10a515125bSLeila Ghaffari 
11a515125bSLeila Ghaffari #ifndef advection_h
12a515125bSLeila Ghaffari #define advection_h
13a515125bSLeila Ghaffari 
14493642f1SJames Wright #include <ceed.h>
15d0cce58aSJeremy L Thompson #include <math.h>
16a515125bSLeila Ghaffari 
17e88b842aSJames Wright #include "advection_types.h"
18ce192147SJames Wright #include "newtonian_state.h"
19ce192147SJames Wright #include "newtonian_types.h"
20e88b842aSJames Wright #include "stabilization_types.h"
211a74fa30SJames Wright #include "utils.h"
221a74fa30SJames Wright 
23a515125bSLeila Ghaffari // *****************************************************************************
249529d636SJames Wright // This QFunction sets the initial conditions and the boundary conditions
259529d636SJames Wright //   for two test cases: ROTATION and TRANSLATION
269529d636SJames Wright //
279529d636SJames Wright // -- ROTATION (default)
289529d636SJames Wright //      Initial Conditions:
299529d636SJames Wright //        Mass Density:
309529d636SJames Wright //          Constant mass density of 1.0
319529d636SJames Wright //        Momentum Density:
329529d636SJames Wright //          Rotational field in x,y
339529d636SJames Wright //        Energy Density:
349529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
359529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
369529d636SJames Wright //
379529d636SJames Wright //      Boundary Conditions:
389529d636SJames Wright //        Mass Density:
399529d636SJames Wright //          0.0 flux
409529d636SJames Wright //        Momentum Density:
419529d636SJames Wright //          0.0
429529d636SJames Wright //        Energy Density:
439529d636SJames Wright //          0.0 flux
449529d636SJames Wright //
459529d636SJames Wright // -- TRANSLATION
469529d636SJames Wright //      Initial Conditions:
479529d636SJames Wright //        Mass Density:
489529d636SJames Wright //          Constant mass density of 1.0
499529d636SJames Wright //        Momentum Density:
509529d636SJames Wright //           Constant rectilinear field in x,y
519529d636SJames Wright //        Energy Density:
529529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
539529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
549529d636SJames Wright //
559529d636SJames Wright //      Boundary Conditions:
569529d636SJames Wright //        Mass Density:
579529d636SJames Wright //          0.0 flux
589529d636SJames Wright //        Momentum Density:
599529d636SJames Wright //          0.0
609529d636SJames Wright //        Energy Density:
619529d636SJames Wright //          Inflow BCs:
629529d636SJames Wright //            E = E_wind
639529d636SJames Wright //          Outflow BCs:
649529d636SJames Wright //            E = E(boundary)
659529d636SJames Wright //          Both In/Outflow BCs for E are applied weakly in the
669529d636SJames Wright //            QFunction "Advection2d_Sur"
679529d636SJames Wright //
689529d636SJames Wright // *****************************************************************************
699529d636SJames Wright 
709529d636SJames Wright // *****************************************************************************
719529d636SJames Wright // This helper function provides the exact, time-dependent solution and IC formulation for 2D advection
729529d636SJames Wright // *****************************************************************************
739529d636SJames Wright CEED_QFUNCTION_HELPER CeedInt Exact_AdvectionGeneric(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
749529d636SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
759529d636SJames Wright   const CeedScalar      rc      = context->rc;
769529d636SJames Wright   const CeedScalar      lx      = context->lx;
779529d636SJames Wright   const CeedScalar      ly      = context->ly;
789529d636SJames Wright   const CeedScalar      lz      = dim == 2 ? 0. : context->lz;
799529d636SJames Wright   const CeedScalar     *wind    = context->wind;
809529d636SJames Wright 
819529d636SJames Wright   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
829529d636SJames Wright   const CeedScalar theta     = dim == 2 ? M_PI / 3 : M_PI;
839529d636SJames Wright   const CeedScalar x0[3]     = {center[0] + .25 * lx * cos(theta + time), center[1] + .25 * ly * sin(theta + time), 0.5 * lz};
849529d636SJames Wright 
859529d636SJames Wright   const CeedScalar x = X[0], y = X[1], z = dim == 2 ? 0. : X[2];
869529d636SJames Wright 
879529d636SJames Wright   CeedScalar r = 0.;
889529d636SJames Wright   switch (context->initial_condition_type) {
899529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
909529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
919529d636SJames Wright       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
929529d636SJames Wright       break;
939529d636SJames Wright     case ADVECTIONIC_COSINE_HILL:
949529d636SJames Wright       r = sqrt(Square(x - center[0]) + Square(y - center[1]));
959529d636SJames Wright       break;
969529d636SJames Wright     case ADVECTIONIC_SKEW:
979529d636SJames Wright       break;
989529d636SJames Wright   }
999529d636SJames Wright 
1009529d636SJames Wright   switch (context->wind_type) {
1019529d636SJames Wright     case WIND_ROTATION:
1029529d636SJames Wright       q[0] = 1.;
1039529d636SJames Wright       q[1] = -(y - center[1]);
1049529d636SJames Wright       q[2] = (x - center[0]);
1059529d636SJames Wright       q[3] = 0;
1069529d636SJames Wright       break;
1079529d636SJames Wright     case WIND_TRANSLATION:
1089529d636SJames Wright       q[0] = 1.;
1099529d636SJames Wright       q[1] = wind[0];
1109529d636SJames Wright       q[2] = wind[1];
1119529d636SJames Wright       q[3] = dim == 2 ? 0. : wind[2];
1129529d636SJames Wright       break;
1139529d636SJames Wright     default:
1149529d636SJames Wright       return 1;
1159529d636SJames Wright   }
1169529d636SJames Wright 
1179529d636SJames Wright   switch (context->initial_condition_type) {
1189529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
1199529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
1209529d636SJames Wright       switch (context->bubble_continuity_type) {
1219529d636SJames Wright         // original continuous, smooth shape
1229529d636SJames Wright         case BUBBLE_CONTINUITY_SMOOTH:
1239529d636SJames Wright           q[4] = r <= rc ? (1. - r / rc) : 0.;
1249529d636SJames Wright           break;
1259529d636SJames Wright         // discontinuous, sharp back half shape
1269529d636SJames Wright         case BUBBLE_CONTINUITY_BACK_SHARP:
1279529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
1289529d636SJames Wright           break;
1299529d636SJames Wright         // attempt to define a finite thickness that will get resolved under grid refinement
1309529d636SJames Wright         case BUBBLE_CONTINUITY_THICK:
1319529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
1329529d636SJames Wright           break;
1339529d636SJames Wright         case BUBBLE_CONTINUITY_COSINE:
1349529d636SJames Wright           q[4] = r <= rc ? .5 + .5 * cos(r * M_PI / rc) : 0;
1359529d636SJames Wright           break;
1369529d636SJames Wright       }
1379529d636SJames Wright       break;
1389529d636SJames Wright     case ADVECTIONIC_COSINE_HILL: {
1399529d636SJames Wright       CeedScalar half_width = context->lx / 2;
1409529d636SJames Wright       q[4]                  = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.;
1419529d636SJames Wright     } break;
1429529d636SJames Wright     case ADVECTIONIC_SKEW: {
1439529d636SJames Wright       CeedScalar       skewed_barrier[3]  = {wind[0], wind[1], 0};
1449529d636SJames Wright       CeedScalar       inflow_to_point[3] = {x - context->lx / 2, y, 0};
1459529d636SJames Wright       CeedScalar       cross_product[3]   = {0};
1469529d636SJames Wright       const CeedScalar boundary_threshold = 20 * CEED_EPSILON;
1479529d636SJames Wright       Cross3(skewed_barrier, inflow_to_point, cross_product);
1489529d636SJames Wright 
1499529d636SJames Wright       q[4] = cross_product[2] > boundary_threshold ? 0 : 1;
1509529d636SJames Wright       if ((x < boundary_threshold && wind[0] < boundary_threshold) ||                // outflow at -x boundary
1519529d636SJames Wright           (y < boundary_threshold && wind[1] < boundary_threshold) ||                // outflow at -y boundary
1529529d636SJames Wright           (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) ||  // outflow at +x boundary
1539529d636SJames Wright           (y > context->ly - boundary_threshold && wind[1] > boundary_threshold)     // outflow at +y boundary
1549529d636SJames Wright       ) {
1559529d636SJames Wright         q[4] = 0;
1569529d636SJames Wright       }
1579529d636SJames Wright     } break;
1589529d636SJames Wright   }
1599529d636SJames Wright   return 0;
1609529d636SJames Wright }
1619529d636SJames Wright 
1629529d636SJames Wright // *****************************************************************************
163a515125bSLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
164a515125bSLeila Ghaffari // *****************************************************************************
1652b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
166a515125bSLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
167a515125bSLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
168a515125bSLeila Ghaffari 
1693d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
170a515125bSLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
171139613f2SLeila Ghaffari     CeedScalar       q[5] = {0.};
172a515125bSLeila Ghaffari 
1730b3a1fabSJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
174a515125bSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1750b3a1fabSJames Wright   }
176a515125bSLeila Ghaffari   return 0;
177a515125bSLeila Ghaffari }
178a515125bSLeila Ghaffari 
179a515125bSLeila Ghaffari // *****************************************************************************
1809529d636SJames Wright // This QFunction sets the initial conditions for 2D advection
181a515125bSLeila Ghaffari // *****************************************************************************
1829529d636SJames Wright CEED_QFUNCTION(ICsAdvection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1839529d636SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1849529d636SJames Wright   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
1859529d636SJames Wright   const SetupContextAdv context    = (SetupContextAdv)ctx;
1869529d636SJames Wright 
1879529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1889529d636SJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i]};
1899529d636SJames Wright     CeedScalar       q[5] = {0.};
1909529d636SJames Wright 
1919529d636SJames Wright     Exact_AdvectionGeneric(2, context->time, x, 5, q, ctx);
1929529d636SJames Wright     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1939529d636SJames Wright   }
194a515125bSLeila Ghaffari   return 0;
195a515125bSLeila Ghaffari }
196a515125bSLeila Ghaffari 
1979529d636SJames Wright CEED_QFUNCTION_HELPER void QdataUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) {
1989529d636SJames Wright   switch (N) {
1999529d636SJames Wright     case 2:
2009529d636SJames Wright       QdataUnpack_2D(Q, i, q_data, wdetJ, (CeedScalar(*)[2])dXdx);
2019529d636SJames Wright       break;
2029529d636SJames Wright     case 3:
2039529d636SJames Wright       QdataUnpack_3D(Q, i, q_data, wdetJ, (CeedScalar(*)[3])dXdx);
2049529d636SJames Wright       break;
2059529d636SJames Wright   }
2069529d636SJames Wright }
2079529d636SJames Wright 
2089529d636SJames Wright CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx,
2099529d636SJames Wright                                                  CeedScalar *normal) {
2109529d636SJames Wright   switch (N) {
2119529d636SJames Wright     case 2:
2129529d636SJames Wright       QdataBoundaryUnpack_2D(Q, i, q_data, wdetJ, normal);
2139529d636SJames Wright       break;
2149529d636SJames Wright     case 3:
2159529d636SJames Wright       QdataBoundaryUnpack_3D(Q, i, q_data, wdetJ, (CeedScalar(*)[3])dXdx, normal);
2169529d636SJames Wright       break;
2179529d636SJames Wright   }
2189529d636SJames Wright   return CEED_ERROR_SUCCESS;
2199529d636SJames Wright }
2209529d636SJames Wright 
2219529d636SJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_ND(CeedInt N, CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
2229529d636SJames Wright                                                                  StateVariable state_var, const CeedScalar *grad_q, const CeedScalar *dXdx,
2239529d636SJames Wright                                                                  State *grad_s) {
2249529d636SJames Wright   switch (N) {
2259529d636SJames Wright     case 2: {
2269529d636SJames Wright       for (CeedInt k = 0; k < 2; k++) {
2279529d636SJames Wright         CeedScalar dqi[5];
2289529d636SJames Wright         for (CeedInt j = 0; j < 5; j++) {
2299529d636SJames Wright           dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0 * N + k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1 * N + k];
2309529d636SJames Wright         }
2319529d636SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
2329529d636SJames Wright       }
2339529d636SJames Wright       CeedScalar U[5] = {0.};
2349529d636SJames Wright       grad_s[2]       = StateFromU(gas, U);
2359529d636SJames Wright     } break;
2369529d636SJames Wright     case 3:
2379529d636SJames Wright       StatePhysicalGradientFromReference(Q, i, gas, s, state_var, grad_q, (CeedScalar(*)[3])dXdx, grad_s);
2389529d636SJames Wright       break;
2399529d636SJames Wright   }
2409529d636SJames Wright }
2419529d636SJames Wright 
2429529d636SJames Wright // *****************************************************************************
2439529d636SJames Wright // This QFunction implements Advection for implicit time stepping method
2449529d636SJames Wright // *****************************************************************************
2459529d636SJames Wright CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
2469529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2479529d636SJames Wright   const CeedScalar(*grad_q)            = in[1];
2489529d636SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
2499529d636SJames Wright   const CeedScalar(*q_data)            = in[3];
2509529d636SJames Wright 
2519529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2529529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2539529d636SJames Wright   CeedScalar *jac_data               = out[2];
2549529d636SJames Wright 
2559529d636SJames Wright   AdvectionContext                 context   = (AdvectionContext)ctx;
2569529d636SJames Wright   const CeedScalar                 CtauS     = context->CtauS;
2579529d636SJames Wright   const CeedScalar                 zeros[14] = {0.};
2589529d636SJames Wright   NewtonianIdealGasContext         gas;
2599529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
2609529d636SJames Wright   gas                                         = &gas_struct;
2619529d636SJames Wright 
2629529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2639529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2649529d636SJames Wright     const State      s     = StateFromU(gas, qi);
2659529d636SJames Wright 
2669529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
2679529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
2689529d636SJames Wright     State grad_s[3];
2699529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
2709529d636SJames Wright 
2719529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
2729529d636SJames Wright 
2739529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
2749529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
2759529d636SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
2769529d636SJames Wright     }
2779529d636SJames Wright 
2789529d636SJames Wright     CeedScalar div_u = 0;
2799529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
2809529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
2819529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
2829529d636SJames Wright       }
2839529d636SJames Wright     }
2849529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
2859529d636SJames Wright     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
2869529d636SJames Wright 
2879529d636SJames Wright     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
2889529d636SJames Wright 
2899529d636SJames Wright     CeedScalar uX[3] = {0.};
2909529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
2919529d636SJames Wright 
2929529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
2939529d636SJames Wright       v[4][i] += wdetJ * strong_conv;
2949529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
2959529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j];
2969529d636SJames Wright     }
2979529d636SJames Wright 
29857272ee0SJames Wright     CeedScalar TauS = 0;
29957272ee0SJames Wright     switch (context->stabilization_tau) {
30057272ee0SJames Wright       case STAB_TAU_CTAU:
30157272ee0SJames Wright         TauS = CtauS / sqrt(Dot3(uX, uX));
30257272ee0SJames Wright         break;
30357272ee0SJames Wright       case STAB_TAU_ADVDIFF_SHAKIB: {
30457272ee0SJames Wright         CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.};
30557272ee0SJames Wright         MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
30657272ee0SJames Wright 
30757272ee0SJames Wright         MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj);
30857272ee0SJames Wright         TauS = 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a);
30957272ee0SJames Wright       } break;
31057272ee0SJames Wright     }
31157272ee0SJames Wright 
3129529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
3139529d636SJames Wright         case STAB_NONE:
3149529d636SJames Wright           break;
3159529d636SJames Wright         case STAB_SU:
3169529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
3179529d636SJames Wright           break;
3189529d636SJames Wright         case STAB_SUPG:
3199529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
3209529d636SJames Wright           break;
3219529d636SJames Wright       }
3229529d636SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
3239529d636SJames Wright   }
3249529d636SJames Wright }
3259529d636SJames Wright 
3262b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
327bd4b5413SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
328a515125bSLeila Ghaffari   return 0;
329a515125bSLeila Ghaffari }
330a515125bSLeila Ghaffari 
3319529d636SJames Wright CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3329529d636SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 2);
3339529d636SJames Wright   return 0;
3349529d636SJames Wright }
3359529d636SJames Wright 
3369529d636SJames Wright // *****************************************************************************
3379529d636SJames Wright // This QFunction implements Advection for explicit time stepping method
3389529d636SJames Wright // *****************************************************************************
3399529d636SJames Wright CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
3409529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3419529d636SJames Wright   const CeedScalar(*grad_q)        = in[1];
3429529d636SJames Wright   const CeedScalar(*q_data)        = in[2];
3439529d636SJames Wright 
3449529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3459529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3469529d636SJames Wright 
3479529d636SJames Wright   AdvectionContext                 context = (AdvectionContext)ctx;
3489529d636SJames Wright   const CeedScalar                 CtauS   = context->CtauS;
3499529d636SJames Wright   NewtonianIdealGasContext         gas;
3509529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
3519529d636SJames Wright   gas                                         = &gas_struct;
3529529d636SJames Wright 
3539529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3549529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3559529d636SJames Wright     const State      s     = StateFromU(gas, qi);
3569529d636SJames Wright 
3579529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
3589529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
3599529d636SJames Wright     State grad_s[3];
3609529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
3619529d636SJames Wright 
3629529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
3639529d636SJames Wright 
3649529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
3659529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
3669529d636SJames Wright       v[f][i] = 0.;
3679529d636SJames Wright     }
3689529d636SJames Wright 
3699529d636SJames Wright     CeedScalar div_u = 0;
3709529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
3719529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
3729529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
3739529d636SJames Wright       }
3749529d636SJames Wright     }
3759529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
3769529d636SJames Wright 
3779529d636SJames Wright     CeedScalar uX[3] = {0.};
3789529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
3799529d636SJames Wright 
3809529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
3819529d636SJames Wright       v[4][i] = -wdetJ * strong_conv;
3829529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0;
3839529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
3849529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j];
3859529d636SJames Wright       v[4][i] = 0.;
3869529d636SJames Wright     }
3879529d636SJames Wright 
3889529d636SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
3899529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
3909529d636SJames Wright         case STAB_NONE:
3919529d636SJames Wright           break;
3929529d636SJames Wright         case STAB_SU:
3939529d636SJames Wright         case STAB_SUPG:
3949d860eefSJames Wright           grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
3959529d636SJames Wright           break;
3969529d636SJames Wright       }
3979529d636SJames Wright   }
3989529d636SJames Wright }
3999529d636SJames Wright 
4009529d636SJames Wright CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4019529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3);
4029529d636SJames Wright   return 0;
4039529d636SJames Wright }
4049529d636SJames Wright 
4059529d636SJames Wright CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4069529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2);
4079529d636SJames Wright   return 0;
4089529d636SJames Wright }
4099529d636SJames Wright 
4109529d636SJames Wright // *****************************************************************************
4119529d636SJames Wright // This QFunction implements consistent outflow and inflow BCs
4129529d636SJames Wright //      for advection
4139529d636SJames Wright //
4149529d636SJames Wright //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
4159529d636SJames Wright //    sign(dot(wind, normal)) > 0 : outflow BCs
4169529d636SJames Wright //    sign(dot(wind, normal)) < 0 : inflow BCs
4179529d636SJames Wright //
4189529d636SJames Wright //  Outflow BCs:
4199529d636SJames Wright //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
4209529d636SJames Wright //
4219529d636SJames Wright //  Inflow BCs:
4229529d636SJames Wright //    A prescribed Total Energy (E_wind) is applied weakly.
4239529d636SJames Wright // *****************************************************************************
4249529d636SJames Wright CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
4259529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4269529d636SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
4279529d636SJames Wright 
4289529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
4299529d636SJames Wright   AdvectionContext context     = (AdvectionContext)ctx;
4309529d636SJames Wright   const CeedScalar E_wind      = context->E_wind;
4319529d636SJames Wright   const CeedScalar strong_form = context->strong_form;
4329529d636SJames Wright   const bool       is_implicit = context->implicit;
4339529d636SJames Wright 
4349529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
4359529d636SJames Wright     const CeedScalar rho  = q[0][i];
4369529d636SJames Wright     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
4379529d636SJames Wright     const CeedScalar E    = q[4][i];
4389529d636SJames Wright 
4399529d636SJames Wright     CeedScalar wdetJb, norm[3];
4409529d636SJames Wright     QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, norm);
4419529d636SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4429529d636SJames Wright 
4439529d636SJames Wright     const CeedScalar u_normal = DotN(norm, u, dim);
4449529d636SJames Wright 
4459529d636SJames Wright     // No Change in density or momentum
4469529d636SJames Wright     for (CeedInt j = 0; j < 4; j++) {
4479529d636SJames Wright       v[j][i] = 0;
4489529d636SJames Wright     }
4499529d636SJames Wright     // Implementing in/outflow BCs
4509529d636SJames Wright     if (u_normal > 0) {  // outflow
4519529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
4529529d636SJames Wright     } else {  // inflow
4539529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
4549529d636SJames Wright     }
4559529d636SJames Wright   }
4569529d636SJames Wright   return 0;
4579529d636SJames Wright }
4589529d636SJames Wright 
4592b916ea7SJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4608dba1efaSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 3);
461a515125bSLeila Ghaffari   return 0;
462a515125bSLeila Ghaffari }
463a515125bSLeila Ghaffari 
4649529d636SJames Wright CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4659529d636SJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 2);
4669529d636SJames Wright   return 0;
4679529d636SJames Wright }
4689529d636SJames Wright 
469a515125bSLeila Ghaffari #endif  // advection_h
470