xref: /honee/problems/advection.c (revision 57272ee07e0b680ce6b8808d5a9de7a29828f9a2)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Utility functions for setting up ADVECTION
10a515125bSLeila Ghaffari 
112b916ea7SJeremy L Thompson #include "../qfunctions/advection.h"
122b916ea7SJeremy L Thompson 
13e419654dSJeremy L Thompson #include <ceed.h>
14e419654dSJeremy L Thompson #include <petscdm.h>
15e419654dSJeremy L Thompson 
16a515125bSLeila Ghaffari #include "../navierstokes.h"
17a515125bSLeila Ghaffari #include "../qfunctions/setupgeo.h"
189529d636SJames Wright #include "../qfunctions/setupgeo2d.h"
19a515125bSLeila Ghaffari 
20d1c51a42SJames Wright PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
21a515125bSLeila Ghaffari   WindType             wind_type;
22c51f031aSJames Wright   AdvectionICType      advectionic_type;
23a515125bSLeila Ghaffari   BubbleContinuityType bubble_continuity_type;
24a515125bSLeila Ghaffari   StabilizationType    stab;
25*57272ee0SJames Wright   StabilizationTauType stab_tau;
263636f6a4SJames Wright   SetupContextAdv      setup_context;
27a515125bSLeila Ghaffari   User                 user = *(User *)ctx;
28b4c37c5cSJames Wright   MPI_Comm             comm = user->comm;
29b4c37c5cSJames Wright   Ceed                 ceed = user->ceed;
30a515125bSLeila Ghaffari   PetscBool            implicit;
3115a3537eSJed Brown   AdvectionContext     advection_ctx;
3215a3537eSJed Brown   CeedQFunctionContext advection_context;
339529d636SJames Wright   PetscInt             dim;
34a515125bSLeila Ghaffari 
3515a3537eSJed Brown   PetscFunctionBeginUser;
362b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
372b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &advection_ctx));
389529d636SJames Wright   PetscCall(DMGetDimension(dm, &dim));
39a515125bSLeila Ghaffari 
40a515125bSLeila Ghaffari   // ------------------------------------------------------
41a515125bSLeila Ghaffari   //               SET UP ADVECTION
42a515125bSLeila Ghaffari   // ------------------------------------------------------
439529d636SJames Wright   switch (dim) {
449529d636SJames Wright     case 2:
459529d636SJames Wright       problem->dim                               = 2;
469529d636SJames Wright       problem->q_data_size_vol                   = 5;
479529d636SJames Wright       problem->q_data_size_sur                   = 3;
489529d636SJames Wright       problem->setup_vol.qfunction               = Setup2d;
499529d636SJames Wright       problem->setup_vol.qfunction_loc           = Setup2d_loc;
509529d636SJames Wright       problem->setup_sur.qfunction               = SetupBoundary2d;
519529d636SJames Wright       problem->setup_sur.qfunction_loc           = SetupBoundary2d_loc;
529529d636SJames Wright       problem->ics.qfunction                     = ICsAdvection2d;
539529d636SJames Wright       problem->ics.qfunction_loc                 = ICsAdvection2d_loc;
549529d636SJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection2d;
559529d636SJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection2d_loc;
569529d636SJames Wright       problem->apply_vol_ifunction.qfunction     = IFunction_Advection2d;
579529d636SJames Wright       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection2d_loc;
589529d636SJames Wright       problem->apply_inflow.qfunction            = Advection2d_InOutFlow;
599529d636SJames Wright       problem->apply_inflow.qfunction_loc        = Advection2d_InOutFlow_loc;
609529d636SJames Wright       problem->non_zero_time                     = PETSC_TRUE;
619529d636SJames Wright       problem->print_info                        = PRINT_ADVECTION;
629529d636SJames Wright       break;
639529d636SJames Wright     case 3:
64a515125bSLeila Ghaffari       problem->dim                               = 3;
65a515125bSLeila Ghaffari       problem->q_data_size_vol                   = 10;
66493642f1SJames Wright       problem->q_data_size_sur                   = 10;
679785fe93SJed Brown       problem->setup_vol.qfunction               = Setup;
689785fe93SJed Brown       problem->setup_vol.qfunction_loc           = Setup_loc;
699785fe93SJed Brown       problem->setup_sur.qfunction               = SetupBoundary;
709785fe93SJed Brown       problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
719785fe93SJed Brown       problem->ics.qfunction                     = ICsAdvection;
729785fe93SJed Brown       problem->ics.qfunction_loc                 = ICsAdvection_loc;
739529d636SJames Wright       problem->apply_vol_rhs.qfunction           = RHS_Advection;
749529d636SJames Wright       problem->apply_vol_rhs.qfunction_loc       = RHS_Advection_loc;
759785fe93SJed Brown       problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
769785fe93SJed Brown       problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
779785fe93SJed Brown       problem->apply_inflow.qfunction            = Advection_InOutFlow;
789785fe93SJed Brown       problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
79a515125bSLeila Ghaffari       problem->non_zero_time                     = PETSC_FALSE;
80a515125bSLeila Ghaffari       problem->print_info                        = PRINT_ADVECTION;
819529d636SJames Wright       break;
829529d636SJames Wright   }
83a515125bSLeila Ghaffari 
84a515125bSLeila Ghaffari   // ------------------------------------------------------
85a515125bSLeila Ghaffari   //             Create the libCEED context
86a515125bSLeila Ghaffari   // ------------------------------------------------------
87a515125bSLeila Ghaffari   CeedScalar rc          = 1000.;  // m (Radius of bubble)
88a515125bSLeila Ghaffari   CeedScalar CtauS       = 0.;     // dimensionless
89059d1c40SJames Wright   PetscBool  strong_form = PETSC_FALSE;
90a515125bSLeila Ghaffari   CeedScalar E_wind      = 1.e6;  // J
91*57272ee0SJames Wright   CeedScalar Ctau_a      = PetscPowScalarInt(user->app_ctx->degree, 2);
92*57272ee0SJames Wright   CeedScalar Ctau_t      = 0.;
93a515125bSLeila Ghaffari   PetscReal  wind[3]     = {1., 0, 0};  // m/s
9405a512bdSLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
952b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
969529d636SJames Wright   for (PetscInt i = 0; i < problem->dim; i++) domain_size[i] = domain_max[i] - domain_min[i];
9705a512bdSLeila Ghaffari 
98a515125bSLeila Ghaffari   // ------------------------------------------------------
99a515125bSLeila Ghaffari   //             Create the PETSc context
100a515125bSLeila Ghaffari   // ------------------------------------------------------
101a515125bSLeila Ghaffari   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
102a515125bSLeila Ghaffari   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
103a515125bSLeila Ghaffari   PetscScalar second   = 1e-2;  // 1 second in scaled time units
104a515125bSLeila Ghaffari   PetscScalar Joule;
105a515125bSLeila Ghaffari 
106a515125bSLeila Ghaffari   // ------------------------------------------------------
107a515125bSLeila Ghaffari   //              Command line Options
108a515125bSLeila Ghaffari   // ------------------------------------------------------
1091485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
110a515125bSLeila Ghaffari   // -- Physics
1112b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
112a515125bSLeila Ghaffari   PetscBool translation;
1132b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
1142b916ea7SJeremy L Thompson                              &translation));
115a515125bSLeila Ghaffari   PetscInt  n = problem->dim;
116a515125bSLeila Ghaffari   PetscBool user_wind;
1172b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
1182b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
119059d1c40SJames Wright   PetscCall(PetscOptionsBool("-strong_form", "Strong (true) or weak/integrated by parts (false) advection residual", NULL, strong_form, &strong_form,
120059d1c40SJames Wright                              NULL));
1212b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
122c51f031aSJames Wright   PetscCall(PetscOptionsEnum("-advection_ic_type", "Initial condition for Advection problem", NULL, AdvectionICTypes,
123c51f031aSJames Wright                              (PetscEnum)(advectionic_type = ADVECTIONIC_BUBBLE_SPHERE), (PetscEnum *)&advectionic_type, NULL));
1249529d636SJames Wright   bubble_continuity_type = problem->dim == 3 ? BUBBLE_CONTINUITY_SMOOTH : BUBBLE_CONTINUITY_COSINE;
1259529d636SJames Wright   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes, (PetscEnum)bubble_continuity_type,
1269529d636SJames Wright                              (PetscEnum *)&bubble_continuity_type, NULL));
1272b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
128*57272ee0SJames Wright   PetscCall(PetscOptionsEnum("-stab_tau", "Stabilization constant, tau", NULL, StabilizationTauTypes, (PetscEnum)(stab_tau = STAB_TAU_CTAU),
129*57272ee0SJames Wright                              (PetscEnum *)&stab_tau, NULL));
130*57272ee0SJames Wright   PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL));
131*57272ee0SJames Wright   PetscCall(PetscOptionsScalar("-Ctau_a", "Coefficient for the stabilization ", NULL, Ctau_a, &Ctau_a, NULL));
1322b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
133a515125bSLeila Ghaffari 
134a515125bSLeila Ghaffari   // -- Units
1352b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
136a515125bSLeila Ghaffari   meter = fabs(meter);
1372b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
138a515125bSLeila Ghaffari   kilogram = fabs(kilogram);
1392b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
140a515125bSLeila Ghaffari   second = fabs(second);
141a515125bSLeila Ghaffari 
142a515125bSLeila Ghaffari   // -- Warnings
143a515125bSLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
1442b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
145a515125bSLeila Ghaffari   }
146c51f031aSJames Wright   if (wind_type == WIND_TRANSLATION && advectionic_type == ADVECTIONIC_BUBBLE_CYLINDER && wind[2] != 0.) {
147a515125bSLeila Ghaffari     wind[2] = 0;
148c51f031aSJames Wright     PetscCall(
149c51f031aSJames Wright         PetscPrintf(comm, "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -advection_ic_type cylinder\n"));
150a515125bSLeila Ghaffari   }
151a515125bSLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
1522b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
153a515125bSLeila Ghaffari   }
154a515125bSLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1552b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
156a515125bSLeila Ghaffari   }
157a515125bSLeila Ghaffari 
1581485969bSJeremy L Thompson   PetscOptionsEnd();
159a515125bSLeila Ghaffari 
160a515125bSLeila Ghaffari   // ------------------------------------------------------
161a515125bSLeila Ghaffari   //           Set up the PETSc context
162a515125bSLeila Ghaffari   // ------------------------------------------------------
163a515125bSLeila Ghaffari   // -- Define derived units
164a515125bSLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
165a515125bSLeila Ghaffari 
166a515125bSLeila Ghaffari   user->units->meter    = meter;
167a515125bSLeila Ghaffari   user->units->kilogram = kilogram;
168a515125bSLeila Ghaffari   user->units->second   = second;
169a515125bSLeila Ghaffari   user->units->Joule    = Joule;
170a515125bSLeila Ghaffari 
171a515125bSLeila Ghaffari   // ------------------------------------------------------
172a515125bSLeila Ghaffari   //           Set up the libCEED context
173a515125bSLeila Ghaffari   // ------------------------------------------------------
174a515125bSLeila Ghaffari   // -- Scale variables to desired units
175a515125bSLeila Ghaffari   E_wind *= Joule;
176a515125bSLeila Ghaffari   rc = fabs(rc) * meter;
1779529d636SJames Wright   for (PetscInt i = 0; i < problem->dim; i++) {
17805a512bdSLeila Ghaffari     wind[i] *= (meter / second);
17905a512bdSLeila Ghaffari     domain_size[i] *= meter;
18005a512bdSLeila Ghaffari   }
18105a512bdSLeila Ghaffari   problem->dm_scale = meter;
182a515125bSLeila Ghaffari 
183a515125bSLeila Ghaffari   // -- Setup Context
184a515125bSLeila Ghaffari   setup_context->rc                     = rc;
18505a512bdSLeila Ghaffari   setup_context->lx                     = domain_size[0];
18605a512bdSLeila Ghaffari   setup_context->ly                     = domain_size[1];
1879529d636SJames Wright   setup_context->lz                     = problem->dim == 3 ? domain_size[2] : 0.;
188a515125bSLeila Ghaffari   setup_context->wind[0]                = wind[0];
189a515125bSLeila Ghaffari   setup_context->wind[1]                = wind[1];
1909529d636SJames Wright   setup_context->wind[2]                = problem->dim == 3 ? wind[2] : 0.;
191a515125bSLeila Ghaffari   setup_context->wind_type              = wind_type;
192c51f031aSJames Wright   setup_context->initial_condition_type = advectionic_type;
193a515125bSLeila Ghaffari   setup_context->bubble_continuity_type = bubble_continuity_type;
194a515125bSLeila Ghaffari   setup_context->time                   = 0;
195a515125bSLeila Ghaffari 
196a515125bSLeila Ghaffari   // -- QFunction Context
197a515125bSLeila Ghaffari   user->phys->implicit             = implicit;
19815a3537eSJed Brown   advection_ctx->CtauS             = CtauS;
19915a3537eSJed Brown   advection_ctx->E_wind            = E_wind;
20015a3537eSJed Brown   advection_ctx->implicit          = implicit;
20115a3537eSJed Brown   advection_ctx->strong_form       = strong_form;
20215a3537eSJed Brown   advection_ctx->stabilization     = stab;
203*57272ee0SJames Wright   advection_ctx->stabilization_tau = stab_tau;
204*57272ee0SJames Wright   advection_ctx->Ctau_a            = Ctau_a;
205*57272ee0SJames Wright   advection_ctx->Ctau_t            = Ctau_t;
206a515125bSLeila Ghaffari 
207b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context));
208b4c37c5cSJames Wright   PetscCallCeed(ceed,
209b4c37c5cSJames Wright                 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
210b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc));
211a515125bSLeila Ghaffari 
212b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &advection_context));
213b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx));
214b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc));
215*57272ee0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(advection_context, "timestep size", offsetof(struct AdvectionContext_, dt), 1,
216*57272ee0SJames Wright                                                          "Size of timestep, delta t"));
21715a3537eSJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
218b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context));
219b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context));
220d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
221a515125bSLeila Ghaffari }
222a515125bSLeila Ghaffari 
2232d49c0afSJames Wright PetscErrorCode PRINT_ADVECTION(User user, ProblemData *problem, AppCtx app_ctx) {
2242d49c0afSJames Wright   MPI_Comm         comm = user->comm;
225b4c37c5cSJames Wright   Ceed             ceed = user->ceed;
2263636f6a4SJames Wright   SetupContextAdv  setup_ctx;
22715a3537eSJed Brown   AdvectionContext advection_ctx;
228a515125bSLeila Ghaffari 
22915a3537eSJed Brown   PetscFunctionBeginUser;
230b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx));
231b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx));
2322b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(comm,
233a515125bSLeila Ghaffari                         "  Problem:\n"
234a515125bSLeila Ghaffari                         "    Problem Name                       : %s\n"
235a515125bSLeila Ghaffari                         "    Stabilization                      : %s\n"
236c51f031aSJames Wright                         "    Initial Condition Type             : %s\n"
237a515125bSLeila Ghaffari                         "    Bubble Continuity                  : %s\n"
238a515125bSLeila Ghaffari                         "    Wind Type                          : %s\n",
239c51f031aSJames Wright                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], AdvectionICTypes[setup_ctx->initial_condition_type],
240c51f031aSJames Wright                         BubbleContinuityTypes[setup_ctx->bubble_continuity_type], WindTypes[setup_ctx->wind_type]));
241a515125bSLeila Ghaffari 
24215a3537eSJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
2439529d636SJames Wright     switch (problem->dim) {
2449529d636SJames Wright       case 2:
2459529d636SJames Wright         PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1]));
2469529d636SJames Wright         break;
2479529d636SJames Wright       case 3:
2489529d636SJames Wright         PetscCall(
2499529d636SJames Wright             PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
2509529d636SJames Wright         break;
2519529d636SJames Wright     }
252a515125bSLeila Ghaffari   }
253b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx));
254b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx));
255d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
256a515125bSLeila Ghaffari }
257