xref: /libCEED/examples/fluids/problems/advection.c (revision 778419476db2fdf3952b1b9a5faed3f766f76223)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 /// @file
18 /// Utility functions for setting up ADVECTION
19 
20 #include "../navierstokes.h"
21 #include "../qfunctions/setupgeo.h"
22 #include "../qfunctions/advection.h"
23 
24 PetscErrorCode NS_ADVECTION(ProblemData *problem, void *setup_ctx, void *ctx) {
25   WindType             wind_type;
26   BubbleType           bubble_type;
27   BubbleContinuityType bubble_continuity_type;
28   StabilizationType    stab;
29   SetupContext         setup_context = *(SetupContext *)setup_ctx;
30   User                 user = *(User *)ctx;
31   MPI_Comm             comm = PETSC_COMM_WORLD;
32   PetscBool            implicit;
33   PetscBool            has_curr_time = PETSC_FALSE;
34   PetscInt             ierr;
35   PetscFunctionBeginUser;
36 
37   ierr = PetscCalloc1(1, &user->phys->advection_ctx); CHKERRQ(ierr);
38 
39   // ------------------------------------------------------
40   //               SET UP ADVECTION
41   // ------------------------------------------------------
42   problem->dim                     = 3;
43   problem->q_data_size_vol         = 10;
44   problem->q_data_size_sur         = 4;
45   problem->setup_vol               = Setup;
46   problem->setup_vol_loc           = Setup_loc;
47   problem->setup_sur               = SetupBoundary;
48   problem->setup_sur_loc           = SetupBoundary_loc;
49   problem->ics                     = ICsAdvection;
50   problem->ics_loc                 = ICsAdvection_loc;
51   problem->apply_vol_rhs           = Advection;
52   problem->apply_vol_rhs_loc       = Advection_loc;
53   problem->apply_vol_ifunction     = IFunction_Advection;
54   problem->apply_vol_ifunction_loc = IFunction_Advection_loc;
55   problem->apply_sur               = Advection_Sur;
56   problem->apply_sur_loc           = Advection_Sur_loc;
57   problem->bc                      = Exact_Advection;
58   problem->bc_func                 = BC_ADVECTION;
59   problem->non_zero_time           = PETSC_FALSE;
60   problem->print_info              = PRINT_ADVECTION;
61 
62   // ------------------------------------------------------
63   //             Create the libCEED context
64   // ------------------------------------------------------
65   PetscScalar lx         = 8000.;      // m
66   PetscScalar ly         = 8000.;      // m
67   PetscScalar lz         = 4000.;      // m
68   CeedScalar rc          = 1000.;      // m (Radius of bubble)
69   CeedScalar CtauS       = 0.;         // dimensionless
70   CeedScalar strong_form = 0.;         // [0,1]
71   CeedScalar E_wind      = 1.e6;       // J
72   PetscReal wind[3]      = {1., 0, 0}; // m/s
73 
74   // ------------------------------------------------------
75   //             Create the PETSc context
76   // ------------------------------------------------------
77   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
78   PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units
79   PetscScalar second   = 1e-2; // 1 second in scaled time units
80   PetscScalar Joule;
81 
82   // ------------------------------------------------------
83   //              Command line Options
84   // ------------------------------------------------------
85   ierr = PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem",
86                            NULL); CHKERRQ(ierr);
87   // -- Physics
88   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
89                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
90   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
91                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
92   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
93                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
94   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
95                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
96   PetscBool translation;
97   ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection",
98                           NULL, WindTypes,
99                           (PetscEnum)(wind_type = WIND_ROTATION),
100                           (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr);
101   if (translation) user->phys->has_neumann = PETSC_TRUE;
102   PetscInt n = problem->dim;
103   PetscBool user_wind;
104   ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector",
105                                NULL, wind, &n, &user_wind); CHKERRQ(ierr);
106   ierr = PetscOptionsScalar("-CtauS",
107                             "Scale coefficient for tau (nondimensional)",
108                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
109   ierr = PetscOptionsScalar("-strong_form",
110                             "Strong (1) or weak/integrated by parts (0) advection residual",
111                             NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr);
112   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
113                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
114   ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)",
115                           NULL, BubbleTypes,
116                           (PetscEnum)(bubble_type = BUBBLE_SPHERE),
117                           (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr);
118   ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick",
119                           NULL, BubbleContinuityTypes,
120                           (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH),
121                           (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr);
122   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
123                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
124                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
125   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
126                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
127   CHKERRQ(ierr);
128 
129   // -- Units
130   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
131                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
132   meter = fabs(meter);
133   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
134                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
135   kilogram = fabs(kilogram);
136   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
137                             NULL, second, &second, NULL); CHKERRQ(ierr);
138   second = fabs(second);
139 
140   // -- Warnings
141   if (wind_type == WIND_ROTATION && user_wind) {
142     ierr = PetscPrintf(comm,
143                        "Warning! Use -wind_translation only with -wind_type translation\n");
144     CHKERRQ(ierr);
145   }
146   if (wind_type == WIND_TRANSLATION
147       && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
148     wind[2] = 0;
149     ierr = PetscPrintf(comm,
150                        "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n");
151     CHKERRQ(ierr);
152   }
153   if (stab == STAB_NONE && CtauS != 0) {
154     ierr = PetscPrintf(comm,
155                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
156     CHKERRQ(ierr);
157   }
158   if (stab == STAB_SUPG && !implicit) {
159     ierr = PetscPrintf(comm,
160                        "Warning! Use -stab supg only with -implicit\n");
161     CHKERRQ(ierr);
162   }
163 
164   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
165 
166   // ------------------------------------------------------
167   //           Set up the PETSc context
168   // ------------------------------------------------------
169   // -- Define derived units
170   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
171 
172   user->units->meter    = meter;
173   user->units->kilogram = kilogram;
174   user->units->second   = second;
175   user->units->Joule    = Joule;
176 
177   // ------------------------------------------------------
178   //           Set up the libCEED context
179   // ------------------------------------------------------
180   // -- Scale variables to desired units
181   E_wind *= Joule;
182   lx = fabs(lx) * meter;
183   ly = fabs(ly) * meter;
184   lz = fabs(lz) * meter;
185   rc = fabs(rc) * meter;
186 
187   // -- Setup Context
188   setup_context->rc                     = rc;
189   setup_context->lx                     = lx;
190   setup_context->ly                     = ly;
191   setup_context->lz                     = lz;
192   setup_context->wind[0]                = wind[0];
193   setup_context->wind[1]                = wind[1];
194   setup_context->wind[2]                = wind[2];
195   setup_context->wind_type              = wind_type;
196   setup_context->bubble_type            = bubble_type;
197   setup_context->bubble_continuity_type = bubble_continuity_type;
198   setup_context->time = 0;
199 
200   // -- QFunction Context
201   user->phys->stab                         = stab;
202   user->phys->wind_type                    = wind_type;
203   user->phys->bubble_type                  = bubble_type;
204   user->phys->bubble_continuity_type       = bubble_continuity_type;
205   //  if passed correctly
206   user->phys->implicit                     = implicit;
207   user->phys->has_curr_time                = has_curr_time;
208   user->phys->advection_ctx->CtauS         = CtauS;
209   user->phys->advection_ctx->E_wind        = E_wind;
210   user->phys->advection_ctx->implicit      = implicit;
211   user->phys->advection_ctx->strong_form   = strong_form;
212   user->phys->advection_ctx->stabilization = stab;
213 
214   PetscFunctionReturn(0);
215 }
216 
217 PetscErrorCode BC_ADVECTION(DM dm, SimpleBC bc, Physics phys,
218                             void *setup_ctx) {
219   PetscErrorCode ierr;
220   PetscFunctionBeginUser;
221 
222   // Define boundary conditions
223   if (phys->wind_type == WIND_TRANSLATION) {
224     bc->num_wall = bc->num_slip[2] = 0;
225   } else if (phys->wind_type == WIND_ROTATION &&
226              phys->bubble_type == BUBBLE_CYLINDER) {
227     bc->num_slip[2] = 2; bc->slips[2][0] = 1; bc->slips[2][1] = 2;
228     bc->num_wall = 4;
229     bc->walls[0] = 3; bc->walls[1] = 4; bc->walls[2] = 5; bc->walls[3] = 6;
230   } else {
231     bc->num_slip[2] = 0;
232     bc->num_wall = 6;
233     bc->walls[0] = 1; bc->walls[1] = 2; bc->walls[2] = 3;
234     bc->walls[3] = 4; bc->walls[4] = 5; bc->walls[5] = 6;
235   }
236 
237   {
238     // Set slip boundary conditions
239     DMLabel label;
240     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
241     PetscInt comps[1] = {3};
242     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
243                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
244                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
245     CHKERRQ(ierr);
246   }
247 
248   // Set wall boundary conditions
249   //   zero energy density and zero flux
250   {
251     DMLabel  label;
252     PetscInt comps[1] = {4};
253     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
254     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, "Face Sets",
255                          bc->num_wall, bc->walls, 0,
256                          1, comps, (void(*)(void))Exact_Advection, NULL,
257                          setup_ctx, NULL); CHKERRQ(ierr);
258   }
259 
260   PetscFunctionReturn(0);
261 }
262 
263 PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx,
264                                AppCtx app_ctx) {
265   MPI_Comm       comm = PETSC_COMM_WORLD;
266   PetscErrorCode ierr;
267   PetscFunctionBeginUser;
268 
269   ierr = PetscPrintf(comm,
270                      "  Problem:\n"
271                      "    Problem Name                       : %s\n"
272                      "    Stabilization                      : %s\n"
273                      "    Bubble Type                        : %s (%dD)\n"
274                      "    Bubble Continuity                  : %s\n"
275                      "    Wind Type                          : %s\n",
276                      app_ctx->problem_name, StabilizationTypes[phys->stab],
277                      BubbleTypes[phys->bubble_type],
278                      phys->bubble_type == BUBBLE_SPHERE ? 3 : 2,
279                      BubbleContinuityTypes[phys->bubble_continuity_type],
280                      WindTypes[phys->wind_type]); CHKERRQ(ierr);
281 
282   if (phys->wind_type == WIND_TRANSLATION) {
283     ierr = PetscPrintf(comm,
284                        "    Background Wind                    : %f,%f,%f\n",
285                        setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr);
286   }
287   PetscFunctionReturn(0);
288 }
289