xref: /honee/problems/shocktube.c (revision ea091b8e43283667a8e2f5f647273e58272b0e63)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Utility functions for setting up SHOCKTUBE
6 
7 #include "../qfunctions/shocktube.h"
8 
9 #include <ceed.h>
10 #include <petscdm.h>
11 
12 #include <navierstokes.h>
13 
14 PetscErrorCode NS_SHOCKTUBE(ProblemData problem, DM dm, void *ctx) {
15   SetupContextShock    setup_context;
16   Honee                honee = *(Honee *)ctx;
17   MPI_Comm             comm  = honee->comm;
18   Ceed                 ceed  = honee->ceed;
19   PetscBool            implicit;
20   PetscBool            yzb;
21   PetscInt             stab;
22   ShockTubeContext     shocktube_ctx;
23   CeedQFunctionContext shocktube_qfctx;
24 
25   PetscFunctionBeginUser;
26   PetscCall(PetscCalloc1(1, &setup_context));
27   PetscCall(PetscCalloc1(1, &shocktube_ctx));
28 
29   // ------------------------------------------------------
30   //               SET UP SHOCKTUBE
31   // ------------------------------------------------------
32   problem->ics.qf_func_ptr                 = ICsShockTube;
33   problem->ics.qf_loc                      = ICsShockTube_loc;
34   problem->apply_vol_rhs.qf_func_ptr       = EulerShockTube;
35   problem->apply_vol_rhs.qf_loc            = EulerShockTube_loc;
36   problem->apply_vol_ifunction.qf_func_ptr = NULL;
37   problem->apply_vol_ifunction.qf_loc      = NULL;
38   problem->jac_data_size_vol               = 0;
39   problem->compute_exact_solution_error    = PETSC_FALSE;
40   problem->print_info                      = PRINT_SHOCKTUBE;
41 
42   // ------------------------------------------------------
43   //             Create the QFunction context
44   // ------------------------------------------------------
45   // Driver section initial conditions
46   CeedScalar P_high   = 1.0;  // Pa
47   CeedScalar rho_high = 1.0;  // kg/m^3
48   // Driven section initial conditions
49   CeedScalar P_low   = 0.1;    // Pa
50   CeedScalar rho_low = 0.125;  // kg/m^3
51   // Stabilization parameter
52   CeedScalar c_tau = 0.5;  // -, based on Hughes et al (2010)
53   // Tuning parameters for the YZB shock capturing
54   CeedScalar Cyzb = 0.1;  // -, used in approximation of (Na),x
55   CeedScalar Byzb = 2.0;  // -, 1 for smooth shocks
56   //                                          2 for sharp shocks
57   PetscReal domain_min[3], domain_max[3], domain_size[3];
58   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
59   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
60 
61   // ------------------------------------------------------
62   //             Create the PETSc context
63   // ------------------------------------------------------
64   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
65   PetscScalar second = 1e-2;  // 1 second in scaled time units
66 
67   // ------------------------------------------------------
68   //              Command line Options
69   // ------------------------------------------------------
70   PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL);
71 
72   // -- Numerical formulation options
73   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
74   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
75   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
76   PetscCall(PetscOptionsBool("-yzb", "Use YZB discontinuity capturing", NULL, yzb = PETSC_FALSE, &yzb, NULL));
77 
78   // -- Units
79   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
80   meter = fabs(meter);
81   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
82   second = fabs(second);
83 
84   // -- Warnings
85   if (stab == STAB_SUPG) {
86     PetscCall(PetscPrintf(comm, "Warning! -stab supg not implemented for the shocktube problem. \n"));
87   }
88   if (yzb && implicit) {
89     PetscCall(PetscPrintf(comm, "Warning! -yzb only implemented for explicit timestepping. \n"));
90   }
91 
92   PetscOptionsEnd();
93 
94   // ------------------------------------------------------
95   //           Set up the PETSc context
96   // ------------------------------------------------------
97   honee->units->meter  = meter;
98   honee->units->second = second;
99 
100   // ------------------------------------------------------
101   //           Set up the QFunction context
102   // ------------------------------------------------------
103   // -- Scale variables to desired units
104   for (PetscInt i = 0; i < 3; i++) {
105     domain_size[i] *= meter;
106     domain_min[i] *= meter;
107   }
108   CeedScalar mid_point = 0.5 * (domain_size[0] + domain_min[0]);
109 
110   // -- Setup Context
111   setup_context->mid_point = mid_point;
112   setup_context->time      = 0.0;
113   setup_context->P_high    = P_high;
114   setup_context->rho_high  = rho_high;
115   setup_context->P_low     = P_low;
116   setup_context->rho_low   = rho_low;
117 
118   // -- QFunction Context
119   honee->phys->implicit        = implicit;
120   shocktube_ctx->implicit      = implicit;
121   shocktube_ctx->stabilization = stab;
122   shocktube_ctx->yzb           = yzb;
123   shocktube_ctx->Cyzb          = Cyzb;
124   shocktube_ctx->Byzb          = Byzb;
125   shocktube_ctx->c_tau         = c_tau;
126 
127   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &problem->ics.qfctx));
128   PetscCallCeed(ceed, CeedQFunctionContextSetData(problem->ics.qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
129   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfctx, CEED_MEM_HOST, FreeContextPetsc));
130 
131   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &shocktube_qfctx));
132   PetscCallCeed(ceed, CeedQFunctionContextSetData(shocktube_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*shocktube_ctx), shocktube_ctx));
133   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(shocktube_qfctx, CEED_MEM_HOST, FreeContextPetsc));
134   problem->apply_vol_rhs.qfctx = shocktube_qfctx;
135   PetscFunctionReturn(PETSC_SUCCESS);
136 }
137 
138 PetscErrorCode PRINT_SHOCKTUBE(Honee honee, ProblemData problem, AppCtx app_ctx) {
139   MPI_Comm comm = honee->comm;
140 
141   PetscFunctionBeginUser;
142   PetscCall(PetscPrintf(comm,
143                         "  Problem:\n"
144                         "    Problem Name                       : %s\n",
145                         app_ctx->problem_name));
146   PetscFunctionReturn(PETSC_SUCCESS);
147 }
148