xref: /libCEED/examples/fluids/problems/shocktube.c (revision 019b76820d7ff306c177822c4e76ffe5939c204b)
1*019b7682STimothy Aiken // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2*019b7682STimothy Aiken // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3*019b7682STimothy Aiken // reserved. See files LICENSE and NOTICE for details.
4*019b7682STimothy Aiken //
5*019b7682STimothy Aiken // This file is part of CEED, a collection of benchmarks, miniapps, software
6*019b7682STimothy Aiken // libraries and APIs for efficient high-order finite element and spectral
7*019b7682STimothy Aiken // element discretizations for exascale applications. For more information and
8*019b7682STimothy Aiken // source code availability see http://github.com/ceed.
9*019b7682STimothy Aiken //
10*019b7682STimothy Aiken // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*019b7682STimothy Aiken // a collaborative effort of two U.S. Department of Energy organizations (Office
12*019b7682STimothy Aiken // of Science and the National Nuclear Security Administration) responsible for
13*019b7682STimothy Aiken // the planning and preparation of a capable exascale ecosystem, including
14*019b7682STimothy Aiken // software, applications, hardware, advanced system engineering and early
15*019b7682STimothy Aiken // testbed platforms, in support of the nation's exascale computing imperative.
16*019b7682STimothy Aiken 
17*019b7682STimothy Aiken /// @file
18*019b7682STimothy Aiken /// Utility functions for setting up SHOCKTUBE
19*019b7682STimothy Aiken 
20*019b7682STimothy Aiken #include "../navierstokes.h"
21*019b7682STimothy Aiken #include "../qfunctions/setupgeo.h"
22*019b7682STimothy Aiken #include "../qfunctions/shocktube.h"
23*019b7682STimothy Aiken 
24*019b7682STimothy Aiken PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *setup_ctx,
25*019b7682STimothy Aiken                             void *ctx) {
26*019b7682STimothy Aiken   SetupContext      setup_context = *(SetupContext *)setup_ctx;
27*019b7682STimothy Aiken   User              user = *(User *)ctx;
28*019b7682STimothy Aiken   MPI_Comm          comm = PETSC_COMM_WORLD;
29*019b7682STimothy Aiken   PetscBool         implicit;
30*019b7682STimothy Aiken   PetscBool         yzb;
31*019b7682STimothy Aiken   PetscInt          stab;
32*019b7682STimothy Aiken   PetscBool         has_curr_time = PETSC_FALSE;
33*019b7682STimothy Aiken   PetscInt          ierr;
34*019b7682STimothy Aiken   PetscFunctionBeginUser;
35*019b7682STimothy Aiken 
36*019b7682STimothy Aiken   ierr = PetscCalloc1(1, &user->phys->shocktube_ctx); CHKERRQ(ierr);
37*019b7682STimothy Aiken 
38*019b7682STimothy Aiken   // ------------------------------------------------------
39*019b7682STimothy Aiken   //               SET UP SHOCKTUBE
40*019b7682STimothy Aiken   // ------------------------------------------------------
41*019b7682STimothy Aiken   problem->dim                     = 3;
42*019b7682STimothy Aiken   problem->q_data_size_vol         = 10;
43*019b7682STimothy Aiken   problem->q_data_size_sur         = 4;
44*019b7682STimothy Aiken   problem->setup_vol               = Setup;
45*019b7682STimothy Aiken   problem->setup_vol_loc           = Setup_loc;
46*019b7682STimothy Aiken   problem->setup_sur               = SetupBoundary;
47*019b7682STimothy Aiken   problem->setup_sur_loc           = SetupBoundary_loc;
48*019b7682STimothy Aiken   problem->ics                     = ICsShockTube;
49*019b7682STimothy Aiken   problem->ics_loc                 = ICsShockTube_loc;
50*019b7682STimothy Aiken   problem->apply_vol_rhs           = EulerShockTube;
51*019b7682STimothy Aiken   problem->apply_vol_rhs_loc       = EulerShockTube_loc;
52*019b7682STimothy Aiken   problem->apply_vol_ifunction     = NULL;
53*019b7682STimothy Aiken   problem->apply_vol_ifunction_loc = NULL;
54*019b7682STimothy Aiken   problem->bc                      = Exact_ShockTube;
55*019b7682STimothy Aiken   problem->setup_ctx               = SetupContext_SHOCKTUBE;
56*019b7682STimothy Aiken   problem->non_zero_time           = PETSC_FALSE;
57*019b7682STimothy Aiken   problem->print_info              = PRINT_SHOCKTUBE;
58*019b7682STimothy Aiken 
59*019b7682STimothy Aiken   // ------------------------------------------------------
60*019b7682STimothy Aiken   //             Create the libCEED context
61*019b7682STimothy Aiken   // ------------------------------------------------------
62*019b7682STimothy Aiken   // Driver section initial conditions
63*019b7682STimothy Aiken   CeedScalar P_high          = 1.0;     // Pa
64*019b7682STimothy Aiken   CeedScalar rho_high        = 1.0;     // kg/m^3
65*019b7682STimothy Aiken   // Driven section initial conditions
66*019b7682STimothy Aiken   CeedScalar P_low           = 0.1;     // Pa
67*019b7682STimothy Aiken   CeedScalar rho_low         = 0.125;   // kg/m^3
68*019b7682STimothy Aiken   // Stabilization parameter
69*019b7682STimothy Aiken   CeedScalar c_tau           = 0.5;     // -, based on Hughes et al (2010)
70*019b7682STimothy Aiken   // Tuning parameters for the YZB shock capturing
71*019b7682STimothy Aiken   CeedScalar Cyzb            = 0.1;     // -, used in approximation of (Na),x
72*019b7682STimothy Aiken   CeedScalar Byzb            = 2.0;     // -, 1 for smooth shocks
73*019b7682STimothy Aiken   //                                          2 for sharp shocks
74*019b7682STimothy Aiken   PetscReal domain_min[3], domain_max[3], domain_size[3];
75*019b7682STimothy Aiken   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
76*019b7682STimothy Aiken   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
77*019b7682STimothy Aiken 
78*019b7682STimothy Aiken   // ------------------------------------------------------
79*019b7682STimothy Aiken   //             Create the PETSc context
80*019b7682STimothy Aiken   // ------------------------------------------------------
81*019b7682STimothy Aiken   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
82*019b7682STimothy Aiken   PetscScalar second   = 1e-2; // 1 second in scaled time units
83*019b7682STimothy Aiken 
84*019b7682STimothy Aiken   // ------------------------------------------------------
85*019b7682STimothy Aiken   //              Command line Options
86*019b7682STimothy Aiken   // ------------------------------------------------------
87*019b7682STimothy Aiken   PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL);
88*019b7682STimothy Aiken 
89*019b7682STimothy Aiken   // -- Numerical formulation options
90*019b7682STimothy Aiken   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
91*019b7682STimothy Aiken                           NULL, implicit=PETSC_FALSE, &implicit, NULL); CHKERRQ(ierr);
92*019b7682STimothy Aiken   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
93*019b7682STimothy Aiken                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
94*019b7682STimothy Aiken                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
95*019b7682STimothy Aiken   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
96*019b7682STimothy Aiken                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
97*019b7682STimothy Aiken   ierr = PetscOptionsBool("-yzb", "Use YZB discontinuity capturing",
98*019b7682STimothy Aiken                           NULL, yzb=PETSC_FALSE, &yzb, NULL); CHKERRQ(ierr);
99*019b7682STimothy Aiken 
100*019b7682STimothy Aiken   // -- Units
101*019b7682STimothy Aiken   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
102*019b7682STimothy Aiken                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
103*019b7682STimothy Aiken   meter = fabs(meter);
104*019b7682STimothy Aiken   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
105*019b7682STimothy Aiken                             NULL, second, &second, NULL); CHKERRQ(ierr);
106*019b7682STimothy Aiken   second = fabs(second);
107*019b7682STimothy Aiken 
108*019b7682STimothy Aiken   // -- Warnings
109*019b7682STimothy Aiken   if (stab == STAB_SUPG) {
110*019b7682STimothy Aiken     ierr = PetscPrintf(comm,
111*019b7682STimothy Aiken                        "Warning! -stab supg not implemented for the shocktube problem. \n");
112*019b7682STimothy Aiken     CHKERRQ(ierr);
113*019b7682STimothy Aiken   }
114*019b7682STimothy Aiken   if (yzb && implicit) {
115*019b7682STimothy Aiken     ierr = PetscPrintf(comm,
116*019b7682STimothy Aiken                        "Warning! -yzb only implemented for explicit timestepping. \n");
117*019b7682STimothy Aiken     CHKERRQ(ierr);
118*019b7682STimothy Aiken   }
119*019b7682STimothy Aiken 
120*019b7682STimothy Aiken 
121*019b7682STimothy Aiken   PetscOptionsEnd();
122*019b7682STimothy Aiken 
123*019b7682STimothy Aiken   // ------------------------------------------------------
124*019b7682STimothy Aiken   //           Set up the PETSc context
125*019b7682STimothy Aiken   // ------------------------------------------------------
126*019b7682STimothy Aiken   user->units->meter  = meter;
127*019b7682STimothy Aiken   user->units->second = second;
128*019b7682STimothy Aiken 
129*019b7682STimothy Aiken   // ------------------------------------------------------
130*019b7682STimothy Aiken   //           Set up the libCEED context
131*019b7682STimothy Aiken   // ------------------------------------------------------
132*019b7682STimothy Aiken   // -- Scale variables to desired units
133*019b7682STimothy Aiken   for (int i=0; i<3; i++) {
134*019b7682STimothy Aiken     domain_size[i] *= meter;
135*019b7682STimothy Aiken     domain_min[i] *= meter;
136*019b7682STimothy Aiken   }
137*019b7682STimothy Aiken   problem->dm_scale = meter;
138*019b7682STimothy Aiken   CeedScalar mid_point = 0.5*(domain_size[0]+domain_min[0]);
139*019b7682STimothy Aiken 
140*019b7682STimothy Aiken   // -- Setup Context
141*019b7682STimothy Aiken   setup_context->lx        = domain_size[0];
142*019b7682STimothy Aiken   setup_context->ly        = domain_size[1];
143*019b7682STimothy Aiken   setup_context->lz        = domain_size[2];
144*019b7682STimothy Aiken   setup_context->mid_point = mid_point;
145*019b7682STimothy Aiken   setup_context->time      = 0.0;
146*019b7682STimothy Aiken   setup_context->P_high    = P_high;
147*019b7682STimothy Aiken   setup_context->rho_high  = rho_high;
148*019b7682STimothy Aiken   setup_context->P_low     = P_low;
149*019b7682STimothy Aiken   setup_context->rho_low   = rho_low;
150*019b7682STimothy Aiken 
151*019b7682STimothy Aiken   // -- QFunction Context
152*019b7682STimothy Aiken   user->phys->implicit                      = implicit;
153*019b7682STimothy Aiken   user->phys->has_curr_time                 = has_curr_time;
154*019b7682STimothy Aiken   user->phys->shocktube_ctx->implicit       = implicit;
155*019b7682STimothy Aiken   user->phys->shocktube_ctx->stabilization  = stab;
156*019b7682STimothy Aiken   user->phys->shocktube_ctx->yzb            = yzb;
157*019b7682STimothy Aiken   user->phys->shocktube_ctx->Cyzb           = Cyzb;
158*019b7682STimothy Aiken   user->phys->shocktube_ctx->Byzb           = Byzb;
159*019b7682STimothy Aiken   user->phys->shocktube_ctx->c_tau          = c_tau;
160*019b7682STimothy Aiken 
161*019b7682STimothy Aiken   PetscFunctionReturn(0);
162*019b7682STimothy Aiken }
163*019b7682STimothy Aiken 
164*019b7682STimothy Aiken PetscErrorCode SetupContext_SHOCKTUBE(Ceed ceed, CeedData ceed_data,
165*019b7682STimothy Aiken                                       AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
166*019b7682STimothy Aiken   PetscFunctionBeginUser;
167*019b7682STimothy Aiken 
168*019b7682STimothy Aiken   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
169*019b7682STimothy Aiken   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
170*019b7682STimothy Aiken                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
171*019b7682STimothy Aiken   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
172*019b7682STimothy Aiken   CeedQFunctionContextCreate(ceed, &ceed_data->shocktube_context);
173*019b7682STimothy Aiken   CeedQFunctionContextSetData(ceed_data->shocktube_context, CEED_MEM_HOST,
174*019b7682STimothy Aiken                               CEED_USE_POINTER,
175*019b7682STimothy Aiken                               sizeof(*phys->shocktube_ctx), phys->shocktube_ctx);
176*019b7682STimothy Aiken   if (ceed_data->qf_rhs_vol)
177*019b7682STimothy Aiken     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->shocktube_context);
178*019b7682STimothy Aiken   if (ceed_data->qf_ifunction_vol)
179*019b7682STimothy Aiken     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
180*019b7682STimothy Aiken                             ceed_data->shocktube_context);
181*019b7682STimothy Aiken 
182*019b7682STimothy Aiken   PetscFunctionReturn(0);
183*019b7682STimothy Aiken }
184*019b7682STimothy Aiken 
185*019b7682STimothy Aiken PetscErrorCode PRINT_SHOCKTUBE(Physics phys, SetupContext setup_ctx,
186*019b7682STimothy Aiken                                AppCtx app_ctx) {
187*019b7682STimothy Aiken   MPI_Comm       comm = PETSC_COMM_WORLD;
188*019b7682STimothy Aiken   PetscErrorCode ierr;
189*019b7682STimothy Aiken   PetscFunctionBeginUser;
190*019b7682STimothy Aiken 
191*019b7682STimothy Aiken   ierr = PetscPrintf(comm,
192*019b7682STimothy Aiken                      "  Problem:\n"
193*019b7682STimothy Aiken                      "    Problem Name                       : %s\n",
194*019b7682STimothy Aiken                      app_ctx->problem_name); CHKERRQ(ierr);
195*019b7682STimothy Aiken 
196*019b7682STimothy Aiken   PetscFunctionReturn(0);
197*019b7682STimothy Aiken }
198