xref: /libCEED/examples/fluids/problems/eulervortex.c (revision 11436a05c428446f6e40fe7eea93d96698ed3bc1)
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 "../navierstokes.h"
1277841947SLeila Ghaffari #include "../qfunctions/setupgeo.h"
1377841947SLeila Ghaffari #include "../qfunctions/eulervortex.h"
1477841947SLeila Ghaffari 
151864f1c2SLeila Ghaffari PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *setup_ctx,
1677841947SLeila Ghaffari                                void *ctx) {
1777841947SLeila Ghaffari   EulerTestType     euler_test;
1877841947SLeila Ghaffari   SetupContext      setup_context = *(SetupContext *)setup_ctx;
1977841947SLeila Ghaffari   User              user = *(User *)ctx;
20e6225c47SLeila Ghaffari   StabilizationType stab;
2177841947SLeila Ghaffari   MPI_Comm          comm = PETSC_COMM_WORLD;
2277841947SLeila Ghaffari   PetscBool         implicit;
2377841947SLeila Ghaffari   PetscBool         has_curr_time = PETSC_TRUE;
2477841947SLeila Ghaffari   PetscBool         has_neumann = PETSC_TRUE;
2577841947SLeila Ghaffari   PetscInt          ierr;
2677841947SLeila Ghaffari   PetscFunctionBeginUser;
2777841947SLeila Ghaffari 
2877841947SLeila Ghaffari   ierr = PetscCalloc1(1, &user->phys->euler_ctx); CHKERRQ(ierr);
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;
3577841947SLeila Ghaffari   problem->q_data_size_sur         = 4;
3677841947SLeila Ghaffari   problem->setup_vol               = Setup;
3777841947SLeila Ghaffari   problem->setup_vol_loc           = Setup_loc;
3877841947SLeila Ghaffari   problem->setup_sur               = SetupBoundary;
3977841947SLeila Ghaffari   problem->setup_sur_loc           = SetupBoundary_loc;
4077841947SLeila Ghaffari   problem->ics                     = ICsEuler;
4177841947SLeila Ghaffari   problem->ics_loc                 = ICsEuler_loc;
4277841947SLeila Ghaffari   problem->apply_vol_rhs           = Euler;
4377841947SLeila Ghaffari   problem->apply_vol_rhs_loc       = Euler_loc;
4477841947SLeila Ghaffari   problem->apply_vol_ifunction     = IFunction_Euler;
4577841947SLeila Ghaffari   problem->apply_vol_ifunction_loc = IFunction_Euler_loc;
462fe7aee7SLeila Ghaffari   problem->apply_inflow            = TravelingVortex_Inflow;
472fe7aee7SLeila Ghaffari   problem->apply_inflow_loc        = TravelingVortex_Inflow_loc;
482fe7aee7SLeila Ghaffari   problem->apply_outflow           = Euler_Outflow;
492fe7aee7SLeila Ghaffari   problem->apply_outflow_loc       = Euler_Outflow_loc;
5077841947SLeila Ghaffari   problem->bc                      = Exact_Euler;
51d0b732dbSLeila Ghaffari   problem->setup_ctx               = SetupContext_EULER_VORTEX;
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];
641864f1c2SLeila Ghaffari   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
651864f1c2SLeila Ghaffari   for (int 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
7877841947SLeila Ghaffari   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
7977841947SLeila Ghaffari                             NULL, vortex_strength, &vortex_strength, NULL);
8077841947SLeila Ghaffari   CHKERRQ(ierr);
8177841947SLeila Ghaffari   PetscInt n = problem->dim;
8277841947SLeila Ghaffari   PetscBool user_velocity;
8377841947SLeila Ghaffari   ierr = PetscOptionsRealArray("-mean_velocity", "Background velocity vector",
8477841947SLeila Ghaffari                                NULL, mean_velocity, &n, &user_velocity);
8577841947SLeila Ghaffari   CHKERRQ(ierr);
861864f1c2SLeila Ghaffari   for (int i=0; i<3; i++) center[i] = .5*domain_size[i];
8777841947SLeila Ghaffari   n = problem->dim;
8877841947SLeila Ghaffari   ierr = PetscOptionsRealArray("-center", "Location of vortex center",
8977841947SLeila Ghaffari                                NULL, center, &n, NULL); CHKERRQ(ierr);
9077841947SLeila Ghaffari   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
9177841947SLeila Ghaffari                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
9277841947SLeila Ghaffari   CHKERRQ(ierr);
9377841947SLeila Ghaffari   ierr = PetscOptionsEnum("-euler_test", "Euler test option", NULL,
94bc7bbd5dSLeila Ghaffari                           EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
9577841947SLeila Ghaffari                           (PetscEnum *)&euler_test, NULL); CHKERRQ(ierr);
96e6225c47SLeila Ghaffari   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
97e6225c47SLeila Ghaffari                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
98e6225c47SLeila Ghaffari                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
99932417b3SJed Brown   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
100932417b3SJed Brown                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
10177841947SLeila Ghaffari   // -- Units
10277841947SLeila Ghaffari   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
10377841947SLeila Ghaffari                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
10477841947SLeila Ghaffari   meter = fabs(meter);
10577841947SLeila Ghaffari   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
10677841947SLeila Ghaffari                             NULL, second, &second, NULL); CHKERRQ(ierr);
10777841947SLeila Ghaffari   second = fabs(second);
10877841947SLeila Ghaffari 
10977841947SLeila Ghaffari   // -- Warnings
110e6225c47SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
111e6225c47SLeila Ghaffari     ierr = PetscPrintf(comm,
112e6225c47SLeila Ghaffari                        "Warning! Use -stab supg only with -implicit\n");
113e6225c47SLeila Ghaffari     CHKERRQ(ierr);
114e6225c47SLeila Ghaffari   }
11577841947SLeila Ghaffari   if (user_velocity && (euler_test == EULER_TEST_1
11677841947SLeila Ghaffari                         || euler_test == EULER_TEST_3)) {
11777841947SLeila Ghaffari     ierr = PetscPrintf(comm,
11877841947SLeila Ghaffari                        "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n");
11977841947SLeila Ghaffari     CHKERRQ(ierr);
12077841947SLeila Ghaffari   }
12177841947SLeila Ghaffari 
12267490bc6SJeremy L Thompson   PetscOptionsEnd();
12377841947SLeila Ghaffari 
12477841947SLeila Ghaffari   // ------------------------------------------------------
12577841947SLeila Ghaffari   //           Set up the PETSc context
12677841947SLeila Ghaffari   // ------------------------------------------------------
12777841947SLeila Ghaffari   user->units->meter  = meter;
12877841947SLeila Ghaffari   user->units->second = second;
12977841947SLeila Ghaffari 
13077841947SLeila Ghaffari   // ------------------------------------------------------
13177841947SLeila Ghaffari   //           Set up the libCEED context
13277841947SLeila Ghaffari   // ------------------------------------------------------
13377841947SLeila Ghaffari   // -- Scale variables to desired units
134e535bb46SLeila Ghaffari   for (int i=0; i<3; i++) {
135e535bb46SLeila Ghaffari     center[i] *= meter;
1361864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1371864f1c2SLeila Ghaffari     mean_velocity[i] *= (meter/second);
138e535bb46SLeila Ghaffari   }
1391864f1c2SLeila Ghaffari   problem->dm_scale = meter;
14077841947SLeila Ghaffari 
14177841947SLeila Ghaffari   // -- Setup Context
1421864f1c2SLeila Ghaffari   setup_context->lx        = domain_size[0];
1431864f1c2SLeila Ghaffari   setup_context->ly        = domain_size[1];
1441864f1c2SLeila Ghaffari   setup_context->lz        = domain_size[2];
14577841947SLeila Ghaffari   setup_context->center[0] = center[0];
14677841947SLeila Ghaffari   setup_context->center[1] = center[1];
14777841947SLeila Ghaffari   setup_context->center[2] = center[2];
14877841947SLeila Ghaffari   setup_context->time      = 0;
14977841947SLeila Ghaffari 
15077841947SLeila Ghaffari   // -- QFunction Context
151e6225c47SLeila Ghaffari   user->phys->stab                        = stab;
15277841947SLeila Ghaffari   user->phys->euler_test                  = euler_test;
15377841947SLeila Ghaffari   user->phys->implicit                    = implicit;
15477841947SLeila Ghaffari   user->phys->has_curr_time               = has_curr_time;
15577841947SLeila Ghaffari   user->phys->has_neumann                 = has_neumann;
15677841947SLeila Ghaffari   user->phys->euler_ctx->curr_time        = 0.;
15777841947SLeila Ghaffari   user->phys->euler_ctx->implicit         = implicit;
15877841947SLeila Ghaffari   user->phys->euler_ctx->euler_test       = euler_test;
15977841947SLeila Ghaffari   user->phys->euler_ctx->center[0]        = center[0];
16077841947SLeila Ghaffari   user->phys->euler_ctx->center[1]        = center[1];
16177841947SLeila Ghaffari   user->phys->euler_ctx->center[2]        = center[2];
16277841947SLeila Ghaffari   user->phys->euler_ctx->vortex_strength  = vortex_strength;
163932417b3SJed Brown   user->phys->euler_ctx->c_tau            = c_tau;
16477841947SLeila Ghaffari   user->phys->euler_ctx->mean_velocity[0] = mean_velocity[0];
16577841947SLeila Ghaffari   user->phys->euler_ctx->mean_velocity[1] = mean_velocity[1];
16677841947SLeila Ghaffari   user->phys->euler_ctx->mean_velocity[2] = mean_velocity[2];
167e6225c47SLeila Ghaffari   user->phys->euler_ctx->stabilization    = stab;
16877841947SLeila Ghaffari 
16977841947SLeila Ghaffari   PetscFunctionReturn(0);
17077841947SLeila Ghaffari }
17177841947SLeila Ghaffari 
172d0b732dbSLeila Ghaffari PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data,
173d0b732dbSLeila Ghaffari     AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
174d0b732dbSLeila Ghaffari   PetscFunctionBeginUser;
175d0b732dbSLeila Ghaffari   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
176d0b732dbSLeila Ghaffari   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
177d0b732dbSLeila Ghaffari                               CEED_USE_POINTER,
178d0b732dbSLeila Ghaffari                               sizeof(*setup_ctx), setup_ctx);
179d0b732dbSLeila Ghaffari   CeedQFunctionContextCreate(ceed, &ceed_data->euler_context);
180d0b732dbSLeila Ghaffari   CeedQFunctionContextSetData(ceed_data->euler_context, CEED_MEM_HOST,
181d0b732dbSLeila Ghaffari                               CEED_USE_POINTER,
182d0b732dbSLeila Ghaffari                               sizeof(*phys->euler_ctx), phys->euler_ctx);
183*11436a05SJames Wright   CeedQFunctionContextRegisterDouble(ceed_data->euler_context, "solution time",
184*11436a05SJames Wright                                      offsetof(struct EulerContext_, curr_time), 1, "Phyiscal time of the solution");
185*11436a05SJames Wright 
186d0b732dbSLeila Ghaffari   if (ceed_data->qf_ics)
187d0b732dbSLeila Ghaffari     CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->euler_context);
188e6225c47SLeila Ghaffari   if (ceed_data->qf_rhs_vol)
189e6225c47SLeila Ghaffari     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->euler_context);
190e6225c47SLeila Ghaffari   if (ceed_data->qf_ifunction_vol)
191e6225c47SLeila Ghaffari     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->euler_context);
1922fe7aee7SLeila Ghaffari   if (ceed_data->qf_apply_inflow)
1932fe7aee7SLeila Ghaffari     CeedQFunctionSetContext(ceed_data->qf_apply_inflow, ceed_data->euler_context);
1942fe7aee7SLeila Ghaffari   if (ceed_data->qf_apply_outflow)
1952fe7aee7SLeila Ghaffari     CeedQFunctionSetContext(ceed_data->qf_apply_outflow, ceed_data->euler_context);
19677841947SLeila Ghaffari   PetscFunctionReturn(0);
19777841947SLeila Ghaffari }
19877841947SLeila Ghaffari 
19977841947SLeila Ghaffari PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx,
20077841947SLeila Ghaffari                                   AppCtx app_ctx) {
20177841947SLeila Ghaffari   MPI_Comm       comm = PETSC_COMM_WORLD;
20277841947SLeila Ghaffari   PetscErrorCode ierr;
20377841947SLeila Ghaffari   PetscFunctionBeginUser;
20477841947SLeila Ghaffari 
20577841947SLeila Ghaffari   ierr = PetscPrintf(comm,
20677841947SLeila Ghaffari                      "  Problem:\n"
20777841947SLeila Ghaffari                      "    Problem Name                       : %s\n"
20877841947SLeila Ghaffari                      "    Test Case                          : %s\n"
209e6225c47SLeila Ghaffari                      "    Background Velocity                : %f,%f,%f\n"
210e6225c47SLeila Ghaffari                      "    Stabilization                      : %s\n",
21177841947SLeila Ghaffari                      app_ctx->problem_name, EulerTestTypes[phys->euler_test],
21277841947SLeila Ghaffari                      phys->euler_ctx->mean_velocity[0],
213e6225c47SLeila Ghaffari                      phys->euler_ctx->mean_velocity[1],
214e6225c47SLeila Ghaffari                      phys->euler_ctx->mean_velocity[2],
215e6225c47SLeila Ghaffari                      StabilizationTypes[phys->stab]); CHKERRQ(ierr);
21677841947SLeila Ghaffari 
21777841947SLeila Ghaffari   PetscFunctionReturn(0);
21877841947SLeila Ghaffari }
219