xref: /honee/problems/eulervortex.c (revision cbdfeaf49eecfe108b28f98e5e7c308a9796a444)
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 PetscErrorCode NS_EULER_VORTEX(ProblemData problem, DM dm, void *ctx, SimpleBC bc) {
15   EulerTestType        euler_test;
16   User                 user = *(User *)ctx;
17   StabilizationType    stab;
18   MPI_Comm             comm = user->comm;
19   Ceed                 ceed = user->ceed;
20   PetscBool            implicit;
21   EulerContext         euler_ctx;
22   CeedQFunctionContext euler_qfctx;
23   PetscInt             dim;
24 
25   PetscFunctionBeginUser;
26   PetscCall(PetscCalloc1(1, &euler_ctx));
27 
28   // ------------------------------------------------------
29   //               SET UP DENSITY_CURRENT
30   // ------------------------------------------------------
31   problem->ics.qf_func_ptr                 = ICsEuler;
32   problem->ics.qf_loc                      = ICsEuler_loc;
33   problem->apply_vol_rhs.qf_func_ptr       = Euler;
34   problem->apply_vol_rhs.qf_loc            = Euler_loc;
35   problem->apply_vol_ifunction.qf_func_ptr = IFunction_Euler;
36   problem->apply_vol_ifunction.qf_loc      = IFunction_Euler_loc;
37   problem->apply_inflow.qf_func_ptr        = TravelingVortex_Inflow;
38   problem->apply_inflow.qf_loc             = TravelingVortex_Inflow_loc;
39   problem->apply_outflow.qf_func_ptr       = Euler_Outflow;
40   problem->apply_outflow.qf_loc            = Euler_Outflow_loc;
41   problem->jac_data_size_vol               = 0;
42   problem->jac_data_size_sur               = 0;
43   problem->compute_exact_solution_error    = PETSC_TRUE;
44   problem->print_info                      = PRINT_EULER_VORTEX;
45 
46   // ------------------------------------------------------
47   //             Create the libCEED context
48   // ------------------------------------------------------
49   CeedScalar vortex_strength = 5.;   // -
50   CeedScalar c_tau           = 0.5;  // -
51   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
52   PetscReal center[3]  = {0.},         // m
53       mean_velocity[3] = {1., 1., 0};  // m/s
54   PetscReal domain_min[3], domain_max[3], domain_size[3] = {0.};
55   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
56   PetscCall(DMGetDimension(dm, &dim));
57   for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
58 
59   // ------------------------------------------------------
60   //             Create the PETSc context
61   // ------------------------------------------------------
62   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
63   PetscScalar second = 1e-2;  // 1 second in scaled time units
64 
65   // ------------------------------------------------------
66   //              Command line Options
67   // ------------------------------------------------------
68   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
69   // -- Physics
70   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
71   PetscInt  n = dim;
72   PetscBool user_velocity;
73   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
74   for (PetscInt i = 0; i < dim; i++) center[i] = .5 * domain_size[i];
75   n = dim;
76   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
77   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
78   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
79                              (PetscEnum *)&euler_test, NULL));
80   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
81   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
82   // -- Units
83   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
84   meter = fabs(meter);
85   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
86   second = fabs(second);
87 
88   // -- Warnings
89   if (stab == STAB_SUPG && !implicit) {
90     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
91   }
92   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
93     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
94   }
95 
96   PetscOptionsEnd();
97 
98   // ------------------------------------------------------
99   //           Set up the PETSc context
100   // ------------------------------------------------------
101   user->units->meter  = meter;
102   user->units->second = second;
103 
104   // ------------------------------------------------------
105   //           Set up the libCEED context
106   // ------------------------------------------------------
107   // -- Scale variables to desired units
108   for (PetscInt i = 0; i < 3; i++) {
109     center[i] *= meter;
110     domain_size[i] *= meter;
111     mean_velocity[i] *= (meter / second);
112   }
113 
114   // -- QFunction Context
115   user->phys->implicit        = implicit;
116   euler_ctx->curr_time        = 0.;
117   euler_ctx->implicit         = implicit;
118   euler_ctx->euler_test       = euler_test;
119   euler_ctx->center[0]        = center[0];
120   euler_ctx->center[1]        = center[1];
121   euler_ctx->center[2]        = center[2];
122   euler_ctx->vortex_strength  = vortex_strength;
123   euler_ctx->c_tau            = c_tau;
124   euler_ctx->mean_velocity[0] = mean_velocity[0];
125   euler_ctx->mean_velocity[1] = mean_velocity[1];
126   euler_ctx->mean_velocity[2] = mean_velocity[2];
127   euler_ctx->stabilization    = stab;
128 
129   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &euler_qfctx));
130   PetscCallCeed(ceed, CeedQFunctionContextSetData(euler_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx));
131   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(euler_qfctx, CEED_MEM_HOST, FreeContextPetsc));
132   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(euler_qfctx, "solution time", offsetof(struct EulerContext_, curr_time), 1,
133                                                          "Physical time of the solution"));
134   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->ics.qfctx));
135   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_rhs.qfctx));
136   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_ifunction.qfctx));
137   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_inflow.qfctx));
138   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_outflow.qfctx));
139   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&euler_qfctx));
140   PetscFunctionReturn(PETSC_SUCCESS);
141 }
142 
143 PetscErrorCode PRINT_EULER_VORTEX(User user, ProblemData problem, AppCtx app_ctx) {
144   MPI_Comm     comm = user->comm;
145   Ceed         ceed = user->ceed;
146   EulerContext euler_ctx;
147 
148   PetscFunctionBeginUser;
149   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfctx, CEED_MEM_HOST, &euler_ctx));
150   PetscCall(PetscPrintf(comm,
151                         "  Problem:\n"
152                         "    Problem Name                       : %s\n"
153                         "    Test Case                          : %s\n"
154                         "    Background Velocity                : %f,%f,%f\n"
155                         "    Stabilization                      : %s\n",
156                         app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test], euler_ctx->mean_velocity[0], euler_ctx->mean_velocity[1],
157                         euler_ctx->mean_velocity[2], StabilizationTypes[euler_ctx->stabilization]));
158 
159   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfctx, &euler_ctx));
160   PetscFunctionReturn(PETSC_SUCCESS);
161 }
162