xref: /libCEED/examples/fluids/problems/shocktube.c (revision 841e4c7362a2acf3a6f116f4961b1eb52fa410fc)
1019b7682STimothy Aiken // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2019b7682STimothy Aiken // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3019b7682STimothy Aiken // reserved. See files LICENSE and NOTICE for details.
4019b7682STimothy Aiken //
5019b7682STimothy Aiken // This file is part of CEED, a collection of benchmarks, miniapps, software
6019b7682STimothy Aiken // libraries and APIs for efficient high-order finite element and spectral
7019b7682STimothy Aiken // element discretizations for exascale applications. For more information and
8019b7682STimothy Aiken // source code availability see http://github.com/ceed.
9019b7682STimothy Aiken //
10019b7682STimothy Aiken // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11019b7682STimothy Aiken // a collaborative effort of two U.S. Department of Energy organizations (Office
12019b7682STimothy Aiken // of Science and the National Nuclear Security Administration) responsible for
13019b7682STimothy Aiken // the planning and preparation of a capable exascale ecosystem, including
14019b7682STimothy Aiken // software, applications, hardware, advanced system engineering and early
15019b7682STimothy Aiken // testbed platforms, in support of the nation's exascale computing imperative.
16019b7682STimothy Aiken 
17019b7682STimothy Aiken /// @file
18019b7682STimothy Aiken /// Utility functions for setting up SHOCKTUBE
19019b7682STimothy Aiken 
20019b7682STimothy Aiken #include "../navierstokes.h"
21019b7682STimothy Aiken #include "../qfunctions/setupgeo.h"
22019b7682STimothy Aiken #include "../qfunctions/shocktube.h"
23019b7682STimothy Aiken 
24019b7682STimothy Aiken PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *setup_ctx,
25019b7682STimothy Aiken                             void *ctx) {
26019b7682STimothy Aiken   SetupContext      setup_context = *(SetupContext *)setup_ctx;
27019b7682STimothy Aiken   User              user = *(User *)ctx;
28019b7682STimothy Aiken   MPI_Comm          comm = PETSC_COMM_WORLD;
29019b7682STimothy Aiken   PetscBool         implicit;
30019b7682STimothy Aiken   PetscBool         yzb;
31019b7682STimothy Aiken   PetscInt          stab;
32019b7682STimothy Aiken   PetscBool         has_curr_time = PETSC_FALSE;
33019b7682STimothy Aiken   PetscInt          ierr;
34*841e4c73SJed Brown   ShockTubeContext  shocktube_ctx;
35*841e4c73SJed Brown   CeedQFunctionContext shocktube_context;
36019b7682STimothy Aiken 
37*841e4c73SJed Brown 
38*841e4c73SJed Brown   PetscFunctionBeginUser;
39*841e4c73SJed Brown   ierr = PetscCalloc1(1, &shocktube_ctx); CHKERRQ(ierr);
40019b7682STimothy Aiken 
41019b7682STimothy Aiken   // ------------------------------------------------------
42019b7682STimothy Aiken   //               SET UP SHOCKTUBE
43019b7682STimothy Aiken   // ------------------------------------------------------
44019b7682STimothy Aiken   problem->dim                               = 3;
45019b7682STimothy Aiken   problem->q_data_size_vol                   = 10;
46019b7682STimothy Aiken   problem->q_data_size_sur                   = 4;
4791e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
4891e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
4991e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
5091e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
5191e5af17SJed Brown   problem->ics.qfunction                     = ICsShockTube;
5291e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsShockTube_loc;
5391e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = EulerShockTube;
5491e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = EulerShockTube_loc;
5591e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = NULL;
5691e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = NULL;
57019b7682STimothy Aiken   problem->bc                                = Exact_ShockTube;
58019b7682STimothy Aiken   problem->non_zero_time                     = PETSC_FALSE;
59019b7682STimothy Aiken   problem->print_info                        = PRINT_SHOCKTUBE;
60019b7682STimothy Aiken 
61019b7682STimothy Aiken   // ------------------------------------------------------
62019b7682STimothy Aiken   //             Create the libCEED context
63019b7682STimothy Aiken   // ------------------------------------------------------
64019b7682STimothy Aiken   // Driver section initial conditions
65019b7682STimothy Aiken   CeedScalar P_high          = 1.0;     // Pa
66019b7682STimothy Aiken   CeedScalar rho_high        = 1.0;     // kg/m^3
67019b7682STimothy Aiken   // Driven section initial conditions
68019b7682STimothy Aiken   CeedScalar P_low           = 0.1;     // Pa
69019b7682STimothy Aiken   CeedScalar rho_low         = 0.125;   // kg/m^3
70019b7682STimothy Aiken   // Stabilization parameter
71019b7682STimothy Aiken   CeedScalar c_tau           = 0.5;     // -, based on Hughes et al (2010)
72019b7682STimothy Aiken   // Tuning parameters for the YZB shock capturing
73019b7682STimothy Aiken   CeedScalar Cyzb            = 0.1;     // -, used in approximation of (Na),x
74019b7682STimothy Aiken   CeedScalar Byzb            = 2.0;     // -, 1 for smooth shocks
75019b7682STimothy Aiken   //                                          2 for sharp shocks
76019b7682STimothy Aiken   PetscReal domain_min[3], domain_max[3], domain_size[3];
77019b7682STimothy Aiken   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
78019b7682STimothy Aiken   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
79019b7682STimothy Aiken 
80019b7682STimothy Aiken   // ------------------------------------------------------
81019b7682STimothy Aiken   //             Create the PETSc context
82019b7682STimothy Aiken   // ------------------------------------------------------
83019b7682STimothy Aiken   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
84019b7682STimothy Aiken   PetscScalar second   = 1e-2; // 1 second in scaled time units
85019b7682STimothy Aiken 
86019b7682STimothy Aiken   // ------------------------------------------------------
87019b7682STimothy Aiken   //              Command line Options
88019b7682STimothy Aiken   // ------------------------------------------------------
89019b7682STimothy Aiken   PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL);
90019b7682STimothy Aiken 
91019b7682STimothy Aiken   // -- Numerical formulation options
92019b7682STimothy Aiken   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
93019b7682STimothy Aiken                           NULL, implicit=PETSC_FALSE, &implicit, NULL); CHKERRQ(ierr);
94019b7682STimothy Aiken   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
95019b7682STimothy Aiken                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
96019b7682STimothy Aiken                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
97019b7682STimothy Aiken   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
98019b7682STimothy Aiken                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
99019b7682STimothy Aiken   ierr = PetscOptionsBool("-yzb", "Use YZB discontinuity capturing",
100019b7682STimothy Aiken                           NULL, yzb=PETSC_FALSE, &yzb, NULL); CHKERRQ(ierr);
101019b7682STimothy Aiken 
102019b7682STimothy Aiken   // -- Units
103019b7682STimothy Aiken   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
104019b7682STimothy Aiken                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
105019b7682STimothy Aiken   meter = fabs(meter);
106019b7682STimothy Aiken   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
107019b7682STimothy Aiken                             NULL, second, &second, NULL); CHKERRQ(ierr);
108019b7682STimothy Aiken   second = fabs(second);
109019b7682STimothy Aiken 
110019b7682STimothy Aiken   // -- Warnings
111019b7682STimothy Aiken   if (stab == STAB_SUPG) {
112019b7682STimothy Aiken     ierr = PetscPrintf(comm,
113019b7682STimothy Aiken                        "Warning! -stab supg not implemented for the shocktube problem. \n");
114019b7682STimothy Aiken     CHKERRQ(ierr);
115019b7682STimothy Aiken   }
116019b7682STimothy Aiken   if (yzb && implicit) {
117019b7682STimothy Aiken     ierr = PetscPrintf(comm,
118019b7682STimothy Aiken                        "Warning! -yzb only implemented for explicit timestepping. \n");
119019b7682STimothy Aiken     CHKERRQ(ierr);
120019b7682STimothy Aiken   }
121019b7682STimothy Aiken 
122019b7682STimothy Aiken 
123019b7682STimothy Aiken   PetscOptionsEnd();
124019b7682STimothy Aiken 
125019b7682STimothy Aiken   // ------------------------------------------------------
126019b7682STimothy Aiken   //           Set up the PETSc context
127019b7682STimothy Aiken   // ------------------------------------------------------
128019b7682STimothy Aiken   user->units->meter  = meter;
129019b7682STimothy Aiken   user->units->second = second;
130019b7682STimothy Aiken 
131019b7682STimothy Aiken   // ------------------------------------------------------
132019b7682STimothy Aiken   //           Set up the libCEED context
133019b7682STimothy Aiken   // ------------------------------------------------------
134019b7682STimothy Aiken   // -- Scale variables to desired units
135019b7682STimothy Aiken   for (int i=0; i<3; i++) {
136019b7682STimothy Aiken     domain_size[i] *= meter;
137019b7682STimothy Aiken     domain_min[i] *= meter;
138019b7682STimothy Aiken   }
139019b7682STimothy Aiken   problem->dm_scale = meter;
140019b7682STimothy Aiken   CeedScalar mid_point = 0.5*(domain_size[0]+domain_min[0]);
141019b7682STimothy Aiken 
142019b7682STimothy Aiken   // -- Setup Context
143019b7682STimothy Aiken   setup_context->lx        = domain_size[0];
144019b7682STimothy Aiken   setup_context->ly        = domain_size[1];
145019b7682STimothy Aiken   setup_context->lz        = domain_size[2];
146019b7682STimothy Aiken   setup_context->mid_point = mid_point;
147019b7682STimothy Aiken   setup_context->time      = 0.0;
148019b7682STimothy Aiken   setup_context->P_high    = P_high;
149019b7682STimothy Aiken   setup_context->rho_high  = rho_high;
150019b7682STimothy Aiken   setup_context->P_low     = P_low;
151019b7682STimothy Aiken   setup_context->rho_low   = rho_low;
152019b7682STimothy Aiken 
153019b7682STimothy Aiken   // -- QFunction Context
154019b7682STimothy Aiken   user->phys->implicit                      = implicit;
155019b7682STimothy Aiken   user->phys->has_curr_time                 = has_curr_time;
156*841e4c73SJed Brown   shocktube_ctx->implicit       = implicit;
157*841e4c73SJed Brown   shocktube_ctx->stabilization  = stab;
158*841e4c73SJed Brown   shocktube_ctx->yzb            = yzb;
159*841e4c73SJed Brown   shocktube_ctx->Cyzb           = Cyzb;
160*841e4c73SJed Brown   shocktube_ctx->Byzb           = Byzb;
161*841e4c73SJed Brown   shocktube_ctx->c_tau          = c_tau;
162019b7682STimothy Aiken 
163*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
164*841e4c73SJed Brown   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST,
165*841e4c73SJed Brown                               CEED_USE_POINTER, sizeof(*setup_context), setup_context);
166019b7682STimothy Aiken 
167*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &shocktube_context);
168*841e4c73SJed Brown   CeedQFunctionContextSetData(shocktube_context, CEED_MEM_HOST,
169019b7682STimothy Aiken                               CEED_USE_POINTER,
170*841e4c73SJed Brown                               sizeof(*shocktube_ctx), shocktube_ctx);
171*841e4c73SJed Brown   CeedQFunctionContextSetDataDestroy(shocktube_context, CEED_MEM_HOST,
172*841e4c73SJed Brown                                      FreeContextPetsc);
173*841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = shocktube_context;
174019b7682STimothy Aiken   PetscFunctionReturn(0);
175019b7682STimothy Aiken }
176019b7682STimothy Aiken 
177*841e4c73SJed Brown PetscErrorCode PRINT_SHOCKTUBE(ProblemData *problem, SetupContext setup_ctx,
178019b7682STimothy Aiken                                AppCtx app_ctx) {
179019b7682STimothy Aiken   MPI_Comm       comm = PETSC_COMM_WORLD;
180019b7682STimothy Aiken   PetscErrorCode ierr;
181019b7682STimothy Aiken   PetscFunctionBeginUser;
182019b7682STimothy Aiken 
183019b7682STimothy Aiken   ierr = PetscPrintf(comm,
184019b7682STimothy Aiken                      "  Problem:\n"
185019b7682STimothy Aiken                      "    Problem Name                       : %s\n",
186019b7682STimothy Aiken                      app_ctx->problem_name); CHKERRQ(ierr);
187019b7682STimothy Aiken 
188019b7682STimothy Aiken   PetscFunctionReturn(0);
189019b7682STimothy Aiken }
190