xref: /honee/problems/shocktube.c (revision d949ddfc92e3b3e8aad90700e3fb60ca7f4585db)
104e40bb6SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
204e40bb6SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3af8870a9STimothy Aiken //
404e40bb6SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5af8870a9STimothy Aiken //
604e40bb6SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7af8870a9STimothy Aiken 
8af8870a9STimothy Aiken /// @file
9af8870a9STimothy Aiken /// Utility functions for setting up SHOCKTUBE
10af8870a9STimothy Aiken 
11af8870a9STimothy Aiken #include "../qfunctions/shocktube.h"
12af8870a9STimothy Aiken 
13e419654dSJeremy L Thompson #include <ceed.h>
14e419654dSJeremy L Thompson #include <petscdm.h>
15e419654dSJeremy L Thompson 
162b916ea7SJeremy L Thompson #include "../navierstokes.h"
172b916ea7SJeremy L Thompson #include "../qfunctions/setupgeo.h"
186dd99bedSLeila Ghaffari 
19d1c51a42SJames Wright PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
203636f6a4SJames Wright   SetupContextShock    setup_context;
21af8870a9STimothy Aiken   User                 user = *(User *)ctx;
22af8870a9STimothy Aiken   MPI_Comm             comm = PETSC_COMM_WORLD;
23af8870a9STimothy Aiken   PetscBool            implicit;
24af8870a9STimothy Aiken   PetscBool            yzb;
25af8870a9STimothy Aiken   PetscInt             stab;
26af8870a9STimothy Aiken   PetscBool            has_curr_time = PETSC_FALSE;
2715a3537eSJed Brown   ShockTubeContext     shocktube_ctx;
2815a3537eSJed Brown   CeedQFunctionContext shocktube_context;
29af8870a9STimothy Aiken 
3015a3537eSJed Brown   PetscFunctionBeginUser;
312b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
322b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &shocktube_ctx));
33af8870a9STimothy Aiken 
34af8870a9STimothy Aiken   // ------------------------------------------------------
35af8870a9STimothy Aiken   //               SET UP SHOCKTUBE
36af8870a9STimothy Aiken   // ------------------------------------------------------
37af8870a9STimothy Aiken   problem->dim                               = 3;
38af8870a9STimothy Aiken   problem->q_data_size_vol                   = 10;
39af8870a9STimothy Aiken   problem->q_data_size_sur                   = 4;
409785fe93SJed Brown   problem->setup_vol.qfunction               = Setup;
419785fe93SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
429785fe93SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
439785fe93SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
449785fe93SJed Brown   problem->ics.qfunction                     = ICsShockTube;
459785fe93SJed Brown   problem->ics.qfunction_loc                 = ICsShockTube_loc;
469785fe93SJed Brown   problem->apply_vol_rhs.qfunction           = EulerShockTube;
479785fe93SJed Brown   problem->apply_vol_rhs.qfunction_loc       = EulerShockTube_loc;
489785fe93SJed Brown   problem->apply_vol_ifunction.qfunction     = NULL;
499785fe93SJed Brown   problem->apply_vol_ifunction.qfunction_loc = NULL;
50af8870a9STimothy Aiken   problem->bc                                = Exact_ShockTube;
51b7f03f12SJed Brown   problem->bc_ctx                            = setup_context;
52af8870a9STimothy Aiken   problem->non_zero_time                     = PETSC_FALSE;
53af8870a9STimothy Aiken   problem->print_info                        = PRINT_SHOCKTUBE;
54af8870a9STimothy Aiken 
55af8870a9STimothy Aiken   // ------------------------------------------------------
56af8870a9STimothy Aiken   //             Create the libCEED context
57af8870a9STimothy Aiken   // ------------------------------------------------------
58af8870a9STimothy Aiken   // Driver section initial conditions
59af8870a9STimothy Aiken   CeedScalar P_high   = 1.0;  // Pa
60af8870a9STimothy Aiken   CeedScalar rho_high = 1.0;  // kg/m^3
61af8870a9STimothy Aiken   // Driven section initial conditions
62af8870a9STimothy Aiken   CeedScalar P_low   = 0.1;    // Pa
63af8870a9STimothy Aiken   CeedScalar rho_low = 0.125;  // kg/m^3
64af8870a9STimothy Aiken   // Stabilization parameter
65af8870a9STimothy Aiken   CeedScalar c_tau = 0.5;  // -, based on Hughes et al (2010)
66af8870a9STimothy Aiken   // Tuning parameters for the YZB shock capturing
67af8870a9STimothy Aiken   CeedScalar Cyzb = 0.1;  // -, used in approximation of (Na),x
68af8870a9STimothy Aiken   CeedScalar Byzb = 2.0;  // -, 1 for smooth shocks
69af8870a9STimothy Aiken   //                                          2 for sharp shocks
70af8870a9STimothy Aiken   PetscReal domain_min[3], domain_max[3], domain_size[3];
712b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
72493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
73af8870a9STimothy Aiken 
74af8870a9STimothy Aiken   // ------------------------------------------------------
75af8870a9STimothy Aiken   //             Create the PETSc context
76af8870a9STimothy Aiken   // ------------------------------------------------------
77af8870a9STimothy Aiken   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
78af8870a9STimothy Aiken   PetscScalar second = 1e-2;  // 1 second in scaled time units
79af8870a9STimothy Aiken 
80af8870a9STimothy Aiken   // ------------------------------------------------------
81af8870a9STimothy Aiken   //              Command line Options
82af8870a9STimothy Aiken   // ------------------------------------------------------
83af8870a9STimothy Aiken   PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL);
84af8870a9STimothy Aiken 
85af8870a9STimothy Aiken   // -- Numerical formulation options
862b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
872b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
882b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
892b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-yzb", "Use YZB discontinuity capturing", NULL, yzb = PETSC_FALSE, &yzb, NULL));
90af8870a9STimothy Aiken 
91af8870a9STimothy Aiken   // -- Units
922b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
93af8870a9STimothy Aiken   meter = fabs(meter);
942b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
95af8870a9STimothy Aiken   second = fabs(second);
96af8870a9STimothy Aiken 
97af8870a9STimothy Aiken   // -- Warnings
98af8870a9STimothy Aiken   if (stab == STAB_SUPG) {
992b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! -stab supg not implemented for the shocktube problem. \n"));
100af8870a9STimothy Aiken   }
101af8870a9STimothy Aiken   if (yzb && implicit) {
1022b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! -yzb only implemented for explicit timestepping. \n"));
103af8870a9STimothy Aiken   }
104af8870a9STimothy Aiken 
105af8870a9STimothy Aiken   PetscOptionsEnd();
106af8870a9STimothy Aiken 
107af8870a9STimothy Aiken   // ------------------------------------------------------
108af8870a9STimothy Aiken   //           Set up the PETSc context
109af8870a9STimothy Aiken   // ------------------------------------------------------
110af8870a9STimothy Aiken   user->units->meter  = meter;
111af8870a9STimothy Aiken   user->units->second = second;
112af8870a9STimothy Aiken 
113af8870a9STimothy Aiken   // ------------------------------------------------------
114af8870a9STimothy Aiken   //           Set up the libCEED context
115af8870a9STimothy Aiken   // ------------------------------------------------------
116af8870a9STimothy Aiken   // -- Scale variables to desired units
117493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) {
118af8870a9STimothy Aiken     domain_size[i] *= meter;
119af8870a9STimothy Aiken     domain_min[i] *= meter;
120af8870a9STimothy Aiken   }
121af8870a9STimothy Aiken   problem->dm_scale    = meter;
122af8870a9STimothy Aiken   CeedScalar mid_point = 0.5 * (domain_size[0] + domain_min[0]);
123af8870a9STimothy Aiken 
124af8870a9STimothy Aiken   // -- Setup Context
125af8870a9STimothy Aiken   setup_context->mid_point = mid_point;
126af8870a9STimothy Aiken   setup_context->time      = 0.0;
127af8870a9STimothy Aiken   setup_context->P_high    = P_high;
128af8870a9STimothy Aiken   setup_context->rho_high  = rho_high;
129af8870a9STimothy Aiken   setup_context->P_low     = P_low;
130af8870a9STimothy Aiken   setup_context->rho_low   = rho_low;
131af8870a9STimothy Aiken 
132af8870a9STimothy Aiken   // -- QFunction Context
133af8870a9STimothy Aiken   user->phys->implicit         = implicit;
134af8870a9STimothy Aiken   user->phys->has_curr_time    = has_curr_time;
13515a3537eSJed Brown   shocktube_ctx->implicit      = implicit;
13615a3537eSJed Brown   shocktube_ctx->stabilization = stab;
13715a3537eSJed Brown   shocktube_ctx->yzb           = yzb;
13815a3537eSJed Brown   shocktube_ctx->Cyzb          = Cyzb;
13915a3537eSJed Brown   shocktube_ctx->Byzb          = Byzb;
14015a3537eSJed Brown   shocktube_ctx->c_tau         = c_tau;
141af8870a9STimothy Aiken 
14215a3537eSJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
1432b916ea7SJeremy L Thompson   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
1442b916ea7SJeremy L Thompson   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
145af8870a9STimothy Aiken 
14615a3537eSJed Brown   CeedQFunctionContextCreate(user->ceed, &shocktube_context);
1472b916ea7SJeremy L Thompson   CeedQFunctionContextSetData(shocktube_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*shocktube_ctx), shocktube_ctx);
1482b916ea7SJeremy L Thompson   CeedQFunctionContextSetDataDestroy(shocktube_context, CEED_MEM_HOST, FreeContextPetsc);
14915a3537eSJed Brown   problem->apply_vol_rhs.qfunction_context = shocktube_context;
150*d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
151af8870a9STimothy Aiken }
152af8870a9STimothy Aiken 
15340ccd21cSJed Brown PetscErrorCode PRINT_SHOCKTUBE(ProblemData *problem, AppCtx app_ctx) {
154af8870a9STimothy Aiken   MPI_Comm comm = PETSC_COMM_WORLD;
155af8870a9STimothy Aiken   PetscFunctionBeginUser;
156af8870a9STimothy Aiken 
1572b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(comm,
158af8870a9STimothy Aiken                         "  Problem:\n"
159af8870a9STimothy Aiken                         "    Problem Name                       : %s\n",
1602b916ea7SJeremy L Thompson                         app_ctx->problem_name));
161af8870a9STimothy Aiken 
162*d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
163af8870a9STimothy Aiken }
164