xref: /libCEED/examples/fluids/problems/advection.c (revision 1f97d2f18688a5caa5dd9f004da3151e92048b76)
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->non_zero_time                     = PETSC_FALSE;
55   problem->print_info                        = PRINT_ADVECTION;
56 
57   // ------------------------------------------------------
58   //             Create the libCEED context
59   // ------------------------------------------------------
60   CeedScalar rc          = 1000.;       // m (Radius of bubble)
61   CeedScalar CtauS       = 0.;          // dimensionless
62   CeedScalar strong_form = 0.;          // [0,1]
63   CeedScalar E_wind      = 1.e6;        // J
64   PetscReal  wind[3]     = {1., 0, 0};  // m/s
65   PetscReal  domain_min[3], domain_max[3], domain_size[3];
66   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
67   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
68 
69   // ------------------------------------------------------
70   //             Create the PETSc context
71   // ------------------------------------------------------
72   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
73   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
74   PetscScalar second   = 1e-2;  // 1 second in scaled time units
75   PetscScalar Joule;
76 
77   // ------------------------------------------------------
78   //              Command line Options
79   // ------------------------------------------------------
80   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
81   // -- Physics
82   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
83   PetscBool translation;
84   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
85                              &translation));
86   if (translation) user->phys->has_neumann = PETSC_TRUE;
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(
92       PetscOptionsScalar("-strong_form", "Strong (1) or weak/integrated by parts (0) advection residual", NULL, strong_form, &strong_form, NULL));
93   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
94   PetscCall(PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)", NULL, BubbleTypes, (PetscEnum)(bubble_type = BUBBLE_SPHERE),
95                              (PetscEnum *)&bubble_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 && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
114     wind[2] = 0;
115     PetscCall(PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n"));
116   }
117   if (stab == STAB_NONE && CtauS != 0) {
118     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
119   }
120   if (stab == STAB_SUPG && !implicit) {
121     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
122   }
123 
124   PetscOptionsEnd();
125 
126   // ------------------------------------------------------
127   //           Set up the PETSc context
128   // ------------------------------------------------------
129   // -- Define derived units
130   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
131 
132   user->units->meter    = meter;
133   user->units->kilogram = kilogram;
134   user->units->second   = second;
135   user->units->Joule    = Joule;
136 
137   // ------------------------------------------------------
138   //           Set up the libCEED context
139   // ------------------------------------------------------
140   // -- Scale variables to desired units
141   E_wind *= Joule;
142   rc = fabs(rc) * meter;
143   for (PetscInt i = 0; i < 3; i++) {
144     wind[i] *= (meter / second);
145     domain_size[i] *= meter;
146   }
147   problem->dm_scale = meter;
148 
149   // -- Setup Context
150   setup_context->rc                     = rc;
151   setup_context->lx                     = domain_size[0];
152   setup_context->ly                     = domain_size[1];
153   setup_context->lz                     = domain_size[2];
154   setup_context->wind[0]                = wind[0];
155   setup_context->wind[1]                = wind[1];
156   setup_context->wind[2]                = wind[2];
157   setup_context->wind_type              = wind_type;
158   setup_context->bubble_type            = bubble_type;
159   setup_context->bubble_continuity_type = bubble_continuity_type;
160   setup_context->time                   = 0;
161 
162   // -- QFunction Context
163   user->phys->stab                   = stab;
164   user->phys->wind_type              = wind_type;
165   user->phys->bubble_type            = bubble_type;
166   user->phys->bubble_continuity_type = bubble_continuity_type;
167   //  if passed correctly
168   user->phys->implicit         = implicit;
169   user->phys->has_curr_time    = has_curr_time;
170   advection_ctx->CtauS         = CtauS;
171   advection_ctx->E_wind        = E_wind;
172   advection_ctx->implicit      = implicit;
173   advection_ctx->strong_form   = strong_form;
174   advection_ctx->stabilization = stab;
175 
176   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
177   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
178   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
179 
180   CeedQFunctionContextCreate(user->ceed, &advection_context);
181   CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx);
182   CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc);
183   problem->apply_vol_rhs.qfunction_context = advection_context;
184   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context);
185   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context);
186   PetscFunctionReturn(PETSC_SUCCESS);
187 }
188 
189 PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx) {
190   MPI_Comm         comm = PETSC_COMM_WORLD;
191   SetupContextAdv  setup_ctx;
192   AdvectionContext advection_ctx;
193 
194   PetscFunctionBeginUser;
195   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx);
196   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx);
197   PetscCall(PetscPrintf(comm,
198                         "  Problem:\n"
199                         "    Problem Name                       : %s\n"
200                         "    Stabilization                      : %s\n"
201                         "    Bubble Type                        : %s (%" CeedInt_FMT "D)\n"
202                         "    Bubble Continuity                  : %s\n"
203                         "    Wind Type                          : %s\n",
204                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], BubbleTypes[setup_ctx->bubble_type],
205                         setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2, BubbleContinuityTypes[setup_ctx->bubble_continuity_type],
206                         WindTypes[setup_ctx->wind_type]));
207 
208   if (setup_ctx->wind_type == WIND_TRANSLATION) {
209     PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
210   }
211   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx);
212   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx);
213   PetscFunctionReturn(PETSC_SUCCESS);
214 }
215