xref: /honee/src/setuplibceed.c (revision a8bca8ac5a983fdbd7665d063f20c0e0c1143c29)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Setup libCEED for Navier-Stokes example using PETSc
10 
11 #include "../navierstokes.h"
12 
13 // Utility function - essential BC dofs are encoded in closure indices as -(i+1).
14 PetscInt Involute(PetscInt i) { return i >= 0 ? i : -(i + 1); }
15 
16 // Utility function to create local CEED restriction
17 PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height, DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr) {
18   PetscInt num_elem, elem_size, num_dof, num_comp, *elem_restr_offsets;
19 
20   PetscFunctionBeginUser;
21   PetscCall(DMPlexGetLocalOffsets(dm, domain_label, value, height, 0, &num_elem, &elem_size, &num_comp, &num_dof, &elem_restr_offsets));
22 
23   CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp, 1, num_dof, CEED_MEM_HOST, CEED_COPY_VALUES, elem_restr_offsets, elem_restr);
24   PetscCall(PetscFree(elem_restr_offsets));
25 
26   PetscFunctionReturn(0);
27 }
28 
29 // Utility function to get Ceed Restriction for each domain
30 PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height, DMLabel domain_label, PetscInt value, CeedInt Q, CeedInt q_data_size,
31                                        CeedElemRestriction *elem_restr_q, CeedElemRestriction *elem_restr_x, CeedElemRestriction *elem_restr_qd_i) {
32   DM                  dm_coord;
33   CeedInt             dim, loc_num_elem;
34   CeedInt             Q_dim;
35   CeedElemRestriction elem_restr_tmp;
36   PetscFunctionBeginUser;
37 
38   PetscCall(DMGetDimension(dm, &dim));
39   dim -= height;
40   Q_dim = CeedIntPow(Q, dim);
41   PetscCall(CreateRestrictionFromPlex(ceed, dm, height, domain_label, value, &elem_restr_tmp));
42   if (elem_restr_q) *elem_restr_q = elem_restr_tmp;
43   if (elem_restr_x) {
44     PetscCall(DMGetCellCoordinateDM(dm, &dm_coord));
45     if (!dm_coord) {
46       PetscCall(DMGetCoordinateDM(dm, &dm_coord));
47     }
48     PetscCall(DMPlexSetClosurePermutationTensor(dm_coord, PETSC_DETERMINE, NULL));
49     PetscCall(CreateRestrictionFromPlex(ceed, dm_coord, height, domain_label, value, elem_restr_x));
50   }
51   if (elem_restr_qd_i) {
52     CeedElemRestrictionGetNumElements(elem_restr_tmp, &loc_num_elem);
53     CeedElemRestrictionCreateStrided(ceed, loc_num_elem, Q_dim, q_data_size, q_data_size * loc_num_elem * Q_dim, CEED_STRIDES_BACKEND,
54                                      elem_restr_qd_i);
55   }
56   if (!elem_restr_q) CeedElemRestrictionDestroy(&elem_restr_tmp);
57   PetscFunctionReturn(0);
58 }
59 
60 PetscErrorCode AddBCSubOperator(Ceed ceed, DM dm, CeedData ceed_data, DMLabel domain_label, PetscInt value, CeedInt height, CeedInt Q_sur,
61                                 CeedInt q_data_size_sur, CeedInt jac_data_size_sur, CeedQFunction qf_apply_bc, CeedQFunction qf_apply_bc_jacobian,
62                                 CeedOperator *op_apply, CeedOperator *op_apply_ijacobian) {
63   CeedVector          q_data_sur, jac_data_sur;
64   CeedOperator        op_setup_sur, op_apply_bc, op_apply_bc_jacobian = NULL;
65   CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur, elem_restr_jd_i_sur;
66   CeedInt             num_qpts_sur;
67   PetscFunctionBeginUser;
68 
69   // --- Get number of quadrature points for the boundaries
70   CeedBasisGetNumQuadraturePoints(ceed_data->basis_q_sur, &num_qpts_sur);
71 
72   // ---- CEED Restriction
73   PetscCall(GetRestrictionForDomain(ceed, dm, height, domain_label, value, Q_sur, q_data_size_sur, &elem_restr_q_sur, &elem_restr_x_sur,
74                                     &elem_restr_qd_i_sur));
75   if (jac_data_size_sur > 0) {
76     // State-dependent data will be passed from residual to Jacobian. This will be collocated.
77     PetscCall(GetRestrictionForDomain(ceed, dm, height, domain_label, value, Q_sur, jac_data_size_sur, NULL, NULL, &elem_restr_jd_i_sur));
78     CeedElemRestrictionCreateVector(elem_restr_jd_i_sur, &jac_data_sur, NULL);
79   } else {
80     elem_restr_jd_i_sur = NULL;
81     jac_data_sur        = NULL;
82   }
83 
84   // ---- CEED Vector
85   PetscInt loc_num_elem_sur;
86   CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur);
87   CeedVectorCreate(ceed, q_data_size_sur * loc_num_elem_sur * num_qpts_sur, &q_data_sur);
88 
89   // ---- CEED Operator
90   // ----- CEED Operator for Setup (geometric factors)
91   CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur);
92   CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur, ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE);
93   CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x_sur, CEED_VECTOR_NONE);
94   CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
95 
96   // ----- CEED Operator for Physics
97   CeedOperatorCreate(ceed, qf_apply_bc, NULL, NULL, &op_apply_bc);
98   CeedOperatorSetField(op_apply_bc, "q", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
99   CeedOperatorSetField(op_apply_bc, "Grad_q", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
100   CeedOperatorSetField(op_apply_bc, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_COLLOCATED, q_data_sur);
101   CeedOperatorSetField(op_apply_bc, "x", elem_restr_x_sur, ceed_data->basis_x_sur, ceed_data->x_coord);
102   CeedOperatorSetField(op_apply_bc, "v", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
103   if (elem_restr_jd_i_sur) CeedOperatorSetField(op_apply_bc, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_COLLOCATED, jac_data_sur);
104 
105   if (qf_apply_bc_jacobian) {
106     CeedOperatorCreate(ceed, qf_apply_bc_jacobian, NULL, NULL, &op_apply_bc_jacobian);
107     CeedOperatorSetField(op_apply_bc_jacobian, "dq", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
108     CeedOperatorSetField(op_apply_bc_jacobian, "Grad_dq", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
109     CeedOperatorSetField(op_apply_bc_jacobian, "surface qdata", elem_restr_qd_i_sur, CEED_BASIS_COLLOCATED, q_data_sur);
110     CeedOperatorSetField(op_apply_bc_jacobian, "x", elem_restr_x_sur, ceed_data->basis_x_sur, ceed_data->x_coord);
111     CeedOperatorSetField(op_apply_bc_jacobian, "surface jacobian data", elem_restr_jd_i_sur, CEED_BASIS_COLLOCATED, jac_data_sur);
112     CeedOperatorSetField(op_apply_bc_jacobian, "v", elem_restr_q_sur, ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
113   }
114 
115   // ----- Apply CEED operator for Setup
116   CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur, CEED_REQUEST_IMMEDIATE);
117 
118   // ----- Apply Sub-Operator for Physics
119   CeedCompositeOperatorAddSub(*op_apply, op_apply_bc);
120   if (op_apply_bc_jacobian) CeedCompositeOperatorAddSub(*op_apply_ijacobian, op_apply_bc_jacobian);
121 
122   // ----- Cleanup
123   CeedVectorDestroy(&q_data_sur);
124   CeedVectorDestroy(&jac_data_sur);
125   CeedElemRestrictionDestroy(&elem_restr_q_sur);
126   CeedElemRestrictionDestroy(&elem_restr_x_sur);
127   CeedElemRestrictionDestroy(&elem_restr_qd_i_sur);
128   CeedElemRestrictionDestroy(&elem_restr_jd_i_sur);
129   CeedOperatorDestroy(&op_setup_sur);
130   CeedOperatorDestroy(&op_apply_bc);
131   CeedOperatorDestroy(&op_apply_bc_jacobian);
132 
133   PetscFunctionReturn(0);
134 }
135 
136 // Utility function to create CEED Composite Operator for the entire domain
137 PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc, CeedData ceed_data, Physics phys, CeedOperator op_apply_vol,
138                                        CeedOperator op_apply_ijacobian_vol, CeedInt height, CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur,
139                                        CeedInt jac_data_size_sur, CeedOperator *op_apply, CeedOperator *op_apply_ijacobian) {
140   DMLabel domain_label;
141   PetscFunctionBeginUser;
142 
143   // Create Composite Operaters
144   CeedCompositeOperatorCreate(ceed, op_apply);
145   if (op_apply_ijacobian) CeedCompositeOperatorCreate(ceed, op_apply_ijacobian);
146 
147   // --Apply Sub-Operator for the volume
148   CeedCompositeOperatorAddSub(*op_apply, op_apply_vol);
149   if (op_apply_ijacobian) CeedCompositeOperatorAddSub(*op_apply_ijacobian, op_apply_ijacobian_vol);
150 
151   // -- Create Sub-Operator for in/outflow BCs
152   if (phys->has_neumann || 1) {
153     // --- Setup
154     PetscCall(DMGetLabel(dm, "Face Sets", &domain_label));
155 
156     // --- Create Sub-Operator for inflow boundaries
157     for (CeedInt i = 0; i < bc->num_inflow; i++) {
158       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->inflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
159                                  ceed_data->qf_apply_inflow, ceed_data->qf_apply_inflow_jacobian, op_apply, op_apply_ijacobian));
160     }
161     // --- Create Sub-Operator for outflow boundaries
162     for (CeedInt i = 0; i < bc->num_outflow; i++) {
163       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->outflows[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
164                                  ceed_data->qf_apply_outflow, ceed_data->qf_apply_outflow_jacobian, op_apply, op_apply_ijacobian));
165     }
166     // --- Create Sub-Operator for freestream boundaries
167     for (CeedInt i = 0; i < bc->num_freestream; i++) {
168       PetscCall(AddBCSubOperator(ceed, dm, ceed_data, domain_label, bc->freestreams[i], height, Q_sur, q_data_size_sur, jac_data_size_sur,
169                                  ceed_data->qf_apply_freestream, ceed_data->qf_apply_freestream_jacobian, op_apply, op_apply_ijacobian));
170     }
171   }
172 
173   // ----- Get Context Labels for Operator
174   CeedOperatorContextGetFieldLabel(*op_apply, "solution time", &phys->solution_time_label);
175   CeedOperatorContextGetFieldLabel(*op_apply, "timestep size", &phys->timestep_size_label);
176 
177   PetscFunctionReturn(0);
178 }
179 
180 PetscErrorCode SetupBCQFunctions(Ceed ceed, PetscInt dim_sur, PetscInt num_comp_x, PetscInt num_comp_q, PetscInt q_data_size_sur,
181                                  PetscInt jac_data_size_sur, ProblemQFunctionSpec apply_bc, ProblemQFunctionSpec apply_bc_jacobian,
182                                  CeedQFunction *qf_apply_bc, CeedQFunction *qf_apply_bc_jacobian) {
183   PetscFunctionBeginUser;
184 
185   if (apply_bc.qfunction) {
186     CeedQFunctionCreateInterior(ceed, 1, apply_bc.qfunction, apply_bc.qfunction_loc, qf_apply_bc);
187     CeedQFunctionSetContext(*qf_apply_bc, apply_bc.qfunction_context);
188     CeedQFunctionContextDestroy(&apply_bc.qfunction_context);
189     CeedQFunctionAddInput(*qf_apply_bc, "q", num_comp_q, CEED_EVAL_INTERP);
190     CeedQFunctionAddInput(*qf_apply_bc, "Grad_q", num_comp_q * dim_sur, CEED_EVAL_GRAD);
191     CeedQFunctionAddInput(*qf_apply_bc, "surface qdata", q_data_size_sur, CEED_EVAL_NONE);
192     CeedQFunctionAddInput(*qf_apply_bc, "x", num_comp_x, CEED_EVAL_INTERP);
193     CeedQFunctionAddOutput(*qf_apply_bc, "v", num_comp_q, CEED_EVAL_INTERP);
194     if (jac_data_size_sur) CeedQFunctionAddOutput(*qf_apply_bc, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE);
195   }
196   if (apply_bc_jacobian.qfunction) {
197     CeedQFunctionCreateInterior(ceed, 1, apply_bc_jacobian.qfunction, apply_bc_jacobian.qfunction_loc, qf_apply_bc_jacobian);
198     CeedQFunctionSetContext(*qf_apply_bc_jacobian, apply_bc_jacobian.qfunction_context);
199     CeedQFunctionContextDestroy(&apply_bc_jacobian.qfunction_context);
200     CeedQFunctionAddInput(*qf_apply_bc_jacobian, "dq", num_comp_q, CEED_EVAL_INTERP);
201     CeedQFunctionAddInput(*qf_apply_bc_jacobian, "Grad_dq", num_comp_q * dim_sur, CEED_EVAL_GRAD);
202     CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface qdata", q_data_size_sur, CEED_EVAL_NONE);
203     CeedQFunctionAddInput(*qf_apply_bc_jacobian, "x", num_comp_x, CEED_EVAL_INTERP);
204     CeedQFunctionAddInput(*qf_apply_bc_jacobian, "surface jacobian data", jac_data_size_sur, CEED_EVAL_NONE);
205     CeedQFunctionAddOutput(*qf_apply_bc_jacobian, "v", num_comp_q, CEED_EVAL_INTERP);
206   }
207   PetscFunctionReturn(0);
208 }
209 
210 PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user, AppCtx app_ctx, ProblemData *problem, SimpleBC bc) {
211   PetscFunctionBeginUser;
212 
213   // *****************************************************************************
214   // Set up CEED objects for the interior domain (volume)
215   // *****************************************************************************
216   const PetscInt num_comp_q = 5;
217   const CeedInt  dim = problem->dim, num_comp_x = problem->dim, q_data_size_vol = problem->q_data_size_vol, jac_data_size_vol = num_comp_q + 6 + 3,
218                 P = app_ctx->degree + 1, Q = P + app_ctx->q_extra;
219   CeedElemRestriction elem_restr_jd_i;
220   CeedVector          jac_data;
221 
222   // -----------------------------------------------------------------------------
223   // CEED Bases
224   // -----------------------------------------------------------------------------
225   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_q, P, Q, CEED_GAUSS, &ceed_data->basis_q);
226   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, Q, CEED_GAUSS, &ceed_data->basis_x);
227   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, P, CEED_GAUSS_LOBATTO, &ceed_data->basis_xc);
228 
229   // -----------------------------------------------------------------------------
230   // CEED Restrictions
231   // -----------------------------------------------------------------------------
232   // -- Create restriction
233   PetscCall(GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, q_data_size_vol, &ceed_data->elem_restr_q, &ceed_data->elem_restr_x,
234                                     &ceed_data->elem_restr_qd_i));
235 
236   PetscCall(GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, jac_data_size_vol, NULL, NULL, &elem_restr_jd_i));
237   // -- Create E vectors
238   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_ceed, NULL);
239   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_dot_ceed, NULL);
240   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->g_ceed, NULL);
241 
242   // -----------------------------------------------------------------------------
243   // CEED QFunctions
244   // -----------------------------------------------------------------------------
245   // -- Create QFunction for quadrature data
246   CeedQFunctionCreateInterior(ceed, 1, problem->setup_vol.qfunction, problem->setup_vol.qfunction_loc, &ceed_data->qf_setup_vol);
247   if (problem->setup_vol.qfunction_context) {
248     CeedQFunctionSetContext(ceed_data->qf_setup_vol, problem->setup_vol.qfunction_context);
249     CeedQFunctionContextDestroy(&problem->setup_vol.qfunction_context);
250   }
251   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "dx", num_comp_x * dim, CEED_EVAL_GRAD);
252   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "weight", 1, CEED_EVAL_WEIGHT);
253   CeedQFunctionAddOutput(ceed_data->qf_setup_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE);
254 
255   // -- Create QFunction for ICs
256   CeedQFunctionCreateInterior(ceed, 1, problem->ics.qfunction, problem->ics.qfunction_loc, &ceed_data->qf_ics);
257   CeedQFunctionSetContext(ceed_data->qf_ics, problem->ics.qfunction_context);
258   CeedQFunctionContextDestroy(&problem->ics.qfunction_context);
259   CeedQFunctionAddInput(ceed_data->qf_ics, "x", num_comp_x, CEED_EVAL_INTERP);
260   CeedQFunctionAddInput(ceed_data->qf_ics, "qdata", q_data_size_vol, CEED_EVAL_NONE);
261   CeedQFunctionAddOutput(ceed_data->qf_ics, "q0", num_comp_q, CEED_EVAL_NONE);
262 
263   // -- Create QFunction for RHS
264   if (problem->apply_vol_rhs.qfunction) {
265     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_rhs.qfunction, problem->apply_vol_rhs.qfunction_loc, &ceed_data->qf_rhs_vol);
266     CeedQFunctionSetContext(ceed_data->qf_rhs_vol, problem->apply_vol_rhs.qfunction_context);
267     CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfunction_context);
268     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "q", num_comp_q, CEED_EVAL_INTERP);
269     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD);
270     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE);
271     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "x", num_comp_x, CEED_EVAL_INTERP);
272     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "v", num_comp_q, CEED_EVAL_INTERP);
273     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD);
274   }
275 
276   // -- Create QFunction for IFunction
277   if (problem->apply_vol_ifunction.qfunction) {
278     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ifunction.qfunction, problem->apply_vol_ifunction.qfunction_loc,
279                                 &ceed_data->qf_ifunction_vol);
280     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol, problem->apply_vol_ifunction.qfunction_context);
281     CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfunction_context);
282     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q", num_comp_q, CEED_EVAL_INTERP);
283     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "Grad_q", num_comp_q * dim, CEED_EVAL_GRAD);
284     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q dot", num_comp_q, CEED_EVAL_INTERP);
285     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE);
286     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "x", num_comp_x, CEED_EVAL_INTERP);
287     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "v", num_comp_q, CEED_EVAL_INTERP);
288     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD);
289     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE);
290   }
291 
292   CeedQFunction qf_ijacobian_vol = NULL;
293   if (problem->apply_vol_ijacobian.qfunction) {
294     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ijacobian.qfunction, problem->apply_vol_ijacobian.qfunction_loc, &qf_ijacobian_vol);
295     CeedQFunctionSetContext(qf_ijacobian_vol, problem->apply_vol_ijacobian.qfunction_context);
296     CeedQFunctionContextDestroy(&problem->apply_vol_ijacobian.qfunction_context);
297     CeedQFunctionAddInput(qf_ijacobian_vol, "dq", num_comp_q, CEED_EVAL_INTERP);
298     CeedQFunctionAddInput(qf_ijacobian_vol, "Grad_dq", num_comp_q * dim, CEED_EVAL_GRAD);
299     CeedQFunctionAddInput(qf_ijacobian_vol, "qdata", q_data_size_vol, CEED_EVAL_NONE);
300     CeedQFunctionAddInput(qf_ijacobian_vol, "x", num_comp_x, CEED_EVAL_INTERP);
301     CeedQFunctionAddInput(qf_ijacobian_vol, "jac_data", jac_data_size_vol, CEED_EVAL_NONE);
302     CeedQFunctionAddOutput(qf_ijacobian_vol, "v", num_comp_q, CEED_EVAL_INTERP);
303     CeedQFunctionAddOutput(qf_ijacobian_vol, "Grad_v", num_comp_q * dim, CEED_EVAL_GRAD);
304   }
305 
306   // ---------------------------------------------------------------------------
307   // Element coordinates
308   // ---------------------------------------------------------------------------
309   // -- Create CEED vector
310   CeedElemRestrictionCreateVector(ceed_data->elem_restr_x, &ceed_data->x_coord, NULL);
311 
312   // -- Copy PETSc vector in CEED vector
313   Vec                X_loc;
314   const PetscScalar *X_loc_array;
315   {
316     DM cdm;
317     PetscCall(DMGetCellCoordinateDM(dm, &cdm));
318     if (cdm) {
319       PetscCall(DMGetCellCoordinatesLocal(dm, &X_loc));
320     } else {
321       PetscCall(DMGetCoordinatesLocal(dm, &X_loc));
322     }
323   }
324   PetscCall(VecScale(X_loc, problem->dm_scale));
325   PetscCall(VecGetArrayRead(X_loc, &X_loc_array));
326   CeedVectorSetArray(ceed_data->x_coord, CEED_MEM_HOST, CEED_COPY_VALUES, (PetscScalar *)X_loc_array);
327   PetscCall(VecRestoreArrayRead(X_loc, &X_loc_array));
328 
329   // -----------------------------------------------------------------------------
330   // CEED vectors
331   // -----------------------------------------------------------------------------
332   // -- Create CEED vector for geometric data
333   CeedElemRestrictionCreateVector(ceed_data->elem_restr_qd_i, &ceed_data->q_data, NULL);
334   CeedElemRestrictionCreateVector(elem_restr_jd_i, &jac_data, NULL);
335 
336   // -----------------------------------------------------------------------------
337   // CEED Operators
338   // -----------------------------------------------------------------------------
339   // -- Create CEED operator for quadrature data
340   CeedOperatorCreate(ceed, ceed_data->qf_setup_vol, NULL, NULL, &ceed_data->op_setup_vol);
341   CeedOperatorSetField(ceed_data->op_setup_vol, "dx", ceed_data->elem_restr_x, ceed_data->basis_x, CEED_VECTOR_ACTIVE);
342   CeedOperatorSetField(ceed_data->op_setup_vol, "weight", CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x, CEED_VECTOR_NONE);
343   CeedOperatorSetField(ceed_data->op_setup_vol, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
344 
345   // -- Create CEED operator for ICs
346   CeedOperatorCreate(ceed, ceed_data->qf_ics, NULL, NULL, &ceed_data->op_ics);
347   CeedOperatorSetField(ceed_data->op_ics, "x", ceed_data->elem_restr_x, ceed_data->basis_xc, CEED_VECTOR_ACTIVE);
348   CeedOperatorSetField(ceed_data->op_ics, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
349   CeedOperatorSetField(ceed_data->op_ics, "q0", ceed_data->elem_restr_q, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
350   CeedOperatorContextGetFieldLabel(ceed_data->op_ics, "evaluation time", &user->phys->ics_time_label);
351 
352   // Create CEED operator for RHS
353   if (ceed_data->qf_rhs_vol) {
354     CeedOperator op;
355     CeedOperatorCreate(ceed, ceed_data->qf_rhs_vol, NULL, NULL, &op);
356     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
357     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
358     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
359     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord);
360     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
361     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
362     user->op_rhs_vol = op;
363   }
364 
365   // -- CEED operator for IFunction
366   if (ceed_data->qf_ifunction_vol) {
367     CeedOperator op;
368     CeedOperatorCreate(ceed, ceed_data->qf_ifunction_vol, NULL, NULL, &op);
369     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
370     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
371     CeedOperatorSetField(op, "q dot", ceed_data->elem_restr_q, ceed_data->basis_q, user->q_dot_ceed);
372     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
373     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord);
374     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
375     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
376     CeedOperatorSetField(op, "jac_data", elem_restr_jd_i, CEED_BASIS_COLLOCATED, jac_data);
377 
378     user->op_ifunction_vol = op;
379   }
380 
381   CeedOperator op_ijacobian_vol = NULL;
382   if (qf_ijacobian_vol) {
383     CeedOperator op;
384     CeedOperatorCreate(ceed, qf_ijacobian_vol, NULL, NULL, &op);
385     CeedOperatorSetField(op, "dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
386     CeedOperatorSetField(op, "Grad_dq", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
387     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, ceed_data->q_data);
388     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x, ceed_data->x_coord);
389     CeedOperatorSetField(op, "jac_data", elem_restr_jd_i, CEED_BASIS_COLLOCATED, jac_data);
390     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
391     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q, CEED_VECTOR_ACTIVE);
392     op_ijacobian_vol = op;
393     CeedQFunctionDestroy(&qf_ijacobian_vol);
394   }
395 
396   // *****************************************************************************
397   // Set up CEED objects for the exterior domain (surface)
398   // *****************************************************************************
399   CeedInt       height = 1, dim_sur = dim - height, P_sur = app_ctx->degree + 1, Q_sur = P_sur + app_ctx->q_extra;
400   const CeedInt q_data_size_sur = problem->q_data_size_sur, jac_data_size_sur = problem->jac_data_size_sur;
401 
402   // -----------------------------------------------------------------------------
403   // CEED Bases
404   // -----------------------------------------------------------------------------
405   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_q, P_sur, Q_sur, CEED_GAUSS, &ceed_data->basis_q_sur);
406   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_x, 2, Q_sur, CEED_GAUSS, &ceed_data->basis_x_sur);
407   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_x, 2, P_sur, CEED_GAUSS_LOBATTO, &ceed_data->basis_xc_sur);
408 
409   // -----------------------------------------------------------------------------
410   // CEED QFunctions
411   // -----------------------------------------------------------------------------
412   // -- Create QFunction for quadrature data
413   CeedQFunctionCreateInterior(ceed, 1, problem->setup_sur.qfunction, problem->setup_sur.qfunction_loc, &ceed_data->qf_setup_sur);
414   if (problem->setup_sur.qfunction_context) {
415     CeedQFunctionSetContext(ceed_data->qf_setup_sur, problem->setup_sur.qfunction_context);
416     CeedQFunctionContextDestroy(&problem->setup_sur.qfunction_context);
417   }
418   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "dx", num_comp_x * dim_sur, CEED_EVAL_GRAD);
419   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "weight", 1, CEED_EVAL_WEIGHT);
420   CeedQFunctionAddOutput(ceed_data->qf_setup_sur, "surface qdata", q_data_size_sur, CEED_EVAL_NONE);
421 
422   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_inflow,
423                               problem->apply_inflow_jacobian, &ceed_data->qf_apply_inflow, &ceed_data->qf_apply_inflow_jacobian));
424   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_outflow,
425                               problem->apply_outflow_jacobian, &ceed_data->qf_apply_outflow, &ceed_data->qf_apply_outflow_jacobian));
426   PetscCall(SetupBCQFunctions(ceed, dim_sur, num_comp_x, num_comp_q, q_data_size_sur, jac_data_size_sur, problem->apply_freestream,
427                               problem->apply_freestream_jacobian, &ceed_data->qf_apply_freestream, &ceed_data->qf_apply_freestream_jacobian));
428 
429   // *****************************************************************************
430   // CEED Operator Apply
431   // *****************************************************************************
432   // -- Apply CEED Operator for the geometric data
433   CeedOperatorApply(ceed_data->op_setup_vol, ceed_data->x_coord, ceed_data->q_data, CEED_REQUEST_IMMEDIATE);
434 
435   // -- Create and apply CEED Composite Operator for the entire domain
436   if (!user->phys->implicit) {  // RHS
437     PetscCall(CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys, user->op_rhs_vol, NULL, height, P_sur, Q_sur, q_data_size_sur, 0,
438                                       &user->op_rhs, NULL));
439   } else {  // IFunction
440     PetscCall(CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys, user->op_ifunction_vol, op_ijacobian_vol, height, P_sur, Q_sur,
441                                       q_data_size_sur, jac_data_size_sur, &user->op_ifunction, op_ijacobian_vol ? &user->op_ijacobian : NULL));
442     if (user->op_ijacobian) {
443       CeedOperatorContextGetFieldLabel(user->op_ijacobian, "ijacobian time shift", &user->phys->ijacobian_time_shift_label);
444     }
445     if (problem->use_dirichlet_ceed) {
446       PetscCall(SetupStrongBC_Ceed(ceed, ceed_data, dm, user, app_ctx, problem, bc, Q_sur, q_data_size_sur));
447     }
448   }
449   CeedElemRestrictionDestroy(&elem_restr_jd_i);
450   CeedOperatorDestroy(&op_ijacobian_vol);
451   CeedVectorDestroy(&jac_data);
452   PetscFunctionReturn(0);
453 }
454