xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 47fa654bdb0443fad7121e250bf7da7adb8330de)
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 typedef struct SetupContextAdv_ *SetupContextAdv;
18 struct SetupContextAdv_ {
19   CeedScalar rc;
20   CeedScalar lx;
21   CeedScalar ly;
22   CeedScalar lz;
23   CeedScalar wind[3];
24   CeedScalar time;
25   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
26   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
27   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
28 };
29 
30 typedef struct AdvectionContext_ *AdvectionContext;
31 struct AdvectionContext_ {
32   CeedScalar CtauS;
33   CeedScalar strong_form;
34   CeedScalar E_wind;
35   bool       implicit;
36   int        stabilization;  // See StabilizationType: 0=none, 1=SU, 2=SUPG
37 };
38 
39 CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x * x; }
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
90 //   (currently not implemented) and IC formulation for 3D advection
91 // *****************************************************************************
92 CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
93   const SetupContextAdv context = (SetupContextAdv)ctx;
94   const CeedScalar      rc      = context->rc;
95   const CeedScalar      lx      = context->lx;
96   const CeedScalar      ly      = context->ly;
97   const CeedScalar      lz      = context->lz;
98   const CeedScalar     *wind    = context->wind;
99 
100   // Setup
101   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
102   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
103 
104   // -- Coordinates
105   const CeedScalar x = X[0];
106   const CeedScalar y = X[1];
107   const CeedScalar z = X[2];
108 
109   // -- Energy
110   CeedScalar r = 0.;
111   switch (context->bubble_type) {
112     //  original sphere
113     case 0: {  // (dim=3)
114       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
115     } break;
116     // cylinder (needs periodicity to work properly)
117     case 1: {  // (dim=2)
118       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
119     } break;
120   }
121 
122   // Initial Conditions
123   switch (context->wind_type) {
124     case 0:  // Rotation
125       q[0] = 1.;
126       q[1] = -(y - center[1]);
127       q[2] = (x - center[0]);
128       q[3] = 0;
129       break;
130     case 1:  // Translation
131       q[0] = 1.;
132       q[1] = wind[0];
133       q[2] = wind[1];
134       q[3] = wind[2];
135       break;
136   }
137 
138   switch (context->bubble_continuity_type) {
139     // original continuous, smooth shape
140     case 0: {
141       q[4] = r <= rc ? (1. - r / rc) : 0.;
142     } break;
143     // discontinuous, sharp back half shape
144     case 1: {
145       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
146     } break;
147     // attempt to define a finite thickness that will get resolved under grid refinement
148     case 2: {
149       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
150     } break;
151   }
152   return 0;
153 }
154 
155 // *****************************************************************************
156 // This QFunction sets the initial conditions for 3D advection
157 // *****************************************************************************
158 CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
159   // Inputs
160   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
161   // Outputs
162   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
163 
164   CeedPragmaSIMD
165       // Quadrature Point Loop
166       for (CeedInt i = 0; i < Q; i++) {
167     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
168     CeedScalar       q[5] = {0.};
169 
170     Exact_Advection(3, 0., x, 5, q, ctx);
171     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
172   }  // End of Quadrature Point Loop
173 
174   // Return
175   return 0;
176 }
177 
178 // *****************************************************************************
179 // This QFunction implements the following formulation of the advection equation
180 //
181 // This is 3D advection given in two formulations based upon the weak form.
182 //
183 // State Variables: q = ( rho, U1, U2, U3, E )
184 //   rho - Mass Density
185 //   Ui  - Momentum Density    ,  Ui = rho ui
186 //   E   - Total Energy Density
187 //
188 // Advection Equation:
189 //   dE/dt + div( E u ) = 0
190 //
191 // *****************************************************************************
192 CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
193   // Inputs
194   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],
195         (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
196 
197   // Outputs
198   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
199 
200   // Context
201   AdvectionContext context     = (AdvectionContext)ctx;
202   const CeedScalar CtauS       = context->CtauS;
203   const CeedScalar strong_form = context->strong_form;
204 
205   CeedPragmaSIMD
206       // Quadrature Point Loop
207       for (CeedInt i = 0; i < Q; i++) {
208     // Setup
209     // -- Interp in
210     const CeedScalar rho  = q[0][i];
211     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
212     const CeedScalar E    = q[4][i];
213     // -- Grad in
214     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
215     const CeedScalar du[3][3] = {
216         {(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},
217         {(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},
218         {(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}
219     };
220     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
221     // -- Interp-to-Interp q_data
222     const CeedScalar wdetJ = q_data[0][i];
223     // -- Interp-to-Grad q_data
224     // ---- Inverse of change of coordinate matrix: X_i,j
225     const CeedScalar dXdx[3][3] = {
226         {q_data[1][i], q_data[2][i], q_data[3][i]},
227         {q_data[4][i], q_data[5][i], q_data[6][i]},
228         {q_data[7][i], q_data[8][i], q_data[9][i]}
229     };
230     // The Physics
231     // Note with the order that du was filled and the order that dXdx was filled
232     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
233     //   dXdx[k][j] = dX_K / dx_j
234     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
235     //   x_j and u_j are jth  physical position and velocity components
236 
237     // No Change in density or momentum
238     for (CeedInt f = 0; f < 4; f++) {
239       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
240       v[f][i] = 0;
241     }
242 
243     // -- Total Energy
244     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
245     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
246     CeedScalar div_u = 0, u_dot_grad_E = 0;
247     for (CeedInt j = 0; j < 3; j++) {
248       CeedScalar dEdx_j = 0;
249       for (CeedInt k = 0; k < 3; k++) {
250         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
251         dEdx_j += dE[k] * dXdx[k][j];
252       }
253       u_dot_grad_E += u[j] * dEdx_j;
254     }
255     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
256 
257     // Weak Galerkin convection term: dv \cdot (E u)
258     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]);
259     v[4][i] = 0;
260 
261     // Strong Galerkin convection term: - v div(E u)
262     v[4][i] = -strong_form * wdetJ * strong_conv;
263 
264     // Stabilization requires a measure of element transit time in the velocity
265     //   field u.
266     CeedScalar uX[3];
267     for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2];
268     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
269     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
270   }  // End Quadrature Point Loop
271 
272   return 0;
273 }
274 
275 // *****************************************************************************
276 // This QFunction implements 3D (mentioned above) with
277 //   implicit time stepping method
278 //
279 // *****************************************************************************
280 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
281   // Inputs
282   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],
283         (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
284   // Outputs
285   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
286   AdvectionContext context     = (AdvectionContext)ctx;
287   const CeedScalar CtauS       = context->CtauS;
288   const CeedScalar strong_form = context->strong_form;
289 
290   CeedPragmaSIMD
291       // Quadrature Point Loop
292       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
382 //    to the outflow and the current values of E are applied.
383 //
384 //  Inflow BCs:
385 //    A prescribed Total Energy (E_wind) is applied weakly.
386 //
387 // *****************************************************************************
388 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
389   // Inputs
390   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
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   CeedPragmaSIMD
399       // Quadrature Point Loop
400       for (CeedInt i = 0; i < Q; i++) {
401     // Setup
402     // -- Interp in
403     const CeedScalar rho  = q[0][i];
404     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
405     const CeedScalar E    = q[4][i];
406 
407     // -- Interp-to-Interp q_data
408     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
409     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
410     // We can effect this by swapping the sign on this weight
411     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
412 
413     // ---- Normal vectors
414     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
415     // Normal velocity
416     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
417 
418     // No Change in density or momentum
419     for (CeedInt j = 0; j < 4; j++) {
420       v[j][i] = 0;
421     }
422     // Implementing in/outflow BCs
423     if (u_normal > 0) {  // outflow
424       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
425     } else {  // inflow
426       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
427     }
428   }  // End Quadrature Point Loop
429   return 0;
430 }
431 // *****************************************************************************
432 
433 #endif  // advection_h
434