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