xref: /honee/problems/eulervortex.c (revision f5dc303c751707e854f20e368f7e802642de97c1)
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 EULER_VORTEX
6 
7 #include "../qfunctions/eulervortex.h"
8 
9 #include <ceed.h>
10 #include <petscdm.h>
11 
12 #include <navierstokes.h>
13 
14 static PetscErrorCode PRINT_EULER_VORTEX(Honee honee, ProblemData problem, AppCtx app_ctx) {
15   MPI_Comm     comm = honee->comm;
16   Ceed         ceed = honee->ceed;
17   EulerContext euler_ctx;
18 
19   PetscFunctionBeginUser;
20   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfctx, CEED_MEM_HOST, &euler_ctx));
21   PetscCall(PetscPrintf(comm,
22                         "  Problem:\n"
23                         "    Problem Name                       : %s\n"
24                         "    Test Case                          : %s\n"
25                         "    Background Velocity                : %f,%f,%f\n"
26                         "    Stabilization                      : %s\n",
27                         app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test], euler_ctx->mean_velocity[0], euler_ctx->mean_velocity[1],
28                         euler_ctx->mean_velocity[2], StabilizationTypes[euler_ctx->stabilization]));
29 
30   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfctx, &euler_ctx));
31   PetscFunctionReturn(PETSC_SUCCESS);
32 }
33 
34 static PetscErrorCode EulerVortexOutflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) {
35   HoneeBCStruct honee_bc;
36 
37   PetscFunctionBeginUser;
38   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
39   PetscCall(HoneeBCCreateIFunctionQF(bc_def, Euler_Outflow, Euler_Outflow_loc, honee_bc->qfctx, qf));
40   PetscFunctionReturn(PETSC_SUCCESS);
41 }
42 
43 static PetscErrorCode EulerVortexInflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) {
44   HoneeBCStruct honee_bc;
45 
46   PetscFunctionBeginUser;
47   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
48   PetscCall(HoneeBCCreateIFunctionQF(bc_def, TravelingVortex_Inflow, TravelingVortex_Inflow_loc, honee_bc->qfctx, qf));
49   PetscFunctionReturn(PETSC_SUCCESS);
50 }
51 
52 static const char *const component_names[] = {"Density", "MomentumX", "MomentumY", "MomentumZ", "TotalEnergy"};
53 
54 PetscErrorCode NS_EULER_VORTEX(ProblemData problem, DM dm, void *ctx) {
55   EulerTestType        euler_test;
56   Honee                honee = *(Honee *)ctx;
57   StabilizationType    stab;
58   MPI_Comm             comm = honee->comm;
59   Ceed                 ceed = honee->ceed;
60   PetscBool            implicit;
61   EulerContext         euler_ctx;
62   CeedQFunctionContext euler_qfctx;
63   PetscInt             dim;
64 
65   PetscFunctionBeginUser;
66   PetscCall(PetscNew(&euler_ctx));
67 
68   // ------------------------------------------------------
69   //               SET UP EULER VORTEX
70   // ------------------------------------------------------
71   problem->ics                          = (HoneeQFSpec){.qf_func_ptr = ICsEuler, .qf_loc = ICsEuler_loc};
72   problem->apply_vol_rhs                = (HoneeQFSpec){.qf_func_ptr = Euler, .qf_loc = Euler_loc};
73   problem->apply_vol_ifunction          = (HoneeQFSpec){.qf_func_ptr = IFunction_Euler, .qf_loc = IFunction_Euler_loc};
74   problem->num_comps_jac_data           = 0;
75   problem->compute_exact_solution_error = PETSC_TRUE;
76   problem->print_info                   = PRINT_EULER_VORTEX;
77 
78   problem->num_components = 5;
79   PetscCall(PetscMalloc1(problem->num_components, &problem->component_names));
80   for (PetscInt i = 0; i < 5; i++) PetscCall(PetscStrallocpy(component_names[i], &problem->component_names[i]));
81 
82   // ------------------------------------------------------
83   //             Create the libCEED context
84   // ------------------------------------------------------
85   CeedScalar vortex_strength = 5.;   // -
86   CeedScalar c_tau           = 0.5;  // -
87   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
88   PetscReal center[3]  = {0.},         // m
89       mean_velocity[3] = {1., 1., 0};  // m/s
90   PetscReal domain_min[3], domain_max[3], domain_size[3] = {0.};
91   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
92   PetscCall(DMGetDimension(dm, &dim));
93   for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
94 
95   // ------------------------------------------------------
96   //              Command line Options
97   // ------------------------------------------------------
98   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
99   // -- Physics
100   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
101   PetscInt  n = dim;
102   PetscBool user_velocity;
103   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
104   for (PetscInt i = 0; i < dim; i++) center[i] = .5 * domain_size[i];
105   n = dim;
106   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
107   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
108   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
109                              (PetscEnum *)&euler_test, NULL));
110   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
111   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
112 
113   // -- Warnings
114   if (stab == STAB_SUPG && !implicit) {
115     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
116   }
117   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
118     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
119   }
120 
121   PetscOptionsEnd();
122 
123   // ------------------------------------------------------
124   //           Set up the libCEED context
125   // ------------------------------------------------------
126   // -- Scale variables to desired units
127   Units units = honee->units;
128   for (PetscInt i = 0; i < 3; i++) {
129     center[i] *= units->meter;
130     domain_size[i] *= units->meter;
131     mean_velocity[i] *= (units->meter / units->second);
132   }
133 
134   // -- QFunction Context
135   honee->phys->implicit       = implicit;
136   euler_ctx->curr_time        = 0.;
137   euler_ctx->implicit         = implicit;
138   euler_ctx->euler_test       = euler_test;
139   euler_ctx->center[0]        = center[0];
140   euler_ctx->center[1]        = center[1];
141   euler_ctx->center[2]        = center[2];
142   euler_ctx->vortex_strength  = vortex_strength;
143   euler_ctx->c_tau            = c_tau;
144   euler_ctx->mean_velocity[0] = mean_velocity[0];
145   euler_ctx->mean_velocity[1] = mean_velocity[1];
146   euler_ctx->mean_velocity[2] = mean_velocity[2];
147   euler_ctx->stabilization    = stab;
148 
149   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &euler_qfctx));
150   PetscCallCeed(ceed, CeedQFunctionContextSetData(euler_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx));
151   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(euler_qfctx, CEED_MEM_HOST, FreeContextPetsc));
152   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(euler_qfctx, "solution time", offsetof(struct EulerContext_, curr_time), 1,
153                                                          "Physical time of the solution"));
154   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->ics.qfctx));
155   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_rhs.qfctx));
156   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_ifunction.qfctx));
157 
158   for (PetscCount b = 0; b < problem->num_bc_defs; b++) {
159     BCDefinition bc_def = problem->bc_defs[b];
160     const char  *name;
161 
162     PetscCall(BCDefinitionGetInfo(bc_def, &name, NULL, NULL));
163     if (!strcmp(name, "outflow")) {
164       HoneeBCStruct honee_bc;
165 
166       PetscCall(PetscNew(&honee_bc));
167       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
168       honee_bc->honee              = honee;
169       honee_bc->num_comps_jac_data = 0;
170       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
171 
172       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexOutflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
173       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
174     } else if (!strcmp(name, "inflow")) {
175       HoneeBCStruct honee_bc;
176 
177       PetscCall(PetscNew(&honee_bc));
178       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
179       honee_bc->honee              = honee;
180       honee_bc->num_comps_jac_data = 0;
181       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
182 
183       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexInflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
184       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
185     }
186   }
187   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&euler_qfctx));
188   PetscFunctionReturn(PETSC_SUCCESS);
189 }
190