xref: /libCEED/examples/fluids/problems/advection.c (revision ee4ca9cbfe2be39196684117442f3ce8d00b6506)
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 /// Utility functions for setting up ADVECTION
10 
11 #include "../qfunctions/advection.h"
12 
13 #include <ceed.h>
14 #include <petscdm.h>
15 
16 #include "../navierstokes.h"
17 #include "../qfunctions/setupgeo.h"
18 
19 PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
20   WindType             wind_type;
21   BubbleType           bubble_type;
22   BubbleContinuityType bubble_continuity_type;
23   StabilizationType    stab;
24   SetupContextAdv      setup_context;
25   User                 user = *(User *)ctx;
26   MPI_Comm             comm = PETSC_COMM_WORLD;
27   PetscBool            implicit;
28   PetscBool            has_curr_time = PETSC_FALSE;
29   AdvectionContext     advection_ctx;
30   CeedQFunctionContext advection_context;
31 
32   PetscFunctionBeginUser;
33   PetscCall(PetscCalloc1(1, &setup_context));
34   PetscCall(PetscCalloc1(1, &advection_ctx));
35 
36   // ------------------------------------------------------
37   //               SET UP ADVECTION
38   // ------------------------------------------------------
39   problem->dim                               = 3;
40   problem->q_data_size_vol                   = 10;
41   problem->q_data_size_sur                   = 10;
42   problem->setup_vol.qfunction               = Setup;
43   problem->setup_vol.qfunction_loc           = Setup_loc;
44   problem->setup_sur.qfunction               = SetupBoundary;
45   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
46   problem->ics.qfunction                     = ICsAdvection;
47   problem->ics.qfunction_loc                 = ICsAdvection_loc;
48   problem->apply_vol_rhs.qfunction           = Advection;
49   problem->apply_vol_rhs.qfunction_loc       = Advection_loc;
50   problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
51   problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
52   problem->apply_inflow.qfunction            = Advection_InOutFlow;
53   problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
54   problem->bc                                = Exact_Advection;
55   problem->bc_ctx                            = setup_context;
56   problem->non_zero_time                     = PETSC_FALSE;
57   problem->print_info                        = PRINT_ADVECTION;
58 
59   // ------------------------------------------------------
60   //             Create the libCEED context
61   // ------------------------------------------------------
62   CeedScalar rc          = 1000.;       // m (Radius of bubble)
63   CeedScalar CtauS       = 0.;          // dimensionless
64   CeedScalar strong_form = 0.;          // [0,1]
65   CeedScalar E_wind      = 1.e6;        // J
66   PetscReal  wind[3]     = {1., 0, 0};  // m/s
67   PetscReal  domain_min[3], domain_max[3], domain_size[3];
68   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
69   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
70 
71   // ------------------------------------------------------
72   //             Create the PETSc context
73   // ------------------------------------------------------
74   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
75   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
76   PetscScalar second   = 1e-2;  // 1 second in scaled time units
77   PetscScalar Joule;
78 
79   // ------------------------------------------------------
80   //              Command line Options
81   // ------------------------------------------------------
82   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
83   // -- Physics
84   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
85   PetscBool translation;
86   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
87                              &translation));
88   if (translation) user->phys->has_neumann = PETSC_TRUE;
89   PetscInt  n = problem->dim;
90   PetscBool user_wind;
91   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
92   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
93   PetscCall(
94       PetscOptionsScalar("-strong_form", "Strong (1) or weak/integrated by parts (0) advection residual", NULL, strong_form, &strong_form, NULL));
95   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
96   PetscCall(PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)", NULL, BubbleTypes, (PetscEnum)(bubble_type = BUBBLE_SPHERE),
97                              (PetscEnum *)&bubble_type, NULL));
98   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes,
99                              (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH), (PetscEnum *)&bubble_continuity_type, NULL));
100   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
101   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
102 
103   // -- Units
104   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
105   meter = fabs(meter);
106   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
107   kilogram = fabs(kilogram);
108   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
109   second = fabs(second);
110 
111   // -- Warnings
112   if (wind_type == WIND_ROTATION && user_wind) {
113     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
114   }
115   if (wind_type == WIND_TRANSLATION && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
116     wind[2] = 0;
117     PetscCall(PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n"));
118   }
119   if (stab == STAB_NONE && CtauS != 0) {
120     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
121   }
122   if (stab == STAB_SUPG && !implicit) {
123     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
124   }
125 
126   PetscOptionsEnd();
127 
128   // ------------------------------------------------------
129   //           Set up the PETSc context
130   // ------------------------------------------------------
131   // -- Define derived units
132   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
133 
134   user->units->meter    = meter;
135   user->units->kilogram = kilogram;
136   user->units->second   = second;
137   user->units->Joule    = Joule;
138 
139   // ------------------------------------------------------
140   //           Set up the libCEED context
141   // ------------------------------------------------------
142   // -- Scale variables to desired units
143   E_wind *= Joule;
144   rc = fabs(rc) * meter;
145   for (PetscInt i = 0; i < 3; i++) {
146     wind[i] *= (meter / second);
147     domain_size[i] *= meter;
148   }
149   problem->dm_scale = meter;
150 
151   // -- Setup Context
152   setup_context->rc                     = rc;
153   setup_context->lx                     = domain_size[0];
154   setup_context->ly                     = domain_size[1];
155   setup_context->lz                     = domain_size[2];
156   setup_context->wind[0]                = wind[0];
157   setup_context->wind[1]                = wind[1];
158   setup_context->wind[2]                = wind[2];
159   setup_context->wind_type              = wind_type;
160   setup_context->bubble_type            = bubble_type;
161   setup_context->bubble_continuity_type = bubble_continuity_type;
162   setup_context->time                   = 0;
163 
164   // -- QFunction Context
165   user->phys->stab                   = stab;
166   user->phys->wind_type              = wind_type;
167   user->phys->bubble_type            = bubble_type;
168   user->phys->bubble_continuity_type = bubble_continuity_type;
169   //  if passed correctly
170   user->phys->implicit         = implicit;
171   user->phys->has_curr_time    = has_curr_time;
172   advection_ctx->CtauS         = CtauS;
173   advection_ctx->E_wind        = E_wind;
174   advection_ctx->implicit      = implicit;
175   advection_ctx->strong_form   = strong_form;
176   advection_ctx->stabilization = stab;
177 
178   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
179   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
180   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
181 
182   CeedQFunctionContextCreate(user->ceed, &advection_context);
183   CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx);
184   CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc);
185   problem->apply_vol_rhs.qfunction_context = advection_context;
186   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context);
187   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context);
188   PetscFunctionReturn(PETSC_SUCCESS);
189 }
190 
191 PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx) {
192   MPI_Comm         comm = PETSC_COMM_WORLD;
193   SetupContextAdv  setup_ctx;
194   AdvectionContext advection_ctx;
195 
196   PetscFunctionBeginUser;
197   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx);
198   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx);
199   PetscCall(PetscPrintf(comm,
200                         "  Problem:\n"
201                         "    Problem Name                       : %s\n"
202                         "    Stabilization                      : %s\n"
203                         "    Bubble Type                        : %s (%" CeedInt_FMT "D)\n"
204                         "    Bubble Continuity                  : %s\n"
205                         "    Wind Type                          : %s\n",
206                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], BubbleTypes[setup_ctx->bubble_type],
207                         setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2, BubbleContinuityTypes[setup_ctx->bubble_continuity_type],
208                         WindTypes[setup_ctx->wind_type]));
209 
210   if (setup_ctx->wind_type == WIND_TRANSLATION) {
211     PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
212   }
213   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx);
214   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx);
215   PetscFunctionReturn(PETSC_SUCCESS);
216 }
217