xref: /libCEED/interface/ceed-operator.c (revision 8a297abd0abffb6af7c8cbd4ffcd5e5f554c8ef0) !
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);
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227a982d89SJeremy L. Thompson }
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson /**
125ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1267a982d89SJeremy L. Thompson 
127ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1287a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
129ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1327a982d89SJeremy L. Thompson 
1337a982d89SJeremy L. Thompson   @ref Utility
1347a982d89SJeremy L. Thompson **/
1357a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1367a982d89SJeremy L. Thompson   const char         *pre = sub ? "  " : "";
1371203703bSJeremy L Thompson   CeedInt             num_elem, num_qpts, total_fields = 0, num_input_fields, num_output_fields;
1381203703bSJeremy L Thompson   CeedQFunction       qf;
1391203703bSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1401203703bSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1417a982d89SJeremy L. Thompson 
1422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1451203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1461203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1471203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1481c66c397SJeremy L Thompson 
1492b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1502b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1511203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, num_input_fields, num_input_fields > 1 ? "s" : "");
1521203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1531203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_input_fields[i], qf_input_fields[i], i, sub, 1, stream));
1547a982d89SJeremy L. Thompson   }
1551203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, num_output_fields, num_output_fields > 1 ? "s" : "");
1561203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1571203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_output_fields[i], qf_output_fields[i], i, sub, 0, stream));
1587a982d89SJeremy L. Thompson   }
159e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1607a982d89SJeremy L. Thompson }
1617a982d89SJeremy L. Thompson 
162d99fa3c5SJeremy L Thompson /**
163ca94c3ddSJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`
164eaf62fffSJeremy L Thompson 
165ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
166ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
167eaf62fffSJeremy L Thompson 
168eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
169eaf62fffSJeremy L Thompson 
170eaf62fffSJeremy L Thompson   @ref Developer
171eaf62fffSJeremy L Thompson **/
172eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
173506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
174506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
175506b1a0cSSebastian Grimberg }
176506b1a0cSSebastian Grimberg 
177506b1a0cSSebastian Grimberg /**
178ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`
179506b1a0cSSebastian Grimberg 
180ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
181ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
182ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
183506b1a0cSSebastian Grimberg 
184506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
185506b1a0cSSebastian Grimberg 
186506b1a0cSSebastian Grimberg   @ref Developer
187506b1a0cSSebastian Grimberg **/
188506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1891203703bSJeremy L Thompson   bool               is_composite;
1901203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
1916aaed0edSJeremy L Thompson   Ceed               ceed;
1921203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
1931c66c397SJeremy L Thompson 
1946aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1951203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1961203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1971203703bSJeremy L Thompson 
198506b1a0cSSebastian Grimberg   if (active_input_basis) {
199506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
2001203703bSJeremy L Thompson     if (!is_composite) {
2011203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2021203703bSJeremy L Thompson         CeedVector vec;
2031203703bSJeremy L Thompson 
2041203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2051203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2061203703bSJeremy L Thompson           CeedBasis basis;
2071203703bSJeremy L Thompson 
2081203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
2091203703bSJeremy L Thompson           CeedCheck(!*active_input_basis || *active_input_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active input CeedBases found");
2101203703bSJeremy L Thompson           *active_input_basis = basis;
211eaf62fffSJeremy L Thompson         }
2122b730f8bSJeremy L Thompson       }
213506b1a0cSSebastian Grimberg       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
214506b1a0cSSebastian Grimberg     }
215506b1a0cSSebastian Grimberg   }
216506b1a0cSSebastian Grimberg   if (active_output_basis) {
217506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
2181203703bSJeremy L Thompson     if (!is_composite) {
2191203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2201203703bSJeremy L Thompson         CeedVector vec;
2211203703bSJeremy L Thompson 
2221203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_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_output_fields[i], &basis));
2271203703bSJeremy L Thompson           CeedCheck(!*active_output_basis || *active_output_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active output CeedBases found");
2281203703bSJeremy L Thompson           *active_output_basis = basis;
229506b1a0cSSebastian Grimberg         }
230506b1a0cSSebastian Grimberg       }
231506b1a0cSSebastian Grimberg       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
232506b1a0cSSebastian Grimberg     }
233506b1a0cSSebastian Grimberg   }
234eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
235eaf62fffSJeremy L Thompson }
236eaf62fffSJeremy L Thompson 
237eaf62fffSJeremy L Thompson /**
238ca94c3ddSJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`
239e2f04181SAndrew T. Barker 
240ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
241ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
242e2f04181SAndrew T. Barker 
243e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
244e2f04181SAndrew T. Barker 
245e2f04181SAndrew T. Barker   @ref Utility
246e2f04181SAndrew T. Barker **/
2472b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
248506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
249506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
250506b1a0cSSebastian Grimberg }
251506b1a0cSSebastian Grimberg 
252506b1a0cSSebastian Grimberg /**
253ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`
254506b1a0cSSebastian Grimberg 
255ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
256ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
257ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
258506b1a0cSSebastian Grimberg 
259506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
260506b1a0cSSebastian Grimberg 
261506b1a0cSSebastian Grimberg   @ref Utility
262506b1a0cSSebastian Grimberg **/
263506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2641203703bSJeremy L Thompson   bool               is_composite;
2651203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2666aaed0edSJeremy L Thompson   Ceed               ceed;
2671203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2681c66c397SJeremy L Thompson 
2696aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2701203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2711203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2721203703bSJeremy L Thompson 
273506b1a0cSSebastian Grimberg   if (active_input_rstr) {
274506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
2751203703bSJeremy L Thompson     if (!is_composite) {
2761203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2771203703bSJeremy L Thompson         CeedVector vec;
2781203703bSJeremy L Thompson 
2791203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2801203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2811203703bSJeremy L Thompson           CeedElemRestriction rstr;
2821203703bSJeremy L Thompson 
2831203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
2841203703bSJeremy L Thompson           CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active input CeedElemRestrictions found");
2851203703bSJeremy L Thompson           *active_input_rstr = rstr;
286e2f04181SAndrew T. Barker         }
2872b730f8bSJeremy L Thompson       }
288506b1a0cSSebastian Grimberg       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
289506b1a0cSSebastian Grimberg     }
290506b1a0cSSebastian Grimberg   }
291506b1a0cSSebastian Grimberg   if (active_output_rstr) {
292506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
2931203703bSJeremy L Thompson     if (!is_composite) {
2941203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2951203703bSJeremy L Thompson         CeedVector vec;
2961203703bSJeremy L Thompson 
2971203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2981203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2991203703bSJeremy L Thompson           CeedElemRestriction rstr;
3001203703bSJeremy L Thompson 
3011203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
3021203703bSJeremy L Thompson           CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active output CeedElemRestrictions found");
3031203703bSJeremy L Thompson           *active_output_rstr = rstr;
304506b1a0cSSebastian Grimberg         }
305506b1a0cSSebastian Grimberg       }
306506b1a0cSSebastian Grimberg       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
307506b1a0cSSebastian Grimberg     }
308506b1a0cSSebastian Grimberg   }
309e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
310e2f04181SAndrew T. Barker }
311e2f04181SAndrew T. Barker 
312d8dd9a91SJeremy L Thompson /**
313ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
3144385fb7fSSebastian Grimberg 
315ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
316ca94c3ddSJeremy 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.
317d8dd9a91SJeremy L Thompson 
318ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
319ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
320ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
3212788fa27SJeremy L Thompson   @param[in]     values      Values to set
322d8dd9a91SJeremy L Thompson 
323d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
324d8dd9a91SJeremy L Thompson 
325d8dd9a91SJeremy L Thompson   @ref User
326d8dd9a91SJeremy L Thompson **/
3272788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3281c66c397SJeremy L Thompson   bool is_composite = false;
3291203703bSJeremy L Thompson   Ceed ceed;
3301c66c397SJeremy L Thompson 
3311203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3321203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3333668ca4bSJeremy L Thompson 
3345ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3355ac9af79SJeremy L Thompson   if (field_label->from_op) {
3365ac9af79SJeremy L Thompson     CeedInt index = -1;
3375ac9af79SJeremy L Thompson 
3385ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3395ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3405ac9af79SJeremy L Thompson     }
3411203703bSJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3425ac9af79SJeremy L Thompson   }
3435ac9af79SJeremy L Thompson 
3442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
345d8dd9a91SJeremy L Thompson   if (is_composite) {
346d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
347d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
348d8dd9a91SJeremy L Thompson 
349c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
350c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3511203703bSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
352d8dd9a91SJeremy L Thompson 
353d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3541203703bSJeremy L Thompson       CeedQFunction        qf;
3551203703bSJeremy L Thompson       CeedQFunctionContext ctx;
3561203703bSJeremy L Thompson 
3571203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
3581203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
359d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3601203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
3611203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values));
362d8dd9a91SJeremy L Thompson       }
363d8dd9a91SJeremy L Thompson     }
364d8dd9a91SJeremy L Thompson   } else {
3651203703bSJeremy L Thompson     CeedQFunction        qf;
3661203703bSJeremy L Thompson     CeedQFunctionContext ctx;
3671203703bSJeremy L Thompson 
3681203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
3691203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
3701203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3711203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values));
3722788fa27SJeremy L Thompson   }
3736d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3742788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3752788fa27SJeremy L Thompson }
3762788fa27SJeremy L Thompson 
3772788fa27SJeremy L Thompson /**
378ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3794385fb7fSSebastian Grimberg 
380ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
381ca94c3ddSJeremy 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.
3822788fa27SJeremy L Thompson 
383ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3842788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3852788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
386c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
387c5d0f995SJed Brown   @param[out]    values      Values in the label
3882788fa27SJeremy L Thompson 
3892788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3902788fa27SJeremy L Thompson 
3912788fa27SJeremy L Thompson   @ref User
3922788fa27SJeremy L Thompson **/
3932788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3942788fa27SJeremy L Thompson                                              void *values) {
3951c66c397SJeremy L Thompson   bool is_composite = false;
3961203703bSJeremy L Thompson   Ceed ceed;
3971c66c397SJeremy L Thompson 
3981203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3991203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4002788fa27SJeremy L Thompson 
4012788fa27SJeremy L Thompson   *(void **)values = NULL;
4022788fa27SJeremy L Thompson   *num_values      = 0;
4032788fa27SJeremy L Thompson 
4045ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4055ac9af79SJeremy L Thompson   if (field_label->from_op) {
4065ac9af79SJeremy L Thompson     CeedInt index = -1;
4075ac9af79SJeremy L Thompson 
4085ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4095ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4105ac9af79SJeremy L Thompson     }
4116e536b99SJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4125ac9af79SJeremy L Thompson   }
4135ac9af79SJeremy L Thompson 
4142788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4152788fa27SJeremy L Thompson   if (is_composite) {
4162788fa27SJeremy L Thompson     CeedInt       num_sub;
4172788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4182788fa27SJeremy L Thompson 
4192788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4202788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4211203703bSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
4222788fa27SJeremy L Thompson 
4232788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4241203703bSJeremy L Thompson       CeedQFunction        qf;
4251203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4261203703bSJeremy L Thompson 
4271203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
4281203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
4292788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4301203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
4311203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values));
4322788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4332788fa27SJeremy L Thompson       }
4342788fa27SJeremy L Thompson     }
4352788fa27SJeremy L Thompson   } else {
4361203703bSJeremy L Thompson     CeedQFunction        qf;
4371203703bSJeremy L Thompson     CeedQFunctionContext ctx;
4381203703bSJeremy L Thompson 
4391203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
4401203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
4411203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4421203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values));
4432788fa27SJeremy L Thompson   }
4442788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4452788fa27SJeremy L Thompson }
4462788fa27SJeremy L Thompson 
4472788fa27SJeremy L Thompson /**
448ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
4494385fb7fSSebastian Grimberg 
450ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
451ca94c3ddSJeremy 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.
4522788fa27SJeremy L Thompson 
453ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4542788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4552788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
456c5d0f995SJed Brown   @param[in]     values      Values array to restore
4572788fa27SJeremy L Thompson 
4582788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4592788fa27SJeremy L Thompson 
4602788fa27SJeremy L Thompson   @ref User
4612788fa27SJeremy L Thompson **/
4622788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4631c66c397SJeremy L Thompson   bool is_composite = false;
4641203703bSJeremy L Thompson   Ceed ceed;
4651c66c397SJeremy L Thompson 
4661203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
4671203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4682788fa27SJeremy L Thompson 
4695ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4705ac9af79SJeremy L Thompson   if (field_label->from_op) {
4715ac9af79SJeremy L Thompson     CeedInt index = -1;
4725ac9af79SJeremy L Thompson 
4735ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4745ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4755ac9af79SJeremy L Thompson     }
4766e536b99SJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4775ac9af79SJeremy L Thompson   }
4785ac9af79SJeremy L Thompson 
4792788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4802788fa27SJeremy L Thompson   if (is_composite) {
4812788fa27SJeremy L Thompson     CeedInt       num_sub;
4822788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4832788fa27SJeremy L Thompson 
4842788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4852788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4866e536b99SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
4872788fa27SJeremy L Thompson 
4882788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4891203703bSJeremy L Thompson       CeedQFunction        qf;
4901203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4911203703bSJeremy L Thompson 
4921203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
4931203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
4942788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4951203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
4961203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values));
4972788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4982788fa27SJeremy L Thompson       }
4992788fa27SJeremy L Thompson     }
5002788fa27SJeremy L Thompson   } else {
5011203703bSJeremy L Thompson     CeedQFunction        qf;
5021203703bSJeremy L Thompson     CeedQFunctionContext ctx;
5031203703bSJeremy L Thompson 
5041203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
5051203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
5061203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
5071203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values));
508d8dd9a91SJeremy L Thompson   }
509d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
510d8dd9a91SJeremy L Thompson }
511d8dd9a91SJeremy L Thompson 
5127a982d89SJeremy L. Thompson /// @}
5137a982d89SJeremy L. Thompson 
5147a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5157a982d89SJeremy L. Thompson /// CeedOperator Backend API
5167a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5177a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
5187a982d89SJeremy L. Thompson /// @{
5197a982d89SJeremy L. Thompson 
5207a982d89SJeremy L. Thompson /**
521ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
5227a982d89SJeremy L. Thompson 
523ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
524d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
5257a982d89SJeremy L. Thompson 
5267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson   @ref Backend
5297a982d89SJeremy L. Thompson **/
530d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
5311203703bSJeremy L Thompson   bool is_composite;
5321203703bSJeremy L Thompson 
5331203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5346e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operators");
535d1d35e2fSjeremylt   *num_args = op->num_fields;
536e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5377a982d89SJeremy L. Thompson }
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson /**
5409463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
5419463e855SJeremy L Thompson 
5429463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
5439463e855SJeremy L Thompson 
5449463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
5459463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
5469463e855SJeremy L Thompson 
5479463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5489463e855SJeremy L Thompson 
5499463e855SJeremy L Thompson   @ref Backend
5509463e855SJeremy L Thompson **/
5519463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
5529463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
5539463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
5549463e855SJeremy L Thompson 
5559463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
5569463e855SJeremy L Thompson   *has_tensor_bases = true;
5579463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
5589463e855SJeremy L Thompson     bool      is_tensor;
5599463e855SJeremy L Thompson     CeedBasis basis;
5609463e855SJeremy L Thompson 
5619463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
5629463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5639463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5649463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5659463e855SJeremy L Thompson     }
5669463e855SJeremy L Thompson   }
5679463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
5689463e855SJeremy L Thompson     bool      is_tensor;
5699463e855SJeremy L Thompson     CeedBasis basis;
5709463e855SJeremy L Thompson 
5719463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
5729463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5739463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5749463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5759463e855SJeremy L Thompson     }
5769463e855SJeremy L Thompson   }
5779463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5789463e855SJeremy L Thompson }
5799463e855SJeremy L Thompson 
5809463e855SJeremy L Thompson /**
5811203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is immutable
5821203703bSJeremy L Thompson 
5831203703bSJeremy L Thompson   @param[in]  op           `CeedOperator`
5841203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5851203703bSJeremy L Thompson 
5861203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5871203703bSJeremy L Thompson 
5881203703bSJeremy L Thompson   @ref Backend
5891203703bSJeremy L Thompson **/
5901203703bSJeremy L Thompson int CeedOperatorIsImmutable(CeedOperator op, bool *is_immutable) {
5911203703bSJeremy L Thompson   *is_immutable = op->is_immutable;
5921203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5931203703bSJeremy L Thompson }
5941203703bSJeremy L Thompson 
5951203703bSJeremy L Thompson /**
596ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
5977a982d89SJeremy L. Thompson 
598ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
599d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6027a982d89SJeremy L. Thompson 
6037a982d89SJeremy L. Thompson   @ref Backend
6047a982d89SJeremy L. Thompson **/
605d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
606f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
607e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6087a982d89SJeremy L. Thompson }
6097a982d89SJeremy L. Thompson 
6107a982d89SJeremy L. Thompson /**
611ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
6127a982d89SJeremy L. Thompson 
613ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
614ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
6157a982d89SJeremy L. Thompson 
6167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6177a982d89SJeremy L. Thompson 
6187a982d89SJeremy L. Thompson   @ref Backend
6197a982d89SJeremy L. Thompson **/
6207a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
6211203703bSJeremy L Thompson   bool is_composite;
6221203703bSJeremy L Thompson 
6231203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6246e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
6257a982d89SJeremy L. Thompson   *qf = op->qf;
626e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6277a982d89SJeremy L. Thompson }
6287a982d89SJeremy L. Thompson 
6297a982d89SJeremy L. Thompson /**
630ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
631c04a41a7SJeremy L Thompson 
632ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
633d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
634c04a41a7SJeremy L Thompson 
635c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
636c04a41a7SJeremy L Thompson 
637c04a41a7SJeremy L Thompson   @ref Backend
638c04a41a7SJeremy L Thompson **/
639d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
640f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
641e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
642c04a41a7SJeremy L Thompson }
643c04a41a7SJeremy L Thompson 
644c04a41a7SJeremy L Thompson /**
645ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
6467a982d89SJeremy L. Thompson 
647ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
6487a982d89SJeremy L. Thompson   @param[out] data Variable to store data
6497a982d89SJeremy L. Thompson 
6507a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6517a982d89SJeremy L. Thompson 
6527a982d89SJeremy L. Thompson   @ref Backend
6537a982d89SJeremy L. Thompson **/
654777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
655777ff853SJeremy L Thompson   *(void **)data = op->data;
656e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6577a982d89SJeremy L. Thompson }
6587a982d89SJeremy L. Thompson 
6597a982d89SJeremy L. Thompson /**
660ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
6617a982d89SJeremy L. Thompson 
662ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
663ea61e9acSJeremy L Thompson   @param[in]     data Data to set
6647a982d89SJeremy L. Thompson 
6657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6667a982d89SJeremy L. Thompson 
6677a982d89SJeremy L. Thompson   @ref Backend
6687a982d89SJeremy L. Thompson **/
669777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
670777ff853SJeremy L Thompson   op->data = data;
671e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6727a982d89SJeremy L. Thompson }
6737a982d89SJeremy L. Thompson 
6747a982d89SJeremy L. Thompson /**
675ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
67634359f16Sjeremylt 
677ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
67834359f16Sjeremylt 
67934359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
68034359f16Sjeremylt 
68134359f16Sjeremylt   @ref Backend
68234359f16Sjeremylt **/
6839560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
68434359f16Sjeremylt   op->ref_count++;
68534359f16Sjeremylt   return CEED_ERROR_SUCCESS;
68634359f16Sjeremylt }
68734359f16Sjeremylt 
68834359f16Sjeremylt /**
689ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
6907a982d89SJeremy L. Thompson 
691ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
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 **/
6977a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
698f04ea552SJeremy L Thompson   op->is_backend_setup = true;
699e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7007a982d89SJeremy L. Thompson }
7017a982d89SJeremy L. Thompson 
7027a982d89SJeremy L. Thompson /// @}
7037a982d89SJeremy L. Thompson 
7047a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7057a982d89SJeremy L. Thompson /// CeedOperator Public API
7067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7077a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
708dfdf5a53Sjeremylt /// @{
709d7b241e6Sjeremylt 
710d7b241e6Sjeremylt /**
711ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
7124385fb7fSSebastian Grimberg 
713ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
714d7b241e6Sjeremylt 
715ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
716ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
717ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
718ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
719ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
720b11c1e72Sjeremylt 
721b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
722dfdf5a53Sjeremylt 
7237a982d89SJeremy L. Thompson   @ref User
724d7b241e6Sjeremylt  */
7252b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
7265fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
7275fe0d4faSjeremylt     Ceed delegate;
7286574a04fSJeremy L Thompson 
7292b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7301ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate");
7312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
732e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7335fe0d4faSjeremylt   }
7345fe0d4faSjeremylt 
735ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
736db002c03SJeremy L Thompson 
7372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
738db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
739d1d35e2fSjeremylt   (*op)->ref_count   = 1;
7402b104005SJeremy L Thompson   (*op)->input_size  = -1;
7412b104005SJeremy L Thompson   (*op)->output_size = -1;
742db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
743db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
744db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
7452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
7462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
7472b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
748e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
749d7b241e6Sjeremylt }
750d7b241e6Sjeremylt 
751d7b241e6Sjeremylt /**
752ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
75348acf710SJeremy L Thompson 
754ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
755ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
75648acf710SJeremy L Thompson 
757ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
758ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
759ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
760ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
76148acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
76248acf710SJeremy L Thompson 
76348acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
76448acf710SJeremy L Thompson 
76548acf710SJeremy L Thompson   @ref User
76648acf710SJeremy L Thompson  */
76748acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
76848acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
76948acf710SJeremy L Thompson     Ceed delegate;
77048acf710SJeremy L Thompson 
77148acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7721ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints");
77348acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
77448acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
77548acf710SJeremy L Thompson   }
77648acf710SJeremy L Thompson 
777ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
77848acf710SJeremy L Thompson 
77948acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
78048acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
78148acf710SJeremy L Thompson   (*op)->ref_count    = 1;
78248acf710SJeremy L Thompson   (*op)->is_at_points = true;
78348acf710SJeremy L Thompson   (*op)->input_size   = -1;
78448acf710SJeremy L Thompson   (*op)->output_size  = -1;
78548acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
78648acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
78748acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
78848acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
78948acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
79048acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
79148acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79248acf710SJeremy L Thompson }
79348acf710SJeremy L Thompson 
79448acf710SJeremy L Thompson /**
795ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
79652d6035fSJeremy L Thompson 
797ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
798ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
79952d6035fSJeremy L Thompson 
80052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80152d6035fSJeremy L Thompson 
8027a982d89SJeremy L. Thompson   @ref User
80352d6035fSJeremy L Thompson  */
80452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
80552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
80652d6035fSJeremy L Thompson     Ceed delegate;
80752d6035fSJeremy L Thompson 
8081c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
809250756a7Sjeremylt     if (delegate) {
8102b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
811e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
81252d6035fSJeremy L Thompson     }
813250756a7Sjeremylt   }
81452d6035fSJeremy L Thompson 
8152b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
816db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
817996d9ab5SJed Brown   (*op)->ref_count    = 1;
818f04ea552SJeremy L Thompson   (*op)->is_composite = true;
8192b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
8202b104005SJeremy L Thompson   (*op)->input_size  = -1;
8212b104005SJeremy L Thompson   (*op)->output_size = -1;
822250756a7Sjeremylt 
823db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
824e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82552d6035fSJeremy L Thompson }
82652d6035fSJeremy L Thompson 
82752d6035fSJeremy L Thompson /**
828ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
8294385fb7fSSebastian Grimberg 
830ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
831512bb800SJeremy L Thompson 
832ca94c3ddSJeremy 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`.
833ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
8349560d06aSjeremylt 
835ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
836ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
8379560d06aSjeremylt 
8389560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8399560d06aSjeremylt 
8409560d06aSjeremylt   @ref User
8419560d06aSjeremylt **/
8429560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
8432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
8442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
8459560d06aSjeremylt   *op_copy = op;
8469560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8479560d06aSjeremylt }
8489560d06aSjeremylt 
8499560d06aSjeremylt /**
850ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
851d7b241e6Sjeremylt 
852ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
853bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
854ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
855d7b241e6Sjeremylt 
856ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
857ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
858d7b241e6Sjeremylt 
859528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
860bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
861528a22edSJeremy L Thompson 
862ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
863ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
864bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
865bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
866bafebce1SSebastian 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`
867b11c1e72Sjeremylt 
868b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
869dfdf5a53Sjeremylt 
8707a982d89SJeremy L. Thompson   @ref User
871b11c1e72Sjeremylt **/
872bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
8731203703bSJeremy L Thompson   bool               is_input = true, is_at_points, is_composite, is_immutable;
8741203703bSJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields;
8751203703bSJeremy L Thompson   Ceed               ceed;
8761203703bSJeremy L Thompson   CeedQFunction      qf;
8771203703bSJeremy L Thompson   CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields;
8781c66c397SJeremy L Thompson   CeedOperatorField *op_field;
8791c66c397SJeremy L Thompson 
8801203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
8811203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
8821203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
8831203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
8841203703bSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
8851203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8861203703bSJeremy L Thompson   CeedCheck(rstr, ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
8871203703bSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
8881203703bSJeremy L Thompson   CeedCheck(vec, ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
88952d6035fSJeremy L Thompson 
890bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
8911203703bSJeremy L Thompson   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, ceed, CEED_ERROR_DIMENSION,
892ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
8932c7e7413SJeremy L Thompson   {
8942c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
8952c7e7413SJeremy L Thompson 
896bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
89748acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
8981203703bSJeremy L Thompson       CeedCheck(is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields");
8991203703bSJeremy L Thompson       CeedCheck(basis == CEED_BASIS_NONE, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
90048acf710SJeremy L Thompson       if (!op->first_points_rstr) {
901bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
90248acf710SJeremy L Thompson       } else {
90348acf710SJeremy L Thompson         bool are_compatible;
90448acf710SJeremy L Thompson 
905bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
9061203703bSJeremy L Thompson         CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE,
907ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
90848acf710SJeremy L Thompson       }
90948acf710SJeremy L Thompson     }
9102c7e7413SJeremy L Thompson   }
911d7b241e6Sjeremylt 
912bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
913bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
9141203703bSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, ceed, CEED_ERROR_DIMENSION,
915ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
916528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
917bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
9181203703bSJeremy L Thompson 
9191203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
9201203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
9211203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9226f8994e9SJeremy L Thompson     const char *qf_field_name;
9231203703bSJeremy L Thompson 
9241203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_input_fields[i], &qf_field_name));
9251203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9261203703bSJeremy L Thompson       qf_field = qf_input_fields[i];
927d1d35e2fSjeremylt       op_field = &op->input_fields[i];
928d7b241e6Sjeremylt       goto found;
929d7b241e6Sjeremylt     }
930d7b241e6Sjeremylt   }
9312b104005SJeremy L Thompson   is_input = false;
9321203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9336f8994e9SJeremy L Thompson     const char *qf_field_name;
9341203703bSJeremy L Thompson 
9351203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_output_fields[i], &qf_field_name));
9361203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9371203703bSJeremy L Thompson       qf_field = qf_output_fields[i];
938d1d35e2fSjeremylt       op_field = &op->output_fields[i];
939d7b241e6Sjeremylt       goto found;
940d7b241e6Sjeremylt     }
941d7b241e6Sjeremylt   }
942c042f62fSJeremy L Thompson   // LCOV_EXCL_START
9431203703bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
944c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
945d7b241e6Sjeremylt found:
9461203703bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(ceed, qf_field, rstr, basis));
9472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
948e15f9bd0SJeremy L Thompson 
949bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
9502b104005SJeremy L Thompson     CeedSize l_size;
9511c66c397SJeremy L Thompson 
952bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
9532b104005SJeremy L Thompson     if (is_input) {
9542b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
9551203703bSJeremy L Thompson       CeedCheck(l_size == op->input_size, ceed, CEED_ERROR_INCOMPATIBLE,
956249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
9572b104005SJeremy L Thompson     } else {
9582b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
9591203703bSJeremy L Thompson       CeedCheck(l_size == op->output_size, ceed, CEED_ERROR_INCOMPATIBLE,
960249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
9612b104005SJeremy L Thompson     }
9622b730f8bSJeremy L Thompson   }
9632b104005SJeremy L Thompson 
964bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
965bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
966bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
967d1d35e2fSjeremylt     op->num_elem        = num_elem;
968d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
969e15f9bd0SJeremy L Thompson   }
970bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
9712a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
972d1d35e2fSjeremylt   op->num_fields += 1;
9732b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
974e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
975d7b241e6Sjeremylt }
976d7b241e6Sjeremylt 
977d7b241e6Sjeremylt /**
978ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
97943bbe138SJeremy L Thompson 
980ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
981f04ea552SJeremy L Thompson 
982ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
983f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
984ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
985f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
986ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
98743bbe138SJeremy L Thompson 
98843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
98943bbe138SJeremy L Thompson 
990e9b533fbSJeremy L Thompson   @ref Advanced
99143bbe138SJeremy L Thompson **/
9922b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
99343bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
9941203703bSJeremy L Thompson   bool          is_composite;
9951203703bSJeremy L Thompson   CeedQFunction qf;
9961203703bSJeremy L Thompson 
9971203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9986e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
9992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
100043bbe138SJeremy L Thompson 
10011203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10021203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL));
100343bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
100443bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
100543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
100643bbe138SJeremy L Thompson }
100743bbe138SJeremy L Thompson 
100843bbe138SJeremy L Thompson /**
1009ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
101048acf710SJeremy L Thompson 
1011ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
101248acf710SJeremy L Thompson 
1013ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
1014ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
1015ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
101648acf710SJeremy L Thompson 
101748acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
101848acf710SJeremy L Thompson 
101948acf710SJeremy L Thompson   @ref Advanced
102048acf710SJeremy L Thompson **/
102148acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
10221203703bSJeremy L Thompson   bool is_at_points, is_immutable;
10231203703bSJeremy L Thompson   Ceed ceed;
10242a3ff1c9SZach Atkins 
10251203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
10262a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10271203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
10281203703bSJeremy L Thompson   CeedCheck(is_at_points, ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
10291203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
103048acf710SJeremy L Thompson 
103148acf710SJeremy L Thompson   if (!op->first_points_rstr) {
103248acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
103348acf710SJeremy L Thompson   } else {
103448acf710SJeremy L Thompson     bool are_compatible;
103548acf710SJeremy L Thompson 
103648acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
10371203703bSJeremy L Thompson     CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE,
1038ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
103948acf710SJeremy L Thompson   }
104048acf710SJeremy L Thompson 
104148acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
104248acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
104348acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
104448acf710SJeremy L Thompson }
104548acf710SJeremy L Thompson 
104648acf710SJeremy L Thompson /**
1047b594f9faSZach Atkins   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
1048b594f9faSZach Atkins 
1049b594f9faSZach Atkins   @param[in]  op           `CeedOperator`
1050b594f9faSZach Atkins   @param[out] is_at_points Variable to store at points status
1051b594f9faSZach Atkins 
1052b594f9faSZach Atkins   @return An error code: 0 - success, otherwise - failure
1053b594f9faSZach Atkins 
1054b594f9faSZach Atkins   @ref User
1055b594f9faSZach Atkins **/
1056b594f9faSZach Atkins int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
1057b594f9faSZach Atkins   *is_at_points = op->is_at_points;
1058b594f9faSZach Atkins   return CEED_ERROR_SUCCESS;
1059b594f9faSZach Atkins }
1060b594f9faSZach Atkins 
1061b594f9faSZach Atkins /**
1062ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
106348acf710SJeremy L Thompson 
1064ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
106548acf710SJeremy L Thompson 
1066ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
1067ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
1068ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
106948acf710SJeremy L Thompson 
107048acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
107148acf710SJeremy L Thompson 
107248acf710SJeremy L Thompson   @ref Advanced
107348acf710SJeremy L Thompson **/
107448acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
10752a3ff1c9SZach Atkins   bool is_at_points;
10762a3ff1c9SZach Atkins 
10772a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10786e536b99SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
107948acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
108048acf710SJeremy L Thompson 
108148acf710SJeremy L Thompson   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
108248acf710SJeremy L Thompson   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
108348acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
108448acf710SJeremy L Thompson }
108548acf710SJeremy L Thompson 
108648acf710SJeremy L Thompson /**
1087be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
1088be9c6463SJeremy L Thompson 
1089be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
1090de5900adSJames Wright 
1091ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1092de5900adSJames Wright 
1093ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
1094ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
1095ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
1096de5900adSJames Wright 
1097de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
1098de5900adSJames Wright 
1099de5900adSJames Wright   @ref Advanced
1100de5900adSJames Wright **/
1101de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
11026f8994e9SJeremy L Thompson   const char        *name;
1103de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
1104de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
1105de5900adSJames Wright 
1106be9c6463SJeremy L Thompson   *op_field = NULL;
11071c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1108de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1109de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
1110de5900adSJames Wright     if (!strcmp(name, field_name)) {
1111de5900adSJames Wright       *op_field = input_fields[i];
1112de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1113de5900adSJames Wright     }
1114de5900adSJames Wright   }
1115de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1116de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
1117de5900adSJames Wright     if (!strcmp(name, field_name)) {
1118de5900adSJames Wright       *op_field = output_fields[i];
1119de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1120de5900adSJames Wright     }
1121de5900adSJames Wright   }
1122be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1123de5900adSJames Wright }
1124de5900adSJames Wright 
1125de5900adSJames Wright /**
1126ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
112728567f8fSJeremy L Thompson 
1128ca94c3ddSJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
112928567f8fSJeremy L Thompson   @param[out] field_name Variable to store the field name
113028567f8fSJeremy L Thompson 
113128567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
113228567f8fSJeremy L Thompson 
1133e9b533fbSJeremy L Thompson   @ref Advanced
113428567f8fSJeremy L Thompson **/
11356f8994e9SJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, const char **field_name) {
11366f8994e9SJeremy L Thompson   *field_name = op_field->field_name;
113728567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
113828567f8fSJeremy L Thompson }
113928567f8fSJeremy L Thompson 
114028567f8fSJeremy L Thompson /**
1141ca94c3ddSJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field
114243bbe138SJeremy L Thompson 
1143ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1144ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
114543bbe138SJeremy L Thompson 
114643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
114743bbe138SJeremy L Thompson 
1148e9b533fbSJeremy L Thompson   @ref Advanced
114943bbe138SJeremy L Thompson **/
11502b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1151437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
115243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
115343bbe138SJeremy L Thompson }
115443bbe138SJeremy L Thompson 
115543bbe138SJeremy L Thompson /**
1156ca94c3ddSJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field
115743bbe138SJeremy L Thompson 
1158ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1159ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
116043bbe138SJeremy L Thompson 
116143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
116243bbe138SJeremy L Thompson 
1163e9b533fbSJeremy L Thompson   @ref Advanced
116443bbe138SJeremy L Thompson **/
116543bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
116643bbe138SJeremy L Thompson   *basis = op_field->basis;
116743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
116843bbe138SJeremy L Thompson }
116943bbe138SJeremy L Thompson 
117043bbe138SJeremy L Thompson /**
1171ca94c3ddSJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field
117243bbe138SJeremy L Thompson 
1173ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1174ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
117543bbe138SJeremy L Thompson 
117643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
117743bbe138SJeremy L Thompson 
1178e9b533fbSJeremy L Thompson   @ref Advanced
117943bbe138SJeremy L Thompson **/
118043bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
118143bbe138SJeremy L Thompson   *vec = op_field->vec;
118243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
118343bbe138SJeremy L Thompson }
118443bbe138SJeremy L Thompson 
118543bbe138SJeremy L Thompson /**
1186ab747706SJeremy L Thompson   @brief Get the data of a `CeedOperator` Field.
1187ab747706SJeremy L Thompson 
1188ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
1189ab747706SJeremy L Thompson 
1190ab747706SJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
1191ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
1192ab747706SJeremy L Thompson   @param[out] rstr       Variable to store `CeedElemRestriction`
1193ab747706SJeremy L Thompson   @param[out] basis      Variable to store `CeedBasis`
1194ab747706SJeremy L Thompson   @param[out] vec        Variable to store `CeedVector`
1195ab747706SJeremy L Thompson 
1196ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1197ab747706SJeremy L Thompson 
1198ab747706SJeremy L Thompson   @ref Advanced
1199ab747706SJeremy L Thompson **/
12006f8994e9SJeremy L Thompson int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name, CeedElemRestriction *rstr, CeedBasis *basis, CeedVector *vec) {
1201ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedOperatorFieldGetName(op_field, field_name));
1202ab747706SJeremy L Thompson   if (rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_field, rstr));
1203ab747706SJeremy L Thompson   if (basis) CeedCall(CeedOperatorFieldGetBasis(op_field, basis));
1204ab747706SJeremy L Thompson   if (vec) CeedCall(CeedOperatorFieldGetVector(op_field, vec));
1205ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1206ab747706SJeremy L Thompson }
1207ab747706SJeremy L Thompson 
1208ab747706SJeremy L Thompson /**
1209ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1210288c0443SJeremy L Thompson 
1211ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1212ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
121352d6035fSJeremy L Thompson 
121452d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
121552d6035fSJeremy L Thompson 
12167a982d89SJeremy L. Thompson   @ref User
121752d6035fSJeremy L Thompson  */
12182b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
12191203703bSJeremy L Thompson   bool is_immutable;
12201203703bSJeremy L Thompson   Ceed ceed;
12211203703bSJeremy L Thompson 
12221203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(composite_op, &ceed));
12231203703bSJeremy L Thompson   CeedCheck(composite_op->is_composite, ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
12241203703bSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
12251203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable));
12261203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
12272b730f8bSJeremy L Thompson 
12282b730f8bSJeremy L Thompson   {
12292b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
12301c66c397SJeremy L Thompson 
12312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
12322b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
12332b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
12342b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
12351203703bSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), ceed,
12361203703bSJeremy L Thompson               CEED_ERROR_MAJOR,
1237249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1238249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1239249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12402b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
12412b730f8bSJeremy L Thompson   }
12422b730f8bSJeremy L Thompson 
1243d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
12442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1245d1d35e2fSjeremylt   composite_op->num_suboperators++;
1246e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
124752d6035fSJeremy L Thompson }
124852d6035fSJeremy L Thompson 
124952d6035fSJeremy L Thompson /**
1250ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
125175f0d5a4SJeremy L Thompson 
1252ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1253ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
125475f0d5a4SJeremy L Thompson 
125575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
125675f0d5a4SJeremy L Thompson 
125775f0d5a4SJeremy L Thompson   @ref Backend
125875f0d5a4SJeremy L Thompson **/
1259c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
12601203703bSJeremy L Thompson   bool is_composite;
12611203703bSJeremy L Thompson 
12621203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12636e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
126475f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
126575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
126675f0d5a4SJeremy L Thompson }
126775f0d5a4SJeremy L Thompson 
126875f0d5a4SJeremy L Thompson /**
1269ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
127075f0d5a4SJeremy L Thompson 
1271ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1272ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
127375f0d5a4SJeremy L Thompson 
127475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
127575f0d5a4SJeremy L Thompson 
127675f0d5a4SJeremy L Thompson   @ref Backend
127775f0d5a4SJeremy L Thompson **/
1278c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
12791203703bSJeremy L Thompson   bool is_composite;
12801203703bSJeremy L Thompson 
12811203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12826e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
128375f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
128475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
128575f0d5a4SJeremy L Thompson }
128675f0d5a4SJeremy L Thompson 
128775f0d5a4SJeremy L Thompson /**
1288*8a297abdSJames Wright   @brief Get a sub `CeedOperator` of a composite `CeedOperator` from its name.
1289*8a297abdSJames Wright 
1290*8a297abdSJames Wright   `sub_op` is set to `NULL` if the sub operator is not found.
1291*8a297abdSJames Wright 
1292*8a297abdSJames Wright   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1293*8a297abdSJames Wright 
1294*8a297abdSJames Wright   @param[in]  op      Composite `CeedOperator`
1295*8a297abdSJames Wright   @param[in]  op_name Name of desired sub `CeedOperator`
1296*8a297abdSJames Wright   @param[out] sub_op  Sub `CeedOperator` corresponding to the name
1297*8a297abdSJames Wright 
1298*8a297abdSJames Wright   @return An error code: 0 - success, otherwise - failure
1299*8a297abdSJames Wright 
1300*8a297abdSJames Wright   @ref Advanced
1301*8a297abdSJames Wright **/
1302*8a297abdSJames Wright int CeedCompositeOperatorGetSubByName(CeedOperator op, const char *op_name, CeedOperator *sub_op) {
1303*8a297abdSJames Wright   bool          is_composite;
1304*8a297abdSJames Wright   CeedInt       num_sub_ops;
1305*8a297abdSJames Wright   CeedOperator *sub_ops;
1306*8a297abdSJames Wright 
1307*8a297abdSJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1308*8a297abdSJames Wright   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
1309*8a297abdSJames Wright   *sub_op = NULL;
1310*8a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_ops));
1311*8a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_ops));
1312*8a297abdSJames Wright   for (CeedInt i = 0; i < num_sub_ops; i++) {
1313*8a297abdSJames Wright     if (sub_ops[i]->name && !strcmp(op_name, sub_ops[i]->name)) {
1314*8a297abdSJames Wright       *sub_op = sub_ops[i];
1315*8a297abdSJames Wright       return CEED_ERROR_SUCCESS;
1316*8a297abdSJames Wright     }
1317*8a297abdSJames Wright   }
1318*8a297abdSJames Wright   return CEED_ERROR_SUCCESS;
1319*8a297abdSJames Wright }
1320*8a297abdSJames Wright 
1321*8a297abdSJames Wright /**
1322ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
13234db537f9SJeremy L Thompson 
1324ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
13254db537f9SJeremy L Thompson 
13264db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13274db537f9SJeremy L Thompson 
13284db537f9SJeremy L Thompson   @ref User
13294db537f9SJeremy L Thompson **/
13304db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
13311203703bSJeremy L Thompson   bool          is_at_points, is_composite;
13324db537f9SJeremy L Thompson   Ceed          ceed;
13331203703bSJeremy L Thompson   CeedQFunction qf = NULL;
13344db537f9SJeremy L Thompson 
13352b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
13364db537f9SJeremy L Thompson 
13371203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
13381203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
13391203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13401203703bSJeremy L Thompson   if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf));
13411203703bSJeremy L Thompson   if (is_composite) {
13421203703bSJeremy L Thompson     CeedInt num_suboperators;
13431203703bSJeremy L Thompson 
13441203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13451203703bSJeremy L Thompson     if (!num_suboperators) {
134643622462SJeremy L Thompson       // Empty operator setup
134743622462SJeremy L Thompson       op->input_size  = 0;
134843622462SJeremy L Thompson       op->output_size = 0;
134943622462SJeremy L Thompson     } else {
13501203703bSJeremy L Thompson       CeedOperator *sub_operators;
13511203703bSJeremy L Thompson 
13521203703bSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13531203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_suboperators; i++) {
13541203703bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(sub_operators[i]));
13554db537f9SJeremy L Thompson       }
13562b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
13572b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
13582b104005SJeremy L Thompson       CeedSize input_size, output_size;
13592b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
136043622462SJeremy L Thompson     }
13614db537f9SJeremy L Thompson   } else {
13621203703bSJeremy L Thompson     CeedInt num_input_fields, num_output_fields;
13631203703bSJeremy L Thompson 
13646574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
13651203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL));
13661203703bSJeremy L Thompson     CeedCheck(op->num_fields == num_input_fields + num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
13676574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
13682a3ff1c9SZach Atkins     CeedCheck(op->num_qpts > 0 || is_at_points, ceed, CEED_ERROR_INCOMPLETE,
1369ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
13702b730f8bSJeremy L Thompson   }
13714db537f9SJeremy L Thompson 
13724db537f9SJeremy L Thompson   // Flag as immutable and ready
13734db537f9SJeremy L Thompson   op->is_interface_setup = true;
13741203703bSJeremy L Thompson   if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf));
13751203703bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf));
13761203703bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT));
13774db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13784db537f9SJeremy L Thompson }
13794db537f9SJeremy L Thompson 
13804db537f9SJeremy L Thompson /**
1381ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
13824385fb7fSSebastian Grimberg 
1383ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1384c9366a6bSJeremy L Thompson 
1385ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1386ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1387ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1388c9366a6bSJeremy L Thompson 
1389c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1390c9366a6bSJeremy L Thompson 
1391c9366a6bSJeremy L Thompson   @ref User
1392c9366a6bSJeremy L Thompson **/
13932b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1394c9366a6bSJeremy L Thompson   bool is_composite;
1395c9366a6bSJeremy L Thompson 
13962b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
13972b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1398c9366a6bSJeremy L Thompson 
13992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14002b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
14011203703bSJeremy L Thompson     CeedInt       num_suboperators;
14021203703bSJeremy L Thompson     CeedOperator *sub_operators;
14031203703bSJeremy L Thompson 
14041203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14051203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14061203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
1407c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
14081c66c397SJeremy L Thompson 
14091203703bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(sub_operators[i], &sub_input_size, &sub_output_size));
14102b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
14112b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
14122b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
14136e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
14146e536b99SJeremy L Thompson                 CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR,
1415249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1416249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1417249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
14182b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1419c9366a6bSJeremy L Thompson     }
14202b730f8bSJeremy L Thompson   }
1421c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1422c9366a6bSJeremy L Thompson }
1423c9366a6bSJeremy L Thompson 
1424c9366a6bSJeremy L Thompson /**
1425ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
14264385fb7fSSebastian Grimberg 
1427ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1428ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
14298b919e6bSJeremy L Thompson 
1430ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1431beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
14328b919e6bSJeremy L Thompson 
14338b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14348b919e6bSJeremy L Thompson 
14358b919e6bSJeremy L Thompson   @ref Advanced
14368b919e6bSJeremy L Thompson **/
14372b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
14388b919e6bSJeremy L Thompson   bool is_composite;
14398b919e6bSJeremy L Thompson 
14402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14418b919e6bSJeremy L Thompson   if (is_composite) {
14428b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
14432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
14448b919e6bSJeremy L Thompson     }
14458b919e6bSJeremy L Thompson   } else {
14467d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14477d5185d7SSebastian Grimberg 
14487d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
14497d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetReuse(data, reuse_assembly_data));
1450beecbf24SJeremy L Thompson   }
1451beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1452beecbf24SJeremy L Thompson }
1453beecbf24SJeremy L Thompson 
1454beecbf24SJeremy L Thompson /**
1455ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1456beecbf24SJeremy L Thompson 
1457ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
14586e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1459beecbf24SJeremy L Thompson 
1460beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1461beecbf24SJeremy L Thompson 
1462beecbf24SJeremy L Thompson   @ref Advanced
1463beecbf24SJeremy L Thompson **/
14642b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1465beecbf24SJeremy L Thompson   bool is_composite;
1466beecbf24SJeremy L Thompson 
14672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1468beecbf24SJeremy L Thompson   if (is_composite) {
14691203703bSJeremy L Thompson     CeedInt       num_suboperators;
14701203703bSJeremy L Thompson     CeedOperator *sub_operators;
14711203703bSJeremy L Thompson 
14721203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14731203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14741203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
14751203703bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(sub_operators[i], needs_data_update));
1476beecbf24SJeremy L Thompson     }
1477beecbf24SJeremy L Thompson   } else {
14787d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14797d5185d7SSebastian Grimberg 
14807d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
14817d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, needs_data_update));
14828b919e6bSJeremy L Thompson   }
14838b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14848b919e6bSJeremy L Thompson }
14858b919e6bSJeremy L Thompson 
14868b919e6bSJeremy L Thompson /**
1487ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1488ea6b5821SJeremy L Thompson 
1489ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1490ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1491ea6b5821SJeremy L Thompson 
1492ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1493ea6b5821SJeremy L Thompson 
1494ea6b5821SJeremy L Thompson   @ref User
1495ea6b5821SJeremy L Thompson **/
1496ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1497ea6b5821SJeremy L Thompson   char  *name_copy;
1498ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1499ea6b5821SJeremy L Thompson 
15002b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1501ea6b5821SJeremy L Thompson   if (name_len > 0) {
15022b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1503ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1504ea6b5821SJeremy L Thompson     op->name = name_copy;
1505ea6b5821SJeremy L Thompson   }
1506ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1507ea6b5821SJeremy L Thompson }
1508ea6b5821SJeremy L Thompson 
1509ea6b5821SJeremy L Thompson /**
1510935f026aSJeremy L Thompson   @brief Core logic for viewing a `CeedOperator`
15117a982d89SJeremy L. Thompson 
1512935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1513ca94c3ddSJeremy L Thompson   @param[in] stream  Stream to write; typically `stdout` or a file
15148bf1b130SJames Wright   @param[in] is_full Whether to write full operator view or terse
15157a982d89SJeremy L. Thompson 
15167a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
15177a982d89SJeremy L. Thompson **/
15188bf1b130SJames Wright static int CeedOperatorView_Core(CeedOperator op, FILE *stream, bool is_full) {
15191203703bSJeremy L Thompson   bool has_name = op->name, is_composite;
15207a982d89SJeremy L. Thompson 
15211203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15221203703bSJeremy L Thompson   if (is_composite) {
15231203703bSJeremy L Thompson     CeedInt       num_suboperators;
15241203703bSJeremy L Thompson     CeedOperator *sub_operators;
15251203703bSJeremy L Thompson 
15261203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15271203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15282b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
15297a982d89SJeremy L. Thompson 
15301203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15311203703bSJeremy L Thompson       has_name = sub_operators[i]->name;
1532935f026aSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s%s\n", i, has_name ? " - " : "", has_name ? sub_operators[i]->name : "", is_full ? ":" : "");
1533935f026aSJeremy L Thompson       if (is_full) CeedCall(CeedOperatorSingleView(sub_operators[i], 1, stream));
15347a982d89SJeremy L. Thompson     }
15357a982d89SJeremy L. Thompson   } else {
15362b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1537935f026aSJeremy L Thompson     if (is_full) CeedCall(CeedOperatorSingleView(op, 0, stream));
15387a982d89SJeremy L. Thompson   }
1539e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15407a982d89SJeremy L. Thompson }
15413bd813ffSjeremylt 
15423bd813ffSjeremylt /**
1543935f026aSJeremy L Thompson   @brief View a `CeedOperator`
1544935f026aSJeremy L Thompson 
1545935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1546935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1547935f026aSJeremy L Thompson 
1548935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1549935f026aSJeremy L Thompson 
1550935f026aSJeremy L Thompson   @ref User
1551935f026aSJeremy L Thompson **/
1552935f026aSJeremy L Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1553935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, true));
1554935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1555935f026aSJeremy L Thompson }
1556935f026aSJeremy L Thompson 
1557935f026aSJeremy L Thompson /**
1558935f026aSJeremy L Thompson   @brief View a brief summary `CeedOperator`
1559935f026aSJeremy L Thompson 
1560935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1561935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1562935f026aSJeremy L Thompson 
1563935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1564935f026aSJeremy L Thompson 
1565935f026aSJeremy L Thompson   @ref User
1566935f026aSJeremy L Thompson **/
1567935f026aSJeremy L Thompson int CeedOperatorViewTerse(CeedOperator op, FILE *stream) {
1568935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, false));
1569935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1570935f026aSJeremy L Thompson }
1571935f026aSJeremy L Thompson 
1572935f026aSJeremy L Thompson /**
1573ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1574b7c9bbdaSJeremy L Thompson 
1575ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1576ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1577b7c9bbdaSJeremy L Thompson 
1578b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1579b7c9bbdaSJeremy L Thompson 
1580b7c9bbdaSJeremy L Thompson   @ref Advanced
1581b7c9bbdaSJeremy L Thompson **/
1582b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
15836e536b99SJeremy L Thompson   *ceed = CeedOperatorReturnCeed(op);
1584b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1585b7c9bbdaSJeremy L Thompson }
1586b7c9bbdaSJeremy L Thompson 
1587b7c9bbdaSJeremy L Thompson /**
15886e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
15896e536b99SJeremy L Thompson 
15906e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
15916e536b99SJeremy L Thompson 
15926e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
15936e536b99SJeremy L Thompson 
15946e536b99SJeremy L Thompson   @ref Advanced
15956e536b99SJeremy L Thompson **/
15966e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
15976e536b99SJeremy L Thompson 
15986e536b99SJeremy L Thompson /**
1599ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1600b7c9bbdaSJeremy L Thompson 
1601ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1602b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1603b7c9bbdaSJeremy L Thompson 
1604b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1605b7c9bbdaSJeremy L Thompson 
1606b7c9bbdaSJeremy L Thompson   @ref Advanced
1607b7c9bbdaSJeremy L Thompson **/
1608b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
16091203703bSJeremy L Thompson   bool is_composite;
16101203703bSJeremy L Thompson 
16111203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16126e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1613b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1614b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1615b7c9bbdaSJeremy L Thompson }
1616b7c9bbdaSJeremy L Thompson 
1617b7c9bbdaSJeremy L Thompson /**
1618ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1619b7c9bbdaSJeremy L Thompson 
1620ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1621b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1622b7c9bbdaSJeremy L Thompson 
1623b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1624b7c9bbdaSJeremy L Thompson 
1625b7c9bbdaSJeremy L Thompson   @ref Advanced
1626b7c9bbdaSJeremy L Thompson **/
1627b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
16281203703bSJeremy L Thompson   bool is_composite;
16291203703bSJeremy L Thompson 
16301203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16316e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1632b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1633b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1634b7c9bbdaSJeremy L Thompson }
1635b7c9bbdaSJeremy L Thompson 
1636b7c9bbdaSJeremy L Thompson /**
1637ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
16386e15d496SJeremy L Thompson 
1639ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1640ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
16416e15d496SJeremy L Thompson 
16426e15d496SJeremy L Thompson   @ref Backend
16436e15d496SJeremy L Thompson **/
16449d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
16456e15d496SJeremy L Thompson   bool is_composite;
16461c66c397SJeremy L Thompson 
16472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
16486e15d496SJeremy L Thompson 
16496e15d496SJeremy L Thompson   *flops = 0;
16502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16516e15d496SJeremy L Thompson   if (is_composite) {
16526e15d496SJeremy L Thompson     CeedInt num_suboperators;
16531c66c397SJeremy L Thompson 
1654c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
16556e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1656c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
16576e15d496SJeremy L Thompson 
16586e15d496SJeremy L Thompson     // FLOPs for each suboperator
16596e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
16609d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
16611c66c397SJeremy L Thompson 
16622b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
16636e15d496SJeremy L Thompson       *flops += suboperator_flops;
16646e15d496SJeremy L Thompson     }
16656e15d496SJeremy L Thompson   } else {
16661c66c397SJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0;
16671203703bSJeremy L Thompson     CeedQFunction       qf;
16681203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
16691203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
16701c66c397SJeremy L Thompson 
16711203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
16721203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
16731203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
16742b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
16754385fb7fSSebastian Grimberg 
16766e15d496SJeremy L Thompson     // Input FLOPs
16776e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
16781203703bSJeremy L Thompson       CeedVector vec;
16796e15d496SJeremy L Thompson 
16801203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
16811203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
16821203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
16831203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
16841203703bSJeremy L Thompson         CeedElemRestriction rstr;
16851203703bSJeremy L Thompson         CeedBasis           basis;
16861203703bSJeremy L Thompson 
16871203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
16881203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1689edb2538eSJeremy L Thompson         *flops += rstr_flops;
16901203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
16911203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
16921203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, &basis_flops));
16936e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
16946e15d496SJeremy L Thompson       }
16956e15d496SJeremy L Thompson     }
16966e15d496SJeremy L Thompson     // QF FLOPs
16971c66c397SJeremy L Thompson     {
16989d36ca50SJeremy L Thompson       CeedInt       num_qpts;
16999d36ca50SJeremy L Thompson       CeedSize      qf_flops;
17001203703bSJeremy L Thompson       CeedQFunction qf;
17011c66c397SJeremy L Thompson 
17022b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
17031203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
17041203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
17056e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
17066e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
17076e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
17081c66c397SJeremy L Thompson     }
17091c66c397SJeremy L Thompson 
17106e15d496SJeremy L Thompson     // Output FLOPs
17116e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
17121203703bSJeremy L Thompson       CeedVector vec;
17136e15d496SJeremy L Thompson 
17141203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
17151203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17161203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17171203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17181203703bSJeremy L Thompson         CeedElemRestriction rstr;
17191203703bSJeremy L Thompson         CeedBasis           basis;
17201203703bSJeremy L Thompson 
17211203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
17221203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1723edb2538eSJeremy L Thompson         *flops += rstr_flops;
17241203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
17251203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
17261203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, &basis_flops));
17276e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17286e15d496SJeremy L Thompson       }
17296e15d496SJeremy L Thompson     }
17306e15d496SJeremy L Thompson   }
17316e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17326e15d496SJeremy L Thompson }
17336e15d496SJeremy L Thompson 
17346e15d496SJeremy L Thompson /**
1735ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
17369fd66db6SSebastian Grimberg 
1737ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1738512bb800SJeremy L Thompson 
1739ca94c3ddSJeremy 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`.
1740ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
17410126412dSJeremy L Thompson 
1742ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1743ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
17440126412dSJeremy L Thompson 
17450126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17460126412dSJeremy L Thompson 
1747859c15bbSJames Wright   @ref Advanced
17480126412dSJeremy L Thompson **/
17490126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
17501203703bSJeremy L Thompson   bool                 is_composite;
17511203703bSJeremy L Thompson   CeedQFunction        qf;
17521203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
17531203703bSJeremy L Thompson 
17541203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17556e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
17561203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
17571203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
17581203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
17590126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
17600126412dSJeremy L Thompson }
17610126412dSJeremy L Thompson 
17620126412dSJeremy L Thompson /**
1763ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
17643668ca4bSJeremy L Thompson 
1765ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1766859c15bbSJames Wright 
1767ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
17683668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
17693668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
17703668ca4bSJeremy L Thompson 
17713668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17723668ca4bSJeremy L Thompson 
17733668ca4bSJeremy L Thompson   @ref User
17743668ca4bSJeremy L Thompson **/
177517b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
17761c66c397SJeremy L Thompson   bool is_composite, field_found = false;
17771c66c397SJeremy L Thompson 
17782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17792b730f8bSJeremy L Thompson 
17803668ca4bSJeremy L Thompson   if (is_composite) {
1781a98a090bSJeremy L Thompson     // Composite operator
1782a98a090bSJeremy L Thompson     // -- Check if composite label already created
17833668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
17843668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
17853668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
17863668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
17873668ca4bSJeremy L Thompson       }
17883668ca4bSJeremy L Thompson     }
17893668ca4bSJeremy L Thompson 
1790a98a090bSJeremy L Thompson     // -- Create composite label if needed
17913668ca4bSJeremy L Thompson     CeedInt               num_sub;
17923668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
17933668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
17943668ca4bSJeremy L Thompson 
17952b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1796c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1797c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
17982b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
17993668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
18003668ca4bSJeremy L Thompson 
18013668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
18023668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
18033668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
18041c66c397SJeremy L Thompson 
18052b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
18063668ca4bSJeremy L Thompson         if (new_field_label_i) {
1807a98a090bSJeremy L Thompson           field_found                    = true;
18083668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
18093668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
18103668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
18112b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
18127bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18132b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18146e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
18152b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
18167bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18177bfe0f0eSJeremy L Thompson           } else {
18187bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
18197bfe0f0eSJeremy L Thompson           }
18202b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
18217bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18222b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18236e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
18246e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
18256e536b99SJeremy L Thompson                              new_field_label_i->num_values);
18267bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18277bfe0f0eSJeremy L Thompson           } else {
18287bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
18297bfe0f0eSJeremy L Thompson           }
18303668ca4bSJeremy L Thompson         }
18313668ca4bSJeremy L Thompson       }
18323668ca4bSJeremy L Thompson     }
1833a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1834a98a090bSJeremy L Thompson     if (field_found) {
1835a98a090bSJeremy L Thompson       *field_label = new_field_label;
1836a98a090bSJeremy L Thompson     } else {
18373668ca4bSJeremy L Thompson       // LCOV_EXCL_START
18382b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
18392b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
18403668ca4bSJeremy L Thompson       *field_label = NULL;
18413668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
18425ac9af79SJeremy L Thompson     }
18435ac9af79SJeremy L Thompson   } else {
18441203703bSJeremy L Thompson     CeedQFunction        qf;
18451203703bSJeremy L Thompson     CeedQFunctionContext ctx;
18461203703bSJeremy L Thompson 
1847a98a090bSJeremy L Thompson     // Single, non-composite operator
18481203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
18491203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
18501203703bSJeremy L Thompson     if (ctx) {
18511203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1852a98a090bSJeremy L Thompson     } else {
1853a98a090bSJeremy L Thompson       *field_label = NULL;
1854a98a090bSJeremy L Thompson     }
18555ac9af79SJeremy L Thompson   }
18565ac9af79SJeremy L Thompson 
18575ac9af79SJeremy L Thompson   // Set label in operator
18585ac9af79SJeremy L Thompson   if (*field_label) {
18595ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
18605ac9af79SJeremy L Thompson 
18613668ca4bSJeremy L Thompson     // Move new composite label to operator
18623668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
18632b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
18643668ca4bSJeremy L Thompson       op->max_context_labels = 1;
18653668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
18662b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
18673668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
18683668ca4bSJeremy L Thompson     }
18695ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
18703668ca4bSJeremy L Thompson     op->num_context_labels++;
18713668ca4bSJeremy L Thompson   }
18723668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
18733668ca4bSJeremy L Thompson }
18743668ca4bSJeremy L Thompson 
18753668ca4bSJeremy L Thompson /**
1876ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
18774385fb7fSSebastian Grimberg 
1878ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1879d8dd9a91SJeremy L Thompson 
1880ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
18812788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1882ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1883d8dd9a91SJeremy L Thompson 
1884d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1885d8dd9a91SJeremy L Thompson 
1886d8dd9a91SJeremy L Thompson   @ref User
1887d8dd9a91SJeremy L Thompson **/
188817b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
18892b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1890d8dd9a91SJeremy L Thompson }
1891d8dd9a91SJeremy L Thompson 
1892d8dd9a91SJeremy L Thompson /**
1893ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
18944385fb7fSSebastian Grimberg 
1895ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
18962788fa27SJeremy L Thompson 
1897ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18982788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
18992788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
19002788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19012788fa27SJeremy L Thompson 
19022788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19032788fa27SJeremy L Thompson 
19042788fa27SJeremy L Thompson   @ref User
19052788fa27SJeremy L Thompson **/
190617b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
19072788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
19082788fa27SJeremy L Thompson }
19092788fa27SJeremy L Thompson 
19102788fa27SJeremy L Thompson /**
1911ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
19122788fa27SJeremy L Thompson 
1913ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19142788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
19152788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19162788fa27SJeremy L Thompson 
19172788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19182788fa27SJeremy L Thompson 
19192788fa27SJeremy L Thompson   @ref User
19202788fa27SJeremy L Thompson **/
192117b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
19222788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
19232788fa27SJeremy L Thompson }
19242788fa27SJeremy L Thompson 
19252788fa27SJeremy L Thompson /**
1926ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
19274385fb7fSSebastian Grimberg 
1928ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1929d8dd9a91SJeremy L Thompson 
1930ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1931ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1932ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1933d8dd9a91SJeremy L Thompson 
1934d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1935d8dd9a91SJeremy L Thompson 
1936d8dd9a91SJeremy L Thompson   @ref User
1937d8dd9a91SJeremy L Thompson **/
193823dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
19392b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1940d8dd9a91SJeremy L Thompson }
1941d8dd9a91SJeremy L Thompson 
1942d8dd9a91SJeremy L Thompson /**
1943ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
19444385fb7fSSebastian Grimberg 
1945ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19462788fa27SJeremy L Thompson 
1947ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19482788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1949ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
19502788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19512788fa27SJeremy L Thompson 
19522788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19532788fa27SJeremy L Thompson 
19542788fa27SJeremy L Thompson   @ref User
19552788fa27SJeremy L Thompson **/
195623dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
19572788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
19582788fa27SJeremy L Thompson }
19592788fa27SJeremy L Thompson 
19602788fa27SJeremy L Thompson /**
1961ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
19622788fa27SJeremy L Thompson 
1963ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19642788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
19652788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19662788fa27SJeremy L Thompson 
19672788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19682788fa27SJeremy L Thompson 
19692788fa27SJeremy L Thompson   @ref User
19702788fa27SJeremy L Thompson **/
197123dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
19722788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
19732788fa27SJeremy L Thompson }
19742788fa27SJeremy L Thompson 
19752788fa27SJeremy L Thompson /**
1976ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
19775b6ec284SJeremy L Thompson 
1978ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
19795b6ec284SJeremy L Thompson 
1980ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19815b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
19825b6ec284SJeremy L Thompson   @param[in]     values      Values to set
19835b6ec284SJeremy L Thompson 
19845b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19855b6ec284SJeremy L Thompson 
19865b6ec284SJeremy L Thompson   @ref User
19875b6ec284SJeremy L Thompson **/
19885b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
19895b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
19905b6ec284SJeremy L Thompson }
19915b6ec284SJeremy L Thompson 
19925b6ec284SJeremy L Thompson /**
1993ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
19945b6ec284SJeremy L Thompson 
1995ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19965b6ec284SJeremy L Thompson 
1997ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19985b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
1999ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
20005b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20015b6ec284SJeremy L Thompson 
20025b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20035b6ec284SJeremy L Thompson 
20045b6ec284SJeremy L Thompson   @ref User
20055b6ec284SJeremy L Thompson **/
20065b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
20075b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
20085b6ec284SJeremy L Thompson }
20095b6ec284SJeremy L Thompson 
20105b6ec284SJeremy L Thompson /**
2011ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
20125b6ec284SJeremy L Thompson 
2013ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20145b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
20155b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20165b6ec284SJeremy L Thompson 
20175b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20185b6ec284SJeremy L Thompson 
20195b6ec284SJeremy L Thompson   @ref User
20205b6ec284SJeremy L Thompson **/
20215b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
20225b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20235b6ec284SJeremy L Thompson }
20245b6ec284SJeremy L Thompson 
20255b6ec284SJeremy L Thompson /**
2026ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
2027d7b241e6Sjeremylt 
2028ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2029ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2030d7b241e6Sjeremylt 
2031ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2032f04ea552SJeremy L Thompson 
2033ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2034ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2035ca94c3ddSJeremy 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
2036ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2037b11c1e72Sjeremylt 
2038b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2039dfdf5a53Sjeremylt 
20407a982d89SJeremy L. Thompson   @ref User
2041b11c1e72Sjeremylt **/
20422b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
20431203703bSJeremy L Thompson   bool is_composite;
20441203703bSJeremy L Thompson 
20452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2046d7b241e6Sjeremylt 
20471203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
20481203703bSJeremy L Thompson   if (is_composite) {
2049250756a7Sjeremylt     // Composite Operator
2050250756a7Sjeremylt     if (op->ApplyComposite) {
20512b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
2052250756a7Sjeremylt     } else {
2053d1d35e2fSjeremylt       CeedInt       num_suboperators;
2054d1d35e2fSjeremylt       CeedOperator *sub_operators;
20551c66c397SJeremy L Thompson 
20561c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2057c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2058250756a7Sjeremylt 
2059250756a7Sjeremylt       // Zero all output vectors
20603ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2061d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
20621203703bSJeremy L Thompson         CeedInt            num_output_fields;
20631203703bSJeremy L Thompson         CeedOperatorField *output_fields;
20641c66c397SJeremy L Thompson 
20651203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
20661203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
20671203703bSJeremy L Thompson           CeedVector vec;
20681203703bSJeremy L Thompson 
20691203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2070250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
20712b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2072250756a7Sjeremylt           }
2073250756a7Sjeremylt         }
2074250756a7Sjeremylt       }
2075250756a7Sjeremylt       // Apply
2076f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2077f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2078cae8b89aSjeremylt       }
2079cae8b89aSjeremylt     }
20803ca2b39bSJeremy L Thompson   } else {
20813ca2b39bSJeremy L Thompson     // Standard Operator
20823ca2b39bSJeremy L Thompson     if (op->Apply) {
20833ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
20843ca2b39bSJeremy L Thompson     } else {
20851203703bSJeremy L Thompson       CeedInt            num_output_fields;
20861203703bSJeremy L Thompson       CeedOperatorField *output_fields;
20871203703bSJeremy L Thompson 
20881203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
20893ca2b39bSJeremy L Thompson       // Zero all output vectors
20901203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
20911203703bSJeremy L Thompson         CeedVector vec;
20921c66c397SJeremy L Thompson 
20931203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
20943ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
20953ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
20963ca2b39bSJeremy L Thompson       }
20973ca2b39bSJeremy L Thompson       // Apply
20981203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
20993ca2b39bSJeremy L Thompson     }
2100250756a7Sjeremylt   }
2101e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2102cae8b89aSjeremylt }
2103cae8b89aSjeremylt 
2104cae8b89aSjeremylt /**
2105ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2106cae8b89aSjeremylt 
2107ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2108ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2109cae8b89aSjeremylt 
2110ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2111ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2112ca94c3ddSJeremy 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
2113ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2114cae8b89aSjeremylt 
2115cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2116cae8b89aSjeremylt 
21177a982d89SJeremy L. Thompson   @ref User
2118cae8b89aSjeremylt **/
21192b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
21201203703bSJeremy L Thompson   bool is_composite;
21211203703bSJeremy L Thompson 
21222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2123cae8b89aSjeremylt 
21241203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
21251203703bSJeremy L Thompson   if (is_composite) {
2126250756a7Sjeremylt     // Composite Operator
2127250756a7Sjeremylt     if (op->ApplyAddComposite) {
21282b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2129cae8b89aSjeremylt     } else {
2130d1d35e2fSjeremylt       CeedInt       num_suboperators;
2131d1d35e2fSjeremylt       CeedOperator *sub_operators;
2132250756a7Sjeremylt 
21331c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
21341c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2135d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
21362b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
21371d7d2407SJeremy L Thompson       }
2138250756a7Sjeremylt     }
21391203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
21403ca2b39bSJeremy L Thompson     // Standard Operator
21413ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2142250756a7Sjeremylt   }
2143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2144d7b241e6Sjeremylt }
2145d7b241e6Sjeremylt 
2146d7b241e6Sjeremylt /**
2147536b928cSSebastian Grimberg   @brief Destroy temporary assembly data associated with a `CeedOperator`
2148536b928cSSebastian Grimberg 
2149536b928cSSebastian Grimberg   @param[in,out] op `CeedOperator` whose assembly data to destroy
2150536b928cSSebastian Grimberg 
2151536b928cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2152536b928cSSebastian Grimberg 
2153536b928cSSebastian Grimberg   @ref User
2154536b928cSSebastian Grimberg **/
2155536b928cSSebastian Grimberg int CeedOperatorAssemblyDataStrip(CeedOperator op) {
2156536b928cSSebastian Grimberg   bool is_composite;
2157536b928cSSebastian Grimberg 
2158536b928cSSebastian Grimberg   CeedCall(CeedQFunctionAssemblyDataDestroy(&op->qf_assembled));
2159536b928cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataDestroy(&op->op_assembled));
2160536b928cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2161536b928cSSebastian Grimberg   if (is_composite) {
2162536b928cSSebastian Grimberg     CeedInt       num_suboperators;
2163536b928cSSebastian Grimberg     CeedOperator *sub_operators;
2164536b928cSSebastian Grimberg 
2165536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2166536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2167536b928cSSebastian Grimberg     for (CeedInt i = 0; i < num_suboperators; i++) {
2168536b928cSSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataDestroy(&sub_operators[i]->qf_assembled));
2169536b928cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataDestroy(&sub_operators[i]->op_assembled));
2170536b928cSSebastian Grimberg     }
2171536b928cSSebastian Grimberg   }
2172536b928cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2173536b928cSSebastian Grimberg }
2174536b928cSSebastian Grimberg 
2175536b928cSSebastian Grimberg /**
2176ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2177d7b241e6Sjeremylt 
2178ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2179b11c1e72Sjeremylt 
2180b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2181dfdf5a53Sjeremylt 
21827a982d89SJeremy L. Thompson   @ref User
2183b11c1e72Sjeremylt **/
2184d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2185ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2186ad6481ceSJeremy L Thompson     *op = NULL;
2187ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2188ad6481ceSJeremy L Thompson   }
2189f883f0a5SSebastian Grimberg   // Backend destroy
2190f883f0a5SSebastian Grimberg   if ((*op)->Destroy) {
2191f883f0a5SSebastian Grimberg     CeedCall((*op)->Destroy(*op));
2192f883f0a5SSebastian Grimberg   }
2193fe2413ffSjeremylt   // Free fields
21942b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2195d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2196437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2197437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
219815910d16Sjeremylt       }
2199356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
22002b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
220171352a93Sjeremylt       }
22022b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
22032b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
220471352a93Sjeremylt       }
22052b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
22062b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2207fe2413ffSjeremylt     }
22082b730f8bSJeremy L Thompson   }
22092b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2210d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2211437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2212356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
22132b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
221471352a93Sjeremylt       }
22152b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
22162b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
221771352a93Sjeremylt       }
22182b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
22192b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
22202b730f8bSJeremy L Thompson     }
2221fe2413ffSjeremylt   }
2222f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->input_fields));
2223f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->output_fields));
2224f883f0a5SSebastian Grimberg   // Destroy AtPoints data
222548acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
222648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
222748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2228c6b536a8SSebastian Grimberg   // Destroy assembly data (must happen before destroying sub_operators)
2229f883f0a5SSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataStrip(*op));
2230d1d35e2fSjeremylt   // Destroy sub_operators
22312b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2232d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
22332b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
223452d6035fSJeremy L Thompson     }
22352b730f8bSJeremy L Thompson   }
2236f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->sub_operators));
22372b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
22382b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
22392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
22403668ca4bSJeremy L Thompson   // Destroy any composite labels
22415ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
22423668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
22432b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
22442b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
22453668ca4bSJeremy L Thompson     }
22465ac9af79SJeremy L Thompson   }
22472b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2248fe2413ffSjeremylt 
22495107b09fSJeremy L Thompson   // Destroy fallback
22502b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
22515107b09fSJeremy L Thompson 
22522b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
2253f883f0a5SSebastian Grimberg   CeedCall(CeedDestroy(&(*op)->ceed));
22542b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2255e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2256d7b241e6Sjeremylt }
2257d7b241e6Sjeremylt 
2258d7b241e6Sjeremylt /// @}
2259