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