xref: /libCEED/examples/fluids/problems/densitycurrent.c (revision f5066b3615781dbcd74af2f846f96d7648d0187d)
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->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   CeedScalar c_tau  = 0.5;     // -
73   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
74   CeedScalar rc     = 1000.;   // m (Radius of bubble)
75   PetscReal center[3], dc_axis[3] = {0, 0, 0};
76   PetscReal domain_min[3], domain_max[3], domain_size[3];
77   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
78   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
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("-rc", "Characteristic radius of thermal bubble",
117                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
118   for (int i=0; i<3; i++) center[i] = .5*domain_size[i];
119   PetscInt n = problem->dim;
120   ierr = PetscOptionsRealArray("-center", "Location of bubble center",
121                                NULL, center, &n, NULL); CHKERRQ(ierr);
122   n = problem->dim;
123   ierr = PetscOptionsRealArray("-dc_axis",
124                                "Axis of density current cylindrical anomaly, or {0,0,0} for spherically symmetric",
125                                NULL, dc_axis, &n, NULL); CHKERRQ(ierr);
126   {
127     PetscReal norm = PetscSqrtReal(PetscSqr(dc_axis[0]) + PetscSqr(dc_axis[1]) +
128                                    PetscSqr(dc_axis[2]));
129     if (norm > 0) {
130       for (int i=0; i<3; i++)  dc_axis[i] /= norm;
131     }
132   }
133   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
134                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
135                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
136   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
137                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
138   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
139                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
140   CHKERRQ(ierr);
141 
142   // -- Units
143   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
144                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
145   meter = fabs(meter);
146   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
147                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
148   kilogram = fabs(kilogram);
149   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
150                             NULL, second, &second, NULL); CHKERRQ(ierr);
151   second = fabs(second);
152   ierr = PetscOptionsScalar("-units_Kelvin",
153                             "1 Kelvin in scaled temperature units",
154                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
155   Kelvin = fabs(Kelvin);
156 
157   // -- Warnings
158   if (stab == STAB_SUPG && !implicit) {
159     ierr = PetscPrintf(comm,
160                        "Warning! Use -stab supg only with -implicit\n");
161     CHKERRQ(ierr);
162   }
163 
164   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
165 
166   // ------------------------------------------------------
167   //           Set up the PETSc context
168   // ------------------------------------------------------
169   // -- Define derived units
170   Pascal          = kilogram / (meter * PetscSqr(second));
171   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
172   m_per_squared_s = meter / PetscSqr(second);
173   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
174 
175   user->units->meter           = meter;
176   user->units->kilogram        = kilogram;
177   user->units->second          = second;
178   user->units->Kelvin          = Kelvin;
179   user->units->Pascal          = Pascal;
180   user->units->J_per_kg_K      = J_per_kg_K;
181   user->units->m_per_squared_s = m_per_squared_s;
182   user->units->W_per_m_K       = W_per_m_K;
183 
184   // ------------------------------------------------------
185   //           Set up the libCEED context
186   // ------------------------------------------------------
187   // -- Scale variables to desired units
188   theta0 *= Kelvin;
189   thetaC *= Kelvin;
190   P0     *= Pascal;
191   N      *= (1./second);
192   cv     *= J_per_kg_K;
193   cp     *= J_per_kg_K;
194   g      *= m_per_squared_s;
195   mu     *= Pascal * second;
196   k      *= W_per_m_K;
197   rc     = fabs(rc) * meter;
198   for (int i=0; i<3; i++) domain_size[i] *= meter;
199   for (int i=0; i<3; i++) center[i] *= meter;
200   problem->dm_scale = meter;
201 
202   // -- Setup Context
203   setup_context->theta0     = theta0;
204   setup_context->thetaC     = thetaC;
205   setup_context->P0         = P0;
206   setup_context->N          = N;
207   setup_context->cv         = cv;
208   setup_context->cp         = cp;
209   setup_context->g          = g;
210   setup_context->rc         = rc;
211   setup_context->lx         = domain_size[0];
212   setup_context->ly         = domain_size[1];
213   setup_context->lz         = domain_size[2];
214   setup_context->center[0]  = center[0];
215   setup_context->center[1]  = center[1];
216   setup_context->center[2]  = center[2];
217   setup_context->dc_axis[0] = dc_axis[0];
218   setup_context->dc_axis[1] = dc_axis[1];
219   setup_context->dc_axis[2] = dc_axis[2];
220   setup_context->time       = 0;
221 
222   // -- QFunction Context
223   user->phys->stab             = stab;
224   user->phys->implicit         = implicit;
225   user->phys->has_curr_time    = has_curr_time;
226   user->phys->dc_ctx->lambda   = lambda;
227   user->phys->dc_ctx->mu       = mu;
228   user->phys->dc_ctx->k        = k;
229   user->phys->dc_ctx->cv       = cv;
230   user->phys->dc_ctx->cp       = cp;
231   user->phys->dc_ctx->g        = g;
232   user->phys->dc_ctx->c_tau    = c_tau;
233   user->phys->dc_ctx->stabilization = stab;
234 
235   PetscFunctionReturn(0);
236 }
237 
238 PetscErrorCode SetupContext_DENSITY_CURRENT(Ceed ceed, CeedData ceed_data,
239     AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
240   PetscFunctionBeginUser;
241   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
242   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
243                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
244   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
245   CeedQFunctionContextCreate(ceed, &ceed_data->dc_context);
246   CeedQFunctionContextSetData(ceed_data->dc_context, CEED_MEM_HOST,
247                               CEED_USE_POINTER,
248                               sizeof(*phys->dc_ctx), phys->dc_ctx);
249   if (ceed_data->qf_rhs_vol)
250     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->dc_context);
251   if (ceed_data->qf_ifunction_vol)
252     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->dc_context);
253   PetscFunctionReturn(0);
254 }
255 
256 PetscErrorCode PRINT_DENSITY_CURRENT(Physics phys, SetupContext setup_ctx,
257                                      AppCtx app_ctx) {
258   MPI_Comm       comm = PETSC_COMM_WORLD;
259   PetscErrorCode ierr;
260   PetscFunctionBeginUser;
261 
262   ierr = PetscPrintf(comm,
263                      "  Problem:\n"
264                      "    Problem Name                       : %s\n"
265                      "    Stabilization                      : %s\n",
266                      app_ctx->problem_name, StabilizationTypes[phys->stab]);
267   CHKERRQ(ierr);
268 
269   PetscFunctionReturn(0);
270 }
271