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