xref: /honee/problems/eulervortex.c (revision 40ccd21c438f7fc3cfb2895be71a121bfd363a4b)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Utility functions for setting up EULER_VORTEX
10a515125bSLeila Ghaffari 
11a515125bSLeila Ghaffari #include "../navierstokes.h"
12a515125bSLeila Ghaffari #include "../qfunctions/setupgeo.h"
13a515125bSLeila Ghaffari #include "../qfunctions/eulervortex.h"
14a515125bSLeila Ghaffari 
1505a512bdSLeila Ghaffari PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *setup_ctx,
16a515125bSLeila Ghaffari                                void *ctx) {
17a515125bSLeila Ghaffari   EulerTestType     euler_test;
18a515125bSLeila Ghaffari   SetupContext      setup_context = *(SetupContext *)setup_ctx;
19a515125bSLeila Ghaffari   User              user = *(User *)ctx;
20139613f2SLeila Ghaffari   StabilizationType stab;
21a515125bSLeila Ghaffari   MPI_Comm          comm = PETSC_COMM_WORLD;
22a515125bSLeila Ghaffari   PetscBool         implicit;
23a515125bSLeila Ghaffari   PetscBool         has_curr_time = PETSC_TRUE;
24a515125bSLeila Ghaffari   PetscBool         has_neumann = PETSC_TRUE;
25a515125bSLeila Ghaffari   PetscInt          ierr;
2615a3537eSJed Brown   EulerContext      euler_ctx;
2715a3537eSJed Brown   CeedQFunctionContext euler_context;
28a515125bSLeila Ghaffari 
2915a3537eSJed Brown   PetscFunctionBeginUser;
3015a3537eSJed Brown   ierr = PetscCalloc1(1, &euler_ctx); CHKERRQ(ierr);
31a515125bSLeila Ghaffari 
32a515125bSLeila Ghaffari   // ------------------------------------------------------
33a515125bSLeila Ghaffari   //               SET UP DENSITY_CURRENT
34a515125bSLeila Ghaffari   // ------------------------------------------------------
35a515125bSLeila Ghaffari   problem->dim                               = 3;
36a515125bSLeila Ghaffari   problem->q_data_size_vol                   = 10;
37a515125bSLeila Ghaffari   problem->q_data_size_sur                   = 4;
389785fe93SJed Brown   problem->setup_vol.qfunction               = Setup;
399785fe93SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
409785fe93SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
419785fe93SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
429785fe93SJed Brown   problem->ics.qfunction                     = ICsEuler;
439785fe93SJed Brown   problem->ics.qfunction_loc                 = ICsEuler_loc;
449785fe93SJed Brown   problem->apply_vol_rhs.qfunction           = Euler;
459785fe93SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Euler_loc;
469785fe93SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Euler;
479785fe93SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Euler_loc;
489785fe93SJed Brown   problem->apply_inflow.qfunction            = TravelingVortex_Inflow;
499785fe93SJed Brown   problem->apply_inflow.qfunction_loc        = TravelingVortex_Inflow_loc;
509785fe93SJed Brown   problem->apply_outflow.qfunction           = Euler_Outflow;
519785fe93SJed Brown   problem->apply_outflow.qfunction_loc       = Euler_Outflow_loc;
52a515125bSLeila Ghaffari   problem->bc                                = Exact_Euler;
53c848b8b7SJed Brown   problem->bc_ctx                            = setup_ctx;
54a515125bSLeila Ghaffari   problem->non_zero_time                     = PETSC_TRUE;
55a515125bSLeila Ghaffari   problem->print_info                        = PRINT_EULER_VORTEX;
56a515125bSLeila Ghaffari 
57a515125bSLeila Ghaffari   // ------------------------------------------------------
58a515125bSLeila Ghaffari   //             Create the libCEED context
59a515125bSLeila Ghaffari   // ------------------------------------------------------
60a515125bSLeila Ghaffari   CeedScalar vortex_strength = 5.;          // -
61f821ee77SLeila Ghaffari   CeedScalar c_tau           = 0.5;         // -
62d8a22b9eSJed Brown   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
633b451b28SLeila Ghaffari   PetscReal center[3],                      // m
643b451b28SLeila Ghaffari             mean_velocity[3] = {1., 1., 0}; // m/s
6505a512bdSLeila Ghaffari   PetscReal domain_min[3], domain_max[3], domain_size[3];
6605a512bdSLeila Ghaffari   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
6705a512bdSLeila Ghaffari   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
68a515125bSLeila Ghaffari 
69a515125bSLeila Ghaffari   // ------------------------------------------------------
70a515125bSLeila Ghaffari   //             Create the PETSc context
71a515125bSLeila Ghaffari   // ------------------------------------------------------
72a515125bSLeila Ghaffari   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
73a515125bSLeila Ghaffari   PetscScalar second   = 1e-2; // 1 second in scaled time units
74a515125bSLeila Ghaffari 
75a515125bSLeila Ghaffari   // ------------------------------------------------------
76a515125bSLeila Ghaffari   //              Command line Options
77a515125bSLeila Ghaffari   // ------------------------------------------------------
781485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem", NULL);
79a515125bSLeila Ghaffari   // -- Physics
80a515125bSLeila Ghaffari   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
81a515125bSLeila Ghaffari                             NULL, vortex_strength, &vortex_strength, NULL);
82a515125bSLeila Ghaffari   CHKERRQ(ierr);
83a515125bSLeila Ghaffari   PetscInt n = problem->dim;
84a515125bSLeila Ghaffari   PetscBool user_velocity;
85a515125bSLeila Ghaffari   ierr = PetscOptionsRealArray("-mean_velocity", "Background velocity vector",
86a515125bSLeila Ghaffari                                NULL, mean_velocity, &n, &user_velocity);
87a515125bSLeila Ghaffari   CHKERRQ(ierr);
8805a512bdSLeila Ghaffari   for (int i=0; i<3; i++) center[i] = .5*domain_size[i];
89a515125bSLeila Ghaffari   n = problem->dim;
90a515125bSLeila Ghaffari   ierr = PetscOptionsRealArray("-center", "Location of vortex center",
91a515125bSLeila Ghaffari                                NULL, center, &n, NULL); CHKERRQ(ierr);
92a515125bSLeila Ghaffari   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
93a515125bSLeila Ghaffari                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
94a515125bSLeila Ghaffari   CHKERRQ(ierr);
95a515125bSLeila Ghaffari   ierr = PetscOptionsEnum("-euler_test", "Euler test option", NULL,
96575f8106SLeila Ghaffari                           EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
97a515125bSLeila Ghaffari                           (PetscEnum *)&euler_test, NULL); CHKERRQ(ierr);
98139613f2SLeila Ghaffari   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
99139613f2SLeila Ghaffari                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
100139613f2SLeila Ghaffari                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
101d8a22b9eSJed Brown   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
102d8a22b9eSJed Brown                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
103a515125bSLeila Ghaffari   // -- Units
104a515125bSLeila Ghaffari   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
105a515125bSLeila Ghaffari                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
106a515125bSLeila Ghaffari   meter = fabs(meter);
107a515125bSLeila Ghaffari   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
108a515125bSLeila Ghaffari                             NULL, second, &second, NULL); CHKERRQ(ierr);
109a515125bSLeila Ghaffari   second = fabs(second);
110a515125bSLeila Ghaffari 
111a515125bSLeila Ghaffari   // -- Warnings
112139613f2SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
113139613f2SLeila Ghaffari     ierr = PetscPrintf(comm,
114139613f2SLeila Ghaffari                        "Warning! Use -stab supg only with -implicit\n");
115139613f2SLeila Ghaffari     CHKERRQ(ierr);
116139613f2SLeila Ghaffari   }
117a515125bSLeila Ghaffari   if (user_velocity && (euler_test == EULER_TEST_1
118a515125bSLeila Ghaffari                         || euler_test == EULER_TEST_3)) {
119a515125bSLeila Ghaffari     ierr = PetscPrintf(comm,
120a515125bSLeila Ghaffari                        "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n");
121a515125bSLeila Ghaffari     CHKERRQ(ierr);
122a515125bSLeila Ghaffari   }
123a515125bSLeila Ghaffari 
1241485969bSJeremy L Thompson   PetscOptionsEnd();
125a515125bSLeila Ghaffari 
126a515125bSLeila Ghaffari   // ------------------------------------------------------
127a515125bSLeila Ghaffari   //           Set up the PETSc context
128a515125bSLeila Ghaffari   // ------------------------------------------------------
129a515125bSLeila Ghaffari   user->units->meter  = meter;
130a515125bSLeila Ghaffari   user->units->second = second;
131a515125bSLeila Ghaffari 
132a515125bSLeila Ghaffari   // ------------------------------------------------------
133a515125bSLeila Ghaffari   //           Set up the libCEED context
134a515125bSLeila Ghaffari   // ------------------------------------------------------
135a515125bSLeila Ghaffari   // -- Scale variables to desired units
1363b451b28SLeila Ghaffari   for (int i=0; i<3; i++) {
1373b451b28SLeila Ghaffari     center[i] *= meter;
13805a512bdSLeila Ghaffari     domain_size[i] *= meter;
13905a512bdSLeila Ghaffari     mean_velocity[i] *= (meter/second);
1403b451b28SLeila Ghaffari   }
14105a512bdSLeila Ghaffari   problem->dm_scale = meter;
142a515125bSLeila Ghaffari 
143a515125bSLeila Ghaffari   // -- Setup Context
14405a512bdSLeila Ghaffari   setup_context->lx        = domain_size[0];
14505a512bdSLeila Ghaffari   setup_context->ly        = domain_size[1];
14605a512bdSLeila Ghaffari   setup_context->lz        = domain_size[2];
147a515125bSLeila Ghaffari   setup_context->center[0] = center[0];
148a515125bSLeila Ghaffari   setup_context->center[1] = center[1];
149a515125bSLeila Ghaffari   setup_context->center[2] = center[2];
150a515125bSLeila Ghaffari   setup_context->time      = 0;
151a515125bSLeila Ghaffari 
152a515125bSLeila Ghaffari   // -- QFunction Context
153139613f2SLeila Ghaffari   user->phys->stab                        = stab;
154a515125bSLeila Ghaffari   user->phys->euler_test                  = euler_test;
155a515125bSLeila Ghaffari   user->phys->implicit                    = implicit;
156a515125bSLeila Ghaffari   user->phys->has_curr_time               = has_curr_time;
157a515125bSLeila Ghaffari   user->phys->has_neumann                 = has_neumann;
15815a3537eSJed Brown   euler_ctx->curr_time        = 0.;
15915a3537eSJed Brown   euler_ctx->implicit         = implicit;
16015a3537eSJed Brown   euler_ctx->euler_test       = euler_test;
16115a3537eSJed Brown   euler_ctx->center[0]        = center[0];
16215a3537eSJed Brown   euler_ctx->center[1]        = center[1];
16315a3537eSJed Brown   euler_ctx->center[2]        = center[2];
16415a3537eSJed Brown   euler_ctx->vortex_strength  = vortex_strength;
16515a3537eSJed Brown   euler_ctx->c_tau            = c_tau;
16615a3537eSJed Brown   euler_ctx->mean_velocity[0] = mean_velocity[0];
16715a3537eSJed Brown   euler_ctx->mean_velocity[1] = mean_velocity[1];
16815a3537eSJed Brown   euler_ctx->mean_velocity[2] = mean_velocity[2];
16915a3537eSJed Brown   euler_ctx->stabilization    = stab;
170a515125bSLeila Ghaffari 
17115a3537eSJed Brown   CeedQFunctionContextCreate(user->ceed, &euler_context);
17215a3537eSJed Brown   CeedQFunctionContextSetData(euler_context, CEED_MEM_HOST, CEED_USE_POINTER,
17315a3537eSJed Brown                               sizeof(*euler_ctx), euler_ctx);
17415a3537eSJed Brown   CeedQFunctionContextSetDataDestroy(euler_context, CEED_MEM_HOST,
17515a3537eSJed Brown                                      FreeContextPetsc);
17615a3537eSJed Brown   CeedQFunctionContextRegisterDouble(euler_context, "solution time",
17792ada588SJames Wright                                      offsetof(struct EulerContext_, curr_time), 1, "Phyiscal time of the solution");
17815a3537eSJed Brown   problem->ics.qfunction_context = euler_context;
17915a3537eSJed Brown   CeedQFunctionContextReferenceCopy(euler_context,
18015a3537eSJed Brown                                     &problem->apply_vol_rhs.qfunction_context);
18115a3537eSJed Brown   CeedQFunctionContextReferenceCopy(euler_context,
18215a3537eSJed Brown                                     &problem->apply_vol_ifunction.qfunction_context);
18315a3537eSJed Brown   CeedQFunctionContextReferenceCopy(euler_context,
18415a3537eSJed Brown                                     &problem->apply_inflow.qfunction_context);
18515a3537eSJed Brown   CeedQFunctionContextReferenceCopy(euler_context,
18615a3537eSJed Brown                                     &problem->apply_outflow.qfunction_context);
187a515125bSLeila Ghaffari   PetscFunctionReturn(0);
188a515125bSLeila Ghaffari }
189a515125bSLeila Ghaffari 
190*40ccd21cSJed Brown PetscErrorCode PRINT_EULER_VORTEX(ProblemData *problem,
191a515125bSLeila Ghaffari                                   AppCtx app_ctx) {
192a515125bSLeila Ghaffari   MPI_Comm       comm = PETSC_COMM_WORLD;
193a515125bSLeila Ghaffari   PetscErrorCode ierr;
19415a3537eSJed Brown   EulerContext   euler_ctx;
195a515125bSLeila Ghaffari 
19615a3537eSJed Brown   PetscFunctionBeginUser;
19715a3537eSJed Brown   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST,
19815a3537eSJed Brown                               &euler_ctx);
199a515125bSLeila Ghaffari   ierr = PetscPrintf(comm,
200a515125bSLeila Ghaffari                      "  Problem:\n"
201a515125bSLeila Ghaffari                      "    Problem Name                       : %s\n"
202a515125bSLeila Ghaffari                      "    Test Case                          : %s\n"
203139613f2SLeila Ghaffari                      "    Background Velocity                : %f,%f,%f\n"
204139613f2SLeila Ghaffari                      "    Stabilization                      : %s\n",
20515a3537eSJed Brown                      app_ctx->problem_name, EulerTestTypes[euler_ctx->euler_test],
20615a3537eSJed Brown                      euler_ctx->mean_velocity[0],
20715a3537eSJed Brown                      euler_ctx->mean_velocity[1],
20815a3537eSJed Brown                      euler_ctx->mean_velocity[2],
20915a3537eSJed Brown                      StabilizationTypes[euler_ctx->stabilization]); CHKERRQ(ierr);
210a515125bSLeila Ghaffari 
21115a3537eSJed Brown   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &euler_ctx);
212a515125bSLeila Ghaffari   PetscFunctionReturn(0);
213a515125bSLeila Ghaffari }
214