xref: /libCEED/examples/fluids/problems/advection.c (revision 367c849e96f2729f89d873e43adcb60b6350e603)
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"
1877841947SLeila Ghaffari 
1946de7363SJames Wright PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
2077841947SLeila Ghaffari   WindType             wind_type;
2177841947SLeila Ghaffari   BubbleType           bubble_type;
2277841947SLeila Ghaffari   BubbleContinuityType bubble_continuity_type;
2377841947SLeila Ghaffari   StabilizationType    stab;
2497baf651SJames Wright   SetupContextAdv      setup_context;
2577841947SLeila Ghaffari   User                 user = *(User *)ctx;
2677841947SLeila Ghaffari   MPI_Comm             comm = PETSC_COMM_WORLD;
2777841947SLeila Ghaffari   PetscBool            implicit;
2877841947SLeila Ghaffari   PetscBool            has_curr_time = PETSC_FALSE;
29841e4c73SJed Brown   AdvectionContext     advection_ctx;
30841e4c73SJed Brown   CeedQFunctionContext advection_context;
3177841947SLeila Ghaffari 
32841e4c73SJed Brown   PetscFunctionBeginUser;
332b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
342b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &advection_ctx));
3577841947SLeila Ghaffari 
3677841947SLeila Ghaffari   // ------------------------------------------------------
3777841947SLeila Ghaffari   //               SET UP ADVECTION
3877841947SLeila Ghaffari   // ------------------------------------------------------
3977841947SLeila Ghaffari   problem->dim                               = 3;
4077841947SLeila Ghaffari   problem->q_data_size_vol                   = 10;
41ba6664aeSJames Wright   problem->q_data_size_sur                   = 10;
4291e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
4391e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
4491e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
4591e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
4691e5af17SJed Brown   problem->ics.qfunction                     = ICsAdvection;
4791e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsAdvection_loc;
4891e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = Advection;
4991e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Advection_loc;
5091e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
5191e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
5291e5af17SJed Brown   problem->apply_inflow.qfunction            = Advection_InOutFlow;
5391e5af17SJed Brown   problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
5477841947SLeila Ghaffari   problem->non_zero_time                     = PETSC_FALSE;
5577841947SLeila Ghaffari   problem->print_info                        = PRINT_ADVECTION;
5677841947SLeila Ghaffari 
5777841947SLeila Ghaffari   // ------------------------------------------------------
5877841947SLeila Ghaffari   //             Create the libCEED context
5977841947SLeila Ghaffari   // ------------------------------------------------------
6077841947SLeila Ghaffari   CeedScalar rc          = 1000.;       // m (Radius of bubble)
6177841947SLeila Ghaffari   CeedScalar CtauS       = 0.;          // dimensionless
6277841947SLeila Ghaffari   CeedScalar strong_form = 0.;          // [0,1]
6377841947SLeila Ghaffari   CeedScalar E_wind      = 1.e6;        // J
6477841947SLeila Ghaffari   PetscReal  wind[3]     = {1., 0, 0};  // m/s
651864f1c2SLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
662b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
67ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
681864f1c2SLeila Ghaffari 
6977841947SLeila Ghaffari   // ------------------------------------------------------
7077841947SLeila Ghaffari   //             Create the PETSc context
7177841947SLeila Ghaffari   // ------------------------------------------------------
7277841947SLeila Ghaffari   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
7377841947SLeila Ghaffari   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
7477841947SLeila Ghaffari   PetscScalar second   = 1e-2;  // 1 second in scaled time units
7577841947SLeila Ghaffari   PetscScalar Joule;
7677841947SLeila Ghaffari 
7777841947SLeila Ghaffari   // ------------------------------------------------------
7877841947SLeila Ghaffari   //              Command line Options
7977841947SLeila Ghaffari   // ------------------------------------------------------
8067490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
8177841947SLeila Ghaffari   // -- Physics
822b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
8377841947SLeila Ghaffari   PetscBool translation;
842b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
852b730f8bSJeremy L Thompson                              &translation));
8677841947SLeila Ghaffari   if (translation) user->phys->has_neumann = PETSC_TRUE;
8777841947SLeila Ghaffari   PetscInt  n = problem->dim;
8877841947SLeila Ghaffari   PetscBool user_wind;
892b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
902b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
912b730f8bSJeremy L Thompson   PetscCall(
922b730f8bSJeremy L Thompson       PetscOptionsScalar("-strong_form", "Strong (1) or weak/integrated by parts (0) advection residual", NULL, strong_form, &strong_form, NULL));
932b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
942b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)", NULL, BubbleTypes, (PetscEnum)(bubble_type = BUBBLE_SPHERE),
952b730f8bSJeremy L Thompson                              (PetscEnum *)&bubble_type, NULL));
962b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes,
972b730f8bSJeremy L Thompson                              (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH), (PetscEnum *)&bubble_continuity_type, NULL));
982b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
992b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
10077841947SLeila Ghaffari 
10177841947SLeila Ghaffari   // -- Units
1022b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
10377841947SLeila Ghaffari   meter = fabs(meter);
1042b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
10577841947SLeila Ghaffari   kilogram = fabs(kilogram);
1062b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
10777841947SLeila Ghaffari   second = fabs(second);
10877841947SLeila Ghaffari 
10977841947SLeila Ghaffari   // -- Warnings
11077841947SLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
1112b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
11277841947SLeila Ghaffari   }
1132b730f8bSJeremy L Thompson   if (wind_type == WIND_TRANSLATION && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
11477841947SLeila Ghaffari     wind[2] = 0;
1152b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n"));
11677841947SLeila Ghaffari   }
11777841947SLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
1182b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
11977841947SLeila Ghaffari   }
12077841947SLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1212b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
12277841947SLeila Ghaffari   }
12377841947SLeila Ghaffari 
12467490bc6SJeremy L Thompson   PetscOptionsEnd();
12577841947SLeila Ghaffari 
12677841947SLeila Ghaffari   // ------------------------------------------------------
12777841947SLeila Ghaffari   //           Set up the PETSc context
12877841947SLeila Ghaffari   // ------------------------------------------------------
12977841947SLeila Ghaffari   // -- Define derived units
13077841947SLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
13177841947SLeila Ghaffari 
13277841947SLeila Ghaffari   user->units->meter    = meter;
13377841947SLeila Ghaffari   user->units->kilogram = kilogram;
13477841947SLeila Ghaffari   user->units->second   = second;
13577841947SLeila Ghaffari   user->units->Joule    = Joule;
13677841947SLeila Ghaffari 
13777841947SLeila Ghaffari   // ------------------------------------------------------
13877841947SLeila Ghaffari   //           Set up the libCEED context
13977841947SLeila Ghaffari   // ------------------------------------------------------
14077841947SLeila Ghaffari   // -- Scale variables to desired units
14177841947SLeila Ghaffari   E_wind *= Joule;
14277841947SLeila Ghaffari   rc = fabs(rc) * meter;
143ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) {
1441864f1c2SLeila Ghaffari     wind[i] *= (meter / second);
1451864f1c2SLeila Ghaffari     domain_size[i] *= meter;
1461864f1c2SLeila Ghaffari   }
1471864f1c2SLeila Ghaffari   problem->dm_scale = meter;
14877841947SLeila Ghaffari 
14977841947SLeila Ghaffari   // -- Setup Context
15077841947SLeila Ghaffari   setup_context->rc                     = rc;
1511864f1c2SLeila Ghaffari   setup_context->lx                     = domain_size[0];
1521864f1c2SLeila Ghaffari   setup_context->ly                     = domain_size[1];
1531864f1c2SLeila Ghaffari   setup_context->lz                     = domain_size[2];
15477841947SLeila Ghaffari   setup_context->wind[0]                = wind[0];
15577841947SLeila Ghaffari   setup_context->wind[1]                = wind[1];
15677841947SLeila Ghaffari   setup_context->wind[2]                = wind[2];
15777841947SLeila Ghaffari   setup_context->wind_type              = wind_type;
15877841947SLeila Ghaffari   setup_context->bubble_type            = bubble_type;
15977841947SLeila Ghaffari   setup_context->bubble_continuity_type = bubble_continuity_type;
16077841947SLeila Ghaffari   setup_context->time                   = 0;
16177841947SLeila Ghaffari 
16277841947SLeila Ghaffari   // -- QFunction Context
16377841947SLeila Ghaffari   user->phys->stab                   = stab;
16477841947SLeila Ghaffari   user->phys->wind_type              = wind_type;
16577841947SLeila Ghaffari   user->phys->bubble_type            = bubble_type;
16677841947SLeila Ghaffari   user->phys->bubble_continuity_type = bubble_continuity_type;
16777841947SLeila Ghaffari   //  if passed correctly
16877841947SLeila Ghaffari   user->phys->implicit         = implicit;
16977841947SLeila Ghaffari   user->phys->has_curr_time    = has_curr_time;
170841e4c73SJed Brown   advection_ctx->CtauS         = CtauS;
171841e4c73SJed Brown   advection_ctx->E_wind        = E_wind;
172841e4c73SJed Brown   advection_ctx->implicit      = implicit;
173841e4c73SJed Brown   advection_ctx->strong_form   = strong_form;
174841e4c73SJed Brown   advection_ctx->stabilization = stab;
17577841947SLeila Ghaffari 
176841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
1772b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
1782b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
17977841947SLeila Ghaffari 
180841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &advection_context);
1812b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx);
1822b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc);
183841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
1842b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context);
1852b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context);
186ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
18777841947SLeila Ghaffari }
18877841947SLeila Ghaffari 
189*367c849eSJames Wright PetscErrorCode PRINT_ADVECTION(User user, ProblemData *problem, AppCtx app_ctx) {
190*367c849eSJames Wright   MPI_Comm         comm = user->comm;
19197baf651SJames Wright   SetupContextAdv  setup_ctx;
192841e4c73SJed Brown   AdvectionContext advection_ctx;
19377841947SLeila Ghaffari 
194841e4c73SJed Brown   PetscFunctionBeginUser;
1952b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx);
1962b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx);
1972b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
19877841947SLeila Ghaffari                         "  Problem:\n"
19977841947SLeila Ghaffari                         "    Problem Name                       : %s\n"
20077841947SLeila Ghaffari                         "    Stabilization                      : %s\n"
201990fdeb6SJeremy L Thompson                         "    Bubble Type                        : %s (%" CeedInt_FMT "D)\n"
20277841947SLeila Ghaffari                         "    Bubble Continuity                  : %s\n"
20377841947SLeila Ghaffari                         "    Wind Type                          : %s\n",
2042b730f8bSJeremy L Thompson                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], BubbleTypes[setup_ctx->bubble_type],
2052b730f8bSJeremy L Thompson                         setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2, BubbleContinuityTypes[setup_ctx->bubble_continuity_type],
2062b730f8bSJeremy L Thompson                         WindTypes[setup_ctx->wind_type]));
20777841947SLeila Ghaffari 
208841e4c73SJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
2092b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
21077841947SLeila Ghaffari   }
2112b730f8bSJeremy L Thompson   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx);
2122b730f8bSJeremy L Thompson   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx);
213ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
21477841947SLeila Ghaffari }
215