xref: /honee/src/setuplibceed.c (revision bbc901031064da70cf931c45df3502c602e8f0f6)
1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Setup libCEED for Navier-Stokes example using PETSc
10a515125bSLeila Ghaffari 
11e419654dSJeremy L Thompson #include <ceed.h>
12e419654dSJeremy L Thompson #include <petscdmplex.h>
13e419654dSJeremy L Thompson 
14a515125bSLeila Ghaffari #include "../navierstokes.h"
15a515125bSLeila Ghaffari 
16a78efa86SJames Wright // @brief Create CeedOperator for unstabilized mass KSP for explicit timestepping
17a78efa86SJames Wright static PetscErrorCode CreateKSPMassOperator_Unstabilized(User user, CeedOperator *op_mass) {
18a78efa86SJames Wright   Ceed                ceed = user->ceed;
19a78efa86SJames Wright   CeedInt             num_comp_q, q_data_size;
20a78efa86SJames Wright   CeedQFunction       qf_mass;
21a78efa86SJames Wright   CeedElemRestriction elem_restr_q, elem_restr_qd_i;
22a78efa86SJames Wright   CeedBasis           basis_q;
23a78efa86SJames Wright   CeedVector          q_data;
24a78efa86SJames Wright 
25a78efa86SJames Wright   PetscFunctionBeginUser;
26a78efa86SJames Wright   {  // Get restriction and basis from the RHS function
27a78efa86SJames Wright     CeedOperator     *sub_ops;
28a78efa86SJames Wright     CeedOperatorField field;
29a78efa86SJames Wright     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
30a78efa86SJames Wright 
31a78efa86SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops));
32a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field));
33a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q));
34a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetBasis(field, &basis_q));
35a78efa86SJames Wright 
36a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &field));
37a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_qd_i));
38a78efa86SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetVector(field, &q_data));
39a78efa86SJames Wright   }
40a78efa86SJames Wright 
41a78efa86SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q));
42a78efa86SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd_i, &q_data_size));
43a78efa86SJames Wright 
44a78efa86SJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
45a78efa86SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass));
46a78efa86SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "u", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
47a78efa86SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd_i, CEED_BASIS_NONE, q_data));
48a78efa86SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
49a78efa86SJames Wright 
50a78efa86SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
51a78efa86SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
52a78efa86SJames Wright }
53a78efa86SJames Wright 
54a78efa86SJames Wright // @brief Create KSP to solve the inverse mass operator for explicit time stepping schemes
55991aef52SJames Wright static PetscErrorCode CreateKSPMass(User user, ProblemData problem) {
56a78efa86SJames Wright   Ceed         ceed = user->ceed;
57a78efa86SJames Wright   DM           dm   = user->dm;
58a78efa86SJames Wright   CeedOperator op_mass;
59a78efa86SJames Wright 
60a78efa86SJames Wright   PetscFunctionBeginUser;
61a78efa86SJames Wright   if (problem->create_mass_operator) PetscCall(problem->create_mass_operator(user, &op_mass));
62a78efa86SJames Wright   else PetscCall(CreateKSPMassOperator_Unstabilized(user, &op_mass));
63a78efa86SJames Wright 
64a78efa86SJames Wright   {  // -- Setup KSP for mass operator
65a78efa86SJames Wright     Mat      mat_mass;
66a78efa86SJames Wright     Vec      Zeros_loc;
67a78efa86SJames Wright     MPI_Comm comm = PetscObjectComm((PetscObject)dm);
68a78efa86SJames Wright 
69a78efa86SJames Wright     PetscCall(DMCreateLocalVector(dm, &Zeros_loc));
70a78efa86SJames Wright     PetscCall(VecZeroEntries(Zeros_loc));
71a78efa86SJames Wright     PetscCall(MatCeedCreate(dm, dm, op_mass, NULL, &mat_mass));
72a78efa86SJames Wright     PetscCall(MatCeedSetLocalVectors(mat_mass, Zeros_loc, NULL));
73a78efa86SJames Wright 
74a78efa86SJames Wright     PetscCall(KSPCreate(comm, &user->mass_ksp));
75a78efa86SJames Wright     PetscCall(KSPSetOptionsPrefix(user->mass_ksp, "mass_"));
76a78efa86SJames Wright     {  // lumped by default
77a78efa86SJames Wright       PC pc;
78a78efa86SJames Wright       PetscCall(KSPGetPC(user->mass_ksp, &pc));
79a78efa86SJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
80a78efa86SJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
81a78efa86SJames Wright       PetscCall(KSPSetType(user->mass_ksp, KSPPREONLY));
82a78efa86SJames Wright     }
83a78efa86SJames Wright     PetscCall(KSPSetFromOptions_WithMatCeed(user->mass_ksp, mat_mass));
84a78efa86SJames Wright     PetscCall(KSPSetFromOptions(user->mass_ksp));
85a78efa86SJames Wright     PetscCall(VecDestroy(&Zeros_loc));
86a78efa86SJames Wright     PetscCall(MatDestroy(&mat_mass));
87a78efa86SJames Wright   }
88a78efa86SJames Wright 
89a78efa86SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
90a78efa86SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
91a78efa86SJames Wright }
92a78efa86SJames Wright 
93866f9b4aSJames Wright static PetscErrorCode AddBCSubOperator(Ceed ceed, DM dm, CeedData ceed_data, DMLabel domain_label, PetscInt label_value, CeedInt height,
94866f9b4aSJames Wright                                        CeedInt Q_sur, CeedInt q_data_size_sur, CeedInt jac_data_size_sur, CeedBasis basis_q_sur,
95866f9b4aSJames Wright                                        CeedBasis basis_x_sur, CeedQFunction qf_apply_bc, CeedQFunction qf_apply_bc_jacobian, CeedOperator op_apply,
96866f9b4aSJames Wright                                        CeedOperator op_apply_ijacobian) {
9715c18037SJames Wright   CeedVector          q_data_sur, jac_data_sur          = NULL;
98866f9b4aSJames Wright   CeedOperator        op_apply_bc, op_apply_bc_jacobian = NULL;
9915c18037SJames Wright   CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur, elem_restr_jd_i_sur = NULL;
100866f9b4aSJames Wright   PetscInt            dm_field = 0;
101368c645fSJames Wright 
10206f41313SJames Wright   PetscFunctionBeginUser;
10315c18037SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, dm_field, &elem_restr_q_sur));
10415c18037SJames Wright   PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, label_value, height, &elem_restr_x_sur));
10515c18037SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, q_data_size_sur, &elem_restr_qd_i_sur));
106368c645fSJames Wright   if (jac_data_size_sur > 0) {
107368c645fSJames Wright     // State-dependent data will be passed from residual to Jacobian. This will be collocated.
10815c18037SJames Wright     PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, jac_data_size_sur, &elem_restr_jd_i_sur));
109b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_jd_i_sur, &jac_data_sur, NULL));
110368c645fSJames Wright   }
111368c645fSJames Wright 
112866f9b4aSJames Wright   {  // Create q_data_sur vector
113866f9b4aSJames Wright     CeedOperator op_setup_sur;
114368c645fSJames Wright 
115866f9b4aSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_qd_i_sur, &q_data_sur, NULL));
116866f9b4aSJames Wright 
117b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur));
118866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur, basis_x_sur, CEED_VECTOR_ACTIVE));
119866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_sur, CEED_VECTOR_NONE));
12058e1cbfdSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
121368c645fSJames Wright 
122866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur, CEED_REQUEST_IMMEDIATE));
123866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_setup_sur));
124866f9b4aSJames Wright   }
125866f9b4aSJames Wright 
126*bbc90103SJames Wright   // CEED Operator for Physics
127b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_apply_bc, NULL, NULL, &op_apply_bc));
128866f9b4aSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "q", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
129866f9b4aSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "Grad_q", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
13058e1cbfdSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, q_data_sur));
131866f9b4aSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "x", elem_restr_x_sur, basis_x_sur, ceed_data->x_coord));
132866f9b4aSJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "v", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
133b4c37c5cSJames Wright   if (elem_restr_jd_i_sur)
13458e1cbfdSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_NONE, jac_data_sur));
135368c645fSJames Wright 
1364b96a86bSJames Wright   if (qf_apply_bc_jacobian && elem_restr_jd_i_sur) {
137b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_apply_bc_jacobian, NULL, NULL, &op_apply_bc_jacobian));
138866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "dq", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
139866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "Grad_dq", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
14058e1cbfdSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, q_data_sur));
141866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "x", elem_restr_x_sur, basis_x_sur, ceed_data->x_coord));
14258e1cbfdSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_NONE, jac_data_sur));
143866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "v", elem_restr_q_sur, basis_q_sur, CEED_VECTOR_ACTIVE));
144368c645fSJames Wright   }
145368c645fSJames Wright 
146*bbc90103SJames Wright   // Apply Sub-Operator for Physics
147866f9b4aSJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_apply, op_apply_bc));
148866f9b4aSJames Wright   if (op_apply_bc_jacobian) PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_apply_ijacobian, op_apply_bc_jacobian));
149368c645fSJames Wright 
150b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data_sur));
151b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&jac_data_sur));
152b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q_sur));
153b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_sur));
154b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd_i_sur));
155b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jd_i_sur));
156b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_apply_bc));
157b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_apply_bc_jacobian));
158d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
159368c645fSJames Wright }
160368c645fSJames Wright 
161866f9b4aSJames Wright static PetscErrorCode SetupBCQFunctions(Ceed ceed, PetscInt dim_sur, PetscInt num_comp_x, PetscInt num_comp_q, PetscInt q_data_size_sur,
1622b916ea7SJeremy L Thompson                                         PetscInt jac_data_size_sur, ProblemQFunctionSpec apply_bc, ProblemQFunctionSpec apply_bc_jacobian,
16325988f00SJames Wright                                         CeedQFunction *qf_apply_bc, CeedQFunction *qf_apply_bc_jacobian) {
16425988f00SJames Wright   PetscFunctionBeginUser;
16525988f00SJames Wright   if (apply_bc.qfunction) {
166b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, apply_bc.qfunction, apply_bc.qfunction_loc, qf_apply_bc));
167b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_apply_bc, apply_bc.qfunction_context));
168ebfabadfSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_apply_bc, 0));
169b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "q", num_comp_q, CEED_EVAL_INTERP));
170b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "Grad_q", num_comp_q * dim_sur, CEED_EVAL_GRAD));
171b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
172b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "x", num_comp_x, CEED_EVAL_INTERP));
173b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc, "v", num_comp_q, CEED_EVAL_INTERP));
174b4c37c5cSJames Wright     if (jac_data_size_sur) PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE));
17525988f00SJames Wright   }
17625988f00SJames Wright   if (apply_bc_jacobian.qfunction) {
177b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, apply_bc_jacobian.qfunction, apply_bc_jacobian.qfunction_loc, qf_apply_bc_jacobian));
178b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_apply_bc_jacobian, apply_bc_jacobian.qfunction_context));
179ebfabadfSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_apply_bc_jacobian, 0));
180b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "dq", num_comp_q, CEED_EVAL_INTERP));
181b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "Grad_dq", num_comp_q * dim_sur, CEED_EVAL_GRAD));
182b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
183b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "x", num_comp_x, CEED_EVAL_INTERP));
184b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE));
185b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc_jacobian, "v", num_comp_q, CEED_EVAL_INTERP));
18625988f00SJames Wright   }
187d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
18825988f00SJames Wright }
18925988f00SJames Wright 
190*bbc90103SJames Wright // Utility function to add boundary operators to the composite operator
191866f9b4aSJames Wright static PetscErrorCode AddBCSubOperators(User user, Ceed ceed, DM dm, SimpleBC bc, ProblemData problem, CeedData ceed_data, CeedOperator op_apply,
192866f9b4aSJames Wright                                         CeedOperator op_apply_ijacobian) {
193866f9b4aSJames Wright   CeedInt       height = 1, num_comp_q, num_comp_x;
194866f9b4aSJames Wright   CeedInt       dim_sur, P_sur = user->app_ctx->degree + 1, Q_sur = P_sur + user->app_ctx->q_extra;
195866f9b4aSJames Wright   const CeedInt q_data_size_sur = problem->q_data_size_sur, jac_data_size_sur = user->phys->implicit ? problem->jac_data_size_sur : 0;
196866f9b4aSJames Wright   PetscInt      dim;
197866f9b4aSJames Wright   DMLabel       face_sets_label;
198866f9b4aSJames Wright   CeedBasis     basis_q_sur, basis_x_sur;
199866f9b4aSJames Wright 
200866f9b4aSJames Wright   PetscFunctionBeginUser;
201866f9b4aSJames Wright   PetscCall(DMGetDimension(dm, &dim));
202866f9b4aSJames Wright   dim_sur = dim - height;
203866f9b4aSJames Wright   {  // Get number of components and coordinate dimension from op_apply
204866f9b4aSJames Wright     CeedOperator       *sub_ops;
205866f9b4aSJames Wright     CeedOperatorField   field;
206866f9b4aSJames Wright     PetscInt            sub_op_index = 0;  // will be 0 for the volume op
207866f9b4aSJames Wright     CeedElemRestriction elem_restr_q, elem_restr_x;
208866f9b4aSJames Wright 
209866f9b4aSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(op_apply, &sub_ops));
210866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field));
211866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q));
212866f9b4aSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q));
213866f9b4aSJames Wright 
214866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "x", &field));
215866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_x));
216866f9b4aSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_x, &num_comp_x));
217866f9b4aSJames Wright   }
218866f9b4aSJames Wright 
219866f9b4aSJames Wright   {  // Get bases
220866f9b4aSJames Wright     DM dm_coord;
221866f9b4aSJames Wright 
222866f9b4aSJames Wright     PetscCall(DMGetCoordinateDM(dm, &dm_coord));
223866f9b4aSJames Wright     DMLabel  label       = NULL;
224866f9b4aSJames Wright     PetscInt label_value = 0;
225*bbc90103SJames Wright     PetscInt field       = 0;
226866f9b4aSJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm, label, label_value, height, field, &basis_q_sur));
227866f9b4aSJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm_coord, label, label_value, height, field, &basis_x_sur));
228866f9b4aSJames Wright   }
229866f9b4aSJames Wright 
230866f9b4aSJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &face_sets_label));
231866f9b4aSJames Wright 
232866f9b4aSJames Wright   {  // -- Create QFunction for quadrature data
233866f9b4aSJames Wright     PetscCallCeed(ceed,
234866f9b4aSJames Wright                   CeedQFunctionCreateInterior(ceed, 1, problem->setup_sur.qfunction, problem->setup_sur.qfunction_loc, &ceed_data->qf_setup_sur));
235866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_setup_sur, problem->setup_sur.qfunction_context));
236866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_setup_sur, 0));
237866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_sur, "dx", num_comp_x * dim_sur, CEED_EVAL_GRAD));
238866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_sur, "weight", 1, CEED_EVAL_WEIGHT));
239866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_setup_sur, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
240866f9b4aSJames Wright   }
241866f9b4aSJames Wright 
242866f9b4aSJames Wright   {  // --- Create Sub-Operator for inflow boundaries
243866f9b4aSJames Wright     CeedQFunction qf_apply_inflow = NULL, qf_apply_inflow_jacobian = NULL;
244866f9b4aSJames Wright 
245866f9b4aSJames Wright     PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_inflow,
246866f9b4aSJames Wright                                 problem->apply_inflow_jacobian, &qf_apply_inflow, &qf_apply_inflow_jacobian));
247866f9b4aSJames Wright     for (CeedInt i = 0; i < bc->num_inflow; i++) {
248866f9b4aSJames Wright       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, face_sets_label, bc->inflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur, basis_q_sur,
249866f9b4aSJames Wright                                  basis_x_sur, qf_apply_inflow, qf_apply_inflow_jacobian, op_apply, op_apply_ijacobian));
250866f9b4aSJames Wright     }
251866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_inflow));
252866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_inflow_jacobian));
253866f9b4aSJames Wright   }
254866f9b4aSJames Wright 
255866f9b4aSJames Wright   {  // --- Create Sub-Operator for outflow boundaries
256866f9b4aSJames Wright     CeedQFunction qf_apply_outflow = NULL, qf_apply_outflow_jacobian = NULL;
257866f9b4aSJames Wright 
258866f9b4aSJames Wright     PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_outflow,
259866f9b4aSJames Wright                                 problem->apply_outflow_jacobian, &qf_apply_outflow, &qf_apply_outflow_jacobian));
260866f9b4aSJames Wright     for (CeedInt i = 0; i < bc->num_outflow; i++) {
261866f9b4aSJames Wright       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, face_sets_label, bc->outflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
262866f9b4aSJames Wright                                  basis_q_sur, basis_x_sur, qf_apply_outflow, qf_apply_outflow_jacobian, op_apply, op_apply_ijacobian));
263866f9b4aSJames Wright     }
264866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_outflow));
265866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_outflow_jacobian));
266866f9b4aSJames Wright   }
267866f9b4aSJames Wright 
268866f9b4aSJames Wright   {  // --- Create Sub-Operator for freestream boundaries
269866f9b4aSJames Wright     CeedQFunction qf_apply_freestream = NULL, qf_apply_freestream_jacobian = NULL;
270866f9b4aSJames Wright 
271866f9b4aSJames Wright     PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_freestream,
272866f9b4aSJames Wright                                 problem->apply_freestream_jacobian, &qf_apply_freestream, &qf_apply_freestream_jacobian));
273866f9b4aSJames Wright     for (CeedInt i = 0; i < bc->num_freestream; i++) {
274866f9b4aSJames Wright       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, face_sets_label, bc->freestreams[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
275866f9b4aSJames Wright                                  basis_q_sur, basis_x_sur, qf_apply_freestream, qf_apply_freestream_jacobian, op_apply, op_apply_ijacobian));
276866f9b4aSJames Wright     }
277866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_freestream));
278866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_freestream_jacobian));
279866f9b4aSJames Wright   }
280866f9b4aSJames Wright 
281866f9b4aSJames Wright   {  // --- Create Sub-Operator for slip boundaries
282866f9b4aSJames Wright     CeedQFunction qf_apply_slip = NULL, qf_apply_slip_jacobian = NULL;
283866f9b4aSJames Wright 
284866f9b4aSJames Wright     PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_slip,
285866f9b4aSJames Wright                                 problem->apply_slip_jacobian, &qf_apply_slip, &qf_apply_slip_jacobian));
286866f9b4aSJames Wright     for (CeedInt i = 0; i < bc->num_slip; i++) {
287866f9b4aSJames Wright       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, face_sets_label, bc->slips[i], height, Q_sur, q_data_size_sur, jac_data_size_sur, basis_q_sur,
288866f9b4aSJames Wright                                  basis_x_sur, qf_apply_slip, qf_apply_slip_jacobian, op_apply, op_apply_ijacobian));
289866f9b4aSJames Wright     }
290866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_slip));
291866f9b4aSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_apply_slip_jacobian));
292866f9b4aSJames Wright   }
293866f9b4aSJames Wright 
294866f9b4aSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_q_sur));
295866f9b4aSJames Wright   PetscCallCeed(ceed, CeedBasisDestroy(&basis_x_sur));
296866f9b4aSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
297866f9b4aSJames Wright }
298866f9b4aSJames Wright 
299991aef52SJames Wright PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, AppCtx app_ctx, ProblemData problem, SimpleBC bc) {
300a515125bSLeila Ghaffari   const PetscInt      num_comp_q = 5;
30194a7b3d2SKenneth E. Jansen   const CeedInt       dim = problem->dim, num_comp_x = problem->dim, q_data_size_vol = problem->q_data_size_vol;
30294a7b3d2SKenneth E. Jansen   CeedInt             jac_data_size_vol = num_comp_q + 6 + 3;
303*bbc90103SJames Wright   CeedElemRestriction elem_restr_jd_i;
304*bbc90103SJames Wright   CeedVector          jac_data;
305*bbc90103SJames Wright   CeedOperator        op_ifunction_vol = NULL, op_rhs_vol = NULL, op_ijacobian_vol = NULL;
306*bbc90103SJames Wright 
307*bbc90103SJames Wright   PetscFunctionBeginUser;
30894a7b3d2SKenneth E. Jansen 
3097d8a615bSJames Wright   if (problem->apply_vol_ifunction.qfunction && problem->uses_newtonian) {
31094a7b3d2SKenneth E. Jansen     NewtonianIdealGasContext gas;
31194a7b3d2SKenneth E. Jansen     PetscCallCeed(ceed, CeedQFunctionContextGetDataRead(problem->apply_vol_ifunction.qfunction_context, CEED_MEM_HOST, &gas));
31294a7b3d2SKenneth E. Jansen     jac_data_size_vol += (gas->idl_enable ? 1 : 0);
31394a7b3d2SKenneth E. Jansen     PetscCallCeed(ceed, CeedQFunctionContextRestoreDataRead(problem->apply_vol_ifunction.qfunction_context, &gas));
31494a7b3d2SKenneth E. Jansen   }
31594a7b3d2SKenneth E. Jansen 
316*bbc90103SJames Wright   {  // Create bases and element restrictions
31715c18037SJames Wright     DMLabel  domain_label = NULL;
31815c18037SJames Wright     PetscInt label_value = 0, height = 0, dm_field = 0;
31967263decSKenneth E. Jansen     DM       dm_coord;
32067263decSKenneth E. Jansen 
321*bbc90103SJames Wright     PetscCall(DMGetCoordinateDM(dm, &dm_coord));
32215c18037SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm, domain_label, label_value, height, dm_field, &ceed_data->basis_q));
32315c18037SJames Wright     PetscCall(CreateBasisFromPlex(ceed, dm_coord, domain_label, label_value, height, dm_field, &ceed_data->basis_x));
324a515125bSLeila Ghaffari 
32515c18037SJames Wright     PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, 0, &ceed_data->elem_restr_q));
32615c18037SJames Wright     PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, label_value, height, &ceed_data->elem_restr_x));
32715c18037SJames Wright     PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, q_data_size_vol, &ceed_data->elem_restr_qd_i));
32815c18037SJames Wright     PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, jac_data_size_vol, &elem_restr_jd_i));
329ce8bebb6SJames Wright 
330b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_ceed, NULL));
331b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_dot_ceed, NULL));
332b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->g_ceed, NULL));
333b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_x, &ceed_data->x_coord, NULL));
334ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_qd_i, &ceed_data->q_data, NULL));
335ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_jd_i, &jac_data, NULL));
336*bbc90103SJames Wright   }
337a515125bSLeila Ghaffari 
338ce8bebb6SJames Wright   {  // -- Copy PETSc coordinate vector into CEED vector
339a515125bSLeila Ghaffari     Vec X_loc;
340cac8aa24SJed Brown     DM  cdm;
341ce8bebb6SJames Wright 
3422b916ea7SJeremy L Thompson     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
3432b916ea7SJeremy L Thompson     if (cdm) {
3442b916ea7SJeremy L Thompson       PetscCall(DMGetCellCoordinatesLocal(dm, &X_loc));
3452b916ea7SJeremy L Thompson     } else {
3462b916ea7SJeremy L Thompson       PetscCall(DMGetCoordinatesLocal(dm, &X_loc));
347cac8aa24SJed Brown     }
3482b916ea7SJeremy L Thompson     PetscCall(VecScale(X_loc, problem->dm_scale));
349a7dac1d5SJames Wright     PetscCall(VecCopyPetscToCeed(X_loc, ceed_data->x_coord));
350ce8bebb6SJames Wright   }
351a515125bSLeila Ghaffari 
352ce8bebb6SJames Wright   {  // -- Create quadrature data
353ce8bebb6SJames Wright     CeedQFunction qf_setup_vol;
354ce8bebb6SJames Wright     CeedOperator  op_setup_vol;
3550b0430b7SJames Wright 
356ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->setup_vol.qfunction, problem->setup_vol.qfunction_loc, &qf_setup_vol));
357ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_setup_vol, problem->setup_vol.qfunction_context));
358ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_setup_vol, 0));
359ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup_vol, "dx", num_comp_x * dim, CEED_EVAL_GRAD));
360ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_setup_vol, "weight", 1, CEED_EVAL_WEIGHT));
361ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_setup_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
362a515125bSLeila Ghaffari 
363ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_setup_vol, NULL, NULL, &op_setup_vol));
364ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_vol, "dx", ceed_data->elem_restr_x, ceed_data->basis_x, CEED_VECTOR_ACTIVE));
365ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_vol, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x, CEED_VECTOR_NONE));
366ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_setup_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
367ce8bebb6SJames Wright 
368ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorApply(op_setup_vol, ceed_data->x_coord, ceed_data->q_data, CEED_REQUEST_IMMEDIATE));
369ce8bebb6SJames Wright 
370ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_setup_vol));
371ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_setup_vol));
372ce8bebb6SJames Wright   }
373ce8bebb6SJames Wright 
374ce8bebb6SJames Wright   {  // -- Create QFunction for ICs
375866f9b4aSJames Wright     CeedBasis     basis_xc;
376ce8bebb6SJames Wright     CeedQFunction qf_ics;
3778f18bb8bSJames Wright     CeedOperator  op_ics;
378ce8bebb6SJames Wright 
379866f9b4aSJames Wright     PetscCallCeed(ceed, CeedBasisCreateProjection(ceed_data->basis_x, ceed_data->basis_q, &basis_xc));
380ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->ics.qfunction, problem->ics.qfunction_loc, &qf_ics));
381ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_ics, problem->ics.qfunction_context));
382ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_ics, 0));
383ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ics, "x", num_comp_x, CEED_EVAL_INTERP));
384ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ics, "dx", num_comp_x * dim, CEED_EVAL_GRAD));
385ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ics, "q0", num_comp_q, CEED_EVAL_NONE));
386ce8bebb6SJames Wright 
387ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics));
388866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "x", ceed_data->elem_restr_x, basis_xc, CEED_VECTOR_ACTIVE));
389866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "dx", ceed_data->elem_restr_x, basis_xc, CEED_VECTOR_ACTIVE));
39058e1cbfdSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "q0", ceed_data->elem_restr_q, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
391b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_ics, "evaluation time", &user->phys->ics_time_label));
3928f18bb8bSJames Wright     PetscCall(OperatorApplyContextCreate(NULL, dm, user->ceed, op_ics, ceed_data->x_coord, NULL, NULL, user->Q_loc, &ceed_data->op_ics_ctx));
393a515125bSLeila Ghaffari 
394866f9b4aSJames Wright     PetscCallCeed(ceed, CeedBasisDestroy(&basis_xc));
395ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_ics));
396ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_ics));
397ce8bebb6SJames Wright   }
398ce8bebb6SJames Wright 
399ce8bebb6SJames Wright   if (problem->apply_vol_rhs.qfunction) {
400ce8bebb6SJames Wright     CeedQFunction qf_rhs_vol;
401ce8bebb6SJames Wright 
402ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_rhs.qfunction, problem->apply_vol_rhs.qfunction_loc, &qf_rhs_vol));
403ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_rhs_vol, problem->apply_vol_rhs.qfunction_context));
404ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_rhs_vol, 0));
405ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_vol, "q", num_comp_q, CEED_EVAL_INTERP));
406ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
407ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
408ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_rhs_vol, "x", num_comp_x, CEED_EVAL_INTERP));
409ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_vol, "v", num_comp_q, CEED_EVAL_INTERP));
410ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_rhs_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
411ce8bebb6SJames Wright 
412*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_rhs_vol, NULL, NULL, &op_rhs_vol));
413*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
414*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
415*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
416*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord));
417*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
418*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_rhs_vol, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
419ce8bebb6SJames Wright 
420ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_rhs_vol));
421a515125bSLeila Ghaffari   }
422a515125bSLeila Ghaffari 
423ce8bebb6SJames Wright   if (problem->apply_vol_ifunction.qfunction) {
424ce8bebb6SJames Wright     CeedQFunction qf_ifunction_vol;
425ce8bebb6SJames Wright 
426ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ifunction.qfunction, problem->apply_vol_ifunction.qfunction_loc,
427ce8bebb6SJames Wright                                                     &qf_ifunction_vol));
428ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_ifunction_vol, problem->apply_vol_ifunction.qfunction_context));
429ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_ifunction_vol, 0));
430ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ifunction_vol, "q", num_comp_q, CEED_EVAL_INTERP));
431ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ifunction_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
432ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ifunction_vol, "q dot", num_comp_q, CEED_EVAL_INTERP));
433ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ifunction_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
434ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ifunction_vol, "x", num_comp_x, CEED_EVAL_INTERP));
435ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ifunction_vol, "v", num_comp_q, CEED_EVAL_INTERP));
436ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ifunction_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
437ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ifunction_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE));
438ce8bebb6SJames Wright 
439*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ifunction_vol, NULL, NULL, &op_ifunction_vol));
440*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
441*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
442*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "q dot", ceed_data->elem_restr_q, ceed_data->basis_q, user->q_dot_ceed));
443*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
444*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord));
445*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
446*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
447*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ifunction_vol, "jac_data", elem_restr_jd_i, CEED_BASIS_NONE, jac_data));
448752f40e3SJed Brown 
449ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_ifunction_vol));
450a515125bSLeila Ghaffari   }
451a515125bSLeila Ghaffari 
452ce8bebb6SJames Wright   if (problem->apply_vol_ijacobian.qfunction) {
453ce8bebb6SJames Wright     CeedQFunction qf_ijacobian_vol;
454ce8bebb6SJames Wright 
455ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ijacobian.qfunction, problem->apply_vol_ijacobian.qfunction_loc,
456ce8bebb6SJames Wright                                                     &qf_ijacobian_vol));
457ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_ijacobian_vol, problem->apply_vol_ijacobian.qfunction_context));
458ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_ijacobian_vol, 0));
459ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "dq", num_comp_q, CEED_EVAL_INTERP));
460ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "Grad_dq", num_comp_q * dim, CEED_EVAL_GRAD));
461ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
462ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE));
463ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ijacobian_vol, "v", num_comp_q, CEED_EVAL_INTERP));
464ce8bebb6SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ijacobian_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
465ce8bebb6SJames Wright 
466*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ijacobian_vol, NULL, NULL, &op_ijacobian_vol));
467*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
468*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "Grad_dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
469*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
470*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "jac_data", elem_restr_jd_i, CEED_BASIS_NONE, jac_data));
471*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
472*bbc90103SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_ijacobian_vol, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
473ce8bebb6SJames Wright 
474b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_ijacobian_vol));
475f0b65372SJed Brown   }
476f0b65372SJed Brown 
477a515125bSLeila Ghaffari   // -- Create and apply CEED Composite Operator for the entire domain
478a515125bSLeila Ghaffari   if (!user->phys->implicit) {  // RHS
479da5fe0e4SJames Wright     CeedOperator op_rhs;
480866f9b4aSJames Wright 
481866f9b4aSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &op_rhs));
482*bbc90103SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_rhs, op_rhs_vol));
483866f9b4aSJames Wright     PetscCall(AddBCSubOperators(user, ceed, dm, bc, problem, ceed_data, op_rhs, NULL));
484866f9b4aSJames Wright 
485da5fe0e4SJames Wright     PetscCall(OperatorApplyContextCreate(dm, dm, ceed, op_rhs, user->q_ceed, user->g_ceed, user->Q_loc, NULL, &user->op_rhs_ctx));
486866f9b4aSJames Wright 
487866f9b4aSJames Wright     // ----- Get Context Labels for Operator
488866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_rhs, "solution time", &user->phys->solution_time_label));
489866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_rhs, "timestep size", &user->phys->timestep_size_label));
490866f9b4aSJames Wright 
491b4c37c5cSJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs));
492a78efa86SJames Wright     PetscCall(CreateKSPMass(user, problem));
493c2376cc9SJames Wright     PetscCheck(app_ctx->sgs_model_type == SGS_MODEL_NONE, user->comm, PETSC_ERR_SUP, "SGS modeling not implemented for explicit timestepping");
494a515125bSLeila Ghaffari   } else {  // IFunction
495ebfabadfSJames Wright     CeedOperator op_ijacobian = NULL;
496ebfabadfSJames Wright 
497866f9b4aSJames Wright     // Create Composite Operaters
498866f9b4aSJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &user->op_ifunction));
499*bbc90103SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorAddSub(user->op_ifunction, op_ifunction_vol));
500866f9b4aSJames Wright     if (op_ijacobian_vol) {
501866f9b4aSJames Wright       PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, &op_ijacobian));
502866f9b4aSJames Wright       PetscCallCeed(ceed, CeedCompositeOperatorAddSub(op_ijacobian, op_ijacobian_vol));
503866f9b4aSJames Wright     }
504866f9b4aSJames Wright     PetscCall(AddBCSubOperators(user, ceed, dm, bc, problem, ceed_data, user->op_ifunction, op_ijacobian));
505866f9b4aSJames Wright 
506866f9b4aSJames Wright     // ----- Get Context Labels for Operator
507866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(user->op_ifunction, "solution time", &user->phys->solution_time_label));
508866f9b4aSJames Wright     PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(user->op_ifunction, "timestep size", &user->phys->timestep_size_label));
509866f9b4aSJames Wright 
510ebfabadfSJames Wright     if (op_ijacobian) {
511ebfabadfSJames Wright       PetscCall(MatCeedCreate(user->dm, user->dm, op_ijacobian, NULL, &user->mat_ijacobian));
512ebfabadfSJames Wright       PetscCall(MatCeedSetLocalVectors(user->mat_ijacobian, user->Q_dot_loc, NULL));
513ebfabadfSJames Wright       PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_ijacobian, "ijacobian time shift", &user->phys->ijacobian_time_shift_label));
514ebfabadfSJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_ijacobian));
515f0b65372SJed Brown     }
516ad494f68SJames Wright     if (app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) PetscCall(SgsDDSetup(ceed, user, ceed_data, problem));
5176d0190e2SJames Wright   }
51891933550SJames Wright 
519c2376cc9SJames Wright   if (problem->use_strong_bc_ceed) PetscCall(SetupStrongBC_Ceed(ceed, ceed_data, dm, user, problem, bc));
52091933550SJames Wright   if (app_ctx->turb_spanstats_enable) PetscCall(TurbulenceStatisticsSetup(ceed, user, ceed_data, problem));
521aa0b7f76SJames Wright   if (app_ctx->diff_filter_monitor && !user->diff_filter) PetscCall(DifferentialFilterSetup(ceed, user, ceed_data, problem));
5221c17f66aSJames Wright   if (app_ctx->sgs_train_enable) PetscCall(SGS_DD_TrainingSetup(ceed, user, ceed_data, problem));
52391933550SJames Wright 
524*bbc90103SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&jac_data));
525b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jd_i));
526b4c37c5cSJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_ijacobian_vol));
527*bbc90103SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_ifunction_vol));
528*bbc90103SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs_vol));
529d949ddfcSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
530a515125bSLeila Ghaffari }
531