xref: /libCEED/interface/ceed-operator.c (revision 19feff82d9a67df6ce1e0b86aa12b71dfb25f41c)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` Field matches the `CeedQFunction` Field
26e15f9bd0SJeremy L Thompson 
27ca94c3ddSJeremy L Thompson   @param[in] ceed     `Ceed` object for error handling
28ca94c3ddSJeremy L Thompson   @param[in] qf_field `CeedQFunction` Field matching `CeedOperator` Field
29bafebce1SSebastian Grimberg   @param[in] rstr     `CeedOperator` Field `CeedElemRestriction`
30bafebce1SSebastian Grimberg   @param[in] basis    `CeedOperator` Field `CeedBasis`
31e15f9bd0SJeremy L Thompson 
32e15f9bd0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33e15f9bd0SJeremy L Thompson 
34e15f9bd0SJeremy L Thompson   @ref Developer
35e15f9bd0SJeremy L Thompson **/
36bafebce1SSebastian Grimberg static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction rstr, CeedBasis basis) {
376f8994e9SJeremy L Thompson   const char  *field_name;
381203703bSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size;
391203703bSJeremy L Thompson   CeedEvalMode eval_mode;
401203703bSJeremy L Thompson 
411203703bSJeremy L Thompson   // Field data
42ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(qf_field, &field_name, &size, &eval_mode));
432b730f8bSJeremy L Thompson 
44e15f9bd0SJeremy L Thompson   // Restriction
45bafebce1SSebastian Grimberg   CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
466574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
47bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE) {
48bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp));
49e1e9e29dSJed Brown   }
50e15f9bd0SJeremy L Thompson   // Basis
51bafebce1SSebastian Grimberg   CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
52356036faSJeremy L Thompson             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
53bafebce1SSebastian Grimberg   if (basis != CEED_BASIS_NONE) {
54bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetDimension(basis, &dim));
55bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
56bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
57bafebce1SSebastian Grimberg     CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
58ca94c3ddSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT
59ca94c3ddSJeremy L Thompson               " components, but CeedBasis has %" CeedInt_FMT " components",
601203703bSJeremy L Thompson               field_name, size, CeedEvalModes[eval_mode], rstr_num_comp, num_comp);
61e15f9bd0SJeremy L Thompson   }
62e15f9bd0SJeremy L Thompson   // Field size
63d1d35e2fSjeremylt   switch (eval_mode) {
64e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
65edb2538eSJeremy L Thompson       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
661203703bSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", field_name, size,
671203703bSJeremy L Thompson                 CeedEvalModes[eval_mode], rstr_num_comp);
68e15f9bd0SJeremy L Thompson       break;
69e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
70c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
71c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
72c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
736574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
741203703bSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components", field_name, size,
751203703bSJeremy L Thompson                 CeedEvalModes[eval_mode], num_comp * q_comp);
76e15f9bd0SJeremy L Thompson       break;
77e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
78d1d35e2fSjeremylt       // No additional checks required
79e15f9bd0SJeremy L Thompson       break;
80e15f9bd0SJeremy L Thompson   }
81e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
827a982d89SJeremy L. Thompson }
837a982d89SJeremy L. Thompson 
847a982d89SJeremy L. Thompson /**
85ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedOperator`
867a982d89SJeremy L. Thompson 
871203703bSJeremy L Thompson   @param[in] op_field     `CeedOperator` Field to view
88ca94c3ddSJeremy L Thompson   @param[in] qf_field     `CeedQFunction` Field (carries field name)
89d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
904c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
91d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
92ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @ref Utility
977a982d89SJeremy L. Thompson **/
981203703bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
997a982d89SJeremy L. Thompson   const char  *pre    = sub ? "  " : "";
100d1d35e2fSjeremylt   const char  *in_out = input ? "Input" : "Output";
1016f8994e9SJeremy L Thompson   const char  *field_name;
1021203703bSJeremy L Thompson   CeedInt      size;
1031203703bSJeremy L Thompson   CeedEvalMode eval_mode;
1041203703bSJeremy L Thompson   CeedVector   vec;
1051203703bSJeremy L Thompson   CeedBasis    basis;
1061203703bSJeremy L Thompson 
1071203703bSJeremy L Thompson   // Field data
108ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(qf_field, &field_name, &size, &eval_mode));
109ab747706SJeremy L Thompson   CeedCall(CeedOperatorFieldGetData(op_field, NULL, NULL, &basis, &vec));
1107a982d89SJeremy L. Thompson 
1112b730f8bSJeremy L Thompson   fprintf(stream,
1122b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1132b730f8bSJeremy L Thompson           ":\n"
1147a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
1151203703bSJeremy L Thompson           pre, in_out, field_number, pre, field_name);
1161203703bSJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, size);
1171203703bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[eval_mode]);
1181203703bSJeremy L Thompson   if (basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
1191203703bSJeremy L Thompson   if (vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1201203703bSJeremy L Thompson   else if (vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
121681d0ea7SJeremy L Thompson 
122681d0ea7SJeremy L Thompson   CeedCall(CeedVectorDestroy(&vec));
123681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis));
124e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257a982d89SJeremy L. Thompson }
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson /**
128ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1297a982d89SJeremy L. Thompson 
130ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1317a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
132ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
1387a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
13999f7f61fSJeremy L Thompson   bool                is_at_points;
1407a982d89SJeremy L. Thompson   const char         *pre = sub ? "  " : "";
1411203703bSJeremy L Thompson   CeedInt             num_elem, num_qpts, total_fields = 0, num_input_fields, num_output_fields;
1421203703bSJeremy L Thompson   CeedQFunction       qf;
1431203703bSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1441203703bSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1457a982d89SJeremy L. Thompson 
14699f7f61fSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
1472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1501203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1511203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1521203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
153c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
1541c66c397SJeremy L Thompson 
15599f7f61fSJeremy L Thompson   if (is_at_points) {
15699f7f61fSJeremy L Thompson     CeedInt             max_points = 0;
15799f7f61fSJeremy L Thompson     CeedElemRestriction rstr_points;
15899f7f61fSJeremy L Thompson 
15999f7f61fSJeremy L Thompson     CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
16099f7f61fSJeremy L Thompson     CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_points));
16199f7f61fSJeremy L Thompson     fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " max points each\n", pre, num_elem, max_points);
16299f7f61fSJeremy L Thompson     CeedCall(CeedElemRestrictionDestroy(&rstr_points));
16399f7f61fSJeremy L Thompson   } else {
1642b730f8bSJeremy L Thompson     fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
16599f7f61fSJeremy L Thompson   }
1662b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1671203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, num_input_fields, num_input_fields > 1 ? "s" : "");
1681203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1691203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_input_fields[i], qf_input_fields[i], i, sub, 1, stream));
1707a982d89SJeremy L. Thompson   }
1711203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, num_output_fields, num_output_fields > 1 ? "s" : "");
1721203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1731203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_output_fields[i], qf_output_fields[i], i, sub, 0, stream));
1747a982d89SJeremy L. Thompson   }
175e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1767a982d89SJeremy L. Thompson }
1777a982d89SJeremy L. Thompson 
178d99fa3c5SJeremy L Thompson /**
179681d0ea7SJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`.
180681d0ea7SJeremy L Thompson 
181681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_basis` with @ref CeedBasisDestroy().
182eaf62fffSJeremy L Thompson 
183ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
184ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
185eaf62fffSJeremy L Thompson 
186eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
187eaf62fffSJeremy L Thompson 
188eaf62fffSJeremy L Thompson   @ref Developer
189eaf62fffSJeremy L Thompson **/
190eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
191506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
192506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
193506b1a0cSSebastian Grimberg }
194506b1a0cSSebastian Grimberg 
195506b1a0cSSebastian Grimberg /**
196681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`.
197681d0ea7SJeremy L Thompson 
198681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the bases with @ref CeedBasisDestroy().
199506b1a0cSSebastian Grimberg 
200ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
201ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
202ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
203506b1a0cSSebastian Grimberg 
204506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
205506b1a0cSSebastian Grimberg 
206506b1a0cSSebastian Grimberg   @ref Developer
207506b1a0cSSebastian Grimberg **/
208506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
2091203703bSJeremy L Thompson   bool               is_composite;
2101203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2111203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2121c66c397SJeremy L Thompson 
2131203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2141203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2151203703bSJeremy L Thompson 
216506b1a0cSSebastian Grimberg   if (active_input_basis) {
217506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
2181203703bSJeremy L Thompson     if (!is_composite) {
2191203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2201203703bSJeremy L Thompson         CeedVector vec;
2211203703bSJeremy L Thompson 
2221203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2231203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2241203703bSJeremy L Thompson           CeedBasis basis;
2251203703bSJeremy L Thompson 
2261203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
2279bc66399SJeremy L Thompson           CeedCheck(!*active_input_basis || *active_input_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
2289bc66399SJeremy L Thompson                     "Multiple active input CeedBases found");
229681d0ea7SJeremy L Thompson           if (!*active_input_basis) CeedCall(CeedBasisReferenceCopy(basis, active_input_basis));
230681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
231eaf62fffSJeremy L Thompson         }
232681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
2332b730f8bSJeremy L Thompson       }
2349bc66399SJeremy L Thompson       CeedCheck(*active_input_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
235506b1a0cSSebastian Grimberg     }
236506b1a0cSSebastian Grimberg   }
237506b1a0cSSebastian Grimberg   if (active_output_basis) {
238506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
2391203703bSJeremy L Thompson     if (!is_composite) {
2401203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2411203703bSJeremy L Thompson         CeedVector vec;
2421203703bSJeremy L Thompson 
2431203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2441203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2451203703bSJeremy L Thompson           CeedBasis basis;
2461203703bSJeremy L Thompson 
2471203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
2489bc66399SJeremy L Thompson           CeedCheck(!*active_output_basis || *active_output_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
2499bc66399SJeremy L Thompson                     "Multiple active output CeedBases found");
250681d0ea7SJeremy L Thompson           if (!*active_output_basis) CeedCall(CeedBasisReferenceCopy(basis, active_output_basis));
251681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
252506b1a0cSSebastian Grimberg         }
253681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
254506b1a0cSSebastian Grimberg       }
2559bc66399SJeremy L Thompson       CeedCheck(*active_output_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
256506b1a0cSSebastian Grimberg     }
257506b1a0cSSebastian Grimberg   }
258eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
259eaf62fffSJeremy L Thompson }
260eaf62fffSJeremy L Thompson 
261eaf62fffSJeremy L Thompson /**
262681d0ea7SJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`.
263681d0ea7SJeremy L Thompson 
264681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_rstr` with @ref CeedElemRestrictionDestroy().
265e2f04181SAndrew T. Barker 
266ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
267ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
268e2f04181SAndrew T. Barker 
269e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
270e2f04181SAndrew T. Barker 
271e2f04181SAndrew T. Barker   @ref Utility
272e2f04181SAndrew T. Barker **/
2732b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
274506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
275506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
276506b1a0cSSebastian Grimberg }
277506b1a0cSSebastian Grimberg 
278506b1a0cSSebastian Grimberg /**
279681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`.
280681d0ea7SJeremy L Thompson 
281681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the restrictions with @ref CeedElemRestrictionDestroy().
282506b1a0cSSebastian Grimberg 
283ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
284ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
285ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
286506b1a0cSSebastian Grimberg 
287506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
288506b1a0cSSebastian Grimberg 
289506b1a0cSSebastian Grimberg   @ref Utility
290506b1a0cSSebastian Grimberg **/
291506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2921203703bSJeremy L Thompson   bool               is_composite;
2931203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2941203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2951c66c397SJeremy L Thompson 
2961203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2971203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2981203703bSJeremy L Thompson 
299506b1a0cSSebastian Grimberg   if (active_input_rstr) {
300506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
3011203703bSJeremy L Thompson     if (!is_composite) {
3021203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
3031203703bSJeremy L Thompson         CeedVector vec;
3041203703bSJeremy L Thompson 
3051203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3061203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
3071203703bSJeremy L Thompson           CeedElemRestriction rstr;
3081203703bSJeremy L Thompson 
3091203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
3109bc66399SJeremy L Thompson           CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
3119bc66399SJeremy L Thompson                     "Multiple active input CeedElemRestrictions found");
312681d0ea7SJeremy L Thompson           if (!*active_input_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_input_rstr));
313681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
314e2f04181SAndrew T. Barker         }
315681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
3162b730f8bSJeremy L Thompson       }
3179bc66399SJeremy L Thompson       CeedCheck(*active_input_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
318506b1a0cSSebastian Grimberg     }
319506b1a0cSSebastian Grimberg   }
320506b1a0cSSebastian Grimberg   if (active_output_rstr) {
321506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
3221203703bSJeremy L Thompson     if (!is_composite) {
3231203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
3241203703bSJeremy L Thompson         CeedVector vec;
3251203703bSJeremy L Thompson 
3261203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
3271203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
3281203703bSJeremy L Thompson           CeedElemRestriction rstr;
3291203703bSJeremy L Thompson 
3301203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
3319bc66399SJeremy L Thompson           CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
3329bc66399SJeremy L Thompson                     "Multiple active output CeedElemRestrictions found");
333681d0ea7SJeremy L Thompson           if (!*active_output_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_output_rstr));
334681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
335506b1a0cSSebastian Grimberg         }
336681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
337506b1a0cSSebastian Grimberg       }
3389bc66399SJeremy L Thompson       CeedCheck(*active_output_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
339506b1a0cSSebastian Grimberg     }
340506b1a0cSSebastian Grimberg   }
341e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
342e2f04181SAndrew T. Barker }
343e2f04181SAndrew T. Barker 
344d8dd9a91SJeremy L Thompson /**
345ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
3464385fb7fSSebastian Grimberg 
347ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
348ca94c3ddSJeremy L Thompson   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type.
349d8dd9a91SJeremy L Thompson 
350ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
351ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
352ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
3532788fa27SJeremy L Thompson   @param[in]     values      Values to set
354d8dd9a91SJeremy L Thompson 
355d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
356d8dd9a91SJeremy L Thompson 
357d8dd9a91SJeremy L Thompson   @ref User
358d8dd9a91SJeremy L Thompson **/
3592788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3601c66c397SJeremy L Thompson   bool is_composite = false;
3611c66c397SJeremy L Thompson 
3629bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
3633668ca4bSJeremy L Thompson 
3645ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3655ac9af79SJeremy L Thompson   if (field_label->from_op) {
3665ac9af79SJeremy L Thompson     CeedInt index = -1;
3675ac9af79SJeremy L Thompson 
3685ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3695ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3705ac9af79SJeremy L Thompson     }
3719bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3725ac9af79SJeremy L Thompson   }
3735ac9af79SJeremy L Thompson 
3742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
375d8dd9a91SJeremy L Thompson   if (is_composite) {
376d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
377d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
378d8dd9a91SJeremy L Thompson 
379c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
380c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3819bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
3829bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
383d8dd9a91SJeremy L Thompson 
384d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3851203703bSJeremy L Thompson       CeedQFunctionContext ctx;
3861203703bSJeremy L Thompson 
3871485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
388d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3891485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
3901203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values));
391d8dd9a91SJeremy L Thompson       }
3921485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
393d8dd9a91SJeremy L Thompson     }
394d8dd9a91SJeremy L Thompson   } else {
3951203703bSJeremy L Thompson     CeedQFunctionContext ctx;
3961203703bSJeremy L Thompson 
3971485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
3989bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3991203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values));
4001485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
4012788fa27SJeremy L Thompson   }
4026d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
4032788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4042788fa27SJeremy L Thompson }
4052788fa27SJeremy L Thompson 
4062788fa27SJeremy L Thompson /**
407ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
4084385fb7fSSebastian Grimberg 
409ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
410ca94c3ddSJeremy L Thompson   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type.
4112788fa27SJeremy L Thompson 
412ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4132788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4142788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
415c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
416c5d0f995SJed Brown   @param[out]    values      Values in the label
4172788fa27SJeremy L Thompson 
4182788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4192788fa27SJeremy L Thompson 
4202788fa27SJeremy L Thompson   @ref User
4212788fa27SJeremy L Thompson **/
4222788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
4232788fa27SJeremy L Thompson                                              void *values) {
4241c66c397SJeremy L Thompson   bool is_composite = false;
4251c66c397SJeremy L Thompson 
4269bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4272788fa27SJeremy L Thompson 
4282788fa27SJeremy L Thompson   *(void **)values = NULL;
4292788fa27SJeremy L Thompson   *num_values      = 0;
4302788fa27SJeremy L Thompson 
4315ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4325ac9af79SJeremy L Thompson   if (field_label->from_op) {
4335ac9af79SJeremy L Thompson     CeedInt index = -1;
4345ac9af79SJeremy L Thompson 
4355ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4365ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4375ac9af79SJeremy L Thompson     }
4389bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4395ac9af79SJeremy L Thompson   }
4405ac9af79SJeremy L Thompson 
4412788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4422788fa27SJeremy L Thompson   if (is_composite) {
4432788fa27SJeremy L Thompson     CeedInt       num_sub;
4442788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4452788fa27SJeremy L Thompson 
4462788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4472788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4489bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
4499bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4502788fa27SJeremy L Thompson 
4512788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4521203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4531203703bSJeremy L Thompson 
4541485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
4552788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4561485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
4571203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values));
4581485364cSJeremy L Thompson         CeedCall(CeedQFunctionContextDestroy(&ctx));
4592788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4602788fa27SJeremy L Thompson       }
4611485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
4622788fa27SJeremy L Thompson     }
4632788fa27SJeremy L Thompson   } else {
4641203703bSJeremy L Thompson     CeedQFunctionContext ctx;
4651203703bSJeremy L Thompson 
4661485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
4679bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4681203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values));
4691485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
4702788fa27SJeremy L Thompson   }
4712788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4722788fa27SJeremy L Thompson }
4732788fa27SJeremy L Thompson 
4742788fa27SJeremy L Thompson /**
475ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
4764385fb7fSSebastian Grimberg 
477ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
478ca94c3ddSJeremy L Thompson   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type.
4792788fa27SJeremy L Thompson 
480ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4812788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4822788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
483c5d0f995SJed Brown   @param[in]     values      Values array to restore
4842788fa27SJeremy L Thompson 
4852788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4862788fa27SJeremy L Thompson 
4872788fa27SJeremy L Thompson   @ref User
4882788fa27SJeremy L Thompson **/
4892788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4901c66c397SJeremy L Thompson   bool is_composite = false;
4911c66c397SJeremy L Thompson 
4929bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4932788fa27SJeremy L Thompson 
4945ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4955ac9af79SJeremy L Thompson   if (field_label->from_op) {
4965ac9af79SJeremy L Thompson     CeedInt index = -1;
4975ac9af79SJeremy L Thompson 
4985ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4995ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
5005ac9af79SJeremy L Thompson     }
5019bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
5025ac9af79SJeremy L Thompson   }
5035ac9af79SJeremy L Thompson 
5042788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5052788fa27SJeremy L Thompson   if (is_composite) {
5062788fa27SJeremy L Thompson     CeedInt       num_sub;
5072788fa27SJeremy L Thompson     CeedOperator *sub_operators;
5082788fa27SJeremy L Thompson 
5092788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
5102788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
5119bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
5129bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
5132788fa27SJeremy L Thompson 
5142788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
5151203703bSJeremy L Thompson       CeedQFunctionContext ctx;
5161203703bSJeremy L Thompson 
5171485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
5182788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
5191485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
5201203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values));
5211485364cSJeremy L Thompson         CeedCall(CeedQFunctionContextDestroy(&ctx));
5222788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
5232788fa27SJeremy L Thompson       }
5241485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
5252788fa27SJeremy L Thompson     }
5262788fa27SJeremy L Thompson   } else {
5271203703bSJeremy L Thompson     CeedQFunctionContext ctx;
5281203703bSJeremy L Thompson 
5291485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
5309bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
5311203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values));
5321485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
533d8dd9a91SJeremy L Thompson   }
534d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
535d8dd9a91SJeremy L Thompson }
536d8dd9a91SJeremy L Thompson 
5377a982d89SJeremy L. Thompson /// @}
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5407a982d89SJeremy L. Thompson /// CeedOperator Backend API
5417a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5427a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
5437a982d89SJeremy L. Thompson /// @{
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson /**
546ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
5477a982d89SJeremy L. Thompson 
548ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
549d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5527a982d89SJeremy L. Thompson 
5537a982d89SJeremy L. Thompson   @ref Backend
5547a982d89SJeremy L. Thompson **/
555d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
5561203703bSJeremy L Thompson   bool is_composite;
5571203703bSJeremy L Thompson 
5581203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5596e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operators");
560d1d35e2fSjeremylt   *num_args = op->num_fields;
561e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5627a982d89SJeremy L. Thompson }
5637a982d89SJeremy L. Thompson 
5647a982d89SJeremy L. Thompson /**
5659463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
5669463e855SJeremy L Thompson 
5679463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
5689463e855SJeremy L Thompson 
5699463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
5709463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
5719463e855SJeremy L Thompson 
5729463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5739463e855SJeremy L Thompson 
5749463e855SJeremy L Thompson   @ref Backend
5759463e855SJeremy L Thompson **/
5769463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
5779463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
5789463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
5799463e855SJeremy L Thompson 
5809463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
5819463e855SJeremy L Thompson   *has_tensor_bases = true;
5829463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
5839463e855SJeremy L Thompson     bool      is_tensor;
5849463e855SJeremy L Thompson     CeedBasis basis;
5859463e855SJeremy L Thompson 
5869463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
5879463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5889463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5899463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5909463e855SJeremy L Thompson     }
591681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
5929463e855SJeremy L Thompson   }
5939463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
5949463e855SJeremy L Thompson     bool      is_tensor;
5959463e855SJeremy L Thompson     CeedBasis basis;
5969463e855SJeremy L Thompson 
5979463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
5989463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5999463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
6009463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
6019463e855SJeremy L Thompson     }
602681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
6039463e855SJeremy L Thompson   }
6049463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6059463e855SJeremy L Thompson }
6069463e855SJeremy L Thompson 
6079463e855SJeremy L Thompson /**
6081203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is immutable
6091203703bSJeremy L Thompson 
6101203703bSJeremy L Thompson   @param[in]  op           `CeedOperator`
6111203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
6121203703bSJeremy L Thompson 
6131203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6141203703bSJeremy L Thompson 
6151203703bSJeremy L Thompson   @ref Backend
6161203703bSJeremy L Thompson **/
6171203703bSJeremy L Thompson int CeedOperatorIsImmutable(CeedOperator op, bool *is_immutable) {
6181203703bSJeremy L Thompson   *is_immutable = op->is_immutable;
6191203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6201203703bSJeremy L Thompson }
6211203703bSJeremy L Thompson 
6221203703bSJeremy L Thompson /**
623ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
6247a982d89SJeremy L. Thompson 
625ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
626d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
6277a982d89SJeremy L. Thompson 
6287a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6297a982d89SJeremy L. Thompson 
6307a982d89SJeremy L. Thompson   @ref Backend
6317a982d89SJeremy L. Thompson **/
632d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
633f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
634e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6357a982d89SJeremy L. Thompson }
6367a982d89SJeremy L. Thompson 
6377a982d89SJeremy L. Thompson /**
638ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
6397a982d89SJeremy L. Thompson 
640ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
641ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
6427a982d89SJeremy L. Thompson 
6437a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6447a982d89SJeremy L. Thompson 
6457a982d89SJeremy L. Thompson   @ref Backend
6467a982d89SJeremy L. Thompson **/
6477a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
6481203703bSJeremy L Thompson   bool is_composite;
6491203703bSJeremy L Thompson 
6501203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6516e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
652c11e12f4SJeremy L Thompson   *qf = NULL;
653c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(op->qf, qf));
654e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6557a982d89SJeremy L. Thompson }
6567a982d89SJeremy L. Thompson 
6577a982d89SJeremy L. Thompson /**
658ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
659c04a41a7SJeremy L Thompson 
660ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
661d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
662c04a41a7SJeremy L Thompson 
663c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
664c04a41a7SJeremy L Thompson 
665c04a41a7SJeremy L Thompson   @ref Backend
666c04a41a7SJeremy L Thompson **/
667d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
668f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
669e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
670c04a41a7SJeremy L Thompson }
671c04a41a7SJeremy L Thompson 
672c04a41a7SJeremy L Thompson /**
673ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
6747a982d89SJeremy L. Thompson 
675ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
6767a982d89SJeremy L. Thompson   @param[out] data Variable to store data
6777a982d89SJeremy L. Thompson 
6787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6797a982d89SJeremy L. Thompson 
6807a982d89SJeremy L. Thompson   @ref Backend
6817a982d89SJeremy L. Thompson **/
682777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
683777ff853SJeremy L Thompson   *(void **)data = op->data;
684e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6857a982d89SJeremy L. Thompson }
6867a982d89SJeremy L. Thompson 
6877a982d89SJeremy L. Thompson /**
688ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
6897a982d89SJeremy L. Thompson 
690ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
691ea61e9acSJeremy L Thompson   @param[in]     data Data to set
6927a982d89SJeremy L. Thompson 
6937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6947a982d89SJeremy L. Thompson 
6957a982d89SJeremy L. Thompson   @ref Backend
6967a982d89SJeremy L. Thompson **/
697777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
698777ff853SJeremy L Thompson   op->data = data;
699e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7007a982d89SJeremy L. Thompson }
7017a982d89SJeremy L. Thompson 
7027a982d89SJeremy L. Thompson /**
703ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
70434359f16Sjeremylt 
705ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
70634359f16Sjeremylt 
70734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
70834359f16Sjeremylt 
70934359f16Sjeremylt   @ref Backend
71034359f16Sjeremylt **/
7119560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
71234359f16Sjeremylt   op->ref_count++;
71334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
71434359f16Sjeremylt }
71534359f16Sjeremylt 
71634359f16Sjeremylt /**
717ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
7187a982d89SJeremy L. Thompson 
719ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
7207a982d89SJeremy L. Thompson 
7217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
7227a982d89SJeremy L. Thompson 
7237a982d89SJeremy L. Thompson   @ref Backend
7247a982d89SJeremy L. Thompson **/
7257a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
726f04ea552SJeremy L Thompson   op->is_backend_setup = true;
727e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7287a982d89SJeremy L. Thompson }
7297a982d89SJeremy L. Thompson 
7307a982d89SJeremy L. Thompson /// @}
7317a982d89SJeremy L. Thompson 
7327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7337a982d89SJeremy L. Thompson /// CeedOperator Public API
7347a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7357a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
736dfdf5a53Sjeremylt /// @{
737d7b241e6Sjeremylt 
738d7b241e6Sjeremylt /**
739ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
7404385fb7fSSebastian Grimberg 
741ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
742d7b241e6Sjeremylt 
743ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
744ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
745ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
746ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
747ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
748b11c1e72Sjeremylt 
749b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
750dfdf5a53Sjeremylt 
7517a982d89SJeremy L. Thompson   @ref User
752d7b241e6Sjeremylt  */
7532b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
7545fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
7555fe0d4faSjeremylt     Ceed delegate;
7566574a04fSJeremy L Thompson 
7572b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7581ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate");
7592b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
7609bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
761e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7625fe0d4faSjeremylt   }
7635fe0d4faSjeremylt 
764ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
765db002c03SJeremy L Thompson 
7662b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
767db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
768d1d35e2fSjeremylt   (*op)->ref_count   = 1;
7692b104005SJeremy L Thompson   (*op)->input_size  = -1;
7702b104005SJeremy L Thompson   (*op)->output_size = -1;
771db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
772db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
773db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
7742b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
7752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
7762b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
777e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
778d7b241e6Sjeremylt }
779d7b241e6Sjeremylt 
780d7b241e6Sjeremylt /**
781ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
78248acf710SJeremy L Thompson 
783ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
784ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
78548acf710SJeremy L Thompson 
786ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
787ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
788ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
789ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
79048acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
79148acf710SJeremy L Thompson 
79248acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79348acf710SJeremy L Thompson 
79448acf710SJeremy L Thompson   @ref User
79548acf710SJeremy L Thompson  */
79648acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
79748acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
79848acf710SJeremy L Thompson     Ceed delegate;
79948acf710SJeremy L Thompson 
80048acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
8011ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints");
80248acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
8039bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
80448acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
80548acf710SJeremy L Thompson   }
80648acf710SJeremy L Thompson 
807ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
80848acf710SJeremy L Thompson 
80948acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
81048acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
81148acf710SJeremy L Thompson   (*op)->ref_count    = 1;
81248acf710SJeremy L Thompson   (*op)->is_at_points = true;
81348acf710SJeremy L Thompson   (*op)->input_size   = -1;
81448acf710SJeremy L Thompson   (*op)->output_size  = -1;
81548acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
81648acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
81748acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
81848acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
81948acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
82048acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
82148acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82248acf710SJeremy L Thompson }
82348acf710SJeremy L Thompson 
82448acf710SJeremy L Thompson /**
825ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
82652d6035fSJeremy L Thompson 
827ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
828ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
82952d6035fSJeremy L Thompson 
83052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83152d6035fSJeremy L Thompson 
8327a982d89SJeremy L. Thompson   @ref User
83352d6035fSJeremy L Thompson  */
83452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
83552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
83652d6035fSJeremy L Thompson     Ceed delegate;
83752d6035fSJeremy L Thompson 
8381c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
839250756a7Sjeremylt     if (delegate) {
8402b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
8419bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&delegate));
842e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
84352d6035fSJeremy L Thompson     }
844250756a7Sjeremylt   }
84552d6035fSJeremy L Thompson 
8462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
847db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
848996d9ab5SJed Brown   (*op)->ref_count    = 1;
849f04ea552SJeremy L Thompson   (*op)->is_composite = true;
8502b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
8512b104005SJeremy L Thompson   (*op)->input_size  = -1;
8522b104005SJeremy L Thompson   (*op)->output_size = -1;
853250756a7Sjeremylt 
854db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
855e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
85652d6035fSJeremy L Thompson }
85752d6035fSJeremy L Thompson 
85852d6035fSJeremy L Thompson /**
859ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
8604385fb7fSSebastian Grimberg 
861ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
862512bb800SJeremy L Thompson 
863ca94c3ddSJeremy L Thompson   Note: If the value of `*op_copy` passed to this function is non-`NULL`, then it is assumed that `*op_copy` is a pointer to a `CeedOperator`.
864ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
8659560d06aSjeremylt 
866ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
867ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
8689560d06aSjeremylt 
8699560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8709560d06aSjeremylt 
8719560d06aSjeremylt   @ref User
8729560d06aSjeremylt **/
8739560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
8742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
8752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
8769560d06aSjeremylt   *op_copy = op;
8779560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8789560d06aSjeremylt }
8799560d06aSjeremylt 
8809560d06aSjeremylt /**
881ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
882d7b241e6Sjeremylt 
883ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
884bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
885ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
886d7b241e6Sjeremylt 
887ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
888ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
889d7b241e6Sjeremylt 
890528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
891bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
892528a22edSJeremy L Thompson 
893ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
894ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
895bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
896bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
897bafebce1SSebastian Grimberg   @param[in]     vec        `CeedVector` to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE if using @ref CEED_EVAL_WEIGHT in the `CeedQFunction`
898b11c1e72Sjeremylt 
899b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
900dfdf5a53Sjeremylt 
9017a982d89SJeremy L. Thompson   @ref User
902b11c1e72Sjeremylt **/
903bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
9041203703bSJeremy L Thompson   bool               is_input = true, is_at_points, is_composite, is_immutable;
9051203703bSJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields;
9061203703bSJeremy L Thompson   CeedQFunction      qf;
9071203703bSJeremy L Thompson   CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields;
9081c66c397SJeremy L Thompson   CeedOperatorField *op_field;
9091c66c397SJeremy L Thompson 
9101203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
9111203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9121203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
9139bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
9149bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
9159bc66399SJeremy L Thompson   CeedCheck(rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
9169bc66399SJeremy L Thompson   CeedCheck(basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
9179bc66399SJeremy L Thompson   CeedCheck(vec, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
91852d6035fSJeremy L Thompson 
919bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
9209bc66399SJeremy L Thompson   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
921ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
9222c7e7413SJeremy L Thompson   {
9232c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
9242c7e7413SJeremy L Thompson 
925bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
92648acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
9279bc66399SJeremy L Thompson       CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
9289bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints not supported for standard operator fields");
9299bc66399SJeremy L Thompson       CeedCheck(basis == CEED_BASIS_NONE, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
9309bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
93148acf710SJeremy L Thompson       if (!op->first_points_rstr) {
932bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
93348acf710SJeremy L Thompson       } else {
93448acf710SJeremy L Thompson         bool are_compatible;
93548acf710SJeremy L Thompson 
936bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
9379bc66399SJeremy L Thompson         CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
938ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
93948acf710SJeremy L Thompson       }
94048acf710SJeremy L Thompson     }
9412c7e7413SJeremy L Thompson   }
942d7b241e6Sjeremylt 
943bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
944bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
9459bc66399SJeremy L Thompson   CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
946ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
947528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
948bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
9491203703bSJeremy L Thompson 
9501203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
9511203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
952c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
9531203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9546f8994e9SJeremy L Thompson     const char *qf_field_name;
9551203703bSJeremy L Thompson 
9561203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_input_fields[i], &qf_field_name));
9571203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9581203703bSJeremy L Thompson       qf_field = qf_input_fields[i];
959d1d35e2fSjeremylt       op_field = &op->input_fields[i];
960d7b241e6Sjeremylt       goto found;
961d7b241e6Sjeremylt     }
962d7b241e6Sjeremylt   }
9632b104005SJeremy L Thompson   is_input = false;
9641203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9656f8994e9SJeremy L Thompson     const char *qf_field_name;
9661203703bSJeremy L Thompson 
9671203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_output_fields[i], &qf_field_name));
9681203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9691203703bSJeremy L Thompson       qf_field = qf_output_fields[i];
970d1d35e2fSjeremylt       op_field = &op->output_fields[i];
971d7b241e6Sjeremylt       goto found;
972d7b241e6Sjeremylt     }
973d7b241e6Sjeremylt   }
974c042f62fSJeremy L Thompson   // LCOV_EXCL_START
9759bc66399SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
976c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
977d7b241e6Sjeremylt found:
9789bc66399SJeremy L Thompson   CeedCall(CeedOperatorCheckField(CeedOperatorReturnCeed(op), qf_field, rstr, basis));
9792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
980e15f9bd0SJeremy L Thompson 
981bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
9822b104005SJeremy L Thompson     CeedSize l_size;
9831c66c397SJeremy L Thompson 
984bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
9852b104005SJeremy L Thompson     if (is_input) {
9862b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
9879bc66399SJeremy L Thompson       CeedCheck(l_size == op->input_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
988249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
9892b104005SJeremy L Thompson     } else {
9902b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
9919bc66399SJeremy L Thompson       CeedCheck(l_size == op->output_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
992249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
9932b104005SJeremy L Thompson     }
9942b730f8bSJeremy L Thompson   }
9952b104005SJeremy L Thompson 
996bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
997bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
998bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
999d1d35e2fSjeremylt     op->num_elem        = num_elem;
1000d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
1001e15f9bd0SJeremy L Thompson   }
1002bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
10032a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
1004d1d35e2fSjeremylt   op->num_fields += 1;
10052b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
1006e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1007d7b241e6Sjeremylt }
1008d7b241e6Sjeremylt 
1009d7b241e6Sjeremylt /**
1010ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
101143bbe138SJeremy L Thompson 
1012ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1013f04ea552SJeremy L Thompson 
1014ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
1015f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
1016ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
1017f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
1018ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
101943bbe138SJeremy L Thompson 
102043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
102143bbe138SJeremy L Thompson 
1022e9b533fbSJeremy L Thompson   @ref Advanced
102343bbe138SJeremy L Thompson **/
10242b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
102543bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
10261203703bSJeremy L Thompson   bool          is_composite;
10271203703bSJeremy L Thompson   CeedQFunction qf;
10281203703bSJeremy L Thompson 
10291203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10306e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
10312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
103243bbe138SJeremy L Thompson 
10331203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10341203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL));
1035c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
103643bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
103743bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
103843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103943bbe138SJeremy L Thompson }
104043bbe138SJeremy L Thompson 
104143bbe138SJeremy L Thompson /**
1042ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
104348acf710SJeremy L Thompson 
1044ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
104548acf710SJeremy L Thompson 
1046ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
1047ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
1048ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
104948acf710SJeremy L Thompson 
105048acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105148acf710SJeremy L Thompson 
105248acf710SJeremy L Thompson   @ref Advanced
105348acf710SJeremy L Thompson **/
105448acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
10551203703bSJeremy L Thompson   bool is_at_points, is_immutable;
10562a3ff1c9SZach Atkins 
10572a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10581203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
10599bc66399SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
10609bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
106148acf710SJeremy L Thompson 
106248acf710SJeremy L Thompson   if (!op->first_points_rstr) {
106348acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
106448acf710SJeremy L Thompson   } else {
106548acf710SJeremy L Thompson     bool are_compatible;
106648acf710SJeremy L Thompson 
106748acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
10689bc66399SJeremy L Thompson     CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
1069ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
107048acf710SJeremy L Thompson   }
107148acf710SJeremy L Thompson 
107248acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
107348acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
107448acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
107548acf710SJeremy L Thompson }
107648acf710SJeremy L Thompson 
107748acf710SJeremy L Thompson /**
1078b594f9faSZach Atkins   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
1079b594f9faSZach Atkins 
1080b594f9faSZach Atkins   @param[in]  op           `CeedOperator`
1081b594f9faSZach Atkins   @param[out] is_at_points Variable to store at points status
1082b594f9faSZach Atkins 
1083b594f9faSZach Atkins   @return An error code: 0 - success, otherwise - failure
1084b594f9faSZach Atkins 
1085b594f9faSZach Atkins   @ref User
1086b594f9faSZach Atkins **/
1087b594f9faSZach Atkins int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
1088b594f9faSZach Atkins   *is_at_points = op->is_at_points;
1089b594f9faSZach Atkins   return CEED_ERROR_SUCCESS;
1090b594f9faSZach Atkins }
1091b594f9faSZach Atkins 
1092b594f9faSZach Atkins /**
1093ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
109448acf710SJeremy L Thompson 
1095ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
109648acf710SJeremy L Thompson 
1097ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
1098ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
1099ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
110048acf710SJeremy L Thompson 
110148acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
110248acf710SJeremy L Thompson 
110348acf710SJeremy L Thompson   @ref Advanced
110448acf710SJeremy L Thompson **/
110548acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
11062a3ff1c9SZach Atkins   bool is_at_points;
11072a3ff1c9SZach Atkins 
11082a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
11096e536b99SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
111048acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
111148acf710SJeremy L Thompson 
11123f919cbcSJeremy L Thompson   if (rstr_points) {
11133f919cbcSJeremy L Thompson     *rstr_points = NULL;
11143f919cbcSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
11153f919cbcSJeremy L Thompson   }
11163f919cbcSJeremy L Thompson   if (point_coords) {
11173f919cbcSJeremy L Thompson     *point_coords = NULL;
11183f919cbcSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
11193f919cbcSJeremy L Thompson   }
112048acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
112148acf710SJeremy L Thompson }
112248acf710SJeremy L Thompson 
112348acf710SJeremy L Thompson /**
1124be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
1125be9c6463SJeremy L Thompson 
1126be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
1127de5900adSJames Wright 
1128ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1129de5900adSJames Wright 
1130ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
1131ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
1132ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
1133de5900adSJames Wright 
1134de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
1135de5900adSJames Wright 
1136de5900adSJames Wright   @ref Advanced
1137de5900adSJames Wright **/
1138de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
11396f8994e9SJeremy L Thompson   const char        *name;
1140de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
1141de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
1142de5900adSJames Wright 
1143be9c6463SJeremy L Thompson   *op_field = NULL;
11441c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1145de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1146de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
1147de5900adSJames Wright     if (!strcmp(name, field_name)) {
1148de5900adSJames Wright       *op_field = input_fields[i];
1149de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1150de5900adSJames Wright     }
1151de5900adSJames Wright   }
1152de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1153de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
1154de5900adSJames Wright     if (!strcmp(name, field_name)) {
1155de5900adSJames Wright       *op_field = output_fields[i];
1156de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1157de5900adSJames Wright     }
1158de5900adSJames Wright   }
1159be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1160de5900adSJames Wright }
1161de5900adSJames Wright 
1162de5900adSJames Wright /**
1163ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
116428567f8fSJeremy L Thompson 
1165ca94c3ddSJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
116628567f8fSJeremy L Thompson   @param[out] field_name Variable to store the field name
116728567f8fSJeremy L Thompson 
116828567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
116928567f8fSJeremy L Thompson 
1170e9b533fbSJeremy L Thompson   @ref Advanced
117128567f8fSJeremy L Thompson **/
11726f8994e9SJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, const char **field_name) {
11736f8994e9SJeremy L Thompson   *field_name = op_field->field_name;
117428567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
117528567f8fSJeremy L Thompson }
117628567f8fSJeremy L Thompson 
117728567f8fSJeremy L Thompson /**
1178681d0ea7SJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field.
1179681d0ea7SJeremy L Thompson 
1180681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr` with @ref CeedElemRestrictionDestroy().
118143bbe138SJeremy L Thompson 
1182ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1183ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
118443bbe138SJeremy L Thompson 
118543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
118643bbe138SJeremy L Thompson 
1187e9b533fbSJeremy L Thompson   @ref Advanced
118843bbe138SJeremy L Thompson **/
11892b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1190681d0ea7SJeremy L Thompson   *rstr = NULL;
1191681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(op_field->elem_rstr, rstr));
119243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
119343bbe138SJeremy L Thompson }
119443bbe138SJeremy L Thompson 
119543bbe138SJeremy L Thompson /**
1196681d0ea7SJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field.
1197681d0ea7SJeremy L Thompson 
1198681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `basis` with @ref CeedBasisDestroy().
119943bbe138SJeremy L Thompson 
1200ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1201ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
120243bbe138SJeremy L Thompson 
120343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
120443bbe138SJeremy L Thompson 
1205e9b533fbSJeremy L Thompson   @ref Advanced
120643bbe138SJeremy L Thompson **/
120743bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
1208681d0ea7SJeremy L Thompson   *basis = NULL;
1209681d0ea7SJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(op_field->basis, basis));
121043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
121143bbe138SJeremy L Thompson }
121243bbe138SJeremy L Thompson 
121343bbe138SJeremy L Thompson /**
1214681d0ea7SJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field.
1215681d0ea7SJeremy L Thompson 
1216681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `vec` with @ref CeedVectorDestroy().
121743bbe138SJeremy L Thompson 
1218ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1219ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
122043bbe138SJeremy L Thompson 
122143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
122243bbe138SJeremy L Thompson 
1223e9b533fbSJeremy L Thompson   @ref Advanced
122443bbe138SJeremy L Thompson **/
122543bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
1226681d0ea7SJeremy L Thompson   *vec = NULL;
1227681d0ea7SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(op_field->vec, vec));
122843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
122943bbe138SJeremy L Thompson }
123043bbe138SJeremy L Thompson 
123143bbe138SJeremy L Thompson /**
1232ab747706SJeremy L Thompson   @brief Get the data of a `CeedOperator` Field.
1233ab747706SJeremy L Thompson 
1234681d0ea7SJeremy L Thompson   Any arguments set as `NULL` are ignored..
1235681d0ea7SJeremy L Thompson 
1236681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr`, `basis`, and `vec`.
1237ab747706SJeremy L Thompson 
1238ab747706SJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
1239ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
1240ab747706SJeremy L Thompson   @param[out] rstr       Variable to store `CeedElemRestriction`
1241ab747706SJeremy L Thompson   @param[out] basis      Variable to store `CeedBasis`
1242ab747706SJeremy L Thompson   @param[out] vec        Variable to store `CeedVector`
1243ab747706SJeremy L Thompson 
1244ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1245ab747706SJeremy L Thompson 
1246ab747706SJeremy L Thompson   @ref Advanced
1247ab747706SJeremy L Thompson **/
12486f8994e9SJeremy L Thompson int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name, CeedElemRestriction *rstr, CeedBasis *basis, CeedVector *vec) {
1249ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedOperatorFieldGetName(op_field, field_name));
1250ab747706SJeremy L Thompson   if (rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_field, rstr));
1251ab747706SJeremy L Thompson   if (basis) CeedCall(CeedOperatorFieldGetBasis(op_field, basis));
1252ab747706SJeremy L Thompson   if (vec) CeedCall(CeedOperatorFieldGetVector(op_field, vec));
1253ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1254ab747706SJeremy L Thompson }
1255ab747706SJeremy L Thompson 
1256ab747706SJeremy L Thompson /**
1257ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1258288c0443SJeremy L Thompson 
1259ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1260ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
126152d6035fSJeremy L Thompson 
126252d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
126352d6035fSJeremy L Thompson 
12647a982d89SJeremy L. Thompson   @ref User
126552d6035fSJeremy L Thompson  */
12662b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
12671203703bSJeremy L Thompson   bool is_immutable;
12681203703bSJeremy L Thompson 
12699bc66399SJeremy L Thompson   CeedCheck(composite_op->is_composite, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
12709bc66399SJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, CeedOperatorReturnCeed(composite_op), CEED_ERROR_UNSUPPORTED,
12719bc66399SJeremy L Thompson             "Cannot add additional sub-operators");
12721203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable));
12739bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
12742b730f8bSJeremy L Thompson 
12752b730f8bSJeremy L Thompson   {
12762b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
12771c66c397SJeremy L Thompson 
12782b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
12792b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
12802b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
12812b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
12829bc66399SJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
12839bc66399SJeremy L Thompson               CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR,
1284249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1285249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1286249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12872b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
12882b730f8bSJeremy L Thompson   }
12892b730f8bSJeremy L Thompson 
1290d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
12912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1292d1d35e2fSjeremylt   composite_op->num_suboperators++;
1293e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
129452d6035fSJeremy L Thompson }
129552d6035fSJeremy L Thompson 
129652d6035fSJeremy L Thompson /**
1297ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
129875f0d5a4SJeremy L Thompson 
1299ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1300ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
130175f0d5a4SJeremy L Thompson 
130275f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
130375f0d5a4SJeremy L Thompson 
130475f0d5a4SJeremy L Thompson   @ref Backend
130575f0d5a4SJeremy L Thompson **/
1306c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
13071203703bSJeremy L Thompson   bool is_composite;
13081203703bSJeremy L Thompson 
13091203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13106e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
131175f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
131275f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
131375f0d5a4SJeremy L Thompson }
131475f0d5a4SJeremy L Thompson 
131575f0d5a4SJeremy L Thompson /**
1316ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
131775f0d5a4SJeremy L Thompson 
1318ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1319ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
132075f0d5a4SJeremy L Thompson 
132175f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
132275f0d5a4SJeremy L Thompson 
132375f0d5a4SJeremy L Thompson   @ref Backend
132475f0d5a4SJeremy L Thompson **/
1325c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
13261203703bSJeremy L Thompson   bool is_composite;
13271203703bSJeremy L Thompson 
13281203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13296e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
133075f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
133175f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
133275f0d5a4SJeremy L Thompson }
133375f0d5a4SJeremy L Thompson 
133475f0d5a4SJeremy L Thompson /**
13358a297abdSJames Wright   @brief Get a sub `CeedOperator` of a composite `CeedOperator` from its name.
13368a297abdSJames Wright 
13378a297abdSJames Wright   `sub_op` is set to `NULL` if the sub operator is not found.
13388a297abdSJames Wright 
13398a297abdSJames Wright   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
13408a297abdSJames Wright 
13418a297abdSJames Wright   @param[in]  op      Composite `CeedOperator`
13428a297abdSJames Wright   @param[in]  op_name Name of desired sub `CeedOperator`
13438a297abdSJames Wright   @param[out] sub_op  Sub `CeedOperator` corresponding to the name
13448a297abdSJames Wright 
13458a297abdSJames Wright   @return An error code: 0 - success, otherwise - failure
13468a297abdSJames Wright 
13478a297abdSJames Wright   @ref Advanced
13488a297abdSJames Wright **/
13498a297abdSJames Wright int CeedCompositeOperatorGetSubByName(CeedOperator op, const char *op_name, CeedOperator *sub_op) {
13508a297abdSJames Wright   bool          is_composite;
13518a297abdSJames Wright   CeedInt       num_sub_ops;
13528a297abdSJames Wright   CeedOperator *sub_ops;
13538a297abdSJames Wright 
13548a297abdSJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13558a297abdSJames Wright   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
13568a297abdSJames Wright   *sub_op = NULL;
13578a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_ops));
13588a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_ops));
13598a297abdSJames Wright   for (CeedInt i = 0; i < num_sub_ops; i++) {
13608a297abdSJames Wright     if (sub_ops[i]->name && !strcmp(op_name, sub_ops[i]->name)) {
13618a297abdSJames Wright       *sub_op = sub_ops[i];
13628a297abdSJames Wright       return CEED_ERROR_SUCCESS;
13638a297abdSJames Wright     }
13648a297abdSJames Wright   }
13658a297abdSJames Wright   return CEED_ERROR_SUCCESS;
13668a297abdSJames Wright }
13678a297abdSJames Wright 
13688a297abdSJames Wright /**
1369ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
13704db537f9SJeremy L Thompson 
1371ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
13724db537f9SJeremy L Thompson 
13734db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13744db537f9SJeremy L Thompson 
13754db537f9SJeremy L Thompson   @ref User
13764db537f9SJeremy L Thompson **/
13774db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
13781203703bSJeremy L Thompson   bool          is_at_points, is_composite;
13791203703bSJeremy L Thompson   CeedQFunction qf = NULL;
13804db537f9SJeremy L Thompson 
13812b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
13824db537f9SJeremy L Thompson 
13831203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
13841203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13851203703bSJeremy L Thompson   if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf));
13861203703bSJeremy L Thompson   if (is_composite) {
13871203703bSJeremy L Thompson     CeedInt num_suboperators;
13881203703bSJeremy L Thompson 
13891203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13901203703bSJeremy L Thompson     if (!num_suboperators) {
139143622462SJeremy L Thompson       // Empty operator setup
139243622462SJeremy L Thompson       op->input_size  = 0;
139343622462SJeremy L Thompson       op->output_size = 0;
139443622462SJeremy L Thompson     } else {
13951203703bSJeremy L Thompson       CeedOperator *sub_operators;
13961203703bSJeremy L Thompson 
13971203703bSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13981203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_suboperators; i++) {
13991203703bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(sub_operators[i]));
14004db537f9SJeremy L Thompson       }
14012b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
14022b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
14032b104005SJeremy L Thompson       CeedSize input_size, output_size;
14042b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
140543622462SJeremy L Thompson     }
14064db537f9SJeremy L Thompson   } else {
14071203703bSJeremy L Thompson     CeedInt num_input_fields, num_output_fields;
14081203703bSJeremy L Thompson 
14099bc66399SJeremy L Thompson     CeedCheck(op->num_fields > 0, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No operator fields set");
14101203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL));
14119bc66399SJeremy L Thompson     CeedCheck(op->num_fields == num_input_fields + num_output_fields, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
14129bc66399SJeremy L Thompson               "Not all operator fields set");
14139bc66399SJeremy L Thompson     CeedCheck(op->has_restriction, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "At least one restriction required");
14149bc66399SJeremy L Thompson     CeedCheck(op->num_qpts > 0 || is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
1415ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
14162b730f8bSJeremy L Thompson   }
14174db537f9SJeremy L Thompson 
14184db537f9SJeremy L Thompson   // Flag as immutable and ready
14194db537f9SJeremy L Thompson   op->is_interface_setup = true;
14201203703bSJeremy L Thompson   if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf));
1421c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
14221203703bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf));
14231203703bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT));
14244db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14254db537f9SJeremy L Thompson }
14264db537f9SJeremy L Thompson 
14274db537f9SJeremy L Thompson /**
1428ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
14294385fb7fSSebastian Grimberg 
1430ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1431c9366a6bSJeremy L Thompson 
1432ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1433ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1434ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1435c9366a6bSJeremy L Thompson 
1436c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1437c9366a6bSJeremy L Thompson 
1438c9366a6bSJeremy L Thompson   @ref User
1439c9366a6bSJeremy L Thompson **/
14402b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1441c9366a6bSJeremy L Thompson   bool is_composite;
1442c9366a6bSJeremy L Thompson 
14432b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
14442b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1445c9366a6bSJeremy L Thompson 
14462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14472b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
14481203703bSJeremy L Thompson     CeedInt       num_suboperators;
14491203703bSJeremy L Thompson     CeedOperator *sub_operators;
14501203703bSJeremy L Thompson 
14511203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14521203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14531203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
1454c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
14551c66c397SJeremy L Thompson 
14561203703bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(sub_operators[i], &sub_input_size, &sub_output_size));
14572b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
14582b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
14592b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
14606e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
14616e536b99SJeremy L Thompson                 CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR,
1462249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1463249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1464249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
14652b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1466c9366a6bSJeremy L Thompson     }
14672b730f8bSJeremy L Thompson   }
1468c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1469c9366a6bSJeremy L Thompson }
1470c9366a6bSJeremy L Thompson 
1471c9366a6bSJeremy L Thompson /**
1472ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
14734385fb7fSSebastian Grimberg 
1474ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1475ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
14768b919e6bSJeremy L Thompson 
1477ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1478beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
14798b919e6bSJeremy L Thompson 
14808b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14818b919e6bSJeremy L Thompson 
14828b919e6bSJeremy L Thompson   @ref Advanced
14838b919e6bSJeremy L Thompson **/
14842b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
14858b919e6bSJeremy L Thompson   bool is_composite;
14868b919e6bSJeremy L Thompson 
14872b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14888b919e6bSJeremy L Thompson   if (is_composite) {
14898b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
14902b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
14918b919e6bSJeremy L Thompson     }
14928b919e6bSJeremy L Thompson   } else {
14937d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14947d5185d7SSebastian Grimberg 
14957d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
14967d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetReuse(data, reuse_assembly_data));
1497beecbf24SJeremy L Thompson   }
1498beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1499beecbf24SJeremy L Thompson }
1500beecbf24SJeremy L Thompson 
1501beecbf24SJeremy L Thompson /**
1502ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1503beecbf24SJeremy L Thompson 
1504ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
15056e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1506beecbf24SJeremy L Thompson 
1507beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1508beecbf24SJeremy L Thompson 
1509beecbf24SJeremy L Thompson   @ref Advanced
1510beecbf24SJeremy L Thompson **/
15112b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1512beecbf24SJeremy L Thompson   bool is_composite;
1513beecbf24SJeremy L Thompson 
15142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1515beecbf24SJeremy L Thompson   if (is_composite) {
15161203703bSJeremy L Thompson     CeedInt       num_suboperators;
15171203703bSJeremy L Thompson     CeedOperator *sub_operators;
15181203703bSJeremy L Thompson 
15191203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15201203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15211203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15221203703bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(sub_operators[i], needs_data_update));
1523beecbf24SJeremy L Thompson     }
1524beecbf24SJeremy L Thompson   } else {
15257d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
15267d5185d7SSebastian Grimberg 
15277d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
15287d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, needs_data_update));
15298b919e6bSJeremy L Thompson   }
15308b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15318b919e6bSJeremy L Thompson }
15328b919e6bSJeremy L Thompson 
15338b919e6bSJeremy L Thompson /**
1534ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1535ea6b5821SJeremy L Thompson 
1536ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1537ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1538ea6b5821SJeremy L Thompson 
1539ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1540ea6b5821SJeremy L Thompson 
1541ea6b5821SJeremy L Thompson   @ref User
1542ea6b5821SJeremy L Thompson **/
1543ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1544ea6b5821SJeremy L Thompson   char  *name_copy;
1545ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1546ea6b5821SJeremy L Thompson 
15472b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1548ea6b5821SJeremy L Thompson   if (name_len > 0) {
15492b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1550ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1551ea6b5821SJeremy L Thompson     op->name = name_copy;
1552ea6b5821SJeremy L Thompson   }
1553ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1554ea6b5821SJeremy L Thompson }
1555ea6b5821SJeremy L Thompson 
1556ea6b5821SJeremy L Thompson /**
1557935f026aSJeremy L Thompson   @brief Core logic for viewing a `CeedOperator`
15587a982d89SJeremy L. Thompson 
1559935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1560ca94c3ddSJeremy L Thompson   @param[in] stream  Stream to write; typically `stdout` or a file
15618bf1b130SJames Wright   @param[in] is_full Whether to write full operator view or terse
15627a982d89SJeremy L. Thompson 
15637a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
15647a982d89SJeremy L. Thompson **/
15658bf1b130SJames Wright static int CeedOperatorView_Core(CeedOperator op, FILE *stream, bool is_full) {
156699f7f61fSJeremy L Thompson   bool has_name = op->name, is_composite, is_at_points;
15677a982d89SJeremy L. Thompson 
15681203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
156999f7f61fSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
15701203703bSJeremy L Thompson   if (is_composite) {
15711203703bSJeremy L Thompson     CeedInt       num_suboperators;
15721203703bSJeremy L Thompson     CeedOperator *sub_operators;
15731203703bSJeremy L Thompson 
15741203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15751203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15762b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
15777a982d89SJeremy L. Thompson 
15781203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15791203703bSJeremy L Thompson       has_name = sub_operators[i]->name;
158099f7f61fSJeremy L Thompson       fprintf(stream, "  SubOperator%s %" CeedInt_FMT "%s%s%s\n", is_at_points ? " AtPoints" : "", i, has_name ? " - " : "",
158199f7f61fSJeremy L Thompson               has_name ? sub_operators[i]->name : "", is_full ? ":" : "");
1582935f026aSJeremy L Thompson       if (is_full) CeedCall(CeedOperatorSingleView(sub_operators[i], 1, stream));
15837a982d89SJeremy L. Thompson     }
15847a982d89SJeremy L. Thompson   } else {
158599f7f61fSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s%s\n", is_at_points ? " AtPoints" : "", has_name ? " - " : "", has_name ? op->name : "");
1586935f026aSJeremy L Thompson     if (is_full) CeedCall(CeedOperatorSingleView(op, 0, stream));
15877a982d89SJeremy L. Thompson   }
1588e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15897a982d89SJeremy L. Thompson }
15903bd813ffSjeremylt 
15913bd813ffSjeremylt /**
1592935f026aSJeremy L Thompson   @brief View a `CeedOperator`
1593935f026aSJeremy L Thompson 
1594935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1595935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1596935f026aSJeremy L Thompson 
1597935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1598935f026aSJeremy L Thompson 
1599935f026aSJeremy L Thompson   @ref User
1600935f026aSJeremy L Thompson **/
1601935f026aSJeremy L Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1602935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, true));
1603935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1604935f026aSJeremy L Thompson }
1605935f026aSJeremy L Thompson 
1606935f026aSJeremy L Thompson /**
1607935f026aSJeremy L Thompson   @brief View a brief summary `CeedOperator`
1608935f026aSJeremy L Thompson 
1609935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1610935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1611935f026aSJeremy L Thompson 
1612935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1613935f026aSJeremy L Thompson 
1614935f026aSJeremy L Thompson   @ref User
1615935f026aSJeremy L Thompson **/
1616935f026aSJeremy L Thompson int CeedOperatorViewTerse(CeedOperator op, FILE *stream) {
1617935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, false));
1618935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1619935f026aSJeremy L Thompson }
1620935f026aSJeremy L Thompson 
1621935f026aSJeremy L Thompson /**
1622ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1623b7c9bbdaSJeremy L Thompson 
1624ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1625ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1626b7c9bbdaSJeremy L Thompson 
1627b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1628b7c9bbdaSJeremy L Thompson 
1629b7c9bbdaSJeremy L Thompson   @ref Advanced
1630b7c9bbdaSJeremy L Thompson **/
1631b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
16329bc66399SJeremy L Thompson   *ceed = NULL;
16339bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), ceed));
1634b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1635b7c9bbdaSJeremy L Thompson }
1636b7c9bbdaSJeremy L Thompson 
1637b7c9bbdaSJeremy L Thompson /**
16386e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
16396e536b99SJeremy L Thompson 
16406e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
16416e536b99SJeremy L Thompson 
16426e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
16436e536b99SJeremy L Thompson 
16446e536b99SJeremy L Thompson   @ref Advanced
16456e536b99SJeremy L Thompson **/
16466e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
16476e536b99SJeremy L Thompson 
16486e536b99SJeremy L Thompson /**
1649ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1650b7c9bbdaSJeremy L Thompson 
1651ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1652b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1653b7c9bbdaSJeremy L Thompson 
1654b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1655b7c9bbdaSJeremy L Thompson 
1656b7c9bbdaSJeremy L Thompson   @ref Advanced
1657b7c9bbdaSJeremy L Thompson **/
1658b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
16591203703bSJeremy L Thompson   bool is_composite;
16601203703bSJeremy L Thompson 
16611203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16626e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1663b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1664b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1665b7c9bbdaSJeremy L Thompson }
1666b7c9bbdaSJeremy L Thompson 
1667b7c9bbdaSJeremy L Thompson /**
1668ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1669b7c9bbdaSJeremy L Thompson 
1670ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1671b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1672b7c9bbdaSJeremy L Thompson 
1673b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1674b7c9bbdaSJeremy L Thompson 
1675b7c9bbdaSJeremy L Thompson   @ref Advanced
1676b7c9bbdaSJeremy L Thompson **/
1677b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
16781203703bSJeremy L Thompson   bool is_composite;
16791203703bSJeremy L Thompson 
16801203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16816e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1682b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1683b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1684b7c9bbdaSJeremy L Thompson }
1685b7c9bbdaSJeremy L Thompson 
1686b7c9bbdaSJeremy L Thompson /**
1687ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
16886e15d496SJeremy L Thompson 
1689ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1690ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
16916e15d496SJeremy L Thompson 
16926e15d496SJeremy L Thompson   @ref Backend
16936e15d496SJeremy L Thompson **/
16949d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
16956e15d496SJeremy L Thompson   bool is_composite;
16961c66c397SJeremy L Thompson 
16972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
16986e15d496SJeremy L Thompson 
16996e15d496SJeremy L Thompson   *flops = 0;
17002b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17016e15d496SJeremy L Thompson   if (is_composite) {
17026e15d496SJeremy L Thompson     CeedInt num_suboperators;
17031c66c397SJeremy L Thompson 
1704c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
17056e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1706c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
17076e15d496SJeremy L Thompson 
17086e15d496SJeremy L Thompson     // FLOPs for each suboperator
17096e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
17109d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
17111c66c397SJeremy L Thompson 
17122b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
17136e15d496SJeremy L Thompson       *flops += suboperator_flops;
17146e15d496SJeremy L Thompson     }
17156e15d496SJeremy L Thompson   } else {
17163f919cbcSJeremy L Thompson     bool                is_at_points;
17173f919cbcSJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0, num_points = 0;
17181203703bSJeremy L Thompson     CeedQFunction       qf;
17191203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
17201203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
17211c66c397SJeremy L Thompson 
17223f919cbcSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1723*19feff82SJeremy L Thompson     if (num_elem == 0) return CEED_ERROR_SUCCESS;
1724*19feff82SJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
17253f919cbcSJeremy L Thompson     if (is_at_points) {
17263f919cbcSJeremy L Thompson       CeedMemType         mem_type;
17273f919cbcSJeremy L Thompson       CeedElemRestriction rstr_points = NULL;
17283f919cbcSJeremy L Thompson 
17293f919cbcSJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
17303f919cbcSJeremy L Thompson       CeedCall(CeedGetPreferredMemType(CeedOperatorReturnCeed(op), &mem_type));
17313f919cbcSJeremy L Thompson       if (mem_type == CEED_MEM_DEVICE) {
17323f919cbcSJeremy L Thompson         // Device backends pad out to the same number of points per element
17333f919cbcSJeremy L Thompson         CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &num_points));
17343f919cbcSJeremy L Thompson       } else {
17353f919cbcSJeremy L Thompson         num_points = 0;
17363f919cbcSJeremy L Thompson         for (CeedInt i = 0; i < num_elem; i++) {
17373f919cbcSJeremy L Thompson           CeedInt points_in_elem = 0;
17383f919cbcSJeremy L Thompson 
17393f919cbcSJeremy L Thompson           CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr_points, i, &points_in_elem));
17403f919cbcSJeremy L Thompson           num_points += points_in_elem;
17413f919cbcSJeremy L Thompson         }
17423f919cbcSJeremy L Thompson         num_points = num_points / num_elem + (num_points % num_elem > 0);
17433f919cbcSJeremy L Thompson       }
17443f919cbcSJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
17453f919cbcSJeremy L Thompson     }
17461203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
17471203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
1748c11e12f4SJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf));
17491203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
17504385fb7fSSebastian Grimberg 
17516e15d496SJeremy L Thompson     // Input FLOPs
17526e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
17531203703bSJeremy L Thompson       CeedVector vec;
17546e15d496SJeremy L Thompson 
17551203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
17561203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17571203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17581203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17591203703bSJeremy L Thompson         CeedElemRestriction rstr;
17601203703bSJeremy L Thompson         CeedBasis           basis;
17611203703bSJeremy L Thompson 
17621203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
17631203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1764681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1765edb2538eSJeremy L Thompson         *flops += rstr_flops;
17661203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
17671203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
17683f919cbcSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
1769681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
17706e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17716e15d496SJeremy L Thompson       }
1772681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
17736e15d496SJeremy L Thompson     }
17746e15d496SJeremy L Thompson     // QF FLOPs
17751c66c397SJeremy L Thompson     {
17769d36ca50SJeremy L Thompson       CeedInt       num_qpts;
17779d36ca50SJeremy L Thompson       CeedSize      qf_flops;
17781203703bSJeremy L Thompson       CeedQFunction qf;
17791c66c397SJeremy L Thompson 
17803f919cbcSJeremy L Thompson       if (is_at_points) num_qpts = num_points;
17813f919cbcSJeremy L Thompson       else CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
17821203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
17831203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
1784c11e12f4SJeremy L Thompson       CeedCall(CeedQFunctionDestroy(&qf));
17856e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
17866e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
17876e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
17881c66c397SJeremy L Thompson     }
17891c66c397SJeremy L Thompson 
17906e15d496SJeremy L Thompson     // Output FLOPs
17916e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
17921203703bSJeremy L Thompson       CeedVector vec;
17936e15d496SJeremy L Thompson 
17941203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
17951203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17961203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17971203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17981203703bSJeremy L Thompson         CeedElemRestriction rstr;
17991203703bSJeremy L Thompson         CeedBasis           basis;
18001203703bSJeremy L Thompson 
18011203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
18021203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1803681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1804edb2538eSJeremy L Thompson         *flops += rstr_flops;
18051203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
18061203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
18073f919cbcSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
1808681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
18096e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
18106e15d496SJeremy L Thompson       }
1811681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
18126e15d496SJeremy L Thompson     }
18136e15d496SJeremy L Thompson   }
18146e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18156e15d496SJeremy L Thompson }
18166e15d496SJeremy L Thompson 
18176e15d496SJeremy L Thompson /**
1818ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
18199fd66db6SSebastian Grimberg 
1820ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1821512bb800SJeremy L Thompson 
1822ca94c3ddSJeremy L Thompson   Note: If the value of `ctx` passed into this function is non-`NULL`, then it is assumed that `ctx` is a pointer to a `CeedQFunctionContext`.
1823ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
18240126412dSJeremy L Thompson 
1825ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1826ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
18270126412dSJeremy L Thompson 
18280126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18290126412dSJeremy L Thompson 
1830859c15bbSJames Wright   @ref Advanced
18310126412dSJeremy L Thompson **/
18320126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
18331203703bSJeremy L Thompson   bool                 is_composite;
18341203703bSJeremy L Thompson   CeedQFunction        qf;
18351203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
18361203703bSJeremy L Thompson 
18371203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
18386e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
18391203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
18401203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
1841c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
18421485364cSJeremy L Thompson   *ctx = NULL;
18431203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
18440126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
18450126412dSJeremy L Thompson }
18460126412dSJeremy L Thompson 
18470126412dSJeremy L Thompson /**
1848ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
18493668ca4bSJeremy L Thompson 
1850ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1851859c15bbSJames Wright 
1852ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18533668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
18543668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
18553668ca4bSJeremy L Thompson 
18563668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18573668ca4bSJeremy L Thompson 
18583668ca4bSJeremy L Thompson   @ref User
18593668ca4bSJeremy L Thompson **/
186017b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
18611c66c397SJeremy L Thompson   bool is_composite, field_found = false;
18621c66c397SJeremy L Thompson 
18632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
18642b730f8bSJeremy L Thompson 
18653668ca4bSJeremy L Thompson   if (is_composite) {
1866a98a090bSJeremy L Thompson     // Composite operator
1867a98a090bSJeremy L Thompson     // -- Check if composite label already created
18683668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
18693668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
18703668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
18713668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
18723668ca4bSJeremy L Thompson       }
18733668ca4bSJeremy L Thompson     }
18743668ca4bSJeremy L Thompson 
1875a98a090bSJeremy L Thompson     // -- Create composite label if needed
18763668ca4bSJeremy L Thompson     CeedInt               num_sub;
18773668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
18783668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
18793668ca4bSJeremy L Thompson 
18802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1881c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1882c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
18832b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
18843668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
18853668ca4bSJeremy L Thompson 
18863668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
18873668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
18883668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
18891c66c397SJeremy L Thompson 
18902b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
18913668ca4bSJeremy L Thompson         if (new_field_label_i) {
1892a98a090bSJeremy L Thompson           field_found                    = true;
18933668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
18943668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
18953668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
18962b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
18977bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18982b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18996e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
19002b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
19017bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
19027bfe0f0eSJeremy L Thompson           } else {
19037bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
19047bfe0f0eSJeremy L Thompson           }
19052b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
19067bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
19072b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
19086e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
19096e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
19106e536b99SJeremy L Thompson                              new_field_label_i->num_values);
19117bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
19127bfe0f0eSJeremy L Thompson           } else {
19137bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
19147bfe0f0eSJeremy L Thompson           }
19153668ca4bSJeremy L Thompson         }
19163668ca4bSJeremy L Thompson       }
19173668ca4bSJeremy L Thompson     }
1918a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1919a98a090bSJeremy L Thompson     if (field_found) {
1920a98a090bSJeremy L Thompson       *field_label = new_field_label;
1921a98a090bSJeremy L Thompson     } else {
19223668ca4bSJeremy L Thompson       // LCOV_EXCL_START
19232b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
19242b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
19253668ca4bSJeremy L Thompson       *field_label = NULL;
19263668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
19275ac9af79SJeremy L Thompson     }
19285ac9af79SJeremy L Thompson   } else {
19291203703bSJeremy L Thompson     CeedQFunction        qf;
19301203703bSJeremy L Thompson     CeedQFunctionContext ctx;
19311203703bSJeremy L Thompson 
1932a98a090bSJeremy L Thompson     // Single, non-composite operator
19331203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
19341203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
1935c11e12f4SJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf));
19361203703bSJeremy L Thompson     if (ctx) {
19371203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1938a98a090bSJeremy L Thompson     } else {
1939a98a090bSJeremy L Thompson       *field_label = NULL;
1940a98a090bSJeremy L Thompson     }
19415ac9af79SJeremy L Thompson   }
19425ac9af79SJeremy L Thompson 
19435ac9af79SJeremy L Thompson   // Set label in operator
19445ac9af79SJeremy L Thompson   if (*field_label) {
19455ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
19465ac9af79SJeremy L Thompson 
19473668ca4bSJeremy L Thompson     // Move new composite label to operator
19483668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
19492b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
19503668ca4bSJeremy L Thompson       op->max_context_labels = 1;
19513668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
19522b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
19533668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
19543668ca4bSJeremy L Thompson     }
19555ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
19563668ca4bSJeremy L Thompson     op->num_context_labels++;
19573668ca4bSJeremy L Thompson   }
19583668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
19593668ca4bSJeremy L Thompson }
19603668ca4bSJeremy L Thompson 
19613668ca4bSJeremy L Thompson /**
1962ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
19634385fb7fSSebastian Grimberg 
1964ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1965d8dd9a91SJeremy L Thompson 
1966ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19672788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1968ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1969d8dd9a91SJeremy L Thompson 
1970d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1971d8dd9a91SJeremy L Thompson 
1972d8dd9a91SJeremy L Thompson   @ref User
1973d8dd9a91SJeremy L Thompson **/
197417b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
19752b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1976d8dd9a91SJeremy L Thompson }
1977d8dd9a91SJeremy L Thompson 
1978d8dd9a91SJeremy L Thompson /**
1979ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
19804385fb7fSSebastian Grimberg 
1981ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19822788fa27SJeremy L Thompson 
1983ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19842788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
19852788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
19862788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19872788fa27SJeremy L Thompson 
19882788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19892788fa27SJeremy L Thompson 
19902788fa27SJeremy L Thompson   @ref User
19912788fa27SJeremy L Thompson **/
199217b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
19932788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
19942788fa27SJeremy L Thompson }
19952788fa27SJeremy L Thompson 
19962788fa27SJeremy L Thompson /**
1997ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
19982788fa27SJeremy L Thompson 
1999ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20002788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
20012788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20022788fa27SJeremy L Thompson 
20032788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20042788fa27SJeremy L Thompson 
20052788fa27SJeremy L Thompson   @ref User
20062788fa27SJeremy L Thompson **/
200717b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
20082788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
20092788fa27SJeremy L Thompson }
20102788fa27SJeremy L Thompson 
20112788fa27SJeremy L Thompson /**
2012ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
20134385fb7fSSebastian Grimberg 
2014ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
2015d8dd9a91SJeremy L Thompson 
2016ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
2017ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
2018ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
2019d8dd9a91SJeremy L Thompson 
2020d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2021d8dd9a91SJeremy L Thompson 
2022d8dd9a91SJeremy L Thompson   @ref User
2023d8dd9a91SJeremy L Thompson **/
202423dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
20252b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
2026d8dd9a91SJeremy L Thompson }
2027d8dd9a91SJeremy L Thompson 
2028d8dd9a91SJeremy L Thompson /**
2029ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
20304385fb7fSSebastian Grimberg 
2031ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
20322788fa27SJeremy L Thompson 
2033ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20342788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
2035ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
20362788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20372788fa27SJeremy L Thompson 
20382788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20392788fa27SJeremy L Thompson 
20402788fa27SJeremy L Thompson   @ref User
20412788fa27SJeremy L Thompson **/
204223dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
20432788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
20442788fa27SJeremy L Thompson }
20452788fa27SJeremy L Thompson 
20462788fa27SJeremy L Thompson /**
2047ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
20482788fa27SJeremy L Thompson 
2049ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20502788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
20512788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20522788fa27SJeremy L Thompson 
20532788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20542788fa27SJeremy L Thompson 
20552788fa27SJeremy L Thompson   @ref User
20562788fa27SJeremy L Thompson **/
205723dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
20582788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
20592788fa27SJeremy L Thompson }
20602788fa27SJeremy L Thompson 
20612788fa27SJeremy L Thompson /**
2062ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
20635b6ec284SJeremy L Thompson 
2064ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
20655b6ec284SJeremy L Thompson 
2066ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
20675b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
20685b6ec284SJeremy L Thompson   @param[in]     values      Values to set
20695b6ec284SJeremy L Thompson 
20705b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20715b6ec284SJeremy L Thompson 
20725b6ec284SJeremy L Thompson   @ref User
20735b6ec284SJeremy L Thompson **/
20745b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
20755b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20765b6ec284SJeremy L Thompson }
20775b6ec284SJeremy L Thompson 
20785b6ec284SJeremy L Thompson /**
2079ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
20805b6ec284SJeremy L Thompson 
2081ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
20825b6ec284SJeremy L Thompson 
2083ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20845b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
2085ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
20865b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20875b6ec284SJeremy L Thompson 
20885b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20895b6ec284SJeremy L Thompson 
20905b6ec284SJeremy L Thompson   @ref User
20915b6ec284SJeremy L Thompson **/
20925b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
20935b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
20945b6ec284SJeremy L Thompson }
20955b6ec284SJeremy L Thompson 
20965b6ec284SJeremy L Thompson /**
2097ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
20985b6ec284SJeremy L Thompson 
2099ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
21005b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
21015b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
21025b6ec284SJeremy L Thompson 
21035b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
21045b6ec284SJeremy L Thompson 
21055b6ec284SJeremy L Thompson   @ref User
21065b6ec284SJeremy L Thompson **/
21075b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
21085b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
21095b6ec284SJeremy L Thompson }
21105b6ec284SJeremy L Thompson 
21115b6ec284SJeremy L Thompson /**
2112ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
2113d7b241e6Sjeremylt 
2114ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2115ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2116d7b241e6Sjeremylt 
2117ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2118f04ea552SJeremy L Thompson 
2119ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2120ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2121ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to store result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
2122ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2123b11c1e72Sjeremylt 
2124b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2125dfdf5a53Sjeremylt 
21267a982d89SJeremy L. Thompson   @ref User
2127b11c1e72Sjeremylt **/
21282b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
21291203703bSJeremy L Thompson   bool is_composite;
21301203703bSJeremy L Thompson 
21312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2132d7b241e6Sjeremylt 
21331203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
21341203703bSJeremy L Thompson   if (is_composite) {
2135250756a7Sjeremylt     // Composite Operator
2136250756a7Sjeremylt     if (op->ApplyComposite) {
21372b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
2138250756a7Sjeremylt     } else {
2139d1d35e2fSjeremylt       CeedInt       num_suboperators;
2140d1d35e2fSjeremylt       CeedOperator *sub_operators;
21411c66c397SJeremy L Thompson 
21421c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2143c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2144250756a7Sjeremylt 
2145250756a7Sjeremylt       // Zero all output vectors
21463ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2147d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
21481203703bSJeremy L Thompson         CeedInt            num_output_fields;
21491203703bSJeremy L Thompson         CeedOperatorField *output_fields;
21501c66c397SJeremy L Thompson 
21511203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
21521203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
21531203703bSJeremy L Thompson           CeedVector vec;
21541203703bSJeremy L Thompson 
21551203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2156250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
21572b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2158250756a7Sjeremylt           }
2159681d0ea7SJeremy L Thompson           CeedCall(CeedVectorDestroy(&vec));
2160250756a7Sjeremylt         }
2161250756a7Sjeremylt       }
2162250756a7Sjeremylt       // Apply
2163f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2164f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2165cae8b89aSjeremylt       }
2166cae8b89aSjeremylt     }
21673ca2b39bSJeremy L Thompson   } else {
21683ca2b39bSJeremy L Thompson     // Standard Operator
21693ca2b39bSJeremy L Thompson     if (op->Apply) {
21703ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
21713ca2b39bSJeremy L Thompson     } else {
21721203703bSJeremy L Thompson       CeedInt            num_output_fields;
21731203703bSJeremy L Thompson       CeedOperatorField *output_fields;
21741203703bSJeremy L Thompson 
21751203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
21763ca2b39bSJeremy L Thompson       // Zero all output vectors
21771203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2178681d0ea7SJeremy L Thompson         bool       is_active;
21791203703bSJeremy L Thompson         CeedVector vec;
21801c66c397SJeremy L Thompson 
21811203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
2182681d0ea7SJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
2183681d0ea7SJeremy L Thompson         if (is_active) vec = out;
21843ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2185681d0ea7SJeremy L Thompson         if (!is_active) CeedCall(CeedVectorDestroy(&vec));
21863ca2b39bSJeremy L Thompson       }
21873ca2b39bSJeremy L Thompson       // Apply
21881203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
21893ca2b39bSJeremy L Thompson     }
2190250756a7Sjeremylt   }
2191e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2192cae8b89aSjeremylt }
2193cae8b89aSjeremylt 
2194cae8b89aSjeremylt /**
2195ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2196cae8b89aSjeremylt 
2197ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2198ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2199cae8b89aSjeremylt 
2200ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2201ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2202ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
2203ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2204cae8b89aSjeremylt 
2205cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2206cae8b89aSjeremylt 
22077a982d89SJeremy L. Thompson   @ref User
2208cae8b89aSjeremylt **/
22092b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
22101203703bSJeremy L Thompson   bool is_composite;
22111203703bSJeremy L Thompson 
22122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2213cae8b89aSjeremylt 
22141203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
22151203703bSJeremy L Thompson   if (is_composite) {
2216250756a7Sjeremylt     // Composite Operator
2217250756a7Sjeremylt     if (op->ApplyAddComposite) {
22182b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2219cae8b89aSjeremylt     } else {
2220d1d35e2fSjeremylt       CeedInt       num_suboperators;
2221d1d35e2fSjeremylt       CeedOperator *sub_operators;
2222250756a7Sjeremylt 
22231c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
22241c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2225d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
22262b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
22271d7d2407SJeremy L Thompson       }
2228250756a7Sjeremylt     }
22291203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
22303ca2b39bSJeremy L Thompson     // Standard Operator
22313ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2232250756a7Sjeremylt   }
2233e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2234d7b241e6Sjeremylt }
2235d7b241e6Sjeremylt 
2236d7b241e6Sjeremylt /**
2237536b928cSSebastian Grimberg   @brief Destroy temporary assembly data associated with a `CeedOperator`
2238536b928cSSebastian Grimberg 
2239536b928cSSebastian Grimberg   @param[in,out] op `CeedOperator` whose assembly data to destroy
2240536b928cSSebastian Grimberg 
2241536b928cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2242536b928cSSebastian Grimberg 
2243536b928cSSebastian Grimberg   @ref User
2244536b928cSSebastian Grimberg **/
2245536b928cSSebastian Grimberg int CeedOperatorAssemblyDataStrip(CeedOperator op) {
2246536b928cSSebastian Grimberg   bool is_composite;
2247536b928cSSebastian Grimberg 
2248536b928cSSebastian Grimberg   CeedCall(CeedQFunctionAssemblyDataDestroy(&op->qf_assembled));
2249536b928cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataDestroy(&op->op_assembled));
2250536b928cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2251536b928cSSebastian Grimberg   if (is_composite) {
2252536b928cSSebastian Grimberg     CeedInt       num_suboperators;
2253536b928cSSebastian Grimberg     CeedOperator *sub_operators;
2254536b928cSSebastian Grimberg 
2255536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2256536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2257536b928cSSebastian Grimberg     for (CeedInt i = 0; i < num_suboperators; i++) {
2258536b928cSSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataDestroy(&sub_operators[i]->qf_assembled));
2259536b928cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataDestroy(&sub_operators[i]->op_assembled));
2260536b928cSSebastian Grimberg     }
2261536b928cSSebastian Grimberg   }
2262536b928cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2263536b928cSSebastian Grimberg }
2264536b928cSSebastian Grimberg 
2265536b928cSSebastian Grimberg /**
2266ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2267d7b241e6Sjeremylt 
2268ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2269b11c1e72Sjeremylt 
2270b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2271dfdf5a53Sjeremylt 
22727a982d89SJeremy L. Thompson   @ref User
2273b11c1e72Sjeremylt **/
2274d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2275ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2276ad6481ceSJeremy L Thompson     *op = NULL;
2277ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2278ad6481ceSJeremy L Thompson   }
2279f883f0a5SSebastian Grimberg   // Backend destroy
2280f883f0a5SSebastian Grimberg   if ((*op)->Destroy) {
2281f883f0a5SSebastian Grimberg     CeedCall((*op)->Destroy(*op));
2282f883f0a5SSebastian Grimberg   }
2283fe2413ffSjeremylt   // Free fields
22842b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2285d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2286437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2287437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
228815910d16Sjeremylt       }
2289356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
22902b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
229171352a93Sjeremylt       }
22922b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
22932b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
229471352a93Sjeremylt       }
22952b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
22962b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2297fe2413ffSjeremylt     }
22982b730f8bSJeremy L Thompson   }
22992b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2300d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2301437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2302356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
23032b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
230471352a93Sjeremylt       }
23052b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
23062b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
230771352a93Sjeremylt       }
23082b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
23092b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
23102b730f8bSJeremy L Thompson     }
2311fe2413ffSjeremylt   }
2312f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->input_fields));
2313f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->output_fields));
2314f883f0a5SSebastian Grimberg   // Destroy AtPoints data
231548acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
231648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
231748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2318c6b536a8SSebastian Grimberg   // Destroy assembly data (must happen before destroying sub_operators)
2319f883f0a5SSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataStrip(*op));
2320d1d35e2fSjeremylt   // Destroy sub_operators
23212b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2322d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
23232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
232452d6035fSJeremy L Thompson     }
23252b730f8bSJeremy L Thompson   }
2326f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->sub_operators));
23272b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
23282b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
23292b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
23303668ca4bSJeremy L Thompson   // Destroy any composite labels
23315ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
23323668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
23332b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
23342b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
23353668ca4bSJeremy L Thompson     }
23365ac9af79SJeremy L Thompson   }
23372b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2338fe2413ffSjeremylt 
23395107b09fSJeremy L Thompson   // Destroy fallback
23402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
23415107b09fSJeremy L Thompson 
23422b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
2343f883f0a5SSebastian Grimberg   CeedCall(CeedDestroy(&(*op)->ceed));
23442b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2345e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2346d7b241e6Sjeremylt }
2347d7b241e6Sjeremylt 
2348d7b241e6Sjeremylt /// @}
2349