xref: /libCEED/examples/fluids/problems/shocktube.c (revision 019b76820d7ff306c177822c4e76ffe5939c204b)
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 SHOCKTUBE
19 
20 #include "../navierstokes.h"
21 #include "../qfunctions/setupgeo.h"
22 #include "../qfunctions/shocktube.h"
23 
24 PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *setup_ctx,
25                             void *ctx) {
26   SetupContext      setup_context = *(SetupContext *)setup_ctx;
27   User              user = *(User *)ctx;
28   MPI_Comm          comm = PETSC_COMM_WORLD;
29   PetscBool         implicit;
30   PetscBool         yzb;
31   PetscInt          stab;
32   PetscBool         has_curr_time = PETSC_FALSE;
33   PetscInt          ierr;
34   PetscFunctionBeginUser;
35 
36   ierr = PetscCalloc1(1, &user->phys->shocktube_ctx); CHKERRQ(ierr);
37 
38   // ------------------------------------------------------
39   //               SET UP SHOCKTUBE
40   // ------------------------------------------------------
41   problem->dim                     = 3;
42   problem->q_data_size_vol         = 10;
43   problem->q_data_size_sur         = 4;
44   problem->setup_vol               = Setup;
45   problem->setup_vol_loc           = Setup_loc;
46   problem->setup_sur               = SetupBoundary;
47   problem->setup_sur_loc           = SetupBoundary_loc;
48   problem->ics                     = ICsShockTube;
49   problem->ics_loc                 = ICsShockTube_loc;
50   problem->apply_vol_rhs           = EulerShockTube;
51   problem->apply_vol_rhs_loc       = EulerShockTube_loc;
52   problem->apply_vol_ifunction     = NULL;
53   problem->apply_vol_ifunction_loc = NULL;
54   problem->bc                      = Exact_ShockTube;
55   problem->setup_ctx               = SetupContext_SHOCKTUBE;
56   problem->non_zero_time           = PETSC_FALSE;
57   problem->print_info              = PRINT_SHOCKTUBE;
58 
59   // ------------------------------------------------------
60   //             Create the libCEED context
61   // ------------------------------------------------------
62   // Driver section initial conditions
63   CeedScalar P_high          = 1.0;     // Pa
64   CeedScalar rho_high        = 1.0;     // kg/m^3
65   // Driven section initial conditions
66   CeedScalar P_low           = 0.1;     // Pa
67   CeedScalar rho_low         = 0.125;   // kg/m^3
68   // Stabilization parameter
69   CeedScalar c_tau           = 0.5;     // -, based on Hughes et al (2010)
70   // Tuning parameters for the YZB shock capturing
71   CeedScalar Cyzb            = 0.1;     // -, used in approximation of (Na),x
72   CeedScalar Byzb            = 2.0;     // -, 1 for smooth shocks
73   //                                          2 for sharp shocks
74   PetscReal domain_min[3], domain_max[3], domain_size[3];
75   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
76   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
77 
78   // ------------------------------------------------------
79   //             Create the PETSc context
80   // ------------------------------------------------------
81   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
82   PetscScalar second   = 1e-2; // 1 second in scaled time units
83 
84   // ------------------------------------------------------
85   //              Command line Options
86   // ------------------------------------------------------
87   PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL);
88 
89   // -- Numerical formulation options
90   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
91                           NULL, implicit=PETSC_FALSE, &implicit, NULL); CHKERRQ(ierr);
92   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
93                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
94                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
95   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
96                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
97   ierr = PetscOptionsBool("-yzb", "Use YZB discontinuity capturing",
98                           NULL, yzb=PETSC_FALSE, &yzb, NULL); CHKERRQ(ierr);
99 
100   // -- Units
101   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
102                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
103   meter = fabs(meter);
104   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
105                             NULL, second, &second, NULL); CHKERRQ(ierr);
106   second = fabs(second);
107 
108   // -- Warnings
109   if (stab == STAB_SUPG) {
110     ierr = PetscPrintf(comm,
111                        "Warning! -stab supg not implemented for the shocktube problem. \n");
112     CHKERRQ(ierr);
113   }
114   if (yzb && implicit) {
115     ierr = PetscPrintf(comm,
116                        "Warning! -yzb only implemented for explicit timestepping. \n");
117     CHKERRQ(ierr);
118   }
119 
120 
121   PetscOptionsEnd();
122 
123   // ------------------------------------------------------
124   //           Set up the PETSc context
125   // ------------------------------------------------------
126   user->units->meter  = meter;
127   user->units->second = second;
128 
129   // ------------------------------------------------------
130   //           Set up the libCEED context
131   // ------------------------------------------------------
132   // -- Scale variables to desired units
133   for (int i=0; i<3; i++) {
134     domain_size[i] *= meter;
135     domain_min[i] *= meter;
136   }
137   problem->dm_scale = meter;
138   CeedScalar mid_point = 0.5*(domain_size[0]+domain_min[0]);
139 
140   // -- Setup Context
141   setup_context->lx        = domain_size[0];
142   setup_context->ly        = domain_size[1];
143   setup_context->lz        = domain_size[2];
144   setup_context->mid_point = mid_point;
145   setup_context->time      = 0.0;
146   setup_context->P_high    = P_high;
147   setup_context->rho_high  = rho_high;
148   setup_context->P_low     = P_low;
149   setup_context->rho_low   = rho_low;
150 
151   // -- QFunction Context
152   user->phys->implicit                      = implicit;
153   user->phys->has_curr_time                 = has_curr_time;
154   user->phys->shocktube_ctx->implicit       = implicit;
155   user->phys->shocktube_ctx->stabilization  = stab;
156   user->phys->shocktube_ctx->yzb            = yzb;
157   user->phys->shocktube_ctx->Cyzb           = Cyzb;
158   user->phys->shocktube_ctx->Byzb           = Byzb;
159   user->phys->shocktube_ctx->c_tau          = c_tau;
160 
161   PetscFunctionReturn(0);
162 }
163 
164 PetscErrorCode SetupContext_SHOCKTUBE(Ceed ceed, CeedData ceed_data,
165                                       AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
166   PetscFunctionBeginUser;
167 
168   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
169   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
170                               CEED_USE_POINTER, sizeof(*setup_ctx), setup_ctx);
171   CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->setup_context);
172   CeedQFunctionContextCreate(ceed, &ceed_data->shocktube_context);
173   CeedQFunctionContextSetData(ceed_data->shocktube_context, CEED_MEM_HOST,
174                               CEED_USE_POINTER,
175                               sizeof(*phys->shocktube_ctx), phys->shocktube_ctx);
176   if (ceed_data->qf_rhs_vol)
177     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->shocktube_context);
178   if (ceed_data->qf_ifunction_vol)
179     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
180                             ceed_data->shocktube_context);
181 
182   PetscFunctionReturn(0);
183 }
184 
185 PetscErrorCode PRINT_SHOCKTUBE(Physics phys, SetupContext setup_ctx,
186                                AppCtx app_ctx) {
187   MPI_Comm       comm = PETSC_COMM_WORLD;
188   PetscErrorCode ierr;
189   PetscFunctionBeginUser;
190 
191   ierr = PetscPrintf(comm,
192                      "  Problem:\n"
193                      "    Problem Name                       : %s\n",
194                      app_ctx->problem_name); CHKERRQ(ierr);
195 
196   PetscFunctionReturn(0);
197 }
198