xref: /libCEED/examples/fluids/problems/advection.c (revision 841e4c7362a2acf3a6f116f4961b1eb52fa410fc)
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 
1177841947SLeila Ghaffari #include "../navierstokes.h"
1277841947SLeila Ghaffari #include "../qfunctions/setupgeo.h"
1377841947SLeila Ghaffari #include "../qfunctions/advection.h"
1477841947SLeila Ghaffari 
151864f1c2SLeila Ghaffari PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *setup_ctx,
161864f1c2SLeila Ghaffari                             void *ctx) {
1777841947SLeila Ghaffari   WindType             wind_type;
1877841947SLeila Ghaffari   BubbleType           bubble_type;
1977841947SLeila Ghaffari   BubbleContinuityType bubble_continuity_type;
2077841947SLeila Ghaffari   StabilizationType    stab;
2177841947SLeila Ghaffari   SetupContext         setup_context = *(SetupContext *)setup_ctx;
2277841947SLeila Ghaffari   User                 user = *(User *)ctx;
2377841947SLeila Ghaffari   MPI_Comm             comm = PETSC_COMM_WORLD;
2477841947SLeila Ghaffari   PetscBool            implicit;
2577841947SLeila Ghaffari   PetscBool            has_curr_time = PETSC_FALSE;
2677841947SLeila Ghaffari   PetscInt             ierr;
27*841e4c73SJed Brown   AdvectionContext     advection_ctx;
28*841e4c73SJed Brown   CeedQFunctionContext advection_context;
2977841947SLeila Ghaffari 
30*841e4c73SJed Brown   PetscFunctionBeginUser;
31*841e4c73SJed Brown   ierr = PetscCalloc1(1, &advection_ctx); CHKERRQ(ierr);
3277841947SLeila Ghaffari 
3377841947SLeila Ghaffari   // ------------------------------------------------------
3477841947SLeila Ghaffari   //               SET UP ADVECTION
3577841947SLeila Ghaffari   // ------------------------------------------------------
3677841947SLeila Ghaffari   problem->dim                               = 3;
3777841947SLeila Ghaffari   problem->q_data_size_vol                   = 10;
3877841947SLeila Ghaffari   problem->q_data_size_sur                   = 4;
3991e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
4091e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
4191e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
4291e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
4391e5af17SJed Brown   problem->ics.qfunction                     = ICsAdvection;
4491e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsAdvection_loc;
4591e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = Advection;
4691e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Advection_loc;
4791e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
4891e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
4991e5af17SJed Brown   problem->apply_inflow.qfunction            = Advection_InOutFlow;
5091e5af17SJed Brown   problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
5177841947SLeila Ghaffari   problem->bc                                = Exact_Advection;
5277841947SLeila Ghaffari   problem->non_zero_time                     = PETSC_FALSE;
5377841947SLeila Ghaffari   problem->print_info                        = PRINT_ADVECTION;
5477841947SLeila Ghaffari 
5577841947SLeila Ghaffari   // ------------------------------------------------------
5677841947SLeila Ghaffari   //             Create the libCEED context
5777841947SLeila Ghaffari   // ------------------------------------------------------
5877841947SLeila Ghaffari   CeedScalar rc          = 1000.;      // m (Radius of bubble)
5977841947SLeila Ghaffari   CeedScalar CtauS       = 0.;         // dimensionless
6077841947SLeila Ghaffari   CeedScalar strong_form = 0.;         // [0,1]
6177841947SLeila Ghaffari   CeedScalar E_wind      = 1.e6;       // J
6277841947SLeila Ghaffari   PetscReal wind[3]      = {1., 0, 0}; // m/s
631864f1c2SLeila Ghaffari   PetscReal domain_min[3], domain_max[3], domain_size[3];
641864f1c2SLeila Ghaffari   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
651864f1c2SLeila Ghaffari   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
661864f1c2SLeila Ghaffari 
6777841947SLeila Ghaffari 
6877841947SLeila Ghaffari   // ------------------------------------------------------
6977841947SLeila Ghaffari   //             Create the PETSc context
7077841947SLeila Ghaffari   // ------------------------------------------------------
7177841947SLeila Ghaffari   PetscScalar meter    = 1e-2; // 1 meter in scaled length units
7277841947SLeila Ghaffari   PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units
7377841947SLeila Ghaffari   PetscScalar second   = 1e-2; // 1 second in scaled time units
7477841947SLeila Ghaffari   PetscScalar Joule;
7577841947SLeila Ghaffari 
7677841947SLeila Ghaffari   // ------------------------------------------------------
7777841947SLeila Ghaffari   //              Command line Options
7877841947SLeila Ghaffari   // ------------------------------------------------------
7967490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
8077841947SLeila Ghaffari   // -- Physics
8177841947SLeila Ghaffari   ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble",
8277841947SLeila Ghaffari                             NULL, rc, &rc, NULL); CHKERRQ(ierr);
8377841947SLeila Ghaffari   PetscBool translation;
8477841947SLeila Ghaffari   ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection",
8577841947SLeila Ghaffari                           NULL, WindTypes,
8677841947SLeila Ghaffari                           (PetscEnum)(wind_type = WIND_ROTATION),
8777841947SLeila Ghaffari                           (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr);
8877841947SLeila Ghaffari   if (translation) user->phys->has_neumann = PETSC_TRUE;
8977841947SLeila Ghaffari   PetscInt n = problem->dim;
9077841947SLeila Ghaffari   PetscBool user_wind;
9177841947SLeila Ghaffari   ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector",
9277841947SLeila Ghaffari                                NULL, wind, &n, &user_wind); CHKERRQ(ierr);
9377841947SLeila Ghaffari   ierr = PetscOptionsScalar("-CtauS",
9477841947SLeila Ghaffari                             "Scale coefficient for tau (nondimensional)",
9577841947SLeila Ghaffari                             NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr);
9677841947SLeila Ghaffari   ierr = PetscOptionsScalar("-strong_form",
9777841947SLeila Ghaffari                             "Strong (1) or weak/integrated by parts (0) advection residual",
9877841947SLeila Ghaffari                             NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr);
9977841947SLeila Ghaffari   ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind",
10077841947SLeila Ghaffari                             NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr);
10177841947SLeila Ghaffari   ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)",
10277841947SLeila Ghaffari                           NULL, BubbleTypes,
10377841947SLeila Ghaffari                           (PetscEnum)(bubble_type = BUBBLE_SPHERE),
10477841947SLeila Ghaffari                           (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr);
10577841947SLeila Ghaffari   ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick",
10677841947SLeila Ghaffari                           NULL, BubbleContinuityTypes,
10777841947SLeila Ghaffari                           (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH),
10877841947SLeila Ghaffari                           (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr);
10977841947SLeila Ghaffari   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
11077841947SLeila Ghaffari                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
11177841947SLeila Ghaffari                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
11277841947SLeila Ghaffari   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
11377841947SLeila Ghaffari                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
11477841947SLeila Ghaffari   CHKERRQ(ierr);
11577841947SLeila Ghaffari 
11677841947SLeila Ghaffari   // -- Units
11777841947SLeila Ghaffari   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
11877841947SLeila Ghaffari                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
11977841947SLeila Ghaffari   meter = fabs(meter);
12077841947SLeila Ghaffari   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
12177841947SLeila Ghaffari                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
12277841947SLeila Ghaffari   kilogram = fabs(kilogram);
12377841947SLeila Ghaffari   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
12477841947SLeila Ghaffari                             NULL, second, &second, NULL); CHKERRQ(ierr);
12577841947SLeila Ghaffari   second = fabs(second);
12677841947SLeila Ghaffari 
12777841947SLeila Ghaffari   // -- Warnings
12877841947SLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
12977841947SLeila Ghaffari     ierr = PetscPrintf(comm,
13077841947SLeila Ghaffari                        "Warning! Use -wind_translation only with -wind_type translation\n");
13177841947SLeila Ghaffari     CHKERRQ(ierr);
13277841947SLeila Ghaffari   }
13377841947SLeila Ghaffari   if (wind_type == WIND_TRANSLATION
13477841947SLeila Ghaffari       && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
13577841947SLeila Ghaffari     wind[2] = 0;
13677841947SLeila Ghaffari     ierr = PetscPrintf(comm,
13777841947SLeila Ghaffari                        "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n");
13877841947SLeila Ghaffari     CHKERRQ(ierr);
13977841947SLeila Ghaffari   }
14077841947SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
14177841947SLeila Ghaffari     ierr = PetscPrintf(comm,
14277841947SLeila Ghaffari                        "Warning! Use -CtauS only with -stab su or -stab supg\n");
14377841947SLeila Ghaffari     CHKERRQ(ierr);
14477841947SLeila Ghaffari   }
14577841947SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
14677841947SLeila Ghaffari     ierr = PetscPrintf(comm,
14777841947SLeila Ghaffari                        "Warning! Use -stab supg only with -implicit\n");
14877841947SLeila Ghaffari     CHKERRQ(ierr);
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;
1701864f1c2SLeila Ghaffari   for (int i=0; i<3; 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];
1801864f1c2SLeila Ghaffari   setup_context->lz                     = domain_size[2];
18177841947SLeila Ghaffari   setup_context->wind[0]                = wind[0];
18277841947SLeila Ghaffari   setup_context->wind[1]                = wind[1];
18377841947SLeila Ghaffari   setup_context->wind[2]                = wind[2];
18477841947SLeila Ghaffari   setup_context->wind_type              = wind_type;
18577841947SLeila Ghaffari   setup_context->bubble_type            = bubble_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->stab                         = stab;
19177841947SLeila Ghaffari   user->phys->wind_type                    = wind_type;
19277841947SLeila Ghaffari   user->phys->bubble_type                  = bubble_type;
19377841947SLeila Ghaffari   user->phys->bubble_continuity_type       = bubble_continuity_type;
19477841947SLeila Ghaffari   //  if passed correctly
19577841947SLeila Ghaffari   user->phys->implicit                     = implicit;
19677841947SLeila Ghaffari   user->phys->has_curr_time                = has_curr_time;
197*841e4c73SJed Brown   advection_ctx->CtauS         = CtauS;
198*841e4c73SJed Brown   advection_ctx->E_wind        = E_wind;
199*841e4c73SJed Brown   advection_ctx->implicit      = implicit;
200*841e4c73SJed Brown   advection_ctx->strong_form   = strong_form;
201*841e4c73SJed Brown   advection_ctx->stabilization = stab;
20277841947SLeila Ghaffari 
203*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
204*841e4c73SJed Brown   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST,
205*841e4c73SJed Brown                               CEED_USE_POINTER, sizeof(*setup_context), setup_context);
20677841947SLeila Ghaffari 
207*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &advection_context);
208*841e4c73SJed Brown   CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST,
209d0b732dbSLeila Ghaffari                               CEED_USE_POINTER,
210*841e4c73SJed Brown                               sizeof(*advection_ctx), advection_ctx);
211*841e4c73SJed Brown   CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST,
212*841e4c73SJed Brown                                      FreeContextPetsc);
213*841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
214*841e4c73SJed Brown   CeedQFunctionContextReferenceCopy(advection_context,
215*841e4c73SJed Brown                                     &problem->apply_vol_ifunction.qfunction_context);
216*841e4c73SJed Brown   CeedQFunctionContextReferenceCopy(advection_context,
217*841e4c73SJed Brown                                     &problem->apply_inflow.qfunction_context);
21877841947SLeila Ghaffari   PetscFunctionReturn(0);
21977841947SLeila Ghaffari }
22077841947SLeila Ghaffari 
221*841e4c73SJed Brown PetscErrorCode PRINT_ADVECTION(ProblemData *problem, SetupContext setup_ctx,
22277841947SLeila Ghaffari                                AppCtx app_ctx) {
22377841947SLeila Ghaffari   MPI_Comm       comm = PETSC_COMM_WORLD;
22477841947SLeila Ghaffari   PetscErrorCode ierr;
225*841e4c73SJed Brown   AdvectionContext advection_ctx;
22677841947SLeila Ghaffari 
227*841e4c73SJed Brown   PetscFunctionBeginUser;
228*841e4c73SJed Brown   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context,
229*841e4c73SJed Brown                               CEED_MEM_HOST, &advection_ctx);
23077841947SLeila Ghaffari   ierr = PetscPrintf(comm,
23177841947SLeila Ghaffari                      "  Problem:\n"
23277841947SLeila Ghaffari                      "    Problem Name                       : %s\n"
23377841947SLeila Ghaffari                      "    Stabilization                      : %s\n"
23477841947SLeila Ghaffari                      "    Bubble Type                        : %s (%dD)\n"
23577841947SLeila Ghaffari                      "    Bubble Continuity                  : %s\n"
23677841947SLeila Ghaffari                      "    Wind Type                          : %s\n",
237*841e4c73SJed Brown                      app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization],
238*841e4c73SJed Brown                      BubbleTypes[setup_ctx->bubble_type],
239*841e4c73SJed Brown                      setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2,
240*841e4c73SJed Brown                      BubbleContinuityTypes[setup_ctx->bubble_continuity_type],
241*841e4c73SJed Brown                      WindTypes[setup_ctx->wind_type]); CHKERRQ(ierr);
24277841947SLeila Ghaffari 
243*841e4c73SJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
24477841947SLeila Ghaffari     ierr = PetscPrintf(comm,
24577841947SLeila Ghaffari                        "    Background Wind                    : %f,%f,%f\n",
24677841947SLeila Ghaffari                        setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr);
24777841947SLeila Ghaffari   }
248*841e4c73SJed Brown   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context,
249*841e4c73SJed Brown                                   &advection_ctx);
25077841947SLeila Ghaffari   PetscFunctionReturn(0);
25177841947SLeila Ghaffari }
252