xref: /libCEED/examples/fluids/problems/eulervortex.c (revision 496f2382898c7791278a056456625d09206fcd2a)
1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3 // reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 /// @file
18 /// Utility functions for setting up EULER_VORTEX
19 
20 #include "../navierstokes.h"
21 #include "../qfunctions/setupgeo.h"
22 #include "../qfunctions/eulervortex.h"
23 #include "../qfunctions/euler-outflow.h"
24 
25 PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, DM dm, void *setup_ctx,
26                                void *ctx) {
27   EulerTestType     euler_test;
28   SetupContext      setup_context = *(SetupContext *)setup_ctx;
29   User              user = *(User *)ctx;
30   StabilizationType stab;
31   MPI_Comm          comm = PETSC_COMM_WORLD;
32   PetscBool         implicit;
33   PetscBool         has_curr_time = PETSC_TRUE;
34   PetscBool         has_neumann = PETSC_TRUE;
35   PetscInt          ierr;
36   PetscFunctionBeginUser;
37 
38   ierr = PetscCalloc1(1, &user->phys->euler_ctx); CHKERRQ(ierr);
39 
40   // ------------------------------------------------------
41   //               SET UP DENSITY_CURRENT
42   // ------------------------------------------------------
43   problem->dim                     = 3;
44   problem->q_data_size_vol         = 10;
45   problem->q_data_size_sur         = 4;
46   problem->setup_vol               = Setup;
47   problem->setup_vol_loc           = Setup_loc;
48   problem->setup_sur               = SetupBoundary;
49   problem->setup_sur_loc           = SetupBoundary_loc;
50   problem->ics                     = ICsEuler;
51   problem->ics_loc                 = ICsEuler_loc;
52   problem->apply_vol_rhs           = Euler;
53   problem->apply_vol_rhs_loc       = Euler_loc;
54   problem->apply_vol_ifunction     = IFunction_Euler;
55   problem->apply_vol_ifunction_loc = IFunction_Euler_loc;
56   problem->apply_inflow            = TravelingVortex_Inflow;
57   problem->apply_inflow_loc        = TravelingVortex_Inflow_loc;
58   problem->apply_outflow           = Euler_Outflow;
59   problem->apply_outflow_loc       = Euler_Outflow_loc;
60   problem->bc                      = Exact_Euler;
61   problem->setup_ctx               = SetupContext_EULER_VORTEX;
62   problem->non_zero_time           = PETSC_TRUE;
63   problem->print_info              = PRINT_EULER_VORTEX;
64 
65   // ------------------------------------------------------
66   //             Create the libCEED context
67   // ------------------------------------------------------
68   CeedScalar vortex_strength = 5.;          // -
69   CeedScalar c_tau           = 0.5;         // -
70   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
71   PetscReal center[3],                      // m
72             mean_velocity[3] = {1., 1., 0}; // m/s
73   PetscReal domain_min[3], domain_max[3], domain_size[3];
74   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
75   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
76 
77   // ------------------------------------------------------
78   //             Create the PETSc context
79   // ------------------------------------------------------
80   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
81   PetscScalar second   = 1e-2; // 1 second in scaled time units
82 
83   // ------------------------------------------------------
84   //              Command line Options
85   // ------------------------------------------------------
86   ierr = PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem",
87                            NULL); CHKERRQ(ierr);
88   // -- Physics
89   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
90                             NULL, vortex_strength, &vortex_strength, NULL);
91   CHKERRQ(ierr);
92   PetscInt n = problem->dim;
93   PetscBool user_velocity;
94   ierr = PetscOptionsRealArray("-mean_velocity", "Background velocity vector",
95                                NULL, mean_velocity, &n, &user_velocity);
96   CHKERRQ(ierr);
97   for (int i=0; i<3; i++) center[i] = .5*domain_size[i];
98   n = problem->dim;
99   ierr = PetscOptionsRealArray("-center", "Location of vortex center",
100                                NULL, center, &n, NULL); CHKERRQ(ierr);
101   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
102                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
103   CHKERRQ(ierr);
104   ierr = PetscOptionsEnum("-euler_test", "Euler test option", NULL,
105                           EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
106                           (PetscEnum *)&euler_test, NULL); CHKERRQ(ierr);
107   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
108                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
109                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
110   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
111                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
112   // -- Units
113   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
114                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
115   meter = fabs(meter);
116   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
117                             NULL, second, &second, NULL); CHKERRQ(ierr);
118   second = fabs(second);
119 
120   // -- Warnings
121   if (stab == STAB_SUPG && !implicit) {
122     ierr = PetscPrintf(comm,
123                        "Warning! Use -stab supg only with -implicit\n");
124     CHKERRQ(ierr);
125   }
126   if (user_velocity && (euler_test == EULER_TEST_1
127                         || euler_test == EULER_TEST_3)) {
128     ierr = PetscPrintf(comm,
129                        "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n");
130     CHKERRQ(ierr);
131   }
132 
133   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
134 
135   // ------------------------------------------------------
136   //           Set up the PETSc context
137   // ------------------------------------------------------
138   user->units->meter  = meter;
139   user->units->second = second;
140 
141   // ------------------------------------------------------
142   //           Set up the libCEED context
143   // ------------------------------------------------------
144   // -- Scale variables to desired units
145   for (int i=0; i<3; i++) {
146     center[i] *= meter;
147     domain_size[i] *= meter;
148     mean_velocity[i] *= (meter/second);
149   }
150   problem->dm_scale = meter;
151 
152   // -- Setup Context
153   setup_context->lx        = domain_size[0];
154   setup_context->ly        = domain_size[1];
155   setup_context->lz        = domain_size[2];
156   setup_context->center[0] = center[0];
157   setup_context->center[1] = center[1];
158   setup_context->center[2] = center[2];
159   setup_context->time      = 0;
160 
161   // -- QFunction Context
162   user->phys->stab                        = stab;
163   user->phys->euler_test                  = euler_test;
164   user->phys->implicit                    = implicit;
165   user->phys->has_curr_time               = has_curr_time;
166   user->phys->has_neumann                 = has_neumann;
167   user->phys->euler_ctx->curr_time        = 0.;
168   user->phys->euler_ctx->implicit         = implicit;
169   user->phys->euler_ctx->euler_test       = euler_test;
170   user->phys->euler_ctx->center[0]        = center[0];
171   user->phys->euler_ctx->center[1]        = center[1];
172   user->phys->euler_ctx->center[2]        = center[2];
173   user->phys->euler_ctx->vortex_strength  = vortex_strength;
174   user->phys->euler_ctx->c_tau            = c_tau;
175   user->phys->euler_ctx->mean_velocity[0] = mean_velocity[0];
176   user->phys->euler_ctx->mean_velocity[1] = mean_velocity[1];
177   user->phys->euler_ctx->mean_velocity[2] = mean_velocity[2];
178   user->phys->euler_ctx->stabilization    = stab;
179 
180   PetscFunctionReturn(0);
181 }
182 
183 PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data,
184     AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
185   PetscFunctionBeginUser;
186   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
187   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
188                               CEED_USE_POINTER,
189                               sizeof(*setup_ctx), setup_ctx);
190   CeedQFunctionContextCreate(ceed, &ceed_data->euler_context);
191   CeedQFunctionContextSetData(ceed_data->euler_context, CEED_MEM_HOST,
192                               CEED_USE_POINTER,
193                               sizeof(*phys->euler_ctx), phys->euler_ctx);
194   if (ceed_data->qf_ics)
195     CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->euler_context);
196   if (ceed_data->qf_rhs_vol)
197     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->euler_context);
198   if (ceed_data->qf_ifunction_vol)
199     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->euler_context);
200   if (ceed_data->qf_apply_inflow)
201     CeedQFunctionSetContext(ceed_data->qf_apply_inflow, ceed_data->euler_context);
202   if (ceed_data->qf_apply_outflow)
203     CeedQFunctionSetContext(ceed_data->qf_apply_outflow, ceed_data->euler_context);
204   PetscFunctionReturn(0);
205 }
206 
207 PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx,
208                                   AppCtx app_ctx) {
209   MPI_Comm       comm = PETSC_COMM_WORLD;
210   PetscErrorCode ierr;
211   PetscFunctionBeginUser;
212 
213   ierr = PetscPrintf(comm,
214                      "  Problem:\n"
215                      "    Problem Name                       : %s\n"
216                      "    Test Case                          : %s\n"
217                      "    Background Velocity                : %f,%f,%f\n"
218                      "    Stabilization                      : %s\n",
219                      app_ctx->problem_name, EulerTestTypes[phys->euler_test],
220                      phys->euler_ctx->mean_velocity[0],
221                      phys->euler_ctx->mean_velocity[1],
222                      phys->euler_ctx->mean_velocity[2],
223                      StabilizationTypes[phys->stab]); CHKERRQ(ierr);
224 
225   PetscFunctionReturn(0);
226 }
227