xref: /honee/problems/eulervortex.c (revision b30619f6337e4aeddfabbcf7d00859553914ef7b)
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 PetscErrorCode NS_EULER_VORTEX(ProblemData problem, DM dm, void *ctx) {
53   EulerTestType        euler_test;
54   Honee                honee = *(Honee *)ctx;
55   StabilizationType    stab;
56   MPI_Comm             comm = honee->comm;
57   Ceed                 ceed = honee->ceed;
58   PetscBool            implicit;
59   EulerContext         euler_ctx;
60   CeedQFunctionContext euler_qfctx;
61   PetscInt             dim;
62 
63   PetscFunctionBeginUser;
64   PetscCall(PetscCalloc1(1, &euler_ctx));
65 
66   // ------------------------------------------------------
67   //               SET UP EULER VORTEX
68   // ------------------------------------------------------
69   problem->ics.qf_func_ptr                 = ICsEuler;
70   problem->ics.qf_loc                      = ICsEuler_loc;
71   problem->apply_vol_rhs.qf_func_ptr       = Euler;
72   problem->apply_vol_rhs.qf_loc            = Euler_loc;
73   problem->apply_vol_ifunction.qf_func_ptr = IFunction_Euler;
74   problem->apply_vol_ifunction.qf_loc      = IFunction_Euler_loc;
75   problem->num_comps_jac_data              = 0;
76   problem->compute_exact_solution_error    = PETSC_TRUE;
77   problem->print_info                      = PRINT_EULER_VORTEX;
78 
79   // ------------------------------------------------------
80   //             Create the libCEED context
81   // ------------------------------------------------------
82   CeedScalar vortex_strength = 5.;   // -
83   CeedScalar c_tau           = 0.5;  // -
84   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
85   PetscReal center[3]  = {0.},         // m
86       mean_velocity[3] = {1., 1., 0};  // m/s
87   PetscReal domain_min[3], domain_max[3], domain_size[3] = {0.};
88   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
89   PetscCall(DMGetDimension(dm, &dim));
90   for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
91 
92   // ------------------------------------------------------
93   //             Create the PETSc context
94   // ------------------------------------------------------
95   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
96   PetscScalar second = 1e-2;  // 1 second in scaled time units
97 
98   // ------------------------------------------------------
99   //              Command line Options
100   // ------------------------------------------------------
101   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
102   // -- Physics
103   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
104   PetscInt  n = dim;
105   PetscBool user_velocity;
106   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
107   for (PetscInt i = 0; i < dim; i++) center[i] = .5 * domain_size[i];
108   n = dim;
109   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
110   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
111   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
112                              (PetscEnum *)&euler_test, NULL));
113   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
114   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
115   // -- Units
116   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
117   meter = fabs(meter);
118   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
119   second = fabs(second);
120 
121   // -- Warnings
122   if (stab == STAB_SUPG && !implicit) {
123     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
124   }
125   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
126     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
127   }
128 
129   PetscOptionsEnd();
130 
131   // ------------------------------------------------------
132   //           Set up the PETSc context
133   // ------------------------------------------------------
134   honee->units->meter  = meter;
135   honee->units->second = second;
136 
137   // ------------------------------------------------------
138   //           Set up the libCEED context
139   // ------------------------------------------------------
140   // -- Scale variables to desired units
141   for (PetscInt i = 0; i < 3; i++) {
142     center[i] *= meter;
143     domain_size[i] *= meter;
144     mean_velocity[i] *= (meter / second);
145   }
146 
147   // -- QFunction Context
148   honee->phys->implicit       = implicit;
149   euler_ctx->curr_time        = 0.;
150   euler_ctx->implicit         = implicit;
151   euler_ctx->euler_test       = euler_test;
152   euler_ctx->center[0]        = center[0];
153   euler_ctx->center[1]        = center[1];
154   euler_ctx->center[2]        = center[2];
155   euler_ctx->vortex_strength  = vortex_strength;
156   euler_ctx->c_tau            = c_tau;
157   euler_ctx->mean_velocity[0] = mean_velocity[0];
158   euler_ctx->mean_velocity[1] = mean_velocity[1];
159   euler_ctx->mean_velocity[2] = mean_velocity[2];
160   euler_ctx->stabilization    = stab;
161 
162   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &euler_qfctx));
163   PetscCallCeed(ceed, CeedQFunctionContextSetData(euler_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx));
164   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(euler_qfctx, CEED_MEM_HOST, FreeContextPetsc));
165   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(euler_qfctx, "solution time", offsetof(struct EulerContext_, curr_time), 1,
166                                                          "Physical time of the solution"));
167   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->ics.qfctx));
168   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_rhs.qfctx));
169   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_ifunction.qfctx));
170 
171   for (PetscCount b = 0; b < problem->num_bc_defs; b++) {
172     BCDefinition bc_def = problem->bc_defs[b];
173     const char  *name;
174 
175     PetscCall(BCDefinitionGetInfo(bc_def, &name, NULL, NULL));
176     if (!strcmp(name, "outflow")) {
177       HoneeBCStruct honee_bc;
178 
179       PetscCall(PetscNew(&honee_bc));
180       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
181       honee_bc->honee              = honee;
182       honee_bc->num_comps_jac_data = 0;
183       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
184 
185       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexOutflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
186       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
187     } else if (!strcmp(name, "inflow")) {
188       HoneeBCStruct honee_bc;
189 
190       PetscCall(PetscNew(&honee_bc));
191       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
192       honee_bc->honee              = honee;
193       honee_bc->num_comps_jac_data = 0;
194       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
195 
196       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexInflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
197       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
198     }
199   }
200   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&euler_qfctx));
201   PetscFunctionReturn(PETSC_SUCCESS);
202 }
203