xref: /libCEED/examples/fluids/problems/advection.c (revision 93639ffbf2bbfa4b6788194663945beabeb0bf2d)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Utility functions for setting up ADVECTION
1077841947SLeila Ghaffari 
112b730f8bSJeremy L Thompson #include "../qfunctions/advection.h"
122b730f8bSJeremy L Thompson 
1349aac155SJeremy L Thompson #include <ceed.h>
1449aac155SJeremy L Thompson #include <petscdm.h>
1549aac155SJeremy L Thompson 
1677841947SLeila Ghaffari #include "../navierstokes.h"
1777841947SLeila Ghaffari #include "../qfunctions/setupgeo.h"
18*93639ffbSJames Wright #include "../qfunctions/setupgeo2d.h"
1977841947SLeila Ghaffari 
2046de7363SJames Wright PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
2177841947SLeila Ghaffari   WindType             wind_type;
227b77ddfdSJames Wright   AdvectionICType      advectionic_type;
2377841947SLeila Ghaffari   BubbleContinuityType bubble_continuity_type;
2477841947SLeila Ghaffari   StabilizationType    stab;
2597baf651SJames Wright   SetupContextAdv      setup_context;
2677841947SLeila Ghaffari   User                 user = *(User *)ctx;
27a424bcd0SJames Wright   MPI_Comm             comm = user->comm;
28a424bcd0SJames Wright   Ceed                 ceed = user->ceed;
2977841947SLeila Ghaffari   PetscBool            implicit;
30841e4c73SJed Brown   AdvectionContext     advection_ctx;
31841e4c73SJed Brown   CeedQFunctionContext advection_context;
32*93639ffbSJames Wright   PetscInt             dim;
3377841947SLeila Ghaffari 
34841e4c73SJed Brown   PetscFunctionBeginUser;
352b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
362b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &advection_ctx));
37*93639ffbSJames Wright   PetscCall(DMGetDimension(dm, &dim));
3877841947SLeila Ghaffari 
3977841947SLeila Ghaffari   // ------------------------------------------------------
4077841947SLeila Ghaffari   //               SET UP ADVECTION
4177841947SLeila Ghaffari   // ------------------------------------------------------
42*93639ffbSJames Wright   switch (dim) {
43*93639ffbSJames Wright     case 2:
44*93639ffbSJames Wright       problem->dim                               = 2;
45*93639ffbSJames Wright       problem->q_data_size_vol                   = 5;
46*93639ffbSJames Wright       problem->q_data_size_sur                   = 3;
47*93639ffbSJames Wright       problem->setup_vol.qfunction               = Setup2d;
48*93639ffbSJames Wright       problem->setup_vol.qfunction_loc           = Setup2d_loc;
49*93639ffbSJames Wright       problem->setup_sur.qfunction               = SetupBoundary2d;
50*93639ffbSJames Wright       problem->setup_sur.qfunction_loc           = SetupBoundary2d_loc;
51*93639ffbSJames Wright       problem->ics.qfunction                     = ICsAdvection2d;
52*93639ffbSJames Wright       problem->ics.qfunction_loc                 = ICsAdvection2d_loc;
53*93639ffbSJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection2d;
54*93639ffbSJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection2d_loc;
55*93639ffbSJames Wright       problem->apply_vol_ifunction.qfunction     = IFunction_Advection2d;
56*93639ffbSJames Wright       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection2d_loc;
57*93639ffbSJames Wright       problem->apply_inflow.qfunction            = Advection2d_InOutFlow;
58*93639ffbSJames Wright       problem->apply_inflow.qfunction_loc        = Advection2d_InOutFlow_loc;
59*93639ffbSJames Wright       problem->non_zero_time                     = PETSC_TRUE;
60*93639ffbSJames Wright       problem->print_info                        = PRINT_ADVECTION;
61*93639ffbSJames Wright       break;
62*93639ffbSJames Wright     case 3:
6377841947SLeila Ghaffari       problem->dim                               = 3;
6477841947SLeila Ghaffari       problem->q_data_size_vol                   = 10;
65ba6664aeSJames Wright       problem->q_data_size_sur                   = 10;
6691e5af17SJed Brown       problem->setup_vol.qfunction               = Setup;
6791e5af17SJed Brown       problem->setup_vol.qfunction_loc           = Setup_loc;
6891e5af17SJed Brown       problem->setup_sur.qfunction               = SetupBoundary;
6991e5af17SJed Brown       problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
7091e5af17SJed Brown       problem->ics.qfunction                     = ICsAdvection;
7191e5af17SJed Brown       problem->ics.qfunction_loc                 = ICsAdvection_loc;
72*93639ffbSJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection;
73*93639ffbSJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection_loc;
7491e5af17SJed Brown       problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
7591e5af17SJed Brown       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
7691e5af17SJed Brown       problem->apply_inflow.qfunction            = Advection_InOutFlow;
7791e5af17SJed Brown       problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
7877841947SLeila Ghaffari       problem->non_zero_time                     = PETSC_FALSE;
7977841947SLeila Ghaffari       problem->print_info                        = PRINT_ADVECTION;
80*93639ffbSJames Wright       break;
81*93639ffbSJames Wright   }
8277841947SLeila Ghaffari 
8377841947SLeila Ghaffari   // ------------------------------------------------------
8477841947SLeila Ghaffari   //             Create the libCEED context
8577841947SLeila Ghaffari   // ------------------------------------------------------
8677841947SLeila Ghaffari   CeedScalar rc          = 1000.;  // m (Radius of bubble)
8777841947SLeila Ghaffari   CeedScalar CtauS       = 0.;     // dimensionless
88b767acfbSJames Wright   PetscBool  strong_form = PETSC_FALSE;
8977841947SLeila Ghaffari   CeedScalar E_wind      = 1.e6;        // J
9077841947SLeila Ghaffari   PetscReal  wind[3]     = {1., 0, 0};  // m/s
911864f1c2SLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
922b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
93*93639ffbSJames Wright   for (PetscInt i = 0; i < problem->dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
941864f1c2SLeila Ghaffari 
9577841947SLeila Ghaffari   // ------------------------------------------------------
9677841947SLeila Ghaffari   //             Create the PETSc context
9777841947SLeila Ghaffari   // ------------------------------------------------------
9877841947SLeila Ghaffari   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
9977841947SLeila Ghaffari   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
10077841947SLeila Ghaffari   PetscScalar second   = 1e-2;  // 1 second in scaled time units
10177841947SLeila Ghaffari   PetscScalar Joule;
10277841947SLeila Ghaffari 
10377841947SLeila Ghaffari   // ------------------------------------------------------
10477841947SLeila Ghaffari   //              Command line Options
10577841947SLeila Ghaffari   // ------------------------------------------------------
10667490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
10777841947SLeila Ghaffari   // -- Physics
1082b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
10977841947SLeila Ghaffari   PetscBool translation;
1102b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
1112b730f8bSJeremy L Thompson                              &translation));
11277841947SLeila Ghaffari   PetscInt  n = problem->dim;
11377841947SLeila Ghaffari   PetscBool user_wind;
1142b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
1152b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
116b767acfbSJames Wright   PetscCall(PetscOptionsBool("-strong_form", "Strong (true) or weak/integrated by parts (false) advection residual", NULL, strong_form, &strong_form,
117b767acfbSJames Wright                              NULL));
1182b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
1197b77ddfdSJames Wright   PetscCall(PetscOptionsEnum("-advection_ic_type", "Initial condition for Advection problem", NULL, AdvectionICTypes,
1207b77ddfdSJames Wright                              (PetscEnum)(advectionic_type = ADVECTIONIC_BUBBLE_SPHERE), (PetscEnum *)&advectionic_type, NULL));
121*93639ffbSJames Wright   bubble_continuity_type = problem->dim == 3 ? BUBBLE_CONTINUITY_SMOOTH : BUBBLE_CONTINUITY_COSINE;
122*93639ffbSJames Wright   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes, (PetscEnum)bubble_continuity_type,
123*93639ffbSJames Wright                              (PetscEnum *)&bubble_continuity_type, NULL));
1242b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
1252b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
12677841947SLeila Ghaffari 
12777841947SLeila Ghaffari   // -- Units
1282b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
12977841947SLeila Ghaffari   meter = fabs(meter);
1302b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
13177841947SLeila Ghaffari   kilogram = fabs(kilogram);
1322b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
13377841947SLeila Ghaffari   second = fabs(second);
13477841947SLeila Ghaffari 
13577841947SLeila Ghaffari   // -- Warnings
13677841947SLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
1372b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
13877841947SLeila Ghaffari   }
1397b77ddfdSJames Wright   if (wind_type == WIND_TRANSLATION && advectionic_type == ADVECTIONIC_BUBBLE_CYLINDER && wind[2] != 0.) {
14077841947SLeila Ghaffari     wind[2] = 0;
1417b77ddfdSJames Wright     PetscCall(
1427b77ddfdSJames Wright         PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -advection_ic_type cylinder\n"));
14377841947SLeila Ghaffari   }
14477841947SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
1452b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
14677841947SLeila Ghaffari   }
14777841947SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1482b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
14977841947SLeila Ghaffari   }
15077841947SLeila Ghaffari 
15167490bc6SJeremy L Thompson   PetscOptionsEnd();
15277841947SLeila Ghaffari 
15377841947SLeila Ghaffari   // ------------------------------------------------------
15477841947SLeila Ghaffari   //           Set up the PETSc context
15577841947SLeila Ghaffari   // ------------------------------------------------------
15677841947SLeila Ghaffari   // -- Define derived units
15777841947SLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
15877841947SLeila Ghaffari 
15977841947SLeila Ghaffari   user->units->meter    = meter;
16077841947SLeila Ghaffari   user->units->kilogram = kilogram;
16177841947SLeila Ghaffari   user->units->second   = second;
16277841947SLeila Ghaffari   user->units->Joule    = Joule;
16377841947SLeila Ghaffari 
16477841947SLeila Ghaffari   // ------------------------------------------------------
16577841947SLeila Ghaffari   //           Set up the libCEED context
16677841947SLeila Ghaffari   // ------------------------------------------------------
16777841947SLeila Ghaffari   // -- Scale variables to desired units
16877841947SLeila Ghaffari   E_wind *= Joule;
16977841947SLeila Ghaffari   rc = fabs(rc) * meter;
170*93639ffbSJames Wright   for (PetscInt i = 0; i < problem->dim; i++) {
1711864f1c2SLeila Ghaffari     wind[i] *= (meter / second);
1721864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1731864f1c2SLeila Ghaffari   }
1741864f1c2SLeila Ghaffari   problem->dm_scale = meter;
17577841947SLeila Ghaffari 
17677841947SLeila Ghaffari   // -- Setup Context
17777841947SLeila Ghaffari   setup_context->rc                     = rc;
1781864f1c2SLeila Ghaffari   setup_context->lx                     = domain_size[0];
1791864f1c2SLeila Ghaffari   setup_context->ly                     = domain_size[1];
180*93639ffbSJames Wright   setup_context->lz                     = problem->dim == 3 ? domain_size[2] : 0.;
18177841947SLeila Ghaffari   setup_context->wind[0]                = wind[0];
18277841947SLeila Ghaffari   setup_context->wind[1]                = wind[1];
183*93639ffbSJames Wright   setup_context->wind[2]                = problem->dim == 3 ? wind[2] : 0.;
18477841947SLeila Ghaffari   setup_context->wind_type              = wind_type;
1857b77ddfdSJames Wright   setup_context->initial_condition_type = advectionic_type;
18677841947SLeila Ghaffari   setup_context->bubble_continuity_type = bubble_continuity_type;
18777841947SLeila Ghaffari   setup_context->time                   = 0;
18877841947SLeila Ghaffari 
18977841947SLeila Ghaffari   // -- QFunction Context
19077841947SLeila Ghaffari   user->phys->implicit         = implicit;
191841e4c73SJed Brown   advection_ctx->CtauS         = CtauS;
192841e4c73SJed Brown   advection_ctx->E_wind        = E_wind;
193841e4c73SJed Brown   advection_ctx->implicit      = implicit;
194841e4c73SJed Brown   advection_ctx->strong_form   = strong_form;
195841e4c73SJed Brown   advection_ctx->stabilization = stab;
19677841947SLeila Ghaffari 
197a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context));
198a424bcd0SJames Wright   PetscCallCeed(ceed,
199a424bcd0SJames Wright                 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
200a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc));
20177841947SLeila Ghaffari 
202a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &advection_context));
203a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx));
204a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc));
205841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
206a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context));
207a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context));
208ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
20977841947SLeila Ghaffari }
21077841947SLeila Ghaffari 
211367c849eSJames Wright PetscErrorCode PRINT_ADVECTION(User user, ProblemData *problem, AppCtx app_ctx) {
212367c849eSJames Wright   MPI_Comm         comm = user->comm;
213a424bcd0SJames Wright   Ceed             ceed = user->ceed;
21497baf651SJames Wright   SetupContextAdv  setup_ctx;
215841e4c73SJed Brown   AdvectionContext advection_ctx;
21677841947SLeila Ghaffari 
217841e4c73SJed Brown   PetscFunctionBeginUser;
218a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx));
219a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx));
2202b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
22177841947SLeila Ghaffari                         "  Problem:\n"
22277841947SLeila Ghaffari                         "    Problem Name                       : %s\n"
22377841947SLeila Ghaffari                         "    Stabilization                      : %s\n"
2247b77ddfdSJames Wright                         "    Initial Condition Type             : %s\n"
22577841947SLeila Ghaffari                         "    Bubble Continuity                  : %s\n"
22677841947SLeila Ghaffari                         "    Wind Type                          : %s\n",
2277b77ddfdSJames Wright                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], AdvectionICTypes[setup_ctx->initial_condition_type],
2287b77ddfdSJames Wright                         BubbleContinuityTypes[setup_ctx->bubble_continuity_type], WindTypes[setup_ctx->wind_type]));
22977841947SLeila Ghaffari 
230841e4c73SJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
231*93639ffbSJames Wright     switch (problem->dim) {
232*93639ffbSJames Wright       case 2:
233*93639ffbSJames Wright         PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1]));
234*93639ffbSJames Wright         break;
235*93639ffbSJames Wright       case 3:
236*93639ffbSJames Wright         PetscCall(
237*93639ffbSJames Wright             PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
238*93639ffbSJames Wright         break;
239*93639ffbSJames Wright     }
24077841947SLeila Ghaffari   }
241a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx));
242a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx));
243ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
24477841947SLeila Ghaffari }
245