xref: /libCEED/examples/fluids/problems/newtonian.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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 "../qfunctions/newtonian.h"
1288b783a1SJames Wright 
13*49aac155SJeremy L Thompson #include <ceed.h>
14*49aac155SJeremy L Thompson #include <petscdm.h>
15*49aac155SJeremy L Thompson 
162b730f8bSJeremy L Thompson #include "../navierstokes.h"
172b730f8bSJeremy L Thompson #include "../qfunctions/setupgeo.h"
186ef2784eSLeila Ghaffari 
19d64246eaSJed Brown // For use with PetscOptionsEnum
20d64246eaSJed Brown static const char *const StateVariables[] = {"CONSERVATIVE", "PRIMITIVE", "StateVariable", "STATEVAR_", NULL};
21d64246eaSJed Brown 
222b730f8bSJeremy L Thompson // Compute relative error |a - b|/|s|
232b730f8bSJeremy L Thompson static PetscErrorCode CheckPrimitiveWithTolerance(StatePrimitive sY, StatePrimitive aY, StatePrimitive bY, const char *name, PetscReal rtol_pressure,
242b730f8bSJeremy L Thompson                                                   PetscReal rtol_velocity, PetscReal rtol_temperature) {
25e334ad8fSJed Brown   PetscFunctionBeginUser;
26e334ad8fSJed Brown   StatePrimitive eY;  // relative error
27e334ad8fSJed Brown   eY.pressure   = (aY.pressure - bY.pressure) / sY.pressure;
282b730f8bSJeremy L Thompson   PetscScalar u = sqrt(Square(sY.velocity[0]) + Square(sY.velocity[1]) + Square(sY.velocity[2]));
29e334ad8fSJed Brown   for (int j = 0; j < 3; j++) eY.velocity[j] = (aY.velocity[j] - bY.velocity[j]) / u;
30e334ad8fSJed Brown   eY.temperature = (aY.temperature - bY.temperature) / sY.temperature;
312b730f8bSJeremy L Thompson   if (fabs(eY.pressure) > rtol_pressure) printf("%s: pressure error %g\n", name, eY.pressure);
322b730f8bSJeremy L Thompson   for (int j = 0; j < 3; j++) {
332b730f8bSJeremy L Thompson     if (fabs(eY.velocity[j]) > rtol_velocity) printf("%s: velocity[%d] error %g\n", name, j, eY.velocity[j]);
342b730f8bSJeremy L Thompson   }
352b730f8bSJeremy L Thompson   if (fabs(eY.temperature) > rtol_temperature) printf("%s: temperature error %g\n", name, eY.temperature);
36e334ad8fSJed Brown   PetscFunctionReturn(0);
37e334ad8fSJed Brown }
38e334ad8fSJed Brown 
392b730f8bSJeremy L Thompson static PetscErrorCode UnitTests_Newtonian(User user, NewtonianIdealGasContext gas) {
40e334ad8fSJed Brown   Units            units = user->units;
41e334ad8fSJed Brown   const CeedScalar eps   = 1e-6;
422b730f8bSJeremy L Thompson   const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second, Pascal = units->Pascal;
43e334ad8fSJed Brown   PetscFunctionBeginUser;
442b730f8bSJeremy L Thompson   const CeedScalar rho = 1.2 * kg / (m * m * m), u = 40 * m / sec;
45e334ad8fSJed Brown   CeedScalar       U[5] = {rho, rho * u, rho * u * 1.1, rho * u * 1.2, 250e3 * Pascal + .5 * rho * u * u};
46e334ad8fSJed Brown   const CeedScalar x[3] = {.1, .2, .3};
47e334ad8fSJed Brown   State            s    = StateFromU(gas, U, x);
48e334ad8fSJed Brown   for (int i = 0; i < 8; i++) {
49e334ad8fSJed Brown     CeedScalar dU[5] = {0}, dx[3] = {0};
50e334ad8fSJed Brown     if (i < 5) dU[i] = U[i];
51e334ad8fSJed Brown     else dx[i - 5] = x[i - 5];
52e334ad8fSJed Brown     State ds = StateFromU_fwd(gas, s, dU, x, dx);
53e334ad8fSJed Brown     for (int j = 0; j < 5; j++) dU[j] = (1 + eps * (i == j)) * U[j];
54e334ad8fSJed Brown     for (int j = 0; j < 3; j++) dx[j] = (1 + eps * (i == 5 + j)) * x[j];
55e334ad8fSJed Brown     State          t = StateFromU(gas, dU, dx);
56e334ad8fSJed Brown     StatePrimitive dY;
57e334ad8fSJed Brown     dY.pressure = (t.Y.pressure - s.Y.pressure) / eps;
582b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) dY.velocity[j] = (t.Y.velocity[j] - s.Y.velocity[j]) / eps;
59e334ad8fSJed Brown     dY.temperature = (t.Y.temperature - s.Y.temperature) / eps;
60e334ad8fSJed Brown     char buf[128];
61e334ad8fSJed Brown     snprintf(buf, sizeof buf, "StateFromU_fwd i=%d", i);
62e334ad8fSJed Brown     PetscCall(CheckPrimitiveWithTolerance(dY, ds.Y, dY, buf, 5e-6, 1e-6, 1e-6));
63e334ad8fSJed Brown   }
64e334ad8fSJed Brown   PetscFunctionReturn(0);
65e334ad8fSJed Brown }
66e334ad8fSJed Brown 
6746de7363SJames Wright PetscErrorCode NS_NEWTONIAN_IG(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) {
68a0add3c9SJed Brown   SetupContext             setup_context;
6988b783a1SJames Wright   User                     user   = *(User *)ctx;
70ebd2ea64SLeila Ghaffari   CeedInt                  degree = user->app_ctx->degree;
7188b783a1SJames Wright   StabilizationType        stab;
7297baf651SJames Wright   StateVariable            state_var;
7388b783a1SJames Wright   MPI_Comm                 comm = PETSC_COMM_WORLD;
7488b783a1SJames Wright   PetscBool                implicit;
7597baf651SJames Wright   PetscBool                has_curr_time = PETSC_FALSE, unit_tests;
76841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ig_ctx;
77841e4c73SJed Brown   CeedQFunctionContext     newtonian_ig_context;
7888b783a1SJames Wright 
79841e4c73SJed Brown   PetscFunctionBeginUser;
802b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
812b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &newtonian_ig_ctx));
8288b783a1SJames Wright 
8388b783a1SJames Wright   // ------------------------------------------------------
8488b783a1SJames Wright   //           Setup Generic Newtonian IG Problem
8588b783a1SJames Wright   // ------------------------------------------------------
8688b783a1SJames Wright   problem->dim                     = 3;
8788b783a1SJames Wright   problem->q_data_size_vol         = 10;
88ba6664aeSJames Wright   problem->q_data_size_sur         = 10;
890ec2498eSJames Wright   problem->jac_data_size_sur       = 11;
9091e5af17SJed Brown   problem->setup_vol.qfunction     = Setup;
9191e5af17SJed Brown   problem->setup_vol.qfunction_loc = Setup_loc;
9291e5af17SJed Brown   problem->setup_sur.qfunction     = SetupBoundary;
9391e5af17SJed Brown   problem->setup_sur.qfunction_loc = SetupBoundary_loc;
94a0add3c9SJed Brown   problem->bc                      = NULL;
95d310b3d3SAdeleke O. Bankole   problem->bc_ctx                  = NULL;
9688b783a1SJames Wright   problem->non_zero_time           = PETSC_FALSE;
97dc805cc4SLeila Ghaffari   problem->print_info              = PRINT_NEWTONIAN;
9888b783a1SJames Wright 
9988b783a1SJames Wright   // ------------------------------------------------------
10088b783a1SJames Wright   //             Create the libCEED context
10188b783a1SJames Wright   // ------------------------------------------------------
10288b783a1SJames Wright   CeedScalar cv         = 717.;           // J/(kg K)
10388b783a1SJames Wright   CeedScalar cp         = 1004.;          // J/(kg K)
10488626eedSJames Wright   CeedScalar g[3]       = {0, 0, -9.81};  // m/s^2
10588b783a1SJames Wright   CeedScalar lambda     = -2. / 3.;       // -
10688626eedSJames Wright   CeedScalar mu         = 1.8e-5;         // Pa s, dynamic viscosity
10788b783a1SJames Wright   CeedScalar k          = 0.02638;        // W/(m K)
108ebd2ea64SLeila Ghaffari   CeedScalar c_tau      = 0.5 / degree;   // -
10988626eedSJames Wright   CeedScalar Ctau_t     = 1.0;            // -
11094c01735SLeila Ghaffari   CeedScalar Cv_func[3] = {36, 60, 128};
111c5dde687SLeila Ghaffari   CeedScalar Ctau_v     = Cv_func[(CeedInt)Min(3, degree) - 1];
11275538696SLeila Ghaffari   CeedScalar Ctau_C     = 0.25 / degree;
11375538696SLeila Ghaffari   CeedScalar Ctau_M     = 0.25 / degree;
11475538696SLeila Ghaffari   CeedScalar Ctau_E     = 0.125;
11588b783a1SJames Wright   PetscReal  domain_min[3], domain_max[3], domain_size[3];
1162b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
117ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
11888b783a1SJames Wright 
119d310b3d3SAdeleke O. Bankole   StatePrimitive reference      = {.pressure = 1.01e5, .velocity = {0}, .temperature = 288.15};
120530ad8c4SKenneth E. Jansen   CeedScalar     idl_decay_time = -1, idl_start = 0, idl_length = 0;
121530ad8c4SKenneth E. Jansen   PetscBool      idl_enable = PETSC_FALSE;
122d310b3d3SAdeleke O. Bankole 
12388b783a1SJames Wright   // ------------------------------------------------------
12488b783a1SJames Wright   //             Create the PETSc context
12588b783a1SJames Wright   // ------------------------------------------------------
12688626eedSJames Wright   PetscScalar meter    = 1;  // 1 meter in scaled length units
12788626eedSJames Wright   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
12888626eedSJames Wright   PetscScalar second   = 1;  // 1 second in scaled time units
12988b783a1SJames Wright   PetscScalar Kelvin   = 1;  // 1 Kelvin in scaled temperature units
13088b783a1SJames Wright   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
13188b783a1SJames Wright 
13288b783a1SJames Wright   // ------------------------------------------------------
13388b783a1SJames Wright   //              Command line Options
13488b783a1SJames Wright   // ------------------------------------------------------
1352b730f8bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", NULL);
136dc805cc4SLeila Ghaffari   // -- Conservative vs Primitive variables
1372b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-state_var", "State variables used", NULL, StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE),
1382b730f8bSJeremy L Thompson                              (PetscEnum *)&state_var, NULL));
13997baf651SJames Wright 
14097baf651SJames Wright   switch (state_var) {
14197baf651SJames Wright     case STATEVAR_CONSERVATIVE:
142d310b3d3SAdeleke O. Bankole       problem->ics.qfunction                       = ICsNewtonianIG_Conserv;
143d310b3d3SAdeleke O. Bankole       problem->ics.qfunction_loc                   = ICsNewtonianIG_Conserv_loc;
144dc805cc4SLeila Ghaffari       problem->apply_vol_rhs.qfunction             = RHSFunction_Newtonian;
145dc805cc4SLeila Ghaffari       problem->apply_vol_rhs.qfunction_loc         = RHSFunction_Newtonian_loc;
1463d02368aSJames Wright       problem->apply_vol_ifunction.qfunction       = IFunction_Newtonian_Conserv;
1473d02368aSJames Wright       problem->apply_vol_ifunction.qfunction_loc   = IFunction_Newtonian_Conserv_loc;
1483d02368aSJames Wright       problem->apply_vol_ijacobian.qfunction       = IJacobian_Newtonian_Conserv;
1493d02368aSJames Wright       problem->apply_vol_ijacobian.qfunction_loc   = IJacobian_Newtonian_Conserv_loc;
15020840d50SJames Wright       problem->apply_inflow.qfunction              = BoundaryIntegral_Conserv;
15120840d50SJames Wright       problem->apply_inflow.qfunction_loc          = BoundaryIntegral_Conserv_loc;
15220840d50SJames Wright       problem->apply_inflow_jacobian.qfunction     = BoundaryIntegral_Jacobian_Conserv;
15320840d50SJames Wright       problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Conserv_loc;
15497baf651SJames Wright       break;
15597baf651SJames Wright 
15697baf651SJames Wright     case STATEVAR_PRIMITIVE:
15797baf651SJames Wright       problem->ics.qfunction                       = ICsNewtonianIG_Prim;
15897baf651SJames Wright       problem->ics.qfunction_loc                   = ICsNewtonianIG_Prim_loc;
15997baf651SJames Wright       problem->apply_vol_ifunction.qfunction       = IFunction_Newtonian_Prim;
16097baf651SJames Wright       problem->apply_vol_ifunction.qfunction_loc   = IFunction_Newtonian_Prim_loc;
16197baf651SJames Wright       problem->apply_vol_ijacobian.qfunction       = IJacobian_Newtonian_Prim;
16297baf651SJames Wright       problem->apply_vol_ijacobian.qfunction_loc   = IJacobian_Newtonian_Prim_loc;
16397baf651SJames Wright       problem->apply_inflow.qfunction              = BoundaryIntegral_Prim;
16497baf651SJames Wright       problem->apply_inflow.qfunction_loc          = BoundaryIntegral_Prim_loc;
16597baf651SJames Wright       problem->apply_inflow_jacobian.qfunction     = BoundaryIntegral_Jacobian_Prim;
16697baf651SJames Wright       problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Prim_loc;
16797baf651SJames Wright       break;
168dc805cc4SLeila Ghaffari   }
16967490bc6SJeremy L Thompson 
17088b783a1SJames Wright   // -- Physics
1712b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-cv", "Heat capacity at constant volume", NULL, cv, &cv, NULL));
1722b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-cp", "Heat capacity at constant pressure", NULL, cp, &cp, NULL));
1732b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-lambda", "Stokes hypothesis second viscosity coefficient", NULL, lambda, &lambda, NULL));
1742b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", NULL, mu, &mu, NULL));
1752b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-k", "Thermal conductivity", NULL, k, &k, NULL));
17688b783a1SJames Wright 
17788626eedSJames Wright   PetscInt dim = problem->dim;
1782b730f8bSJeremy L Thompson   PetscCall(PetscOptionsRealArray("-g", "Gravitational acceleration", NULL, g, &dim, NULL));
1792b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
1802b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
1812b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL));
1822b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", NULL, Ctau_v, &Ctau_v, NULL));
1832b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", NULL, Ctau_C, &Ctau_C, NULL));
1842b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", NULL, Ctau_M, &Ctau_M, NULL));
1852b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", NULL, Ctau_E, &Ctau_E, NULL));
1862b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
1872b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", NULL, unit_tests = PETSC_FALSE, &unit_tests, NULL));
18888b783a1SJames Wright 
189d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_pressure", "Reference/initial pressure", NULL, reference.pressure, &reference.pressure, NULL));
190d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalarArray("-reference_velocity", "Reference/initial velocity", NULL, reference.velocity, &dim, NULL));
191d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_temperature", "Reference/initial temperature", NULL, reference.temperature, &reference.temperature, NULL));
192d310b3d3SAdeleke O. Bankole 
19388b783a1SJames Wright   // -- Units
1942b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
19588b783a1SJames Wright   meter = fabs(meter);
1962b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
19788b783a1SJames Wright   kilogram = fabs(kilogram);
1982b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
19988b783a1SJames Wright   second = fabs(second);
2002b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_Kelvin", "1 Kelvin in scaled temperature units", NULL, Kelvin, &Kelvin, NULL));
20188b783a1SJames Wright   Kelvin = fabs(Kelvin);
20288b783a1SJames Wright 
20388b783a1SJames Wright   // -- Warnings
20488b783a1SJames Wright   if (stab == STAB_SUPG && !implicit) {
2052b730f8bSJeremy L Thompson     PetscCall(PetscPrintf(comm, "Warning! Use -stab supg only with -implicit\n"));
20688b783a1SJames Wright   }
20797baf651SJames Wright   if (state_var == STATEVAR_PRIMITIVE && !implicit) {
208530ad8c4SKenneth E. Jansen     SETERRQ(comm, PETSC_ERR_SUP, "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n");
209dc805cc4SLeila Ghaffari   }
210530ad8c4SKenneth E. Jansen 
211530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_decay_time", "Characteristic timescale of the pressure deviance decay. The timestep is good starting point",
212530ad8c4SKenneth E. Jansen                                NULL, idl_decay_time, &idl_decay_time, &idl_enable));
213530ad8c4SKenneth E. Jansen   if (idl_enable && idl_decay_time == 0) SETERRQ(comm, PETSC_ERR_SUP, "idl_decay_time may not be equal to zero.");
214530ad8c4SKenneth E. Jansen   else if (idl_decay_time < 0) idl_enable = PETSC_FALSE;
215530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_start", "Start of IDL in the x direction", NULL, idl_start, &idl_start, NULL));
216530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_length", "Length of IDL in the positive x direction", NULL, idl_length, &idl_length, NULL));
21767490bc6SJeremy L Thompson   PetscOptionsEnd();
21888b783a1SJames Wright 
21988b783a1SJames Wright   // ------------------------------------------------------
22088b783a1SJames Wright   //           Set up the PETSc context
22188b783a1SJames Wright   // ------------------------------------------------------
22288b783a1SJames Wright   // -- Define derived units
22388b783a1SJames Wright   Pascal          = kilogram / (meter * PetscSqr(second));
22488b783a1SJames Wright   J_per_kg_K      = PetscSqr(meter) / (PetscSqr(second) * Kelvin);
22588b783a1SJames Wright   m_per_squared_s = meter / PetscSqr(second);
22688b783a1SJames Wright   W_per_m_K       = kilogram * meter / (pow(second, 3) * Kelvin);
22788b783a1SJames Wright 
22888b783a1SJames Wright   user->units->meter           = meter;
22988b783a1SJames Wright   user->units->kilogram        = kilogram;
23088b783a1SJames Wright   user->units->second          = second;
23188b783a1SJames Wright   user->units->Kelvin          = Kelvin;
23288b783a1SJames Wright   user->units->Pascal          = Pascal;
23388b783a1SJames Wright   user->units->J_per_kg_K      = J_per_kg_K;
23488b783a1SJames Wright   user->units->m_per_squared_s = m_per_squared_s;
23588b783a1SJames Wright   user->units->W_per_m_K       = W_per_m_K;
23688b783a1SJames Wright 
23788b783a1SJames Wright   // ------------------------------------------------------
23888b783a1SJames Wright   //           Set up the libCEED context
23988b783a1SJames Wright   // ------------------------------------------------------
24088b783a1SJames Wright   // -- Scale variables to desired units
24188b783a1SJames Wright   cv *= J_per_kg_K;
24288b783a1SJames Wright   cp *= J_per_kg_K;
24388b783a1SJames Wright   mu *= Pascal * second;
24488b783a1SJames Wright   k *= W_per_m_K;
245ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] *= meter;
246ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) g[i] *= m_per_squared_s;
247d310b3d3SAdeleke O. Bankole   reference.pressure *= Pascal;
248d310b3d3SAdeleke O. Bankole   for (PetscInt i = 0; i < 3; i++) reference.velocity[i] *= meter / second;
249d310b3d3SAdeleke O. Bankole   reference.temperature *= Kelvin;
25088b783a1SJames Wright   problem->dm_scale = meter;
25188b783a1SJames Wright 
25288b783a1SJames Wright   // -- Solver Settings
25388b783a1SJames Wright   user->phys->stab          = stab;
25488b783a1SJames Wright   user->phys->implicit      = implicit;
25597baf651SJames Wright   user->phys->state_var     = state_var;
25688b783a1SJames Wright   user->phys->has_curr_time = has_curr_time;
25788b783a1SJames Wright 
25888b783a1SJames Wright   // -- QFunction Context
259841e4c73SJed Brown   newtonian_ig_ctx->lambda        = lambda;
260841e4c73SJed Brown   newtonian_ig_ctx->mu            = mu;
261841e4c73SJed Brown   newtonian_ig_ctx->k             = k;
262841e4c73SJed Brown   newtonian_ig_ctx->cv            = cv;
263841e4c73SJed Brown   newtonian_ig_ctx->cp            = cp;
264841e4c73SJed Brown   newtonian_ig_ctx->c_tau         = c_tau;
265841e4c73SJed Brown   newtonian_ig_ctx->Ctau_t        = Ctau_t;
266841e4c73SJed Brown   newtonian_ig_ctx->Ctau_v        = Ctau_v;
267841e4c73SJed Brown   newtonian_ig_ctx->Ctau_C        = Ctau_C;
268841e4c73SJed Brown   newtonian_ig_ctx->Ctau_M        = Ctau_M;
269841e4c73SJed Brown   newtonian_ig_ctx->Ctau_E        = Ctau_E;
270530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->P0            = reference.pressure;
271841e4c73SJed Brown   newtonian_ig_ctx->stabilization = stab;
2728a94a473SJed Brown   newtonian_ig_ctx->P0            = reference.pressure;
27365dd5cafSJames Wright   newtonian_ig_ctx->is_implicit   = implicit;
27497baf651SJames Wright   newtonian_ig_ctx->state_var     = state_var;
275530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_enable    = idl_enable;
276530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_amplitude = 1 / (idl_decay_time * second);
277530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_start     = idl_start * meter;
278530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_length    = idl_length * meter;
2792b730f8bSJeremy L Thompson   PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3));
28088b783a1SJames Wright 
281d310b3d3SAdeleke O. Bankole   // -- Setup Context
282d310b3d3SAdeleke O. Bankole   setup_context->reference = reference;
283d310b3d3SAdeleke O. Bankole   setup_context->gas       = *newtonian_ig_ctx;
284d310b3d3SAdeleke O. Bankole   setup_context->lx        = domain_size[0];
285d310b3d3SAdeleke O. Bankole   setup_context->ly        = domain_size[1];
286d310b3d3SAdeleke O. Bankole   setup_context->lz        = domain_size[2];
287d310b3d3SAdeleke O. Bankole   setup_context->time      = 0;
288d310b3d3SAdeleke O. Bankole 
289d310b3d3SAdeleke O. Bankole   if (bc->num_freestream > 0) PetscCall(FreestreamBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
2908a94a473SJed Brown   if (bc->num_outflow > 0) PetscCall(OutflowBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
291c5c39209SJames Wright 
292841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context);
2932b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context);
2942b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc);
295d310b3d3SAdeleke O. Bankole   CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context, "evaluation time", offsetof(struct SetupContext_, time), 1,
2962b730f8bSJeremy L Thompson                                      "Time of evaluation");
297841e4c73SJed Brown 
298841e4c73SJed Brown   CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context);
2992b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newtonian_ig_ctx), newtonian_ig_ctx);
3002b730f8bSJeremy L Thompson   CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST, FreeContextPetsc);
3012b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size", offsetof(struct NewtonianIdealGasContext_, dt), 1,
3022b730f8bSJeremy L Thompson                                      "Size of timestep, delta t");
3032b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift", offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift),
3042b730f8bSJeremy L Thompson                                      1, "Shift for mass matrix in IJacobian");
305f179a332SJames Wright   CeedQFunctionContextRegisterDouble(newtonian_ig_context, "solution time", offsetof(struct NewtonianIdealGasContext_, time), 1,
306f179a332SJames Wright                                      "Current solution time");
307f179a332SJames Wright 
308841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = newtonian_ig_context;
3092b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ifunction.qfunction_context);
3102b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ijacobian.qfunction_context);
3112b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow.qfunction_context);
3122b730f8bSJeremy L Thompson   CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow_jacobian.qfunction_context);
313e334ad8fSJed Brown 
314e334ad8fSJed Brown   if (unit_tests) {
315e334ad8fSJed Brown     PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx));
316e334ad8fSJed Brown   }
31788b783a1SJames Wright   PetscFunctionReturn(0);
31888b783a1SJames Wright }
31988b783a1SJames Wright 
320dc805cc4SLeila Ghaffari PetscErrorCode PRINT_NEWTONIAN(ProblemData *problem, AppCtx app_ctx) {
321841e4c73SJed Brown   MPI_Comm                 comm = PETSC_COMM_WORLD;
322841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ctx;
323841e4c73SJed Brown 
32488b783a1SJames Wright   PetscFunctionBeginUser;
3252b730f8bSJeremy L Thompson   CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ctx);
3262b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
327841e4c73SJed Brown                         "  Problem:\n"
328841e4c73SJed Brown                         "    Problem Name                       : %s\n"
329841e4c73SJed Brown                         "    Stabilization                      : %s\n",
3302b730f8bSJeremy L Thompson                         app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]));
3312b730f8bSJeremy L Thompson   CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ctx);
33288b783a1SJames Wright   PetscFunctionReturn(0);
33388b783a1SJames Wright }
334