xref: /libCEED/examples/fluids/problems/newtonian.c (revision 02b29df7b0ab76fdb65016753e3b39a54ba3f090)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 
1349aac155SJeremy L Thompson #include <ceed.h>
1449aac155SJeremy L Thompson #include <petscdm.h>
1549aac155SJeremy L Thompson 
162b730f8bSJeremy L Thompson #include "../navierstokes.h"
176ef2784eSLeila Ghaffari 
18d64246eaSJed Brown // For use with PetscOptionsEnum
19*02b29df7SJames Wright static const char *const StateVariables[] = {"conservative", "primitive", "entropy", "StateVariable", "STATEVAR_", NULL};
20d64246eaSJed Brown 
21*02b29df7SJames Wright static PetscErrorCode CheckQWithTolerance(const CeedScalar Q_s[5], const CeedScalar Q_a[5], const CeedScalar Q_b[5], const char *name,
22*02b29df7SJames Wright                                           PetscReal rtol_0, PetscReal rtol_u, PetscReal rtol_4) {
23*02b29df7SJames Wright   CeedScalar relative_error[5];  // relative error
24*02b29df7SJames Wright   CeedScalar divisor_threshold = 10 * CEED_EPSILON;
25f17d818dSJames Wright 
26f17d818dSJames Wright   PetscFunctionBeginUser;
27*02b29df7SJames Wright   relative_error[0] = (Q_a[0] - Q_b[0]) / (fabs(Q_s[0]) > divisor_threshold ? Q_s[0] : 1);
28*02b29df7SJames Wright   relative_error[4] = (Q_a[4] - Q_b[4]) / (fabs(Q_s[4]) > divisor_threshold ? Q_s[4] : 1);
29*02b29df7SJames Wright 
30*02b29df7SJames Wright   CeedScalar u_magnitude = sqrt(Square(Q_s[1]) + Square(Q_s[2]) + Square(Q_s[3]));
31*02b29df7SJames Wright   CeedScalar u_divisor   = u_magnitude > divisor_threshold ? u_magnitude : 1;
32*02b29df7SJames Wright   for (int i = 1; i < 4; i++) {
33*02b29df7SJames Wright     relative_error[i] = (Q_a[i] - Q_b[i]) / u_divisor;
342b730f8bSJeremy L Thompson   }
35*02b29df7SJames Wright 
36*02b29df7SJames Wright   if (fabs(relative_error[0]) >= rtol_0) {
37*02b29df7SJames Wright     printf("%s[0] error %g (expected %.10e, got %.10e)\n", name, relative_error[0], Q_s[0], Q_a[0]);
38*02b29df7SJames Wright   }
39*02b29df7SJames Wright   for (int i = 1; i < 4; i++) {
40*02b29df7SJames Wright     if (fabs(relative_error[i]) >= rtol_u) {
41*02b29df7SJames Wright       printf("%s[%d] error %g (expected %.10e, got %.10e)\n", name, i, relative_error[i], Q_s[i], Q_a[i]);
42*02b29df7SJames Wright     }
43*02b29df7SJames Wright   }
44*02b29df7SJames Wright   if (fabs(relative_error[4]) >= rtol_4) {
45*02b29df7SJames Wright     printf("%s[4] error %g (expected %.10e, got %.10e)\n", name, relative_error[4], Q_s[4], Q_a[4]);
46*02b29df7SJames Wright   }
47ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
48e334ad8fSJed Brown }
49e334ad8fSJed Brown 
50*02b29df7SJames Wright // @brief Verify `StateFromQ` by converting A0 -> B0 -> A0_test, where A0 should equal A0_test
51*02b29df7SJames Wright static PetscErrorCode TestState(StateVariable state_var_A, StateVariable state_var_B, NewtonianIdealGasContext gas, const CeedScalar A0[5],
52*02b29df7SJames Wright                                 CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) {
53*02b29df7SJames Wright   CeedScalar        B0[5], A0_test[5];
54*02b29df7SJames Wright   char              buf[128];
55*02b29df7SJames Wright   const char *const StateVariables_Initial[] = {"U", "Y", "V"};
56*02b29df7SJames Wright 
57*02b29df7SJames Wright   PetscFunctionBeginUser;
58*02b29df7SJames Wright   const char *A_initial = StateVariables_Initial[state_var_A];
59*02b29df7SJames Wright   const char *B_initial = StateVariables_Initial[state_var_B];
60*02b29df7SJames Wright 
61*02b29df7SJames Wright   State state_A0 = StateFromQ(gas, A0, state_var_A);
62*02b29df7SJames Wright   StateToQ(gas, state_A0, B0, state_var_B);
63*02b29df7SJames Wright   State state_B0 = StateFromQ(gas, B0, state_var_B);
64*02b29df7SJames Wright   StateToQ(gas, state_B0, A0_test, state_var_A);
65*02b29df7SJames Wright 
66*02b29df7SJames Wright   snprintf(buf, sizeof buf, "%s->%s->%s: %s", A_initial, B_initial, A_initial, A_initial);
67*02b29df7SJames Wright   PetscCall(CheckQWithTolerance(A0, A0_test, A0, buf, rtol_0, rtol_u, rtol_4));
68*02b29df7SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
69*02b29df7SJames Wright }
70*02b29df7SJames Wright 
71*02b29df7SJames Wright // @brief Verify `StateFromQ_fwd` via a finite difference approximation
72*02b29df7SJames Wright static PetscErrorCode TestState_fwd(StateVariable state_var_A, StateVariable state_var_B, NewtonianIdealGasContext gas, const CeedScalar A0[5],
73*02b29df7SJames Wright                                     CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) {
74*02b29df7SJames Wright   CeedScalar        eps = 4e-7;  // Finite difference step
75*02b29df7SJames Wright   char              buf[128];
76*02b29df7SJames Wright   const char *const StateVariables_Initial[] = {"U", "Y", "V"};
77*02b29df7SJames Wright 
78*02b29df7SJames Wright   PetscFunctionBeginUser;
79*02b29df7SJames Wright   const char *A_initial = StateVariables_Initial[state_var_A];
80*02b29df7SJames Wright   const char *B_initial = StateVariables_Initial[state_var_B];
81*02b29df7SJames Wright   State       state_0   = StateFromQ(gas, A0, state_var_A);
82*02b29df7SJames Wright 
83*02b29df7SJames Wright   for (int i = 0; i < 5; i++) {
84*02b29df7SJames Wright     CeedScalar dB[5] = {0.}, dB_fd[5] = {0.};
85*02b29df7SJames Wright     {  // Calculate dB using State functions
86*02b29df7SJames Wright       CeedScalar dA[5] = {0};
87*02b29df7SJames Wright 
88*02b29df7SJames Wright       dA[i]    = A0[i];
89*02b29df7SJames Wright       State ds = StateFromQ_fwd(gas, state_0, dA, state_var_A);
90*02b29df7SJames Wright       StateToQ(gas, ds, dB, state_var_B);
91*02b29df7SJames Wright     }
92*02b29df7SJames Wright 
93*02b29df7SJames Wright     {  // Calculate dB_fd via finite difference approximation
94*02b29df7SJames Wright       CeedScalar A1[5], B0[5], B1[5];
95*02b29df7SJames Wright 
96*02b29df7SJames Wright       for (int j = 0; j < 5; j++) A1[j] = (1 + eps * (i == j)) * A0[j];
97*02b29df7SJames Wright       State state_1 = StateFromQ(gas, A1, state_var_A);
98*02b29df7SJames Wright       StateToQ(gas, state_0, B0, state_var_B);
99*02b29df7SJames Wright       StateToQ(gas, state_1, B1, state_var_B);
100*02b29df7SJames Wright       for (int j = 0; j < 5; j++) dB_fd[j] = (B1[j] - B0[j]) / eps;
101*02b29df7SJames Wright     }
102*02b29df7SJames Wright 
103*02b29df7SJames Wright     snprintf(buf, sizeof buf, "d%s->d%s: StateFrom%s_fwd i=%d: d%s", A_initial, B_initial, A_initial, i, B_initial);
104*02b29df7SJames Wright     PetscCall(CheckQWithTolerance(dB_fd, dB, dB_fd, buf, rtol_0, rtol_u, rtol_4));
105*02b29df7SJames Wright   }
106*02b29df7SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
107*02b29df7SJames Wright }
108*02b29df7SJames Wright 
109*02b29df7SJames Wright // @brief Test the Newtonian State transformation functions, `StateFrom*`
1102b730f8bSJeremy L Thompson static PetscErrorCode UnitTests_Newtonian(User user, NewtonianIdealGasContext gas) {
111e334ad8fSJed Brown   Units            units = user->units;
112*02b29df7SJames Wright   const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second, K = units->Kelvin;
113*02b29df7SJames Wright 
114e334ad8fSJed Brown   PetscFunctionBeginUser;
115*02b29df7SJames Wright   const CeedScalar T          = 200 * K;
116*02b29df7SJames Wright   const CeedScalar rho        = 1.2 * kg / Cube(m);
117*02b29df7SJames Wright   const CeedScalar P          = (HeatCapacityRatio(gas) - 1) * rho * gas->cv * T;
118*02b29df7SJames Wright   const CeedScalar u_base     = 40 * m / sec;
119*02b29df7SJames Wright   const CeedScalar u[3]       = {u_base, u_base * 1.1, u_base * 1.2};
120*02b29df7SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(u, u);
121*02b29df7SJames Wright   const CeedScalar e_internal = gas->cv * T;
122*02b29df7SJames Wright   const CeedScalar e_total    = e_kinetic + e_internal;
123*02b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
124*02b29df7SJames Wright   const CeedScalar entropy    = log(P) - gamma * log(rho);
125*02b29df7SJames Wright   const CeedScalar rho_div_p  = rho / P;
126*02b29df7SJames Wright   const CeedScalar Y0[5]      = {P, u[0], u[1], u[2], T};
127*02b29df7SJames Wright   const CeedScalar U0[5]      = {rho, rho * u[0], rho * u[1], rho * u[2], rho * e_total};
128*02b29df7SJames Wright   const CeedScalar V0[5]      = {(gamma - entropy) / (gamma - 1) - rho_div_p * (e_kinetic), rho_div_p * u[0], rho_div_p * u[1], rho_div_p * u[2],
129*02b29df7SJames Wright                                  -rho_div_p};
130*02b29df7SJames Wright 
131*02b29df7SJames Wright   {
132*02b29df7SJames Wright     CeedScalar rtol = 20 * CEED_EPSILON;
133*02b29df7SJames Wright 
134*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol));
135*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol));
136*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol));
137*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, rtol, rtol, rtol));
138*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, rtol, rtol, rtol));
139*02b29df7SJames Wright     PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, rtol, rtol, rtol));
140*02b29df7SJames Wright   }
141*02b29df7SJames Wright 
142*02b29df7SJames Wright   {
143*02b29df7SJames Wright     CeedScalar rtol = 5e-6;
144*02b29df7SJames Wright 
145*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol));
146*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol));
147*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol));
148*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, 10 * rtol, rtol, rtol));
149*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, 5 * rtol, rtol, rtol));
150*02b29df7SJames Wright     PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, 5 * rtol, 5 * rtol, 5 * rtol));
151e334ad8fSJed Brown   }
152ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
153e334ad8fSJed Brown }
154e334ad8fSJed Brown 
1550fcbc436SJames Wright // @brief Create CeedOperator for stabilized mass KSP for explicit timestepping
1560fcbc436SJames Wright //
1570fcbc436SJames Wright // Only used for SUPG stabilization
1580fcbc436SJames Wright PetscErrorCode CreateKSPMassOperator_NewtonianStabilized(User user, CeedOperator *op_mass) {
1590fcbc436SJames Wright   Ceed                 ceed = user->ceed;
1600fcbc436SJames Wright   CeedInt              num_comp_q, q_data_size;
1610fcbc436SJames Wright   CeedQFunction        qf_mass;
1620fcbc436SJames Wright   CeedElemRestriction  elem_restr_q, elem_restr_qd_i;
1630fcbc436SJames Wright   CeedBasis            basis_q;
1640fcbc436SJames Wright   CeedVector           q_data;
1650fcbc436SJames Wright   CeedQFunctionContext qf_ctx = NULL;
1660fcbc436SJames Wright   PetscInt             dim    = 3;
1670fcbc436SJames Wright 
1680fcbc436SJames Wright   PetscFunctionBeginUser;
1690fcbc436SJames Wright   {  // Get restriction and basis from the RHS function
1700fcbc436SJames Wright     CeedOperator     *sub_ops;
1710fcbc436SJames Wright     CeedOperatorField field;
1720fcbc436SJames Wright     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
1730fcbc436SJames Wright 
1740fcbc436SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops));
1750fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field));
1760fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q));
1770fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetBasis(field, &basis_q));
1780fcbc436SJames Wright 
1790fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &field));
1800fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_qd_i));
1810fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetVector(field, &q_data));
1820fcbc436SJames Wright 
1830fcbc436SJames Wright     PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &qf_ctx));
1840fcbc436SJames Wright   }
1850fcbc436SJames Wright 
1860fcbc436SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q));
1870fcbc436SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd_i, &q_data_size));
1880fcbc436SJames Wright 
1890fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MassFunction_Newtonian_Conserv, MassFunction_Newtonian_Conserv_loc, &qf_mass));
1900fcbc436SJames Wright 
1910fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetContext(qf_mass, qf_ctx));
1920fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_mass, 0));
1930fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q_dot", 5, CEED_EVAL_INTERP));
1940fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q", 5, CEED_EVAL_INTERP));
1950fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE));
1960fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "v", 5, CEED_EVAL_INTERP));
1970fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "Grad_v", 5 * dim, CEED_EVAL_GRAD));
1980fcbc436SJames Wright 
1990fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass));
2000fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q_dot", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
2010fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q", elem_restr_q, basis_q, user->q_ceed));
2020fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd_i, CEED_BASIS_NONE, q_data));
2030fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
2040fcbc436SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "Grad_v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
2050fcbc436SJames Wright 
2060fcbc436SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
2070fcbc436SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2080fcbc436SJames Wright }
209013a5551SJames Wright 
210731c13d7SJames Wright PetscErrorCode NS_NEWTONIAN_IG(ProblemData problem, DM dm, void *ctx, SimpleBC bc) {
211a0add3c9SJed Brown   SetupContext             setup_context;
21288b783a1SJames Wright   User                     user   = *(User *)ctx;
213ebd2ea64SLeila Ghaffari   CeedInt                  degree = user->app_ctx->degree;
21488b783a1SJames Wright   StabilizationType        stab;
21597baf651SJames Wright   StateVariable            state_var;
216a424bcd0SJames Wright   MPI_Comm                 comm = user->comm;
217a424bcd0SJames Wright   Ceed                     ceed = user->ceed;
21888b783a1SJames Wright   PetscBool                implicit;
219a2f6637eSJames Wright   PetscBool                unit_tests;
220841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ig_ctx;
221841e4c73SJed Brown   CeedQFunctionContext     newtonian_ig_context;
22288b783a1SJames Wright 
223841e4c73SJed Brown   PetscFunctionBeginUser;
2242b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
2252b730f8bSJeremy L Thompson   PetscCall(PetscCalloc1(1, &newtonian_ig_ctx));
22688b783a1SJames Wright 
22788b783a1SJames Wright   // ------------------------------------------------------
22888b783a1SJames Wright   //           Setup Generic Newtonian IG Problem
22988b783a1SJames Wright   // ------------------------------------------------------
23088b783a1SJames Wright   problem->dim               = 3;
2310ec2498eSJames Wright   problem->jac_data_size_sur = 11;
23288b783a1SJames Wright   problem->non_zero_time     = PETSC_FALSE;
233dc805cc4SLeila Ghaffari   problem->print_info        = PRINT_NEWTONIAN;
234752a08a7SJames Wright   problem->uses_newtonian    = PETSC_TRUE;
23588b783a1SJames Wright 
23688b783a1SJames Wright   // ------------------------------------------------------
23788b783a1SJames Wright   //             Create the libCEED context
23888b783a1SJames Wright   // ------------------------------------------------------
23988b783a1SJames Wright   CeedScalar cv         = 717.;          // J/(kg K)
24088b783a1SJames Wright   CeedScalar cp         = 1004.;         // J/(kg K)
241a2726bdbSJames Wright   CeedScalar g[3]       = {0, 0, 0};     // m/s^2
24288b783a1SJames Wright   CeedScalar lambda     = -2. / 3.;      // -
24388626eedSJames Wright   CeedScalar mu         = 1.8e-5;        // Pa s, dynamic viscosity
24488b783a1SJames Wright   CeedScalar k          = 0.02638;       // W/(m K)
245ebd2ea64SLeila Ghaffari   CeedScalar c_tau      = 0.5 / degree;  // -
24688626eedSJames Wright   CeedScalar Ctau_t     = 1.0;           // -
24794c01735SLeila Ghaffari   CeedScalar Cv_func[3] = {36, 60, 128};
248c5dde687SLeila Ghaffari   CeedScalar Ctau_v     = Cv_func[(CeedInt)Min(3, degree) - 1];
24975538696SLeila Ghaffari   CeedScalar Ctau_C     = 0.25 / degree;
25075538696SLeila Ghaffari   CeedScalar Ctau_M     = 0.25 / degree;
25175538696SLeila Ghaffari   CeedScalar Ctau_E     = 0.125;
25288b783a1SJames Wright   PetscReal  domain_min[3], domain_max[3], domain_size[3];
2532b730f8bSJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
254ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
25588b783a1SJames Wright 
256d310b3d3SAdeleke O. Bankole   StatePrimitive reference      = {.pressure = 1.01e5, .velocity = {0}, .temperature = 288.15};
2572249ac91SJames Wright   CeedScalar     idl_decay_time = -1, idl_start = 0, idl_length = 0, idl_pressure = reference.pressure;
258530ad8c4SKenneth E. Jansen   PetscBool      idl_enable = PETSC_FALSE;
259d310b3d3SAdeleke O. Bankole 
26088b783a1SJames Wright   // ------------------------------------------------------
26188b783a1SJames Wright   //             Create the PETSc context
26288b783a1SJames Wright   // ------------------------------------------------------
26388626eedSJames Wright   PetscScalar meter    = 1;  // 1 meter in scaled length units
26488626eedSJames Wright   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
26588626eedSJames Wright   PetscScalar second   = 1;  // 1 second in scaled time units
26688b783a1SJames Wright   PetscScalar Kelvin   = 1;  // 1 Kelvin in scaled temperature units
26788b783a1SJames Wright   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
26888b783a1SJames Wright 
26988b783a1SJames Wright   // ------------------------------------------------------
27088b783a1SJames Wright   //              Command line Options
27188b783a1SJames Wright   // ------------------------------------------------------
272a2726bdbSJames Wright   PetscBool given_option = PETSC_FALSE;
2732b730f8bSJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", NULL);
274dc805cc4SLeila Ghaffari   // -- Conservative vs Primitive variables
2752b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-state_var", "State variables used", NULL, StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE),
2762b730f8bSJeremy L Thompson                              (PetscEnum *)&state_var, NULL));
27797baf651SJames Wright 
27897baf651SJames Wright   switch (state_var) {
27997baf651SJames Wright     case STATEVAR_CONSERVATIVE:
280d310b3d3SAdeleke O. Bankole       problem->ics.qfunction                       = ICsNewtonianIG_Conserv;
281d310b3d3SAdeleke O. Bankole       problem->ics.qfunction_loc                   = ICsNewtonianIG_Conserv_loc;
282dc805cc4SLeila Ghaffari       problem->apply_vol_rhs.qfunction             = RHSFunction_Newtonian;
283dc805cc4SLeila Ghaffari       problem->apply_vol_rhs.qfunction_loc         = RHSFunction_Newtonian_loc;
2843d02368aSJames Wright       problem->apply_vol_ifunction.qfunction       = IFunction_Newtonian_Conserv;
2853d02368aSJames Wright       problem->apply_vol_ifunction.qfunction_loc   = IFunction_Newtonian_Conserv_loc;
2863d02368aSJames Wright       problem->apply_vol_ijacobian.qfunction       = IJacobian_Newtonian_Conserv;
2873d02368aSJames Wright       problem->apply_vol_ijacobian.qfunction_loc   = IJacobian_Newtonian_Conserv_loc;
28820840d50SJames Wright       problem->apply_inflow.qfunction              = BoundaryIntegral_Conserv;
28920840d50SJames Wright       problem->apply_inflow.qfunction_loc          = BoundaryIntegral_Conserv_loc;
29020840d50SJames Wright       problem->apply_inflow_jacobian.qfunction     = BoundaryIntegral_Jacobian_Conserv;
29120840d50SJames Wright       problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Conserv_loc;
29297baf651SJames Wright       break;
29397baf651SJames Wright 
29497baf651SJames Wright     case STATEVAR_PRIMITIVE:
29597baf651SJames Wright       problem->ics.qfunction                       = ICsNewtonianIG_Prim;
29697baf651SJames Wright       problem->ics.qfunction_loc                   = ICsNewtonianIG_Prim_loc;
29797baf651SJames Wright       problem->apply_vol_ifunction.qfunction       = IFunction_Newtonian_Prim;
29897baf651SJames Wright       problem->apply_vol_ifunction.qfunction_loc   = IFunction_Newtonian_Prim_loc;
29997baf651SJames Wright       problem->apply_vol_ijacobian.qfunction       = IJacobian_Newtonian_Prim;
30097baf651SJames Wright       problem->apply_vol_ijacobian.qfunction_loc   = IJacobian_Newtonian_Prim_loc;
30197baf651SJames Wright       problem->apply_inflow.qfunction              = BoundaryIntegral_Prim;
30297baf651SJames Wright       problem->apply_inflow.qfunction_loc          = BoundaryIntegral_Prim_loc;
30397baf651SJames Wright       problem->apply_inflow_jacobian.qfunction     = BoundaryIntegral_Jacobian_Prim;
30497baf651SJames Wright       problem->apply_inflow_jacobian.qfunction_loc = BoundaryIntegral_Jacobian_Prim_loc;
30597baf651SJames Wright       break;
306*02b29df7SJames Wright     case STATEVAR_ENTROPY:
307*02b29df7SJames Wright       break;
308dc805cc4SLeila Ghaffari   }
30967490bc6SJeremy L Thompson 
31088b783a1SJames Wright   // -- Physics
3112b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-cv", "Heat capacity at constant volume", NULL, cv, &cv, NULL));
3122b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-cp", "Heat capacity at constant pressure", NULL, cp, &cp, NULL));
3132b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-lambda", "Stokes hypothesis second viscosity coefficient", NULL, lambda, &lambda, NULL));
3142b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", NULL, mu, &mu, NULL));
3152b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-k", "Thermal conductivity", NULL, k, &k, NULL));
31688b783a1SJames Wright 
31788626eedSJames Wright   PetscInt dim = problem->dim;
318a2726bdbSJames Wright   PetscCall(PetscOptionsDeprecated("-g", "-gravity", "libCEED 0.11.1", NULL));
319a2726bdbSJames Wright   PetscCall(PetscOptionsRealArray("-gravity", "Gravitational acceleration vector", NULL, g, &dim, &given_option));
320a2726bdbSJames Wright   if (given_option) PetscCheck(dim == 3, comm, PETSC_ERR_ARG_SIZ, "Gravity vector must be size 3, %" PetscInt_FMT " values given", dim);
321a2726bdbSJames Wright 
3222b730f8bSJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
3232b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
3242b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL));
3252b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", NULL, Ctau_v, &Ctau_v, NULL));
3262b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", NULL, Ctau_C, &Ctau_C, NULL));
3272b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", NULL, Ctau_M, &Ctau_M, NULL));
3282b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", NULL, Ctau_E, &Ctau_E, NULL));
3292b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
3302b730f8bSJeremy L Thompson   PetscCall(PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", NULL, unit_tests = PETSC_FALSE, &unit_tests, NULL));
33188b783a1SJames Wright 
332e30587cbSJames Wright   dim = 3;
333d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_pressure", "Reference/initial pressure", NULL, reference.pressure, &reference.pressure, NULL));
334d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalarArray("-reference_velocity", "Reference/initial velocity", NULL, reference.velocity, &dim, NULL));
335d310b3d3SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_temperature", "Reference/initial temperature", NULL, reference.temperature, &reference.temperature, NULL));
336d310b3d3SAdeleke O. Bankole 
33788b783a1SJames Wright   // -- Units
3382b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
33988b783a1SJames Wright   meter = fabs(meter);
3402b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
34188b783a1SJames Wright   kilogram = fabs(kilogram);
3422b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
34388b783a1SJames Wright   second = fabs(second);
3442b730f8bSJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_Kelvin", "1 Kelvin in scaled temperature units", NULL, Kelvin, &Kelvin, NULL));
34588b783a1SJames Wright   Kelvin = fabs(Kelvin);
34688b783a1SJames Wright 
34788b783a1SJames Wright   // -- Warnings
3480e654f56SJames Wright   PetscCheck(!(state_var == STATEVAR_PRIMITIVE && !implicit), comm, PETSC_ERR_SUP,
3490e654f56SJames Wright              "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n");
350530ad8c4SKenneth E. Jansen 
351530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_decay_time", "Characteristic timescale of the pressure deviance decay. The timestep is good starting point",
352530ad8c4SKenneth E. Jansen                                NULL, idl_decay_time, &idl_decay_time, &idl_enable));
353fe1e732eSJames Wright   PetscCheck(!(idl_enable && idl_decay_time == 0), comm, PETSC_ERR_SUP, "idl_decay_time may not be equal to zero.");
354fe1e732eSJames Wright   if (idl_decay_time < 0) idl_enable = PETSC_FALSE;
355530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_start", "Start of IDL in the x direction", NULL, idl_start, &idl_start, NULL));
356530ad8c4SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_length", "Length of IDL in the positive x direction", NULL, idl_length, &idl_length, NULL));
3572249ac91SJames Wright   idl_pressure = reference.pressure;
3582249ac91SJames Wright   PetscCall(PetscOptionsScalar("-idl_pressure", "Pressure IDL uses as reference (default is `-reference_pressure`)", NULL, idl_pressure,
3592249ac91SJames Wright                                &idl_pressure, NULL));
36067490bc6SJeremy L Thompson   PetscOptionsEnd();
36188b783a1SJames Wright 
3620fcbc436SJames Wright   if (stab == STAB_SUPG && !implicit) problem->create_mass_operator = CreateKSPMassOperator_NewtonianStabilized;
3630fcbc436SJames Wright 
36488b783a1SJames Wright   // ------------------------------------------------------
36588b783a1SJames Wright   //           Set up the PETSc context
36688b783a1SJames Wright   // ------------------------------------------------------
36788b783a1SJames Wright   // -- Define derived units
36888b783a1SJames Wright   Pascal          = kilogram / (meter * PetscSqr(second));
36988b783a1SJames Wright   J_per_kg_K      = PetscSqr(meter) / (PetscSqr(second) * Kelvin);
37088b783a1SJames Wright   m_per_squared_s = meter / PetscSqr(second);
37188b783a1SJames Wright   W_per_m_K       = kilogram * meter / (pow(second, 3) * Kelvin);
37288b783a1SJames Wright 
37388b783a1SJames Wright   user->units->meter           = meter;
37488b783a1SJames Wright   user->units->kilogram        = kilogram;
37588b783a1SJames Wright   user->units->second          = second;
37688b783a1SJames Wright   user->units->Kelvin          = Kelvin;
37788b783a1SJames Wright   user->units->Pascal          = Pascal;
37888b783a1SJames Wright   user->units->J_per_kg_K      = J_per_kg_K;
37988b783a1SJames Wright   user->units->m_per_squared_s = m_per_squared_s;
38088b783a1SJames Wright   user->units->W_per_m_K       = W_per_m_K;
38188b783a1SJames Wright 
38288b783a1SJames Wright   // ------------------------------------------------------
38388b783a1SJames Wright   //           Set up the libCEED context
38488b783a1SJames Wright   // ------------------------------------------------------
38588b783a1SJames Wright   // -- Scale variables to desired units
38688b783a1SJames Wright   cv *= J_per_kg_K;
38788b783a1SJames Wright   cp *= J_per_kg_K;
38888b783a1SJames Wright   mu *= Pascal * second;
38988b783a1SJames Wright   k *= W_per_m_K;
390ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] *= meter;
391ba6664aeSJames Wright   for (PetscInt i = 0; i < 3; i++) g[i] *= m_per_squared_s;
392d310b3d3SAdeleke O. Bankole   reference.pressure *= Pascal;
393d310b3d3SAdeleke O. Bankole   for (PetscInt i = 0; i < 3; i++) reference.velocity[i] *= meter / second;
394d310b3d3SAdeleke O. Bankole   reference.temperature *= Kelvin;
39588b783a1SJames Wright   problem->dm_scale = meter;
39688b783a1SJames Wright 
39788b783a1SJames Wright   // -- Solver Settings
39888b783a1SJames Wright   user->phys->implicit  = implicit;
39997baf651SJames Wright   user->phys->state_var = state_var;
40088b783a1SJames Wright 
40188b783a1SJames Wright   // -- QFunction Context
402841e4c73SJed Brown   newtonian_ig_ctx->lambda        = lambda;
403841e4c73SJed Brown   newtonian_ig_ctx->mu            = mu;
404841e4c73SJed Brown   newtonian_ig_ctx->k             = k;
405841e4c73SJed Brown   newtonian_ig_ctx->cv            = cv;
406841e4c73SJed Brown   newtonian_ig_ctx->cp            = cp;
407841e4c73SJed Brown   newtonian_ig_ctx->c_tau         = c_tau;
408841e4c73SJed Brown   newtonian_ig_ctx->Ctau_t        = Ctau_t;
409841e4c73SJed Brown   newtonian_ig_ctx->Ctau_v        = Ctau_v;
410841e4c73SJed Brown   newtonian_ig_ctx->Ctau_C        = Ctau_C;
411841e4c73SJed Brown   newtonian_ig_ctx->Ctau_M        = Ctau_M;
412841e4c73SJed Brown   newtonian_ig_ctx->Ctau_E        = Ctau_E;
413841e4c73SJed Brown   newtonian_ig_ctx->stabilization = stab;
41465dd5cafSJames Wright   newtonian_ig_ctx->is_implicit   = implicit;
41597baf651SJames Wright   newtonian_ig_ctx->state_var     = state_var;
416530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_enable    = idl_enable;
417530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_amplitude = 1 / (idl_decay_time * second);
418530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_start     = idl_start * meter;
419530ad8c4SKenneth E. Jansen   newtonian_ig_ctx->idl_length    = idl_length * meter;
4202249ac91SJames Wright   newtonian_ig_ctx->idl_pressure  = idl_pressure;
4212b730f8bSJeremy L Thompson   PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3));
42288b783a1SJames Wright 
423d310b3d3SAdeleke O. Bankole   // -- Setup Context
424d310b3d3SAdeleke O. Bankole   setup_context->reference = reference;
425d310b3d3SAdeleke O. Bankole   setup_context->gas       = *newtonian_ig_ctx;
426d310b3d3SAdeleke O. Bankole   setup_context->lx        = domain_size[0];
427d310b3d3SAdeleke O. Bankole   setup_context->ly        = domain_size[1];
428d310b3d3SAdeleke O. Bankole   setup_context->lz        = domain_size[2];
429d310b3d3SAdeleke O. Bankole   setup_context->time      = 0;
430d310b3d3SAdeleke O. Bankole 
431a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context));
432a424bcd0SJames Wright   PetscCallCeed(ceed,
433a424bcd0SJames Wright                 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
434a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc));
435a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(problem->ics.qfunction_context, "evaluation time", offsetof(struct SetupContext_, time), 1,
436a424bcd0SJames Wright                                                          "Time of evaluation"));
437841e4c73SJed Brown 
438a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &newtonian_ig_context));
439a424bcd0SJames Wright   PetscCallCeed(ceed,
440a424bcd0SJames Wright                 CeedQFunctionContextSetData(newtonian_ig_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newtonian_ig_ctx), newtonian_ig_ctx));
441a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(newtonian_ig_context, CEED_MEM_HOST, FreeContextPetsc));
442a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "timestep size", offsetof(struct NewtonianIdealGasContext_, dt), 1,
443a424bcd0SJames Wright                                                          "Size of timestep, delta t"));
444a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "ijacobian time shift",
445a424bcd0SJames Wright                                                          offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1,
446a424bcd0SJames Wright                                                          "Shift for mass matrix in IJacobian"));
447a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_context, "solution time", offsetof(struct NewtonianIdealGasContext_, time), 1,
448a424bcd0SJames Wright                                                          "Current solution time"));
449f179a332SJames Wright 
450841e4c73SJed Brown   problem->apply_vol_rhs.qfunction_context = newtonian_ig_context;
451a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ifunction.qfunction_context));
452a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_vol_ijacobian.qfunction_context));
453a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow.qfunction_context));
454a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_context, &problem->apply_inflow_jacobian.qfunction_context));
455e334ad8fSJed Brown 
4569f844368SJames Wright   if (bc->num_freestream > 0) PetscCall(FreestreamBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
4579f844368SJames Wright   if (bc->num_outflow > 0) PetscCall(OutflowBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
4589f844368SJames Wright   if (bc->num_slip > 0) PetscCall(SlipBCSetup(problem, dm, ctx, newtonian_ig_context));
4599f844368SJames Wright 
460e334ad8fSJed Brown   if (unit_tests) {
461e334ad8fSJed Brown     PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx));
462e334ad8fSJed Brown   }
463ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
46488b783a1SJames Wright }
46588b783a1SJames Wright 
466731c13d7SJames Wright PetscErrorCode PRINT_NEWTONIAN(User user, ProblemData problem, AppCtx app_ctx) {
467367c849eSJames Wright   MPI_Comm                 comm = user->comm;
468a424bcd0SJames Wright   Ceed                     ceed = user->ceed;
469841e4c73SJed Brown   NewtonianIdealGasContext newtonian_ctx;
470841e4c73SJed Brown 
47188b783a1SJames Wright   PetscFunctionBeginUser;
472a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, CEED_MEM_HOST, &newtonian_ctx));
4732b730f8bSJeremy L Thompson   PetscCall(PetscPrintf(comm,
474841e4c73SJed Brown                         "  Problem:\n"
475841e4c73SJed Brown                         "    Problem Name                       : %s\n"
476841e4c73SJed Brown                         "    Stabilization                      : %s\n",
4772b730f8bSJeremy L Thompson                         app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]));
478a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, &newtonian_ctx));
479ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
48088b783a1SJames Wright }
481