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