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 const NewtonianIdealGasContext gas = &context->newtonian_ctx; 69 70 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 71 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 72 State s = Exact_Channel(3, 0., x, 5, ctx); 73 CeedScalar q[5] = {0}; 74 StateToQ(gas, s, q, gas->state_var); 75 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 76 } 77 return 0; 78 } 79 80 // ***************************************************************************** 81 // This QFunction set the inflow boundary condition for conservative variables 82 // ***************************************************************************** 83 CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 84 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 85 const CeedScalar(*q_data_sur) = in[2]; 86 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 87 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 88 89 const ChannelContext context = (ChannelContext)ctx; 90 const bool is_implicit = context->implicit; 91 NewtonianIdealGasContext gas = &context->newtonian_ctx; 92 const CeedScalar gamma = HeatCapacityRatio(&context->newtonian_ctx); 93 94 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 95 CeedScalar wdetJb, norm[3]; 96 QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 97 wdetJb *= is_implicit ? -1. : 1.; 98 99 // There is a gravity body force but it is excluded from 100 // the potential energy due to periodicity. 101 // g = (g, 0, 0) 102 // x = (0, x_2, x_3) 103 // e_potential = dot(g, x) = 0 104 const CeedScalar x[3] = {0, X[1][i], X[2][i]}; 105 106 // Calculate prescribed inflow values 107 State s_exact = Exact_Channel(3, 0., x, 5, ctx); 108 CeedScalar q_exact[5] = {0.}; 109 UnpackState_U(s_exact.U, q_exact); 110 111 // Find pressure using state inside the domain 112 CeedScalar q_inside[5] = {0}; 113 for (CeedInt j = 0; j < 5; j++) q_inside[j] = q[j][i]; 114 State s_inside = StateFromU(gas, q_inside); 115 const CeedScalar P = s_inside.Y.pressure; 116 117 // Find inflow state using calculated P and prescribed velocity, theta0 118 const CeedScalar e_internal = gas->cv * s_exact.Y.temperature; 119 const CeedScalar rho_in = P / ((gamma - 1) * e_internal); 120 const CeedScalar E_kinetic = .5 * rho_in * Dot3(s_exact.Y.velocity, s_exact.Y.velocity); 121 const CeedScalar E = rho_in * e_internal + E_kinetic; 122 123 // The Physics 124 // Zero v so all future terms can safely sum into it 125 for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 126 127 const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity); 128 129 // The Physics 130 // -- Density 131 v[0][i] -= wdetJb * rho_in * u_normal; 132 133 // -- Momentum 134 for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_in * u_normal * s_exact.Y.velocity[j] + norm[j] * P); 135 136 // -- Total Energy Density 137 v[4][i] -= wdetJb * u_normal * (E + P); 138 } 139 return 0; 140 } 141 142 // ***************************************************************************** 143 // This QFunction set the outflow boundary condition for conservative variables 144 // ***************************************************************************** 145 CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 146 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 147 const CeedScalar(*q_data_sur) = in[2]; 148 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 149 150 const ChannelContext context = (ChannelContext)ctx; 151 const bool is_implicit = context->implicit; 152 153 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 154 CeedScalar wdetJb, norm[3]; 155 QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 156 wdetJb *= is_implicit ? -1. : 1.; 157 158 const CeedScalar rho = q[0][i]; 159 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 160 const CeedScalar E = q[4][i]; 161 162 // The Physics 163 // Zero v so all future terms can safely sum into it 164 for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 165 166 // Implementing outflow condition 167 const CeedScalar P = context->P0; // pressure 168 const CeedScalar u_normal = Dot3(norm, u); // Normal velocity 169 // The Physics 170 // -- Density 171 v[0][i] -= wdetJb * rho * u_normal; 172 173 // -- Momentum 174 for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P); 175 176 // -- Total Energy Density 177 v[4][i] -= wdetJb * u_normal * (E + P); 178 } 179 return 0; 180 } 181