xref: /honee/problems/densitycurrent.c (revision ba5420e56d94c233eb792ade1a6474abb68728b5)
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   CeedScalar Rd;
79 
80   // ------------------------------------------------------
81   //             Create the PETSc context
82   // ------------------------------------------------------
83   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
84   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
85   PetscScalar second   = 1e-2;  // 1 second in scaled time units
86   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
87   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
88 
89   // ------------------------------------------------------
90   //              Command line Options
91   // ------------------------------------------------------
92   ierr = PetscOptionsBegin(comm, NULL, "Options for DENSITY_CURRENT problem",
93                            NULL); CHKERRQ(ierr);
94   // -- Physics
95   ierr = PetscOptionsScalar("-theta0", "Reference potential temperature",
96                             NULL, theta0, &theta0, NULL); CHKERRQ(ierr);
97   ierr = PetscOptionsScalar("-thetaC", "Perturbation of potential temperature",
98                             NULL, thetaC, &thetaC, NULL); CHKERRQ(ierr);
99   ierr = PetscOptionsScalar("-P0", "Atmospheric pressure",
100                             NULL, P0, &P0, NULL); CHKERRQ(ierr);
101   ierr = PetscOptionsScalar("-N", "Brunt-Vaisala frequency",
102                             NULL, N, &N, NULL); CHKERRQ(ierr);
103   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
104                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
105   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
106                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
107   ierr = PetscOptionsScalar("-g", "Gravitational acceleration",
108                             NULL, g, &g, NULL); CHKERRQ(ierr);
109   ierr = PetscOptionsScalar("-lambda",
110                             "Stokes hypothesis second viscosity coefficient",
111                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
112   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
113                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
114   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
115                             NULL, k, &k, NULL); CHKERRQ(ierr);
116   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
117                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
118   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
119                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
120   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
121                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
122   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
123                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
124   PetscInt n = problem->dim;
125   center[0] = 0.5 * lx;
126   center[1] = 0.5 * ly;
127   center[2] = 0.5 * lz;
128   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
129                                NULL, center, &n, NULL); CHKERRQ(ierr);
130   n = problem->dim;
131   ierr = PetscOptionsRealArray("-dc_axis",
132                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
133                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
134   {
135     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
136                                    PetscSqr(dc_axis[2]));
137     if (norm > 0) {
138       for (int i=0; i<3; i++)  dc_axis[i] /= norm;
139     }
140   }
141   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
142                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
143                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
144 
145   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
146                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
147   CHKERRQ(ierr);
148 
149   // -- Units
150   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
151                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
152   meter = fabs(meter);
153   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
154                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
155   kilogram = fabs(kilogram);
156   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
157                             NULL, second, &second, NULL); CHKERRQ(ierr);
158   second = fabs(second);
159   ierr = PetscOptionsScalar("-units_Kelvin",
160                             "1 Kelvin in scaled temperature units",
161                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
162   Kelvin = fabs(Kelvin);
163 
164   // -- Warnings
165   if (stab == STAB_SUPG && !implicit) {
166     ierr = PetscPrintf(comm,
167                        "Warning! Use -stab supg only with -implicit\n");
168     CHKERRQ(ierr);
169   }
170 
171   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
172 
173   // ------------------------------------------------------
174   //           Set up the PETSc context
175   // ------------------------------------------------------
176   // -- Define derived units
177   Pascal          = kilogram / (meter * PetscSqr(second));
178   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
179   m_per_squared_s = meter / PetscSqr(second);
180   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
181 
182   user->units->meter           = meter;
183   user->units->kilogram        = kilogram;
184   user->units->second          = second;
185   user->units->Kelvin          = Kelvin;
186   user->units->Pascal          = Pascal;
187   user->units->J_per_kg_K      = J_per_kg_K;
188   user->units->m_per_squared_s = m_per_squared_s;
189   user->units->W_per_m_K       = W_per_m_K;
190 
191   // ------------------------------------------------------
192   //           Set up the libCEED context
193   // ------------------------------------------------------
194   // -- Scale variables to desired units
195   theta0 *= Kelvin;
196   thetaC *= Kelvin;
197   P0     *= Pascal;
198   N      *= (1./second);
199   cv     *= J_per_kg_K;
200   cp     *= J_per_kg_K;
201   Rd     = cp - cv;
202   g      *= m_per_squared_s;
203   mu     *= Pascal * second;
204   k      *= W_per_m_K;
205   lx     = fabs(lx) * meter;
206   ly     = fabs(ly) * meter;
207   lz     = fabs(lz) * meter;
208   rc     = fabs(rc) * meter;
209   for (int i=0; i<3; i++) center[i] *= meter;
210 
211   // -- Setup Context
212   setup_context->theta0     = theta0;
213   setup_context->thetaC     = thetaC;
214   setup_context->P0         = P0;
215   setup_context->N          = N;
216   setup_context->cv         = cv;
217   setup_context->cp         = cp;
218   setup_context->Rd         = Rd;
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->Rd       = Rd;
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 == PETSC_TRUE) {
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