xref: /libCEED/examples/fluids/problems/advection.c (revision 2459f3f1cd4d7d2e210e1c26d669bd2fde41a0b6)
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 "../navierstokes.h"
12 #include "../qfunctions/setupgeo.h"
13 #include "../qfunctions/advection.h"
14 
15 PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *setup_ctx,
16                             void *ctx) {
17   WindType             wind_type;
18   BubbleType           bubble_type;
19   BubbleContinuityType bubble_continuity_type;
20   StabilizationType    stab;
21   SetupContext         setup_context = *(SetupContext *)setup_ctx;
22   User                 user = *(User *)ctx;
23   MPI_Comm             comm = PETSC_COMM_WORLD;
24   PetscBool            implicit;
25   PetscBool            has_curr_time = PETSC_FALSE;
26   PetscInt             ierr;
27   PetscFunctionBeginUser;
28 
29   ierr = PetscCalloc1(1, &user->phys->advection_ctx); CHKERRQ(ierr);
30 
31   // ------------------------------------------------------
32   //               SET UP ADVECTION
33   // ------------------------------------------------------
34   problem->dim                     = 3;
35   problem->q_data_size_vol         = 10;
36   problem->q_data_size_sur         = 4;
37   problem->setup_vol               = Setup;
38   problem->setup_vol_loc           = Setup_loc;
39   problem->setup_sur               = SetupBoundary;
40   problem->setup_sur_loc           = SetupBoundary_loc;
41   problem->ics                     = ICsAdvection;
42   problem->ics_loc                 = ICsAdvection_loc;
43   problem->apply_vol_rhs           = Advection;
44   problem->apply_vol_rhs_loc       = Advection_loc;
45   problem->apply_vol_ifunction     = IFunction_Advection;
46   problem->apply_vol_ifunction_loc = IFunction_Advection_loc;
47   problem->apply_inflow            = Advection_InOutFlow;
48   problem->apply_inflow_loc        = Advection_InOutFlow_loc;
49   problem->bc                      = Exact_Advection;
50   problem->setup_ctx               = SetupContext_ADVECTION;
51   problem->non_zero_time           = PETSC_FALSE;
52   problem->print_info              = PRINT_ADVECTION;
53 
54   // ------------------------------------------------------
55   //             Create the libCEED context
56   // ------------------------------------------------------
57   CeedScalar rc          = 1000.;      // m (Radius of bubble)
58   CeedScalar CtauS       = 0.;         // dimensionless
59   CeedScalar strong_form = 0.;         // [0,1]
60   CeedScalar E_wind      = 1.e6;       // J
61   PetscReal wind[3]      = {1., 0, 0}; // m/s
62   PetscReal domain_min[3], domain_max[3], domain_size[3];
63   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
64   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
65 
66 
67   // ------------------------------------------------------
68   //             Create the PETSc context
69   // ------------------------------------------------------
70   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
71   PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units
72   PetscScalar second   = 1e-2; // 1 second in scaled time units
73   PetscScalar Joule;
74 
75   // ------------------------------------------------------
76   //              Command line Options
77   // ------------------------------------------------------
78   ierr = PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem",
79                            NULL); CHKERRQ(ierr);
80   // -- Physics
81   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
82                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
83   PetscBool translation;
84   ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection",
85                           NULL, WindTypes,
86                           (PetscEnum)(wind_type = WIND_ROTATION),
87                           (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr);
88   if (translation) user->phys->has_neumann = PETSC_TRUE;
89   PetscInt n = problem->dim;
90   PetscBool user_wind;
91   ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector",
92                                NULL, wind, &n, &user_wind); CHKERRQ(ierr);
93   ierr = PetscOptionsScalar("-CtauS",
94                             "Scale coefficient for tau (nondimensional)",
95                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
96   ierr = PetscOptionsScalar("-strong_form",
97                             "Strong (1) or weak/integrated by parts (0) advection residual",
98                             NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr);
99   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
100                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
101   ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)",
102                           NULL, BubbleTypes,
103                           (PetscEnum)(bubble_type = BUBBLE_SPHERE),
104                           (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr);
105   ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick",
106                           NULL, BubbleContinuityTypes,
107                           (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH),
108                           (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr);
109   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
110                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
111                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
112   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
113                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
114   CHKERRQ(ierr);
115 
116   // -- Units
117   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
118                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
119   meter = fabs(meter);
120   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
121                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
122   kilogram = fabs(kilogram);
123   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
124                             NULL, second, &second, NULL); CHKERRQ(ierr);
125   second = fabs(second);
126 
127   // -- Warnings
128   if (wind_type == WIND_ROTATION && user_wind) {
129     ierr = PetscPrintf(comm,
130                        "Warning! Use -wind_translation only with -wind_type translation\n");
131     CHKERRQ(ierr);
132   }
133   if (wind_type == WIND_TRANSLATION
134       && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
135     wind[2] = 0;
136     ierr = PetscPrintf(comm,
137                        "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n");
138     CHKERRQ(ierr);
139   }
140   if (stab == STAB_NONE && CtauS != 0) {
141     ierr = PetscPrintf(comm,
142                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
143     CHKERRQ(ierr);
144   }
145   if (stab == STAB_SUPG && !implicit) {
146     ierr = PetscPrintf(comm,
147                        "Warning! Use -stab supg only with -implicit\n");
148     CHKERRQ(ierr);
149   }
150 
151   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
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 (int i=0; i<3; 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                     = domain_size[2];
181   setup_context->wind[0]                = wind[0];
182   setup_context->wind[1]                = wind[1];
183   setup_context->wind[2]                = wind[2];
184   setup_context->wind_type              = wind_type;
185   setup_context->bubble_type            = bubble_type;
186   setup_context->bubble_continuity_type = bubble_continuity_type;
187   setup_context->time = 0;
188 
189   // -- QFunction Context
190   user->phys->stab                         = stab;
191   user->phys->wind_type                    = wind_type;
192   user->phys->bubble_type                  = bubble_type;
193   user->phys->bubble_continuity_type       = bubble_continuity_type;
194   //  if passed correctly
195   user->phys->implicit                     = implicit;
196   user->phys->has_curr_time                = has_curr_time;
197   user->phys->advection_ctx->CtauS         = CtauS;
198   user->phys->advection_ctx->E_wind        = E_wind;
199   user->phys->advection_ctx->implicit      = implicit;
200   user->phys->advection_ctx->strong_form   = strong_form;
201   user->phys->advection_ctx->stabilization = stab;
202 
203   PetscFunctionReturn(0);
204 }
205 
206 PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data,
207                                       AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
208   PetscFunctionBeginUser;
209   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
210   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
211                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
212   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
213   CeedQFunctionContextCreate(ceed, &ceed_data->advection_context);
214   CeedQFunctionContextSetData(ceed_data->advection_context, CEED_MEM_HOST,
215                               CEED_USE_POINTER,
216                               sizeof(*phys->advection_ctx), phys->advection_ctx);
217   if (ceed_data->qf_rhs_vol)
218     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->advection_context);
219   if (ceed_data->qf_ifunction_vol)
220     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
221                             ceed_data->advection_context);
222   if (ceed_data->qf_apply_inflow)
223     CeedQFunctionSetContext(ceed_data->qf_apply_inflow,
224                             ceed_data->advection_context);
225   PetscFunctionReturn(0);
226 }
227 
228 PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx,
229                                AppCtx app_ctx) {
230   MPI_Comm       comm = PETSC_COMM_WORLD;
231   PetscErrorCode ierr;
232   PetscFunctionBeginUser;
233 
234   ierr = PetscPrintf(comm,
235                      "  Problem:\n"
236                      "    Problem Name                       : %s\n"
237                      "    Stabilization                      : %s\n"
238                      "    Bubble Type                        : %s (%dD)\n"
239                      "    Bubble Continuity                  : %s\n"
240                      "    Wind Type                          : %s\n",
241                      app_ctx->problem_name, StabilizationTypes[phys->stab],
242                      BubbleTypes[phys->bubble_type],
243                      phys->bubble_type == BUBBLE_SPHERE ? 3 : 2,
244                      BubbleContinuityTypes[phys->bubble_continuity_type],
245                      WindTypes[phys->wind_type]); CHKERRQ(ierr);
246 
247   if (phys->wind_type == WIND_TRANSLATION) {
248     ierr = PetscPrintf(comm,
249                        "    Background Wind                    : %f,%f,%f\n",
250                        setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr);
251   }
252   PetscFunctionReturn(0);
253 }
254