xref: /libCEED/examples/fluids/src/setuplibceed.c (revision a3ae0734a04ace7b99bbe186ac606878fa3ae395)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 
1177841947SLeila Ghaffari #include "../navierstokes.h"
1277841947SLeila Ghaffari 
1377841947SLeila Ghaffari // Utility function - essential BC dofs are encoded in closure indices as -(i+1).
1477841947SLeila Ghaffari PetscInt Involute(PetscInt i) {
1577841947SLeila Ghaffari   return i >= 0 ? i : -(i+1);
1677841947SLeila Ghaffari }
1777841947SLeila Ghaffari 
1877841947SLeila Ghaffari // Utility function to create local CEED restriction
197ed3e4cdSJeremy L Thompson PetscErrorCode CreateRestrictionFromPlex(Ceed ceed, DM dm, CeedInt height,
207ed3e4cdSJeremy L Thompson     DMLabel domain_label, CeedInt value, CeedElemRestriction *elem_restr) {
217ed3e4cdSJeremy L Thompson   PetscInt num_elem, elem_size, num_dof, num_comp, *elem_restr_offsets;
2277841947SLeila Ghaffari   PetscErrorCode ierr;
237ed3e4cdSJeremy L Thompson 
2477841947SLeila Ghaffari   PetscFunctionBeginUser;
257ed3e4cdSJeremy L Thompson   ierr = DMPlexGetLocalOffsets(dm, domain_label, value, height, 0, &num_elem,
267ed3e4cdSJeremy L Thompson                                &elem_size, &num_comp, &num_dof, &elem_restr_offsets);
277ed3e4cdSJeremy L Thompson   CHKERRQ(ierr);
2877841947SLeila Ghaffari 
297ed3e4cdSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, elem_size, num_comp,
307ed3e4cdSJeremy L Thompson                             1, num_dof, CEED_MEM_HOST, CEED_COPY_VALUES,
317ed3e4cdSJeremy L Thompson                             elem_restr_offsets, elem_restr);
327ed3e4cdSJeremy L Thompson   ierr = PetscFree(elem_restr_offsets); CHKERRQ(ierr);
3377841947SLeila Ghaffari 
3477841947SLeila Ghaffari   PetscFunctionReturn(0);
3577841947SLeila Ghaffari }
3677841947SLeila Ghaffari 
3777841947SLeila Ghaffari // Utility function to get Ceed Restriction for each domain
3877841947SLeila Ghaffari PetscErrorCode GetRestrictionForDomain(Ceed ceed, DM dm, CeedInt height,
3977841947SLeila Ghaffari                                        DMLabel domain_label, PetscInt value,
407ed3e4cdSJeremy L Thompson                                        CeedInt Q, CeedInt q_data_size,
4177841947SLeila Ghaffari                                        CeedElemRestriction *elem_restr_q,
4277841947SLeila Ghaffari                                        CeedElemRestriction *elem_restr_x,
4377841947SLeila Ghaffari                                        CeedElemRestriction *elem_restr_qd_i) {
4477841947SLeila Ghaffari   DM             dm_coord;
4577841947SLeila Ghaffari   CeedInt        dim, loc_num_elem;
4677841947SLeila Ghaffari   CeedInt        Q_dim;
472534dcc8SJed Brown   CeedElemRestriction elem_restr_tmp;
4877841947SLeila Ghaffari   PetscErrorCode ierr;
4977841947SLeila Ghaffari   PetscFunctionBeginUser;
5077841947SLeila Ghaffari 
5177841947SLeila Ghaffari   ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
5277841947SLeila Ghaffari   dim -= height;
5377841947SLeila Ghaffari   Q_dim = CeedIntPow(Q, dim);
542534dcc8SJed Brown   ierr = CreateRestrictionFromPlex(ceed, dm, height, domain_label, value,
552534dcc8SJed Brown                                    &elem_restr_tmp);
562534dcc8SJed Brown   CHKERRQ(ierr);
572534dcc8SJed Brown   if (elem_restr_q) *elem_restr_q = elem_restr_tmp;
582534dcc8SJed Brown   if (elem_restr_x) {
5977841947SLeila Ghaffari     ierr = DMGetCoordinateDM(dm, &dm_coord); CHKERRQ(ierr);
6077841947SLeila Ghaffari     ierr = DMPlexSetClosurePermutationTensor(dm_coord, PETSC_DETERMINE, NULL);
6177841947SLeila Ghaffari     CHKERRQ(ierr);
627ed3e4cdSJeremy L Thompson     ierr = CreateRestrictionFromPlex(ceed, dm_coord, height, domain_label, value,
637ed3e4cdSJeremy L Thompson                                      elem_restr_x);
647ed3e4cdSJeremy L Thompson     CHKERRQ(ierr);
652534dcc8SJed Brown   }
662534dcc8SJed Brown   if (elem_restr_qd_i) {
672534dcc8SJed Brown     CeedElemRestrictionGetNumElements(elem_restr_tmp, &loc_num_elem);
6877841947SLeila Ghaffari     CeedElemRestrictionCreateStrided(ceed, loc_num_elem, Q_dim,
6977841947SLeila Ghaffari                                      q_data_size, q_data_size*loc_num_elem*Q_dim,
7077841947SLeila Ghaffari                                      CEED_STRIDES_BACKEND, elem_restr_qd_i);
712534dcc8SJed Brown   }
722534dcc8SJed Brown   if (!elem_restr_q) CeedElemRestrictionDestroy(&elem_restr_tmp);
7377841947SLeila Ghaffari   PetscFunctionReturn(0);
7477841947SLeila Ghaffari }
7577841947SLeila Ghaffari 
7677841947SLeila Ghaffari // Utility function to create CEED Composite Operator for the entire domain
7777841947SLeila Ghaffari PetscErrorCode CreateOperatorForDomain(Ceed ceed, DM dm, SimpleBC bc,
7877841947SLeila Ghaffari                                        CeedData ceed_data, Physics phys,
7977841947SLeila Ghaffari                                        CeedOperator op_apply_vol, CeedInt height,
8077841947SLeila Ghaffari                                        CeedInt P_sur, CeedInt Q_sur, CeedInt q_data_size_sur,
8177841947SLeila Ghaffari                                        CeedOperator *op_apply) {
822fe7aee7SLeila Ghaffari   //CeedInt        dim;
8377841947SLeila Ghaffari   DMLabel        domain_label;
8477841947SLeila Ghaffari   PetscErrorCode ierr;
8577841947SLeila Ghaffari   PetscFunctionBeginUser;
8677841947SLeila Ghaffari 
8777841947SLeila Ghaffari   // Create Composite Operaters
8877841947SLeila Ghaffari   CeedCompositeOperatorCreate(ceed, op_apply);
8977841947SLeila Ghaffari 
9077841947SLeila Ghaffari   // --Apply Sub-Operator for the volume
9177841947SLeila Ghaffari   CeedCompositeOperatorAddSub(*op_apply, op_apply_vol);
9277841947SLeila Ghaffari 
9377841947SLeila Ghaffari   // -- Create Sub-Operator for in/outflow BCs
9488626eedSJames Wright   if (phys->has_neumann || 1) {
9577841947SLeila Ghaffari     // --- Setup
9677841947SLeila Ghaffari     ierr = DMGetLabel(dm, "Face Sets", &domain_label); CHKERRQ(ierr);
972fe7aee7SLeila Ghaffari     //ierr = DMGetDimension(dm, &dim); CHKERRQ(ierr);
9877841947SLeila Ghaffari 
9977841947SLeila Ghaffari     // --- Get number of quadrature points for the boundaries
10077841947SLeila Ghaffari     CeedInt num_qpts_sur;
10177841947SLeila Ghaffari     CeedBasisGetNumQuadraturePoints(ceed_data->basis_q_sur, &num_qpts_sur);
10277841947SLeila Ghaffari 
1032fe7aee7SLeila Ghaffari     // --- Create Sub-Operator for inflow boundaries
1042fe7aee7SLeila Ghaffari     for (CeedInt i=0; i < bc->num_inflow; i++) {
10577841947SLeila Ghaffari       CeedVector          q_data_sur;
1062fe7aee7SLeila Ghaffari       CeedOperator        op_setup_sur, op_apply_inflow;
1072fe7aee7SLeila Ghaffari       CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur;
10877841947SLeila Ghaffari 
10977841947SLeila Ghaffari       // ---- CEED Restriction
1102fe7aee7SLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domain_label, bc->inflows[i],
1112fe7aee7SLeila Ghaffari                                      Q_sur, q_data_size_sur, &elem_restr_q_sur, &elem_restr_x_sur,
1122fe7aee7SLeila Ghaffari                                      &elem_restr_qd_i_sur);
11377841947SLeila Ghaffari       CHKERRQ(ierr);
11477841947SLeila Ghaffari 
11577841947SLeila Ghaffari       // ---- CEED Vector
11677841947SLeila Ghaffari       PetscInt loc_num_elem_sur;
11777841947SLeila Ghaffari       CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur);
11877841947SLeila Ghaffari       CeedVectorCreate(ceed, q_data_size_sur*loc_num_elem_sur*num_qpts_sur,
11977841947SLeila Ghaffari                        &q_data_sur);
12077841947SLeila Ghaffari 
12177841947SLeila Ghaffari       // ---- CEED Operator
12277841947SLeila Ghaffari       // ----- CEED Operator for Setup (geometric factors)
12377841947SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur);
12477841947SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur,
125e6225c47SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE);
12677841947SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE,
12777841947SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_NONE);
128a61c78d6SJeremy L Thompson       CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur,
12977841947SLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
13077841947SLeila Ghaffari 
13177841947SLeila Ghaffari       // ----- CEED Operator for Physics
1322fe7aee7SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_apply_inflow, NULL, NULL,
1332fe7aee7SLeila Ghaffari                          &op_apply_inflow);
1342fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "q", elem_restr_q_sur,
13577841947SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
1362fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "surface qdata", elem_restr_qd_i_sur,
13777841947SLeila Ghaffari                            CEED_BASIS_COLLOCATED, q_data_sur);
1382fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "x", elem_restr_x_sur,
13977841947SLeila Ghaffari                            ceed_data->basis_x_sur, ceed_data->x_coord);
1402fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_inflow, "v", elem_restr_q_sur,
14177841947SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
14277841947SLeila Ghaffari 
14377841947SLeila Ghaffari       // ----- Apply CEED operator for Setup
14477841947SLeila Ghaffari       CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur,
14577841947SLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
14677841947SLeila Ghaffari 
1472fe7aee7SLeila Ghaffari       // ----- Apply Sub-Operator for Physics
1482fe7aee7SLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_apply_inflow);
14977841947SLeila Ghaffari 
15077841947SLeila Ghaffari       // ----- Cleanup
15177841947SLeila Ghaffari       CeedVectorDestroy(&q_data_sur);
15277841947SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_q_sur);
15377841947SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_x_sur);
15477841947SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_qd_i_sur);
15577841947SLeila Ghaffari       CeedOperatorDestroy(&op_setup_sur);
1562fe7aee7SLeila Ghaffari       CeedOperatorDestroy(&op_apply_inflow);
15777841947SLeila Ghaffari     }
15877841947SLeila Ghaffari 
1592fe7aee7SLeila Ghaffari     // --- Create Sub-Operator for outflow boundaries
1602fe7aee7SLeila Ghaffari     for (CeedInt i=0; i < bc->num_outflow; i++) {
1612fe7aee7SLeila Ghaffari       CeedVector          q_data_sur;
1622fe7aee7SLeila Ghaffari       CeedOperator        op_setup_sur, op_apply_outflow;
1632fe7aee7SLeila Ghaffari       CeedElemRestriction elem_restr_x_sur, elem_restr_q_sur, elem_restr_qd_i_sur;
1642fe7aee7SLeila Ghaffari 
1652fe7aee7SLeila Ghaffari       // ---- CEED Restriction
1662fe7aee7SLeila Ghaffari       ierr = GetRestrictionForDomain(ceed, dm, height, domain_label, bc->outflows[i],
1672fe7aee7SLeila Ghaffari                                      Q_sur, q_data_size_sur, &elem_restr_q_sur, &elem_restr_x_sur,
1682fe7aee7SLeila Ghaffari                                      &elem_restr_qd_i_sur);
1692fe7aee7SLeila Ghaffari       CHKERRQ(ierr);
1702fe7aee7SLeila Ghaffari 
1712fe7aee7SLeila Ghaffari       // ---- CEED Vector
1722fe7aee7SLeila Ghaffari       PetscInt loc_num_elem_sur;
1732fe7aee7SLeila Ghaffari       CeedElemRestrictionGetNumElements(elem_restr_q_sur, &loc_num_elem_sur);
1742fe7aee7SLeila Ghaffari       CeedVectorCreate(ceed, q_data_size_sur*loc_num_elem_sur*num_qpts_sur,
1752fe7aee7SLeila Ghaffari                        &q_data_sur);
1762fe7aee7SLeila Ghaffari 
1772fe7aee7SLeila Ghaffari       // ---- CEED Operator
1782fe7aee7SLeila Ghaffari       // ----- CEED Operator for Setup (geometric factors)
1792fe7aee7SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_setup_sur, NULL, NULL, &op_setup_sur);
1802fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "dx", elem_restr_x_sur,
1812fe7aee7SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_ACTIVE);
1822fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "weight", CEED_ELEMRESTRICTION_NONE,
1832fe7aee7SLeila Ghaffari                            ceed_data->basis_x_sur, CEED_VECTOR_NONE);
1842fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_setup_sur, "surface qdata", elem_restr_qd_i_sur,
1852fe7aee7SLeila Ghaffari                            CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
1862fe7aee7SLeila Ghaffari 
1872fe7aee7SLeila Ghaffari       // ----- CEED Operator for Physics
1882fe7aee7SLeila Ghaffari       CeedOperatorCreate(ceed, ceed_data->qf_apply_outflow, NULL, NULL,
1892fe7aee7SLeila Ghaffari                          &op_apply_outflow);
1902fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "q", elem_restr_q_sur,
1912fe7aee7SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
1922fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "surface qdata", elem_restr_qd_i_sur,
1932fe7aee7SLeila Ghaffari                            CEED_BASIS_COLLOCATED, q_data_sur);
1942fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "x", elem_restr_x_sur,
1952fe7aee7SLeila Ghaffari                            ceed_data->basis_x_sur, ceed_data->x_coord);
1962fe7aee7SLeila Ghaffari       CeedOperatorSetField(op_apply_outflow, "v", elem_restr_q_sur,
1972fe7aee7SLeila Ghaffari                            ceed_data->basis_q_sur, CEED_VECTOR_ACTIVE);
1982fe7aee7SLeila Ghaffari 
1992fe7aee7SLeila Ghaffari       // ----- Apply CEED operator for Setup
2002fe7aee7SLeila Ghaffari       CeedOperatorApply(op_setup_sur, ceed_data->x_coord, q_data_sur,
2012fe7aee7SLeila Ghaffari                         CEED_REQUEST_IMMEDIATE);
2022fe7aee7SLeila Ghaffari 
2032fe7aee7SLeila Ghaffari       // ----- Apply Sub-Operator for Physics
2042fe7aee7SLeila Ghaffari       CeedCompositeOperatorAddSub(*op_apply, op_apply_outflow);
2052fe7aee7SLeila Ghaffari 
2062fe7aee7SLeila Ghaffari       // ----- Cleanup
2072fe7aee7SLeila Ghaffari       CeedVectorDestroy(&q_data_sur);
2082fe7aee7SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_q_sur);
2092fe7aee7SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_x_sur);
2102fe7aee7SLeila Ghaffari       CeedElemRestrictionDestroy(&elem_restr_qd_i_sur);
2112fe7aee7SLeila Ghaffari       CeedOperatorDestroy(&op_setup_sur);
2122fe7aee7SLeila Ghaffari       CeedOperatorDestroy(&op_apply_outflow);
2132fe7aee7SLeila Ghaffari     }
2142fe7aee7SLeila Ghaffari   }
21511436a05SJames Wright 
21611436a05SJames Wright   // ----- Get Context Labels for Operator
21711436a05SJames Wright   CeedOperatorContextGetFieldLabel(*op_apply, "solution time",
21811436a05SJames Wright                                    &phys->solution_time_label);
21988626eedSJames Wright   CeedOperatorContextGetFieldLabel(*op_apply, "timestep size",
22088626eedSJames Wright                                    &phys->timestep_size_label);
22111436a05SJames Wright 
22277841947SLeila Ghaffari   PetscFunctionReturn(0);
22377841947SLeila Ghaffari }
22477841947SLeila Ghaffari 
22577841947SLeila Ghaffari PetscErrorCode SetupLibceed(Ceed ceed, CeedData ceed_data, DM dm, User user,
226c95f9967SJed Brown                             AppCtx app_ctx, ProblemData *problem, SimpleBC bc) {
22777841947SLeila Ghaffari   PetscErrorCode ierr;
22877841947SLeila Ghaffari   PetscFunctionBeginUser;
22977841947SLeila Ghaffari 
23077841947SLeila Ghaffari   // *****************************************************************************
23177841947SLeila Ghaffari   // Set up CEED objects for the interior domain (volume)
23277841947SLeila Ghaffari   // *****************************************************************************
23377841947SLeila Ghaffari   const PetscInt num_comp_q      = 5;
23477841947SLeila Ghaffari   const CeedInt  dim             = problem->dim,
23577841947SLeila Ghaffari                  num_comp_x      = problem->dim,
23677841947SLeila Ghaffari                  q_data_size_vol = problem->q_data_size_vol,
237*a3ae0734SJed Brown                  jac_data_size_vol = num_comp_q + 3,
23877841947SLeila Ghaffari                  P               = app_ctx->degree + 1,
23977841947SLeila Ghaffari                  Q               = P + app_ctx->q_extra;
240*a3ae0734SJed Brown   CeedElemRestriction elem_restr_jd_i;
241*a3ae0734SJed Brown   CeedVector jac_data;
24277841947SLeila Ghaffari 
24377841947SLeila Ghaffari   // -----------------------------------------------------------------------------
24477841947SLeila Ghaffari   // CEED Bases
24577841947SLeila Ghaffari   // -----------------------------------------------------------------------------
24677841947SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_q, P, Q, CEED_GAUSS,
24777841947SLeila Ghaffari                                   &ceed_data->basis_q);
24877841947SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, Q, CEED_GAUSS,
24977841947SLeila Ghaffari                                   &ceed_data->basis_x);
25077841947SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp_x, 2, P,
25177841947SLeila Ghaffari                                   CEED_GAUSS_LOBATTO, &ceed_data->basis_xc);
25277841947SLeila Ghaffari 
25377841947SLeila Ghaffari   // -----------------------------------------------------------------------------
25477841947SLeila Ghaffari   // CEED Restrictions
25577841947SLeila Ghaffari   // -----------------------------------------------------------------------------
25677841947SLeila Ghaffari   // -- Create restriction
2577ed3e4cdSJeremy L Thompson   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, q_data_size_vol,
2587ed3e4cdSJeremy L Thompson                                  &ceed_data->elem_restr_q, &ceed_data->elem_restr_x,
25977841947SLeila Ghaffari                                  &ceed_data->elem_restr_qd_i); CHKERRQ(ierr);
260*a3ae0734SJed Brown 
261*a3ae0734SJed Brown   ierr = GetRestrictionForDomain(ceed, dm, 0, 0, 0, Q, jac_data_size_vol,
262*a3ae0734SJed Brown                                  NULL, NULL,
263*a3ae0734SJed Brown                                  &elem_restr_jd_i); CHKERRQ(ierr);
26477841947SLeila Ghaffari // -- Create E vectors
26577841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_ceed, NULL);
26677841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->q_dot_ceed,
26777841947SLeila Ghaffari                                   NULL);
26877841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_q, &user->g_ceed, NULL);
26977841947SLeila Ghaffari 
27077841947SLeila Ghaffari   // -----------------------------------------------------------------------------
27177841947SLeila Ghaffari   // CEED QFunctions
27277841947SLeila Ghaffari   // -----------------------------------------------------------------------------
27377841947SLeila Ghaffari   // -- Create QFunction for quadrature data
27491e5af17SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->setup_vol.qfunction,
27591e5af17SJed Brown                               problem->setup_vol.qfunction_loc,
27677841947SLeila Ghaffari                               &ceed_data->qf_setup_vol);
277841e4c73SJed Brown   if (problem->setup_vol.qfunction_context) {
278841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_setup_vol,
279841e4c73SJed Brown                             problem->setup_vol.qfunction_context);
280841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->setup_vol.qfunction_context);
281841e4c73SJed Brown   }
28277841947SLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "dx", num_comp_x*dim,
28377841947SLeila Ghaffari                         CEED_EVAL_GRAD);
28477841947SLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_vol, "weight", 1, CEED_EVAL_WEIGHT);
285a61c78d6SJeremy L Thompson   CeedQFunctionAddOutput(ceed_data->qf_setup_vol, "qdata", q_data_size_vol,
28677841947SLeila Ghaffari                          CEED_EVAL_NONE);
28777841947SLeila Ghaffari 
28877841947SLeila Ghaffari   // -- Create QFunction for ICs
28991e5af17SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->ics.qfunction,
29091e5af17SJed Brown                               problem->ics.qfunction_loc,
29177841947SLeila Ghaffari                               &ceed_data->qf_ics);
292841e4c73SJed Brown   CeedQFunctionSetContext(ceed_data->qf_ics, problem->ics.qfunction_context);
293841e4c73SJed Brown   CeedQFunctionContextDestroy(&problem->ics.qfunction_context);
29477841947SLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_ics, "x", num_comp_x, CEED_EVAL_INTERP);
29577841947SLeila Ghaffari   CeedQFunctionAddOutput(ceed_data->qf_ics, "q0", num_comp_q, CEED_EVAL_NONE);
29677841947SLeila Ghaffari 
29777841947SLeila Ghaffari   // -- Create QFunction for RHS
29891e5af17SJed Brown   if (problem->apply_vol_rhs.qfunction) {
29991e5af17SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_rhs.qfunction,
30091e5af17SJed Brown                                 problem->apply_vol_rhs.qfunction_loc, &ceed_data->qf_rhs_vol);
301841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_rhs_vol,
302841e4c73SJed Brown                             problem->apply_vol_rhs.qfunction_context);
303841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->apply_vol_rhs.qfunction_context);
30477841947SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "q", num_comp_q, CEED_EVAL_INTERP);
305*a3ae0734SJed Brown     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "Grad_q", num_comp_q*dim,
30677841947SLeila Ghaffari                           CEED_EVAL_GRAD);
307a61c78d6SJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "qdata", q_data_size_vol,
30877841947SLeila Ghaffari                           CEED_EVAL_NONE);
30977841947SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_rhs_vol, "x", num_comp_x, CEED_EVAL_INTERP);
31077841947SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "v", num_comp_q,
31177841947SLeila Ghaffari                            CEED_EVAL_INTERP);
312*a3ae0734SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_rhs_vol, "Grad_v", num_comp_q*dim,
31377841947SLeila Ghaffari                            CEED_EVAL_GRAD);
31477841947SLeila Ghaffari   }
31577841947SLeila Ghaffari 
31677841947SLeila Ghaffari   // -- Create QFunction for IFunction
31791e5af17SJed Brown   if (problem->apply_vol_ifunction.qfunction) {
31891e5af17SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_vol_ifunction.qfunction,
31991e5af17SJed Brown                                 problem->apply_vol_ifunction.qfunction_loc, &ceed_data->qf_ifunction_vol);
320841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_ifunction_vol,
321841e4c73SJed Brown                             problem->apply_vol_ifunction.qfunction_context);
322841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->apply_vol_ifunction.qfunction_context);
32377841947SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q", num_comp_q,
32477841947SLeila Ghaffari                           CEED_EVAL_INTERP);
325*a3ae0734SJed Brown     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "Grad_q", num_comp_q*dim,
32677841947SLeila Ghaffari                           CEED_EVAL_GRAD);
327a61c78d6SJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "q dot", num_comp_q,
32877841947SLeila Ghaffari                           CEED_EVAL_INTERP);
329a61c78d6SJeremy L Thompson     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "qdata", q_data_size_vol,
33077841947SLeila Ghaffari                           CEED_EVAL_NONE);
33177841947SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_ifunction_vol, "x", num_comp_x,
33277841947SLeila Ghaffari                           CEED_EVAL_INTERP);
33377841947SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "v", num_comp_q,
33477841947SLeila Ghaffari                            CEED_EVAL_INTERP);
335*a3ae0734SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "Grad_v", num_comp_q*dim,
33677841947SLeila Ghaffari                            CEED_EVAL_GRAD);
337*a3ae0734SJed Brown     CeedQFunctionAddOutput(ceed_data->qf_ifunction_vol, "jac_data",
338*a3ae0734SJed Brown                            jac_data_size_vol, CEED_EVAL_NONE);
33977841947SLeila Ghaffari   }
34077841947SLeila Ghaffari 
34177841947SLeila Ghaffari   // ---------------------------------------------------------------------------
34277841947SLeila Ghaffari   // Element coordinates
34377841947SLeila Ghaffari   // ---------------------------------------------------------------------------
34477841947SLeila Ghaffari   // -- Create CEED vector
34577841947SLeila Ghaffari   CeedElemRestrictionCreateVector(ceed_data->elem_restr_x, &ceed_data->x_coord,
34677841947SLeila Ghaffari                                   NULL);
34777841947SLeila Ghaffari 
34877841947SLeila Ghaffari   // -- Copy PETSc vector in CEED vector
34977841947SLeila Ghaffari   Vec               X_loc;
35077841947SLeila Ghaffari   const PetscScalar *X_loc_array;
35177841947SLeila Ghaffari   ierr = DMGetCoordinatesLocal(dm, &X_loc); CHKERRQ(ierr);
3521864f1c2SLeila Ghaffari   ierr = VecScale(X_loc, problem->dm_scale); CHKERRQ(ierr);
35377841947SLeila Ghaffari   ierr = VecGetArrayRead(X_loc, &X_loc_array); CHKERRQ(ierr);
35477841947SLeila Ghaffari   CeedVectorSetArray(ceed_data->x_coord, CEED_MEM_HOST, CEED_COPY_VALUES,
35577841947SLeila Ghaffari                      (PetscScalar *)X_loc_array);
35677841947SLeila Ghaffari   ierr = VecRestoreArrayRead(X_loc, &X_loc_array); CHKERRQ(ierr);
35777841947SLeila Ghaffari 
35877841947SLeila Ghaffari   // -----------------------------------------------------------------------------
35977841947SLeila Ghaffari   // CEED vectors
36077841947SLeila Ghaffari   // -----------------------------------------------------------------------------
36177841947SLeila Ghaffari   // -- Create CEED vector for geometric data
36277841947SLeila Ghaffari   CeedInt  num_qpts_vol;
36377841947SLeila Ghaffari   PetscInt loc_num_elem_vol;
36477841947SLeila Ghaffari   CeedBasisGetNumQuadraturePoints(ceed_data->basis_q, &num_qpts_vol);
36577841947SLeila Ghaffari   CeedElemRestrictionGetNumElements(ceed_data->elem_restr_q, &loc_num_elem_vol);
36677841947SLeila Ghaffari   CeedVectorCreate(ceed, q_data_size_vol*loc_num_elem_vol*num_qpts_vol,
36777841947SLeila Ghaffari                    &ceed_data->q_data);
36877841947SLeila Ghaffari 
369*a3ae0734SJed Brown   CeedElemRestrictionCreateVector(elem_restr_jd_i, &jac_data, NULL);
37077841947SLeila Ghaffari   // -----------------------------------------------------------------------------
37177841947SLeila Ghaffari   // CEED Operators
37277841947SLeila Ghaffari   // -----------------------------------------------------------------------------
37377841947SLeila Ghaffari   // -- Create CEED operator for quadrature data
37477841947SLeila Ghaffari   CeedOperatorCreate(ceed, ceed_data->qf_setup_vol, NULL, NULL,
37577841947SLeila Ghaffari                      &ceed_data->op_setup_vol);
37677841947SLeila Ghaffari   CeedOperatorSetField(ceed_data->op_setup_vol, "dx", ceed_data->elem_restr_x,
37777841947SLeila Ghaffari                        ceed_data->basis_x, CEED_VECTOR_ACTIVE);
37877841947SLeila Ghaffari   CeedOperatorSetField(ceed_data->op_setup_vol, "weight",
379e6225c47SLeila Ghaffari                        CEED_ELEMRESTRICTION_NONE, ceed_data->basis_x, CEED_VECTOR_NONE);
380a61c78d6SJeremy L Thompson   CeedOperatorSetField(ceed_data->op_setup_vol, "qdata",
381e6225c47SLeila Ghaffari                        ceed_data->elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
38277841947SLeila Ghaffari 
38377841947SLeila Ghaffari   // -- Create CEED operator for ICs
38477841947SLeila Ghaffari   CeedOperatorCreate(ceed, ceed_data->qf_ics, NULL, NULL, &ceed_data->op_ics);
38577841947SLeila Ghaffari   CeedOperatorSetField(ceed_data->op_ics, "x", ceed_data->elem_restr_x,
38677841947SLeila Ghaffari                        ceed_data->basis_xc, CEED_VECTOR_ACTIVE);
38777841947SLeila Ghaffari   CeedOperatorSetField(ceed_data->op_ics, "q0", ceed_data->elem_restr_q,
38877841947SLeila Ghaffari                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
389841e4c73SJed Brown   CeedOperatorContextGetFieldLabel(ceed_data->op_ics, "evaluation time",
390841e4c73SJed Brown                                    &user->phys->ics_time_label);
39177841947SLeila Ghaffari 
39277841947SLeila Ghaffari   // Create CEED operator for RHS
39377841947SLeila Ghaffari   if (ceed_data->qf_rhs_vol) {
39477841947SLeila Ghaffari     CeedOperator op;
39577841947SLeila Ghaffari     CeedOperatorCreate(ceed, ceed_data->qf_rhs_vol, NULL, NULL, &op);
39677841947SLeila Ghaffari     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q,
39777841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
398*a3ae0734SJed Brown     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q,
39977841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
400a61c78d6SJeremy L Thompson     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i,
401e6225c47SLeila Ghaffari                          CEED_BASIS_COLLOCATED, ceed_data->q_data);
40277841947SLeila Ghaffari     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x,
40377841947SLeila Ghaffari                          ceed_data->x_coord);
40477841947SLeila Ghaffari     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q,
40577841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
406*a3ae0734SJed Brown     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q,
40777841947SLeila Ghaffari                          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;
41477841947SLeila Ghaffari     CeedOperatorCreate(ceed, ceed_data->qf_ifunction_vol, NULL, NULL, &op);
41577841947SLeila Ghaffari     CeedOperatorSetField(op, "q", ceed_data->elem_restr_q, ceed_data->basis_q,
41677841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
417*a3ae0734SJed Brown     CeedOperatorSetField(op, "Grad_q", ceed_data->elem_restr_q, ceed_data->basis_q,
41877841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
419a61c78d6SJeremy L Thompson     CeedOperatorSetField(op, "q dot", ceed_data->elem_restr_q, ceed_data->basis_q,
42077841947SLeila Ghaffari                          user->q_dot_ceed);
421a61c78d6SJeremy L Thompson     CeedOperatorSetField(op, "qdata", ceed_data->elem_restr_qd_i,
422e6225c47SLeila Ghaffari                          CEED_BASIS_COLLOCATED, ceed_data->q_data);
42377841947SLeila Ghaffari     CeedOperatorSetField(op, "x", ceed_data->elem_restr_x, ceed_data->basis_x,
42477841947SLeila Ghaffari                          ceed_data->x_coord);
42577841947SLeila Ghaffari     CeedOperatorSetField(op, "v", ceed_data->elem_restr_q, ceed_data->basis_q,
42677841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
427*a3ae0734SJed Brown     CeedOperatorSetField(op, "Grad_v", ceed_data->elem_restr_q, ceed_data->basis_q,
42877841947SLeila Ghaffari                          CEED_VECTOR_ACTIVE);
429*a3ae0734SJed Brown     CeedOperatorSetField(op, "jac_data", elem_restr_jd_i,
430*a3ae0734SJed Brown                          CEED_BASIS_COLLOCATED, jac_data);
431*a3ae0734SJed Brown 
43277841947SLeila Ghaffari     user->op_ifunction_vol = op;
43377841947SLeila Ghaffari   }
43477841947SLeila Ghaffari 
43577841947SLeila Ghaffari   // *****************************************************************************
43677841947SLeila Ghaffari   // Set up CEED objects for the exterior domain (surface)
43777841947SLeila Ghaffari   // *****************************************************************************
43877841947SLeila Ghaffari   CeedInt height  = 1,
43977841947SLeila Ghaffari           dim_sur = dim - height,
44077841947SLeila Ghaffari           P_sur   = app_ctx->degree + 1,
44177841947SLeila Ghaffari           Q_sur   = P_sur + app_ctx->q_extra;
44277841947SLeila Ghaffari   const CeedInt q_data_size_sur = problem->q_data_size_sur;
44377841947SLeila Ghaffari 
44477841947SLeila Ghaffari   // -----------------------------------------------------------------------------
44577841947SLeila Ghaffari   // CEED Bases
44677841947SLeila Ghaffari   // -----------------------------------------------------------------------------
44777841947SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_q, P_sur, Q_sur,
44877841947SLeila Ghaffari                                   CEED_GAUSS, &ceed_data->basis_q_sur);
44977841947SLeila Ghaffari   CeedBasisCreateTensorH1Lagrange(ceed, dim_sur, num_comp_x, 2, Q_sur, CEED_GAUSS,
45077841947SLeila Ghaffari                                   &ceed_data->basis_x_sur);
45177841947SLeila Ghaffari 
45277841947SLeila Ghaffari   // -----------------------------------------------------------------------------
45377841947SLeila Ghaffari   // CEED QFunctions
45477841947SLeila Ghaffari   // -----------------------------------------------------------------------------
45577841947SLeila Ghaffari   // -- Create QFunction for quadrature data
45691e5af17SJed Brown   CeedQFunctionCreateInterior(ceed, 1, problem->setup_sur.qfunction,
45791e5af17SJed Brown                               problem->setup_sur.qfunction_loc,
45877841947SLeila Ghaffari                               &ceed_data->qf_setup_sur);
459841e4c73SJed Brown   if (problem->setup_sur.qfunction_context) {
460841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_setup_sur,
461841e4c73SJed Brown                             problem->setup_sur.qfunction_context);
462841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->setup_sur.qfunction_context);
463841e4c73SJed Brown   }
46477841947SLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "dx", num_comp_x*dim_sur,
46577841947SLeila Ghaffari                         CEED_EVAL_GRAD);
46677841947SLeila Ghaffari   CeedQFunctionAddInput(ceed_data->qf_setup_sur, "weight", 1, CEED_EVAL_WEIGHT);
467a61c78d6SJeremy L Thompson   CeedQFunctionAddOutput(ceed_data->qf_setup_sur, "surface qdata",
4682fe7aee7SLeila Ghaffari                          q_data_size_sur, CEED_EVAL_NONE);
46977841947SLeila Ghaffari 
4702fe7aee7SLeila Ghaffari   // -- Creat QFunction for inflow boundaries
47191e5af17SJed Brown   if (problem->apply_inflow.qfunction) {
47291e5af17SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_inflow.qfunction,
47391e5af17SJed Brown                                 problem->apply_inflow.qfunction_loc, &ceed_data->qf_apply_inflow);
474841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_apply_inflow,
475841e4c73SJed Brown                             problem->apply_inflow.qfunction_context);
476841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->apply_inflow.qfunction_context);
4772fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "q", num_comp_q,
47877841947SLeila Ghaffari                           CEED_EVAL_INTERP);
4792fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "surface qdata",
4802fe7aee7SLeila Ghaffari                           q_data_size_sur, CEED_EVAL_NONE);
4812fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_inflow, "x", num_comp_x,
48277841947SLeila Ghaffari                           CEED_EVAL_INTERP);
4832fe7aee7SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_apply_inflow, "v", num_comp_q,
4842fe7aee7SLeila Ghaffari                            CEED_EVAL_INTERP);
4852fe7aee7SLeila Ghaffari   }
4862fe7aee7SLeila Ghaffari 
4872fe7aee7SLeila Ghaffari   // -- Creat QFunction for outflow boundaries
48891e5af17SJed Brown   if (problem->apply_outflow.qfunction) {
48991e5af17SJed Brown     CeedQFunctionCreateInterior(ceed, 1, problem->apply_outflow.qfunction,
49091e5af17SJed Brown                                 problem->apply_outflow.qfunction_loc, &ceed_data->qf_apply_outflow);
491841e4c73SJed Brown     CeedQFunctionSetContext(ceed_data->qf_apply_outflow,
492841e4c73SJed Brown                             problem->apply_outflow.qfunction_context);
493841e4c73SJed Brown     CeedQFunctionContextDestroy(&problem->apply_outflow.qfunction_context);
4942fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "q", num_comp_q,
4952fe7aee7SLeila Ghaffari                           CEED_EVAL_INTERP);
4962fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "surface qdata",
4972fe7aee7SLeila Ghaffari                           q_data_size_sur, CEED_EVAL_NONE);
4982fe7aee7SLeila Ghaffari     CeedQFunctionAddInput(ceed_data->qf_apply_outflow, "x", num_comp_x,
4992fe7aee7SLeila Ghaffari                           CEED_EVAL_INTERP);
5002fe7aee7SLeila Ghaffari     CeedQFunctionAddOutput(ceed_data->qf_apply_outflow, "v", num_comp_q,
50177841947SLeila Ghaffari                            CEED_EVAL_INTERP);
50277841947SLeila Ghaffari   }
50377841947SLeila Ghaffari 
50477841947SLeila Ghaffari   // *****************************************************************************
50577841947SLeila Ghaffari   // CEED Operator Apply
50677841947SLeila Ghaffari   // *****************************************************************************
50777841947SLeila Ghaffari   // -- Apply CEED Operator for the geometric data
50877841947SLeila Ghaffari   CeedOperatorApply(ceed_data->op_setup_vol, ceed_data->x_coord,
50977841947SLeila Ghaffari                     ceed_data->q_data, CEED_REQUEST_IMMEDIATE);
51077841947SLeila Ghaffari 
51177841947SLeila Ghaffari   // -- Create and apply CEED Composite Operator for the entire domain
51277841947SLeila Ghaffari   if (!user->phys->implicit) { // RHS
51377841947SLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys,
51477841947SLeila Ghaffari                                    user->op_rhs_vol, height, P_sur, Q_sur,
51577841947SLeila Ghaffari                                    q_data_size_sur, &user->op_rhs); CHKERRQ(ierr);
51677841947SLeila Ghaffari   } else { // IFunction
51777841947SLeila Ghaffari     ierr = CreateOperatorForDomain(ceed, dm, bc, ceed_data, user->phys,
51877841947SLeila Ghaffari                                    user->op_ifunction_vol, height, P_sur, Q_sur,
51977841947SLeila Ghaffari                                    q_data_size_sur, &user->op_ifunction); CHKERRQ(ierr);
52077841947SLeila Ghaffari   }
521*a3ae0734SJed Brown   CeedElemRestrictionDestroy(&elem_restr_jd_i);
522*a3ae0734SJed Brown   CeedVectorDestroy(&jac_data);
52377841947SLeila Ghaffari   PetscFunctionReturn(0);
52477841947SLeila Ghaffari }
525