xref: /libCEED/interface/ceed-operator.c (revision edb2538e3dd6743c029967fc4e89c6fcafedb8c2)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
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 /**
25e15f9bd0SJeremy L Thompson   @brief Check if a CeedOperator Field matches the QFunction Field
26e15f9bd0SJeremy L Thompson 
27e15f9bd0SJeremy L Thompson   @param[in] ceed     Ceed object for error handling
28d1d35e2fSjeremylt   @param[in] qf_field QFunction Field matching Operator Field
29e15f9bd0SJeremy L Thompson   @param[in] r        Operator Field ElemRestriction
30e15f9bd0SJeremy L Thompson   @param[in] b        Operator Field Basis
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 **/
362b730f8bSJeremy L Thompson static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction r, CeedBasis b) {
37*edb2538eSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size = qf_field->size;
381c66c397SJeremy L Thompson   CeedEvalMode eval_mode = qf_field->eval_mode;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
416574a04fSJeremy L Thompson   CeedCheck((r == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
426574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
44*edb2538eSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &rstr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
47356036faSJeremy L Thompson   CeedCheck((b == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
48356036faSJeremy L Thompson             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
49356036faSJeremy L Thompson   if (b != CEED_BASIS_NONE) {
502b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
52c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp));
53*edb2538eSJeremy L Thompson     CeedCheck(r == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
546574a04fSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components, but Basis has %" CeedInt_FMT
556574a04fSJeremy L Thompson               " components",
56*edb2538eSJeremy L Thompson               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp, num_comp);
57e15f9bd0SJeremy L Thompson   }
58e15f9bd0SJeremy L Thompson   // Field size
59d1d35e2fSjeremylt   switch (eval_mode) {
60e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
61*edb2538eSJeremy L Thompson       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
62953dad27SJames Wright                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
63*edb2538eSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp);
64e15f9bd0SJeremy L Thompson       break;
65e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
66c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
67c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
68c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
696574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
706574a04fSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components", qf_field->field_name,
716574a04fSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72e15f9bd0SJeremy L Thompson       break;
73e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
74d1d35e2fSjeremylt       // No additional checks required
75e15f9bd0SJeremy L Thompson       break;
76e15f9bd0SJeremy L Thompson   }
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
817a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
827a982d89SJeremy L. Thompson 
837a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
84d1d35e2fSjeremylt   @param[in] qf_field     QFunction field (carries field name)
85d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
864c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
887a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @ref Utility
937a982d89SJeremy L. Thompson **/
942b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
957a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
96d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
977a982d89SJeremy L. Thompson 
982b730f8bSJeremy L Thompson   fprintf(stream,
992b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1002b730f8bSJeremy L Thompson           ":\n"
1017a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
102d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
103990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
1042b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
105356036faSJeremy L Thompson   if (field->basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
1062b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1072b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
1127a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1157a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1167a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1197a982d89SJeremy L. Thompson 
1207a982d89SJeremy L. Thompson   @ref Utility
1217a982d89SJeremy L. Thompson **/
1227a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1237a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1241c66c397SJeremy L Thompson   CeedInt     num_elem, num_qpts, total_fields = 0;
1257a982d89SJeremy L. Thompson 
1262b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1291c66c397SJeremy L Thompson 
1302b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1312b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1322b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
133d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1357a982d89SJeremy L. Thompson   }
1362b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
137d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1382b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1397a982d89SJeremy L. Thompson   }
140e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1417a982d89SJeremy L. Thompson }
1427a982d89SJeremy L. Thompson 
143d99fa3c5SJeremy L Thompson /**
1440f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
145eaf62fffSJeremy L Thompson 
146eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
1470f60e0a8SJeremy L Thompson   @param[out] active_basis Basis for active input vector or NULL for composite operator
148eaf62fffSJeremy L Thompson 
149eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150eaf62fffSJeremy L Thompson 
151eaf62fffSJeremy L Thompson   @ref Developer
152eaf62fffSJeremy L Thompson **/
153eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
1546aaed0edSJeremy L Thompson   Ceed ceed;
1551c66c397SJeremy L Thompson 
1566aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1576aaed0edSJeremy L Thompson 
158eaf62fffSJeremy L Thompson   *active_basis = NULL;
1590f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1602b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
161eaf62fffSJeremy L Thompson     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1626574a04fSJeremy L Thompson       CeedCheck(!*active_basis || *active_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found");
163eaf62fffSJeremy L Thompson       *active_basis = op->input_fields[i]->basis;
164eaf62fffSJeremy L Thompson     }
1652b730f8bSJeremy L Thompson   }
166eaf62fffSJeremy L Thompson 
1676574a04fSJeremy L Thompson   CeedCheck(*active_basis, ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found");
168eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
169eaf62fffSJeremy L Thompson }
170eaf62fffSJeremy L Thompson 
171eaf62fffSJeremy L Thompson /**
1720f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
173e2f04181SAndrew T. Barker 
174e2f04181SAndrew T. Barker   @param[in] op           CeedOperator to find active basis for
1750f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator
176e2f04181SAndrew T. Barker 
177e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
178e2f04181SAndrew T. Barker 
179e2f04181SAndrew T. Barker   @ref Utility
180e2f04181SAndrew T. Barker **/
1812b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
1826aaed0edSJeremy L Thompson   Ceed ceed;
1831c66c397SJeremy L Thompson 
1846aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1856aaed0edSJeremy L Thompson 
186d1d35e2fSjeremylt   *active_rstr = NULL;
1870f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1882b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
189d1d35e2fSjeremylt     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1906574a04fSJeremy L Thompson       CeedCheck(!*active_rstr || *active_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
1916574a04fSJeremy L Thompson                 "Multiple active CeedElemRestrictions found");
192437c7c90SJeremy L Thompson       *active_rstr = op->input_fields[i]->elem_rstr;
193e2f04181SAndrew T. Barker     }
1942b730f8bSJeremy L Thompson   }
195e2f04181SAndrew T. Barker 
1966574a04fSJeremy L Thompson   CeedCheck(*active_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found");
197e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
198e2f04181SAndrew T. Barker }
199e2f04181SAndrew T. Barker 
200d8dd9a91SJeremy L Thompson /**
2012788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
2024385fb7fSSebastian Grimberg 
203ea61e9acSJeremy L Thompson   For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
2049fd66db6SSebastian Grimberg   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
2059fd66db6SSebastian Grimberg any field of a matching type.
206d8dd9a91SJeremy L Thompson 
207ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
208ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
209ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2102788fa27SJeremy L Thompson   @param[in]     values      Values to set
211d8dd9a91SJeremy L Thompson 
212d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
213d8dd9a91SJeremy L Thompson 
214d8dd9a91SJeremy L Thompson   @ref User
215d8dd9a91SJeremy L Thompson **/
2162788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2171c66c397SJeremy L Thompson   bool is_composite = false;
2181c66c397SJeremy L Thompson 
2196574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2203668ca4bSJeremy L Thompson 
2215ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2225ac9af79SJeremy L Thompson   if (field_label->from_op) {
2235ac9af79SJeremy L Thompson     CeedInt index = -1;
2245ac9af79SJeremy L Thompson 
2255ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2265ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2275ac9af79SJeremy L Thompson     }
2286574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2295ac9af79SJeremy L Thompson   }
2305ac9af79SJeremy L Thompson 
2312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
232d8dd9a91SJeremy L Thompson   if (is_composite) {
233d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
234d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
235d8dd9a91SJeremy L Thompson 
236c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
237c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2386574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2396574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
240d8dd9a91SJeremy L Thompson 
241d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
242d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
2433668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
2442788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
245d8dd9a91SJeremy L Thompson       }
246d8dd9a91SJeremy L Thompson     }
247d8dd9a91SJeremy L Thompson   } else {
2486574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
2492788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
2502788fa27SJeremy L Thompson   }
2512788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2522788fa27SJeremy L Thompson }
2532788fa27SJeremy L Thompson 
2542788fa27SJeremy L Thompson /**
2552788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
2564385fb7fSSebastian Grimberg 
2572788fa27SJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
2589fd66db6SSebastian Grimberg   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
2599fd66db6SSebastian Grimberg any field of a matching type.
2602788fa27SJeremy L Thompson 
2612788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
2622788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
2632788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
264c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
265c5d0f995SJed Brown   @param[out]    values      Values in the label
2662788fa27SJeremy L Thompson 
2672788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2682788fa27SJeremy L Thompson 
2692788fa27SJeremy L Thompson   @ref User
2702788fa27SJeremy L Thompson **/
2712788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
2722788fa27SJeremy L Thompson                                              void *values) {
2731c66c397SJeremy L Thompson   bool is_composite = false;
2741c66c397SJeremy L Thompson 
2756574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2762788fa27SJeremy L Thompson 
2772788fa27SJeremy L Thompson   *(void **)values = NULL;
2782788fa27SJeremy L Thompson   *num_values      = 0;
2792788fa27SJeremy L Thompson 
2805ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2815ac9af79SJeremy L Thompson   if (field_label->from_op) {
2825ac9af79SJeremy L Thompson     CeedInt index = -1;
2835ac9af79SJeremy L Thompson 
2845ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2855ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2865ac9af79SJeremy L Thompson     }
2876574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2885ac9af79SJeremy L Thompson   }
2895ac9af79SJeremy L Thompson 
2902788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2912788fa27SJeremy L Thompson   if (is_composite) {
2922788fa27SJeremy L Thompson     CeedInt       num_sub;
2932788fa27SJeremy L Thompson     CeedOperator *sub_operators;
2942788fa27SJeremy L Thompson 
2952788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
2962788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2976574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2986574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
2992788fa27SJeremy L Thompson 
3002788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3012788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3022788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3032788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3042788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3052788fa27SJeremy L Thompson       }
3062788fa27SJeremy L Thompson     }
3072788fa27SJeremy L Thompson   } else {
3086574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3092788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3102788fa27SJeremy L Thompson   }
3112788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3122788fa27SJeremy L Thompson }
3132788fa27SJeremy L Thompson 
3142788fa27SJeremy L Thompson /**
3152788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
3164385fb7fSSebastian Grimberg 
3172788fa27SJeremy L Thompson   For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
3189fd66db6SSebastian Grimberg   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
3199fd66db6SSebastian Grimberg any field of a matching type.
3202788fa27SJeremy L Thompson 
3212788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3222788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3232788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
324c5d0f995SJed Brown   @param[in]     values      Values array to restore
3252788fa27SJeremy L Thompson 
3262788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3272788fa27SJeremy L Thompson 
3282788fa27SJeremy L Thompson   @ref User
3292788fa27SJeremy L Thompson **/
3302788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3311c66c397SJeremy L Thompson   bool is_composite = false;
3321c66c397SJeremy L Thompson 
3336574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3342788fa27SJeremy L Thompson 
3355ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3365ac9af79SJeremy L Thompson   if (field_label->from_op) {
3375ac9af79SJeremy L Thompson     CeedInt index = -1;
3385ac9af79SJeremy L Thompson 
3395ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3405ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3415ac9af79SJeremy L Thompson     }
3426574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3435ac9af79SJeremy L Thompson   }
3445ac9af79SJeremy L Thompson 
3452788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3462788fa27SJeremy L Thompson   if (is_composite) {
3472788fa27SJeremy L Thompson     CeedInt       num_sub;
3482788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3492788fa27SJeremy L Thompson 
3502788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3512788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3526574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3536574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3542788fa27SJeremy L Thompson 
3552788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3562788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3572788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3582788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
3592788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3602788fa27SJeremy L Thompson       }
3612788fa27SJeremy L Thompson     }
3622788fa27SJeremy L Thompson   } else {
3636574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3642788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
365d8dd9a91SJeremy L Thompson   }
366d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
367d8dd9a91SJeremy L Thompson }
368d8dd9a91SJeremy L Thompson 
3697a982d89SJeremy L. Thompson /// @}
3707a982d89SJeremy L. Thompson 
3717a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3727a982d89SJeremy L. Thompson /// CeedOperator Backend API
3737a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3747a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
3757a982d89SJeremy L. Thompson /// @{
3767a982d89SJeremy L. Thompson 
3777a982d89SJeremy L. Thompson /**
3787a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
3797a982d89SJeremy L. Thompson 
380ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
381d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
3827a982d89SJeremy L. Thompson 
3837a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3847a982d89SJeremy L. Thompson 
3857a982d89SJeremy L. Thompson   @ref Backend
3867a982d89SJeremy L. Thompson **/
387d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
3886574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
389d1d35e2fSjeremylt   *num_args = op->num_fields;
390e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3917a982d89SJeremy L. Thompson }
3927a982d89SJeremy L. Thompson 
3937a982d89SJeremy L. Thompson /**
3947a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
3957a982d89SJeremy L. Thompson 
396ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
397d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
3987a982d89SJeremy L. Thompson 
3997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4007a982d89SJeremy L. Thompson 
4017a982d89SJeremy L. Thompson   @ref Backend
4027a982d89SJeremy L. Thompson **/
403d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
404f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
405e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4067a982d89SJeremy L. Thompson }
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson /**
4097a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4107a982d89SJeremy L. Thompson 
411ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4127a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4157a982d89SJeremy L. Thompson 
4167a982d89SJeremy L. Thompson   @ref Backend
4177a982d89SJeremy L. Thompson **/
4187a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
4196574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
4207a982d89SJeremy L. Thompson   *qf = op->qf;
421e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4227a982d89SJeremy L. Thompson }
4237a982d89SJeremy L. Thompson 
4247a982d89SJeremy L. Thompson /**
425c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
426c04a41a7SJeremy L Thompson 
427ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
428d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
429c04a41a7SJeremy L Thompson 
430c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
431c04a41a7SJeremy L Thompson 
432c04a41a7SJeremy L Thompson   @ref Backend
433c04a41a7SJeremy L Thompson **/
434d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
435f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
436e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
437c04a41a7SJeremy L Thompson }
438c04a41a7SJeremy L Thompson 
439c04a41a7SJeremy L Thompson /**
4407a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
4417a982d89SJeremy L. Thompson 
442ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
4437a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson   @ref Backend
4487a982d89SJeremy L. Thompson **/
449777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
450777ff853SJeremy L Thompson   *(void **)data = op->data;
451e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4527a982d89SJeremy L. Thompson }
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson /**
4557a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
4567a982d89SJeremy L. Thompson 
457ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
458ea61e9acSJeremy L Thompson   @param[in]     data Data to set
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson   @ref Backend
4637a982d89SJeremy L. Thompson **/
464777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
465777ff853SJeremy L Thompson   op->data = data;
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4677a982d89SJeremy L. Thompson }
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson /**
47034359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
47134359f16Sjeremylt 
472ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
47334359f16Sjeremylt 
47434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
47534359f16Sjeremylt 
47634359f16Sjeremylt   @ref Backend
47734359f16Sjeremylt **/
4789560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
47934359f16Sjeremylt   op->ref_count++;
48034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
48134359f16Sjeremylt }
48234359f16Sjeremylt 
48334359f16Sjeremylt /**
4847a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
4857a982d89SJeremy L. Thompson 
486ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4897a982d89SJeremy L. Thompson 
4907a982d89SJeremy L. Thompson   @ref Backend
4917a982d89SJeremy L. Thompson **/
4927a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
493f04ea552SJeremy L Thompson   op->is_backend_setup = true;
494e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4957a982d89SJeremy L. Thompson }
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson /// @}
4987a982d89SJeremy L. Thompson 
4997a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5007a982d89SJeremy L. Thompson /// CeedOperator Public API
5017a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5027a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
503dfdf5a53Sjeremylt /// @{
504d7b241e6Sjeremylt 
505d7b241e6Sjeremylt /**
506ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
5074385fb7fSSebastian Grimberg 
508ea61e9acSJeremy L Thompson   A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
509d7b241e6Sjeremylt 
510ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
511ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
512ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
513ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
514ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
515b11c1e72Sjeremylt 
516b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
517dfdf5a53Sjeremylt 
5187a982d89SJeremy L. Thompson   @ref User
519d7b241e6Sjeremylt  */
5202b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5215fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5225fe0d4faSjeremylt     Ceed delegate;
5236574a04fSJeremy L Thompson 
5242b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
5256574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
5262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
527e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5285fe0d4faSjeremylt   }
5295fe0d4faSjeremylt 
5306574a04fSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
531db002c03SJeremy L Thompson 
5322b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
533db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
534d1d35e2fSjeremylt   (*op)->ref_count   = 1;
5352b104005SJeremy L Thompson   (*op)->input_size  = -1;
5362b104005SJeremy L Thompson   (*op)->output_size = -1;
537db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
538db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
539db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
5402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
5412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
5422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
5432b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
544e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
545d7b241e6Sjeremylt }
546d7b241e6Sjeremylt 
547d7b241e6Sjeremylt /**
54852d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
54952d6035fSJeremy L Thompson 
550ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
551ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
55252d6035fSJeremy L Thompson 
55352d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
55452d6035fSJeremy L Thompson 
5557a982d89SJeremy L. Thompson   @ref User
55652d6035fSJeremy L Thompson  */
55752d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
55852d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
55952d6035fSJeremy L Thompson     Ceed delegate;
56052d6035fSJeremy L Thompson 
5611c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
562250756a7Sjeremylt     if (delegate) {
5632b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
564e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
56552d6035fSJeremy L Thompson     }
566250756a7Sjeremylt   }
56752d6035fSJeremy L Thompson 
5682b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
569db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
570996d9ab5SJed Brown   (*op)->ref_count    = 1;
571f04ea552SJeremy L Thompson   (*op)->is_composite = true;
5722b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
5732b104005SJeremy L Thompson   (*op)->input_size  = -1;
5742b104005SJeremy L Thompson   (*op)->output_size = -1;
575250756a7Sjeremylt 
576db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
577e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
57852d6035fSJeremy L Thompson }
57952d6035fSJeremy L Thompson 
58052d6035fSJeremy L Thompson /**
581ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
5824385fb7fSSebastian Grimberg 
583ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedOperatorDestroy()`.
584512bb800SJeremy L Thompson 
585512bb800SJeremy 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.
586512bb800SJeremy L Thompson         This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
5879560d06aSjeremylt 
588ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
589ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
5909560d06aSjeremylt 
5919560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5929560d06aSjeremylt 
5939560d06aSjeremylt   @ref User
5949560d06aSjeremylt **/
5959560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
5962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
5972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
5989560d06aSjeremylt   *op_copy = op;
5999560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6009560d06aSjeremylt }
6019560d06aSjeremylt 
6029560d06aSjeremylt /**
603ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
604d7b241e6Sjeremylt 
605ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
606ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
607ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
608d7b241e6Sjeremylt 
609ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
610ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
611d7b241e6Sjeremylt 
612528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
613356036faSJeremy L Thompson   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of r.
614528a22edSJeremy L Thompson 
615ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
616ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
617ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
618356036faSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
6195ac9af79SJeremy L Thompson   @param[in]     v          CeedVector to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE
6205ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
621b11c1e72Sjeremylt 
622b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
623dfdf5a53Sjeremylt 
6247a982d89SJeremy L. Thompson   @ref User
625b11c1e72Sjeremylt **/
6262b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
6271c66c397SJeremy L Thompson   bool               is_input = true;
6281c66c397SJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0;
6291c66c397SJeremy L Thompson   CeedQFunctionField qf_field;
6301c66c397SJeremy L Thompson   CeedOperatorField *op_field;
6311c66c397SJeremy L Thompson 
6326574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
6336574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
6346574a04fSJeremy L Thompson   CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
6356574a04fSJeremy L Thompson   CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
6366574a04fSJeremy L Thompson   CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
63752d6035fSJeremy L Thompson 
6382b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
6396574a04fSJeremy L Thompson   CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
6402b730f8bSJeremy L Thompson             "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
641d7b241e6Sjeremylt 
642356036faSJeremy L Thompson   if (b == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts));
6439a18a30cSJeremy L Thompson   else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
644528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
645528a22edSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added Bases. Found %" CeedInt_FMT
646528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
647356036faSJeremy L Thompson             b == CEED_BASIS_NONE ? "ElemRestriction" : "Basis", num_qpts, op->num_qpts);
648d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
649d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
650d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
651d1d35e2fSjeremylt       op_field = &op->input_fields[i];
652d7b241e6Sjeremylt       goto found;
653d7b241e6Sjeremylt     }
654d7b241e6Sjeremylt   }
6552b104005SJeremy L Thompson   is_input = false;
656d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
657d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
658d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
659d1d35e2fSjeremylt       op_field = &op->output_fields[i];
660d7b241e6Sjeremylt       goto found;
661d7b241e6Sjeremylt     }
662d7b241e6Sjeremylt   }
663c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6642b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
665c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
666d7b241e6Sjeremylt found:
6672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6682b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
669e15f9bd0SJeremy L Thompson 
6702b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6712b104005SJeremy L Thompson     CeedSize l_size;
6721c66c397SJeremy L Thompson 
6732b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6742b104005SJeremy L Thompson     if (is_input) {
6752b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
6766574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6776574a04fSJeremy L Thompson                 op->input_size);
6782b104005SJeremy L Thompson     } else {
6792b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
6806574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6816574a04fSJeremy L Thompson                 op->output_size);
6822b104005SJeremy L Thompson     }
6832b730f8bSJeremy L Thompson   }
6842b104005SJeremy L Thompson 
685393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
686393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
687e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
688d1d35e2fSjeremylt     op->num_elem        = num_elem;
689d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
690e15f9bd0SJeremy L Thompson   }
691393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
6925a72c492SJeremy L Thompson   if (op->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
693e15f9bd0SJeremy L Thompson 
694d1d35e2fSjeremylt   op->num_fields += 1;
6952b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
696e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
697d7b241e6Sjeremylt }
698d7b241e6Sjeremylt 
699d7b241e6Sjeremylt /**
70043bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
70143bbe138SJeremy L Thompson 
702ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
703f04ea552SJeremy L Thompson 
704ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
705f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
70643bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
707f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
70843bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
70943bbe138SJeremy L Thompson 
71043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71143bbe138SJeremy L Thompson 
712e9b533fbSJeremy L Thompson   @ref Advanced
71343bbe138SJeremy L Thompson **/
7142b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
71543bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7166574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
71843bbe138SJeremy L Thompson 
71943bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
72043bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
72143bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
72243bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
72343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72443bbe138SJeremy L Thompson }
72543bbe138SJeremy L Thompson 
72643bbe138SJeremy L Thompson /**
727de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
728de5900adSJames Wright 
729de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
730de5900adSJames Wright 
731de5900adSJames Wright   @param[in]  op         CeedOperator
732de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
733de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
734de5900adSJames Wright 
735de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
736de5900adSJames Wright 
737de5900adSJames Wright   @ref Advanced
738de5900adSJames Wright **/
739de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
7401c66c397SJeremy L Thompson   char              *name;
741de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
742de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
743de5900adSJames Wright 
7441c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
745de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
746de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
747de5900adSJames Wright     if (!strcmp(name, field_name)) {
748de5900adSJames Wright       *op_field = input_fields[i];
749de5900adSJames Wright       return CEED_ERROR_SUCCESS;
750de5900adSJames Wright     }
751de5900adSJames Wright   }
752de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
753de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
754de5900adSJames Wright     if (!strcmp(name, field_name)) {
755de5900adSJames Wright       *op_field = output_fields[i];
756de5900adSJames Wright       return CEED_ERROR_SUCCESS;
757de5900adSJames Wright     }
758de5900adSJames Wright   }
759de5900adSJames Wright   // LCOV_EXCL_START
7601c66c397SJeremy L Thompson   bool has_name = op->name;
7611c66c397SJeremy L Thompson 
762de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
763de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
764de5900adSJames Wright   // LCOV_EXCL_STOP
765de5900adSJames Wright }
766de5900adSJames Wright 
767de5900adSJames Wright /**
76828567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
76928567f8fSJeremy L Thompson 
770ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
77128567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
77228567f8fSJeremy L Thompson 
77328567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
77428567f8fSJeremy L Thompson 
775e9b533fbSJeremy L Thompson   @ref Advanced
77628567f8fSJeremy L Thompson **/
77728567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
77828567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
77928567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78028567f8fSJeremy L Thompson }
78128567f8fSJeremy L Thompson 
78228567f8fSJeremy L Thompson /**
78343bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
78443bbe138SJeremy L Thompson 
785ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
78643bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
78743bbe138SJeremy L Thompson 
78843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78943bbe138SJeremy L Thompson 
790e9b533fbSJeremy L Thompson   @ref Advanced
79143bbe138SJeremy L Thompson **/
7922b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
793437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
79443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79543bbe138SJeremy L Thompson }
79643bbe138SJeremy L Thompson 
79743bbe138SJeremy L Thompson /**
79843bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
79943bbe138SJeremy L Thompson 
800ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
80143bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
80243bbe138SJeremy L Thompson 
80343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80443bbe138SJeremy L Thompson 
805e9b533fbSJeremy L Thompson   @ref Advanced
80643bbe138SJeremy L Thompson **/
80743bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
80843bbe138SJeremy L Thompson   *basis = op_field->basis;
80943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81043bbe138SJeremy L Thompson }
81143bbe138SJeremy L Thompson 
81243bbe138SJeremy L Thompson /**
81343bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
81443bbe138SJeremy L Thompson 
815ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
81643bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
81743bbe138SJeremy L Thompson 
81843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81943bbe138SJeremy L Thompson 
820e9b533fbSJeremy L Thompson   @ref Advanced
82143bbe138SJeremy L Thompson **/
82243bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
82343bbe138SJeremy L Thompson   *vec = op_field->vec;
82443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82543bbe138SJeremy L Thompson }
82643bbe138SJeremy L Thompson 
82743bbe138SJeremy L Thompson /**
82852d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
829288c0443SJeremy L Thompson 
830ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
831ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
83252d6035fSJeremy L Thompson 
83352d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83452d6035fSJeremy L Thompson 
8357a982d89SJeremy L. Thompson   @ref User
83652d6035fSJeremy L Thompson  */
8372b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
8386574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
8396574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
8406574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8412b730f8bSJeremy L Thompson 
8422b730f8bSJeremy L Thompson   {
8432b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8441c66c397SJeremy L Thompson 
8452b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8462b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8472b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8482b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
8496574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
8506574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8512b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8522b730f8bSJeremy L Thompson               "shape (%td, %td)",
8532b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8542b730f8bSJeremy L Thompson   }
8552b730f8bSJeremy L Thompson 
856d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
858d1d35e2fSjeremylt   composite_op->num_suboperators++;
859e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86052d6035fSJeremy L Thompson }
86152d6035fSJeremy L Thompson 
86252d6035fSJeremy L Thompson /**
86375f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
86475f0d5a4SJeremy L Thompson 
86575f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
86675f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
86775f0d5a4SJeremy L Thompson 
86875f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86975f0d5a4SJeremy L Thompson 
87075f0d5a4SJeremy L Thompson   @ref Backend
87175f0d5a4SJeremy L Thompson **/
872c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
8736574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
87475f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
87575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87675f0d5a4SJeremy L Thompson }
87775f0d5a4SJeremy L Thompson 
87875f0d5a4SJeremy L Thompson /**
87975f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
88075f0d5a4SJeremy L Thompson 
88175f0d5a4SJeremy L Thompson   @param op                  CeedOperator
88275f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
88375f0d5a4SJeremy L Thompson 
88475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88575f0d5a4SJeremy L Thompson 
88675f0d5a4SJeremy L Thompson   @ref Backend
88775f0d5a4SJeremy L Thompson **/
888c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
8896574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
89075f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
89175f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89275f0d5a4SJeremy L Thompson }
89375f0d5a4SJeremy L Thompson 
89475f0d5a4SJeremy L Thompson /**
8954db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
8964db537f9SJeremy L Thompson 
8974db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
8984db537f9SJeremy L Thompson 
8994db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9004db537f9SJeremy L Thompson 
9014db537f9SJeremy L Thompson   @ref User
9024db537f9SJeremy L Thompson **/
9034db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9044db537f9SJeremy L Thompson   Ceed ceed;
9051c66c397SJeremy L Thompson 
9062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9074db537f9SJeremy L Thompson 
9082b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9094db537f9SJeremy L Thompson 
9104db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9114db537f9SJeremy L Thompson   if (op->is_composite) {
91243622462SJeremy L Thompson     if (!op->num_suboperators) {
91343622462SJeremy L Thompson       // Empty operator setup
91443622462SJeremy L Thompson       op->input_size  = 0;
91543622462SJeremy L Thompson       op->output_size = 0;
91643622462SJeremy L Thompson     } else {
9174db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9182b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9194db537f9SJeremy L Thompson       }
9202b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9212b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9222b104005SJeremy L Thompson       CeedSize input_size, output_size;
9232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
92443622462SJeremy L Thompson     }
9254db537f9SJeremy L Thompson   } else {
9266574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9276574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9286574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9296574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9306574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9312b730f8bSJeremy L Thompson   }
9324db537f9SJeremy L Thompson 
9334db537f9SJeremy L Thompson   // Flag as immutable and ready
9344db537f9SJeremy L Thompson   op->is_interface_setup = true;
9352b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9362b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9372b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9384db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9394db537f9SJeremy L Thompson }
9404db537f9SJeremy L Thompson 
9414db537f9SJeremy L Thompson /**
942c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
9434385fb7fSSebastian Grimberg 
944ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
945c9366a6bSJeremy L Thompson 
946c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
947c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
948c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
949c9366a6bSJeremy L Thompson 
950c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
951c9366a6bSJeremy L Thompson 
952c9366a6bSJeremy L Thompson   @ref User
953c9366a6bSJeremy L Thompson **/
9542b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
955c9366a6bSJeremy L Thompson   bool is_composite;
956c9366a6bSJeremy L Thompson 
9572b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9582b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
959c9366a6bSJeremy L Thompson 
9602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9612b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
962c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
963c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9641c66c397SJeremy L Thompson 
9652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9662b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9672b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9682b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
9696574a04fSJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed,
9706574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9712b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9722b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9732b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
974c9366a6bSJeremy L Thompson     }
9752b730f8bSJeremy L Thompson   }
976c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
977c9366a6bSJeremy L Thompson }
978c9366a6bSJeremy L Thompson 
979c9366a6bSJeremy L Thompson /**
980beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
9814385fb7fSSebastian Grimberg 
982ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
9839fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
9849fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
9859fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9868b919e6bSJeremy L Thompson 
987beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
988beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9898b919e6bSJeremy L Thompson 
9908b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9918b919e6bSJeremy L Thompson 
9928b919e6bSJeremy L Thompson   @ref Advanced
9938b919e6bSJeremy L Thompson **/
9942b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
9958b919e6bSJeremy L Thompson   bool is_composite;
9968b919e6bSJeremy L Thompson 
9972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9988b919e6bSJeremy L Thompson   if (is_composite) {
9998b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10002b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10018b919e6bSJeremy L Thompson     }
10028b919e6bSJeremy L Thompson   } else {
10032b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1004beecbf24SJeremy L Thompson   }
1005beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1006beecbf24SJeremy L Thompson }
1007beecbf24SJeremy L Thompson 
1008beecbf24SJeremy L Thompson /**
1009beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1010beecbf24SJeremy L Thompson 
1011beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10126e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1013beecbf24SJeremy L Thompson 
1014beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1015beecbf24SJeremy L Thompson 
1016beecbf24SJeremy L Thompson   @ref Advanced
1017beecbf24SJeremy L Thompson **/
10182b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1019beecbf24SJeremy L Thompson   bool is_composite;
1020beecbf24SJeremy L Thompson 
10212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1022beecbf24SJeremy L Thompson   if (is_composite) {
1023beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10242b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1025beecbf24SJeremy L Thompson     }
1026beecbf24SJeremy L Thompson   } else {
10272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10288b919e6bSJeremy L Thompson   }
10298b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10308b919e6bSJeremy L Thompson }
10318b919e6bSJeremy L Thompson 
10328b919e6bSJeremy L Thompson /**
1033cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
10344385fb7fSSebastian Grimberg 
1035ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1036ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1037cd4dfc48Sjeremylt 
1038ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1039ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1040cd4dfc48Sjeremylt 
1041cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1042cd4dfc48Sjeremylt 
1043e9b533fbSJeremy L Thompson   @ref Advanced
1044cd4dfc48Sjeremylt **/
1045cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
1046aca496beSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite CeedOperator");
10476574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1048aca496beSJeremy L Thompson   if (op->num_qpts > 0) {
1049aca496beSJeremy L Thompson     CeedWarn(
1050aca496beSJeremy L Thompson         "CeedOperatorSetNumQuadraturePoints will be removed from the libCEED interface in the next release.\n"
1051aca496beSJeremy L Thompson         "This function is reduntant and you can safely remove any calls to this function without replacing them.");
1052aca496beSJeremy L Thompson     CeedCheck(num_qpts == op->num_qpts, op->ceed, CEED_ERROR_DIMENSION, "Different number of quadrature points already defined for the CeedOperator");
1053aca496beSJeremy L Thompson   }
1054cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1055cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1056cd4dfc48Sjeremylt }
1057cd4dfc48Sjeremylt 
1058cd4dfc48Sjeremylt /**
1059ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1060ea6b5821SJeremy L Thompson 
1061ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1062ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1063ea6b5821SJeremy L Thompson 
1064ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1065ea6b5821SJeremy L Thompson 
1066ea6b5821SJeremy L Thompson   @ref User
1067ea6b5821SJeremy L Thompson **/
1068ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1069ea6b5821SJeremy L Thompson   char  *name_copy;
1070ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1071ea6b5821SJeremy L Thompson 
10722b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1073ea6b5821SJeremy L Thompson   if (name_len > 0) {
10742b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1075ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1076ea6b5821SJeremy L Thompson     op->name = name_copy;
1077ea6b5821SJeremy L Thompson   }
1078ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1079ea6b5821SJeremy L Thompson }
1080ea6b5821SJeremy L Thompson 
1081ea6b5821SJeremy L Thompson /**
10827a982d89SJeremy L. Thompson   @brief View a CeedOperator
10837a982d89SJeremy L. Thompson 
10847a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
10857a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
10867a982d89SJeremy L. Thompson 
10877a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
10887a982d89SJeremy L. Thompson 
10897a982d89SJeremy L. Thompson   @ref User
10907a982d89SJeremy L. Thompson **/
10917a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1092ea6b5821SJeremy L Thompson   bool has_name = op->name;
10937a982d89SJeremy L. Thompson 
1094f04ea552SJeremy L Thompson   if (op->is_composite) {
10952b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
10967a982d89SJeremy L. Thompson 
1097d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1098ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
10992b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
11002b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
11017a982d89SJeremy L. Thompson     }
11027a982d89SJeremy L. Thompson   } else {
11032b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11042b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11057a982d89SJeremy L. Thompson   }
1106e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11077a982d89SJeremy L. Thompson }
11083bd813ffSjeremylt 
11093bd813ffSjeremylt /**
1110b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1111b7c9bbdaSJeremy L Thompson 
1112ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1113b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1114b7c9bbdaSJeremy L Thompson 
1115b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1116b7c9bbdaSJeremy L Thompson 
1117b7c9bbdaSJeremy L Thompson   @ref Advanced
1118b7c9bbdaSJeremy L Thompson **/
1119b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1120b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1121b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1122b7c9bbdaSJeremy L Thompson }
1123b7c9bbdaSJeremy L Thompson 
1124b7c9bbdaSJeremy L Thompson /**
1125b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1126b7c9bbdaSJeremy L Thompson 
1127ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1128b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1129b7c9bbdaSJeremy L Thompson 
1130b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1131b7c9bbdaSJeremy L Thompson 
1132b7c9bbdaSJeremy L Thompson   @ref Advanced
1133b7c9bbdaSJeremy L Thompson **/
1134b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
11356574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1136b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1137b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1138b7c9bbdaSJeremy L Thompson }
1139b7c9bbdaSJeremy L Thompson 
1140b7c9bbdaSJeremy L Thompson /**
1141b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1142b7c9bbdaSJeremy L Thompson 
1143ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1144b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1145b7c9bbdaSJeremy L Thompson 
1146b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1147b7c9bbdaSJeremy L Thompson 
1148b7c9bbdaSJeremy L Thompson   @ref Advanced
1149b7c9bbdaSJeremy L Thompson **/
1150b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
11516574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1152b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1153b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1154b7c9bbdaSJeremy L Thompson }
1155b7c9bbdaSJeremy L Thompson 
1156b7c9bbdaSJeremy L Thompson /**
11576e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
11586e15d496SJeremy L Thompson 
1159ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1160ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
11616e15d496SJeremy L Thompson 
11626e15d496SJeremy L Thompson   @ref Backend
11636e15d496SJeremy L Thompson **/
11649d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
11656e15d496SJeremy L Thompson   bool is_composite;
11661c66c397SJeremy L Thompson 
11672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11686e15d496SJeremy L Thompson 
11696e15d496SJeremy L Thompson   *flops = 0;
11702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11716e15d496SJeremy L Thompson   if (is_composite) {
11726e15d496SJeremy L Thompson     CeedInt num_suboperators;
11731c66c397SJeremy L Thompson 
1174c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11756e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1176c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11776e15d496SJeremy L Thompson 
11786e15d496SJeremy L Thompson     // FLOPs for each suboperator
11796e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11809d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11811c66c397SJeremy L Thompson 
11822b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11836e15d496SJeremy L Thompson       *flops += suboperator_flops;
11846e15d496SJeremy L Thompson     }
11856e15d496SJeremy L Thompson   } else {
11861c66c397SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
11876e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11881c66c397SJeremy L Thompson 
11892b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11902b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11914385fb7fSSebastian Grimberg 
11926e15d496SJeremy L Thompson     // Input FLOPs
11936e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11946e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1195*edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
11966e15d496SJeremy L Thompson 
1197*edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1198*edb2538eSJeremy L Thompson         *flops += rstr_flops;
11992b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
12006e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12016e15d496SJeremy L Thompson       }
12026e15d496SJeremy L Thompson     }
12036e15d496SJeremy L Thompson     // QF FLOPs
12041c66c397SJeremy L Thompson     {
12059d36ca50SJeremy L Thompson       CeedInt  num_qpts;
12069d36ca50SJeremy L Thompson       CeedSize qf_flops;
12071c66c397SJeremy L Thompson 
12082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12092b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12106e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
12111c66c397SJeremy L Thompson     }
12121c66c397SJeremy L Thompson 
12136e15d496SJeremy L Thompson     // Output FLOPs
12146e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12156e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1216*edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
12176e15d496SJeremy L Thompson 
1218*edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1219*edb2538eSJeremy L Thompson         *flops += rstr_flops;
12202b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12216e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12226e15d496SJeremy L Thompson       }
12236e15d496SJeremy L Thompson     }
12246e15d496SJeremy L Thompson   }
12256e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12266e15d496SJeremy L Thompson }
12276e15d496SJeremy L Thompson 
12286e15d496SJeremy L Thompson /**
12290126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12309fd66db6SSebastian Grimberg 
1231512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1232512bb800SJeremy L Thompson 
12339fd66db6SSebastian Grimberg   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.
12349fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12350126412dSJeremy L Thompson 
1236859c15bbSJames Wright   @param[in]  op  CeedOperator
12370126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12380126412dSJeremy L Thompson 
12390126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12400126412dSJeremy L Thompson 
1241859c15bbSJames Wright   @ref Advanced
12420126412dSJeremy L Thompson **/
12430126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
12446574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12450126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12460126412dSJeremy L Thompson   else *ctx = NULL;
12470126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12480126412dSJeremy L Thompson }
12490126412dSJeremy L Thompson 
12500126412dSJeremy L Thompson /**
1251ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12523668ca4bSJeremy L Thompson 
1253859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1254859c15bbSJames Wright 
12553668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12563668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12573668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12583668ca4bSJeremy L Thompson 
12593668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12603668ca4bSJeremy L Thompson 
12613668ca4bSJeremy L Thompson   @ref User
12623668ca4bSJeremy L Thompson **/
126317b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12641c66c397SJeremy L Thompson   bool is_composite, field_found = false;
12651c66c397SJeremy L Thompson 
12662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12672b730f8bSJeremy L Thompson 
12683668ca4bSJeremy L Thompson   if (is_composite) {
1269a98a090bSJeremy L Thompson     // Composite operator
1270a98a090bSJeremy L Thompson     // -- Check if composite label already created
12713668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12723668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12733668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12743668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12753668ca4bSJeremy L Thompson       }
12763668ca4bSJeremy L Thompson     }
12773668ca4bSJeremy L Thompson 
1278a98a090bSJeremy L Thompson     // -- Create composite label if needed
12793668ca4bSJeremy L Thompson     CeedInt               num_sub;
12803668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12813668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12823668ca4bSJeremy L Thompson 
12832b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1284c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1285c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12862b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12873668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12883668ca4bSJeremy L Thompson 
12893668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12903668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12913668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12921c66c397SJeremy L Thompson 
12932b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
12943668ca4bSJeremy L Thompson         if (new_field_label_i) {
1295a98a090bSJeremy L Thompson           field_found                    = true;
12963668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
12973668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
12983668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
12992b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
13007bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13012b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13022b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
13032b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
13047bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13057bfe0f0eSJeremy L Thompson           } else {
13067bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
13077bfe0f0eSJeremy L Thompson           }
13082b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
13097bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13102b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13112b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13127bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13137bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13147bfe0f0eSJeremy L Thompson           } else {
13157bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13167bfe0f0eSJeremy L Thompson           }
13173668ca4bSJeremy L Thompson         }
13183668ca4bSJeremy L Thompson       }
13193668ca4bSJeremy L Thompson     }
1320a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1321a98a090bSJeremy L Thompson     if (field_found) {
1322a98a090bSJeremy L Thompson       *field_label = new_field_label;
1323a98a090bSJeremy L Thompson     } else {
13243668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13252b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13262b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13273668ca4bSJeremy L Thompson       *field_label = NULL;
13283668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13295ac9af79SJeremy L Thompson     }
13305ac9af79SJeremy L Thompson   } else {
1331a98a090bSJeremy L Thompson     // Single, non-composite operator
1332a98a090bSJeremy L Thompson     if (op->qf->ctx) {
13335ac9af79SJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1334a98a090bSJeremy L Thompson     } else {
1335a98a090bSJeremy L Thompson       *field_label = NULL;
1336a98a090bSJeremy L Thompson     }
13375ac9af79SJeremy L Thompson   }
13385ac9af79SJeremy L Thompson 
13395ac9af79SJeremy L Thompson   // Set label in operator
13405ac9af79SJeremy L Thompson   if (*field_label) {
13415ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13425ac9af79SJeremy L Thompson 
13433668ca4bSJeremy L Thompson     // Move new composite label to operator
13443668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13452b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13463668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13473668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13482b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13493668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13503668ca4bSJeremy L Thompson     }
13515ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13523668ca4bSJeremy L Thompson     op->num_context_labels++;
13533668ca4bSJeremy L Thompson   }
13543668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13553668ca4bSJeremy L Thompson }
13563668ca4bSJeremy L Thompson 
13573668ca4bSJeremy L Thompson /**
13582788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13594385fb7fSSebastian Grimberg 
13602788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1361d8dd9a91SJeremy L Thompson 
1362ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13632788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1364ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1365d8dd9a91SJeremy L Thompson 
1366d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1367d8dd9a91SJeremy L Thompson 
1368d8dd9a91SJeremy L Thompson   @ref User
1369d8dd9a91SJeremy L Thompson **/
137017b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13712b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1372d8dd9a91SJeremy L Thompson }
1373d8dd9a91SJeremy L Thompson 
1374d8dd9a91SJeremy L Thompson /**
13752788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13764385fb7fSSebastian Grimberg 
13772788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13782788fa27SJeremy L Thompson 
13792788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13802788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13812788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13822788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13832788fa27SJeremy L Thompson 
13842788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13852788fa27SJeremy L Thompson 
13862788fa27SJeremy L Thompson   @ref User
13872788fa27SJeremy L Thompson **/
138817b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13892788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13902788fa27SJeremy L Thompson }
13912788fa27SJeremy L Thompson 
13922788fa27SJeremy L Thompson /**
13932788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
13942788fa27SJeremy L Thompson 
13952788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13962788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
13972788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13982788fa27SJeremy L Thompson 
13992788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14002788fa27SJeremy L Thompson 
14012788fa27SJeremy L Thompson   @ref User
14022788fa27SJeremy L Thompson **/
140317b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
14042788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
14052788fa27SJeremy L Thompson }
14062788fa27SJeremy L Thompson 
14072788fa27SJeremy L Thompson /**
14082788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
14094385fb7fSSebastian Grimberg 
14102788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1411d8dd9a91SJeremy L Thompson 
1412ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1413ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1414ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1415d8dd9a91SJeremy L Thompson 
1416d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1417d8dd9a91SJeremy L Thompson 
1418d8dd9a91SJeremy L Thompson   @ref User
1419d8dd9a91SJeremy L Thompson **/
142017b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14212b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1422d8dd9a91SJeremy L Thompson }
1423d8dd9a91SJeremy L Thompson 
1424d8dd9a91SJeremy L Thompson /**
14252788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14264385fb7fSSebastian Grimberg 
14272788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14282788fa27SJeremy L Thompson 
14292788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14302788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1431c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14322788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14332788fa27SJeremy L Thompson 
14342788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14352788fa27SJeremy L Thompson 
14362788fa27SJeremy L Thompson   @ref User
14372788fa27SJeremy L Thompson **/
143817b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14392788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14402788fa27SJeremy L Thompson }
14412788fa27SJeremy L Thompson 
14422788fa27SJeremy L Thompson /**
14432788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14442788fa27SJeremy L Thompson 
14452788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14462788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14472788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14482788fa27SJeremy L Thompson 
14492788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14502788fa27SJeremy L Thompson 
14512788fa27SJeremy L Thompson   @ref User
14522788fa27SJeremy L Thompson **/
145317b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14542788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14552788fa27SJeremy L Thompson }
14562788fa27SJeremy L Thompson 
14572788fa27SJeremy L Thompson /**
14583bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1459d7b241e6Sjeremylt 
1460ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1461ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1462d7b241e6Sjeremylt 
1463ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1464f04ea552SJeremy L Thompson 
1465ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1466ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14675ac9af79SJeremy L Thompson   @param[out] out     CeedVector to store result of applying operator (must be distinct from @a in) or @ref CEED_VECTOR_NONE if there are no
14685ac9af79SJeremy L Thompson active outputs
1469ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1470b11c1e72Sjeremylt 
1471b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1472dfdf5a53Sjeremylt 
14737a982d89SJeremy L. Thompson   @ref User
1474b11c1e72Sjeremylt **/
14752b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1477d7b241e6Sjeremylt 
14783ca2b39bSJeremy L Thompson   if (op->is_composite) {
1479250756a7Sjeremylt     // Composite Operator
1480250756a7Sjeremylt     if (op->ApplyComposite) {
14812b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1482250756a7Sjeremylt     } else {
1483d1d35e2fSjeremylt       CeedInt       num_suboperators;
1484d1d35e2fSjeremylt       CeedOperator *sub_operators;
14851c66c397SJeremy L Thompson 
14861c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1487c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1488250756a7Sjeremylt 
1489250756a7Sjeremylt       // Zero all output vectors
14903ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1491d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1492d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1493d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
14941c66c397SJeremy L Thompson 
1495250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
14962b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1497250756a7Sjeremylt           }
1498250756a7Sjeremylt         }
1499250756a7Sjeremylt       }
1500250756a7Sjeremylt       // Apply
1501f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
1502f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1503cae8b89aSjeremylt       }
1504cae8b89aSjeremylt     }
15053ca2b39bSJeremy L Thompson   } else {
15063ca2b39bSJeremy L Thompson     // Standard Operator
15073ca2b39bSJeremy L Thompson     if (op->Apply) {
15083ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
15093ca2b39bSJeremy L Thompson     } else {
15103ca2b39bSJeremy L Thompson       // Zero all output vectors
15113ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
15121c66c397SJeremy L Thompson 
15133ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
15143ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
15151c66c397SJeremy L Thompson 
15163ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
15173ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
15183ca2b39bSJeremy L Thompson       }
15193ca2b39bSJeremy L Thompson       // Apply
15203ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15213ca2b39bSJeremy L Thompson     }
1522250756a7Sjeremylt   }
1523e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1524cae8b89aSjeremylt }
1525cae8b89aSjeremylt 
1526cae8b89aSjeremylt /**
1527cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1528cae8b89aSjeremylt 
1529ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1530ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1531cae8b89aSjeremylt 
1532ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1533ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1534ea61e9acSJeremy L Thompson   @param[out] out     CeedVector to sum in result of applying operator (must be distinct from @a in) or NULL if there are no active outputs
1535ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1536cae8b89aSjeremylt 
1537cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1538cae8b89aSjeremylt 
15397a982d89SJeremy L. Thompson   @ref User
1540cae8b89aSjeremylt **/
15412b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1543cae8b89aSjeremylt 
15443ca2b39bSJeremy L Thompson   if (op->is_composite) {
1545250756a7Sjeremylt     // Composite Operator
1546250756a7Sjeremylt     if (op->ApplyAddComposite) {
15472b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1548cae8b89aSjeremylt     } else {
1549d1d35e2fSjeremylt       CeedInt       num_suboperators;
1550d1d35e2fSjeremylt       CeedOperator *sub_operators;
1551250756a7Sjeremylt 
15521c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15531c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1554d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15552b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15561d7d2407SJeremy L Thompson       }
1557250756a7Sjeremylt     }
15583ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
15593ca2b39bSJeremy L Thompson     // Standard Operator
15603ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1561250756a7Sjeremylt   }
1562e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1563d7b241e6Sjeremylt }
1564d7b241e6Sjeremylt 
1565d7b241e6Sjeremylt /**
1566b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1567d7b241e6Sjeremylt 
1568ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1569b11c1e72Sjeremylt 
1570b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1571dfdf5a53Sjeremylt 
15727a982d89SJeremy L. Thompson   @ref User
1573b11c1e72Sjeremylt **/
1574d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1575ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1576ad6481ceSJeremy L Thompson     *op = NULL;
1577ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1578ad6481ceSJeremy L Thompson   }
15792b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15802b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1581fe2413ffSjeremylt   // Free fields
15822b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1583d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1584437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1585437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
158615910d16Sjeremylt       }
1587356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
15882b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
158971352a93Sjeremylt       }
15902b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15912b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
159271352a93Sjeremylt       }
15932b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
15942b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1595fe2413ffSjeremylt     }
15962b730f8bSJeremy L Thompson   }
15972b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1598d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1599437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1600356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
16012b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
160271352a93Sjeremylt       }
16032b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
16042b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
160571352a93Sjeremylt       }
16062b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
16072b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
16082b730f8bSJeremy L Thompson     }
1609fe2413ffSjeremylt   }
1610d1d35e2fSjeremylt   // Destroy sub_operators
16112b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1612d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
16132b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
161452d6035fSJeremy L Thompson     }
16152b730f8bSJeremy L Thompson   }
16162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
16172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
16182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16193668ca4bSJeremy L Thompson   // Destroy any composite labels
16205ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16213668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16222b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16232b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16243668ca4bSJeremy L Thompson     }
16255ac9af79SJeremy L Thompson   }
16262b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1627fe2413ffSjeremylt 
16285107b09fSJeremy L Thompson   // Destroy fallback
16292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16305107b09fSJeremy L Thompson 
1631ed9e99e6SJeremy L Thompson   // Destroy assembly data
16322b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
163470a7ffb3SJeremy L Thompson 
16352b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16362b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16372b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16382b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16392b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1640e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1641d7b241e6Sjeremylt }
1642d7b241e6Sjeremylt 
1643d7b241e6Sjeremylt /// @}
1644