xref: /libCEED/examples/fluids/problems/advection.c (revision ae718e2f2476441c07fb6c037655a83f28d5f429)
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->setup_ctx               = SetupContext_ADVECTION;
59   problem->bc_func                 = BC_ADVECTION;
60   problem->non_zero_time           = PETSC_FALSE;
61   problem->print_info              = PRINT_ADVECTION;
62 
63   // ------------------------------------------------------
64   //             Create the libCEED context
65   // ------------------------------------------------------
66   PetscScalar lx         = 8000.;      // m
67   PetscScalar ly         = 8000.;      // m
68   PetscScalar lz         = 4000.;      // m
69   CeedScalar rc          = 1000.;      // m (Radius of bubble)
70   CeedScalar CtauS       = 0.;         // dimensionless
71   CeedScalar strong_form = 0.;         // [0,1]
72   CeedScalar E_wind      = 1.e6;       // J
73   PetscReal wind[3]      = {1., 0, 0}; // m/s
74 
75   // ------------------------------------------------------
76   //             Create the PETSc context
77   // ------------------------------------------------------
78   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
79   PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units
80   PetscScalar second   = 1e-2; // 1 second in scaled time units
81   PetscScalar Joule;
82 
83   // ------------------------------------------------------
84   //              Command line Options
85   // ------------------------------------------------------
86   ierr = PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem",
87                            NULL); CHKERRQ(ierr);
88   // -- Physics
89   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
90                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
91   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
92                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
93   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
94                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
95   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
96                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
97   PetscBool translation;
98   ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection",
99                           NULL, WindTypes,
100                           (PetscEnum)(wind_type = WIND_ROTATION),
101                           (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr);
102   if (translation) user->phys->has_neumann = PETSC_TRUE;
103   PetscInt n = problem->dim;
104   PetscBool user_wind;
105   ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector",
106                                NULL, wind, &n, &user_wind); CHKERRQ(ierr);
107   ierr = PetscOptionsScalar("-CtauS",
108                             "Scale coefficient for tau (nondimensional)",
109                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
110   ierr = PetscOptionsScalar("-strong_form",
111                             "Strong (1) or weak/integrated by parts (0) advection residual",
112                             NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr);
113   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
114                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
115   ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)",
116                           NULL, BubbleTypes,
117                           (PetscEnum)(bubble_type = BUBBLE_SPHERE),
118                           (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr);
119   ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick",
120                           NULL, BubbleContinuityTypes,
121                           (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH),
122                           (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr);
123   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
124                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
125                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
126   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
127                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
128   CHKERRQ(ierr);
129 
130   // -- Units
131   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
132                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
133   meter = fabs(meter);
134   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
135                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
136   kilogram = fabs(kilogram);
137   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
138                             NULL, second, &second, NULL); CHKERRQ(ierr);
139   second = fabs(second);
140 
141   // -- Warnings
142   if (wind_type == WIND_ROTATION && user_wind) {
143     ierr = PetscPrintf(comm,
144                        "Warning! Use -wind_translation only with -wind_type translation\n");
145     CHKERRQ(ierr);
146   }
147   if (wind_type == WIND_TRANSLATION
148       && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
149     wind[2] = 0;
150     ierr = PetscPrintf(comm,
151                        "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n");
152     CHKERRQ(ierr);
153   }
154   if (stab == STAB_NONE && CtauS != 0) {
155     ierr = PetscPrintf(comm,
156                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
157     CHKERRQ(ierr);
158   }
159   if (stab == STAB_SUPG && !implicit) {
160     ierr = PetscPrintf(comm,
161                        "Warning! Use -stab supg only with -implicit\n");
162     CHKERRQ(ierr);
163   }
164 
165   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
166 
167   // ------------------------------------------------------
168   //           Set up the PETSc context
169   // ------------------------------------------------------
170   // -- Define derived units
171   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
172 
173   user->units->meter    = meter;
174   user->units->kilogram = kilogram;
175   user->units->second   = second;
176   user->units->Joule    = Joule;
177 
178   // ------------------------------------------------------
179   //           Set up the libCEED context
180   // ------------------------------------------------------
181   // -- Scale variables to desired units
182   E_wind *= Joule;
183   lx = fabs(lx) * meter;
184   ly = fabs(ly) * meter;
185   lz = fabs(lz) * meter;
186   rc = fabs(rc) * meter;
187 
188   // -- Setup Context
189   setup_context->rc                     = rc;
190   setup_context->lx                     = lx;
191   setup_context->ly                     = ly;
192   setup_context->lz                     = lz;
193   setup_context->wind[0]                = wind[0];
194   setup_context->wind[1]                = wind[1];
195   setup_context->wind[2]                = wind[2];
196   setup_context->wind_type              = wind_type;
197   setup_context->bubble_type            = bubble_type;
198   setup_context->bubble_continuity_type = bubble_continuity_type;
199   setup_context->time = 0;
200 
201   // -- QFunction Context
202   user->phys->stab                         = stab;
203   user->phys->wind_type                    = wind_type;
204   user->phys->bubble_type                  = bubble_type;
205   user->phys->bubble_continuity_type       = bubble_continuity_type;
206   //  if passed correctly
207   user->phys->implicit                     = implicit;
208   user->phys->has_curr_time                = has_curr_time;
209   user->phys->advection_ctx->CtauS         = CtauS;
210   user->phys->advection_ctx->E_wind        = E_wind;
211   user->phys->advection_ctx->implicit      = implicit;
212   user->phys->advection_ctx->strong_form   = strong_form;
213   user->phys->advection_ctx->stabilization = stab;
214 
215   PetscFunctionReturn(0);
216 }
217 
218 PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data,
219                                       AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
220   PetscFunctionBeginUser;
221 
222   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
223   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
224                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
225   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
226   CeedQFunctionContextCreate(ceed, &ceed_data->advection_context);
227   CeedQFunctionContextSetData(ceed_data->advection_context, CEED_MEM_HOST,
228                               CEED_USE_POINTER,
229                               sizeof(*phys->advection_ctx), phys->advection_ctx);
230   if (ceed_data->qf_rhs_vol)
231     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->advection_context);
232   if (ceed_data->qf_ifunction_vol)
233     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
234                             ceed_data->advection_context);
235   if (ceed_data->qf_apply_sur)
236     CeedQFunctionSetContext(ceed_data->qf_apply_sur, ceed_data->advection_context);
237 
238   PetscFunctionReturn(0);
239 }
240 
241 PetscErrorCode BC_ADVECTION(DM dm, SimpleBC bc, Physics phys,
242                             void *setup_ctx) {
243   PetscErrorCode ierr;
244   PetscFunctionBeginUser;
245 
246   // Define boundary conditions
247   if (phys->wind_type == WIND_TRANSLATION) {
248     bc->num_wall = bc->num_slip[2] = 0;
249   } else if (phys->wind_type == WIND_ROTATION &&
250              phys->bubble_type == BUBBLE_CYLINDER) {
251     bc->num_slip[2] = 2; bc->slips[2][0] = 1; bc->slips[2][1] = 2;
252     bc->num_wall = 4;
253     bc->walls[0] = 3; bc->walls[1] = 4; bc->walls[2] = 5; bc->walls[3] = 6;
254   } else {
255     bc->num_slip[2] = 0;
256     bc->num_wall = 6;
257     bc->walls[0] = 1; bc->walls[1] = 2; bc->walls[2] = 3;
258     bc->walls[3] = 4; bc->walls[4] = 5; bc->walls[5] = 6;
259   }
260 
261   {
262     // Set slip boundary conditions
263     DMLabel label;
264     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
265     PetscInt comps[1] = {3};
266     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
267                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
268                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
269     CHKERRQ(ierr);
270   }
271 
272   // Set wall boundary conditions
273   //   zero energy density and zero flux
274   {
275     DMLabel  label;
276     PetscInt comps[1] = {4};
277     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
278     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, "Face Sets",
279                          bc->num_wall, bc->walls, 0,
280                          1, comps, (void(*)(void))Exact_Advection, NULL,
281                          setup_ctx, NULL); CHKERRQ(ierr);
282   }
283 
284   PetscFunctionReturn(0);
285 }
286 
287 PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx,
288                                AppCtx app_ctx) {
289   MPI_Comm       comm = PETSC_COMM_WORLD;
290   PetscErrorCode ierr;
291   PetscFunctionBeginUser;
292 
293   ierr = PetscPrintf(comm,
294                      "  Problem:\n"
295                      "    Problem Name                       : %s\n"
296                      "    Stabilization                      : %s\n"
297                      "    Bubble Type                        : %s (%dD)\n"
298                      "    Bubble Continuity                  : %s\n"
299                      "    Wind Type                          : %s\n",
300                      app_ctx->problem_name, StabilizationTypes[phys->stab],
301                      BubbleTypes[phys->bubble_type],
302                      phys->bubble_type == BUBBLE_SPHERE ? 3 : 2,
303                      BubbleContinuityTypes[phys->bubble_continuity_type],
304                      WindTypes[phys->wind_type]); CHKERRQ(ierr);
305 
306   if (phys->wind_type == WIND_TRANSLATION) {
307     ierr = PetscPrintf(comm,
308                        "    Background Wind                    : %f,%f,%f\n",
309                        setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr);
310   }
311   PetscFunctionReturn(0);
312 }
313