xref: /libCEED/examples/fluids/problems/eulervortex.c (revision 367c849e96f2729f89d873e43adcb60b6350e603)
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 
1349aac155SJeremy L Thompson #include <ceed.h>
1449aac155SJeremy L Thompson #include <petscdm.h>
1549aac155SJeremy L Thompson 
162b730f8bSJeremy L Thompson #include "../navierstokes.h"
172b730f8bSJeremy L Thompson #include "../qfunctions/setupgeo.h"
186ef2784eSLeila Ghaffari 
1946de7363SJames Wright PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
2077841947SLeila Ghaffari   EulerTestType        euler_test;
2177841947SLeila Ghaffari   User                 user = *(User *)ctx;
22e6225c47SLeila Ghaffari   StabilizationType    stab;
2377841947SLeila Ghaffari   MPI_Comm             comm = PETSC_COMM_WORLD;
2477841947SLeila Ghaffari   PetscBool            implicit;
2577841947SLeila Ghaffari   PetscBool            has_curr_time = PETSC_TRUE;
2677841947SLeila Ghaffari   PetscBool            has_neumann   = PETSC_TRUE;
27841e4c73SJed Brown   EulerContext         euler_ctx;
28841e4c73SJed Brown   CeedQFunctionContext euler_context;
2977841947SLeila Ghaffari 
30841e4c73SJed Brown   PetscFunctionBeginUser;
312b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &euler_ctx));
3277841947SLeila Ghaffari 
3377841947SLeila Ghaffari   // ------------------------------------------------------
3477841947SLeila Ghaffari   //               SET UP DENSITY_CURRENT
3577841947SLeila Ghaffari   // ------------------------------------------------------
3677841947SLeila Ghaffari   problem->dim                               = 3;
3777841947SLeila Ghaffari   problem->q_data_size_vol                   = 10;
38ba6664aeSJames Wright   problem->q_data_size_sur                   = 10;
3991e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
4091e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
4191e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
4291e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
4391e5af17SJed Brown   problem->ics.qfunction                     = ICsEuler;
4491e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsEuler_loc;
4591e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = Euler;
4691e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Euler_loc;
4791e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Euler;
4891e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Euler_loc;
4991e5af17SJed Brown   problem->apply_inflow.qfunction            = TravelingVortex_Inflow;
5091e5af17SJed Brown   problem->apply_inflow.qfunction_loc        = TravelingVortex_Inflow_loc;
5191e5af17SJed Brown   problem->apply_outflow.qfunction           = Euler_Outflow;
5291e5af17SJed Brown   problem->apply_outflow.qfunction_loc       = Euler_Outflow_loc;
5377841947SLeila Ghaffari   problem->non_zero_time                     = PETSC_TRUE;
5477841947SLeila Ghaffari   problem->print_info                        = PRINT_EULER_VORTEX;
5577841947SLeila Ghaffari 
5677841947SLeila Ghaffari   // ------------------------------------------------------
5777841947SLeila Ghaffari   //             Create the libCEED context
5877841947SLeila Ghaffari   // ------------------------------------------------------
5977841947SLeila Ghaffari   CeedScalar vortex_strength = 5.;   // -
60504dc8e0SLeila Ghaffari   CeedScalar c_tau           = 0.5;  // -
61932417b3SJed Brown   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
62e535bb46SLeila Ghaffari   PetscReal center[3],                 // m
63e535bb46SLeila Ghaffari       mean_velocity[3] = {1., 1., 0};  // m/s
641864f1c2SLeila Ghaffari   PetscReal domain_min[3], domain_max[3], domain_size[3];
652b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
66ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
6777841947SLeila Ghaffari 
6877841947SLeila Ghaffari   // ------------------------------------------------------
6977841947SLeila Ghaffari   //             Create the PETSc context
7077841947SLeila Ghaffari   // ------------------------------------------------------
7177841947SLeila Ghaffari   PetscScalar meter  = 1e-2;  // 1 meter in scaled length units
7277841947SLeila Ghaffari   PetscScalar second = 1e-2;  // 1 second in scaled time units
7377841947SLeila Ghaffari 
7477841947SLeila Ghaffari   // ------------------------------------------------------
7577841947SLeila Ghaffari   //              Command line Options
7677841947SLeila Ghaffari   // ------------------------------------------------------
7767490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
7877841947SLeila Ghaffari   // -- Physics
792b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-vortex_strength", "Strength of Vortex", NULL, vortex_strength, &vortex_strength, NULL));
8077841947SLeila Ghaffari   PetscInt  n = problem->dim;
8177841947SLeila Ghaffari   PetscBool user_velocity;
822b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-mean_velocity", "Background velocity vector", NULL, mean_velocity, &n, &user_velocity));
83ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) center[i] = .5 * domain_size[i];
8477841947SLeila Ghaffari   n = problem->dim;
852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-center", "Location of vortex center", NULL, center, &n, NULL));
862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
872b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-euler_test", "Euler test option", NULL, EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
882b730f8bSJeremy L Thompson                              (PetscEnum *)&euler_test, NULL));
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
902b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
9177841947SLeila Ghaffari   // -- Units
922b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
9377841947SLeila Ghaffari   meter = fabs(meter);
942b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
9577841947SLeila Ghaffari   second = fabs(second);
9677841947SLeila Ghaffari 
9777841947SLeila Ghaffari   // -- Warnings
98e6225c47SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
992b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
100e6225c47SLeila Ghaffari   }
1012b730f8bSJeremy L Thompson   if (user_velocity && (euler_test == EULER_TEST_1 || euler_test == EULER_TEST_3)) {
1022b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n"));
10377841947SLeila Ghaffari   }
10477841947SLeila Ghaffari 
10567490bc6SJeremy L Thompson   PetscOptionsEnd();
10677841947SLeila Ghaffari 
10777841947SLeila Ghaffari   // ------------------------------------------------------
10877841947SLeila Ghaffari   //           Set up the PETSc context
10977841947SLeila Ghaffari   // ------------------------------------------------------
11077841947SLeila Ghaffari   user->units->meter  = meter;
11177841947SLeila Ghaffari   user->units->second = second;
11277841947SLeila Ghaffari 
11377841947SLeila Ghaffari   // ------------------------------------------------------
11477841947SLeila Ghaffari   //           Set up the libCEED context
11577841947SLeila Ghaffari   // ------------------------------------------------------
11677841947SLeila Ghaffari   // -- Scale variables to desired units
117ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) {
118e535bb46SLeila Ghaffari     center[i] *= meter;
1191864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1201864f1c2SLeila Ghaffari     mean_velocity[i] *= (meter / second);
121e535bb46SLeila Ghaffari   }
1221864f1c2SLeila Ghaffari   problem->dm_scale = meter;
12377841947SLeila Ghaffari 
12477841947SLeila Ghaffari   // -- QFunction Context
125e6225c47SLeila Ghaffari   user->phys->stab            = stab;
12677841947SLeila Ghaffari   user->phys->euler_test      = euler_test;
12777841947SLeila Ghaffari   user->phys->implicit        = implicit;
12877841947SLeila Ghaffari   user->phys->has_curr_time   = has_curr_time;
12977841947SLeila Ghaffari   user->phys->has_neumann     = has_neumann;
130841e4c73SJed Brown   euler_ctx->curr_time        = 0.;
131841e4c73SJed Brown   euler_ctx->implicit         = implicit;
132841e4c73SJed Brown   euler_ctx->euler_test       = euler_test;
133841e4c73SJed Brown   euler_ctx->center[0]        = center[0];
134841e4c73SJed Brown   euler_ctx->center[1]        = center[1];
135841e4c73SJed Brown   euler_ctx->center[2]        = center[2];
136841e4c73SJed Brown   euler_ctx->vortex_strength  = vortex_strength;
137841e4c73SJed Brown   euler_ctx->c_tau            = c_tau;
138841e4c73SJed Brown   euler_ctx->mean_velocity[0] = mean_velocity[0];
139841e4c73SJed Brown   euler_ctx->mean_velocity[1] = mean_velocity[1];
140841e4c73SJed Brown   euler_ctx->mean_velocity[2] = mean_velocity[2];
141841e4c73SJed Brown   euler_ctx->stabilization    = stab;
14277841947SLeila Ghaffari 
143841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &euler_context);
1442b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(euler_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*euler_ctx), euler_ctx);
1452b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(euler_context, CEED_MEM_HOST, FreeContextPetsc);
1462b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(euler_context, "solution time", offsetof(struct EulerContext_, curr_time), 1, "Physical time of the solution");
1472b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->ics.qfunction_context);
1482b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_vol_rhs.qfunction_context);
1492b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_vol_ifunction.qfunction_context);
1502b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_inflow.qfunction_context);
1512b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(euler_context, &problem->apply_outflow.qfunction_context);
152ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
15377841947SLeila Ghaffari }
15477841947SLeila Ghaffari 
155*367c849eSJames Wright PetscErrorCode PRINT_EULER_VORTEX(User user, ProblemData *problem, AppCtx app_ctx) {
156*367c849eSJames Wright   MPI_Comm     comm = user->comm;
157841e4c73SJed Brown   EulerContext euler_ctx;
15877841947SLeila Ghaffari 
159841e4c73SJed Brown   PetscFunctionBeginUser;
1602b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &euler_ctx);
1612b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
16277841947SLeila Ghaffari                         "  Problem:\n"
16377841947SLeila Ghaffari                         "    Problem Name                       : %s\n"
16477841947SLeila Ghaffari                         "    Test Case                          : %s\n"
165e6225c47SLeila Ghaffari                         "    Background Velocity                : %f,%f,%f\n"
166e6225c47SLeila Ghaffari                         "    Stabilization                      : %s\n",
1672b730f8bSJeremy L Thompson                         app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test], euler_ctx->mean_velocity[0], euler_ctx->mean_velocity[1],
1682b730f8bSJeremy L Thompson                         euler_ctx->mean_velocity[2], StabilizationTypes[euler_ctx->stabilization]));
16977841947SLeila Ghaffari 
170841e4c73SJed Brown   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &euler_ctx);
171ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
17277841947SLeila Ghaffari }
173