xref: /libCEED/examples/fluids/problems/newtonian.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.
388b783a1SJames Wright //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
588b783a1SJames Wright //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
788b783a1SJames Wright 
888b783a1SJames Wright /// @file
988b783a1SJames Wright /// Utility functions for setting up problems using the Newtonian Qfunction
1088b783a1SJames Wright 
1188b783a1SJames Wright #include "../navierstokes.h"
1288b783a1SJames Wright #include "../qfunctions/setupgeo.h"
1388b783a1SJames Wright #include "../qfunctions/newtonian.h"
1488b783a1SJames Wright 
1588b783a1SJames Wright PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *setup_ctx,
1688b783a1SJames Wright                                void *ctx) {
1788b783a1SJames Wright   SetupContext      setup_context = *(SetupContext *)setup_ctx;
1888b783a1SJames Wright   User              user = *(User *)ctx;
1988b783a1SJames Wright   StabilizationType stab;
2088b783a1SJames Wright   MPI_Comm          comm = PETSC_COMM_WORLD;
2188b783a1SJames Wright   PetscBool         implicit;
2288b783a1SJames Wright   PetscBool         has_curr_time = PETSC_FALSE;
2388b783a1SJames Wright   PetscInt          ierr;
24*841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ig_ctx;
25*841e4c73SJed Brown   CeedQFunctionContext newtonian_ig_context;
2688b783a1SJames Wright 
27*841e4c73SJed Brown   PetscFunctionBeginUser;
28*841e4c73SJed Brown   ierr = PetscCalloc1(1, &newtonian_ig_ctx); CHKERRQ(ierr);
2988b783a1SJames Wright 
3088b783a1SJames Wright   // ------------------------------------------------------
3188b783a1SJames Wright   //           Setup Generic Newtonian IG Problem
3288b783a1SJames Wright   // ------------------------------------------------------
3388b783a1SJames Wright   problem->dim                               = 3;
3488b783a1SJames Wright   problem->q_data_size_vol                   = 10;
3588b783a1SJames Wright   problem->q_data_size_sur                   = 4;
3691e5af17SJed Brown   problem->setup_vol.qfunction               = Setup;
3791e5af17SJed Brown   problem->setup_vol.qfunction_loc           = Setup_loc;
3891e5af17SJed Brown   problem->ics.qfunction                     = ICsNewtonianIG;
3991e5af17SJed Brown   problem->ics.qfunction_loc                 = ICsNewtonianIG_loc;
4091e5af17SJed Brown   problem->setup_sur.qfunction               = SetupBoundary;
4191e5af17SJed Brown   problem->setup_sur.qfunction_loc           = SetupBoundary_loc;
4291e5af17SJed Brown   problem->apply_vol_rhs.qfunction           = Newtonian;
4391e5af17SJed Brown   problem->apply_vol_rhs.qfunction_loc       = Newtonian_loc;
4491e5af17SJed Brown   problem->apply_vol_ifunction.qfunction     = IFunction_Newtonian;
4591e5af17SJed Brown   problem->apply_vol_ifunction.qfunction_loc = IFunction_Newtonian_loc;
4688b783a1SJames Wright   problem->non_zero_time                     = PETSC_FALSE;
4788b783a1SJames Wright   problem->print_info                        = PRINT_DENSITY_CURRENT;
4888b783a1SJames Wright 
4988b783a1SJames Wright   // ------------------------------------------------------
5088b783a1SJames Wright   //             Create the libCEED context
5188b783a1SJames Wright   // ------------------------------------------------------
5288b783a1SJames Wright   CeedScalar cv     = 717.;          // J/(kg K)
5388b783a1SJames Wright   CeedScalar cp     = 1004.;         // J/(kg K)
5488626eedSJames Wright   CeedScalar g[3]   = {0, 0, -9.81}; // m/s^2
5588b783a1SJames Wright   CeedScalar lambda = -2./3.;        // -
5688626eedSJames Wright   CeedScalar mu     = 1.8e-5;        // Pa s, dynamic viscosity
5788b783a1SJames Wright   CeedScalar k      = 0.02638;       // W/(m K)
5888b783a1SJames Wright   CeedScalar c_tau  = 0.5;           // -
5988626eedSJames Wright   CeedScalar Ctau_t  = 1.0;          // -
6088626eedSJames Wright   CeedScalar Ctau_v  = 36.0;         // TODO make function of degree
6188626eedSJames Wright   CeedScalar Ctau_C  = 1.0;          // TODO make function of degree
6288626eedSJames Wright   CeedScalar Ctau_M  = 1.0;          // TODO make function of degree
6388626eedSJames Wright   CeedScalar Ctau_E  = 1.0;          // TODO make function of degree
6488b783a1SJames Wright   PetscReal domain_min[3], domain_max[3], domain_size[3];
6588b783a1SJames Wright   ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr);
6688b783a1SJames Wright   for (int i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i];
6788b783a1SJames Wright 
6888b783a1SJames Wright   // ------------------------------------------------------
6988b783a1SJames Wright   //             Create the PETSc context
7088b783a1SJames Wright   // ------------------------------------------------------
7188626eedSJames Wright   PetscScalar meter    = 1;  // 1 meter in scaled length units
7288626eedSJames Wright   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
7388626eedSJames Wright   PetscScalar second   = 1;  // 1 second in scaled time units
7488b783a1SJames Wright   PetscScalar Kelvin   = 1;     // 1 Kelvin in scaled temperature units
7588b783a1SJames Wright   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
7688b783a1SJames Wright 
7788b783a1SJames Wright   // ------------------------------------------------------
7888b783a1SJames Wright   //              Command line Options
7988b783a1SJames Wright   // ------------------------------------------------------
8067490bc6SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem",
8167490bc6SJeremy L Thompson                     NULL);
8267490bc6SJeremy L Thompson 
8388b783a1SJames Wright   // -- Physics
8488b783a1SJames Wright   ierr = PetscOptionsScalar("-cv", "Heat capacity at constant volume",
8588b783a1SJames Wright                             NULL, cv, &cv, NULL); CHKERRQ(ierr);
8688b783a1SJames Wright   ierr = PetscOptionsScalar("-cp", "Heat capacity at constant pressure",
8788b783a1SJames Wright                             NULL, cp, &cp, NULL); CHKERRQ(ierr);
8888b783a1SJames Wright   ierr = PetscOptionsScalar("-lambda",
8988b783a1SJames Wright                             "Stokes hypothesis second viscosity coefficient",
9088b783a1SJames Wright                             NULL, lambda, &lambda, NULL); CHKERRQ(ierr);
9188b783a1SJames Wright   ierr = PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient",
9288b783a1SJames Wright                             NULL, mu, &mu, NULL); CHKERRQ(ierr);
9388b783a1SJames Wright   ierr = PetscOptionsScalar("-k", "Thermal conductivity",
9488b783a1SJames Wright                             NULL, k, &k, NULL); CHKERRQ(ierr);
9588b783a1SJames Wright 
9688626eedSJames Wright   PetscInt dim = problem->dim;
9788626eedSJames Wright   ierr = PetscOptionsRealArray("-g", "Gravitational acceleration",
9888626eedSJames Wright                                NULL, g, &dim, NULL); CHKERRQ(ierr);
9988b783a1SJames Wright   ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL,
10088b783a1SJames Wright                           StabilizationTypes, (PetscEnum)(stab = STAB_NONE),
10188b783a1SJames Wright                           (PetscEnum *)&stab, NULL); CHKERRQ(ierr);
10288b783a1SJames Wright   ierr = PetscOptionsScalar("-c_tau", "Stabilization constant",
10388b783a1SJames Wright                             NULL, c_tau, &c_tau, NULL); CHKERRQ(ierr);
10488626eedSJames Wright   ierr = PetscOptionsScalar("-Ctau_t", "Stabilization time constant",
10588626eedSJames Wright                             NULL, Ctau_t, &Ctau_t, NULL); CHKERRQ(ierr);
10688626eedSJames Wright   ierr = PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant",
10788626eedSJames Wright                             NULL, Ctau_v, &Ctau_v, NULL); CHKERRQ(ierr);
10888626eedSJames Wright   ierr = PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant",
10988626eedSJames Wright                             NULL, Ctau_C, &Ctau_C, NULL); CHKERRQ(ierr);
11088626eedSJames Wright   ierr = PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant",
11188626eedSJames Wright                             NULL, Ctau_M, &Ctau_M, NULL); CHKERRQ(ierr);
11288626eedSJames Wright   ierr = PetscOptionsScalar("-Ctau_E", "Stabilization energy constant",
11388626eedSJames Wright                             NULL, Ctau_E, &Ctau_E, NULL); CHKERRQ(ierr);
11488b783a1SJames Wright   ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation",
11588b783a1SJames Wright                           NULL, implicit=PETSC_FALSE, &implicit, NULL);
11688b783a1SJames Wright   CHKERRQ(ierr);
11788b783a1SJames Wright 
11888b783a1SJames Wright   // -- Units
11988b783a1SJames Wright   ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units",
12088b783a1SJames Wright                             NULL, meter, &meter, NULL); CHKERRQ(ierr);
12188b783a1SJames Wright   meter = fabs(meter);
12288b783a1SJames Wright   ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units",
12388b783a1SJames Wright                             NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr);
12488b783a1SJames Wright   kilogram = fabs(kilogram);
12588b783a1SJames Wright   ierr = PetscOptionsScalar("-units_second","1 second in scaled time units",
12688b783a1SJames Wright                             NULL, second, &second, NULL); CHKERRQ(ierr);
12788b783a1SJames Wright   second = fabs(second);
12888b783a1SJames Wright   ierr = PetscOptionsScalar("-units_Kelvin",
12988b783a1SJames Wright                             "1 Kelvin in scaled temperature units",
13088b783a1SJames Wright                             NULL, Kelvin, &Kelvin, NULL); CHKERRQ(ierr);
13188b783a1SJames Wright   Kelvin = fabs(Kelvin);
13288b783a1SJames Wright 
13388b783a1SJames Wright   // -- Warnings
13488b783a1SJames Wright   if (stab == STAB_SUPG && !implicit) {
13588b783a1SJames Wright     ierr = PetscPrintf(comm,
13688b783a1SJames Wright                        "Warning! Use -stab supg only with -implicit\n");
13788b783a1SJames Wright     CHKERRQ(ierr);
13888b783a1SJames Wright   }
13967490bc6SJeremy L Thompson   PetscOptionsEnd();
14088b783a1SJames Wright 
14188b783a1SJames Wright   // ------------------------------------------------------
14288b783a1SJames Wright   //           Set up the PETSc context
14388b783a1SJames Wright   // ------------------------------------------------------
14488b783a1SJames Wright   // -- Define derived units
14588b783a1SJames Wright   Pascal          = kilogram / (meter * PetscSqr(second));
14688b783a1SJames Wright   J_per_kg_K      =  PetscSqr(meter) / (PetscSqr(second) * Kelvin);
14788b783a1SJames Wright   m_per_squared_s = meter / PetscSqr(second);
14888b783a1SJames Wright   W_per_m_K       = kilogram * meter / (pow(second,3) * Kelvin);
14988b783a1SJames Wright 
15088b783a1SJames Wright   user->units->meter           = meter;
15188b783a1SJames Wright   user->units->kilogram        = kilogram;
15288b783a1SJames Wright   user->units->second          = second;
15388b783a1SJames Wright   user->units->Kelvin          = Kelvin;
15488b783a1SJames Wright   user->units->Pascal          = Pascal;
15588b783a1SJames Wright   user->units->J_per_kg_K      = J_per_kg_K;
15688b783a1SJames Wright   user->units->m_per_squared_s = m_per_squared_s;
15788b783a1SJames Wright   user->units->W_per_m_K       = W_per_m_K;
15888b783a1SJames Wright 
15988b783a1SJames Wright   // ------------------------------------------------------
16088b783a1SJames Wright   //           Set up the libCEED context
16188b783a1SJames Wright   // ------------------------------------------------------
16288b783a1SJames Wright   // -- Scale variables to desired units
16388b783a1SJames Wright   cv     *= J_per_kg_K;
16488b783a1SJames Wright   cp     *= J_per_kg_K;
16588b783a1SJames Wright   mu     *= Pascal * second;
16688b783a1SJames Wright   k      *= W_per_m_K;
16788b783a1SJames Wright   for (int i=0; i<3; i++) domain_size[i] *= meter;
16888626eedSJames Wright   for (int i=0; i<3; i++) g[i]           *= m_per_squared_s;
16988b783a1SJames Wright   problem->dm_scale = meter;
17088b783a1SJames Wright 
17188b783a1SJames Wright   // -- Setup Context
17288b783a1SJames Wright   setup_context->cv         = cv;
17388b783a1SJames Wright   setup_context->cp         = cp;
17488b783a1SJames Wright   setup_context->lx         = domain_size[0];
17588b783a1SJames Wright   setup_context->ly         = domain_size[1];
17688b783a1SJames Wright   setup_context->lz         = domain_size[2];
17788b783a1SJames Wright   setup_context->time       = 0;
17888626eedSJames Wright   ierr = PetscArraycpy(setup_context->g, g, 3); CHKERRQ(ierr);
17988b783a1SJames Wright 
18088b783a1SJames Wright   // -- Solver Settings
18188b783a1SJames Wright   user->phys->stab          = stab;
18288b783a1SJames Wright   user->phys->implicit      = implicit;
18388b783a1SJames Wright   user->phys->has_curr_time = has_curr_time;
18488b783a1SJames Wright 
18588b783a1SJames Wright   // -- QFunction Context
186*841e4c73SJed Brown   newtonian_ig_ctx->lambda        = lambda;
187*841e4c73SJed Brown   newtonian_ig_ctx->mu            = mu;
188*841e4c73SJed Brown   newtonian_ig_ctx->k             = k;
189*841e4c73SJed Brown   newtonian_ig_ctx->cv            = cv;
190*841e4c73SJed Brown   newtonian_ig_ctx->cp            = cp;
191*841e4c73SJed Brown   newtonian_ig_ctx->c_tau         = c_tau;
192*841e4c73SJed Brown   newtonian_ig_ctx->Ctau_t        = Ctau_t;
193*841e4c73SJed Brown   newtonian_ig_ctx->Ctau_v        = Ctau_v;
194*841e4c73SJed Brown   newtonian_ig_ctx->Ctau_C        = Ctau_C;
195*841e4c73SJed Brown   newtonian_ig_ctx->Ctau_M        = Ctau_M;
196*841e4c73SJed Brown   newtonian_ig_ctx->Ctau_E        = Ctau_E;
197*841e4c73SJed Brown   newtonian_ig_ctx->stabilization = stab;
198*841e4c73SJed Brown   ierr = PetscArraycpy(newtonian_ig_ctx->g, g, 3); CHKERRQ(ierr);
19988b783a1SJames Wright 
200*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
201*841e4c73SJed Brown   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST,
202*841e4c73SJed Brown                               CEED_USE_POINTER, sizeof(*setup_context), setup_context);
203*841e4c73SJed Brown   CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context,
204*841e4c73SJed Brown                                      "evaluation time",
205*841e4c73SJed Brown                                      (char *)&setup_context->time - (char *)setup_context, 1, "Time of evaluation");
206*841e4c73SJed Brown 
207*841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context);
208*841e4c73SJed Brown   CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST,
209*841e4c73SJed Brown                               CEED_USE_POINTER,
210*841e4c73SJed Brown                               sizeof(*newtonian_ig_ctx), newtonian_ig_ctx);
211*841e4c73SJed Brown   CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST,
212*841e4c73SJed Brown                                      FreeContextPetsc);
213*841e4c73SJed Brown   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size",
214*841e4c73SJed Brown                                      offsetof(struct NewtonianIdealGasContext_, dt), 1, "Size of timestep, delta t");
215*841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = newtonian_ig_context;
216*841e4c73SJed Brown   CeedQFunctionContextReferenceCopy(newtonian_ig_context,
217*841e4c73SJed Brown                                     &problem->apply_vol_ifunction.qfunction_context);
21888b783a1SJames Wright   PetscFunctionReturn(0);
21988b783a1SJames Wright }
22088b783a1SJames Wright 
221*841e4c73SJed Brown PetscErrorCode PRINT_DENSITY_CURRENT(ProblemData *problem,
222*841e4c73SJed Brown                                      SetupContext setup_ctx,
223*841e4c73SJed Brown                                      AppCtx app_ctx) {
224*841e4c73SJed Brown   MPI_Comm comm = PETSC_COMM_WORLD;
225*841e4c73SJed Brown   PetscErrorCode ierr;
226*841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ctx;
227*841e4c73SJed Brown 
22888b783a1SJames Wright   PetscFunctionBeginUser;
229*841e4c73SJed Brown   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context,
230*841e4c73SJed Brown                               CEED_MEM_HOST, &newtonian_ctx);
231*841e4c73SJed Brown   ierr = PetscPrintf(comm,
232*841e4c73SJed Brown                      "  Problem:\n"
233*841e4c73SJed Brown                      "    Problem Name                       : %s\n"
234*841e4c73SJed Brown                      "    Stabilization                      : %s\n",
235*841e4c73SJed Brown                      app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]);
236*841e4c73SJed Brown   CHKERRQ(ierr);
237*841e4c73SJed Brown   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context,
238*841e4c73SJed Brown                                   &newtonian_ctx);
23988b783a1SJames Wright   PetscFunctionReturn(0);
24088b783a1SJames Wright }
241