xref: /honee/problems/eulervortex.c (revision d3c60aff9e7fa19eda39d99916bd3a7d6211ce97)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3a515125bSLeila Ghaffari 
4a515125bSLeila Ghaffari /// @file
5a515125bSLeila Ghaffari /// Utility functions for setting up EULER_VORTEX
6a515125bSLeila Ghaffari 
7a515125bSLeila Ghaffari #include "../qfunctions/eulervortex.h"
8a515125bSLeila Ghaffari 
9e419654dSJeremy L Thompson #include <ceed.h>
10e419654dSJeremy L Thompson #include <petscdm.h>
11e419654dSJeremy L Thompson 
12149fb536SJames Wright #include <navierstokes.h>
136dd99bedSLeila Ghaffari 
14f978755dSJames Wright static PetscErrorCode EulerVortexOutflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) {
15f978755dSJames Wright   HoneeBCStruct honee_bc;
16f978755dSJames Wright 
17f978755dSJames Wright   PetscFunctionBeginUser;
18f978755dSJames Wright   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
19f978755dSJames Wright   PetscCall(HoneeBCCreateIFunctionQF(bc_def, Euler_Outflow, Euler_Outflow_loc, honee_bc->qfctx, qf));
20f978755dSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
21f978755dSJames Wright }
22f978755dSJames Wright 
23d6cac220SJames Wright static PetscErrorCode EulerVortexInflowBCSetup_CreateIFunctionQF(BCDefinition bc_def, CeedQFunction *qf) {
24d6cac220SJames Wright   HoneeBCStruct honee_bc;
25d6cac220SJames Wright 
26d6cac220SJames Wright   PetscFunctionBeginUser;
27d6cac220SJames Wright   PetscCall(BCDefinitionGetContext(bc_def, &honee_bc));
28d6cac220SJames Wright   PetscCall(HoneeBCCreateIFunctionQF(bc_def, TravelingVortex_Inflow, TravelingVortex_Inflow_loc, honee_bc->qfctx, qf));
29d6cac220SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
30d6cac220SJames Wright }
31d6cac220SJames Wright 
32*d3c60affSJames Wright PetscErrorCode NS_EULER_VORTEX(ProblemData problem, DM dm, void *ctx) {
33a515125bSLeila Ghaffari   EulerTestType        euler_test;
340c373b74SJames Wright   Honee                honee = *(Honee *)ctx;
35139613f2SLeila Ghaffari   StabilizationType    stab;
360c373b74SJames Wright   MPI_Comm             comm = honee->comm;
370c373b74SJames Wright   Ceed                 ceed = honee->ceed;
38a515125bSLeila Ghaffari   PetscBool            implicit;
3915a3537eSJed Brown   EulerContext         euler_ctx;
40e07531f7SJames Wright   CeedQFunctionContext euler_qfctx;
4166d54740SJames Wright   PetscInt             dim;
42a515125bSLeila Ghaffari 
4315a3537eSJed Brown   PetscFunctionBeginUser;
442b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &euler_ctx));
45a515125bSLeila Ghaffari 
46a515125bSLeila Ghaffari   // ------------------------------------------------------
47f978755dSJames Wright   //               SET UP EULER VORTEX
48a515125bSLeila Ghaffari   // ------------------------------------------------------
49e07531f7SJames Wright   problem->ics.qf_func_ptr                 = ICsEuler;
50e07531f7SJames Wright   problem->ics.qf_loc                      = ICsEuler_loc;
51e07531f7SJames Wright   problem->apply_vol_rhs.qf_func_ptr       = Euler;
52e07531f7SJames Wright   problem->apply_vol_rhs.qf_loc            = Euler_loc;
53e07531f7SJames Wright   problem->apply_vol_ifunction.qf_func_ptr = IFunction_Euler;
54e07531f7SJames Wright   problem->apply_vol_ifunction.qf_loc      = IFunction_Euler_loc;
5528160fc2SJames Wright   problem->jac_data_size_vol               = 0;
5628160fc2SJames Wright   problem->jac_data_size_sur               = 0;
5758ce1233SJames Wright   problem->compute_exact_solution_error    = PETSC_TRUE;
58a515125bSLeila Ghaffari   problem->print_info                      = PRINT_EULER_VORTEX;
59a515125bSLeila Ghaffari 
60a515125bSLeila Ghaffari   // ------------------------------------------------------
61a515125bSLeila Ghaffari   //             Create the libCEED context
62a515125bSLeila Ghaffari   // ------------------------------------------------------
63a515125bSLeila Ghaffari   CeedScalar vortex_strength = 5.;   // -
64f821ee77SLeila Ghaffari   CeedScalar c_tau           = 0.5;  // -
65d8a22b9eSJed Brown   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
6666d54740SJames Wright   PetscReal center[3]  = {0.},         // m
673b451b28SLeila Ghaffari       mean_velocity[3] = {1., 1., 0};  // m/s
6866d54740SJames Wright   PetscReal domain_min[3], domain_max[3], domain_size[3] = {0.};
692b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
7066d54740SJames Wright   PetscCall(DMGetDimension(dm, &dim));
7166d54740SJames Wright   for (PetscInt i = 0; i < dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
72a515125bSLeila Ghaffari 
73a515125bSLeila Ghaffari   // ------------------------------------------------------
74a515125bSLeila Ghaffari   //             Create the PETSc context
75a515125bSLeila Ghaffari   // ------------------------------------------------------
76a515125bSLeila Ghaffari   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
77a515125bSLeila Ghaffari   PetscScalar second = 1e-2;  // 1 second in scaled time units
78a515125bSLeila Ghaffari 
79a515125bSLeila Ghaffari   // ------------------------------------------------------
80a515125bSLeila Ghaffari   //              Command line Options
81a515125bSLeila Ghaffari   // ------------------------------------------------------
821485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
83a515125bSLeila Ghaffari   // -- Physics
842b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
8566d54740SJames Wright   PetscInt  n = dim;
86a515125bSLeila Ghaffari   PetscBool user_velocity;
872b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
8866d54740SJames Wright   for (PetscInt i = 0; i < dim; i++) center[i] = .5 * domain_size[i];
8966d54740SJames Wright   n = dim;
902b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
912b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
922b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
932b916ea7SJeremy L Thompson                              (PetscEnum *)&euler_test, NULL));
942b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
952b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
96a515125bSLeila Ghaffari   // -- Units
972b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
98a515125bSLeila Ghaffari   meter = fabs(meter);
992b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
100a515125bSLeila Ghaffari   second = fabs(second);
101a515125bSLeila Ghaffari 
102a515125bSLeila Ghaffari   // -- Warnings
103139613f2SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1042b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
105139613f2SLeila Ghaffari   }
1062b916ea7SJeremy L Thompson   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
1072b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
108a515125bSLeila Ghaffari   }
109a515125bSLeila Ghaffari 
1101485969bSJeremy L Thompson   PetscOptionsEnd();
111a515125bSLeila Ghaffari 
112a515125bSLeila Ghaffari   // ------------------------------------------------------
113a515125bSLeila Ghaffari   //           Set up the PETSc context
114a515125bSLeila Ghaffari   // ------------------------------------------------------
1150c373b74SJames Wright   honee->units->meter  = meter;
1160c373b74SJames Wright   honee->units->second = second;
117a515125bSLeila Ghaffari 
118a515125bSLeila Ghaffari   // ------------------------------------------------------
119a515125bSLeila Ghaffari   //           Set up the libCEED context
120a515125bSLeila Ghaffari   // ------------------------------------------------------
121a515125bSLeila Ghaffari   // -- Scale variables to desired units
122493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) {
1233b451b28SLeila Ghaffari     center[i] *= meter;
12405a512bdSLeila Ghaffari     domain_size[i] *= meter;
12505a512bdSLeila Ghaffari     mean_velocity[i] *= (meter / second);
1263b451b28SLeila Ghaffari   }
127a515125bSLeila Ghaffari 
128a515125bSLeila Ghaffari   // -- QFunction Context
1290c373b74SJames Wright   honee->phys->implicit       = implicit;
13015a3537eSJed Brown   euler_ctx->curr_time        = 0.;
13115a3537eSJed Brown   euler_ctx->implicit         = implicit;
13215a3537eSJed Brown   euler_ctx->euler_test       = euler_test;
13315a3537eSJed Brown   euler_ctx->center[0]        = center[0];
13415a3537eSJed Brown   euler_ctx->center[1]        = center[1];
13515a3537eSJed Brown   euler_ctx->center[2]        = center[2];
13615a3537eSJed Brown   euler_ctx->vortex_strength  = vortex_strength;
13715a3537eSJed Brown   euler_ctx->c_tau            = c_tau;
13815a3537eSJed Brown   euler_ctx->mean_velocity[0] = mean_velocity[0];
13915a3537eSJed Brown   euler_ctx->mean_velocity[1] = mean_velocity[1];
14015a3537eSJed Brown   euler_ctx->mean_velocity[2] = mean_velocity[2];
14115a3537eSJed Brown   euler_ctx->stabilization    = stab;
142a515125bSLeila Ghaffari 
1430c373b74SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(honee->ceed, &euler_qfctx));
144e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(euler_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx));
145e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(euler_qfctx, CEED_MEM_HOST, FreeContextPetsc));
146e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(euler_qfctx, "solution time", offsetof(struct EulerContext_, curr_time), 1,
147b4c37c5cSJames Wright                                                          "Physical time of the solution"));
148e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->ics.qfctx));
149e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_rhs.qfctx));
150e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &problem->apply_vol_ifunction.qfctx));
151f978755dSJames Wright 
152f978755dSJames Wright   for (PetscCount b = 0; b < problem->num_bc_defs; b++) {
153f978755dSJames Wright     BCDefinition bc_def = problem->bc_defs[b];
154f978755dSJames Wright     const char  *name;
155f978755dSJames Wright 
156f978755dSJames Wright     PetscCall(BCDefinitionGetInfo(bc_def, &name, NULL, NULL));
157f978755dSJames Wright     if (!strcmp(name, "outflow")) {
158f978755dSJames Wright       HoneeBCStruct honee_bc;
159f978755dSJames Wright 
160f978755dSJames Wright       PetscCall(PetscNew(&honee_bc));
161f978755dSJames Wright       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
162f978755dSJames Wright       honee_bc->honee             = honee;
163f978755dSJames Wright       honee_bc->jac_data_size_sur = honee->phys->implicit ? problem->jac_data_size_sur : 0;
164f978755dSJames Wright       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
165f978755dSJames Wright 
166f978755dSJames Wright       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexOutflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
167f978755dSJames Wright       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
168d6cac220SJames Wright     } else if (!strcmp(name, "inflow")) {
169d6cac220SJames Wright       HoneeBCStruct honee_bc;
170d6cac220SJames Wright 
171d6cac220SJames Wright       PetscCall(PetscNew(&honee_bc));
172d6cac220SJames Wright       PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(euler_qfctx, &honee_bc->qfctx));
173d6cac220SJames Wright       honee_bc->honee             = honee;
174d6cac220SJames Wright       honee_bc->jac_data_size_sur = honee->phys->implicit ? problem->jac_data_size_sur : 0;
175d6cac220SJames Wright       PetscCall(BCDefinitionSetContext(bc_def, HoneeBCDestroy, honee_bc));
176d6cac220SJames Wright 
177d6cac220SJames Wright       PetscCall(BCDefinitionSetIFunction(bc_def, EulerVortexInflowBCSetup_CreateIFunctionQF, HoneeBCAddIFunctionOp));
178d6cac220SJames Wright       PetscCall(BCDefinitionSetIJacobian(bc_def, NULL, NULL));
179f978755dSJames Wright     }
180f978755dSJames Wright   }
181e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&euler_qfctx));
182d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
183a515125bSLeila Ghaffari }
184a515125bSLeila Ghaffari 
1850c373b74SJames Wright PetscErrorCode PRINT_EULER_VORTEX(Honee honee, ProblemData problem, AppCtx app_ctx) {
1860c373b74SJames Wright   MPI_Comm     comm = honee->comm;
1870c373b74SJames Wright   Ceed         ceed = honee->ceed;
18815a3537eSJed Brown   EulerContext euler_ctx;
189a515125bSLeila Ghaffari 
19015a3537eSJed Brown   PetscFunctionBeginUser;
191e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfctx, CEED_MEM_HOST, &euler_ctx));
1922b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(comm,
193a515125bSLeila Ghaffari                         "  Problem:\n"
194a515125bSLeila Ghaffari                         "    Problem Name                       : %s\n"
195a515125bSLeila Ghaffari                         "    Test Case                          : %s\n"
196139613f2SLeila Ghaffari                         "    Background Velocity                : %f,%f,%f\n"
197139613f2SLeila Ghaffari                         "    Stabilization                      : %s\n",
1982b916ea7SJeremy L Thompson                         app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test], euler_ctx->mean_velocity[0], euler_ctx->mean_velocity[1],
1992b916ea7SJeremy L Thompson                         euler_ctx->mean_velocity[2], StabilizationTypes[euler_ctx->stabilization]));
200a515125bSLeila Ghaffari 
201e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfctx, &euler_ctx));
202d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
203a515125bSLeila Ghaffari }
204