// Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. // // SPDX-License-Identifier: BSD-2-Clause // // This file is part of CEED: http://github.com/ceed /// @file /// Operator for Navier-Stokes example using PETSc #ifndef newtonian_h #define newtonian_h #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #ifndef setup_context_struct #define setup_context_struct typedef struct SetupContext_ *SetupContext; struct SetupContext_ { CeedScalar theta0; CeedScalar thetaC; CeedScalar P0; CeedScalar N; CeedScalar cv; CeedScalar cp; CeedScalar g; CeedScalar rc; CeedScalar lx; CeedScalar ly; CeedScalar lz; CeedScalar center[3]; CeedScalar dc_axis[3]; CeedScalar wind[3]; CeedScalar time; int wind_type; // See WindType: 0=ROTATION, 1=TRANSLATION int bubble_type; // See BubbleType: 0=SPHERE, 1=CYLINDER int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK }; #endif #ifndef newtonian_context_struct #define newtonian_context_struct typedef enum { STAB_NONE = 0, STAB_SU = 1, // Streamline Upwind STAB_SUPG = 2, // Streamline Upwind Petrov-Galerkin } StabilizationType; typedef struct NewtonianIdealGasContext_ *NewtonianIdealGasContext; struct NewtonianIdealGasContext_ { CeedScalar lambda; CeedScalar mu; CeedScalar k; CeedScalar cv; CeedScalar cp; CeedScalar g; CeedScalar c_tau; StabilizationType stabilization; }; #endif // ***************************************************************************** // Helper function for computing flux Jacobian // ***************************************************************************** CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E, const CeedScalar gamma, const CeedScalar g, CeedScalar z) { CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - g*z)) : 0.) - u[i]*u[j]; for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix dF[i][0][k+1] = ((i==k) ? 1. : 0.); dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) + ((i==k) ? u[j] : 0.) - ((i==j) ? u[k] : 0.) * (gamma-1.); dF[i][4][k+1] = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) - (gamma-1.)*u[i]*u[k]; } dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.); } dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho); dF[i][4][4] = u[i] * gamma; } } // ***************************************************************************** // Helper function for computing Tau elements (stabilization constant) // Model from: // Stabilized Methods for Compressible Flows, Hughes et al 2010 // // Spatial criterion #2 - Tau is a 3x3 diagonal matrix // Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum) // // Where // c_tau = stabilization constant (0.5 is reported as "optimal") // h[i] = 2 length(dxdX[i]) // Pe = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity ) // Xi(Pe) = coth Pe - 1. / Pe (1. at large local Peclet number ) // rho(A[i]) = spectral radius of the convective flux Jacobian i, // wave speed in direction i // ***************************************************************************** CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3], const CeedScalar dXdx[3][3], const CeedScalar u[3], const CeedScalar sound_speed, const CeedScalar c_tau) { for (int i=0; i<3; i++) { // length of element in direction i CeedScalar h = 2 / sqrt(dXdx[0][i]*dXdx[0][i] + dXdx[1][i]*dXdx[1][i] + dXdx[2][i]*dXdx[2][i]); // fastest wave in direction i CeedScalar fastest_wave = fabs(u[i]) + sound_speed; Tau_x[i] = c_tau * h / fastest_wave; } } // ***************************************************************************** // This QFunction sets a "still" initial condition for generic Newtonian IG problems // ***************************************************************************** CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { // Inputs const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; // Outputs CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; // Quadrature Point Loop CeedPragmaSIMD for (CeedInt i=0; itheta0; const CeedScalar P0 = context->P0; const CeedScalar N = context->N; const CeedScalar cv = context->cv; const CeedScalar cp = context->cp; const CeedScalar g = context->g; const CeedScalar Rd = cp - cv; // Setup // -- Coordinates const CeedScalar z = X[2][i]; // -- Exner pressure, hydrostatic balance const CeedScalar Pi = 1. + g*g*(exp(-N*N*z/g) - 1.) / (cp*theta0*N*N); // -- Density const CeedScalar rho = P0 * pow(Pi, cv/Rd) / (Rd*theta0); // Initial Conditions q[0] = rho; q[1] = 0.0; q[2] = 0.0; q[3] = 0.0; q[4] = rho * (cv*theta0*Pi + g*z); for (CeedInt j=0; j<5; j++) q0[j][i] = q[j]; } // End of Quadrature Point Loop return 0; } // ***************************************************************************** // This QFunction implements the following formulation of Navier-Stokes with // explicit time stepping method // // This is 3D compressible Navier-Stokes in conservation form with state // variables of density, momentum density, and total energy density. // // State Variables: q = ( rho, U1, U2, U3, E ) // rho - Mass Density // Ui - Momentum Density, Ui = rho ui // E - Total Energy Density, E = rho (cv T + (u u)/2 + g z) // // Navier-Stokes Equations: // drho/dt + div( U ) = 0 // dU/dt + div( rho (u x u) + P I3 ) + rho g khat = div( Fu ) // dE/dt + div( (E + P) u ) = div( Fe ) // // Viscous Stress: // Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3) // // Thermal Stress: // Fe = u Fu + k grad( T ) // // Equation of State: // P = (gamma - 1) (E - rho (u u) / 2 - rho g z) // // Stabilization: // Tau = diag(TauC, TauM, TauM, TauM, TauE) // f1 = rho sqrt(ui uj gij) // gij = dXi/dX * dXi/dX // TauC = Cc f1 / (8 gii) // TauM = min( 1 , 1 / f1 ) // TauE = TauM / (Ce cv) // // SU = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) ) // // Constants: // lambda = - 2 / 3, From Stokes hypothesis // mu , Dynamic viscosity // k , Thermal conductivity // cv , Specific heat, constant volume // cp , Specific heat, constant pressure // g , Gravity // gamma = cp / cv, Specific heat ratio // // We require the product of the inverse of the Jacobian (dXdx_j,k) and // its transpose (dXdx_k,j) to properly compute integrals of the form: // int( gradv gradu ) // // ***************************************************************************** CEED_QFUNCTION(Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { // *INDENT-OFF* // Inputs const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; // Outputs CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; // *INDENT-ON* // Context NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx; const CeedScalar lambda = context->lambda; const CeedScalar mu = context->mu; const CeedScalar k = context->k; const CeedScalar cv = context->cv; const CeedScalar cp = context->cp; const CeedScalar g = context->g; const CeedScalar c_tau = context->c_tau; const CeedScalar gamma = cp / cv; CeedPragmaSIMD // Quadrature Point Loop for (CeedInt i=0; istabilization) { case STAB_NONE: // Galerkin break; case STAB_SU: // SU for (int j=0; j<3; j++) for (int k=0; k<5; k++) for (int l=0; l<5; l++) stab[k][j] = jacob_F_conv_T[j][k][l] * Tau_x[j] * strong_conv[l]; for (int j=0; j<5; j++) for (int k=0; k<3; k++) dv[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]); break; case STAB_SUPG: // SUPG is not implemented for explicit scheme break; } } // End Quadrature Point Loop // Return return 0; } // ***************************************************************************** // This QFunction implements the Navier-Stokes equations (mentioned above) with // implicit time stepping method // // SU = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) ) // SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) ) // (diffussive terms will be added later) // // ***************************************************************************** CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { // *INDENT-OFF* // Inputs const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3], (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4]; // Outputs CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; // *INDENT-ON* // Context NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx; const CeedScalar lambda = context->lambda; const CeedScalar mu = context->mu; const CeedScalar k = context->k; const CeedScalar cv = context->cv; const CeedScalar cp = context->cp; const CeedScalar g = context->g; const CeedScalar c_tau = context->c_tau; const CeedScalar gamma = cp / cv; CeedPragmaSIMD // Quadrature Point Loop for (CeedInt i=0; istabilization) { case STAB_NONE: // Galerkin break; case STAB_SU: // SU for (int j=0; j<3; j++) for (int k=0; k<5; k++) for (int l=0; l<5; l++) stab[k][j] = jacob_F_conv_T[j][k][l] * Tau_x[j] * strong_conv[l]; for (int j=0; j<5; j++) for (int k=0; k<3; k++) dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]); break; case STAB_SUPG: // SUPG for (int j=0; j<3; j++) for (int k=0; k<5; k++) for (int l=0; l<5; l++) stab[k][j] = jacob_F_conv_T[j][k][l] * Tau_x[j] * strong_res[l]; for (int j=0; j<5; j++) for (int k=0; k<3; k++) dv[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]); break; } } // End Quadrature Point Loop // Return return 0; } // ***************************************************************************** #endif // newtonian_h