xref: /honee/src/setuplibceed.c (revision 752f40e3cf4e9e12ff9fff9133a2bd9a934f2aaf)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, 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 
11a515125bSLeila Ghaffari #include "../navierstokes.h"
12a515125bSLeila Ghaffari 
13a515125bSLeila Ghaffari // Utility function - essential BC dofs are encoded in closure indices as -(i+1).
14a515125bSLeila Ghaffari PetscInt Involute(PetscInt i) {
15a515125bSLeila Ghaffari   return i >= 0 ? i : -(i+1);
16a515125bSLeila Ghaffari }
17a515125bSLeila Ghaffari 
18a515125bSLeila Ghaffari // Utility function to create local CEED restriction
196b1ccf21SJeremy L Thompson PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height,
206b1ccf21SJeremy L Thompson     DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr) {
216b1ccf21SJeremy L Thompson   PetscInt num_elem, elem_size, num_dof, num_comp, *elem_restr_offsets;
22a515125bSLeila Ghaffari   PetscErrorCode ierr;
236b1ccf21SJeremy L Thompson 
24a515125bSLeila Ghaffari   PetscFunctionBeginUser;
256b1ccf21SJeremy L Thompson   ierr = DMPlexGetLocalOffsets(dm, domain_label, value, height, 0, &num_elem,
266b1ccf21SJeremy L Thompson                                &elem_size, &num_comp, &num_dof, &elem_restr_offsets);
276b1ccf21SJeremy L Thompson   CHKERRQ(ierr);
28a515125bSLeila Ghaffari 
296b1ccf21SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp,
306b1ccf21SJeremy L Thompson                             1, num_dof, CEED_MEM_HOST, CEED_COPY_VALUES,
316b1ccf21SJeremy L Thompson                             elem_restr_offsets, elem_restr);
326b1ccf21SJeremy L Thompson   ierr = PetscFree(elem_restr_offsets); CHKERRQ(ierr);
33a515125bSLeila Ghaffari 
34a515125bSLeila Ghaffari   PetscFunctionReturn(0);
35a515125bSLeila Ghaffari }
36a515125bSLeila Ghaffari 
37a515125bSLeila Ghaffari // Utility function to get Ceed Restriction for each domain
38a515125bSLeila Ghaffari PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
39a515125bSLeila Ghaffari                                        DMLabel domain_label, PetscInt value,
406b1ccf21SJeremy L Thompson                                        CeedInt Q, CeedInt q_data_size,
41a515125bSLeila Ghaffari                                        CeedElemRestriction *elem_restr_q,
42a515125bSLeila Ghaffari                                        CeedElemRestriction *elem_restr_x,
43a515125bSLeila Ghaffari                                        CeedElemRestriction *elem_restr_qd_i) {
44a515125bSLeila Ghaffari   DM             dm_coord;
45a515125bSLeila Ghaffari   CeedInt        dim, loc_num_elem;
46a515125bSLeila Ghaffari   CeedInt        Q_dim;
47395c96d3SJed Brown   CeedElemRestriction elem_restr_tmp;
48a515125bSLeila Ghaffari   PetscErrorCode ierr;
49a515125bSLeila Ghaffari   PetscFunctionBeginUser;
50a515125bSLeila Ghaffari 
51a515125bSLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
52a515125bSLeila Ghaffari   dim -= height;
53a515125bSLeila Ghaffari   Q_dim = CeedIntPow(Q, dim);
54395c96d3SJed Brown   ierr = CreateRestrictionFromPlex(ceed, dm, height, domain_label, value,
55395c96d3SJed Brown                                    &elem_restr_tmp);
56395c96d3SJed Brown   CHKERRQ(ierr);
57395c96d3SJed Brown   if (elem_restr_q) *elem_restr_q = elem_restr_tmp;
58395c96d3SJed Brown   if (elem_restr_x) {
59a515125bSLeila Ghaffari     ierr = DMGetCoordinateDM(dm, &dm_coord); CHKERRQ(ierr);
60a515125bSLeila Ghaffari     ierr = DMPlexSetClosurePermutationTensor(dm_coord, PETSC_DETERMINE, NULL);
61a515125bSLeila Ghaffari     CHKERRQ(ierr);
626b1ccf21SJeremy L Thompson     ierr = CreateRestrictionFromPlex(ceed, dm_coord, height, domain_label, value,
636b1ccf21SJeremy L Thompson                                      elem_restr_x);
646b1ccf21SJeremy L Thompson     CHKERRQ(ierr);
65395c96d3SJed Brown   }
66395c96d3SJed Brown   if (elem_restr_qd_i) {
67395c96d3SJed Brown     CeedElemRestrictionGetNumElements(elem_restr_tmp, &loc_num_elem);
68a515125bSLeila Ghaffari     CeedElemRestrictionCreateStrided(ceed, loc_num_elem, Q_dim,
69a515125bSLeila Ghaffari                                      q_data_size, q_data_size*loc_num_elem*Q_dim,
70a515125bSLeila Ghaffari                                      CEED_STRIDES_BACKEND, elem_restr_qd_i);
71395c96d3SJed Brown   }
72395c96d3SJed Brown   if (!elem_restr_q) CeedElemRestrictionDestroy(&elem_restr_tmp);
73a515125bSLeila Ghaffari   PetscFunctionReturn(0);
74a515125bSLeila Ghaffari }
75a515125bSLeila Ghaffari 
76a515125bSLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
77a515125bSLeila Ghaffari PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
78a515125bSLeila Ghaffari                                        CeedData ceed_data, Physics phys,
79a515125bSLeila Ghaffari                                        CeedOperator op_apply_vol, CeedInt height,
80a515125bSLeila Ghaffari                                        CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur,
81a515125bSLeila Ghaffari                                        CeedOperator *op_apply) {
82002797a3SLeila Ghaffari   //CeedInt        dim;
83a515125bSLeila Ghaffari   DMLabel        domain_label;
84a515125bSLeila Ghaffari   PetscErrorCode ierr;
85a515125bSLeila Ghaffari   PetscFunctionBeginUser;
86a515125bSLeila Ghaffari 
87a515125bSLeila Ghaffari   // Create Composite Operaters
88a515125bSLeila Ghaffari   CeedCompositeOperatorCreate(ceed, op_apply);
89a515125bSLeila Ghaffari 
90a515125bSLeila Ghaffari   // --Apply Sub-Operator for the volume
91a515125bSLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply, op_apply_vol);
92a515125bSLeila Ghaffari 
93a515125bSLeila Ghaffari   // -- Create Sub-Operator for in/outflow BCs
94bb8a0c61SJames Wright   if (phys->has_neumann || 1) {
95a515125bSLeila Ghaffari     // --- Setup
96a515125bSLeila Ghaffari     ierr = DMGetLabel(dm, "Face Sets", &domain_label); CHKERRQ(ierr);
97002797a3SLeila Ghaffari     //ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
98a515125bSLeila Ghaffari 
99a515125bSLeila Ghaffari     // --- Get number of quadrature points for the boundaries
100a515125bSLeila Ghaffari     CeedInt num_qpts_sur;
101a515125bSLeila Ghaffari     CeedBasisGetNumQuadraturePoints(ceed_data->basis_q_sur, &num_qpts_sur);
102a515125bSLeila Ghaffari 
103002797a3SLeila Ghaffari     // --- Create Sub-Operator for inflow boundaries
104002797a3SLeila Ghaffari     for (CeedInt i=0; i < bc->num_inflow; i++) {
105a515125bSLeila Ghaffari       CeedVector          q_data_sur;
106002797a3SLeila Ghaffari       CeedOperator        op_setup_sur, op_apply_inflow;
107002797a3SLeila Ghaffari       CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur;
108a515125bSLeila Ghaffari 
109a515125bSLeila Ghaffari       // ---- CEED Restriction
110002797a3SLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domain_label, bc->inflows[i],
111002797a3SLeila Ghaffari                                      Q_sur, q_data_size_sur, &elem_restr_q_sur, &elem_restr_x_sur,
112002797a3SLeila Ghaffari                                      &elem_restr_qd_i_sur);
113a515125bSLeila Ghaffari       CHKERRQ(ierr);
114a515125bSLeila Ghaffari 
115a515125bSLeila Ghaffari       // ---- CEED Vector
116a515125bSLeila Ghaffari       PetscInt loc_num_elem_sur;
117a515125bSLeila Ghaffari       CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur);
118a515125bSLeila Ghaffari       CeedVectorCreate(ceed, q_data_size_sur*loc_num_elem_sur*num_qpts_sur,
119a515125bSLeila Ghaffari                        &q_data_sur);
120a515125bSLeila Ghaffari 
121a515125bSLeila Ghaffari       // ---- CEED Operator
122a515125bSLeila Ghaffari       // ----- CEED Operator for Setup (geometric factors)
123a515125bSLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur);
124a515125bSLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur,
125139613f2SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE);
126a515125bSLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE,
127a515125bSLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_NONE);
1283b1e209bSJeremy L Thompson       CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur,
129a515125bSLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
130a515125bSLeila Ghaffari 
131a515125bSLeila Ghaffari       // ----- CEED Operator for Physics
132002797a3SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_apply_inflow, NULL, NULL,
133002797a3SLeila Ghaffari                          &op_apply_inflow);
134002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "q", elem_restr_q_sur,
135a515125bSLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
136002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "surface qdata", elem_restr_qd_i_sur,
137a515125bSLeila Ghaffari                            CEED_BASIS_COLLOCATED, q_data_sur);
138002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "x", elem_restr_x_sur,
139a515125bSLeila Ghaffari                            ceed_data->basis_x_sur, ceed_data->x_coord);
140002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "v", elem_restr_q_sur,
141a515125bSLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
142a515125bSLeila Ghaffari 
143a515125bSLeila Ghaffari       // ----- Apply CEED operator for Setup
144a515125bSLeila Ghaffari       CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur,
145a515125bSLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
146a515125bSLeila Ghaffari 
147002797a3SLeila Ghaffari       // ----- Apply Sub-Operator for Physics
148002797a3SLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_apply_inflow);
149a515125bSLeila Ghaffari 
150a515125bSLeila Ghaffari       // ----- Cleanup
151a515125bSLeila Ghaffari       CeedVectorDestroy(&q_data_sur);
152a515125bSLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_q_sur);
153a515125bSLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_x_sur);
154a515125bSLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_qd_i_sur);
155a515125bSLeila Ghaffari       CeedOperatorDestroy(&op_setup_sur);
156002797a3SLeila Ghaffari       CeedOperatorDestroy(&op_apply_inflow);
157a515125bSLeila Ghaffari     }
158a515125bSLeila Ghaffari 
159002797a3SLeila Ghaffari     // --- Create Sub-Operator for outflow boundaries
160002797a3SLeila Ghaffari     for (CeedInt i=0; i < bc->num_outflow; i++) {
161002797a3SLeila Ghaffari       CeedVector          q_data_sur;
162002797a3SLeila Ghaffari       CeedOperator        op_setup_sur, op_apply_outflow;
163002797a3SLeila Ghaffari       CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur;
164002797a3SLeila Ghaffari 
165002797a3SLeila Ghaffari       // ---- CEED Restriction
166002797a3SLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domain_label, bc->outflows[i],
167002797a3SLeila Ghaffari                                      Q_sur, q_data_size_sur, &elem_restr_q_sur, &elem_restr_x_sur,
168002797a3SLeila Ghaffari                                      &elem_restr_qd_i_sur);
169002797a3SLeila Ghaffari       CHKERRQ(ierr);
170002797a3SLeila Ghaffari 
171002797a3SLeila Ghaffari       // ---- CEED Vector
172002797a3SLeila Ghaffari       PetscInt loc_num_elem_sur;
173002797a3SLeila Ghaffari       CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur);
174002797a3SLeila Ghaffari       CeedVectorCreate(ceed, q_data_size_sur*loc_num_elem_sur*num_qpts_sur,
175002797a3SLeila Ghaffari                        &q_data_sur);
176002797a3SLeila Ghaffari 
177002797a3SLeila Ghaffari       // ---- CEED Operator
178002797a3SLeila Ghaffari       // ----- CEED Operator for Setup (geometric factors)
179002797a3SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur);
180002797a3SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur,
181002797a3SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE);
182002797a3SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE,
183002797a3SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_NONE);
184002797a3SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur,
185002797a3SLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
186002797a3SLeila Ghaffari 
187002797a3SLeila Ghaffari       // ----- CEED Operator for Physics
188002797a3SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_apply_outflow, NULL, NULL,
189002797a3SLeila Ghaffari                          &op_apply_outflow);
190002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "q", elem_restr_q_sur,
191002797a3SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
192002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "surface qdata", elem_restr_qd_i_sur,
193002797a3SLeila Ghaffari                            CEED_BASIS_COLLOCATED, q_data_sur);
194002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "x", elem_restr_x_sur,
195002797a3SLeila Ghaffari                            ceed_data->basis_x_sur, ceed_data->x_coord);
196002797a3SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "v", elem_restr_q_sur,
197002797a3SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
198002797a3SLeila Ghaffari 
199002797a3SLeila Ghaffari       // ----- Apply CEED operator for Setup
200002797a3SLeila Ghaffari       CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur,
201002797a3SLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
202002797a3SLeila Ghaffari 
203002797a3SLeila Ghaffari       // ----- Apply Sub-Operator for Physics
204002797a3SLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_apply_outflow);
205002797a3SLeila Ghaffari 
206002797a3SLeila Ghaffari       // ----- Cleanup
207002797a3SLeila Ghaffari       CeedVectorDestroy(&q_data_sur);
208002797a3SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_q_sur);
209002797a3SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_x_sur);
210002797a3SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_qd_i_sur);
211002797a3SLeila Ghaffari       CeedOperatorDestroy(&op_setup_sur);
212002797a3SLeila Ghaffari       CeedOperatorDestroy(&op_apply_outflow);
213002797a3SLeila Ghaffari     }
214002797a3SLeila Ghaffari   }
21592ada588SJames Wright 
21692ada588SJames Wright   // ----- Get Context Labels for Operator
21792ada588SJames Wright   CeedOperatorContextGetFieldLabel(*op_apply, "solution time",
21892ada588SJames Wright                                    &phys->solution_time_label);
219bb8a0c61SJames Wright   CeedOperatorContextGetFieldLabel(*op_apply, "timestep size",
220bb8a0c61SJames Wright                                    &phys->timestep_size_label);
22192ada588SJames Wright 
222a515125bSLeila Ghaffari   PetscFunctionReturn(0);
223a515125bSLeila Ghaffari }
224a515125bSLeila Ghaffari 
225a515125bSLeila Ghaffari PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user,
226c848b8b7SJed Brown                             AppCtx app_ctx, ProblemData *problem, SimpleBC bc) {
227a515125bSLeila Ghaffari   PetscErrorCode ierr;
228a515125bSLeila Ghaffari   PetscFunctionBeginUser;
229a515125bSLeila Ghaffari 
230a515125bSLeila Ghaffari   // *****************************************************************************
231a515125bSLeila Ghaffari   // Set up CEED objects for the interior domain (volume)
232a515125bSLeila Ghaffari   // *****************************************************************************
233a515125bSLeila Ghaffari   const PetscInt num_comp_q      = 5;
234a515125bSLeila Ghaffari   const CeedInt  dim             = problem->dim,
235a515125bSLeila Ghaffari                  num_comp_x      = problem->dim,
236a515125bSLeila Ghaffari                  q_data_size_vol = problem->q_data_size_vol,
237*752f40e3SJed Brown                  jac_data_size_vol = num_comp_q + 3,
238a515125bSLeila Ghaffari                  P               = app_ctx->degree + 1,
239a515125bSLeila Ghaffari                  Q               = P + app_ctx->q_extra;
240*752f40e3SJed Brown   CeedElemRestriction elem_restr_jd_i;
241*752f40e3SJed Brown   CeedVector jac_data;
242a515125bSLeila Ghaffari 
243a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
244a515125bSLeila Ghaffari   // CEED Bases
245a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
246a515125bSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_q, P, Q, CEED_GAUSS,
247a515125bSLeila Ghaffari                                   &ceed_data->basis_q);
248a515125bSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, Q, CEED_GAUSS,
249a515125bSLeila Ghaffari                                   &ceed_data->basis_x);
250a515125bSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, P,
251a515125bSLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &ceed_data->basis_xc);
252a515125bSLeila Ghaffari 
253a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
254a515125bSLeila Ghaffari   // CEED Restrictions
255a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
256a515125bSLeila Ghaffari   // -- Create restriction
2576b1ccf21SJeremy L Thompson   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, q_data_size_vol,
2586b1ccf21SJeremy L Thompson                                  &ceed_data->elem_restr_q, &ceed_data->elem_restr_x,
259a515125bSLeila Ghaffari                                  &ceed_data->elem_restr_qd_i); CHKERRQ(ierr);
260*752f40e3SJed Brown 
261*752f40e3SJed Brown   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, jac_data_size_vol,
262*752f40e3SJed Brown                                  NULL, NULL,
263*752f40e3SJed Brown                                  &elem_restr_jd_i); CHKERRQ(ierr);
264a515125bSLeila Ghaffari // -- Create E vectors
265a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_ceed, NULL);
266a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_dot_ceed,
267a515125bSLeila Ghaffari                                   NULL);
268a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->g_ceed, NULL);
269a515125bSLeila Ghaffari 
270a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
271a515125bSLeila Ghaffari   // CEED QFunctions
272a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
273a515125bSLeila Ghaffari   // -- Create QFunction for quadrature data
2749785fe93SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->setup_vol.qfunction,
2759785fe93SJed Brown                               problem->setup_vol.qfunction_loc,
276a515125bSLeila Ghaffari                               &ceed_data->qf_setup_vol);
27715a3537eSJed Brown   if (problem->setup_vol.qfunction_context) {
27815a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_setup_vol,
27915a3537eSJed Brown                             problem->setup_vol.qfunction_context);
28015a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->setup_vol.qfunction_context);
28115a3537eSJed Brown   }
282a515125bSLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "dx", num_comp_x*dim,
283a515125bSLeila Ghaffari                         CEED_EVAL_GRAD);
284a515125bSLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "weight", 1, CEED_EVAL_WEIGHT);
2853b1e209bSJeremy L Thompson   CeedQFunctionAddOutput(ceed_data->qf_setup_vol, "qdata", q_data_size_vol,
286a515125bSLeila Ghaffari                          CEED_EVAL_NONE);
287a515125bSLeila Ghaffari 
288a515125bSLeila Ghaffari   // -- Create QFunction for ICs
2899785fe93SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->ics.qfunction,
2909785fe93SJed Brown                               problem->ics.qfunction_loc,
291a515125bSLeila Ghaffari                               &ceed_data->qf_ics);
29215a3537eSJed Brown   CeedQFunctionSetContext(ceed_data->qf_ics, problem->ics.qfunction_context);
29315a3537eSJed Brown   CeedQFunctionContextDestroy(&problem->ics.qfunction_context);
294a515125bSLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_ics, "x", num_comp_x, CEED_EVAL_INTERP);
295a515125bSLeila Ghaffari   CeedQFunctionAddOutput(ceed_data->qf_ics, "q0", num_comp_q, CEED_EVAL_NONE);
296a515125bSLeila Ghaffari 
297a515125bSLeila Ghaffari   // -- Create QFunction for RHS
2989785fe93SJed Brown   if (problem->apply_vol_rhs.qfunction) {
2999785fe93SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_rhs.qfunction,
3009785fe93SJed Brown                                 problem->apply_vol_rhs.qfunction_loc, &ceed_data->qf_rhs_vol);
30115a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_rhs_vol,
30215a3537eSJed Brown                             problem->apply_vol_rhs.qfunction_context);
30315a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfunction_context);
304a515125bSLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "q", num_comp_q, CEED_EVAL_INTERP);
305*752f40e3SJed Brown     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "Grad_q", num_comp_q*dim,
306a515125bSLeila Ghaffari                           CEED_EVAL_GRAD);
3073b1e209bSJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "qdata", q_data_size_vol,
308a515125bSLeila Ghaffari                           CEED_EVAL_NONE);
309a515125bSLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "x", num_comp_x, CEED_EVAL_INTERP);
310a515125bSLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "v", num_comp_q,
311a515125bSLeila Ghaffari                            CEED_EVAL_INTERP);
312*752f40e3SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "Grad_v", num_comp_q*dim,
313a515125bSLeila Ghaffari                            CEED_EVAL_GRAD);
314a515125bSLeila Ghaffari   }
315a515125bSLeila Ghaffari 
316a515125bSLeila Ghaffari   // -- Create QFunction for IFunction
3179785fe93SJed Brown   if (problem->apply_vol_ifunction.qfunction) {
3189785fe93SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ifunction.qfunction,
3199785fe93SJed Brown                                 problem->apply_vol_ifunction.qfunction_loc, &ceed_data->qf_ifunction_vol);
32015a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
32115a3537eSJed Brown                             problem->apply_vol_ifunction.qfunction_context);
32215a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfunction_context);
323a515125bSLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q", num_comp_q,
324a515125bSLeila Ghaffari                           CEED_EVAL_INTERP);
325*752f40e3SJed Brown     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "Grad_q", num_comp_q*dim,
326a515125bSLeila Ghaffari                           CEED_EVAL_GRAD);
3273b1e209bSJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q dot", num_comp_q,
328a515125bSLeila Ghaffari                           CEED_EVAL_INTERP);
3293b1e209bSJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "qdata", q_data_size_vol,
330a515125bSLeila Ghaffari                           CEED_EVAL_NONE);
331a515125bSLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "x", num_comp_x,
332a515125bSLeila Ghaffari                           CEED_EVAL_INTERP);
333a515125bSLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "v", num_comp_q,
334a515125bSLeila Ghaffari                            CEED_EVAL_INTERP);
335*752f40e3SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "Grad_v", num_comp_q*dim,
336a515125bSLeila Ghaffari                            CEED_EVAL_GRAD);
337*752f40e3SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "jac_data",
338*752f40e3SJed Brown                            jac_data_size_vol, CEED_EVAL_NONE);
339a515125bSLeila Ghaffari   }
340a515125bSLeila Ghaffari 
341a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
342a515125bSLeila Ghaffari   // Element coordinates
343a515125bSLeila Ghaffari   // ---------------------------------------------------------------------------
344a515125bSLeila Ghaffari   // -- Create CEED vector
345a515125bSLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_x, &ceed_data->x_coord,
346a515125bSLeila Ghaffari                                   NULL);
347a515125bSLeila Ghaffari 
348a515125bSLeila Ghaffari   // -- Copy PETSc vector in CEED vector
349a515125bSLeila Ghaffari   Vec               X_loc;
350a515125bSLeila Ghaffari   const PetscScalar *X_loc_array;
351a515125bSLeila Ghaffari   ierr = DMGetCoordinatesLocal(dm, &X_loc); CHKERRQ(ierr);
35205a512bdSLeila Ghaffari   ierr = VecScale(X_loc, problem->dm_scale); CHKERRQ(ierr);
353a515125bSLeila Ghaffari   ierr = VecGetArrayRead(X_loc, &X_loc_array); CHKERRQ(ierr);
354a515125bSLeila Ghaffari   CeedVectorSetArray(ceed_data->x_coord, CEED_MEM_HOST, CEED_COPY_VALUES,
355a515125bSLeila Ghaffari                      (PetscScalar *)X_loc_array);
356a515125bSLeila Ghaffari   ierr = VecRestoreArrayRead(X_loc, &X_loc_array); CHKERRQ(ierr);
357a515125bSLeila Ghaffari 
358a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
359a515125bSLeila Ghaffari   // CEED vectors
360a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
361a515125bSLeila Ghaffari   // -- Create CEED vector for geometric data
362a515125bSLeila Ghaffari   CeedInt  num_qpts_vol;
363a515125bSLeila Ghaffari   PetscInt loc_num_elem_vol;
364a515125bSLeila Ghaffari   CeedBasisGetNumQuadraturePoints(ceed_data->basis_q, &num_qpts_vol);
365a515125bSLeila Ghaffari   CeedElemRestrictionGetNumElements(ceed_data->elem_restr_q, &loc_num_elem_vol);
366a515125bSLeila Ghaffari   CeedVectorCreate(ceed, q_data_size_vol*loc_num_elem_vol*num_qpts_vol,
367a515125bSLeila Ghaffari                    &ceed_data->q_data);
368a515125bSLeila Ghaffari 
369*752f40e3SJed Brown   CeedElemRestrictionCreateVector(elem_restr_jd_i, &jac_data, NULL);
370a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
371a515125bSLeila Ghaffari   // CEED Operators
372a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
373a515125bSLeila Ghaffari   // -- Create CEED operator for quadrature data
374a515125bSLeila Ghaffari   CeedOperatorCreate(ceed, ceed_data->qf_setup_vol, NULL, NULL,
375a515125bSLeila Ghaffari                      &ceed_data->op_setup_vol);
376a515125bSLeila Ghaffari   CeedOperatorSetField(ceed_data->op_setup_vol, "dx", ceed_data->elem_restr_x,
377a515125bSLeila Ghaffari                        ceed_data->basis_x, CEED_VECTOR_ACTIVE);
378a515125bSLeila Ghaffari   CeedOperatorSetField(ceed_data->op_setup_vol, "weight",
379139613f2SLeila Ghaffari                        CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x, CEED_VECTOR_NONE);
3803b1e209bSJeremy L Thompson   CeedOperatorSetField(ceed_data->op_setup_vol, "qdata",
381139613f2SLeila Ghaffari                        ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
382a515125bSLeila Ghaffari 
383a515125bSLeila Ghaffari   // -- Create CEED operator for ICs
384a515125bSLeila Ghaffari   CeedOperatorCreate(ceed, ceed_data->qf_ics, NULL, NULL, &ceed_data->op_ics);
385a515125bSLeila Ghaffari   CeedOperatorSetField(ceed_data->op_ics, "x", ceed_data->elem_restr_x,
386a515125bSLeila Ghaffari                        ceed_data->basis_xc, CEED_VECTOR_ACTIVE);
387a515125bSLeila Ghaffari   CeedOperatorSetField(ceed_data->op_ics, "q0", ceed_data->elem_restr_q,
388a515125bSLeila Ghaffari                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
38915a3537eSJed Brown   CeedOperatorContextGetFieldLabel(ceed_data->op_ics, "evaluation time",
39015a3537eSJed Brown                                    &user->phys->ics_time_label);
391a515125bSLeila Ghaffari 
392a515125bSLeila Ghaffari   // Create CEED operator for RHS
393a515125bSLeila Ghaffari   if (ceed_data->qf_rhs_vol) {
394a515125bSLeila Ghaffari     CeedOperator op;
395a515125bSLeila Ghaffari     CeedOperatorCreate(ceed, ceed_data->qf_rhs_vol, NULL, NULL, &op);
396a515125bSLeila Ghaffari     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q,
397a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
398*752f40e3SJed Brown     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q,
399a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
4003b1e209bSJeremy L Thompson     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i,
401139613f2SLeila Ghaffari                          CEED_BASIS_COLLOCATED, ceed_data->q_data);
402a515125bSLeila Ghaffari     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x,
403a515125bSLeila Ghaffari                          ceed_data->x_coord);
404a515125bSLeila Ghaffari     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q,
405a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
406*752f40e3SJed Brown     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q,
407a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
408a515125bSLeila Ghaffari     user->op_rhs_vol = op;
409a515125bSLeila Ghaffari   }
410a515125bSLeila Ghaffari 
411a515125bSLeila Ghaffari   // -- CEED operator for IFunction
412a515125bSLeila Ghaffari   if (ceed_data->qf_ifunction_vol) {
413a515125bSLeila Ghaffari     CeedOperator op;
414a515125bSLeila Ghaffari     CeedOperatorCreate(ceed, ceed_data->qf_ifunction_vol, NULL, NULL, &op);
415a515125bSLeila Ghaffari     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q,
416a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
417*752f40e3SJed Brown     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q,
418a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
4193b1e209bSJeremy L Thompson     CeedOperatorSetField(op, "q dot", ceed_data->elem_restr_q, ceed_data->basis_q,
420a515125bSLeila Ghaffari                          user->q_dot_ceed);
4213b1e209bSJeremy L Thompson     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i,
422139613f2SLeila Ghaffari                          CEED_BASIS_COLLOCATED, ceed_data->q_data);
423a515125bSLeila Ghaffari     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x,
424a515125bSLeila Ghaffari                          ceed_data->x_coord);
425a515125bSLeila Ghaffari     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q,
426a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
427*752f40e3SJed Brown     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q,
428a515125bSLeila Ghaffari                          CEED_VECTOR_ACTIVE);
429*752f40e3SJed Brown     CeedOperatorSetField(op, "jac_data", elem_restr_jd_i,
430*752f40e3SJed Brown                          CEED_BASIS_COLLOCATED, jac_data);
431*752f40e3SJed Brown 
432a515125bSLeila Ghaffari     user->op_ifunction_vol = op;
433a515125bSLeila Ghaffari   }
434a515125bSLeila Ghaffari 
435a515125bSLeila Ghaffari   // *****************************************************************************
436a515125bSLeila Ghaffari   // Set up CEED objects for the exterior domain (surface)
437a515125bSLeila Ghaffari   // *****************************************************************************
438a515125bSLeila Ghaffari   CeedInt height  = 1,
439a515125bSLeila Ghaffari           dim_sur = dim - height,
440a515125bSLeila Ghaffari           P_sur   = app_ctx->degree + 1,
441a515125bSLeila Ghaffari           Q_sur   = P_sur + app_ctx->q_extra;
442a515125bSLeila Ghaffari   const CeedInt q_data_size_sur = problem->q_data_size_sur;
443a515125bSLeila Ghaffari 
444a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
445a515125bSLeila Ghaffari   // CEED Bases
446a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
447a515125bSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_q, P_sur, Q_sur,
448a515125bSLeila Ghaffari                                   CEED_GAUSS, &ceed_data->basis_q_sur);
449a515125bSLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_x, 2, Q_sur, CEED_GAUSS,
450a515125bSLeila Ghaffari                                   &ceed_data->basis_x_sur);
451a515125bSLeila Ghaffari 
452a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
453a515125bSLeila Ghaffari   // CEED QFunctions
454a515125bSLeila Ghaffari   // -----------------------------------------------------------------------------
455a515125bSLeila Ghaffari   // -- Create QFunction for quadrature data
4569785fe93SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->setup_sur.qfunction,
4579785fe93SJed Brown                               problem->setup_sur.qfunction_loc,
458a515125bSLeila Ghaffari                               &ceed_data->qf_setup_sur);
45915a3537eSJed Brown   if (problem->setup_sur.qfunction_context) {
46015a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_setup_sur,
46115a3537eSJed Brown                             problem->setup_sur.qfunction_context);
46215a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->setup_sur.qfunction_context);
46315a3537eSJed Brown   }
464a515125bSLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "dx", num_comp_x*dim_sur,
465a515125bSLeila Ghaffari                         CEED_EVAL_GRAD);
466a515125bSLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "weight", 1, CEED_EVAL_WEIGHT);
4673b1e209bSJeremy L Thompson   CeedQFunctionAddOutput(ceed_data->qf_setup_sur, "surface qdata",
468002797a3SLeila Ghaffari                          q_data_size_sur, CEED_EVAL_NONE);
469a515125bSLeila Ghaffari 
470002797a3SLeila Ghaffari   // -- Creat QFunction for inflow boundaries
4719785fe93SJed Brown   if (problem->apply_inflow.qfunction) {
4729785fe93SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_inflow.qfunction,
4739785fe93SJed Brown                                 problem->apply_inflow.qfunction_loc, &ceed_data->qf_apply_inflow);
47415a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_apply_inflow,
47515a3537eSJed Brown                             problem->apply_inflow.qfunction_context);
47615a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->apply_inflow.qfunction_context);
477002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "q", num_comp_q,
478a515125bSLeila Ghaffari                           CEED_EVAL_INTERP);
479002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "surface qdata",
480002797a3SLeila Ghaffari                           q_data_size_sur, CEED_EVAL_NONE);
481002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "x", num_comp_x,
482a515125bSLeila Ghaffari                           CEED_EVAL_INTERP);
483002797a3SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_apply_inflow, "v", num_comp_q,
484002797a3SLeila Ghaffari                            CEED_EVAL_INTERP);
485002797a3SLeila Ghaffari   }
486002797a3SLeila Ghaffari 
487002797a3SLeila Ghaffari   // -- Creat QFunction for outflow boundaries
4889785fe93SJed Brown   if (problem->apply_outflow.qfunction) {
4899785fe93SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_outflow.qfunction,
4909785fe93SJed Brown                                 problem->apply_outflow.qfunction_loc, &ceed_data->qf_apply_outflow);
49115a3537eSJed Brown     CeedQFunctionSetContext(ceed_data->qf_apply_outflow,
49215a3537eSJed Brown                             problem->apply_outflow.qfunction_context);
49315a3537eSJed Brown     CeedQFunctionContextDestroy(&problem->apply_outflow.qfunction_context);
494002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "q", num_comp_q,
495002797a3SLeila Ghaffari                           CEED_EVAL_INTERP);
496002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "surface qdata",
497002797a3SLeila Ghaffari                           q_data_size_sur, CEED_EVAL_NONE);
498002797a3SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "x", num_comp_x,
499002797a3SLeila Ghaffari                           CEED_EVAL_INTERP);
500002797a3SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_apply_outflow, "v", num_comp_q,
501a515125bSLeila Ghaffari                            CEED_EVAL_INTERP);
502a515125bSLeila Ghaffari   }
503a515125bSLeila Ghaffari 
504a515125bSLeila Ghaffari   // *****************************************************************************
505a515125bSLeila Ghaffari   // CEED Operator Apply
506a515125bSLeila Ghaffari   // *****************************************************************************
507a515125bSLeila Ghaffari   // -- Apply CEED Operator for the geometric data
508a515125bSLeila Ghaffari   CeedOperatorApply(ceed_data->op_setup_vol, ceed_data->x_coord,
509a515125bSLeila Ghaffari                     ceed_data->q_data, CEED_REQUEST_IMMEDIATE);
510a515125bSLeila Ghaffari 
511a515125bSLeila Ghaffari   // -- Create and apply CEED Composite Operator for the entire domain
512a515125bSLeila Ghaffari   if (!user->phys->implicit) { // RHS
513a515125bSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys,
514a515125bSLeila Ghaffari                                    user->op_rhs_vol, height, P_sur, Q_sur,
515a515125bSLeila Ghaffari                                    q_data_size_sur, &user->op_rhs); CHKERRQ(ierr);
516a515125bSLeila Ghaffari   } else { // IFunction
517a515125bSLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys,
518a515125bSLeila Ghaffari                                    user->op_ifunction_vol, height, P_sur, Q_sur,
519a515125bSLeila Ghaffari                                    q_data_size_sur, &user->op_ifunction); CHKERRQ(ierr);
520a515125bSLeila Ghaffari   }
521*752f40e3SJed Brown   CeedElemRestrictionDestroy(&elem_restr_jd_i);
522*752f40e3SJed Brown   CeedVectorDestroy(&jac_data);
523a515125bSLeila Ghaffari   PetscFunctionReturn(0);
524a515125bSLeila Ghaffari }
525