xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision e6225c4737d2b5358b9c1c5b7c13d86915d842f6)
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   PetscScalar lx    = 8000.;   // m
74   PetscScalar ly    = 8000.;   // m
75   PetscScalar lz    = 4000.;   // m
76   CeedScalar rc     = 1000.;   // m (Radius of bubble)
77   PetscReal center[3], dc_axis[3] = {0, 0, 0};
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   g      *= m_per_squared_s;
201   mu     *= Pascal * second;
202   k      *= W_per_m_K;
203   lx     = fabs(lx) * meter;
204   ly     = fabs(ly) * meter;
205   lz     = fabs(lz) * meter;
206   rc     = fabs(rc) * meter;
207   for (int i=0; i<3; i++) center[i] *= meter;
208 
209   // -- Setup Context
210   setup_context->theta0     = theta0;
211   setup_context->thetaC     = thetaC;
212   setup_context->P0         = P0;
213   setup_context->N          = N;
214   setup_context->cv         = cv;
215   setup_context->cp         = cp;
216   setup_context->g          = g;
217   setup_context->rc         = rc;
218   setup_context->lx         = lx;
219   setup_context->ly         = ly;
220   setup_context->lz         = lz;
221   setup_context->center[0]  = center[0];
222   setup_context->center[1]  = center[1];
223   setup_context->center[2]  = center[2];
224   setup_context->dc_axis[0] = dc_axis[0];
225   setup_context->dc_axis[1] = dc_axis[1];
226   setup_context->dc_axis[2] = dc_axis[2];
227   setup_context->time       = 0;
228 
229   // -- QFunction Context
230   user->phys->stab             = stab;
231   user->phys->implicit         = implicit;
232   user->phys->has_curr_time    = has_curr_time;
233   user->phys->dc_ctx->lambda   = lambda;
234   user->phys->dc_ctx->mu       = mu;
235   user->phys->dc_ctx->k        = k;
236   user->phys->dc_ctx->cv       = cv;
237   user->phys->dc_ctx->cp       = cp;
238   user->phys->dc_ctx->g        = g;
239   user->phys->dc_ctx->stabilization = stab;
240 
241   PetscFunctionReturn(0);
242 }
243 
244 PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, CeedData ceed_data,
245     AppCtx app_ctx, SetupContext setup_ctx,
246     Physics phys) {
247   PetscFunctionBeginUser;
248 
249   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
250   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
251                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
252   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
253   CeedQFunctionContextCreate(ceed, &ceed_data->dc_context);
254   CeedQFunctionContextSetData(ceed_data->dc_context, CEED_MEM_HOST,
255                               CEED_USE_POINTER,
256                               sizeof(*phys->dc_ctx), phys->dc_ctx);
257   if (ceed_data->qf_rhs_vol)
258     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->dc_context);
259   if (ceed_data->qf_ifunction_vol)
260     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->dc_context);
261 
262   PetscFunctionReturn(0);
263 }
264 
265 PetscErrorCode BC_DENSITY_CURRENT(DM dm, SimpleBC bc, Physics phys,
266                                   void *setup_ctx) {
267 
268   PetscInt       len;
269   PetscBool      flg;
270   MPI_Comm       comm = PETSC_COMM_WORLD;
271   PetscErrorCode ierr;
272   PetscFunctionBeginUser;
273 
274   // Default boundary conditions
275   //   slip bc on all faces and no wall bc
276   bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 2;
277   bc->slips[0][0] = 5;
278   bc->slips[0][1] = 6;
279   bc->slips[1][0] = 3;
280   bc->slips[1][1] = 4;
281   bc->slips[2][0] = 1;
282   bc->slips[2][1] = 2;
283 
284   // Parse command line options
285   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT BCs ",
286                            NULL); CHKERRQ(ierr);
287   ierr = PetscOptionsIntArray("-bc_wall",
288                               "Use wall boundary conditions on this list of faces",
289                               NULL, bc->walls,
290                               (len = sizeof(bc->walls) / sizeof(bc->walls[0]),
291                                &len), &flg); CHKERRQ(ierr);
292   if (flg) {
293     bc->num_wall = len;
294     // Using a no-slip wall disables automatic slip walls (they must be set explicitly)
295     bc->num_slip[0] = bc->num_slip[1] = bc->num_slip[2] = 0;
296   }
297   for (PetscInt j=0; j<3; j++) {
298     const char *flags[3] = {"-bc_slip_x", "-bc_slip_y", "-bc_slip_z"};
299     ierr = PetscOptionsIntArray(flags[j],
300                                 "Use slip boundary conditions on this list of faces",
301                                 NULL, bc->slips[j],
302                                 (len = sizeof(bc->slips[j]) / sizeof(bc->slips[j][0]),
303                                  &len), &flg); CHKERRQ(ierr);
304     if (flg) {
305       bc->num_slip[j] = len;
306       bc->user_bc = PETSC_TRUE;
307     }
308   }
309   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
310 
311   {
312     // Set slip boundary conditions
313     DMLabel label;
314     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
315     PetscInt comps[1] = {1};
316     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, "Face Sets",
317                          bc->num_slip[0], bc->slips[0], 0, 1, comps,
318                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
319     CHKERRQ(ierr);
320     comps[0] = 2;
321     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, "Face Sets",
322                          bc->num_slip[1], bc->slips[1], 0, 1, comps,
323                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
324     CHKERRQ(ierr);
325     comps[0] = 3;
326     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
327                          bc->num_slip[2], bc->slips[2], 0, 1, comps,
328                          (void(*)(void))NULL, NULL, setup_ctx, NULL);
329     CHKERRQ(ierr);
330   }
331 
332   if (bc->user_bc) {
333     for (PetscInt c = 0; c < 3; c++) {
334       for (PetscInt s = 0; s < bc->num_slip[c]; s++) {
335         for (PetscInt w = 0; w < bc->num_wall; w++) {
336           if (bc->slips[c][s] == bc->walls[w])
337             SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,
338                      "Boundary condition already set on face %D!\n",
339                      bc->walls[w]);
340         }
341       }
342     }
343   }
344 
345   // Set wall boundary conditions
346   //   zero velocity and zero flux for mass density and energy density
347   {
348     DMLabel  label;
349     PetscInt comps[3] = {1, 2, 3};
350     ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
351     ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, "Face Sets",
352                          bc->num_wall, bc->walls, 0,
353                          3, comps, (void(*)(void))Exact_DC, NULL,
354                          setup_ctx, NULL); CHKERRQ(ierr);
355   }
356 
357   PetscFunctionReturn(0);
358 }
359 
360 PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
361                                      AppCtx app_ctx) {
362   MPI_Comm       comm = PETSC_COMM_WORLD;
363   PetscErrorCode ierr;
364   PetscFunctionBeginUser;
365 
366   ierr = PetscPrintf(comm,
367                      "  Problem:\n"
368                      "    Problem Name                       : %s\n"
369                      "    Stabilization                      : %s\n",
370                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
371   CHKERRQ(ierr);
372 
373   PetscFunctionReturn(0);
374 }
375