xref: /libCEED/examples/fluids/qfunctions/advection.h (revision bb229da952f7e9779ba6cb3cd1ca2ebeac5feb1f)
1 // Copyright (c) 2017-2022, 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 /// Advection initial condition and operator for Navier-Stokes example using PETSc
10 
11 #ifndef advection_h
12 #define advection_h
13 
14 #include <ceed.h>
15 #include <math.h>
16 
17 #include "utils.h"
18 
19 typedef struct SetupContextAdv_ *SetupContextAdv;
20 struct SetupContextAdv_ {
21   CeedScalar rc;
22   CeedScalar lx;
23   CeedScalar ly;
24   CeedScalar lz;
25   CeedScalar wind[3];
26   CeedScalar time;
27   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
28   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
29   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
30 };
31 
32 typedef struct AdvectionContext_ *AdvectionContext;
33 struct AdvectionContext_ {
34   CeedScalar CtauS;
35   CeedScalar strong_form;
36   CeedScalar E_wind;
37   bool       implicit;
38   int        stabilization;  // See StabilizationType: 0=none, 1=SU, 2=SUPG
39 };
40 
41 // *****************************************************************************
42 // This QFunction sets the initial conditions and the boundary conditions
43 //   for two test cases: ROTATION and TRANSLATION
44 //
45 // -- ROTATION (default)
46 //      Initial Conditions:
47 //        Mass Density:
48 //          Constant mass density of 1.0
49 //        Momentum Density:
50 //          Rotational field in x,y
51 //        Energy Density:
52 //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
53 //            increases to (1.-r/rc), then 0. everywhere else
54 //
55 //      Boundary Conditions:
56 //        Mass Density:
57 //          0.0 flux
58 //        Momentum Density:
59 //          0.0
60 //        Energy Density:
61 //          0.0 flux
62 //
63 // -- TRANSLATION
64 //      Initial Conditions:
65 //        Mass Density:
66 //          Constant mass density of 1.0
67 //        Momentum Density:
68 //           Constant rectilinear field in x,y
69 //        Energy Density:
70 //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
71 //            increases to (1.-r/rc), then 0. everywhere else
72 //
73 //      Boundary Conditions:
74 //        Mass Density:
75 //          0.0 flux
76 //        Momentum Density:
77 //          0.0
78 //        Energy Density:
79 //          Inflow BCs:
80 //            E = E_wind
81 //          Outflow BCs:
82 //            E = E(boundary)
83 //          Both In/Outflow BCs for E are applied weakly in the
84 //            QFunction "Advection_Sur"
85 //
86 // *****************************************************************************
87 
88 // *****************************************************************************
89 // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for 3D advection
90 // *****************************************************************************
91 CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
92   const SetupContextAdv context = (SetupContextAdv)ctx;
93   const CeedScalar      rc      = context->rc;
94   const CeedScalar      lx      = context->lx;
95   const CeedScalar      ly      = context->ly;
96   const CeedScalar      lz      = context->lz;
97   const CeedScalar     *wind    = context->wind;
98 
99   // Setup
100   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
101   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
102 
103   // -- Coordinates
104   const CeedScalar x = X[0];
105   const CeedScalar y = X[1];
106   const CeedScalar z = X[2];
107 
108   // -- Energy
109   CeedScalar r = 0.;
110   switch (context->bubble_type) {
111     //  original sphere
112     case 0: {  // (dim=3)
113       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
114     } break;
115     // cylinder (needs periodicity to work properly)
116     case 1: {  // (dim=2)
117       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
118     } break;
119   }
120 
121   // Initial Conditions
122   switch (context->wind_type) {
123     case 0:  // Rotation
124       q[0] = 1.;
125       q[1] = -(y - center[1]);
126       q[2] = (x - center[0]);
127       q[3] = 0;
128       break;
129     case 1:  // Translation
130       q[0] = 1.;
131       q[1] = wind[0];
132       q[2] = wind[1];
133       q[3] = wind[2];
134       break;
135   }
136 
137   switch (context->bubble_continuity_type) {
138     // original continuous, smooth shape
139     case 0: {
140       q[4] = r <= rc ? (1. - r / rc) : 0.;
141     } break;
142     // discontinuous, sharp back half shape
143     case 1: {
144       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
145     } break;
146     // attempt to define a finite thickness that will get resolved under grid refinement
147     case 2: {
148       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
149     } break;
150   }
151   return 0;
152 }
153 
154 // *****************************************************************************
155 // This QFunction sets the initial conditions for 3D advection
156 // *****************************************************************************
157 CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
158   // Inputs
159   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
160   // Outputs
161   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
162 
163   // Quadrature Point Loop
164   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
165     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
166     CeedScalar       q[5] = {0.};
167 
168     Exact_Advection(3, 0., x, 5, q, ctx);
169     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
170   }  // End of Quadrature Point Loop
171 
172   // Return
173   return 0;
174 }
175 
176 // *****************************************************************************
177 // This QFunction implements the following formulation of the advection equation
178 //
179 // This is 3D advection given in two formulations based upon the weak form.
180 //
181 // State Variables: q = ( rho, U1, U2, U3, E )
182 //   rho - Mass Density
183 //   Ui  - Momentum Density    ,  Ui = rho ui
184 //   E   - Total Energy Density
185 //
186 // Advection Equation:
187 //   dE/dt + div( E u ) = 0
188 // *****************************************************************************
189 CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
190   // Inputs
191   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
192   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
193   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
194 
195   // Outputs
196   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
197   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
198 
199   // Context
200   AdvectionContext context     = (AdvectionContext)ctx;
201   const CeedScalar CtauS       = context->CtauS;
202   const CeedScalar strong_form = context->strong_form;
203 
204   // Quadrature Point Loop
205   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
206     // Setup
207     // -- Interp in
208     const CeedScalar rho  = q[0][i];
209     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
210     const CeedScalar E    = q[4][i];
211     // -- Grad in
212     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
213     const CeedScalar du[3][3] = {
214         {(dq[0][1][i] - drho[0] * u[0]) / rho, (dq[1][1][i] - drho[1] * u[0]) / rho, (dq[2][1][i] - drho[2] * u[0]) / rho},
215         {(dq[0][2][i] - drho[0] * u[1]) / rho, (dq[1][2][i] - drho[1] * u[1]) / rho, (dq[2][2][i] - drho[2] * u[1]) / rho},
216         {(dq[0][3][i] - drho[0] * u[2]) / rho, (dq[1][3][i] - drho[1] * u[2]) / rho, (dq[2][3][i] - drho[2] * u[2]) / rho}
217     };
218     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
219     // -- Interp-to-Interp q_data
220     const CeedScalar wdetJ = q_data[0][i];
221     // -- Interp-to-Grad q_data
222     // ---- Inverse of change of coordinate matrix: X_i,j
223     const CeedScalar dXdx[3][3] = {
224         {q_data[1][i], q_data[2][i], q_data[3][i]},
225         {q_data[4][i], q_data[5][i], q_data[6][i]},
226         {q_data[7][i], q_data[8][i], q_data[9][i]}
227     };
228     // The Physics
229     // Note with the order that du was filled and the order that dXdx was filled
230     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
231     //   dXdx[k][j] = dX_K / dx_j
232     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
233     //   x_j and u_j are jth  physical position and velocity components
234 
235     // No Change in density or momentum
236     for (CeedInt f = 0; f < 4; f++) {
237       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
238       v[f][i] = 0;
239     }
240 
241     // -- Total Energy
242     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
243     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
244     CeedScalar div_u = 0, u_dot_grad_E = 0;
245     for (CeedInt j = 0; j < 3; j++) {
246       CeedScalar dEdx_j = 0;
247       for (CeedInt k = 0; k < 3; k++) {
248         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
249         dEdx_j += dE[k] * dXdx[k][j];
250       }
251       u_dot_grad_E += u[j] * dEdx_j;
252     }
253     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
254 
255     // Weak Galerkin convection term: dv \cdot (E u)
256     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = (1 - strong_form) * wdetJ * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
257     v[4][i] = 0;
258 
259     // Strong Galerkin convection term: - v div(E u)
260     v[4][i] = -strong_form * wdetJ * strong_conv;
261 
262     // Stabilization requires a measure of element transit time in the velocity
263     //   field u.
264     CeedScalar uX[3];
265     for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2];
266     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
267     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
268   }  // End Quadrature Point Loop
269 
270   return 0;
271 }
272 
273 // *****************************************************************************
274 // This QFunction implements 3D (mentioned above) with implicit time stepping method
275 // *****************************************************************************
276 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
277   // Inputs
278   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
279   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
280   const CeedScalar(*q_dot)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[2];
281   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
282 
283   // Outputs
284   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
285   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
286 
287   AdvectionContext context     = (AdvectionContext)ctx;
288   const CeedScalar CtauS       = context->CtauS;
289   const CeedScalar strong_form = context->strong_form;
290 
291   // Quadrature Point Loop
292   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
293     // Setup
294     // -- Interp in
295     const CeedScalar rho  = q[0][i];
296     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
297     const CeedScalar E    = q[4][i];
298     // -- Grad in
299     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
300     const CeedScalar du[3][3] = {
301         {(dq[0][1][i] - drho[0] * u[0]) / rho, (dq[1][1][i] - drho[1] * u[0]) / rho, (dq[2][1][i] - drho[2] * u[0]) / rho},
302         {(dq[0][2][i] - drho[0] * u[1]) / rho, (dq[1][2][i] - drho[1] * u[1]) / rho, (dq[2][2][i] - drho[2] * u[1]) / rho},
303         {(dq[0][3][i] - drho[0] * u[2]) / rho, (dq[1][3][i] - drho[1] * u[2]) / rho, (dq[2][3][i] - drho[2] * u[2]) / rho}
304     };
305     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
306     // -- Interp-to-Interp q_data
307     const CeedScalar wdetJ = q_data[0][i];
308     // -- Interp-to-Grad q_data
309     // ---- Inverse of change of coordinate matrix: X_i,j
310     const CeedScalar dXdx[3][3] = {
311         {q_data[1][i], q_data[2][i], q_data[3][i]},
312         {q_data[4][i], q_data[5][i], q_data[6][i]},
313         {q_data[7][i], q_data[8][i], q_data[9][i]}
314     };
315     // The Physics
316     // Note with the order that du was filled and the order that dXdx was filled
317     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k} )
318     //   dXdx[k][j] = dX_K / dx_j
319     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
320     //   x_j and u_j are jth  physical position and velocity components
321 
322     // No Change in density or momentum
323     for (CeedInt f = 0; f < 4; f++) {
324       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
325       v[f][i] = wdetJ * q_dot[f][i];  // K Mass/transient term
326     }
327 
328     // -- Total Energy
329     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
330     //   or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
331     CeedScalar div_u = 0, u_dot_grad_E = 0;
332     for (CeedInt j = 0; j < 3; j++) {
333       CeedScalar dEdx_j = 0;
334       for (CeedInt k = 0; k < 3; k++) {
335         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
336         dEdx_j += dE[k] * dXdx[k][j];
337       }
338       u_dot_grad_E += u[j] * dEdx_j;
339     }
340     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
341     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
342 
343     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
344 
345     // Weak Galerkin convection term: -dv \cdot (E u)
346     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = -wdetJ * (1 - strong_form) * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
347 
348     // Strong Galerkin convection term: v div(E u)
349     v[4][i] += wdetJ * strong_form * strong_conv;
350 
351     // Stabilization requires a measure of element transit time in the velocity
352     //   field u.
353     CeedScalar uX[3];
354     for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2];
355     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
356 
357     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
358         case 0:
359           break;
360         case 1:
361           dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j];  // SU
362           break;
363         case 2:
364           dv[j][4][i] += wdetJ * TauS * strong_res * uX[j];  // SUPG
365           break;
366       }
367   }  // End Quadrature Point Loop
368 
369   return 0;
370 }
371 
372 // *****************************************************************************
373 // This QFunction implements consistent outflow and inflow BCs
374 //      for 3D advection
375 //
376 //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
377 //    sign(dot(wind, normal)) > 0 : outflow BCs
378 //    sign(dot(wind, normal)) < 0 : inflow BCs
379 //
380 //  Outflow BCs:
381 //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
382 //
383 //  Inflow BCs:
384 //    A prescribed Total Energy (E_wind) is applied weakly.
385 // *****************************************************************************
386 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
387   // Inputs
388   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
389   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
390 
391   // Outputs
392   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
393   AdvectionContext context     = (AdvectionContext)ctx;
394   const CeedScalar E_wind      = context->E_wind;
395   const CeedScalar strong_form = context->strong_form;
396   const bool       implicit    = context->implicit;
397 
398   // Quadrature Point Loop
399   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
400     // Setup
401     // -- Interp in
402     const CeedScalar rho  = q[0][i];
403     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
404     const CeedScalar E    = q[4][i];
405 
406     // -- Interp-to-Interp q_data
407     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
408     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
409     // We can effect this by swapping the sign on this weight
410     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
411 
412     // ---- Normal vectors
413     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
414     // Normal velocity
415     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
416 
417     // No Change in density or momentum
418     for (CeedInt j = 0; j < 4; j++) {
419       v[j][i] = 0;
420     }
421     // Implementing in/outflow BCs
422     if (u_normal > 0) {  // outflow
423       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
424     } else {  // inflow
425       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
426     }
427   }  // End Quadrature Point Loop
428   return 0;
429 }
430 // *****************************************************************************
431 
432 #endif  // advection_h
433