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