xref: /honee/problems/advection.c (revision d949ddfc92e3b3e8aad90700e3fb60ca7f4585db)
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"
18a515125bSLeila Ghaffari 
19d1c51a42SJames Wright PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
20a515125bSLeila Ghaffari   WindType             wind_type;
21a515125bSLeila Ghaffari   BubbleType           bubble_type;
22a515125bSLeila Ghaffari   BubbleContinuityType bubble_continuity_type;
23a515125bSLeila Ghaffari   StabilizationType    stab;
243636f6a4SJames Wright   SetupContextAdv      setup_context;
25a515125bSLeila Ghaffari   User                 user = *(User *)ctx;
26a515125bSLeila Ghaffari   MPI_Comm             comm = PETSC_COMM_WORLD;
27a515125bSLeila Ghaffari   PetscBool            implicit;
28a515125bSLeila Ghaffari   PetscBool            has_curr_time = PETSC_FALSE;
2915a3537eSJed Brown   AdvectionContext     advection_ctx;
3015a3537eSJed Brown   CeedQFunctionContext advection_context;
31a515125bSLeila Ghaffari 
3215a3537eSJed Brown   PetscFunctionBeginUser;
332b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
342b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &advection_ctx));
35a515125bSLeila Ghaffari 
36a515125bSLeila Ghaffari   // ------------------------------------------------------
37a515125bSLeila Ghaffari   //               SET UP ADVECTION
38a515125bSLeila Ghaffari   // ------------------------------------------------------
39a515125bSLeila Ghaffari   problem->dim                               = 3;
40a515125bSLeila Ghaffari   problem->q_data_size_vol                   = 10;
41493642f1SJames Wright   problem->q_data_size_sur                   = 10;
429785fe93SJed Brown   problem->setup_vol.qfunction               = Setup;
439785fe93SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
449785fe93SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
459785fe93SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
469785fe93SJed Brown   problem->ics.qfunction                     = ICsAdvection;
479785fe93SJed Brown   problem->ics.qfunction_loc                 = ICsAdvection_loc;
489785fe93SJed Brown   problem->apply_vol_rhs.qfunction           = Advection;
499785fe93SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Advection_loc;
509785fe93SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Advection;
519785fe93SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc;
529785fe93SJed Brown   problem->apply_inflow.qfunction            = Advection_InOutFlow;
539785fe93SJed Brown   problem->apply_inflow.qfunction_loc        = Advection_InOutFlow_loc;
54a515125bSLeila Ghaffari   problem->bc                                = Exact_Advection;
55b7f03f12SJed Brown   problem->bc_ctx                            = setup_context;
56a515125bSLeila Ghaffari   problem->non_zero_time                     = PETSC_FALSE;
57a515125bSLeila Ghaffari   problem->print_info                        = PRINT_ADVECTION;
58a515125bSLeila Ghaffari 
59a515125bSLeila Ghaffari   // ------------------------------------------------------
60a515125bSLeila Ghaffari   //             Create the libCEED context
61a515125bSLeila Ghaffari   // ------------------------------------------------------
62a515125bSLeila Ghaffari   CeedScalar rc          = 1000.;       // m (Radius of bubble)
63a515125bSLeila Ghaffari   CeedScalar CtauS       = 0.;          // dimensionless
64a515125bSLeila Ghaffari   CeedScalar strong_form = 0.;          // [0,1]
65a515125bSLeila Ghaffari   CeedScalar E_wind      = 1.e6;        // J
66a515125bSLeila Ghaffari   PetscReal  wind[3]     = {1., 0, 0};  // m/s
6705a512bdSLeila Ghaffari   PetscReal  domain_min[3], domain_max[3], domain_size[3];
682b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
69493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
7005a512bdSLeila Ghaffari 
71a515125bSLeila Ghaffari   // ------------------------------------------------------
72a515125bSLeila Ghaffari   //             Create the PETSc context
73a515125bSLeila Ghaffari   // ------------------------------------------------------
74a515125bSLeila Ghaffari   PetscScalar meter    = 1e-2;  // 1 meter in scaled length units
75a515125bSLeila Ghaffari   PetscScalar kilogram = 1e-6;  // 1 kilogram in scaled mass units
76a515125bSLeila Ghaffari   PetscScalar second   = 1e-2;  // 1 second in scaled time units
77a515125bSLeila Ghaffari   PetscScalar Joule;
78a515125bSLeila Ghaffari 
79a515125bSLeila Ghaffari   // ------------------------------------------------------
80a515125bSLeila Ghaffari   //              Command line Options
81a515125bSLeila Ghaffari   // ------------------------------------------------------
821485969bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL);
83a515125bSLeila Ghaffari   // -- Physics
842b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", NULL, rc, &rc, NULL));
85a515125bSLeila Ghaffari   PetscBool translation;
862b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-wind_type", "Wind type in Advection", NULL, WindTypes, (PetscEnum)(wind_type = WIND_ROTATION), (PetscEnum *)&wind_type,
872b916ea7SJeremy L Thompson                              &translation));
88a515125bSLeila Ghaffari   if (translation) user->phys->has_neumann = PETSC_TRUE;
89a515125bSLeila Ghaffari   PetscInt  n = problem->dim;
90a515125bSLeila Ghaffari   PetscBool user_wind;
912b916ea7SJeremy L Thompson   PetscCall(PetscOptionsRealArray("-wind_translation", "Constant wind vector", NULL, wind, &n, &user_wind));
922b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-CtauS", "Scale coefficient for tau (nondimensional)", NULL, CtauS, &CtauS, NULL));
932b916ea7SJeremy L Thompson   PetscCall(
942b916ea7SJeremy L Thompson       PetscOptionsScalar("-strong_form", "Strong (1) or weak/integrated by parts (0) advection residual", NULL, strong_form, &strong_form, NULL));
952b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-E_wind", "Total energy of inflow wind", NULL, E_wind, &E_wind, NULL));
962b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)", NULL, BubbleTypes, (PetscEnum)(bubble_type = BUBBLE_SPHERE),
972b916ea7SJeremy L Thompson                              (PetscEnum *)&bubble_type, NULL));
982b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", NULL, BubbleContinuityTypes,
992b916ea7SJeremy L Thompson                              (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH), (PetscEnum *)&bubble_continuity_type, NULL));
1002b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
1012b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
102a515125bSLeila Ghaffari 
103a515125bSLeila Ghaffari   // -- Units
1042b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
105a515125bSLeila Ghaffari   meter = fabs(meter);
1062b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
107a515125bSLeila Ghaffari   kilogram = fabs(kilogram);
1082b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
109a515125bSLeila Ghaffari   second = fabs(second);
110a515125bSLeila Ghaffari 
111a515125bSLeila Ghaffari   // -- Warnings
112a515125bSLeila Ghaffari   if (wind_type == WIND_ROTATION && user_wind) {
1132b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -wind_translation only with -wind_type translation\n"));
114a515125bSLeila Ghaffari   }
1152b916ea7SJeremy L Thompson   if (wind_type == WIND_TRANSLATION && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) {
116a515125bSLeila Ghaffari     wind[2] = 0;
1172b916ea7SJeremy 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"));
118a515125bSLeila Ghaffari   }
119a515125bSLeila Ghaffari   if (stab == STAB_NONE && CtauS != 0) {
1202b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -CtauS only with -stab su or -stab supg\n"));
121a515125bSLeila Ghaffari   }
122a515125bSLeila Ghaffari   if (stab == STAB_SUPG && !implicit) {
1232b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
124a515125bSLeila Ghaffari   }
125a515125bSLeila Ghaffari 
1261485969bSJeremy L Thompson   PetscOptionsEnd();
127a515125bSLeila Ghaffari 
128a515125bSLeila Ghaffari   // ------------------------------------------------------
129a515125bSLeila Ghaffari   //           Set up the PETSc context
130a515125bSLeila Ghaffari   // ------------------------------------------------------
131a515125bSLeila Ghaffari   // -- Define derived units
132a515125bSLeila Ghaffari   Joule = kilogram * PetscSqr(meter) / PetscSqr(second);
133a515125bSLeila Ghaffari 
134a515125bSLeila Ghaffari   user->units->meter    = meter;
135a515125bSLeila Ghaffari   user->units->kilogram = kilogram;
136a515125bSLeila Ghaffari   user->units->second   = second;
137a515125bSLeila Ghaffari   user->units->Joule    = Joule;
138a515125bSLeila Ghaffari 
139a515125bSLeila Ghaffari   // ------------------------------------------------------
140a515125bSLeila Ghaffari   //           Set up the libCEED context
141a515125bSLeila Ghaffari   // ------------------------------------------------------
142a515125bSLeila Ghaffari   // -- Scale variables to desired units
143a515125bSLeila Ghaffari   E_wind *= Joule;
144a515125bSLeila Ghaffari   rc = fabs(rc) * meter;
145493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) {
14605a512bdSLeila Ghaffari     wind[i] *= (meter / second);
14705a512bdSLeila Ghaffari     domain_size[i] *= meter;
14805a512bdSLeila Ghaffari   }
14905a512bdSLeila Ghaffari   problem->dm_scale = meter;
150a515125bSLeila Ghaffari 
151a515125bSLeila Ghaffari   // -- Setup Context
152a515125bSLeila Ghaffari   setup_context->rc                     = rc;
15305a512bdSLeila Ghaffari   setup_context->lx                     = domain_size[0];
15405a512bdSLeila Ghaffari   setup_context->ly                     = domain_size[1];
15505a512bdSLeila Ghaffari   setup_context->lz                     = domain_size[2];
156a515125bSLeila Ghaffari   setup_context->wind[0]                = wind[0];
157a515125bSLeila Ghaffari   setup_context->wind[1]                = wind[1];
158a515125bSLeila Ghaffari   setup_context->wind[2]                = wind[2];
159a515125bSLeila Ghaffari   setup_context->wind_type              = wind_type;
160a515125bSLeila Ghaffari   setup_context->bubble_type            = bubble_type;
161a515125bSLeila Ghaffari   setup_context->bubble_continuity_type = bubble_continuity_type;
162a515125bSLeila Ghaffari   setup_context->time                   = 0;
163a515125bSLeila Ghaffari 
164a515125bSLeila Ghaffari   // -- QFunction Context
165a515125bSLeila Ghaffari   user->phys->stab                   = stab;
166a515125bSLeila Ghaffari   user->phys->wind_type              = wind_type;
167a515125bSLeila Ghaffari   user->phys->bubble_type            = bubble_type;
168a515125bSLeila Ghaffari   user->phys->bubble_continuity_type = bubble_continuity_type;
169a515125bSLeila Ghaffari   //  if passed correctly
170a515125bSLeila Ghaffari   user->phys->implicit         = implicit;
171a515125bSLeila Ghaffari   user->phys->has_curr_time    = has_curr_time;
17215a3537eSJed Brown   advection_ctx->CtauS         = CtauS;
17315a3537eSJed Brown   advection_ctx->E_wind        = E_wind;
17415a3537eSJed Brown   advection_ctx->implicit      = implicit;
17515a3537eSJed Brown   advection_ctx->strong_form   = strong_form;
17615a3537eSJed Brown   advection_ctx->stabilization = stab;
177a515125bSLeila Ghaffari 
17815a3537eSJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
1792b916ea7SJeremy L Thompson   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
1802b916ea7SJeremy L Thompson   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
181a515125bSLeila Ghaffari 
18215a3537eSJed Brown   CeedQFunctionContextCreate(user->ceed, &advection_context);
1832b916ea7SJeremy L Thompson   CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*advection_ctx), advection_ctx);
1842b916ea7SJeremy L Thompson   CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, FreeContextPetsc);
18515a3537eSJed Brown   problem->apply_vol_rhs.qfunction_context = advection_context;
1862b916ea7SJeremy L Thompson   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_vol_ifunction.qfunction_context);
1872b916ea7SJeremy L Thompson   CeedQFunctionContextReferenceCopy(advection_context, &problem->apply_inflow.qfunction_context);
188*d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
189a515125bSLeila Ghaffari }
190a515125bSLeila Ghaffari 
19140ccd21cSJed Brown PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx) {
192a515125bSLeila Ghaffari   MPI_Comm         comm = PETSC_COMM_WORLD;
1933636f6a4SJames Wright   SetupContextAdv  setup_ctx;
19415a3537eSJed Brown   AdvectionContext advection_ctx;
195a515125bSLeila Ghaffari 
19615a3537eSJed Brown   PetscFunctionBeginUser;
1972b916ea7SJeremy L Thompson   CeedQFunctionContextGetData(problem->ics.qfunction_context, CEED_MEM_HOST, &setup_ctx);
1982b916ea7SJeremy L Thompson   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &advection_ctx);
1992b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(comm,
200a515125bSLeila Ghaffari                         "  Problem:\n"
201a515125bSLeila Ghaffari                         "    Problem Name                       : %s\n"
202a515125bSLeila Ghaffari                         "    Stabilization                      : %s\n"
20345aa3cadSJeremy L Thompson                         "    Bubble Type                        : %s (%" CeedInt_FMT "D)\n"
204a515125bSLeila Ghaffari                         "    Bubble Continuity                  : %s\n"
205a515125bSLeila Ghaffari                         "    Wind Type                          : %s\n",
2062b916ea7SJeremy L Thompson                         app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], BubbleTypes[setup_ctx->bubble_type],
2072b916ea7SJeremy L Thompson                         setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2, BubbleContinuityTypes[setup_ctx->bubble_continuity_type],
2082b916ea7SJeremy L Thompson                         WindTypes[setup_ctx->wind_type]));
209a515125bSLeila Ghaffari 
21015a3537eSJed Brown   if (setup_ctx->wind_type == WIND_TRANSLATION) {
2112b916ea7SJeremy L Thompson     PetscCall(PetscPrintf(comm, "    Background Wind                    : %f,%f,%f\n", setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]));
212a515125bSLeila Ghaffari   }
2132b916ea7SJeremy L Thompson   CeedQFunctionContextRestoreData(problem->ics.qfunction_context, &setup_ctx);
2142b916ea7SJeremy L Thompson   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &advection_ctx);
215*d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
216a515125bSLeila Ghaffari }
217