xref: /libCEED/examples/fluids/problems/eulervortex.c (revision 318af0d1a760765edef6872663e190e11f82cd8e)
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 
24 PetscErrorCode NS_EULER_VORTEX(ProblemData *problem, void *setup_ctx,
25                                void *ctx) {
26   EulerTestType     euler_test;
27   SetupContext      setup_context = *(SetupContext *)setup_ctx;
28   User              user = *(User *)ctx;
29   StabilizationType stab;
30   MPI_Comm          comm = PETSC_COMM_WORLD;
31   PetscBool         implicit;
32   PetscBool         has_curr_time = PETSC_TRUE;
33   PetscBool         has_neumann = PETSC_TRUE;
34   PetscInt          ierr;
35   PetscFunctionBeginUser;
36 
37   ierr = PetscCalloc1(1, &user->phys->euler_ctx); CHKERRQ(ierr);
38 
39   // ------------------------------------------------------
40   //               SET UP DENSITY_CURRENT
41   // ------------------------------------------------------
42   problem->dim                     = 3;
43   problem->q_data_size_vol         = 10;
44   problem->q_data_size_sur         = 4;
45   problem->setup_vol               = Setup;
46   problem->setup_vol_loc           = Setup_loc;
47   problem->setup_sur               = SetupBoundary;
48   problem->setup_sur_loc           = SetupBoundary_loc;
49   problem->ics                     = ICsEuler;
50   problem->ics_loc                 = ICsEuler_loc;
51   problem->apply_vol_rhs           = Euler;
52   problem->apply_vol_rhs_loc       = Euler_loc;
53   problem->apply_vol_ifunction     = IFunction_Euler;
54   problem->apply_vol_ifunction_loc = IFunction_Euler_loc;
55   problem->apply_sur               = Euler_Sur;
56   problem->apply_sur_loc           = Euler_Sur_loc;
57   problem->bc                      = Exact_Euler;
58   problem->setup_ctx               = SetupContext_EULER_VORTEX;
59   problem->bc_func                 = BC_EULER_VORTEX;
60   problem->non_zero_time           = PETSC_TRUE;
61   problem->print_info              = PRINT_EULER_VORTEX;
62 
63   // ------------------------------------------------------
64   //             Create the libCEED context
65   // ------------------------------------------------------
66   CeedScalar vortex_strength = 5.;          // -
67   CeedScalar c_tau           = 0.5;         // -
68   // c_tau = 0.5 is reported as "optimal" in Hughes et al 2010
69   PetscScalar lx             = 1000.;       // m
70   PetscScalar ly             = 1000.;       // m
71   PetscScalar lz             = 1.;          // m
72   PetscReal center[3],                      // m
73             mean_velocity[3] = {1., 1., 0}; // m/s
74 
75   // ------------------------------------------------------
76   //             Create the PETSc context
77   // ------------------------------------------------------
78   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
79   PetscScalar second   = 1e-2; // 1 second in scaled time units
80 
81   // ------------------------------------------------------
82   //              Command line Options
83   // ------------------------------------------------------
84   ierr = PetscOptionsBegin(comm, NULL, "Options for EULER_VORTEX problem",
85                            NULL); CHKERRQ(ierr);
86   // -- Physics
87   ierr = PetscOptionsScalar("-vortex_strength", "Strength of Vortex",
88                             NULL, vortex_strength, &vortex_strength, NULL);
89   CHKERRQ(ierr);
90   PetscInt n = problem->dim;
91   PetscBool user_velocity;
92   ierr = PetscOptionsRealArray("-mean_velocity", "Background velocity vector",
93                                NULL, mean_velocity, &n, &user_velocity);
94   CHKERRQ(ierr);
95   ierr = PetscOptionsScalar("-lx", "Length scale in x direction",
96                             NULL, lx, &lx, NULL); CHKERRQ(ierr);
97   ierr = PetscOptionsScalar("-ly", "Length scale in y direction",
98                             NULL, ly, &ly, NULL); CHKERRQ(ierr);
99   ierr = PetscOptionsScalar("-lz", "Length scale in z direction",
100                             NULL, lz, &lz, NULL); CHKERRQ(ierr);
101   n = problem->dim;
102   center[0] = 0.5 * lx;
103   center[1] = 0.5 * ly;
104   center[2] = 0.5 * lz;
105   ierr = PetscOptionsRealArray("-center", "Location of vortex center",
106                                NULL, center, &n, NULL); CHKERRQ(ierr);
107   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
108                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
109   CHKERRQ(ierr);
110   ierr = PetscOptionsEnum("-euler_test", "Euler test option", NULL,
111                           EulerTestTypes, (PetscEnum)(euler_test = EULER_TEST_ISENTROPIC_VORTEX),
112                           (PetscEnum *)&euler_test, NULL); CHKERRQ(ierr);
113   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
114                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
115                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
116   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
117                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
118   // -- Units
119   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
120                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
121   meter = fabs(meter);
122   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
123                             NULL, second, &second, NULL); CHKERRQ(ierr);
124   second = fabs(second);
125 
126   // -- Warnings
127   if (stab == STAB_SUPG && !implicit) {
128     ierr = PetscPrintf(comm,
129                        "Warning! Use -stab supg only with -implicit\n");
130     CHKERRQ(ierr);
131   }
132   if (user_velocity && (euler_test == EULER_TEST_1
133                         || euler_test == EULER_TEST_3)) {
134     ierr = PetscPrintf(comm,
135                        "Warning! Background velocity vector for -euler_test t1 and -euler_test t3 is (0,0,0)\n");
136     CHKERRQ(ierr);
137   }
138 
139   ierr = PetscOptionsEnd(); CHKERRQ(ierr);
140 
141   // ------------------------------------------------------
142   //           Set up the PETSc context
143   // ------------------------------------------------------
144   user->units->meter  = meter;
145   user->units->second = second;
146 
147   // ------------------------------------------------------
148   //           Set up the libCEED context
149   // ------------------------------------------------------
150   // -- Scale variables to desired units
151   lx = fabs(lx) * meter;
152   ly = fabs(ly) * meter;
153   lz = fabs(lz) * meter;
154   for (int i=0; i<3; i++) {
155     center[i] *= meter;
156     mean_velocity[i] = mean_velocity[i] * (meter/second);
157   }
158 
159   // -- Setup Context
160   setup_context->lx        = lx;
161   setup_context->ly        = ly;
162   setup_context->lz        = lz;
163   setup_context->center[0] = center[0];
164   setup_context->center[1] = center[1];
165   setup_context->center[2] = center[2];
166   setup_context->time      = 0;
167 
168   // -- QFunction Context
169   user->phys->stab                        = stab;
170   user->phys->euler_test                  = euler_test;
171   user->phys->implicit                    = implicit;
172   user->phys->has_curr_time               = has_curr_time;
173   user->phys->has_neumann                 = has_neumann;
174   user->phys->euler_ctx->curr_time        = 0.;
175   user->phys->euler_ctx->implicit         = implicit;
176   user->phys->euler_ctx->euler_test       = euler_test;
177   user->phys->euler_ctx->center[0]        = center[0];
178   user->phys->euler_ctx->center[1]        = center[1];
179   user->phys->euler_ctx->center[2]        = center[2];
180   user->phys->euler_ctx->vortex_strength  = vortex_strength;
181   user->phys->euler_ctx->c_tau            = c_tau;
182   user->phys->euler_ctx->mean_velocity[0] = mean_velocity[0];
183   user->phys->euler_ctx->mean_velocity[1] = mean_velocity[1];
184   user->phys->euler_ctx->mean_velocity[2] = mean_velocity[2];
185   user->phys->euler_ctx->stabilization    = stab;
186 
187   PetscFunctionReturn(0);
188 }
189 
190 PetscErrorCode SetupContext_EULER_VORTEX(Ceed ceed, CeedData ceed_data,
191     AppCtx app_ctx, SetupContext setup_ctx, Physics phys) {
192   PetscFunctionBeginUser;
193 
194   CeedQFunctionContextCreate(ceed, &ceed_data->setup_context);
195   CeedQFunctionContextSetData(ceed_data->setup_context, CEED_MEM_HOST,
196                               CEED_USE_POINTER,
197                               sizeof(*setup_ctx), setup_ctx);
198   CeedQFunctionContextCreate(ceed, &ceed_data->euler_context);
199   CeedQFunctionContextSetData(ceed_data->euler_context, CEED_MEM_HOST,
200                               CEED_USE_POINTER,
201                               sizeof(*phys->euler_ctx), phys->euler_ctx);
202   if (ceed_data->qf_ics)
203     CeedQFunctionSetContext(ceed_data->qf_ics, ceed_data->euler_context);
204   if (ceed_data->qf_apply_sur)
205     CeedQFunctionSetContext(ceed_data->qf_apply_sur, ceed_data->euler_context);
206   if (ceed_data->qf_rhs_vol)
207     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, ceed_data->euler_context);
208   if (ceed_data->qf_ifunction_vol)
209     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, ceed_data->euler_context);
210 
211   PetscFunctionReturn(0);
212 }
213 
214 PetscErrorCode BC_EULER_VORTEX(DM dm, SimpleBC bc, Physics phys,
215                                void *setup_ctx) {
216   PetscErrorCode ierr;
217   PetscFunctionBeginUser;
218 
219   // Define boundary conditions
220   bc->num_slip[2] = 2; bc->slips[2][0] = 1; bc->slips[2][1] = 2;
221 
222   // Set boundary conditions
223   DMLabel label;
224   ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr);
225   PetscInt comps[1] = {3};
226   ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, "Face Sets",
227                        bc->num_slip[2], bc->slips[2], 0, 1, comps,
228                        (void(*)(void))NULL, NULL, setup_ctx, NULL);
229   CHKERRQ(ierr);
230 
231   PetscFunctionReturn(0);
232 }
233 
234 PetscErrorCode PRINT_EULER_VORTEX(Physics phys, SetupContext setup_ctx,
235                                   AppCtx app_ctx) {
236   MPI_Comm       comm = PETSC_COMM_WORLD;
237   PetscErrorCode ierr;
238   PetscFunctionBeginUser;
239 
240   ierr = PetscPrintf(comm,
241                      "  Problem:\n"
242                      "    Problem Name                       : %s\n"
243                      "    Test Case                          : %s\n"
244                      "    Background Velocity                : %f,%f,%f\n"
245                      "    Stabilization                      : %s\n",
246                      app_ctx->problem_name, EulerTestTypes[phys->euler_test],
247                      phys->euler_ctx->mean_velocity[0],
248                      phys->euler_ctx->mean_velocity[1],
249                      phys->euler_ctx->mean_velocity[2],
250                      StabilizationTypes[phys->stab]); CHKERRQ(ierr);
251 
252   PetscFunctionReturn(0);
253 }
254