xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision 318af0d1a760765edef6872663e190e11f82cd8e)
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->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   PetscScalar lx    = 8000.;   // m
76   PetscScalar ly    = 8000.;   // m
77   PetscScalar lz    = 4000.;   // m
78   CeedScalar rc     = 1000.;   // m (Radius of bubble)
79   PetscReal center[3], dc_axis[3] = {0, 0, 0};
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("-lx", "Length scale in x direction",
118                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
119   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
120                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
121   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
122                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
123   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
124                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
125   PetscInt n = problem->dim;
126   center[0] = 0.5 * lx;
127   center[1] = 0.5 * ly;
128   center[2] = 0.5 * lz;
129   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
130                                NULL, center, &n, NULL); CHKERRQ(ierr);
131   n = problem->dim;
132   ierr = PetscOptionsRealArray("-dc_axis",
133                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
134                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
135   {
136     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
137                                    PetscSqr(dc_axis[2]));
138     if (norm > 0) {
139       for (int i=0; i<3; i++)  dc_axis[i] /= norm;
140     }
141   }
142   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
143                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
144                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
145   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
146                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
147   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
148                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
149   CHKERRQ(ierr);
150 
151   // -- Units
152   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
153                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
154   meter = fabs(meter);
155   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
156                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
157   kilogram = fabs(kilogram);
158   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
159                             NULL, second, &second, NULL); CHKERRQ(ierr);
160   second = fabs(second);
161   ierr = PetscOptionsScalar("-units_Kelvin",
162                             "1 Kelvin in scaled temperature units",
163                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
164   Kelvin = fabs(Kelvin);
165 
166   // -- Warnings
167   if (stab == STAB_SUPG && !implicit) {
168     ierr = PetscPrintf(comm,
169                        "Warning! Use -stab supg only with -implicit\n");
170     CHKERRQ(ierr);
171   }
172 
173   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
174 
175   // ------------------------------------------------------
176   //           Set up the PETSc context
177   // ------------------------------------------------------
178   // -- Define derived units
179   Pascal          = kilogram / (meter * PetscSqr(second));
180   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
181   m_per_squared_s = meter / PetscSqr(second);
182   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
183 
184   user->units->meter           = meter;
185   user->units->kilogram        = kilogram;
186   user->units->second          = second;
187   user->units->Kelvin          = Kelvin;
188   user->units->Pascal          = Pascal;
189   user->units->J_per_kg_K      = J_per_kg_K;
190   user->units->m_per_squared_s = m_per_squared_s;
191   user->units->W_per_m_K       = W_per_m_K;
192 
193   // ------------------------------------------------------
194   //           Set up the libCEED context
195   // ------------------------------------------------------
196   // -- Scale variables to desired units
197   theta0 *= Kelvin;
198   thetaC *= Kelvin;
199   P0     *= Pascal;
200   N      *= (1./second);
201   cv     *= J_per_kg_K;
202   cp     *= J_per_kg_K;
203   g      *= m_per_squared_s;
204   mu     *= Pascal * second;
205   k      *= W_per_m_K;
206   lx     = fabs(lx) * meter;
207   ly     = fabs(ly) * meter;
208   lz     = fabs(lz) * meter;
209   rc     = fabs(rc) * meter;
210   for (int i=0; i<3; i++) center[i] *= meter;
211 
212   // -- Setup Context
213   setup_context->theta0     = theta0;
214   setup_context->thetaC     = thetaC;
215   setup_context->P0         = P0;
216   setup_context->N          = N;
217   setup_context->cv         = cv;
218   setup_context->cp         = cp;
219   setup_context->g          = g;
220   setup_context->rc         = rc;
221   setup_context->lx         = lx;
222   setup_context->ly         = ly;
223   setup_context->lz         = lz;
224   setup_context->center[0]  = center[0];
225   setup_context->center[1]  = center[1];
226   setup_context->center[2]  = center[2];
227   setup_context->dc_axis[0] = dc_axis[0];
228   setup_context->dc_axis[1] = dc_axis[1];
229   setup_context->dc_axis[2] = dc_axis[2];
230   setup_context->time       = 0;
231 
232   // -- QFunction Context
233   user->phys->stab             = stab;
234   user->phys->implicit         = implicit;
235   user->phys->has_curr_time    = has_curr_time;
236   user->phys->dc_ctx->lambda   = lambda;
237   user->phys->dc_ctx->mu       = mu;
238   user->phys->dc_ctx->k        = k;
239   user->phys->dc_ctx->cv       = cv;
240   user->phys->dc_ctx->cp       = cp;
241   user->phys->dc_ctx->g        = g;
242   user->phys->dc_ctx->c_tau    = c_tau;
243   user->phys->dc_ctx->stabilization = stab;
244 
245   PetscFunctionReturn(0);
246 }
247 
248 PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, CeedData ceed_data,
249     AppCtx app_ctx, SetupContext setup_ctx,
250     Physics phys) {
251   PetscFunctionBeginUser;
252 
253   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
254   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
255                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
256   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
257   CeedQFunctionContextCreate(ceed, &ceed_data->dc_context);
258   CeedQFunctionContextSetData(ceed_data->dc_context, CEED_MEM_HOST,
259                               CEED_USE_POINTER,
260                               sizeof(*phys->dc_ctx), phys->dc_ctx);
261   if (ceed_data->qf_rhs_vol)
262     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->dc_context);
263   if (ceed_data->qf_ifunction_vol)
264     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->dc_context);
265 
266   PetscFunctionReturn(0);
267 }
268 
269 PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys,
270                                   void *setup_ctx) {
271 
272   PetscInt       len;
273   PetscBool      flg;
274   MPI_Comm       comm = PETSC_COMM_WORLD;
275   PetscErrorCode ierr;
276   PetscFunctionBeginUser;
277 
278   // Default boundary conditions
279   //   slip bc on all faces and no wall bc
280   bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 2;
281   bc->slips[0][0] = 5;
282   bc->slips[0][1] = 6;
283   bc->slips[1][0] = 3;
284   bc->slips[1][1] = 4;
285   bc->slips[2][0] = 1;
286   bc->slips[2][1] = 2;
287 
288   // Parse command line options
289   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT BCs ",
290                            NULL); CHKERRQ(ierr);
291   ierr = PetscOptionsIntArray("-bc_wall",
292                               "Use wall boundary conditions on this list of faces",
293                               NULL, bc->walls,
294                               (len = sizeof(bc->walls) / sizeof(bc->walls[0]),
295                                &len), &flg); CHKERRQ(ierr);
296   if (flg) {
297     bc->num_wall = len;
298     // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
299     bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 0;
300   }
301   for (PetscInt j=0; j<3; j++) {
302     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
303     ierr = PetscOptionsIntArray(flags[j],
304                                 "Use slip boundary conditions on this list of faces",
305                                 NULL, bc->slips[j],
306                                 (len = sizeof(bc->slips[j]) / sizeof(bc->slips[j][0]),
307                                  &len), &flg); CHKERRQ(ierr);
308     if (flg) {
309       bc->num_slip[j] = len;
310       bc->user_bc = PETSC_TRUE;
311     }
312   }
313   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
314 
315   {
316     // Set slip boundary conditions
317     DMLabel label;
318     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
319     PetscInt comps[1] = {1};
320     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, "Face Sets",
321                          bc->num_slip[0], bc->slips[0], 0, 1, comps,
322                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
323     CHKERRQ(ierr);
324     comps[0] = 2;
325     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, "Face Sets",
326                          bc->num_slip[1], bc->slips[1], 0, 1, comps,
327                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
328     CHKERRQ(ierr);
329     comps[0] = 3;
330     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
331                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
332                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
333     CHKERRQ(ierr);
334   }
335 
336   if (bc->user_bc) {
337     for (PetscInt c = 0; c < 3; c++) {
338       for (PetscInt s = 0; s < bc->num_slip[c]; s++) {
339         for (PetscInt w = 0; w < bc->num_wall; w++) {
340           if (bc->slips[c][s] == bc->walls[w])
341             SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
342                      "Boundary condition already set on face %D!\n",
343                      bc->walls[w]);
344         }
345       }
346     }
347   }
348 
349   // Set wall boundary conditions
350   //   zero velocity and zero flux for mass density and energy density
351   {
352     DMLabel  label;
353     PetscInt comps[3] = {1, 2, 3};
354     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
355     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, "Face Sets",
356                          bc->num_wall, bc->walls, 0,
357                          3, comps, (void(*)(void))Exact_DC, NULL,
358                          setup_ctx, NULL); CHKERRQ(ierr);
359   }
360 
361   PetscFunctionReturn(0);
362 }
363 
364 PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
365                                      AppCtx app_ctx) {
366   MPI_Comm       comm = PETSC_COMM_WORLD;
367   PetscErrorCode ierr;
368   PetscFunctionBeginUser;
369 
370   ierr = PetscPrintf(comm,
371                      "  Problem:\n"
372                      "    Problem Name                       : %s\n"
373                      "    Stabilization                      : %s\n",
374                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
375   CHKERRQ(ierr);
376 
377   PetscFunctionReturn(0);
378 }
379