xref: /libCEED/examples/fluids/src/setuplibceed.c (revision b4e9a8f894a0481205b699a80d40c97cb033c4b2)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Setup libCEED for Navier-Stokes example using PETSc
1077841947SLeila Ghaffari 
1149aac155SJeremy L Thompson #include <ceed.h>
1249aac155SJeremy L Thompson #include <petscdmplex.h>
1349aac155SJeremy L Thompson 
1477841947SLeila Ghaffari #include "../navierstokes.h"
1577841947SLeila Ghaffari 
16*b4e9a8f8SJames Wright // @brief Create CeedOperator for unstabilized mass KSP for explicit timestepping
17*b4e9a8f8SJames Wright static PetscErrorCode CreateKSPMassOperator_Unstabilized(User user, CeedOperator *op_mass) {
18*b4e9a8f8SJames Wright   Ceed                ceed = user->ceed;
19*b4e9a8f8SJames Wright   CeedInt             num_comp_q, q_data_size;
20*b4e9a8f8SJames Wright   CeedQFunction       qf_mass;
21*b4e9a8f8SJames Wright   CeedElemRestriction elem_restr_q, elem_restr_qd_i;
22*b4e9a8f8SJames Wright   CeedBasis           basis_q;
23*b4e9a8f8SJames Wright   CeedVector          q_data;
24*b4e9a8f8SJames Wright 
25*b4e9a8f8SJames Wright   PetscFunctionBeginUser;
26*b4e9a8f8SJames Wright   {  // Get restriction and basis from the RHS function
27*b4e9a8f8SJames Wright     CeedOperator     *sub_ops;
28*b4e9a8f8SJames Wright     CeedOperatorField field;
29*b4e9a8f8SJames Wright     PetscInt          sub_op_index = 0;  // will be 0 for the volume op
30*b4e9a8f8SJames Wright 
31*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedCompositeOperatorGetSubList(user->op_rhs_ctx->op, &sub_ops));
32*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "q", &field));
33*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_q));
34*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetBasis(field, &basis_q));
35*b4e9a8f8SJames Wright 
36*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorGetFieldByName(sub_ops[sub_op_index], "qdata", &field));
37*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetElemRestriction(field, &elem_restr_qd_i));
38*b4e9a8f8SJames Wright     PetscCallCeed(ceed, CeedOperatorFieldGetVector(field, &q_data));
39*b4e9a8f8SJames Wright   }
40*b4e9a8f8SJames Wright 
41*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_q, &num_comp_q));
42*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumComponents(elem_restr_qd_i, &q_data_size));
43*b4e9a8f8SJames Wright 
44*b4e9a8f8SJames Wright   PetscCall(CreateMassQFunction(ceed, num_comp_q, q_data_size, &qf_mass));
45*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_mass, NULL, NULL, op_mass));
46*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "u", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
47*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "qdata", elem_restr_qd_i, CEED_BASIS_NONE, q_data));
48*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(*op_mass, "v", elem_restr_q, basis_q, CEED_VECTOR_ACTIVE));
49*b4e9a8f8SJames Wright 
50*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_mass));
51*b4e9a8f8SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
52*b4e9a8f8SJames Wright }
53*b4e9a8f8SJames Wright 
54*b4e9a8f8SJames Wright // @brief Create KSP to solve the inverse mass operator for explicit time stepping schemes
55*b4e9a8f8SJames Wright static PetscErrorCode CreateKSPMass(User user, ProblemData *problem) {
56*b4e9a8f8SJames Wright   Ceed         ceed = user->ceed;
57*b4e9a8f8SJames Wright   DM           dm   = user->dm;
58*b4e9a8f8SJames Wright   CeedOperator op_mass;
59*b4e9a8f8SJames Wright 
60*b4e9a8f8SJames Wright   PetscFunctionBeginUser;
61*b4e9a8f8SJames Wright   if (problem->create_mass_operator) PetscCall(problem->create_mass_operator(user, &op_mass));
62*b4e9a8f8SJames Wright   else PetscCall(CreateKSPMassOperator_Unstabilized(user, &op_mass));
63*b4e9a8f8SJames Wright 
64*b4e9a8f8SJames Wright   {  // -- Setup KSP for mass operator
65*b4e9a8f8SJames Wright     Mat      mat_mass;
66*b4e9a8f8SJames Wright     Vec      Zeros_loc;
67*b4e9a8f8SJames Wright     MPI_Comm comm = PetscObjectComm((PetscObject)dm);
68*b4e9a8f8SJames Wright 
69*b4e9a8f8SJames Wright     PetscCall(DMCreateLocalVector(dm, &Zeros_loc));
70*b4e9a8f8SJames Wright     PetscCall(VecZeroEntries(Zeros_loc));
71*b4e9a8f8SJames Wright     PetscCall(MatCeedCreate(dm, dm, op_mass, NULL, &mat_mass));
72*b4e9a8f8SJames Wright     PetscCall(MatCeedSetLocalVectors(mat_mass, Zeros_loc, NULL));
73*b4e9a8f8SJames Wright 
74*b4e9a8f8SJames Wright     PetscCall(KSPCreate(comm, &user->mass_ksp));
75*b4e9a8f8SJames Wright     PetscCall(KSPSetOptionsPrefix(user->mass_ksp, "mass_"));
76*b4e9a8f8SJames Wright     {  // lumped by default
77*b4e9a8f8SJames Wright       PC pc;
78*b4e9a8f8SJames Wright       PetscCall(KSPGetPC(user->mass_ksp, &pc));
79*b4e9a8f8SJames Wright       PetscCall(PCSetType(pc, PCJACOBI));
80*b4e9a8f8SJames Wright       PetscCall(PCJacobiSetType(pc, PC_JACOBI_ROWSUM));
81*b4e9a8f8SJames Wright       PetscCall(KSPSetType(user->mass_ksp, KSPPREONLY));
82*b4e9a8f8SJames Wright     }
83*b4e9a8f8SJames Wright     PetscCall(KSPSetFromOptions_WithMatCeed(user->mass_ksp, mat_mass));
84*b4e9a8f8SJames Wright     PetscCall(KSPSetFromOptions(user->mass_ksp));
85*b4e9a8f8SJames Wright     PetscCall(VecDestroy(&Zeros_loc));
86*b4e9a8f8SJames Wright     PetscCall(MatDestroy(&mat_mass));
87*b4e9a8f8SJames Wright   }
88*b4e9a8f8SJames Wright 
89*b4e9a8f8SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_mass));
90*b4e9a8f8SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
91*b4e9a8f8SJames Wright }
92*b4e9a8f8SJames Wright 
93431cd09aSJames Wright PetscErrorCode AddBCSubOperator(Ceed ceed, DM dm, CeedData ceed_data, DMLabel domain_label, PetscInt label_value, CeedInt height, CeedInt Q_sur,
942b730f8bSJeremy L Thompson                                 CeedInt q_data_size_sur, CeedInt jac_data_size_sur, CeedQFunction qf_apply_bc, CeedQFunction qf_apply_bc_jacobian,
959eb9c072SJames Wright                                 CeedOperator *op_apply, CeedOperator *op_apply_ijacobian) {
96bb85d312SJames Wright   CeedVector          q_data_sur, jac_data_sur = NULL;
972b730f8bSJeremy L Thompson   CeedOperator        op_setup_sur, op_apply_bc, op_apply_bc_jacobian = NULL;
98bb85d312SJames Wright   CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur, elem_restr_jd_i_sur = NULL;
99bb85d312SJames Wright   CeedInt             num_qpts_sur, dm_field = 0;
1009eb9c072SJames Wright 
101f17d818dSJames Wright   PetscFunctionBeginUser;
1029eb9c072SJames Wright   // --- Get number of quadrature points for the boundaries
103a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisGetNumQuadraturePoints(ceed_data->basis_q_sur, &num_qpts_sur));
1049eb9c072SJames Wright 
1059eb9c072SJames Wright   // ---- CEED Restriction
106bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, dm_field, &elem_restr_q_sur));
107bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, label_value, height, &elem_restr_x_sur));
108bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, q_data_size_sur, &elem_restr_qd_i_sur));
1099eb9c072SJames Wright   if (jac_data_size_sur > 0) {
1109eb9c072SJames Wright     // State-dependent data will be passed from residual to Jacobian. This will be collocated.
111bb85d312SJames Wright     PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, jac_data_size_sur, &elem_restr_jd_i_sur));
112a424bcd0SJames Wright     PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_jd_i_sur, &jac_data_sur, NULL));
1139eb9c072SJames Wright   }
1149eb9c072SJames Wright 
1159eb9c072SJames Wright   // ---- CEED Vector
116457e73b2SJames Wright   CeedInt loc_num_elem_sur;
117a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur));
118a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorCreate(ceed, q_data_size_sur * loc_num_elem_sur * num_qpts_sur, &q_data_sur));
1199eb9c072SJames Wright 
1209eb9c072SJames Wright   // ---- CEED Operator
1219eb9c072SJames Wright   // ----- CEED Operator for Setup (geometric factors)
122a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur));
123a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur, ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE));
124a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x_sur, CEED_VECTOR_NONE));
125356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
1269eb9c072SJames Wright 
1279eb9c072SJames Wright   // ----- CEED Operator for Physics
128a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_apply_bc, NULL, NULL, &op_apply_bc));
129a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "q", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
130a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "Grad_q", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
131356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, q_data_sur));
132a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "x", elem_restr_x_sur, ceed_data->basis_x_sur, ceed_data->x_coord));
133a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "v", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
134a424bcd0SJames Wright   if (elem_restr_jd_i_sur)
135356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_NONE, jac_data_sur));
1369eb9c072SJames Wright 
137f21e6b1cSJames Wright   if (qf_apply_bc_jacobian && elem_restr_jd_i_sur) {
138a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_apply_bc_jacobian, NULL, NULL, &op_apply_bc_jacobian));
139a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "dq", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
140a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "Grad_dq", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
141356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_NONE, q_data_sur));
142a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "x", elem_restr_x_sur, ceed_data->basis_x_sur, ceed_data->x_coord));
143356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_NONE, jac_data_sur));
144a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op_apply_bc_jacobian, "v", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE));
1459eb9c072SJames Wright   }
1469eb9c072SJames Wright 
1479eb9c072SJames Wright   // ----- Apply CEED operator for Setup
148a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur, CEED_REQUEST_IMMEDIATE));
1499eb9c072SJames Wright 
1509eb9c072SJames Wright   // ----- Apply Sub-Operator for Physics
151a424bcd0SJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_apply, op_apply_bc));
152a424bcd0SJames Wright   if (op_apply_bc_jacobian) PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_apply_ijacobian, op_apply_bc_jacobian));
1539eb9c072SJames Wright 
1549eb9c072SJames Wright   // ----- Cleanup
155a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&q_data_sur));
156a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&jac_data_sur));
157a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_q_sur));
158a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_x_sur));
159a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_qd_i_sur));
160a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jd_i_sur));
161a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_setup_sur));
162a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_apply_bc));
163a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_apply_bc_jacobian));
164ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
1659eb9c072SJames Wright }
1669eb9c072SJames Wright 
16777841947SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
1682b730f8bSJeremy L Thompson PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, CeedData ceed_data, Physics phys, CeedOperator op_apply_vol,
1692b730f8bSJeremy L Thompson                                        CeedOperator op_apply_ijacobian_vol, CeedInt height, CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur,
1702b730f8bSJeremy L Thompson                                        CeedInt jac_data_size_sur, CeedOperator *op_apply, CeedOperator *op_apply_ijacobian) {
17177841947SLeila Ghaffari   DMLabel domain_label;
17277841947SLeila Ghaffari 
173f17d818dSJames Wright   PetscFunctionBeginUser;
17477841947SLeila Ghaffari   // Create Composite Operaters
175a424bcd0SJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, op_apply));
176a424bcd0SJames Wright   if (op_apply_ijacobian) PetscCallCeed(ceed, CeedCompositeOperatorCreate(ceed, op_apply_ijacobian));
17777841947SLeila Ghaffari 
17877841947SLeila Ghaffari   // --Apply Sub-Operator for the volume
179a424bcd0SJames Wright   PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_apply, op_apply_vol));
180a424bcd0SJames Wright   if (op_apply_ijacobian) PetscCallCeed(ceed, CeedCompositeOperatorAddSub(*op_apply_ijacobian, op_apply_ijacobian_vol));
18177841947SLeila Ghaffari 
18277841947SLeila Ghaffari   // -- Create Sub-Operator for in/outflow BCs
1832b730f8bSJeremy L Thompson   PetscCall(DMGetLabel(dm, "Face Sets", &domain_label));
18477841947SLeila Ghaffari 
1852fe7aee7SLeila Ghaffari   // --- Create Sub-Operator for inflow boundaries
1862fe7aee7SLeila Ghaffari   for (CeedInt i = 0; i < bc->num_inflow; i++) {
1872b730f8bSJeremy L Thompson     PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->inflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
1882b730f8bSJeremy L Thompson                                ceed_data->qf_apply_inflow, ceed_data->qf_apply_inflow_jacobian, op_apply, op_apply_ijacobian));
18977841947SLeila Ghaffari   }
1902fe7aee7SLeila Ghaffari   // --- Create Sub-Operator for outflow boundaries
1912fe7aee7SLeila Ghaffari   for (CeedInt i = 0; i < bc->num_outflow; i++) {
1922b730f8bSJeremy L Thompson     PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->outflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
1932b730f8bSJeremy L Thompson                                ceed_data->qf_apply_outflow, ceed_data->qf_apply_outflow_jacobian, op_apply, op_apply_ijacobian));
19439c69132SJed Brown   }
195f4ca79c2SJames Wright   // --- Create Sub-Operator for freestream boundaries
196f4ca79c2SJames Wright   for (CeedInt i = 0; i < bc->num_freestream; i++) {
1972b730f8bSJeremy L Thompson     PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->freestreams[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
1982b730f8bSJeremy L Thompson                                ceed_data->qf_apply_freestream, ceed_data->qf_apply_freestream_jacobian, op_apply, op_apply_ijacobian));
1992fe7aee7SLeila Ghaffari   }
2009f844368SJames Wright   // --- Create Sub-Operator for slip boundaries
2019f844368SJames Wright   for (CeedInt i = 0; i < bc->num_slip; i++) {
2029f844368SJames Wright     PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->slips[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
2039f844368SJames Wright                                ceed_data->qf_apply_slip, ceed_data->qf_apply_slip_jacobian, op_apply, op_apply_ijacobian));
2049f844368SJames Wright   }
20511436a05SJames Wright 
20611436a05SJames Wright   // ----- Get Context Labels for Operator
207a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(*op_apply, "solution time", &phys->solution_time_label));
208a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(*op_apply, "timestep size", &phys->timestep_size_label));
209ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
21077841947SLeila Ghaffari }
21177841947SLeila Ghaffari 
2122b730f8bSJeremy L Thompson PetscErrorCode SetupBCQFunctions(Ceed ceed, PetscInt dim_sur, PetscInt num_comp_x, PetscInt num_comp_q, PetscInt q_data_size_sur,
2132b730f8bSJeremy L Thompson                                  PetscInt jac_data_size_sur, ProblemQFunctionSpec apply_bc, ProblemQFunctionSpec apply_bc_jacobian,
2145b881d11SJames Wright                                  CeedQFunction *qf_apply_bc, CeedQFunction *qf_apply_bc_jacobian) {
2155b881d11SJames Wright   PetscFunctionBeginUser;
2165b881d11SJames Wright   if (apply_bc.qfunction) {
217a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, apply_bc.qfunction, apply_bc.qfunction_loc, qf_apply_bc));
218a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_apply_bc, apply_bc.qfunction_context));
21991c97f41SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_apply_bc, 0));
220a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "q", num_comp_q, CEED_EVAL_INTERP));
221a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "Grad_q", num_comp_q * dim_sur, CEED_EVAL_GRAD));
222a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
223a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc, "x", num_comp_x, CEED_EVAL_INTERP));
224a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc, "v", num_comp_q, CEED_EVAL_INTERP));
225a424bcd0SJames Wright     if (jac_data_size_sur) PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE));
2265b881d11SJames Wright   }
2275b881d11SJames Wright   if (apply_bc_jacobian.qfunction) {
228a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, apply_bc_jacobian.qfunction, apply_bc_jacobian.qfunction_loc, qf_apply_bc_jacobian));
229a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(*qf_apply_bc_jacobian, apply_bc_jacobian.qfunction_context));
23091c97f41SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(*qf_apply_bc_jacobian, 0));
231a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "dq", num_comp_q, CEED_EVAL_INTERP));
232a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "Grad_dq", num_comp_q * dim_sur, CEED_EVAL_GRAD));
233a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
234a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "x", num_comp_x, CEED_EVAL_INTERP));
235a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE));
236a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(*qf_apply_bc_jacobian, "v", num_comp_q, CEED_EVAL_INTERP));
2375b881d11SJames Wright   }
238ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
2395b881d11SJames Wright }
2405b881d11SJames Wright 
2412b730f8bSJeremy L Thompson PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, AppCtx app_ctx, ProblemData *problem, SimpleBC bc) {
24277841947SLeila Ghaffari   PetscFunctionBeginUser;
24377841947SLeila Ghaffari   // *****************************************************************************
24477841947SLeila Ghaffari   // Set up CEED objects for the interior domain (volume)
24577841947SLeila Ghaffari   // *****************************************************************************
24677841947SLeila Ghaffari   const PetscInt num_comp_q = 5;
2471d2a9659SKenneth E. Jansen   const CeedInt  dim = problem->dim, num_comp_x = problem->dim, q_data_size_vol = problem->q_data_size_vol;
2481d2a9659SKenneth E. Jansen   CeedInt        jac_data_size_vol = num_comp_q + 6 + 3;
2491d2a9659SKenneth E. Jansen 
250752a08a7SJames Wright   if (problem->apply_vol_ifunction.qfunction && problem->uses_newtonian) {
2511d2a9659SKenneth E. Jansen     NewtonianIdealGasContext gas;
2521d2a9659SKenneth E. Jansen     PetscCallCeed(ceed, CeedQFunctionContextGetDataRead(problem->apply_vol_ifunction.qfunction_context, CEED_MEM_HOST, &gas));
2531d2a9659SKenneth E. Jansen     jac_data_size_vol += (gas->idl_enable ? 1 : 0);
2541d2a9659SKenneth E. Jansen     PetscCallCeed(ceed, CeedQFunctionContextRestoreDataRead(problem->apply_vol_ifunction.qfunction_context, &gas));
2551d2a9659SKenneth E. Jansen   }
2561d2a9659SKenneth E. Jansen 
257a3ae0734SJed Brown   CeedElemRestriction elem_restr_jd_i;
258a3ae0734SJed Brown   CeedVector          jac_data;
2590814d5a7SKenneth E. Jansen   CeedInt             num_qpts;
260bb85d312SJames Wright   DMLabel             domain_label = NULL;
261bb85d312SJames Wright   PetscInt            label_value = 0, height = 0, dm_field = 0;
26277841947SLeila Ghaffari 
26377841947SLeila Ghaffari   // -----------------------------------------------------------------------------
26477841947SLeila Ghaffari   // CEED Bases
26577841947SLeila Ghaffari   // -----------------------------------------------------------------------------
2660814d5a7SKenneth E. Jansen   DM dm_coord;
2670814d5a7SKenneth E. Jansen   PetscCall(DMGetCoordinateDM(dm, &dm_coord));
2680814d5a7SKenneth E. Jansen 
269bb85d312SJames Wright   PetscCall(CreateBasisFromPlex(ceed, dm, domain_label, label_value, height, dm_field, &ceed_data->basis_q));
270bb85d312SJames Wright   PetscCall(CreateBasisFromPlex(ceed, dm_coord, domain_label, label_value, height, dm_field, &ceed_data->basis_x));
271a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisCreateProjection(ceed_data->basis_x, ceed_data->basis_q, &ceed_data->basis_xc));
272a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisGetNumQuadraturePoints(ceed_data->basis_q, &num_qpts));
27377841947SLeila Ghaffari 
27477841947SLeila Ghaffari   // -----------------------------------------------------------------------------
27577841947SLeila Ghaffari   // CEED Restrictions
27677841947SLeila Ghaffari   // -----------------------------------------------------------------------------
27777841947SLeila Ghaffari   // -- Create restriction
278bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionCreate(ceed, dm, domain_label, label_value, height, 0, &ceed_data->elem_restr_q));
279bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionCoordinateCreate(ceed, dm, domain_label, label_value, height, &ceed_data->elem_restr_x));
280bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, q_data_size_vol, &ceed_data->elem_restr_qd_i));
281bb85d312SJames Wright   PetscCall(DMPlexCeedElemRestrictionQDataCreate(ceed, dm, domain_label, label_value, height, jac_data_size_vol, &elem_restr_jd_i));
28277841947SLeila Ghaffari   // -- Create E vectors
283a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_ceed, NULL));
284a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_dot_ceed, NULL));
285a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->g_ceed, NULL));
28677841947SLeila Ghaffari 
28777841947SLeila Ghaffari   // -----------------------------------------------------------------------------
28877841947SLeila Ghaffari   // CEED QFunctions
28977841947SLeila Ghaffari   // -----------------------------------------------------------------------------
29077841947SLeila Ghaffari   // -- Create QFunction for quadrature data
291a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->setup_vol.qfunction, problem->setup_vol.qfunction_loc, &ceed_data->qf_setup_vol));
292841e4c73SJed Brown   if (problem->setup_vol.qfunction_context) {
293a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_setup_vol, problem->setup_vol.qfunction_context));
294841e4c73SJed Brown   }
29591c97f41SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_setup_vol, 0));
296a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_vol, "dx", num_comp_x * dim, CEED_EVAL_GRAD));
297a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_vol, "weight", 1, CEED_EVAL_WEIGHT));
298a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_setup_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
29977841947SLeila Ghaffari 
30077841947SLeila Ghaffari   // -- Create QFunction for ICs
301a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->ics.qfunction, problem->ics.qfunction_loc, &ceed_data->qf_ics));
302a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_ics, problem->ics.qfunction_context));
30391c97f41SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_ics, 0));
304a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ics, "x", num_comp_x, CEED_EVAL_INTERP));
305a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ics, "dx", num_comp_x * dim, CEED_EVAL_GRAD));
306a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_ics, "q0", num_comp_q, CEED_EVAL_NONE));
30777841947SLeila Ghaffari 
30877841947SLeila Ghaffari   // -- Create QFunction for RHS
30991e5af17SJed Brown   if (problem->apply_vol_rhs.qfunction) {
310a424bcd0SJames Wright     PetscCallCeed(
311a424bcd0SJames Wright         ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_rhs.qfunction, problem->apply_vol_rhs.qfunction_loc, &ceed_data->qf_rhs_vol));
312a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_rhs_vol, problem->apply_vol_rhs.qfunction_context));
31391c97f41SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_rhs_vol, 0));
314a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "q", num_comp_q, CEED_EVAL_INTERP));
315a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
316a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
317a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "x", num_comp_x, CEED_EVAL_INTERP));
318a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "v", num_comp_q, CEED_EVAL_INTERP));
319a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
32077841947SLeila Ghaffari   }
32177841947SLeila Ghaffari 
32277841947SLeila Ghaffari   // -- Create QFunction for IFunction
32391e5af17SJed Brown   if (problem->apply_vol_ifunction.qfunction) {
324a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ifunction.qfunction, problem->apply_vol_ifunction.qfunction_loc,
325a424bcd0SJames Wright                                                     &ceed_data->qf_ifunction_vol));
326a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, problem->apply_vol_ifunction.qfunction_context));
32791c97f41SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_ifunction_vol, 0));
328a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q", num_comp_q, CEED_EVAL_INTERP));
329a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD));
330a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q dot", num_comp_q, CEED_EVAL_INTERP));
331a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
332a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "x", num_comp_x, CEED_EVAL_INTERP));
333a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "v", num_comp_q, CEED_EVAL_INTERP));
334a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
335a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE));
33677841947SLeila Ghaffari   }
33777841947SLeila Ghaffari 
338e334ad8fSJed Brown   CeedQFunction qf_ijacobian_vol = NULL;
339e334ad8fSJed Brown   if (problem->apply_vol_ijacobian.qfunction) {
340a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ijacobian.qfunction, problem->apply_vol_ijacobian.qfunction_loc,
341a424bcd0SJames Wright                                                     &qf_ijacobian_vol));
342a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(qf_ijacobian_vol, problem->apply_vol_ijacobian.qfunction_context));
34391c97f41SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(qf_ijacobian_vol, 0));
344a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "dq", num_comp_q, CEED_EVAL_INTERP));
345a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "Grad_dq", num_comp_q * dim, CEED_EVAL_GRAD));
346a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE));
347a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddInput(qf_ijacobian_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE));
348a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ijacobian_vol, "v", num_comp_q, CEED_EVAL_INTERP));
349a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionAddOutput(qf_ijacobian_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD));
350e334ad8fSJed Brown   }
351e334ad8fSJed Brown 
35277841947SLeila Ghaffari   // ---------------------------------------------------------------------------
35377841947SLeila Ghaffari   // Element coordinates
35477841947SLeila Ghaffari   // ---------------------------------------------------------------------------
35577841947SLeila Ghaffari   // -- Create CEED vector
356a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_x, &ceed_data->x_coord, NULL));
35777841947SLeila Ghaffari 
35877841947SLeila Ghaffari   // -- Copy PETSc vector in CEED vector
35977841947SLeila Ghaffari   Vec X_loc;
3603796c488SJed Brown   {
3613796c488SJed Brown     DM cdm;
3622b730f8bSJeremy L Thompson     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
3632b730f8bSJeremy L Thompson     if (cdm) {
3642b730f8bSJeremy L Thompson       PetscCall(DMGetCellCoordinatesLocal(dm, &X_loc));
3652b730f8bSJeremy L Thompson     } else {
3662b730f8bSJeremy L Thompson       PetscCall(DMGetCoordinatesLocal(dm, &X_loc));
3673796c488SJed Brown     }
3682b730f8bSJeremy L Thompson   }
3692b730f8bSJeremy L Thompson   PetscCall(VecScale(X_loc, problem->dm_scale));
370d0593705SJames Wright   PetscCall(VecCopyPetscToCeed(X_loc, ceed_data->x_coord));
37177841947SLeila Ghaffari 
37277841947SLeila Ghaffari   // -----------------------------------------------------------------------------
37377841947SLeila Ghaffari   // CEED vectors
37477841947SLeila Ghaffari   // -----------------------------------------------------------------------------
37577841947SLeila Ghaffari   // -- Create CEED vector for geometric data
376a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(ceed_data->elem_restr_qd_i, &ceed_data->q_data, NULL));
377a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionCreateVector(elem_restr_jd_i, &jac_data, NULL));
378d52d2babSJames Wright 
37977841947SLeila Ghaffari   // -----------------------------------------------------------------------------
38077841947SLeila Ghaffari   // CEED Operators
38177841947SLeila Ghaffari   // -----------------------------------------------------------------------------
38277841947SLeila Ghaffari   // -- Create CEED operator for quadrature data
383a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_setup_vol, NULL, NULL, &ceed_data->op_setup_vol));
384a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(ceed_data->op_setup_vol, "dx", ceed_data->elem_restr_x, ceed_data->basis_x, CEED_VECTOR_ACTIVE));
385a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(ceed_data->op_setup_vol, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x, CEED_VECTOR_NONE));
386356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(ceed_data->op_setup_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
38777841947SLeila Ghaffari 
38877841947SLeila Ghaffari   // -- Create CEED operator for ICs
3895263e9c6SJames Wright   CeedOperator op_ics;
390a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_ics, NULL, NULL, &op_ics));
391a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "x", ceed_data->elem_restr_x, ceed_data->basis_xc, CEED_VECTOR_ACTIVE));
392a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "dx", ceed_data->elem_restr_x, ceed_data->basis_xc, CEED_VECTOR_ACTIVE));
393356036faSJeremy L Thompson   PetscCallCeed(ceed, CeedOperatorSetField(op_ics, "q0", ceed_data->elem_restr_q, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE));
394a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_ics, "evaluation time", &user->phys->ics_time_label));
3955263e9c6SJames Wright   PetscCall(OperatorApplyContextCreate(NULL, dm, user->ceed, op_ics, ceed_data->x_coord, NULL, NULL, user->Q_loc, &ceed_data->op_ics_ctx));
396a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_ics));
39777841947SLeila Ghaffari 
39877841947SLeila Ghaffari   // Create CEED operator for RHS
39977841947SLeila Ghaffari   if (ceed_data->qf_rhs_vol) {
40077841947SLeila Ghaffari     CeedOperator op;
401a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_rhs_vol, NULL, NULL, &op));
402a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
403a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
404356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
405a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord));
406a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
407a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
40877841947SLeila Ghaffari     user->op_rhs_vol = op;
40977841947SLeila Ghaffari   }
41077841947SLeila Ghaffari 
41177841947SLeila Ghaffari   // -- CEED operator for IFunction
41277841947SLeila Ghaffari   if (ceed_data->qf_ifunction_vol) {
41377841947SLeila Ghaffari     CeedOperator op;
414a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, ceed_data->qf_ifunction_vol, NULL, NULL, &op));
415a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
416a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
417a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "q dot", ceed_data->elem_restr_q, ceed_data->basis_q, user->q_dot_ceed));
418356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
419a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord));
420a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
421a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
422356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op, "jac_data", elem_restr_jd_i, CEED_BASIS_NONE, jac_data));
423a3ae0734SJed Brown 
42477841947SLeila Ghaffari     user->op_ifunction_vol = op;
42577841947SLeila Ghaffari   }
42677841947SLeila Ghaffari 
427e334ad8fSJed Brown   CeedOperator op_ijacobian_vol = NULL;
428e334ad8fSJed Brown   if (qf_ijacobian_vol) {
429e334ad8fSJed Brown     CeedOperator op;
430a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorCreate(ceed, qf_ijacobian_vol, NULL, NULL, &op));
431a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
432a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
433356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_NONE, ceed_data->q_data));
434356036faSJeremy L Thompson     PetscCallCeed(ceed, CeedOperatorSetField(op, "jac_data", elem_restr_jd_i, CEED_BASIS_NONE, jac_data));
435a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
436a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE));
437e334ad8fSJed Brown     op_ijacobian_vol = op;
438a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionDestroy(&qf_ijacobian_vol));
439e334ad8fSJed Brown   }
440e334ad8fSJed Brown 
44177841947SLeila Ghaffari   // *****************************************************************************
44277841947SLeila Ghaffari   // Set up CEED objects for the exterior domain (surface)
44377841947SLeila Ghaffari   // *****************************************************************************
444bb85d312SJames Wright   height                = 1;
445bb85d312SJames Wright   CeedInt       dim_sur = dim - height, P_sur = app_ctx->degree + 1, Q_sur = P_sur + app_ctx->q_extra;
446f21e6b1cSJames 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;
44777841947SLeila Ghaffari 
44877841947SLeila Ghaffari   // -----------------------------------------------------------------------------
44977841947SLeila Ghaffari   // CEED Bases
45077841947SLeila Ghaffari   // -----------------------------------------------------------------------------
4510814d5a7SKenneth E. Jansen 
4520814d5a7SKenneth E. Jansen   DMLabel  label   = 0;
4530814d5a7SKenneth E. Jansen   PetscInt face_id = 0;
4540814d5a7SKenneth E. Jansen   PetscInt field   = 0;  // Still want the normal, default field
4550814d5a7SKenneth E. Jansen   PetscCall(CreateBasisFromPlex(ceed, dm, label, face_id, height, field, &ceed_data->basis_q_sur));
4560814d5a7SKenneth E. Jansen   PetscCall(CreateBasisFromPlex(ceed, dm_coord, label, face_id, height, field, &ceed_data->basis_x_sur));
457a424bcd0SJames Wright   PetscCallCeed(ceed, CeedBasisCreateProjection(ceed_data->basis_x_sur, ceed_data->basis_q_sur, &ceed_data->basis_xc_sur));
45877841947SLeila Ghaffari 
45977841947SLeila Ghaffari   // -----------------------------------------------------------------------------
46077841947SLeila Ghaffari   // CEED QFunctions
46177841947SLeila Ghaffari   // -----------------------------------------------------------------------------
46277841947SLeila Ghaffari   // -- Create QFunction for quadrature data
463a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionCreateInterior(ceed, 1, problem->setup_sur.qfunction, problem->setup_sur.qfunction_loc, &ceed_data->qf_setup_sur));
464841e4c73SJed Brown   if (problem->setup_sur.qfunction_context) {
465a424bcd0SJames Wright     PetscCallCeed(ceed, CeedQFunctionSetContext(ceed_data->qf_setup_sur, problem->setup_sur.qfunction_context));
466841e4c73SJed Brown   }
46791c97f41SJames Wright   PetscCallCeed(ceed, CeedQFunctionSetUserFlopsEstimate(ceed_data->qf_setup_sur, 0));
468a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_sur, "dx", num_comp_x * dim_sur, CEED_EVAL_GRAD));
469a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddInput(ceed_data->qf_setup_sur, "weight", 1, CEED_EVAL_WEIGHT));
470a424bcd0SJames Wright   PetscCallCeed(ceed, CeedQFunctionAddOutput(ceed_data->qf_setup_sur, "surface qdata", q_data_size_sur, CEED_EVAL_NONE));
47177841947SLeila Ghaffari 
4722b730f8bSJeremy L Thompson   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_inflow,
4732b730f8bSJeremy L Thompson                               problem->apply_inflow_jacobian, &ceed_data->qf_apply_inflow, &ceed_data->qf_apply_inflow_jacobian));
4742b730f8bSJeremy L Thompson   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_outflow,
4752b730f8bSJeremy L Thompson                               problem->apply_outflow_jacobian, &ceed_data->qf_apply_outflow, &ceed_data->qf_apply_outflow_jacobian));
4762b730f8bSJeremy L Thompson   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_freestream,
4772b730f8bSJeremy L Thompson                               problem->apply_freestream_jacobian, &ceed_data->qf_apply_freestream, &ceed_data->qf_apply_freestream_jacobian));
4789f844368SJames Wright   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_slip,
4799f844368SJames Wright                               problem->apply_slip_jacobian, &ceed_data->qf_apply_slip, &ceed_data->qf_apply_slip_jacobian));
48077841947SLeila Ghaffari 
48177841947SLeila Ghaffari   // *****************************************************************************
48277841947SLeila Ghaffari   // CEED Operator Apply
48377841947SLeila Ghaffari   // *****************************************************************************
48477841947SLeila Ghaffari   // -- Apply CEED Operator for the geometric data
485a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorApply(ceed_data->op_setup_vol, ceed_data->x_coord, ceed_data->q_data, CEED_REQUEST_IMMEDIATE));
48677841947SLeila Ghaffari 
48777841947SLeila Ghaffari   // -- Create and apply CEED Composite Operator for the entire domain
48877841947SLeila Ghaffari   if (!user->phys->implicit) {  // RHS
4899ad5e8e4SJames Wright     CeedOperator op_rhs;
4909ad5e8e4SJames Wright     PetscCall(CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys, user->op_rhs_vol, NULL, height, P_sur, Q_sur, q_data_size_sur, 0, &op_rhs,
4919ad5e8e4SJames Wright                                       NULL));
4929ad5e8e4SJames Wright     PetscCall(OperatorApplyContextCreate(dm, dm, ceed, op_rhs, user->q_ceed, user->g_ceed, user->Q_loc, NULL, &user->op_rhs_ctx));
493a424bcd0SJames Wright     PetscCallCeed(ceed, CeedOperatorDestroy(&op_rhs));
494*b4e9a8f8SJames Wright     PetscCall(CreateKSPMass(user, problem));
495577c3361SJames Wright     PetscCheck(app_ctx->sgs_model_type == SGS_MODEL_NONE, user->comm, PETSC_ERR_SUP, "SGS modeling not implemented for explicit timestepping");
49677841947SLeila Ghaffari   } else {  // IFunction
49791c97f41SJames Wright     CeedOperator op_ijacobian = NULL;
49891c97f41SJames Wright 
4992b730f8bSJeremy L Thompson     PetscCall(CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys, user->op_ifunction_vol, op_ijacobian_vol, height, P_sur, Q_sur,
50091c97f41SJames Wright                                       q_data_size_sur, jac_data_size_sur, &user->op_ifunction, op_ijacobian_vol ? &op_ijacobian : NULL));
50191c97f41SJames Wright     if (op_ijacobian) {
50291c97f41SJames Wright       PetscCall(MatCeedCreate(user->dm, user->dm, op_ijacobian, NULL, &user->mat_ijacobian));
50391c97f41SJames Wright       PetscCall(MatCeedSetLocalVectors(user->mat_ijacobian, user->Q_dot_loc, NULL));
50491c97f41SJames Wright       PetscCallCeed(ceed, CeedOperatorGetContextFieldLabel(op_ijacobian, "ijacobian time shift", &user->phys->ijacobian_time_shift_label));
50591c97f41SJames Wright       PetscCallCeed(ceed, CeedOperatorDestroy(&op_ijacobian));
506e334ad8fSJed Brown     }
5078f5ab23bSJames Wright     if (app_ctx->sgs_model_type == SGS_MODEL_DATA_DRIVEN) PetscCall(SgsDDSetup(ceed, user, ceed_data, problem));
508dada6cc0SJames Wright   }
509f5452247SJames Wright 
510577c3361SJames Wright   if (problem->use_strong_bc_ceed) PetscCall(SetupStrongBC_Ceed(ceed, ceed_data, dm, user, problem, bc));
511f5452247SJames Wright   if (app_ctx->turb_spanstats_enable) PetscCall(TurbulenceStatisticsSetup(ceed, user, ceed_data, problem));
512ee3b213aSJames Wright   if (app_ctx->diff_filter_monitor && !user->diff_filter) PetscCall(DifferentialFilterSetup(ceed, user, ceed_data, problem));
51328134cfaSJames Wright   if (app_ctx->sgs_train_enable) PetscCall(SGS_DD_TrainingSetup(ceed, user, ceed_data, problem));
514f5452247SJames Wright 
515a424bcd0SJames Wright   PetscCallCeed(ceed, CeedElemRestrictionDestroy(&elem_restr_jd_i));
516a424bcd0SJames Wright   PetscCallCeed(ceed, CeedOperatorDestroy(&op_ijacobian_vol));
517a424bcd0SJames Wright   PetscCallCeed(ceed, CeedVectorDestroy(&jac_data));
518ee4ca9cbSJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
51977841947SLeila Ghaffari }
520