xref: /libCEED/examples/fluids/problems/advection.c (revision b18328c48bac463522bc9df72304dbd8dd7879bd)
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"
1893639ffbSJames 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;
25*b18328c4SJames Wright   StabilizationTauType stab_tau;
2697baf651SJames Wright   SetupContextAdv      setup_context;
2777841947SLeila Ghaffari   User                 user = *(User *)ctx;
28a424bcd0SJames Wright   MPI_Comm             comm = user->comm;
29a424bcd0SJames Wright   Ceed                 ceed = user->ceed;
3077841947SLeila Ghaffari   PetscBool            implicit;
31841e4c73SJed Brown   AdvectionContext     advection_ctx;
32841e4c73SJed Brown   CeedQFunctionContext advection_context;
3393639ffbSJames Wright   PetscInt             dim;
3477841947SLeila Ghaffari 
35841e4c73SJed Brown   PetscFunctionBeginUser;
362b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
372b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &advection_ctx));
3893639ffbSJames Wright   PetscCall(DMGetDimension(dm, &dim));
3977841947SLeila Ghaffari 
4077841947SLeila Ghaffari   // ------------------------------------------------------
4177841947SLeila Ghaffari   //               SET UP ADVECTION
4277841947SLeila Ghaffari   // ------------------------------------------------------
4393639ffbSJames Wright   switch (dim) {
4493639ffbSJames Wright     case 2:
4593639ffbSJames Wright       problem->dim                               = 2;
4693639ffbSJames Wright       problem->q_data_size_vol                   = 5;
4793639ffbSJames Wright       problem->q_data_size_sur                   = 3;
4893639ffbSJames Wright       problem->setup_vol.qfunction               = Setup2d;
4993639ffbSJames Wright       problem->setup_vol.qfunction_loc           = Setup2d_loc;
5093639ffbSJames Wright       problem->setup_sur.qfunction               = SetupBoundary2d;
5193639ffbSJames Wright       problem->setup_sur.qfunction_loc           = SetupBoundary2d_loc;
5293639ffbSJames Wright       problem->ics.qfunction                     = ICsAdvection2d;
5393639ffbSJames Wright       problem->ics.qfunction_loc                 = ICsAdvection2d_loc;
5493639ffbSJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection2d;
5593639ffbSJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection2d_loc;
5693639ffbSJames Wright       problem->apply_vol_ifunction.qfunction     = IFunction_Advection2d;
5793639ffbSJames Wright       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection2d_loc;
5893639ffbSJames Wright       problem->apply_inflow.qfunction            = Advection2d_InOutFlow;
5993639ffbSJames Wright       problem->apply_inflow.qfunction_loc        = Advection2d_InOutFlow_loc;
6093639ffbSJames Wright       problem->non_zero_time                     = PETSC_TRUE;
6193639ffbSJames Wright       problem->print_info                        = PRINT_ADVECTION;
6293639ffbSJames Wright       break;
6393639ffbSJames Wright     case 3:
6477841947SLeila Ghaffari       problem->dim                               = 3;
6577841947SLeila Ghaffari       problem->q_data_size_vol                   = 10;
66ba6664aeSJames Wright       problem->q_data_size_sur                   = 10;
6791e5af17SJed Brown       problem->setup_vol.qfunction               = Setup;
6891e5af17SJed Brown       problem->setup_vol.qfunction_loc           = Setup_loc;
6991e5af17SJed Brown       problem->setup_sur.qfunction               = SetupBoundary;
7091e5af17SJed Brown       problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
7191e5af17SJed Brown       problem->ics.qfunction                     = ICsAdvection;
7291e5af17SJed Brown       problem->ics.qfunction_loc                 = ICsAdvection_loc;
7393639ffbSJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection;
7493639ffbSJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection_loc;
7591e5af17SJed Brown       problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
7691e5af17SJed Brown       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
7791e5af17SJed Brown       problem->apply_inflow.qfunction            = Advection_InOutFlow;
7891e5af17SJed Brown       problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
7977841947SLeila Ghaffari       problem->non_zero_time                     = PETSC_FALSE;
8077841947SLeila Ghaffari       problem->print_info                        = PRINT_ADVECTION;
8193639ffbSJames Wright       break;
8293639ffbSJames Wright   }
8377841947SLeila Ghaffari 
8477841947SLeila Ghaffari   // ------------------------------------------------------
8577841947SLeila Ghaffari   //             Create the libCEED context
8677841947SLeila Ghaffari   // ------------------------------------------------------
8777841947SLeila Ghaffari   CeedScalar rc          = 1000.;  // m (Radius of bubble)
8877841947SLeila Ghaffari   CeedScalar CtauS       = 0.;     // dimensionless
89b767acfbSJames Wright   PetscBool  strong_form = PETSC_FALSE;
9077841947SLeila Ghaffari   CeedScalar E_wind      = 1.e6;  // J
91*b18328c4SJames Wright   CeedScalar Ctau_a      = PetscPowScalarInt(user->app_ctx->degree, 2);
92*b18328c4SJames Wright   CeedScalar Ctau_t      = 0.;
9377841947SLeila Ghaffari   PetscReal  wind[3]     = {1., 0, 0};  // m/s
941864f1c2SLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
952b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
9693639ffbSJames Wright   for (PetscInt i = 0; i < problem->dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
971864f1c2SLeila Ghaffari 
9877841947SLeila Ghaffari   // ------------------------------------------------------
9977841947SLeila Ghaffari   //             Create the PETSc context
10077841947SLeila Ghaffari   // ------------------------------------------------------
10177841947SLeila Ghaffari   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
10277841947SLeila Ghaffari   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
10377841947SLeila Ghaffari   PetscScalar second   = 1e-2;  // 1 second in scaled time units
10477841947SLeila Ghaffari   PetscScalar Joule;
10577841947SLeila Ghaffari 
10677841947SLeila Ghaffari   // ------------------------------------------------------
10777841947SLeila Ghaffari   //              Command line Options
10877841947SLeila Ghaffari   // ------------------------------------------------------
10967490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
11077841947SLeila Ghaffari   // -- Physics
1112b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
11277841947SLeila Ghaffari   PetscBool translation;
1132b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
1142b730f8bSJeremy L Thompson                              &translation));
11577841947SLeila Ghaffari   PetscInt  n = problem->dim;
11677841947SLeila Ghaffari   PetscBool user_wind;
1172b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
1182b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
119b767acfbSJames Wright   PetscCall(PetscOptionsBool("-strong_form", "Strong (true) or weak/integrated by parts (false) advection residual", NULL, strong_form, &strong_form,
120b767acfbSJames Wright                              NULL));
1212b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
1227b77ddfdSJames Wright   PetscCall(PetscOptionsEnum("-advection_ic_type", "Initial condition for Advection problem", NULL, AdvectionICTypes,
1237b77ddfdSJames Wright                              (PetscEnum)(advectionic_type = ADVECTIONIC_BUBBLE_SPHERE), (PetscEnum *)&advectionic_type, NULL));
12493639ffbSJames Wright   bubble_continuity_type = problem->dim == 3 ? BUBBLE_CONTINUITY_SMOOTH : BUBBLE_CONTINUITY_COSINE;
12593639ffbSJames Wright   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes, (PetscEnum)bubble_continuity_type,
12693639ffbSJames Wright                              (PetscEnum *)&bubble_continuity_type, NULL));
1272b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
128*b18328c4SJames Wright   PetscCall(PetscOptionsEnum("-stab_tau", "Stabilization constant, tau", NULL, StabilizationTauTypes, (PetscEnum)(stab_tau = STAB_TAU_CTAU),
129*b18328c4SJames Wright                              (PetscEnum *)&stab_tau, NULL));
130*b18328c4SJames Wright   PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL));
131*b18328c4SJames Wright   PetscCall(PetscOptionsScalar("-Ctau_a", "Coefficient for the stabilization ", NULL, Ctau_a, &Ctau_a, NULL));
1322b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
13377841947SLeila Ghaffari 
13477841947SLeila Ghaffari   // -- Units
1352b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
13677841947SLeila Ghaffari   meter = fabs(meter);
1372b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
13877841947SLeila Ghaffari   kilogram = fabs(kilogram);
1392b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
14077841947SLeila Ghaffari   second = fabs(second);
14177841947SLeila Ghaffari 
14277841947SLeila Ghaffari   // -- Warnings
14377841947SLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
1442b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
14577841947SLeila Ghaffari   }
1467b77ddfdSJames Wright   if (wind_type == WIND_TRANSLATION && advectionic_type == ADVECTIONIC_BUBBLE_CYLINDER && wind[2] != 0.) {
14777841947SLeila Ghaffari     wind[2] = 0;
1487b77ddfdSJames Wright     PetscCall(
1497b77ddfdSJames Wright         PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -advection_ic_type cylinder\n"));
15077841947SLeila Ghaffari   }
15177841947SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
1522b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
15377841947SLeila Ghaffari   }
15477841947SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1552b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
15677841947SLeila Ghaffari   }
15777841947SLeila Ghaffari 
15867490bc6SJeremy L Thompson   PetscOptionsEnd();
15977841947SLeila Ghaffari 
16077841947SLeila Ghaffari   // ------------------------------------------------------
16177841947SLeila Ghaffari   //           Set up the PETSc context
16277841947SLeila Ghaffari   // ------------------------------------------------------
16377841947SLeila Ghaffari   // -- Define derived units
16477841947SLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
16577841947SLeila Ghaffari 
16677841947SLeila Ghaffari   user->units->meter    = meter;
16777841947SLeila Ghaffari   user->units->kilogram = kilogram;
16877841947SLeila Ghaffari   user->units->second   = second;
16977841947SLeila Ghaffari   user->units->Joule    = Joule;
17077841947SLeila Ghaffari 
17177841947SLeila Ghaffari   // ------------------------------------------------------
17277841947SLeila Ghaffari   //           Set up the libCEED context
17377841947SLeila Ghaffari   // ------------------------------------------------------
17477841947SLeila Ghaffari   // -- Scale variables to desired units
17577841947SLeila Ghaffari   E_wind *= Joule;
17677841947SLeila Ghaffari   rc = fabs(rc) * meter;
17793639ffbSJames Wright   for (PetscInt i = 0; i < problem->dim; i++) {
1781864f1c2SLeila Ghaffari     wind[i] *= (meter / second);
1791864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1801864f1c2SLeila Ghaffari   }
1811864f1c2SLeila Ghaffari   problem->dm_scale = meter;
18277841947SLeila Ghaffari 
18377841947SLeila Ghaffari   // -- Setup Context
18477841947SLeila Ghaffari   setup_context->rc                     = rc;
1851864f1c2SLeila Ghaffari   setup_context->lx                     = domain_size[0];
1861864f1c2SLeila Ghaffari   setup_context->ly                     = domain_size[1];
18793639ffbSJames Wright   setup_context->lz                     = problem->dim == 3 ? domain_size[2] : 0.;
18877841947SLeila Ghaffari   setup_context->wind[0]                = wind[0];
18977841947SLeila Ghaffari   setup_context->wind[1]                = wind[1];
19093639ffbSJames Wright   setup_context->wind[2]                = problem->dim == 3 ? wind[2] : 0.;
19177841947SLeila Ghaffari   setup_context->wind_type              = wind_type;
1927b77ddfdSJames Wright   setup_context->initial_condition_type = advectionic_type;
19377841947SLeila Ghaffari   setup_context->bubble_continuity_type = bubble_continuity_type;
19477841947SLeila Ghaffari   setup_context->time                   = 0;
19577841947SLeila Ghaffari 
19677841947SLeila Ghaffari   // -- QFunction Context
19777841947SLeila Ghaffari   user->phys->implicit             = implicit;
198841e4c73SJed Brown   advection_ctx->CtauS             = CtauS;
199841e4c73SJed Brown   advection_ctx->E_wind            = E_wind;
200841e4c73SJed Brown   advection_ctx->implicit          = implicit;
201841e4c73SJed Brown   advection_ctx->strong_form       = strong_form;
202841e4c73SJed Brown   advection_ctx->stabilization     = stab;
203*b18328c4SJames Wright   advection_ctx->stabilization_tau = stab_tau;
204*b18328c4SJames Wright   advection_ctx->Ctau_a            = Ctau_a;
205*b18328c4SJames Wright   advection_ctx->Ctau_t            = Ctau_t;
20677841947SLeila Ghaffari 
207a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context));
208a424bcd0SJames Wright   PetscCallCeed(ceed,
209a424bcd0SJames Wright                 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
210a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc));
21177841947SLeila Ghaffari 
212a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &advection_context));
213a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx));
214a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc));
215*b18328c4SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(advection_context, "timestep size", offsetof(struct AdvectionContext_, dt), 1,
216*b18328c4SJames Wright                                                          "Size of timestep, delta t"));
217841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
218a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context));
219a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context));
220ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
22177841947SLeila Ghaffari }
22277841947SLeila Ghaffari 
223367c849eSJames Wright PetscErrorCode PRINT_ADVECTION(User user, ProblemData *problem, AppCtx app_ctx) {
224367c849eSJames Wright   MPI_Comm         comm = user->comm;
225a424bcd0SJames Wright   Ceed             ceed = user->ceed;
22697baf651SJames Wright   SetupContextAdv  setup_ctx;
227841e4c73SJed Brown   AdvectionContext advection_ctx;
22877841947SLeila Ghaffari 
229841e4c73SJed Brown   PetscFunctionBeginUser;
230a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx));
231a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx));
2322b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
23377841947SLeila Ghaffari                         "  Problem:\n"
23477841947SLeila Ghaffari                         "    Problem Name                       : %s\n"
23577841947SLeila Ghaffari                         "    Stabilization                      : %s\n"
2367b77ddfdSJames Wright                         "    Initial Condition Type             : %s\n"
23777841947SLeila Ghaffari                         "    Bubble Continuity                  : %s\n"
23877841947SLeila Ghaffari                         "    Wind Type                          : %s\n",
2397b77ddfdSJames Wright                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], AdvectionICTypes[setup_ctx->initial_condition_type],
2407b77ddfdSJames Wright                         BubbleContinuityTypes[setup_ctx->bubble_continuity_type], WindTypes[setup_ctx->wind_type]));
24177841947SLeila Ghaffari 
242841e4c73SJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
24393639ffbSJames Wright     switch (problem->dim) {
24493639ffbSJames Wright       case 2:
24593639ffbSJames Wright         PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1]));
24693639ffbSJames Wright         break;
24793639ffbSJames Wright       case 3:
24893639ffbSJames Wright         PetscCall(
24993639ffbSJames Wright             PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
25093639ffbSJames Wright         break;
25193639ffbSJames Wright     }
25277841947SLeila Ghaffari   }
253a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx));
254a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx));
255ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
25677841947SLeila Ghaffari }
257