15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 288626eedSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 388626eedSJames Wright // 488626eedSJames Wright // SPDX-License-Identifier: BSD-2-Clause 588626eedSJames Wright // 688626eedSJames Wright // This file is part of CEED: http://github.com/ceed 788626eedSJames Wright 888626eedSJames Wright /// @file 988626eedSJames Wright /// Operator for Navier-Stokes example using PETSc 10*c0b5abf0SJeremy L Thompson #include <ceed/types.h> 11*c0b5abf0SJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS 1288626eedSJames Wright #include <math.h> 13*c0b5abf0SJeremy L Thompson #include <stdbool.h> 14*c0b5abf0SJeremy L Thompson #endif 152b730f8bSJeremy L Thompson 16dc805cc4SLeila Ghaffari #include "newtonian_state.h" 17c9c2c079SJeremy L Thompson #include "newtonian_types.h" 1813fa47b2SJames Wright #include "utils.h" 1988626eedSJames Wright 2088626eedSJames Wright typedef struct ChannelContext_ *ChannelContext; 2188626eedSJames Wright struct ChannelContext_ { 2288626eedSJames Wright bool implicit; // !< Using implicit timesteping or not 2388626eedSJames Wright CeedScalar theta0; // !< Reference temperature 2488626eedSJames Wright CeedScalar P0; // !< Reference Pressure 2588626eedSJames Wright CeedScalar umax; // !< Centerline velocity 2688626eedSJames Wright CeedScalar center; // !< Y Coordinate for center of channel 2788626eedSJames Wright CeedScalar H; // !< Channel half-height 2888626eedSJames Wright CeedScalar B; // !< Body-force driving the flow 2988626eedSJames Wright struct NewtonianIdealGasContext_ newtonian_ctx; 3088626eedSJames Wright }; 3188626eedSJames Wright 322b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, void *ctx) { 3388626eedSJames Wright const ChannelContext context = (ChannelContext)ctx; 3488626eedSJames Wright const CeedScalar theta0 = context->theta0; 3588626eedSJames Wright const CeedScalar P0 = context->P0; 3688626eedSJames Wright const CeedScalar umax = context->umax; 3788626eedSJames Wright const CeedScalar center = context->center; 3888626eedSJames Wright const CeedScalar H = context->H; 39dc805cc4SLeila Ghaffari NewtonianIdealGasContext gas = &context->newtonian_ctx; 40dc805cc4SLeila Ghaffari const CeedScalar cp = gas->cp; 41dc805cc4SLeila Ghaffari const CeedScalar mu = gas->mu; 42dc805cc4SLeila Ghaffari const CeedScalar k = gas->k; 43dc805cc4SLeila Ghaffari // There is a gravity body force but it is excluded from 44dc805cc4SLeila Ghaffari // the potential energy due to periodicity. 452b89d87eSLeila Ghaffari // g = (g, 0, 0) 462b89d87eSLeila Ghaffari // x = (0, x_2, x_3) 472b89d87eSLeila Ghaffari // e_potential = dot(g, x) = 0 482b89d87eSLeila Ghaffari const CeedScalar x[3] = {0, X[1], X[2]}; 4988626eedSJames Wright 5088626eedSJames Wright const CeedScalar Pr = mu / (cp * k); 5188626eedSJames Wright const CeedScalar Ec = (umax * umax) / (cp * theta0); 522b730f8bSJeremy L Thompson const CeedScalar theta = theta0 * (1 + (Pr * Ec / 3) * (1 - Square(Square((x[1] - center) / H)))); 53dc805cc4SLeila Ghaffari CeedScalar Y[5] = {0.}; 54dc805cc4SLeila Ghaffari Y[0] = P0; 552b89d87eSLeila Ghaffari Y[1] = umax * (1 - Square((x[1] - center) / H)); 56dc805cc4SLeila Ghaffari Y[2] = 0.; 57dc805cc4SLeila Ghaffari Y[3] = 0.; 58dc805cc4SLeila Ghaffari Y[4] = theta; 5988626eedSJames Wright 603bd61617SKenneth E. Jansen return StateFromY(gas, Y); 6188626eedSJames Wright } 6288626eedSJames Wright 6388626eedSJames Wright // ***************************************************************************** 64dc805cc4SLeila Ghaffari // This QFunction set the initial condition 6588626eedSJames Wright // ***************************************************************************** 662b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 6788626eedSJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 6888626eedSJames Wright CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 6988626eedSJames Wright 70dc805cc4SLeila Ghaffari const ChannelContext context = (ChannelContext)ctx; 71a2d72b6fSJames Wright const NewtonianIdealGasContext gas = &context->newtonian_ctx; 72dc805cc4SLeila Ghaffari 732b730f8bSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 7488626eedSJames Wright const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 75dc805cc4SLeila Ghaffari State s = Exact_Channel(3, 0., x, 5, ctx); 762b89d87eSLeila Ghaffari CeedScalar q[5] = {0}; 77a2d72b6fSJames Wright StateToQ(gas, s, q, gas->state_var); 782b730f8bSJeremy L Thompson for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 79f0b01153SJames Wright } 8088626eedSJames Wright return 0; 8188626eedSJames Wright } 8288626eedSJames Wright 8388626eedSJames Wright // ***************************************************************************** 842b89d87eSLeila Ghaffari // This QFunction set the inflow boundary condition for conservative variables 852b89d87eSLeila Ghaffari // ***************************************************************************** 862b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 8746603fc5SJames Wright const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 88f3e15844SJames Wright const CeedScalar(*q_data_sur) = in[2]; 8946603fc5SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 9088626eedSJames Wright CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 9146603fc5SJames Wright 9288626eedSJames Wright const ChannelContext context = (ChannelContext)ctx; 93f3e15844SJames Wright const bool is_implicit = context->implicit; 942b89d87eSLeila Ghaffari NewtonianIdealGasContext gas = &context->newtonian_ctx; 9546603fc5SJames Wright const CeedScalar gamma = HeatCapacityRatio(&context->newtonian_ctx); 9688626eedSJames Wright 9746603fc5SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 98f3e15844SJames Wright CeedScalar wdetJb, norm[3]; 99f3e15844SJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 100f3e15844SJames Wright wdetJb *= is_implicit ? -1. : 1.; 10188626eedSJames Wright 1022b89d87eSLeila Ghaffari // There is a gravity body force but it is excluded from 1032b89d87eSLeila Ghaffari // the potential energy due to periodicity. 1042b89d87eSLeila Ghaffari // g = (g, 0, 0) 1052b89d87eSLeila Ghaffari // x = (0, x_2, x_3) 1062b89d87eSLeila Ghaffari // e_potential = dot(g, x) = 0 1072b89d87eSLeila Ghaffari const CeedScalar x[3] = {0, X[1][i], X[2][i]}; 1082b89d87eSLeila Ghaffari 109f21e6b1cSJames Wright // Calculate prescribed inflow values 1102b89d87eSLeila Ghaffari State s_exact = Exact_Channel(3, 0., x, 5, ctx); 11188626eedSJames Wright CeedScalar q_exact[5] = {0.}; 1122b89d87eSLeila Ghaffari UnpackState_U(s_exact.U, q_exact); 11388626eedSJames Wright 11488626eedSJames Wright // Find pressure using state inside the domain 1152b89d87eSLeila Ghaffari CeedScalar q_inside[5] = {0}; 1162b730f8bSJeremy L Thompson for (CeedInt j = 0; j < 5; j++) q_inside[j] = q[j][i]; 1173bd61617SKenneth E. Jansen State s_inside = StateFromU(gas, q_inside); 1182b89d87eSLeila Ghaffari const CeedScalar P = s_inside.Y.pressure; 11988626eedSJames Wright 12088626eedSJames Wright // Find inflow state using calculated P and prescribed velocity, theta0 1214c0e8230SJames Wright const CeedScalar e_internal = gas->cv * s_exact.Y.temperature; 12288626eedSJames Wright const CeedScalar rho_in = P / ((gamma - 1) * e_internal); 1232b730f8bSJeremy L Thompson const CeedScalar E_kinetic = .5 * rho_in * Dot3(s_exact.Y.velocity, s_exact.Y.velocity); 12488626eedSJames Wright const CeedScalar E = rho_in * e_internal + E_kinetic; 1252b89d87eSLeila Ghaffari 12688626eedSJames Wright // The Physics 12788626eedSJames Wright // Zero v so all future terms can safely sum into it 128ba6664aeSJames Wright for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 12988626eedSJames Wright 1302b89d87eSLeila Ghaffari const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity); 13188626eedSJames Wright 13288626eedSJames Wright // The Physics 13388626eedSJames Wright // -- Density 13488626eedSJames Wright v[0][i] -= wdetJb * rho_in * u_normal; 13588626eedSJames Wright 13688626eedSJames Wright // -- Momentum 1372b730f8bSJeremy L Thompson for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_in * u_normal * s_exact.Y.velocity[j] + norm[j] * P); 13888626eedSJames Wright 13988626eedSJames Wright // -- Total Energy Density 14088626eedSJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 1414c0e8230SJames Wright } 14288626eedSJames Wright return 0; 14388626eedSJames Wright } 14488626eedSJames Wright 14588626eedSJames Wright // ***************************************************************************** 1462b89d87eSLeila Ghaffari // This QFunction set the outflow boundary condition for conservative variables 1472b89d87eSLeila Ghaffari // ***************************************************************************** 1482b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 14946603fc5SJames Wright const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 150f3e15844SJames Wright const CeedScalar(*q_data_sur) = in[2]; 15188626eedSJames Wright CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 15288626eedSJames Wright 15388626eedSJames Wright const ChannelContext context = (ChannelContext)ctx; 154f3e15844SJames Wright const bool is_implicit = context->implicit; 15588626eedSJames Wright 15646603fc5SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 157f3e15844SJames Wright CeedScalar wdetJb, norm[3]; 158f3e15844SJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 159f3e15844SJames Wright wdetJb *= is_implicit ? -1. : 1.; 160f3e15844SJames Wright 16188626eedSJames Wright const CeedScalar rho = q[0][i]; 1622b730f8bSJeremy L Thompson const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 16388626eedSJames Wright const CeedScalar E = q[4][i]; 16488626eedSJames Wright 16588626eedSJames Wright // The Physics 16688626eedSJames Wright // Zero v so all future terms can safely sum into it 167ba6664aeSJames Wright for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 16888626eedSJames Wright 16988626eedSJames Wright // Implementing outflow condition 1704c0e8230SJames Wright const CeedScalar P = context->P0; // pressure 17113fa47b2SJames Wright const CeedScalar u_normal = Dot3(norm, u); // Normal velocity 17288626eedSJames Wright // The Physics 17388626eedSJames Wright // -- Density 17488626eedSJames Wright v[0][i] -= wdetJb * rho * u_normal; 17588626eedSJames Wright 17688626eedSJames Wright // -- Momentum 1772b730f8bSJeremy L Thompson for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P); 17888626eedSJames Wright 17988626eedSJames Wright // -- Total Energy Density 18088626eedSJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 1814c0e8230SJames Wright } 18288626eedSJames Wright return 0; 18388626eedSJames Wright } 184