xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision bcb2dfae4c301ddfdddf58806f08f6e7d17f4ea5)
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, 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->bc_func                 = BC_DENSITY_CURRENT;
55   problem->non_zero_time           = PETSC_FALSE;
56   problem->print_info              = PRINT_DENSITY_CURRENT;
57 
58   // ------------------------------------------------------
59   //             Create the libCEED context
60   // ------------------------------------------------------
61   CeedScalar theta0 = 300.;    // K
62   CeedScalar thetaC = -15.;    // K
63   CeedScalar P0     = 1.e5;    // Pa
64   CeedScalar N      = 0.01;    // 1/s
65   CeedScalar cv     = 717.;    // J/(kg K)
66   CeedScalar cp     = 1004.;   // J/(kg K)
67   CeedScalar g      = 9.81;    // m/s^2
68   CeedScalar lambda = -2./3.;  // -
69   CeedScalar mu     = 75.;     // Pa s, dynamic viscosity
70   // mu = 75 is not physical for air, but is good for numerical stability
71   CeedScalar k      = 0.02638; // W/(m K)
72   PetscScalar lx    = 8000.;   // m
73   PetscScalar ly    = 8000.;   // m
74   PetscScalar lz    = 4000.;   // m
75   CeedScalar rc     = 1000.;   // m (Radius of bubble)
76   PetscReal center[3], dc_axis[3] = {0, 0, 0};
77   CeedScalar Rd;
78 
79   // ------------------------------------------------------
80   //             Create the PETSc context
81   // ------------------------------------------------------
82   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
83   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
84   PetscScalar second   = 1e-2;  // 1 second in scaled time units
85   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
86   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
87 
88   // ------------------------------------------------------
89   //              Command line Options
90   // ------------------------------------------------------
91   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem",
92                            NULL); CHKERRQ(ierr);
93   // -- Physics
94   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
95                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
96   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
97                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
98   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
99                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
100   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
101                             NULL, N, &N, NULL); CHKERRQ(ierr);
102   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
103                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
104   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
105                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
106   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
107                             NULL, g, &g, NULL); CHKERRQ(ierr);
108   ierr = PetscOptionsScalar("-lambda",
109                             "Stokes hypothesis second viscosity coefficient",
110                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
111   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
112                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
113   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
114                             NULL, k, &k, NULL); CHKERRQ(ierr);
115   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
116                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
117   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
118                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
119   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
120                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
121   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
122                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
123   PetscInt n = problem->dim;
124   center[0] = 0.5 * lx;
125   center[1] = 0.5 * ly;
126   center[2] = 0.5 * lz;
127   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
128                                NULL, center, &n, NULL); CHKERRQ(ierr);
129   n = problem->dim;
130   ierr = PetscOptionsRealArray("-dc_axis",
131                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
132                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
133   {
134     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
135                                    PetscSqr(dc_axis[2]));
136     if (norm > 0) {
137       for (int i=0; i<3; i++)  dc_axis[i] /= norm;
138     }
139   }
140   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
141                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
142                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
143 
144   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
145                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
146   CHKERRQ(ierr);
147 
148   // -- Units
149   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
150                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
151   meter = fabs(meter);
152   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
153                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
154   kilogram = fabs(kilogram);
155   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
156                             NULL, second, &second, NULL); CHKERRQ(ierr);
157   second = fabs(second);
158   ierr = PetscOptionsScalar("-units_Kelvin",
159                             "1 Kelvin in scaled temperature units",
160                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
161   Kelvin = fabs(Kelvin);
162 
163   // -- Warnings
164   if (stab == STAB_SUPG && !implicit) {
165     ierr = PetscPrintf(comm,
166                        "Warning! Use -stab supg only with -implicit\n");
167     CHKERRQ(ierr);
168   }
169 
170   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
171 
172   // ------------------------------------------------------
173   //           Set up the PETSc context
174   // ------------------------------------------------------
175   // -- Define derived units
176   Pascal          = kilogram / (meter * PetscSqr(second));
177   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
178   m_per_squared_s = meter / PetscSqr(second);
179   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
180 
181   user->units->meter           = meter;
182   user->units->kilogram        = kilogram;
183   user->units->second          = second;
184   user->units->Kelvin          = Kelvin;
185   user->units->Pascal          = Pascal;
186   user->units->J_per_kg_K      = J_per_kg_K;
187   user->units->m_per_squared_s = m_per_squared_s;
188   user->units->W_per_m_K       = W_per_m_K;
189 
190   // ------------------------------------------------------
191   //           Set up the libCEED context
192   // ------------------------------------------------------
193   // -- Scale variables to desired units
194   theta0 *= Kelvin;
195   thetaC *= Kelvin;
196   P0     *= Pascal;
197   N      *= (1./second);
198   cv     *= J_per_kg_K;
199   cp     *= J_per_kg_K;
200   Rd     = cp - cv;
201   g      *= m_per_squared_s;
202   mu     *= Pascal * second;
203   k      *= W_per_m_K;
204   lx     = fabs(lx) * meter;
205   ly     = fabs(ly) * meter;
206   lz     = fabs(lz) * meter;
207   rc     = fabs(rc) * meter;
208   for (int i=0; i<3; i++) center[i] *= meter;
209 
210   // -- Setup Context
211   setup_context->theta0     = theta0;
212   setup_context->thetaC     = thetaC;
213   setup_context->P0         = P0;
214   setup_context->N          = N;
215   setup_context->cv         = cv;
216   setup_context->cp         = cp;
217   setup_context->Rd         = Rd;
218   setup_context->g          = g;
219   setup_context->rc         = rc;
220   setup_context->lx         = lx;
221   setup_context->ly         = ly;
222   setup_context->lz         = lz;
223   setup_context->center[0]  = center[0];
224   setup_context->center[1]  = center[1];
225   setup_context->center[2]  = center[2];
226   setup_context->dc_axis[0] = dc_axis[0];
227   setup_context->dc_axis[1] = dc_axis[1];
228   setup_context->dc_axis[2] = dc_axis[2];
229   setup_context->time       = 0;
230 
231   // -- QFunction Context
232   user->phys->stab             = stab;
233   user->phys->implicit         = implicit;
234   user->phys->has_curr_time    = has_curr_time;
235   user->phys->dc_ctx->lambda   = lambda;
236   user->phys->dc_ctx->mu       = mu;
237   user->phys->dc_ctx->k        = k;
238   user->phys->dc_ctx->cv       = cv;
239   user->phys->dc_ctx->cp       = cp;
240   user->phys->dc_ctx->g        = g;
241   user->phys->dc_ctx->Rd       = Rd;
242   user->phys->dc_ctx->stabilization = stab;
243 
244   PetscFunctionReturn(0);
245 }
246 
247 PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys,
248                                   void *setup_ctx) {
249 
250   PetscInt       len;
251   PetscBool      flg;
252   MPI_Comm       comm = PETSC_COMM_WORLD;
253   PetscErrorCode ierr;
254   PetscFunctionBeginUser;
255 
256   // Default boundary conditions
257   //   slip bc on all faces and no wall bc
258   bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 2;
259   bc->slips[0][0] = 5;
260   bc->slips[0][1] = 6;
261   bc->slips[1][0] = 3;
262   bc->slips[1][1] = 4;
263   bc->slips[2][0] = 1;
264   bc->slips[2][1] = 2;
265 
266   // Parse command line options
267   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT BCs ",
268                            NULL); CHKERRQ(ierr);
269   ierr = PetscOptionsIntArray("-bc_wall",
270                               "Use wall boundary conditions on this list of faces",
271                               NULL, bc->walls,
272                               (len = sizeof(bc->walls) / sizeof(bc->walls[0]),
273                                &len), &flg); CHKERRQ(ierr);
274   if (flg) {
275     bc->num_wall = len;
276     // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
277     bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 0;
278   }
279   for (PetscInt j=0; j<3; j++) {
280     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
281     ierr = PetscOptionsIntArray(flags[j],
282                                 "Use slip boundary conditions on this list of faces",
283                                 NULL, bc->slips[j],
284                                 (len = sizeof(bc->slips[j]) / sizeof(bc->slips[j][0]),
285                                  &len), &flg); CHKERRQ(ierr);
286     if (flg) {
287       bc->num_slip[j] = len;
288       bc->user_bc = PETSC_TRUE;
289     }
290   }
291   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
292 
293   {
294     // Set slip boundary conditions
295     DMLabel label;
296     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
297     PetscInt comps[1] = {1};
298     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, "Face Sets",
299                          bc->num_slip[0], bc->slips[0], 0, 1, comps,
300                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
301     CHKERRQ(ierr);
302     comps[0] = 2;
303     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, "Face Sets",
304                          bc->num_slip[1], bc->slips[1], 0, 1, comps,
305                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
306     CHKERRQ(ierr);
307     comps[0] = 3;
308     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
309                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
310                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
311     CHKERRQ(ierr);
312   }
313 
314   if (bc->user_bc == PETSC_TRUE) {
315     for (PetscInt c = 0; c < 3; c++) {
316       for (PetscInt s = 0; s < bc->num_slip[c]; s++) {
317         for (PetscInt w = 0; w < bc->num_wall; w++) {
318           if (bc->slips[c][s] == bc->walls[w])
319             SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
320                      "Boundary condition already set on face %D!\n",
321                      bc->walls[w]);
322         }
323       }
324     }
325   }
326 
327   // Set wall boundary conditions
328   //   zero velocity and zero flux for mass density and energy density
329   {
330     DMLabel  label;
331     PetscInt comps[3] = {1, 2, 3};
332     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
333     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, "Face Sets",
334                          bc->num_wall, bc->walls, 0,
335                          3, comps, (void(*)(void))Exact_DC, NULL,
336                          setup_ctx, NULL); CHKERRQ(ierr);
337   }
338 
339   PetscFunctionReturn(0);
340 }
341 
342 PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
343                                      AppCtx app_ctx) {
344   MPI_Comm       comm = PETSC_COMM_WORLD;
345   PetscErrorCode ierr;
346   PetscFunctionBeginUser;
347 
348   ierr = PetscPrintf(comm,
349                      "  Problem:\n"
350                      "    Problem Name                       : %s\n"
351                      "    Stabilization                      : %s\n",
352                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
353   CHKERRQ(ierr);
354 
355   PetscFunctionReturn(0);
356 }
357