xref: /libCEED/examples/fluids/problems/eulervortex.c (revision 46de7363edb07f0ea682e5e2fa0ce97210b98e34)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Utility functions for setting up EULER_VORTEX
1077841947SLeila Ghaffari 
1177841947SLeila Ghaffari #include "../qfunctions/eulervortex.h"
1277841947SLeila Ghaffari 
132b730f8bSJeremy L Thompson #include "../navierstokes.h"
142b730f8bSJeremy L Thompson #include "../qfunctions/setupgeo.h"
156ef2784eSLeila Ghaffari 
16*46de7363SJames Wright PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
1777841947SLeila Ghaffari   EulerTestType        euler_test;
1877841947SLeila Ghaffari   User                 user = *(User *)ctx;
19e6225c47SLeila Ghaffari   StabilizationType    stab;
2077841947SLeila Ghaffari   MPI_Comm             comm = PETSC_COMM_WORLD;
2177841947SLeila Ghaffari   PetscBool            implicit;
2277841947SLeila Ghaffari   PetscBool            has_curr_time = PETSC_TRUE;
2377841947SLeila Ghaffari   PetscBool            has_neumann   = PETSC_TRUE;
24841e4c73SJed Brown   EulerContext         euler_ctx;
25841e4c73SJed Brown   CeedQFunctionContext euler_context;
2677841947SLeila Ghaffari 
27841e4c73SJed Brown   PetscFunctionBeginUser;
282b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &euler_ctx));
2977841947SLeila Ghaffari 
3077841947SLeila Ghaffari   // ------------------------------------------------------
3177841947SLeila Ghaffari   //               SET UP DENSITY_CURRENT
3277841947SLeila Ghaffari   // ------------------------------------------------------
3377841947SLeila Ghaffari   problem->dim                               = 3;
3477841947SLeila Ghaffari   problem->q_data_size_vol                   = 10;
35ba6664aeSJames Wright   problem->q_data_size_sur                   = 10;
3691e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
3791e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
3891e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
3991e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
4091e5af17SJed Brown   problem->ics.qfunction                     = ICsEuler;
4191e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsEuler_loc;
4291e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = Euler;
4391e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Euler_loc;
4491e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Euler;
4591e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Euler_loc;
4691e5af17SJed Brown   problem->apply_inflow.qfunction            = TravelingVortex_Inflow;
4791e5af17SJed Brown   problem->apply_inflow.qfunction_loc        = TravelingVortex_Inflow_loc;
4891e5af17SJed Brown   problem->apply_outflow.qfunction           = Euler_Outflow;
4991e5af17SJed Brown   problem->apply_outflow.qfunction_loc       = Euler_Outflow_loc;
5077841947SLeila Ghaffari   problem->bc                                = Exact_Euler;
51a0add3c9SJed Brown   problem->bc_ctx                            = euler_ctx;
5277841947SLeila Ghaffari   problem->non_zero_time                     = PETSC_TRUE;
5377841947SLeila Ghaffari   problem->print_info                        = PRINT_EULER_VORTEX;
5477841947SLeila Ghaffari 
5577841947SLeila Ghaffari   // ------------------------------------------------------
5677841947SLeila Ghaffari   //             Create the libCEED context
5777841947SLeila Ghaffari   // ------------------------------------------------------
5877841947SLeila Ghaffari   CeedScalar vortex_strength = 5.;   // -
59504dc8e0SLeila Ghaffari   CeedScalar c_tau           = 0.5;  // -
60932417b3SJed Brown   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
61e535bb46SLeila Ghaffari   PetscReal center[3],                 // m
62e535bb46SLeila Ghaffari       mean_velocity[3] = {1., 1., 0};  // m/s
631864f1c2SLeila Ghaffari   PetscReal domain_min[3], domain_max[3], domain_size[3];
642b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
65ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
6677841947SLeila Ghaffari 
6777841947SLeila Ghaffari   // ------------------------------------------------------
6877841947SLeila Ghaffari   //             Create the PETSc context
6977841947SLeila Ghaffari   // ------------------------------------------------------
7077841947SLeila Ghaffari   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
7177841947SLeila Ghaffari   PetscScalar second = 1e-2;  // 1 second in scaled time units
7277841947SLeila Ghaffari 
7377841947SLeila Ghaffari   // ------------------------------------------------------
7477841947SLeila Ghaffari   //              Command line Options
7577841947SLeila Ghaffari   // ------------------------------------------------------
7667490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
7777841947SLeila Ghaffari   // -- Physics
782b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
7977841947SLeila Ghaffari   PetscInt  n = problem->dim;
8077841947SLeila Ghaffari   PetscBool user_velocity;
812b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
82ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) center[i] = .5 * domain_size[i];
8377841947SLeila Ghaffari   n = problem->dim;
842b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
872b730f8bSJeremy L Thompson                              (PetscEnum *)&euler_test, NULL));
882b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
9077841947SLeila Ghaffari   // -- Units
912b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
9277841947SLeila Ghaffari   meter = fabs(meter);
932b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
9477841947SLeila Ghaffari   second = fabs(second);
9577841947SLeila Ghaffari 
9677841947SLeila Ghaffari   // -- Warnings
97e6225c47SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
982b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
99e6225c47SLeila Ghaffari   }
1002b730f8bSJeremy L Thompson   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
1012b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
10277841947SLeila Ghaffari   }
10377841947SLeila Ghaffari 
10467490bc6SJeremy L Thompson   PetscOptionsEnd();
10577841947SLeila Ghaffari 
10677841947SLeila Ghaffari   // ------------------------------------------------------
10777841947SLeila Ghaffari   //           Set up the PETSc context
10877841947SLeila Ghaffari   // ------------------------------------------------------
10977841947SLeila Ghaffari   user->units->meter  = meter;
11077841947SLeila Ghaffari   user->units->second = second;
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari   // ------------------------------------------------------
11377841947SLeila Ghaffari   //           Set up the libCEED context
11477841947SLeila Ghaffari   // ------------------------------------------------------
11577841947SLeila Ghaffari   // -- Scale variables to desired units
116ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) {
117e535bb46SLeila Ghaffari     center[i] *= meter;
1181864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1191864f1c2SLeila Ghaffari     mean_velocity[i] *= (meter / second);
120e535bb46SLeila Ghaffari   }
1211864f1c2SLeila Ghaffari   problem->dm_scale = meter;
12277841947SLeila Ghaffari 
12377841947SLeila Ghaffari   // -- QFunction Context
124e6225c47SLeila Ghaffari   user->phys->stab            = stab;
12577841947SLeila Ghaffari   user->phys->euler_test      = euler_test;
12677841947SLeila Ghaffari   user->phys->implicit        = implicit;
12777841947SLeila Ghaffari   user->phys->has_curr_time   = has_curr_time;
12877841947SLeila Ghaffari   user->phys->has_neumann     = has_neumann;
129841e4c73SJed Brown   euler_ctx->curr_time        = 0.;
130841e4c73SJed Brown   euler_ctx->implicit         = implicit;
131841e4c73SJed Brown   euler_ctx->euler_test       = euler_test;
132841e4c73SJed Brown   euler_ctx->center[0]        = center[0];
133841e4c73SJed Brown   euler_ctx->center[1]        = center[1];
134841e4c73SJed Brown   euler_ctx->center[2]        = center[2];
135841e4c73SJed Brown   euler_ctx->vortex_strength  = vortex_strength;
136841e4c73SJed Brown   euler_ctx->c_tau            = c_tau;
137841e4c73SJed Brown   euler_ctx->mean_velocity[0] = mean_velocity[0];
138841e4c73SJed Brown   euler_ctx->mean_velocity[1] = mean_velocity[1];
139841e4c73SJed Brown   euler_ctx->mean_velocity[2] = mean_velocity[2];
140841e4c73SJed Brown   euler_ctx->stabilization    = stab;
14177841947SLeila Ghaffari 
142841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &euler_context);
1432b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(euler_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx);
1442b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(euler_context, CEED_MEM_HOST, FreeContextPetsc);
1452b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(euler_context, "solution time", offsetof(struct EulerContext_, curr_time), 1, "Physical time of the solution");
1462b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->ics.qfunction_context);
1472b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_vol_rhs.qfunction_context);
1482b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_vol_ifunction.qfunction_context);
1492b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_inflow.qfunction_context);
1502b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_outflow.qfunction_context);
15177841947SLeila Ghaffari   PetscFunctionReturn(0);
15277841947SLeila Ghaffari }
15377841947SLeila Ghaffari 
1542b730f8bSJeremy L Thompson PetscErrorCode PRINT_EULER_VORTEX(ProblemData *problem, AppCtx app_ctx) {
15577841947SLeila Ghaffari   MPI_Comm     comm = PETSC_COMM_WORLD;
156841e4c73SJed Brown   EulerContext euler_ctx;
15777841947SLeila Ghaffari 
158841e4c73SJed Brown   PetscFunctionBeginUser;
1592b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &euler_ctx);
1602b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
16177841947SLeila Ghaffari                         "  Problem:\n"
16277841947SLeila Ghaffari                         "    Problem Name                       : %s\n"
16377841947SLeila Ghaffari                         "    Test Case                          : %s\n"
164e6225c47SLeila Ghaffari                         "    Background Velocity                : %f,%f,%f\n"
165e6225c47SLeila Ghaffari                         "    Stabilization                      : %s\n",
1662b730f8bSJeremy L Thompson                         app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test], euler_ctx->mean_velocity[0], euler_ctx->mean_velocity[1],
1672b730f8bSJeremy L Thompson                         euler_ctx->mean_velocity[2], StabilizationTypes[euler_ctx->stabilization]));
16877841947SLeila Ghaffari 
169841e4c73SJed Brown   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &euler_ctx);
17077841947SLeila Ghaffari   PetscFunctionReturn(0);
17177841947SLeila Ghaffari }
172