xref: /libCEED/examples/fluids/problems/advection.c (revision a71fcd9fac4e7a8dfa69a197fd7b41b8f31fd6a3)
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   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
79   // -- Physics
80   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
81                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
82   PetscBool translation;
83   ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection",
84                           NULL, WindTypes,
85                           (PetscEnum)(wind_type = WIND_ROTATION),
86                           (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr);
87   if (translation) user->phys->has_neumann = PETSC_TRUE;
88   PetscInt n = problem->dim;
89   PetscBool user_wind;
90   ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector",
91                                NULL, wind, &n, &user_wind); CHKERRQ(ierr);
92   ierr = PetscOptionsScalar("-CtauS",
93                             "Scale coefficient for tau (nondimensional)",
94                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
95   ierr = PetscOptionsScalar("-strong_form",
96                             "Strong (1) or weak/integrated by parts (0) advection residual",
97                             NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr);
98   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
99                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
100   ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)",
101                           NULL, BubbleTypes,
102                           (PetscEnum)(bubble_type = BUBBLE_SPHERE),
103                           (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr);
104   ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick",
105                           NULL, BubbleContinuityTypes,
106                           (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH),
107                           (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr);
108   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
109                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
110                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
111   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
112                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
113   CHKERRQ(ierr);
114 
115   // -- Units
116   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
117                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
118   meter = fabs(meter);
119   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
120                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
121   kilogram = fabs(kilogram);
122   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
123                             NULL, second, &second, NULL); CHKERRQ(ierr);
124   second = fabs(second);
125 
126   // -- Warnings
127   if (wind_type == WIND_ROTATION && user_wind) {
128     ierr = PetscPrintf(comm,
129                        "Warning! Use -wind_translation only with -wind_type translation\n");
130     CHKERRQ(ierr);
131   }
132   if (wind_type == WIND_TRANSLATION
133       && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
134     wind[2] = 0;
135     ierr = PetscPrintf(comm,
136                        "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n");
137     CHKERRQ(ierr);
138   }
139   if (stab == STAB_NONE && CtauS != 0) {
140     ierr = PetscPrintf(comm,
141                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
142     CHKERRQ(ierr);
143   }
144   if (stab == STAB_SUPG && !implicit) {
145     ierr = PetscPrintf(comm,
146                        "Warning! Use -stab supg only with -implicit\n");
147     CHKERRQ(ierr);
148   }
149 
150   PetscOptionsEnd();
151 
152   // ------------------------------------------------------
153   //           Set up the PETSc context
154   // ------------------------------------------------------
155   // -- Define derived units
156   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
157 
158   user->units->meter    = meter;
159   user->units->kilogram = kilogram;
160   user->units->second   = second;
161   user->units->Joule    = Joule;
162 
163   // ------------------------------------------------------
164   //           Set up the libCEED context
165   // ------------------------------------------------------
166   // -- Scale variables to desired units
167   E_wind *= Joule;
168   rc = fabs(rc) * meter;
169   for (int i=0; i<3; i++) {
170     wind[i] *= (meter/second);
171     domain_size[i] *= meter;
172   }
173   problem->dm_scale = meter;
174 
175   // -- Setup Context
176   setup_context->rc                     = rc;
177   setup_context->lx                     = domain_size[0];
178   setup_context->ly                     = domain_size[1];
179   setup_context->lz                     = domain_size[2];
180   setup_context->wind[0]                = wind[0];
181   setup_context->wind[1]                = wind[1];
182   setup_context->wind[2]                = wind[2];
183   setup_context->wind_type              = wind_type;
184   setup_context->bubble_type            = bubble_type;
185   setup_context->bubble_continuity_type = bubble_continuity_type;
186   setup_context->time = 0;
187 
188   // -- QFunction Context
189   user->phys->stab                         = stab;
190   user->phys->wind_type                    = wind_type;
191   user->phys->bubble_type                  = bubble_type;
192   user->phys->bubble_continuity_type       = bubble_continuity_type;
193   //  if passed correctly
194   user->phys->implicit                     = implicit;
195   user->phys->has_curr_time                = has_curr_time;
196   user->phys->advection_ctx->CtauS         = CtauS;
197   user->phys->advection_ctx->E_wind        = E_wind;
198   user->phys->advection_ctx->implicit      = implicit;
199   user->phys->advection_ctx->strong_form   = strong_form;
200   user->phys->advection_ctx->stabilization = stab;
201 
202   PetscFunctionReturn(0);
203 }
204 
205 PetscErrorCode SetupContext_ADVECTION(Ceed ceed, CeedData ceed_data,
206                                       AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
207   PetscFunctionBeginUser;
208   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
209   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
210                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
211   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
212   CeedQFunctionContextCreate(ceed, &ceed_data->advection_context);
213   CeedQFunctionContextSetData(ceed_data->advection_context, CEED_MEM_HOST,
214                               CEED_USE_POINTER,
215                               sizeof(*phys->advection_ctx), phys->advection_ctx);
216   if (ceed_data->qf_rhs_vol)
217     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->advection_context);
218   if (ceed_data->qf_ifunction_vol)
219     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
220                             ceed_data->advection_context);
221   if (ceed_data->qf_apply_inflow)
222     CeedQFunctionSetContext(ceed_data->qf_apply_inflow,
223                             ceed_data->advection_context);
224   PetscFunctionReturn(0);
225 }
226 
227 PetscErrorCode PRINT_ADVECTION(Physics phys, SetupContext setup_ctx,
228                                AppCtx app_ctx) {
229   MPI_Comm       comm = PETSC_COMM_WORLD;
230   PetscErrorCode ierr;
231   PetscFunctionBeginUser;
232 
233   ierr = PetscPrintf(comm,
234                      "  Problem:\n"
235                      "    Problem Name                       : %s\n"
236                      "    Stabilization                      : %s\n"
237                      "    Bubble Type                        : %s (%dD)\n"
238                      "    Bubble Continuity                  : %s\n"
239                      "    Wind Type                          : %s\n",
240                      app_ctx->problem_name, StabilizationTypes[phys->stab],
241                      BubbleTypes[phys->bubble_type],
242                      phys->bubble_type == BUBBLE_SPHERE ? 3 : 2,
243                      BubbleContinuityTypes[phys->bubble_continuity_type],
244                      WindTypes[phys->wind_type]); CHKERRQ(ierr);
245 
246   if (phys->wind_type == WIND_TRANSLATION) {
247     ierr = PetscPrintf(comm,
248                        "    Background Wind                    : %f,%f,%f\n",
249                        setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr);
250   }
251   PetscFunctionReturn(0);
252 }
253