xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision 1864f1c2b4e770a2a9adc26a02ef77fc3a284256)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 /// @file
18 /// Utility functions for setting up DENSITY_CURRENT
19 
20 #include "../navierstokes.h"
21 #include "../qfunctions/setupgeo.h"
22 #include "../qfunctions/densitycurrent.h"
23 
24 PetscErrorCode NS_DENSITY_CURRENT(ProblemData *problem, DM dm, void *setup_ctx,
25                                   void *ctx) {
26   SetupContext      setup_context = *(SetupContext *)setup_ctx;
27   User              user = *(User *)ctx;
28   StabilizationType stab;
29   MPI_Comm          comm = PETSC_COMM_WORLD;
30   PetscBool         implicit;
31   PetscBool         has_curr_time = PETSC_FALSE;
32   PetscInt          ierr;
33   PetscFunctionBeginUser;
34 
35   ierr = PetscCalloc1(1, &user->phys->dc_ctx); CHKERRQ(ierr);
36 
37   // ------------------------------------------------------
38   //               SET UP DENSITY_CURRENT
39   // ------------------------------------------------------
40   problem->dim                     = 3;
41   problem->q_data_size_vol         = 10;
42   problem->q_data_size_sur         = 4;
43   problem->setup_vol               = Setup;
44   problem->setup_vol_loc           = Setup_loc;
45   problem->setup_sur               = SetupBoundary;
46   problem->setup_sur_loc           = SetupBoundary_loc;
47   problem->ics                     = ICsDC;
48   problem->ics_loc                 = ICsDC_loc;
49   problem->apply_vol_rhs           = DC;
50   problem->apply_vol_rhs_loc       = DC_loc;
51   problem->apply_vol_ifunction     = IFunction_DC;
52   problem->apply_vol_ifunction_loc = IFunction_DC_loc;
53   problem->bc                      = Exact_DC;
54   problem->setup_ctx               = SetupContext_DENSITY_CURRENT;
55   problem->bc_func                 = BC_DENSITY_CURRENT;
56   problem->non_zero_time           = PETSC_FALSE;
57   problem->print_info              = PRINT_DENSITY_CURRENT;
58 
59   // ------------------------------------------------------
60   //             Create the libCEED context
61   // ------------------------------------------------------
62   CeedScalar theta0 = 300.;    // K
63   CeedScalar thetaC = -15.;    // K
64   CeedScalar P0     = 1.e5;    // Pa
65   CeedScalar N      = 0.01;    // 1/s
66   CeedScalar cv     = 717.;    // J/(kg K)
67   CeedScalar cp     = 1004.;   // J/(kg K)
68   CeedScalar g      = 9.81;    // m/s^2
69   CeedScalar lambda = -2./3.;  // -
70   CeedScalar mu     = 75.;     // Pa s, dynamic viscosity
71   // mu = 75 is not physical for air, but is good for numerical stability
72   CeedScalar k      = 0.02638; // W/(m K)
73   CeedScalar c_tau  = 0.5;     // -
74   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
75   CeedScalar rc     = 1000.;   // m (Radius of bubble)
76   PetscReal center[3], dc_axis[3] = {0, 0, 0};
77   PetscReal domain_min[3], domain_max[3], domain_size[3];
78   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
79   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
80 
81   // ------------------------------------------------------
82   //             Create the PETSc context
83   // ------------------------------------------------------
84   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
85   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
86   PetscScalar second   = 1e-2;  // 1 second in scaled time units
87   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
88   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
89 
90   // ------------------------------------------------------
91   //              Command line Options
92   // ------------------------------------------------------
93   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem",
94                            NULL); CHKERRQ(ierr);
95   // -- Physics
96   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
97                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
98   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
99                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
100   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
101                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
102   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
103                             NULL, N, &N, NULL); CHKERRQ(ierr);
104   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
105                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
106   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
107                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
108   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
109                             NULL, g, &g, NULL); CHKERRQ(ierr);
110   ierr = PetscOptionsScalar("-lambda",
111                             "Stokes hypothesis second viscosity coefficient",
112                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
113   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
114                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
115   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
116                             NULL, k, &k, NULL); CHKERRQ(ierr);
117   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
118                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
119   for (int i=0; i<3; i++) center[i] = .5*domain_size[i];
120   PetscInt n = problem->dim;
121   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
122                                NULL, center, &n, NULL); CHKERRQ(ierr);
123   n = problem->dim;
124   ierr = PetscOptionsRealArray("-dc_axis",
125                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
126                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
127   {
128     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
129                                    PetscSqr(dc_axis[2]));
130     if (norm > 0) {
131       for (int i=0; i<3; i++)  dc_axis[i] /= norm;
132     }
133   }
134   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
135                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
136                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
137   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
138                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
139   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
140                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
141   CHKERRQ(ierr);
142 
143   // -- Units
144   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
145                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
146   meter = fabs(meter);
147   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
148                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
149   kilogram = fabs(kilogram);
150   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
151                             NULL, second, &second, NULL); CHKERRQ(ierr);
152   second = fabs(second);
153   ierr = PetscOptionsScalar("-units_Kelvin",
154                             "1 Kelvin in scaled temperature units",
155                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
156   Kelvin = fabs(Kelvin);
157 
158   // -- Warnings
159   if (stab == STAB_SUPG && !implicit) {
160     ierr = PetscPrintf(comm,
161                        "Warning! Use -stab supg only with -implicit\n");
162     CHKERRQ(ierr);
163   }
164 
165   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
166 
167   // ------------------------------------------------------
168   //           Set up the PETSc context
169   // ------------------------------------------------------
170   // -- Define derived units
171   Pascal          = kilogram / (meter * PetscSqr(second));
172   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
173   m_per_squared_s = meter / PetscSqr(second);
174   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
175 
176   user->units->meter           = meter;
177   user->units->kilogram        = kilogram;
178   user->units->second          = second;
179   user->units->Kelvin          = Kelvin;
180   user->units->Pascal          = Pascal;
181   user->units->J_per_kg_K      = J_per_kg_K;
182   user->units->m_per_squared_s = m_per_squared_s;
183   user->units->W_per_m_K       = W_per_m_K;
184 
185   // ------------------------------------------------------
186   //           Set up the libCEED context
187   // ------------------------------------------------------
188   // -- Scale variables to desired units
189   theta0 *= Kelvin;
190   thetaC *= Kelvin;
191   P0     *= Pascal;
192   N      *= (1./second);
193   cv     *= J_per_kg_K;
194   cp     *= J_per_kg_K;
195   g      *= m_per_squared_s;
196   mu     *= Pascal * second;
197   k      *= W_per_m_K;
198   rc     = fabs(rc) * meter;
199   for (int i=0; i<3; i++) domain_size[i] *= meter;
200   for (int i=0; i<3; i++) center[i] *= meter;
201   problem->dm_scale = meter;
202 
203   // -- Setup Context
204   setup_context->theta0     = theta0;
205   setup_context->thetaC     = thetaC;
206   setup_context->P0         = P0;
207   setup_context->N          = N;
208   setup_context->cv         = cv;
209   setup_context->cp         = cp;
210   setup_context->g          = g;
211   setup_context->rc         = rc;
212   setup_context->lx         = domain_size[0];
213   setup_context->ly         = domain_size[1];
214   setup_context->lz         = domain_size[2];
215   setup_context->center[0]  = center[0];
216   setup_context->center[1]  = center[1];
217   setup_context->center[2]  = center[2];
218   setup_context->dc_axis[0] = dc_axis[0];
219   setup_context->dc_axis[1] = dc_axis[1];
220   setup_context->dc_axis[2] = dc_axis[2];
221   setup_context->time       = 0;
222 
223   // -- QFunction Context
224   user->phys->stab             = stab;
225   user->phys->implicit         = implicit;
226   user->phys->has_curr_time    = has_curr_time;
227   user->phys->dc_ctx->lambda   = lambda;
228   user->phys->dc_ctx->mu       = mu;
229   user->phys->dc_ctx->k        = k;
230   user->phys->dc_ctx->cv       = cv;
231   user->phys->dc_ctx->cp       = cp;
232   user->phys->dc_ctx->g        = g;
233   user->phys->dc_ctx->c_tau    = c_tau;
234   user->phys->dc_ctx->stabilization = stab;
235 
236   PetscFunctionReturn(0);
237 }
238 
239 PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, CeedData ceed_data,
240     AppCtx app_ctx, SetupContext setup_ctx,
241     Physics phys) {
242   PetscFunctionBeginUser;
243 
244   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
245   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
246                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
247   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
248   CeedQFunctionContextCreate(ceed, &ceed_data->dc_context);
249   CeedQFunctionContextSetData(ceed_data->dc_context, CEED_MEM_HOST,
250                               CEED_USE_POINTER,
251                               sizeof(*phys->dc_ctx), phys->dc_ctx);
252   if (ceed_data->qf_rhs_vol)
253     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->dc_context);
254   if (ceed_data->qf_ifunction_vol)
255     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->dc_context);
256 
257   PetscFunctionReturn(0);
258 }
259 
260 PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys,
261                                   void *setup_ctx) {
262 
263   PetscInt       len;
264   PetscBool      flg;
265   MPI_Comm       comm = PETSC_COMM_WORLD;
266   PetscErrorCode ierr;
267   PetscFunctionBeginUser;
268 
269   // Default boundary conditions
270   //   slip bc on all faces and no wall bc
271   bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 2;
272   bc->slips[0][0] = 5;
273   bc->slips[0][1] = 6;
274   bc->slips[1][0] = 3;
275   bc->slips[1][1] = 4;
276   bc->slips[2][0] = 1;
277   bc->slips[2][1] = 2;
278 
279   // Parse command line options
280   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT BCs ",
281                            NULL); CHKERRQ(ierr);
282   ierr = PetscOptionsIntArray("-bc_wall",
283                               "Use wall boundary conditions on this list of faces",
284                               NULL, bc->walls,
285                               (len = sizeof(bc->walls) / sizeof(bc->walls[0]),
286                                &len), &flg); CHKERRQ(ierr);
287   if (flg) {
288     bc->num_wall = len;
289     // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
290     bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 0;
291   }
292   for (PetscInt j=0; j<3; j++) {
293     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
294     ierr = PetscOptionsIntArray(flags[j],
295                                 "Use slip boundary conditions on this list of faces",
296                                 NULL, bc->slips[j],
297                                 (len = sizeof(bc->slips[j]) / sizeof(bc->slips[j][0]),
298                                  &len), &flg); CHKERRQ(ierr);
299     if (flg) {
300       bc->num_slip[j] = len;
301       bc->user_bc = PETSC_TRUE;
302     }
303   }
304   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
305 
306   {
307     // Set slip boundary conditions
308     DMLabel label;
309     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
310     PetscInt comps[1] = {1};
311     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label,
312                          bc->num_slip[0], bc->slips[0], 0, 1, comps,
313                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
314     CHKERRQ(ierr);
315     comps[0] = 2;
316     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label,
317                          bc->num_slip[1], bc->slips[1], 0, 1, comps,
318                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
319     CHKERRQ(ierr);
320     comps[0] = 3;
321     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label,
322                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
323                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
324     CHKERRQ(ierr);
325   }
326 
327   if (bc->user_bc) {
328     for (PetscInt c = 0; c < 3; c++) {
329       for (PetscInt s = 0; s < bc->num_slip[c]; s++) {
330         for (PetscInt w = 0; w < bc->num_wall; w++) {
331           if (bc->slips[c][s] == bc->walls[w])
332             SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
333                      "Boundary condition already set on face %D!\n",
334                      bc->walls[w]);
335         }
336       }
337     }
338   }
339 
340   // Set wall boundary conditions
341   //   zero velocity and zero flux for mass density and energy density
342   {
343     DMLabel  label;
344     PetscInt comps[3] = {1, 2, 3};
345     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
346     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label,
347                          bc->num_wall, bc->walls, 0, 3, comps,
348                          (void(*)(void))Exact_DC, NULL,
349                          setup_ctx, NULL); CHKERRQ(ierr);
350   }
351 
352   PetscFunctionReturn(0);
353 }
354 
355 PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
356                                      AppCtx app_ctx) {
357   MPI_Comm       comm = PETSC_COMM_WORLD;
358   PetscErrorCode ierr;
359   PetscFunctionBeginUser;
360 
361   ierr = PetscPrintf(comm,
362                      "  Problem:\n"
363                      "    Problem Name                       : %s\n"
364                      "    Stabilization                      : %s\n",
365                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
366   CHKERRQ(ierr);
367 
368   PetscFunctionReturn(0);
369 }
370