1bb8a0c61SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2bb8a0c61SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3bb8a0c61SJames Wright // 4bb8a0c61SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5bb8a0c61SJames Wright // 6bb8a0c61SJames Wright // This file is part of CEED: http://github.com/ceed 7bb8a0c61SJames Wright 8bb8a0c61SJames Wright /// @file 9bb8a0c61SJames Wright /// Operator for Navier-Stokes example using PETSc 10bb8a0c61SJames Wright 11bb8a0c61SJames Wright 12bb8a0c61SJames Wright #ifndef channel_h 13bb8a0c61SJames Wright #define channel_h 14bb8a0c61SJames Wright 15bb8a0c61SJames Wright #include <math.h> 16c58dce4fSJed Brown #include <ceed/ceed.h> 1715a3537eSJed Brown #include "newtonian_types.h" 18cbe60e31SLeila Ghaffari #include "newtonian_state.h" 19704b8bbeSJames Wright #include "utils.h" 20bb8a0c61SJames Wright 21bb8a0c61SJames Wright typedef struct ChannelContext_ *ChannelContext; 22bb8a0c61SJames Wright struct ChannelContext_ { 23bb8a0c61SJames Wright bool implicit; // !< Using implicit timesteping or not 24bb8a0c61SJames Wright CeedScalar theta0; // !< Reference temperature 25bb8a0c61SJames Wright CeedScalar P0; // !< Reference Pressure 26bb8a0c61SJames Wright CeedScalar umax; // !< Centerline velocity 27bb8a0c61SJames Wright CeedScalar center; // !< Y Coordinate for center of channel 28bb8a0c61SJames Wright CeedScalar H; // !< Channel half-height 29bb8a0c61SJames Wright CeedScalar B; // !< Body-force driving the flow 30bb8a0c61SJames Wright struct NewtonianIdealGasContext_ newtonian_ctx; 31bb8a0c61SJames Wright }; 32bb8a0c61SJames Wright 33cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time, 34cbe60e31SLeila Ghaffari const CeedScalar X[], CeedInt Nf, void *ctx) { 35bb8a0c61SJames Wright 36bb8a0c61SJames Wright const ChannelContext context = (ChannelContext)ctx; 37bb8a0c61SJames Wright const CeedScalar theta0 = context->theta0; 38bb8a0c61SJames Wright const CeedScalar P0 = context->P0; 39bb8a0c61SJames Wright const CeedScalar umax = context->umax; 40bb8a0c61SJames Wright const CeedScalar center = context->center; 41bb8a0c61SJames Wright const CeedScalar H = context->H; 42cbe60e31SLeila Ghaffari NewtonianIdealGasContext gas = &context->newtonian_ctx; 43cbe60e31SLeila Ghaffari const CeedScalar cp = gas->cp; 44cbe60e31SLeila Ghaffari const CeedScalar mu = gas->mu; 45cbe60e31SLeila Ghaffari const CeedScalar k = gas->k; 46cbe60e31SLeila Ghaffari // There is a gravity body force but it is excluded from 47cbe60e31SLeila Ghaffari // the potential energy due to periodicity. 48*d1b9ef12SLeila Ghaffari // g = (g, 0, 0) 49*d1b9ef12SLeila Ghaffari // x = (0, x_2, x_3) 50*d1b9ef12SLeila Ghaffari // e_potential = dot(g, x) = 0 51*d1b9ef12SLeila Ghaffari const CeedScalar x[3] = {0, X[1], X[2]}; 52bb8a0c61SJames Wright 53bb8a0c61SJames Wright const CeedScalar Pr = mu / (cp*k); 54bb8a0c61SJames Wright const CeedScalar Ec = (umax*umax) / (cp*theta0); 55c58dce4fSJed Brown const CeedScalar theta = theta0*(1 + (Pr*Ec/3) 56*d1b9ef12SLeila Ghaffari * (1 - Square(Square((x[1]-center)/H)))); 57cbe60e31SLeila Ghaffari CeedScalar Y[5] = {0.}; 58cbe60e31SLeila Ghaffari Y[0] = P0; 59*d1b9ef12SLeila Ghaffari Y[1] = umax*(1 - Square((x[1]-center)/H)); 60cbe60e31SLeila Ghaffari Y[2] = 0.; 61cbe60e31SLeila Ghaffari Y[3] = 0.; 62cbe60e31SLeila Ghaffari Y[4] = theta; 63bb8a0c61SJames Wright 64*d1b9ef12SLeila Ghaffari return StateFromY(gas, Y, x); 65bb8a0c61SJames Wright } 66bb8a0c61SJames Wright 67bb8a0c61SJames Wright // ***************************************************************************** 68cbe60e31SLeila Ghaffari // This QFunction set the initial condition 69bb8a0c61SJames Wright // ***************************************************************************** 70bb8a0c61SJames Wright CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q, 71bb8a0c61SJames Wright const CeedScalar *const *in, CeedScalar *const *out) { 72bb8a0c61SJames Wright // Inputs 73bb8a0c61SJames Wright const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 74bb8a0c61SJames Wright 75bb8a0c61SJames Wright // Outputs 76bb8a0c61SJames Wright CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 77bb8a0c61SJames Wright 78cbe60e31SLeila Ghaffari // Context 79cbe60e31SLeila Ghaffari const ChannelContext context = (ChannelContext)ctx; 80cbe60e31SLeila Ghaffari 81bb8a0c61SJames Wright // Quadrature Point Loop 82bb8a0c61SJames Wright CeedPragmaSIMD 83bb8a0c61SJames Wright for (CeedInt i=0; i<Q; i++) { 84bb8a0c61SJames Wright const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 85cbe60e31SLeila Ghaffari State s = Exact_Channel(3, 0., x, 5, ctx); 86*d1b9ef12SLeila Ghaffari CeedScalar q[5] = {0}; 87*d1b9ef12SLeila Ghaffari if (context->newtonian_ctx.is_primitive) 88*d1b9ef12SLeila Ghaffari UnpackState_Y(s.Y, q); 89*d1b9ef12SLeila Ghaffari else 90*d1b9ef12SLeila Ghaffari UnpackState_U(s.U, q); 91*d1b9ef12SLeila Ghaffari 92*d1b9ef12SLeila Ghaffari for (CeedInt j=0; j<5; j++) 93*d1b9ef12SLeila Ghaffari q0[j][i] = q[j]; 94bb8a0c61SJames Wright 95bb8a0c61SJames Wright } // End of Quadrature Point Loop 96bb8a0c61SJames Wright return 0; 97bb8a0c61SJames Wright } 98bb8a0c61SJames Wright 99bb8a0c61SJames Wright // ***************************************************************************** 100*d1b9ef12SLeila Ghaffari // This QFunction set the inflow boundary condition for conservative variables 101*d1b9ef12SLeila Ghaffari // ***************************************************************************** 102bb8a0c61SJames Wright CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, 103bb8a0c61SJames Wright const CeedScalar *const *in, 104bb8a0c61SJames Wright CeedScalar *const *out) { 105bb8a0c61SJames Wright // *INDENT-OFF* 106bb8a0c61SJames Wright // Inputs 107bb8a0c61SJames Wright const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 108dd64951cSJames Wright (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], 109dd64951cSJames Wright (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 110bb8a0c61SJames Wright 111bb8a0c61SJames Wright // Outputs 112bb8a0c61SJames Wright CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 113bb8a0c61SJames Wright // *INDENT-ON* 114bb8a0c61SJames Wright const ChannelContext context = (ChannelContext)ctx; 115bb8a0c61SJames Wright const bool implicit = context->implicit; 116*d1b9ef12SLeila Ghaffari NewtonianIdealGasContext gas = &context->newtonian_ctx; 117*d1b9ef12SLeila Ghaffari const CeedScalar cv = gas->cv; 118*d1b9ef12SLeila Ghaffari const CeedScalar cp = gas->cp; 119bb8a0c61SJames Wright const CeedScalar gamma = cp / cv; 120bb8a0c61SJames Wright 121bb8a0c61SJames Wright CeedPragmaSIMD 122bb8a0c61SJames Wright // Quadrature Point Loop 123bb8a0c61SJames Wright for (CeedInt i=0; i<Q; i++) { 124bb8a0c61SJames Wright // Setup 125bb8a0c61SJames Wright // -- Interp-to-Interp q_data 126bb8a0c61SJames Wright // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q). 127bb8a0c61SJames Wright // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q). 128bb8a0c61SJames Wright // We can effect this by swapping the sign on this weight 129bb8a0c61SJames Wright const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i]; 130bb8a0c61SJames Wright 131*d1b9ef12SLeila Ghaffari // There is a gravity body force but it is excluded from 132*d1b9ef12SLeila Ghaffari // the potential energy due to periodicity. 133*d1b9ef12SLeila Ghaffari // g = (g, 0, 0) 134*d1b9ef12SLeila Ghaffari // x = (0, x_2, x_3) 135*d1b9ef12SLeila Ghaffari // e_potential = dot(g, x) = 0 136*d1b9ef12SLeila Ghaffari const CeedScalar x[3] = {0, X[1][i], X[2][i]}; 137*d1b9ef12SLeila Ghaffari 138bb8a0c61SJames Wright // Calcualte prescribed inflow values 139*d1b9ef12SLeila Ghaffari State s_exact = Exact_Channel(3, 0., x, 5, ctx); 140bb8a0c61SJames Wright CeedScalar q_exact[5] = {0.}; 141*d1b9ef12SLeila Ghaffari UnpackState_U(s_exact.U, q_exact); 142bb8a0c61SJames Wright 143bb8a0c61SJames Wright // Find pressure using state inside the domain 144*d1b9ef12SLeila Ghaffari CeedScalar q_inside[5] = {0}; 145*d1b9ef12SLeila Ghaffari for (CeedInt j; j<5; j++) 146*d1b9ef12SLeila Ghaffari q_inside[j] = q[j][i]; 147*d1b9ef12SLeila Ghaffari State s_inside = StateFromU(gas, q_inside, x); 148*d1b9ef12SLeila Ghaffari const CeedScalar P = s_inside.Y.pressure; 149bb8a0c61SJames Wright 150bb8a0c61SJames Wright // Find inflow state using calculated P and prescribed velocity, theta0 151*d1b9ef12SLeila Ghaffari const CeedScalar e_internal = cv * s_exact.Y.temperature; 152bb8a0c61SJames Wright const CeedScalar rho_in = P / ((gamma - 1) * e_internal); 153*d1b9ef12SLeila Ghaffari const CeedScalar E_kinetic = .5 * rho_in * Dot3(s_exact.Y.velocity, 154*d1b9ef12SLeila Ghaffari s_exact.Y.velocity); 155bb8a0c61SJames Wright const CeedScalar E = rho_in * e_internal + E_kinetic; 156*d1b9ef12SLeila Ghaffari 157bb8a0c61SJames Wright // ---- Normal vect 158bb8a0c61SJames Wright const CeedScalar norm[3] = {q_data_sur[1][i], 159bb8a0c61SJames Wright q_data_sur[2][i], 160bb8a0c61SJames Wright q_data_sur[3][i] 161bb8a0c61SJames Wright }; 162bb8a0c61SJames Wright // The Physics 163bb8a0c61SJames Wright // Zero v so all future terms can safely sum into it 164493642f1SJames Wright for (CeedInt j=0; j<5; j++) v[j][i] = 0.; 165bb8a0c61SJames Wright 166*d1b9ef12SLeila Ghaffari const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity); 167bb8a0c61SJames Wright 168bb8a0c61SJames Wright // The Physics 169bb8a0c61SJames Wright // -- Density 170bb8a0c61SJames Wright v[0][i] -= wdetJb * rho_in * u_normal; 171bb8a0c61SJames Wright 172bb8a0c61SJames Wright // -- Momentum 173493642f1SJames Wright for (CeedInt j=0; j<3; j++) 174*d1b9ef12SLeila Ghaffari v[j+1][i] -= wdetJb * (rho_in * u_normal * s_exact.Y.velocity[j] + 175bb8a0c61SJames Wright norm[j] * P); 176bb8a0c61SJames Wright 177bb8a0c61SJames Wright // -- Total Energy Density 178bb8a0c61SJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 179bb8a0c61SJames Wright 180bb8a0c61SJames Wright } // End Quadrature Point Loop 181bb8a0c61SJames Wright return 0; 182bb8a0c61SJames Wright } 183bb8a0c61SJames Wright 184bb8a0c61SJames Wright // ***************************************************************************** 185*d1b9ef12SLeila Ghaffari // This QFunction set the outflow boundary condition for conservative variables 186*d1b9ef12SLeila Ghaffari // ***************************************************************************** 187bb8a0c61SJames Wright CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, 188bb8a0c61SJames Wright const CeedScalar *const *in, 189bb8a0c61SJames Wright CeedScalar *const *out) { 190bb8a0c61SJames Wright // *INDENT-OFF* 191bb8a0c61SJames Wright // Inputs 192bb8a0c61SJames Wright const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 193dd64951cSJames Wright (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 194dd64951cSJames Wright 195bb8a0c61SJames Wright // Outputs 196bb8a0c61SJames Wright CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 197bb8a0c61SJames Wright // *INDENT-ON* 198bb8a0c61SJames Wright 199bb8a0c61SJames Wright const ChannelContext context = (ChannelContext)ctx; 200bb8a0c61SJames Wright const bool implicit = context->implicit; 201bb8a0c61SJames Wright const CeedScalar P0 = context->P0; 202bb8a0c61SJames Wright 203bb8a0c61SJames Wright CeedPragmaSIMD 204bb8a0c61SJames Wright // Quadrature Point Loop 205bb8a0c61SJames Wright for (CeedInt i=0; i<Q; i++) { 206bb8a0c61SJames Wright // Setup 207bb8a0c61SJames Wright // -- Interp in 208bb8a0c61SJames Wright const CeedScalar rho = q[0][i]; 209bb8a0c61SJames Wright const CeedScalar u[3] = {q[1][i] / rho, 210bb8a0c61SJames Wright q[2][i] / rho, 211bb8a0c61SJames Wright q[3][i] / rho 212bb8a0c61SJames Wright }; 213bb8a0c61SJames Wright const CeedScalar E = q[4][i]; 214bb8a0c61SJames Wright 215bb8a0c61SJames Wright // -- Interp-to-Interp q_data 216bb8a0c61SJames Wright // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q). 217bb8a0c61SJames Wright // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q). 218bb8a0c61SJames Wright // We can effect this by swapping the sign on this weight 219bb8a0c61SJames Wright const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i]; 220bb8a0c61SJames Wright 221bb8a0c61SJames Wright // ---- Normal vect 222bb8a0c61SJames Wright const CeedScalar norm[3] = {q_data_sur[1][i], 223bb8a0c61SJames Wright q_data_sur[2][i], 224bb8a0c61SJames Wright q_data_sur[3][i] 225bb8a0c61SJames Wright }; 226bb8a0c61SJames Wright // The Physics 227bb8a0c61SJames Wright // Zero v so all future terms can safely sum into it 228493642f1SJames Wright for (CeedInt j=0; j<5; j++) v[j][i] = 0.; 229bb8a0c61SJames Wright 230bb8a0c61SJames Wright // Implementing outflow condition 231bb8a0c61SJames Wright const CeedScalar P = P0; // pressure 232704b8bbeSJames Wright const CeedScalar u_normal = Dot3(norm, u); // Normal velocity 233bb8a0c61SJames Wright // The Physics 234bb8a0c61SJames Wright // -- Density 235bb8a0c61SJames Wright v[0][i] -= wdetJb * rho * u_normal; 236bb8a0c61SJames Wright 237bb8a0c61SJames Wright // -- Momentum 238493642f1SJames Wright for (CeedInt j=0; j<3; j++) 239bb8a0c61SJames Wright v[j+1][i] -= wdetJb *(rho * u_normal * u[j] + norm[j] * P); 240bb8a0c61SJames Wright 241bb8a0c61SJames Wright // -- Total Energy Density 242bb8a0c61SJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 243bb8a0c61SJames Wright 244bb8a0c61SJames Wright } // End Quadrature Point Loop 245bb8a0c61SJames Wright return 0; 246bb8a0c61SJames Wright } 247cbe60e31SLeila Ghaffari 248cbe60e31SLeila Ghaffari // ***************************************************************************** 249bb8a0c61SJames Wright #endif // channel_h 250