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