1 // Copyright (c) 2017-2024, 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 /// @file 9 /// Operator for Navier-Stokes example using PETSc 10 #include <ceed.h> 11 #include <math.h> 12 13 #include "newtonian_state.h" 14 #include "newtonian_types.h" 15 #include "utils.h" 16 17 typedef struct ChannelContext_ *ChannelContext; 18 struct ChannelContext_ { 19 bool implicit; // !< Using implicit timesteping or not 20 CeedScalar theta0; // !< Reference temperature 21 CeedScalar P0; // !< Reference Pressure 22 CeedScalar umax; // !< Centerline velocity 23 CeedScalar center; // !< Y Coordinate for center of channel 24 CeedScalar H; // !< Channel half-height 25 CeedScalar B; // !< Body-force driving the flow 26 struct NewtonianIdealGasContext_ newtonian_ctx; 27 }; 28 29 CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, void *ctx) { 30 const ChannelContext context = (ChannelContext)ctx; 31 const CeedScalar theta0 = context->theta0; 32 const CeedScalar P0 = context->P0; 33 const CeedScalar umax = context->umax; 34 const CeedScalar center = context->center; 35 const CeedScalar H = context->H; 36 NewtonianIdealGasContext gas = &context->newtonian_ctx; 37 const CeedScalar cp = gas->cp; 38 const CeedScalar mu = gas->mu; 39 const CeedScalar k = gas->k; 40 // There is a gravity body force but it is excluded from 41 // the potential energy due to periodicity. 42 // g = (g, 0, 0) 43 // x = (0, x_2, x_3) 44 // e_potential = dot(g, x) = 0 45 const CeedScalar x[3] = {0, X[1], X[2]}; 46 47 const CeedScalar Pr = mu / (cp * k); 48 const CeedScalar Ec = (umax * umax) / (cp * theta0); 49 const CeedScalar theta = theta0 * (1 + (Pr * Ec / 3) * (1 - Square(Square((x[1] - center) / H)))); 50 CeedScalar Y[5] = {0.}; 51 Y[0] = P0; 52 Y[1] = umax * (1 - Square((x[1] - center) / H)); 53 Y[2] = 0.; 54 Y[3] = 0.; 55 Y[4] = theta; 56 57 return StateFromY(gas, Y); 58 } 59 60 // ***************************************************************************** 61 // This QFunction set the initial condition 62 // ***************************************************************************** 63 CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 64 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 65 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 66 67 const ChannelContext context = (ChannelContext)ctx; 68 69 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 70 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 71 State s = Exact_Channel(3, 0., x, 5, ctx); 72 CeedScalar q[5] = {0}; 73 switch (context->newtonian_ctx.state_var) { 74 case STATEVAR_CONSERVATIVE: 75 UnpackState_U(s.U, q); 76 break; 77 case STATEVAR_PRIMITIVE: 78 UnpackState_Y(s.Y, q); 79 break; 80 } 81 82 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 83 } 84 return 0; 85 } 86 87 // ***************************************************************************** 88 // This QFunction set the inflow boundary condition for conservative variables 89 // ***************************************************************************** 90 CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 91 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 92 const CeedScalar(*q_data_sur) = in[2]; 93 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 94 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 95 96 const ChannelContext context = (ChannelContext)ctx; 97 const bool is_implicit = context->implicit; 98 NewtonianIdealGasContext gas = &context->newtonian_ctx; 99 const CeedScalar gamma = HeatCapacityRatio(&context->newtonian_ctx); 100 101 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 102 CeedScalar wdetJb, norm[3]; 103 QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 104 wdetJb *= is_implicit ? -1. : 1.; 105 106 // There is a gravity body force but it is excluded from 107 // the potential energy due to periodicity. 108 // g = (g, 0, 0) 109 // x = (0, x_2, x_3) 110 // e_potential = dot(g, x) = 0 111 const CeedScalar x[3] = {0, X[1][i], X[2][i]}; 112 113 // Calculate prescribed inflow values 114 State s_exact = Exact_Channel(3, 0., x, 5, ctx); 115 CeedScalar q_exact[5] = {0.}; 116 UnpackState_U(s_exact.U, q_exact); 117 118 // Find pressure using state inside the domain 119 CeedScalar q_inside[5] = {0}; 120 for (CeedInt j = 0; j < 5; j++) q_inside[j] = q[j][i]; 121 State s_inside = StateFromU(gas, q_inside); 122 const CeedScalar P = s_inside.Y.pressure; 123 124 // Find inflow state using calculated P and prescribed velocity, theta0 125 const CeedScalar e_internal = gas->cv * s_exact.Y.temperature; 126 const CeedScalar rho_in = P / ((gamma - 1) * e_internal); 127 const CeedScalar E_kinetic = .5 * rho_in * Dot3(s_exact.Y.velocity, s_exact.Y.velocity); 128 const CeedScalar E = rho_in * e_internal + E_kinetic; 129 130 // The Physics 131 // Zero v so all future terms can safely sum into it 132 for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 133 134 const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity); 135 136 // The Physics 137 // -- Density 138 v[0][i] -= wdetJb * rho_in * u_normal; 139 140 // -- Momentum 141 for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_in * u_normal * s_exact.Y.velocity[j] + norm[j] * P); 142 143 // -- Total Energy Density 144 v[4][i] -= wdetJb * u_normal * (E + P); 145 } 146 return 0; 147 } 148 149 // ***************************************************************************** 150 // This QFunction set the outflow boundary condition for conservative variables 151 // ***************************************************************************** 152 CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 153 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 154 const CeedScalar(*q_data_sur) = in[2]; 155 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 156 157 const ChannelContext context = (ChannelContext)ctx; 158 const bool is_implicit = context->implicit; 159 160 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 161 CeedScalar wdetJb, norm[3]; 162 QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 163 wdetJb *= is_implicit ? -1. : 1.; 164 165 const CeedScalar rho = q[0][i]; 166 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 167 const CeedScalar E = q[4][i]; 168 169 // The Physics 170 // Zero v so all future terms can safely sum into it 171 for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 172 173 // Implementing outflow condition 174 const CeedScalar P = context->P0; // pressure 175 const CeedScalar u_normal = Dot3(norm, u); // Normal velocity 176 // The Physics 177 // -- Density 178 v[0][i] -= wdetJb * rho * u_normal; 179 180 // -- Momentum 181 for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P); 182 183 // -- Total Energy Density 184 v[4][i] -= wdetJb * u_normal * (E + P); 185 } 186 return 0; 187 } 188