xref: /honee/problems/newtonian.c (revision 36038bbcf8b5827c2ae723a5a7c52ea5c4047506)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
33a8779fbSJames Wright 
43a8779fbSJames Wright /// @file
53a8779fbSJames Wright /// Utility functions for setting up problems using the Newtonian Qfunction
63a8779fbSJames Wright 
73a8779fbSJames Wright #include "../qfunctions/newtonian.h"
83a8779fbSJames Wright 
9e419654dSJeremy L Thompson #include <ceed.h>
10e419654dSJeremy L Thompson #include <petscdm.h>
11e419654dSJeremy L Thompson 
12149fb536SJames Wright #include <navierstokes.h>
136dd99bedSLeila Ghaffari 
14f7534ca8SJed Brown // For use with PetscOptionsEnum
156dfcbb05SJames Wright static const char *const StateVariables[] = {"CONSERVATIVE", "PRIMITIVE", "ENTROPY", "StateVariable", "STATEVAR_", NULL};
16f7534ca8SJed Brown 
17c62f7daeSJames Wright static PetscErrorCode CheckQWithTolerance(const CeedScalar Q_s[5], const CeedScalar Q_a[5], const CeedScalar Q_b[5], const char *name,
18c62f7daeSJames Wright                                           PetscReal rtol_0, PetscReal rtol_u, PetscReal rtol_4) {
19c62f7daeSJames Wright   CeedScalar relative_error[5];  // relative error
20c62f7daeSJames Wright   CeedScalar divisor_threshold = 10 * CEED_EPSILON;
2106f41313SJames Wright 
2206f41313SJames Wright   PetscFunctionBeginUser;
23c62f7daeSJames Wright   relative_error[0] = (Q_a[0] - Q_b[0]) / (fabs(Q_s[0]) > divisor_threshold ? Q_s[0] : 1);
24c62f7daeSJames Wright   relative_error[4] = (Q_a[4] - Q_b[4]) / (fabs(Q_s[4]) > divisor_threshold ? Q_s[4] : 1);
25c62f7daeSJames Wright 
26c62f7daeSJames Wright   CeedScalar u_magnitude = sqrt(Square(Q_s[1]) + Square(Q_s[2]) + Square(Q_s[3]));
27c62f7daeSJames Wright   CeedScalar u_divisor   = u_magnitude > divisor_threshold ? u_magnitude : 1;
28c62f7daeSJames Wright   for (int i = 1; i < 4; i++) {
29c62f7daeSJames Wright     relative_error[i] = (Q_a[i] - Q_b[i]) / u_divisor;
302b916ea7SJeremy L Thompson   }
31c62f7daeSJames Wright 
32c62f7daeSJames Wright   if (fabs(relative_error[0]) >= rtol_0) {
33c62f7daeSJames Wright     printf("%s[0] error %g (expected %.10e, got %.10e)\n", name, relative_error[0], Q_s[0], Q_a[0]);
34c62f7daeSJames Wright   }
35c62f7daeSJames Wright   for (int i = 1; i < 4; i++) {
36c62f7daeSJames Wright     if (fabs(relative_error[i]) >= rtol_u) {
37c62f7daeSJames Wright       printf("%s[%d] error %g (expected %.10e, got %.10e)\n", name, i, relative_error[i], Q_s[i], Q_a[i]);
38c62f7daeSJames Wright     }
39c62f7daeSJames Wright   }
40c62f7daeSJames Wright   if (fabs(relative_error[4]) >= rtol_4) {
41c62f7daeSJames Wright     printf("%s[4] error %g (expected %.10e, got %.10e)\n", name, relative_error[4], Q_s[4], Q_a[4]);
42c62f7daeSJames Wright   }
43d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
44f0b65372SJed Brown }
45f0b65372SJed Brown 
46c62f7daeSJames Wright // @brief Verify `StateFromQ` by converting A0 -> B0 -> A0_test, where A0 should equal A0_test
47c62f7daeSJames Wright static PetscErrorCode TestState(StateVariable state_var_A, StateVariable state_var_B, NewtonianIdealGasContext gas, const CeedScalar A0[5],
48c62f7daeSJames Wright                                 CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) {
49c62f7daeSJames Wright   CeedScalar        B0[5], A0_test[5];
50c62f7daeSJames Wright   char              buf[128];
51c62f7daeSJames Wright   const char *const StateVariables_Initial[] = {"U", "Y", "V"};
52c62f7daeSJames Wright 
53c62f7daeSJames Wright   PetscFunctionBeginUser;
54c62f7daeSJames Wright   const char *A_initial = StateVariables_Initial[state_var_A];
55c62f7daeSJames Wright   const char *B_initial = StateVariables_Initial[state_var_B];
56c62f7daeSJames Wright 
57c62f7daeSJames Wright   State state_A0 = StateFromQ(gas, A0, state_var_A);
58c62f7daeSJames Wright   StateToQ(gas, state_A0, B0, state_var_B);
59c62f7daeSJames Wright   State state_B0 = StateFromQ(gas, B0, state_var_B);
60c62f7daeSJames Wright   StateToQ(gas, state_B0, A0_test, state_var_A);
61c62f7daeSJames Wright 
62c62f7daeSJames Wright   snprintf(buf, sizeof buf, "%s->%s->%s: %s", A_initial, B_initial, A_initial, A_initial);
63c62f7daeSJames Wright   PetscCall(CheckQWithTolerance(A0, A0_test, A0, buf, rtol_0, rtol_u, rtol_4));
64c62f7daeSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
65c62f7daeSJames Wright }
66c62f7daeSJames Wright 
67c62f7daeSJames Wright // @brief Verify `StateFromQ_fwd` via a finite difference approximation
68c62f7daeSJames Wright static PetscErrorCode TestState_fwd(StateVariable state_var_A, StateVariable state_var_B, NewtonianIdealGasContext gas, const CeedScalar A0[5],
69c62f7daeSJames Wright                                     CeedScalar rtol_0, CeedScalar rtol_u, CeedScalar rtol_4) {
70c62f7daeSJames Wright   CeedScalar        eps = 4e-7;  // Finite difference step
71c62f7daeSJames Wright   char              buf[128];
72c62f7daeSJames Wright   const char *const StateVariables_Initial[] = {"U", "Y", "V"};
73c62f7daeSJames Wright 
74c62f7daeSJames Wright   PetscFunctionBeginUser;
75c62f7daeSJames Wright   const char *A_initial = StateVariables_Initial[state_var_A];
76c62f7daeSJames Wright   const char *B_initial = StateVariables_Initial[state_var_B];
77c62f7daeSJames Wright   State       state_0   = StateFromQ(gas, A0, state_var_A);
78c62f7daeSJames Wright 
79c62f7daeSJames Wright   for (int i = 0; i < 5; i++) {
80c62f7daeSJames Wright     CeedScalar dB[5] = {0.}, dB_fd[5] = {0.};
81c62f7daeSJames Wright     {  // Calculate dB using State functions
82c62f7daeSJames Wright       CeedScalar dA[5] = {0};
83c62f7daeSJames Wright 
84c62f7daeSJames Wright       dA[i]          = A0[i];
85a6654c3eSJames Wright       State dstate_0 = StateFromQ_fwd(gas, state_0, dA, state_var_A);
86a6654c3eSJames Wright       StateToQ_fwd(gas, state_0, dstate_0, dB, state_var_B);
87c62f7daeSJames Wright     }
88c62f7daeSJames Wright 
89c62f7daeSJames Wright     {  // Calculate dB_fd via finite difference approximation
90c62f7daeSJames Wright       CeedScalar A1[5], B0[5], B1[5];
91c62f7daeSJames Wright 
92c62f7daeSJames Wright       for (int j = 0; j < 5; j++) A1[j] = (1 + eps * (i == j)) * A0[j];
93c62f7daeSJames Wright       State state_1 = StateFromQ(gas, A1, state_var_A);
94c62f7daeSJames Wright       StateToQ(gas, state_0, B0, state_var_B);
95c62f7daeSJames Wright       StateToQ(gas, state_1, B1, state_var_B);
96c62f7daeSJames Wright       for (int j = 0; j < 5; j++) dB_fd[j] = (B1[j] - B0[j]) / eps;
97c62f7daeSJames Wright     }
98c62f7daeSJames Wright 
99c62f7daeSJames 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);
100c62f7daeSJames Wright     PetscCall(CheckQWithTolerance(dB_fd, dB, dB_fd, buf, rtol_0, rtol_u, rtol_4));
101c62f7daeSJames Wright   }
102c62f7daeSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
103c62f7daeSJames Wright }
104c62f7daeSJames Wright 
105c62f7daeSJames Wright // @brief Test the Newtonian State transformation functions, `StateFrom*`
1062b916ea7SJeremy L Thompson static PetscErrorCode UnitTests_Newtonian(User user, NewtonianIdealGasContext gas) {
107f0b65372SJed Brown   Units            units = user->units;
108c62f7daeSJames Wright   const CeedScalar kg = units->kilogram, m = units->meter, sec = units->second, K = units->Kelvin;
109c62f7daeSJames Wright 
110f0b65372SJed Brown   PetscFunctionBeginUser;
111c62f7daeSJames Wright   const CeedScalar T          = 200 * K;
112c62f7daeSJames Wright   const CeedScalar rho        = 1.2 * kg / Cube(m);
113c62f7daeSJames Wright   const CeedScalar P          = (HeatCapacityRatio(gas) - 1) * rho * gas->cv * T;
114c62f7daeSJames Wright   const CeedScalar u_base     = 40 * m / sec;
115c62f7daeSJames Wright   const CeedScalar u[3]       = {u_base, u_base * 1.1, u_base * 1.2};
116c62f7daeSJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(u, u);
117c62f7daeSJames Wright   const CeedScalar e_internal = gas->cv * T;
118c62f7daeSJames Wright   const CeedScalar e_total    = e_kinetic + e_internal;
119c62f7daeSJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
120c62f7daeSJames Wright   const CeedScalar entropy    = log(P) - gamma * log(rho);
121c62f7daeSJames Wright   const CeedScalar rho_div_p  = rho / P;
122c62f7daeSJames Wright   const CeedScalar Y0[5]      = {P, u[0], u[1], u[2], T};
123c62f7daeSJames Wright   const CeedScalar U0[5]      = {rho, rho * u[0], rho * u[1], rho * u[2], rho * e_total};
124c62f7daeSJames 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],
125c62f7daeSJames Wright                                  -rho_div_p};
126c62f7daeSJames Wright 
127c62f7daeSJames Wright   {
128c62f7daeSJames Wright     CeedScalar rtol = 20 * CEED_EPSILON;
129c62f7daeSJames Wright 
130c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol));
131c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol));
132c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol));
133c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, rtol, rtol, rtol));
134c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, rtol, rtol, rtol));
135c62f7daeSJames Wright     PetscCall(TestState(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, rtol, rtol, rtol));
136c62f7daeSJames Wright   }
137c62f7daeSJames Wright 
138c62f7daeSJames Wright   {
139c62f7daeSJames Wright     CeedScalar rtol = 5e-6;
140c62f7daeSJames Wright 
141c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_CONSERVATIVE, gas, Y0, rtol, rtol, rtol));
142c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_PRIMITIVE, STATEVAR_ENTROPY, gas, Y0, rtol, rtol, rtol));
143c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_PRIMITIVE, gas, U0, rtol, rtol, rtol));
144c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_CONSERVATIVE, STATEVAR_ENTROPY, gas, U0, 10 * rtol, rtol, rtol));
145c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_CONSERVATIVE, gas, V0, 5 * rtol, rtol, rtol));
146c62f7daeSJames Wright     PetscCall(TestState_fwd(STATEVAR_ENTROPY, STATEVAR_PRIMITIVE, gas, V0, 5 * rtol, 5 * rtol, 5 * rtol));
147f0b65372SJed Brown   }
148d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
149f0b65372SJed Brown }
150f0b65372SJed Brown 
15165dee3d2SJames Wright // @brief Create CeedOperator for stabilized mass KSP for explicit timestepping
15265dee3d2SJames Wright //
15365dee3d2SJames Wright // Only used for SUPG stabilization
15465dee3d2SJames Wright PetscErrorCode CreateKSPMassOperator_NewtonianStabilized(User user, CeedOperator *op_mass) {
15565dee3d2SJames Wright   Ceed                 ceed = user->ceed;
15665dee3d2SJames Wright   CeedInt              num_comp_q, q_data_size;
15765dee3d2SJames Wright   CeedQFunction        qf_mass;
15865dee3d2SJames Wright   CeedElemRestriction  elem_restr_q, elem_restr_qd_i;
15965dee3d2SJames Wright   CeedBasis            basis_q;
16065dee3d2SJames Wright   CeedVector           q_data;
16165dee3d2SJames Wright   CeedQFunctionContext qf_ctx = NULL;
16265dee3d2SJames Wright   PetscInt             dim    = 3;
16365dee3d2SJames Wright 
16465dee3d2SJames Wright   PetscFunctionBeginUser;
16565dee3d2SJames Wright   {  // Get restriction and basis from the RHS function
16665dee3d2SJames Wright     CeedOperator     *sub_ops;
16765dee3d2SJames Wright     CeedOperatorField field;
16865dee3d2SJames Wright     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
16965dee3d2SJames Wright 
17065dee3d2SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops));
17165dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field));
17265dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q));
17365dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetBasis(field, &basis_q));
17465dee3d2SJames Wright 
17565dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &field));
17665dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_qd_i));
17765dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetVector(field, &q_data));
17865dee3d2SJames Wright 
17965dee3d2SJames Wright     PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &qf_ctx));
18065dee3d2SJames Wright   }
18165dee3d2SJames Wright 
18265dee3d2SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q));
18365dee3d2SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd_i, &q_data_size));
18465dee3d2SJames Wright 
18565dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, MassFunction_Newtonian_Conserv, MassFunction_Newtonian_Conserv_loc, &qf_mass));
18665dee3d2SJames Wright 
18765dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetContext(qf_mass, qf_ctx));
18865dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_mass, 0));
18965dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q_dot", 5, CEED_EVAL_INTERP));
19065dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "q", 5, CEED_EVAL_INTERP));
19165dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_mass, "qdata", q_data_size, CEED_EVAL_NONE));
19265dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "v", 5, CEED_EVAL_INTERP));
19365dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_mass, "Grad_v", 5 * dim, CEED_EVAL_GRAD));
19465dee3d2SJames Wright 
19565dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass));
19665dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q_dot", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
19765dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "q", elem_restr_q, basis_q, user->q_ceed));
19865dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd_i, CEED_BASIS_NONE, q_data));
19965dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
20065dee3d2SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "Grad_v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
20165dee3d2SJames Wright 
202d438a78dSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&qf_ctx));
20365dee3d2SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
20465dee3d2SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
20565dee3d2SJames Wright }
2064c07ec22SJames Wright 
207*36038bbcSJames Wright /**
208*36038bbcSJames Wright   @brief Create RHS CeedOperator for direct projection of divergence of diffusive flux
209*36038bbcSJames Wright 
210*36038bbcSJames Wright   @param[in]  user           `User` object
211*36038bbcSJames Wright   @param[in]  ceed_data      `CeedData` object
212*36038bbcSJames Wright   @param[in]  diff_flux_proj `DivDiffFluxProjectionData` object
213*36038bbcSJames Wright   @param[out] op_rhs         Operator to calculate the RHS of the L^2 projection
214*36038bbcSJames Wright **/
215*36038bbcSJames Wright static PetscErrorCode DivDiffFluxProjectionCreateRHS_Direct_NS(User user, CeedData ceed_data, DivDiffFluxProjectionData diff_flux_proj,
216*36038bbcSJames Wright                                                                CeedOperator *op_rhs) {
217*36038bbcSJames Wright   Ceed                 ceed       = user->ceed;
218*36038bbcSJames Wright   NodalProjectionData  projection = diff_flux_proj->projection;
219*36038bbcSJames Wright   CeedBasis            basis_diff_flux;
220*36038bbcSJames Wright   CeedElemRestriction  elem_restr_diff_flux_volume;
221*36038bbcSJames Wright   CeedInt              num_comp_q;
222*36038bbcSJames Wright   PetscInt             dim, label_value = 0;
223*36038bbcSJames Wright   DMLabel              domain_label     = NULL;
224*36038bbcSJames Wright   CeedQFunctionContext newtonian_qf_ctx = NULL;
225*36038bbcSJames Wright 
226*36038bbcSJames Wright   PetscFunctionBeginUser;
227*36038bbcSJames Wright   // -- Get Pre-requisite things
228*36038bbcSJames Wright   PetscCall(DMGetDimension(projection->dm, &dim));
229*36038bbcSJames Wright   PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q));
230*36038bbcSJames Wright 
231*36038bbcSJames Wright   {  // Get elem_restr_diff_flux and basis_diff_flux
232*36038bbcSJames Wright     CeedOperator     *sub_ops;
233*36038bbcSJames Wright     CeedOperatorField op_field;
234*36038bbcSJames Wright     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
235*36038bbcSJames Wright 
236*36038bbcSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_ifunction, &sub_ops));
237*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "div F_diff", &op_field));
238*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(op_field, &elem_restr_diff_flux_volume));
239*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetBasis(op_field, &basis_diff_flux));
240*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newtonian_qf_ctx));
241*36038bbcSJames Wright   }
242*36038bbcSJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, op_rhs));
243*36038bbcSJames Wright   {  // Add the volume integral CeedOperator
244*36038bbcSJames Wright     CeedQFunction       qf_rhs_volume;
245*36038bbcSJames Wright     CeedOperator        op_rhs_volume;
246*36038bbcSJames Wright     CeedVector          q_data;
247*36038bbcSJames Wright     CeedElemRestriction elem_restr_qd;
248*36038bbcSJames Wright     CeedInt             q_data_size;
249*36038bbcSJames Wright 
250*36038bbcSJames Wright     PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord,
251*36038bbcSJames Wright                        &elem_restr_qd, &q_data, &q_data_size));
252*36038bbcSJames Wright     switch (user->phys->state_var) {
253*36038bbcSJames Wright       case STATEVAR_PRIMITIVE:
254*36038bbcSJames Wright         PetscCallCeed(ceed,
255*36038bbcSJames Wright                       CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Prim, DivDiffusiveFluxVolumeRHS_NS_Prim_loc, &qf_rhs_volume));
256*36038bbcSJames Wright         break;
257*36038bbcSJames Wright       case STATEVAR_CONSERVATIVE:
258*36038bbcSJames Wright         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Conserv, DivDiffusiveFluxVolumeRHS_NS_Conserv_loc,
259*36038bbcSJames Wright                                                         &qf_rhs_volume));
260*36038bbcSJames Wright         break;
261*36038bbcSJames Wright       case STATEVAR_ENTROPY:
262*36038bbcSJames Wright         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxVolumeRHS_NS_Entropy, DivDiffusiveFluxVolumeRHS_NS_Entropy_loc,
263*36038bbcSJames Wright                                                         &qf_rhs_volume));
264*36038bbcSJames Wright         break;
265*36038bbcSJames Wright     }
266*36038bbcSJames Wright 
267*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_volume, newtonian_qf_ctx));
268*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "q", num_comp_q, CEED_EVAL_INTERP));
269*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
270*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_volume, "qdata", q_data_size, CEED_EVAL_NONE));
271*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_volume, "diffusive flux RHS", projection->num_comp * dim, CEED_EVAL_GRAD));
272*36038bbcSJames Wright 
273*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_volume, NULL, NULL, &op_rhs_volume));
274*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
275*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
276*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
277*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_volume, "diffusive flux RHS", elem_restr_diff_flux_volume, basis_diff_flux, CEED_VECTOR_ACTIVE));
278*36038bbcSJames Wright 
279*36038bbcSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_rhs, op_rhs_volume));
280*36038bbcSJames Wright 
281*36038bbcSJames Wright     PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
282*36038bbcSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
283*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_volume));
284*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_volume));
285*36038bbcSJames Wright   }
286*36038bbcSJames Wright 
287*36038bbcSJames Wright   {  // Add the boundary integral CeedOperator
288*36038bbcSJames Wright     CeedQFunction qf_rhs_boundary;
289*36038bbcSJames Wright     DMLabel       face_sets_label;
290*36038bbcSJames Wright     PetscInt      num_face_set_values, *face_set_values;
291*36038bbcSJames Wright     CeedInt       q_data_size;
292*36038bbcSJames Wright 
293*36038bbcSJames Wright     // -- Build RHS operator
294*36038bbcSJames Wright     switch (user->phys->state_var) {
295*36038bbcSJames Wright       case STATEVAR_PRIMITIVE:
296*36038bbcSJames Wright         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Prim, DivDiffusiveFluxBoundaryRHS_NS_Prim_loc,
297*36038bbcSJames Wright                                                         &qf_rhs_boundary));
298*36038bbcSJames Wright         break;
299*36038bbcSJames Wright       case STATEVAR_CONSERVATIVE:
300*36038bbcSJames Wright         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Conserv, DivDiffusiveFluxBoundaryRHS_NS_Conserv_loc,
301*36038bbcSJames Wright                                                         &qf_rhs_boundary));
302*36038bbcSJames Wright         break;
303*36038bbcSJames Wright       case STATEVAR_ENTROPY:
304*36038bbcSJames Wright         PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DivDiffusiveFluxBoundaryRHS_NS_Entropy, DivDiffusiveFluxBoundaryRHS_NS_Entropy_loc,
305*36038bbcSJames Wright                                                         &qf_rhs_boundary));
306*36038bbcSJames Wright         break;
307*36038bbcSJames Wright     }
308*36038bbcSJames Wright 
309*36038bbcSJames Wright     PetscCall(QDataBoundaryGradientGetNumComponents(user->dm, &q_data_size));
310*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_boundary, newtonian_qf_ctx));
311*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "q", num_comp_q, CEED_EVAL_INTERP));
312*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
313*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_boundary, "qdata", q_data_size, CEED_EVAL_NONE));
314*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_boundary, "diffusive flux RHS", projection->num_comp, CEED_EVAL_INTERP));
315*36038bbcSJames Wright 
316*36038bbcSJames Wright     PetscCall(DMGetLabel(projection->dm, "Face Sets", &face_sets_label));
317*36038bbcSJames Wright     PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_sets_label, &num_face_set_values, &face_set_values));
318*36038bbcSJames Wright     for (PetscInt f = 0; f < num_face_set_values; f++) {
319*36038bbcSJames Wright       DMLabel  face_orientation_label;
320*36038bbcSJames Wright       PetscInt num_orientations_values, *orientation_values;
321*36038bbcSJames Wright 
322*36038bbcSJames Wright       {
323*36038bbcSJames Wright         char *face_orientation_label_name;
324*36038bbcSJames Wright 
325*36038bbcSJames Wright         PetscCall(DMPlexCreateFaceLabel(projection->dm, face_set_values[f], &face_orientation_label_name));
326*36038bbcSJames Wright         PetscCall(DMGetLabel(projection->dm, face_orientation_label_name, &face_orientation_label));
327*36038bbcSJames Wright         PetscCall(PetscFree(face_orientation_label_name));
328*36038bbcSJames Wright       }
329*36038bbcSJames Wright       PetscCall(DMLabelCreateGlobalValueArray(projection->dm, face_orientation_label, &num_orientations_values, &orientation_values));
330*36038bbcSJames Wright       for (PetscInt o = 0; o < num_orientations_values; o++) {
331*36038bbcSJames Wright         CeedOperator        op_rhs_boundary;
332*36038bbcSJames Wright         CeedBasis           basis_q, basis_diff_flux_boundary;
333*36038bbcSJames Wright         CeedElemRestriction elem_restr_qdata, elem_restr_q, elem_restr_diff_flux_boundary;
334*36038bbcSJames Wright         CeedVector          q_data;
335*36038bbcSJames Wright         CeedInt             q_data_size;
336*36038bbcSJames Wright         PetscInt            orientation = orientation_values[o], dm_field_q = 0, height_cell = 0, height_face = 1;
337*36038bbcSJames Wright 
338*36038bbcSJames Wright         PetscCall(DMPlexCeedElemRestrictionCreate(ceed, user->dm, face_orientation_label, orientation, height_cell, dm_field_q, &elem_restr_q));
339*36038bbcSJames Wright         PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, face_orientation_label, orientation, height_face, 0,
340*36038bbcSJames Wright                                                   &elem_restr_diff_flux_boundary));
341*36038bbcSJames Wright         PetscCall(QDataBoundaryGradientGet(ceed, user->dm, face_orientation_label, orientation, ceed_data->x_coord, &elem_restr_qdata, &q_data,
342*36038bbcSJames Wright                                            &q_data_size));
343*36038bbcSJames Wright         PetscCall(DMPlexCeedBasisCellToFaceCreate(ceed, user->dm, face_orientation_label, orientation, orientation, dm_field_q, &basis_q));
344*36038bbcSJames Wright         PetscCall(CreateBasisFromPlex(ceed, projection->dm, face_orientation_label, orientation, height_face, 0, &basis_diff_flux_boundary));
345*36038bbcSJames Wright 
346*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_boundary, NULL, NULL, &op_rhs_boundary));
347*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
348*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "Grad_q", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
349*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "qdata", elem_restr_qdata, CEED_BASIS_NONE, q_data));
350*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_boundary, "diffusive flux RHS", elem_restr_diff_flux_boundary, basis_diff_flux_boundary,
351*36038bbcSJames Wright                                                  CEED_VECTOR_ACTIVE));
352*36038bbcSJames Wright 
353*36038bbcSJames Wright         PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_rhs, op_rhs_boundary));
354*36038bbcSJames Wright 
355*36038bbcSJames Wright         PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_boundary));
356*36038bbcSJames Wright         PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qdata));
357*36038bbcSJames Wright         PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q));
358*36038bbcSJames Wright         PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux_boundary));
359*36038bbcSJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_q));
360*36038bbcSJames Wright         PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux_boundary));
361*36038bbcSJames Wright         PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
362*36038bbcSJames Wright       }
363*36038bbcSJames Wright       PetscCall(PetscFree(orientation_values));
364*36038bbcSJames Wright     }
365*36038bbcSJames Wright     PetscCall(PetscFree(face_set_values));
366*36038bbcSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_boundary));
367*36038bbcSJames Wright   }
368*36038bbcSJames Wright 
369*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newtonian_qf_ctx));
370*36038bbcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
371*36038bbcSJames Wright }
372*36038bbcSJames Wright 
373*36038bbcSJames Wright /**
374*36038bbcSJames Wright   @brief Create RHS CeedOperator for indirect projection of divergence of diffusive flux
375*36038bbcSJames Wright 
376*36038bbcSJames Wright   @param[in]  user           `User` object
377*36038bbcSJames Wright   @param[in]  ceed_data      `CeedData` object
378*36038bbcSJames Wright   @param[in]  diff_flux_proj `DivDiffFluxProjectionData` object
379*36038bbcSJames Wright   @param[out] op_rhs         Operator to calculate the RHS of the L^2 projection
380*36038bbcSJames Wright **/
381*36038bbcSJames Wright static PetscErrorCode DivDiffFluxProjectionCreateRHS_Indirect_NS(User user, CeedData ceed_data, DivDiffFluxProjectionData diff_flux_proj,
382*36038bbcSJames Wright                                                                  CeedOperator *op_rhs) {
383*36038bbcSJames Wright   Ceed                 ceed       = user->ceed;
384*36038bbcSJames Wright   NodalProjectionData  projection = diff_flux_proj->projection;
385*36038bbcSJames Wright   CeedBasis            basis_diff_flux;
386*36038bbcSJames Wright   CeedElemRestriction  elem_restr_diff_flux, elem_restr_qd;
387*36038bbcSJames Wright   CeedVector           q_data;
388*36038bbcSJames Wright   CeedInt              num_comp_q, q_data_size;
389*36038bbcSJames Wright   PetscInt             dim;
390*36038bbcSJames Wright   PetscInt             label_value = 0, height = 0, dm_field = 0;
391*36038bbcSJames Wright   DMLabel              domain_label = NULL;
392*36038bbcSJames Wright   CeedQFunction        qf_rhs;
393*36038bbcSJames Wright   CeedQFunctionContext newtonian_qf_ctx = NULL;
394*36038bbcSJames Wright 
395*36038bbcSJames Wright   PetscFunctionBeginUser;
396*36038bbcSJames Wright   PetscCall(DMGetDimension(projection->dm, &dim));
397*36038bbcSJames Wright   PetscCallCeed(ceed, CeedBasisGetNumComponents(ceed_data->basis_q, &num_comp_q));
398*36038bbcSJames Wright 
399*36038bbcSJames Wright   {  // Get elem_restr_div_diff_flux
400*36038bbcSJames Wright     CeedOperator *sub_ops;
401*36038bbcSJames Wright     PetscInt      sub_op_index = 0;  // will be 0 for the volume op
402*36038bbcSJames Wright 
403*36038bbcSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_ifunction, &sub_ops));
404*36038bbcSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContext(sub_ops[sub_op_index], &newtonian_qf_ctx));
405*36038bbcSJames Wright   }
406*36038bbcSJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, projection->dm, domain_label, label_value, height, dm_field, &elem_restr_diff_flux));
407*36038bbcSJames Wright   PetscCall(CreateBasisFromPlex(ceed, projection->dm, domain_label, label_value, height, dm_field, &basis_diff_flux));
408*36038bbcSJames Wright   PetscCall(QDataGet(ceed, projection->dm, domain_label, label_value, ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord, &elem_restr_qd,
409*36038bbcSJames Wright                      &q_data, &q_data_size));
410*36038bbcSJames Wright 
411*36038bbcSJames Wright   switch (user->phys->state_var) {
412*36038bbcSJames Wright     case STATEVAR_PRIMITIVE:
413*36038bbcSJames Wright       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Prim, DiffusiveFluxRHS_NS_Prim_loc, &qf_rhs));
414*36038bbcSJames Wright       break;
415*36038bbcSJames Wright     case STATEVAR_CONSERVATIVE:
416*36038bbcSJames Wright       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Conserv, DiffusiveFluxRHS_NS_Conserv_loc, &qf_rhs));
417*36038bbcSJames Wright       break;
418*36038bbcSJames Wright     case STATEVAR_ENTROPY:
419*36038bbcSJames Wright       PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, DiffusiveFluxRHS_NS_Entropy, DiffusiveFluxRHS_NS_Entropy_loc, &qf_rhs));
420*36038bbcSJames Wright       break;
421*36038bbcSJames Wright   }
422*36038bbcSJames Wright 
423*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs, newtonian_qf_ctx));
424*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "q", num_comp_q, CEED_EVAL_INTERP));
425*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
426*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs, "qdata", q_data_size, CEED_EVAL_NONE));
427*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs, "F_diff RHS", projection->num_comp, CEED_EVAL_INTERP));
428*36038bbcSJames Wright 
429*36038bbcSJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs, NULL, NULL, op_rhs));
430*36038bbcSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
431*36038bbcSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
432*36038bbcSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "qdata", elem_restr_qd, CEED_BASIS_NONE, q_data));
433*36038bbcSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_rhs, "F_diff RHS", elem_restr_diff_flux, basis_diff_flux, CEED_VECTOR_ACTIVE));
434*36038bbcSJames Wright 
435*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs));
436*36038bbcSJames Wright   PetscCallCeed(ceed, CeedQFunctionContextDestroy(&newtonian_qf_ctx));
437*36038bbcSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_diff_flux));
438*36038bbcSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data));
439*36038bbcSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd));
440*36038bbcSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_diff_flux));
441*36038bbcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
442*36038bbcSJames Wright }
443*36038bbcSJames Wright 
444991aef52SJames Wright PetscErrorCode NS_NEWTONIAN_IG(ProblemData problem, DM dm, void *ctx, SimpleBC bc) {
445b7f03f12SJed Brown   SetupContext             setup_context;
4463a8779fbSJames Wright   User                     user   = *(User *)ctx;
4474351d27fSLeila Ghaffari   CeedInt                  degree = user->app_ctx->degree;
4483a8779fbSJames Wright   StabilizationType        stab;
4493636f6a4SJames Wright   StateVariable            state_var;
450b4c37c5cSJames Wright   MPI_Comm                 comm = user->comm;
451b4c37c5cSJames Wright   Ceed                     ceed = user->ceed;
4523a8779fbSJames Wright   PetscBool                implicit;
4530d8cd818SJames Wright   PetscBool                unit_tests;
45415a3537eSJed Brown   NewtonianIdealGasContext newtonian_ig_ctx;
455e07531f7SJames Wright   CeedQFunctionContext     newtonian_ig_qfctx;
4563a8779fbSJames Wright 
45715a3537eSJed Brown   PetscFunctionBeginUser;
4582b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &setup_context));
4592b916ea7SJeremy L Thompson   PetscCall(PetscCalloc1(1, &newtonian_ig_ctx));
4603a8779fbSJames Wright 
4613a8779fbSJames Wright   // ------------------------------------------------------
4623a8779fbSJames Wright   //           Setup Generic Newtonian IG Problem
4633a8779fbSJames Wright   // ------------------------------------------------------
46428160fc2SJames Wright   problem->jac_data_size_vol            = 14;
465b01ba163SJames Wright   problem->jac_data_size_sur            = 11;
46658ce1233SJames Wright   problem->compute_exact_solution_error = PETSC_FALSE;
467cbe60e31SLeila Ghaffari   problem->print_info                   = PRINT_NEWTONIAN;
4683a8779fbSJames Wright 
469*36038bbcSJames Wright   PetscCall(DivDiffFluxProjectionCreate(user, 4, &user->diff_flux_proj));
470*36038bbcSJames Wright   if (user->diff_flux_proj) {
471*36038bbcSJames Wright     DivDiffFluxProjectionData diff_flux_proj = user->diff_flux_proj;
472*36038bbcSJames Wright     NodalProjectionData       projection     = diff_flux_proj->projection;
473*36038bbcSJames Wright 
474*36038bbcSJames Wright     diff_flux_proj->CreateRHSOperator_Direct   = DivDiffFluxProjectionCreateRHS_Direct_NS;
475*36038bbcSJames Wright     diff_flux_proj->CreateRHSOperator_Indirect = DivDiffFluxProjectionCreateRHS_Indirect_NS;
476*36038bbcSJames Wright 
477*36038bbcSJames Wright     switch (user->diff_flux_proj->method) {
478*36038bbcSJames Wright       case DIV_DIFF_FLUX_PROJ_DIRECT: {
479*36038bbcSJames Wright         PetscSection section;
480*36038bbcSJames Wright 
481*36038bbcSJames Wright         PetscCall(DMGetLocalSection(projection->dm, &section));
482*36038bbcSJames Wright         PetscCall(PetscSectionSetFieldName(section, 0, ""));
483*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 0, "DivDiffusiveFlux_MomentumX"));
484*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 1, "DivDiffusiveFlux_MomentumY"));
485*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 2, "DivDiffusiveFlux_MomentumZ"));
486*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 3, "DivDiffusiveFlux_Energy"));
487*36038bbcSJames Wright       } break;
488*36038bbcSJames Wright       case DIV_DIFF_FLUX_PROJ_INDIRECT: {
489*36038bbcSJames Wright         PetscSection section;
490*36038bbcSJames Wright 
491*36038bbcSJames Wright         PetscCall(DMGetLocalSection(projection->dm, &section));
492*36038bbcSJames Wright         PetscCall(PetscSectionSetFieldName(section, 0, ""));
493*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 0, "DiffusiveFlux_MomentumXX"));
494*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 1, "DiffusiveFlux_MomentumXY"));
495*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 2, "DiffusiveFlux_MomentumXZ"));
496*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 3, "DiffusiveFlux_MomentumYX"));
497*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 4, "DiffusiveFlux_MomentumYY"));
498*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 5, "DiffusiveFlux_MomentumYZ"));
499*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 6, "DiffusiveFlux_MomentumZX"));
500*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 7, "DiffusiveFlux_MomentumZY"));
501*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 8, "DiffusiveFlux_MomentumZZ"));
502*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 9, "DiffusiveFlux_EnergyX"));
503*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 10, "DiffusiveFlux_EnergyY"));
504*36038bbcSJames Wright         PetscCall(PetscSectionSetComponentName(section, 0, 11, "DiffusiveFlux_EnergyZ"));
505*36038bbcSJames Wright       } break;
506*36038bbcSJames Wright       case DIV_DIFF_FLUX_PROJ_NONE:
507*36038bbcSJames Wright         SETERRQ(PetscObjectComm((PetscObject)user->dm), PETSC_ERR_ARG_WRONG, "Should not reach here with div_diff_flux_projection_method %s",
508*36038bbcSJames Wright                 DivDiffFluxProjectionMethods[user->app_ctx->divFdiffproj_method]);
509*36038bbcSJames Wright         break;
510*36038bbcSJames Wright     }
511*36038bbcSJames Wright   }
512*36038bbcSJames Wright 
5133a8779fbSJames Wright   // ------------------------------------------------------
5143a8779fbSJames Wright   //             Create the libCEED context
5153a8779fbSJames Wright   // ------------------------------------------------------
5163a8779fbSJames Wright   CeedScalar cv         = 717.;          // J/(kg K)
5173a8779fbSJames Wright   CeedScalar cp         = 1004.;         // J/(kg K)
518d9bb1cdbSJames Wright   CeedScalar g[3]       = {0, 0, 0};     // m/s^2
5193a8779fbSJames Wright   CeedScalar lambda     = -2. / 3.;      // -
520bb8a0c61SJames Wright   CeedScalar mu         = 1.8e-5;        // Pa s, dynamic viscosity
5213a8779fbSJames Wright   CeedScalar k          = 0.02638;       // W/(m K)
5224351d27fSLeila Ghaffari   CeedScalar c_tau      = 0.5 / degree;  // -
523bb8a0c61SJames Wright   CeedScalar Ctau_t     = 1.0;           // -
524b5786772SLeila Ghaffari   CeedScalar Cv_func[3] = {36, 60, 128};
525c176988bSLeila Ghaffari   CeedScalar Ctau_v     = Cv_func[(CeedInt)Min(3, degree) - 1];
5262a0eee6eSLeila Ghaffari   CeedScalar Ctau_C     = 0.25 / degree;
5272a0eee6eSLeila Ghaffari   CeedScalar Ctau_M     = 0.25 / degree;
5282a0eee6eSLeila Ghaffari   CeedScalar Ctau_E     = 0.125;
5293a8779fbSJames Wright   PetscReal  domain_min[3], domain_max[3], domain_size[3];
5302b916ea7SJeremy L Thompson   PetscCall(DMGetBoundingBox(dm, domain_min, domain_max));
531493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i];
5323a8779fbSJames Wright 
533b8fb7609SAdeleke O. Bankole   StatePrimitive reference      = {.pressure = 1.01e5, .velocity = {0}, .temperature = 288.15};
534a213b8aaSJames Wright   CeedScalar     idl_decay_time = -1, idl_start = 0, idl_length = 0, idl_pressure = reference.pressure;
535e7754af5SKenneth E. Jansen   PetscBool      idl_enable = PETSC_FALSE;
536b8fb7609SAdeleke O. Bankole 
5373a8779fbSJames Wright   // ------------------------------------------------------
5383a8779fbSJames Wright   //             Create the PETSc context
5393a8779fbSJames Wright   // ------------------------------------------------------
540bb8a0c61SJames Wright   PetscScalar meter    = 1;  // 1 meter in scaled length units
541bb8a0c61SJames Wright   PetscScalar kilogram = 1;  // 1 kilogram in scaled mass units
542bb8a0c61SJames Wright   PetscScalar second   = 1;  // 1 second in scaled time units
5433a8779fbSJames Wright   PetscScalar Kelvin   = 1;  // 1 Kelvin in scaled temperature units
5443a8779fbSJames Wright   PetscScalar W_per_m_K, Pascal, J_per_kg_K, m_per_squared_s;
5453a8779fbSJames Wright 
5463a8779fbSJames Wright   // ------------------------------------------------------
5473a8779fbSJames Wright   //              Command line Options
5483a8779fbSJames Wright   // ------------------------------------------------------
549d9bb1cdbSJames Wright   PetscBool given_option = PETSC_FALSE;
5502b916ea7SJeremy L Thompson   PetscOptionsBegin(comm, NULL, "Options for Newtonian Ideal Gas based problem", NULL);
551cbe60e31SLeila Ghaffari   // -- Conservative vs Primitive variables
5522b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-state_var", "State variables used", NULL, StateVariables, (PetscEnum)(state_var = STATEVAR_CONSERVATIVE),
5532b916ea7SJeremy L Thompson                              (PetscEnum *)&state_var, NULL));
5543636f6a4SJames Wright 
5553636f6a4SJames Wright   switch (state_var) {
5563636f6a4SJames Wright     case STATEVAR_CONSERVATIVE:
557e07531f7SJames Wright       problem->ics.qf_func_ptr                   = ICsNewtonianIG_Conserv;
558e07531f7SJames Wright       problem->ics.qf_loc                        = ICsNewtonianIG_Conserv_loc;
559e07531f7SJames Wright       problem->apply_vol_rhs.qf_func_ptr         = RHSFunction_Newtonian;
560e07531f7SJames Wright       problem->apply_vol_rhs.qf_loc              = RHSFunction_Newtonian_loc;
561e07531f7SJames Wright       problem->apply_vol_ifunction.qf_func_ptr   = IFunction_Newtonian_Conserv;
562e07531f7SJames Wright       problem->apply_vol_ifunction.qf_loc        = IFunction_Newtonian_Conserv_loc;
563e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_func_ptr   = IJacobian_Newtonian_Conserv;
564e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_loc        = IJacobian_Newtonian_Conserv_loc;
565e07531f7SJames Wright       problem->apply_inflow.qf_func_ptr          = BoundaryIntegral_Conserv;
566e07531f7SJames Wright       problem->apply_inflow.qf_loc               = BoundaryIntegral_Conserv_loc;
567e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_func_ptr = BoundaryIntegral_Jacobian_Conserv;
568e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_loc      = BoundaryIntegral_Jacobian_Conserv_loc;
5693636f6a4SJames Wright       break;
5703636f6a4SJames Wright     case STATEVAR_PRIMITIVE:
571e07531f7SJames Wright       problem->ics.qf_func_ptr                   = ICsNewtonianIG_Prim;
572e07531f7SJames Wright       problem->ics.qf_loc                        = ICsNewtonianIG_Prim_loc;
573e07531f7SJames Wright       problem->apply_vol_ifunction.qf_func_ptr   = IFunction_Newtonian_Prim;
574e07531f7SJames Wright       problem->apply_vol_ifunction.qf_loc        = IFunction_Newtonian_Prim_loc;
575e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_func_ptr   = IJacobian_Newtonian_Prim;
576e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_loc        = IJacobian_Newtonian_Prim_loc;
577e07531f7SJames Wright       problem->apply_inflow.qf_func_ptr          = BoundaryIntegral_Prim;
578e07531f7SJames Wright       problem->apply_inflow.qf_loc               = BoundaryIntegral_Prim_loc;
579e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_func_ptr = BoundaryIntegral_Jacobian_Prim;
580e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_loc      = BoundaryIntegral_Jacobian_Prim_loc;
5813636f6a4SJames Wright       break;
582c62f7daeSJames Wright     case STATEVAR_ENTROPY:
583e07531f7SJames Wright       problem->ics.qf_func_ptr                   = ICsNewtonianIG_Entropy;
584e07531f7SJames Wright       problem->ics.qf_loc                        = ICsNewtonianIG_Entropy_loc;
585e07531f7SJames Wright       problem->apply_vol_ifunction.qf_func_ptr   = IFunction_Newtonian_Entropy;
586e07531f7SJames Wright       problem->apply_vol_ifunction.qf_loc        = IFunction_Newtonian_Entropy_loc;
587e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_func_ptr   = IJacobian_Newtonian_Entropy;
588e07531f7SJames Wright       problem->apply_vol_ijacobian.qf_loc        = IJacobian_Newtonian_Entropy_loc;
589e07531f7SJames Wright       problem->apply_inflow.qf_func_ptr          = BoundaryIntegral_Entropy;
590e07531f7SJames Wright       problem->apply_inflow.qf_loc               = BoundaryIntegral_Entropy_loc;
591e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_func_ptr = BoundaryIntegral_Jacobian_Entropy;
592e07531f7SJames Wright       problem->apply_inflow_jacobian.qf_loc      = BoundaryIntegral_Jacobian_Entropy_loc;
593c62f7daeSJames Wright       break;
594cbe60e31SLeila Ghaffari   }
5951485969bSJeremy L Thompson 
5963a8779fbSJames Wright   // -- Physics
5972b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-cv", "Heat capacity at constant volume", NULL, cv, &cv, NULL));
5982b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-cp", "Heat capacity at constant pressure", NULL, cp, &cp, NULL));
5992b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-lambda", "Stokes hypothesis second viscosity coefficient", NULL, lambda, &lambda, NULL));
6002b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-mu", "Shear dynamic viscosity coefficient", NULL, mu, &mu, NULL));
6012b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-k", "Thermal conductivity", NULL, k, &k, NULL));
6023a8779fbSJames Wright 
60366d54740SJames Wright   PetscInt dim = 3;
604d9bb1cdbSJames Wright   PetscCall(PetscOptionsDeprecated("-g", "-gravity", "libCEED 0.11.1", NULL));
605d9bb1cdbSJames Wright   PetscCall(PetscOptionsRealArray("-gravity", "Gravitational acceleration vector", NULL, g, &dim, &given_option));
606d9bb1cdbSJames Wright   if (given_option) PetscCheck(dim == 3, comm, PETSC_ERR_ARG_SIZ, "Gravity vector must be size 3, %" PetscInt_FMT " values given", dim);
607d9bb1cdbSJames Wright 
6082b916ea7SJeremy L Thompson   PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL));
6092b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL));
6102b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_t", "Stabilization time constant", NULL, Ctau_t, &Ctau_t, NULL));
6112b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_v", "Stabilization viscous constant", NULL, Ctau_v, &Ctau_v, NULL));
6122b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_C", "Stabilization continuity constant", NULL, Ctau_C, &Ctau_C, NULL));
6132b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_M", "Stabilization momentum constant", NULL, Ctau_M, &Ctau_M, NULL));
6142b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-Ctau_E", "Stabilization energy constant", NULL, Ctau_E, &Ctau_E, NULL));
6152b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL));
6162b916ea7SJeremy L Thompson   PetscCall(PetscOptionsBool("-newtonian_unit_tests", "Run Newtonian unit tests", NULL, unit_tests = PETSC_FALSE, &unit_tests, NULL));
6173a8779fbSJames Wright 
618db95602eSJames Wright   dim = 3;
619b8fb7609SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_pressure", "Reference/initial pressure", NULL, reference.pressure, &reference.pressure, NULL));
620b8fb7609SAdeleke O. Bankole   PetscCall(PetscOptionsScalarArray("-reference_velocity", "Reference/initial velocity", NULL, reference.velocity, &dim, NULL));
621b8fb7609SAdeleke O. Bankole   PetscCall(PetscOptionsScalar("-reference_temperature", "Reference/initial temperature", NULL, reference.temperature, &reference.temperature, NULL));
622b8fb7609SAdeleke O. Bankole 
6233a8779fbSJames Wright   // -- Units
6242b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL));
6253a8779fbSJames Wright   meter = fabs(meter);
6262b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_kilogram", "1 kilogram in scaled mass units", NULL, kilogram, &kilogram, NULL));
6273a8779fbSJames Wright   kilogram = fabs(kilogram);
6282b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL));
6293a8779fbSJames Wright   second = fabs(second);
6302b916ea7SJeremy L Thompson   PetscCall(PetscOptionsScalar("-units_Kelvin", "1 Kelvin in scaled temperature units", NULL, Kelvin, &Kelvin, NULL));
6313a8779fbSJames Wright   Kelvin = fabs(Kelvin);
6323a8779fbSJames Wright 
6333a8779fbSJames Wright   // -- Warnings
6345d28dccaSJames Wright   PetscCheck(!(state_var == STATEVAR_PRIMITIVE && !implicit), comm, PETSC_ERR_SUP,
6355d28dccaSJames Wright              "RHSFunction is not provided for primitive variables (use -state_var primitive only with -implicit)\n");
6368c85b835SJames Wright   PetscCheck(!(user->app_ctx->divFdiffproj_method != DIV_DIFF_FLUX_PROJ_NONE && !implicit), comm, PETSC_ERR_SUP,
6378c85b835SJames Wright              "Projection of divergence of diffusive flux is not implemented for explicit timestepping");
638e7754af5SKenneth E. Jansen 
639e7754af5SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_decay_time", "Characteristic timescale of the pressure deviance decay. The timestep is good starting point",
640e7754af5SKenneth E. Jansen                                NULL, idl_decay_time, &idl_decay_time, &idl_enable));
6419cbdf780SJames Wright   PetscCheck(!(idl_enable && idl_decay_time == 0), comm, PETSC_ERR_SUP, "idl_decay_time may not be equal to zero.");
6429cbdf780SJames Wright   if (idl_decay_time < 0) idl_enable = PETSC_FALSE;
64328160fc2SJames Wright   if (idl_enable) problem->jac_data_size_vol++;
644e7754af5SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_start", "Start of IDL in the x direction", NULL, idl_start, &idl_start, NULL));
645e7754af5SKenneth E. Jansen   PetscCall(PetscOptionsScalar("-idl_length", "Length of IDL in the positive x direction", NULL, idl_length, &idl_length, NULL));
646a213b8aaSJames Wright   idl_pressure = reference.pressure;
647a213b8aaSJames Wright   PetscCall(PetscOptionsScalar("-idl_pressure", "Pressure IDL uses as reference (default is `-reference_pressure`)", NULL, idl_pressure,
648a213b8aaSJames Wright                                &idl_pressure, NULL));
6491485969bSJeremy L Thompson   PetscOptionsEnd();
6503a8779fbSJames Wright 
65165dee3d2SJames Wright   if (stab == STAB_SUPG && !implicit) problem->create_mass_operator = CreateKSPMassOperator_NewtonianStabilized;
65265dee3d2SJames Wright 
6533a8779fbSJames Wright   // ------------------------------------------------------
6543a8779fbSJames Wright   //           Set up the PETSc context
6553a8779fbSJames Wright   // ------------------------------------------------------
6563a8779fbSJames Wright   // -- Define derived units
6573a8779fbSJames Wright   Pascal          = kilogram / (meter * PetscSqr(second));
6583a8779fbSJames Wright   J_per_kg_K      = PetscSqr(meter) / (PetscSqr(second) * Kelvin);
6593a8779fbSJames Wright   m_per_squared_s = meter / PetscSqr(second);
6603a8779fbSJames Wright   W_per_m_K       = kilogram * meter / (pow(second, 3) * Kelvin);
6613a8779fbSJames Wright 
6623a8779fbSJames Wright   user->units->meter           = meter;
6633a8779fbSJames Wright   user->units->kilogram        = kilogram;
6643a8779fbSJames Wright   user->units->second          = second;
6653a8779fbSJames Wright   user->units->Kelvin          = Kelvin;
6663a8779fbSJames Wright   user->units->Pascal          = Pascal;
6673a8779fbSJames Wright   user->units->J_per_kg_K      = J_per_kg_K;
6683a8779fbSJames Wright   user->units->m_per_squared_s = m_per_squared_s;
6693a8779fbSJames Wright   user->units->W_per_m_K       = W_per_m_K;
6703a8779fbSJames Wright 
6713a8779fbSJames Wright   // ------------------------------------------------------
6723a8779fbSJames Wright   //           Set up the libCEED context
6733a8779fbSJames Wright   // ------------------------------------------------------
6743a8779fbSJames Wright   // -- Scale variables to desired units
6753a8779fbSJames Wright   cv *= J_per_kg_K;
6763a8779fbSJames Wright   cp *= J_per_kg_K;
6773a8779fbSJames Wright   mu *= Pascal * second;
6783a8779fbSJames Wright   k *= W_per_m_K;
679493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) domain_size[i] *= meter;
680493642f1SJames Wright   for (PetscInt i = 0; i < 3; i++) g[i] *= m_per_squared_s;
681b8fb7609SAdeleke O. Bankole   reference.pressure *= Pascal;
682b8fb7609SAdeleke O. Bankole   for (PetscInt i = 0; i < 3; i++) reference.velocity[i] *= meter / second;
683b8fb7609SAdeleke O. Bankole   reference.temperature *= Kelvin;
6843a8779fbSJames Wright 
6853a8779fbSJames Wright   // -- Solver Settings
6863a8779fbSJames Wright   user->phys->implicit  = implicit;
6873636f6a4SJames Wright   user->phys->state_var = state_var;
6883a8779fbSJames Wright 
6893a8779fbSJames Wright   // -- QFunction Context
69015a3537eSJed Brown   newtonian_ig_ctx->lambda          = lambda;
69115a3537eSJed Brown   newtonian_ig_ctx->mu              = mu;
69215a3537eSJed Brown   newtonian_ig_ctx->k               = k;
69315a3537eSJed Brown   newtonian_ig_ctx->cv              = cv;
69415a3537eSJed Brown   newtonian_ig_ctx->cp              = cp;
69515a3537eSJed Brown   newtonian_ig_ctx->c_tau           = c_tau;
69615a3537eSJed Brown   newtonian_ig_ctx->Ctau_t          = Ctau_t;
69715a3537eSJed Brown   newtonian_ig_ctx->Ctau_v          = Ctau_v;
69815a3537eSJed Brown   newtonian_ig_ctx->Ctau_C          = Ctau_C;
69915a3537eSJed Brown   newtonian_ig_ctx->Ctau_M          = Ctau_M;
70015a3537eSJed Brown   newtonian_ig_ctx->Ctau_E          = Ctau_E;
70115a3537eSJed Brown   newtonian_ig_ctx->stabilization   = stab;
7028085925cSJames Wright   newtonian_ig_ctx->is_implicit     = implicit;
7033636f6a4SJames Wright   newtonian_ig_ctx->state_var       = state_var;
704e7754af5SKenneth E. Jansen   newtonian_ig_ctx->idl_enable      = idl_enable;
705e7754af5SKenneth E. Jansen   newtonian_ig_ctx->idl_amplitude   = 1 / (idl_decay_time * second);
706e7754af5SKenneth E. Jansen   newtonian_ig_ctx->idl_start       = idl_start * meter;
707e7754af5SKenneth E. Jansen   newtonian_ig_ctx->idl_length      = idl_length * meter;
708a213b8aaSJames Wright   newtonian_ig_ctx->idl_pressure    = idl_pressure;
7098c85b835SJames Wright   newtonian_ig_ctx->divFdiff_method = user->app_ctx->divFdiffproj_method;
7102b916ea7SJeremy L Thompson   PetscCall(PetscArraycpy(newtonian_ig_ctx->g, g, 3));
7113a8779fbSJames Wright 
712b8fb7609SAdeleke O. Bankole   // -- Setup Context
713b8fb7609SAdeleke O. Bankole   setup_context->reference = reference;
714b8fb7609SAdeleke O. Bankole   setup_context->gas       = *newtonian_ig_ctx;
715b8fb7609SAdeleke O. Bankole   setup_context->lx        = domain_size[0];
716b8fb7609SAdeleke O. Bankole   setup_context->ly        = domain_size[1];
717b8fb7609SAdeleke O. Bankole   setup_context->lz        = domain_size[2];
718b8fb7609SAdeleke O. Bankole   setup_context->time      = 0;
719b8fb7609SAdeleke O. Bankole 
720e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfctx));
721e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(problem->ics.qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context));
722e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfctx, CEED_MEM_HOST, FreeContextPetsc));
723e07531f7SJames Wright   PetscCallCeed(
724e07531f7SJames Wright       ceed, CeedQFunctionContextRegisterDouble(problem->ics.qfctx, "evaluation time", offsetof(struct SetupContext_, time), 1, "Time of evaluation"));
72515a3537eSJed Brown 
726e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &newtonian_ig_qfctx));
727e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetData(newtonian_ig_qfctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*newtonian_ig_ctx), newtonian_ig_ctx));
728e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(newtonian_ig_qfctx, CEED_MEM_HOST, FreeContextPetsc));
729e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "timestep size", offsetof(struct NewtonianIdealGasContext_, dt), 1,
730b4c37c5cSJames Wright                                                          "Size of timestep, delta t"));
731e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "ijacobian time shift",
732b4c37c5cSJames Wright                                                          offsetof(struct NewtonianIdealGasContext_, ijacobian_time_shift), 1,
733b4c37c5cSJames Wright                                                          "Shift for mass matrix in IJacobian"));
734e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRegisterDouble(newtonian_ig_qfctx, "solution time", offsetof(struct NewtonianIdealGasContext_, time), 1,
735b4c37c5cSJames Wright                                                          "Current solution time"));
736a8bca8acSJames Wright 
737e07531f7SJames Wright   problem->apply_vol_rhs.qfctx = newtonian_ig_qfctx;
738e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_vol_ifunction.qfctx));
739e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_vol_ijacobian.qfctx));
740e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_inflow.qfctx));
741e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextReferenceCopy(newtonian_ig_qfctx, &problem->apply_inflow_jacobian.qfctx));
742f0b65372SJed Brown 
7439ed3d70dSJames Wright   if (bc->num_freestream > 0) PetscCall(FreestreamBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
7449ed3d70dSJames Wright   if (bc->num_outflow > 0) PetscCall(OutflowBCSetup(problem, dm, ctx, newtonian_ig_ctx, &reference));
745e07531f7SJames Wright   if (bc->num_slip > 0) PetscCall(SlipBCSetup(problem, dm, ctx, newtonian_ig_qfctx));
7469ed3d70dSJames Wright 
747f0b65372SJed Brown   if (unit_tests) {
748f0b65372SJed Brown     PetscCall(UnitTests_Newtonian(user, newtonian_ig_ctx));
749f0b65372SJed Brown   }
750d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
7513a8779fbSJames Wright }
7523a8779fbSJames Wright 
753991aef52SJames Wright PetscErrorCode PRINT_NEWTONIAN(User user, ProblemData problem, AppCtx app_ctx) {
7542d49c0afSJames Wright   MPI_Comm                 comm = user->comm;
755b4c37c5cSJames Wright   Ceed                     ceed = user->ceed;
75615a3537eSJed Brown   NewtonianIdealGasContext newtonian_ctx;
75715a3537eSJed Brown 
7583a8779fbSJames Wright   PetscFunctionBeginUser;
759e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextGetData(problem->apply_vol_rhs.qfctx, CEED_MEM_HOST, &newtonian_ctx));
7602b916ea7SJeremy L Thompson   PetscCall(PetscPrintf(comm,
76115a3537eSJed Brown                         "  Problem:\n"
76215a3537eSJed Brown                         "    Problem Name                       : %s\n"
76315a3537eSJed Brown                         "    Stabilization                      : %s\n",
7642b916ea7SJeremy L Thompson                         app_ctx->problem_name, StabilizationTypes[newtonian_ctx->stabilization]));
765e07531f7SJames Wright   PetscCallCeed(ceed, CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfctx, &newtonian_ctx));
766d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
7673a8779fbSJames Wright }
768