xref: /libCEED/interface/ceed-operator.c (revision 2c7e74134068de5221afb7e7255d8033016bfd12)
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) {
37edb2538eSJeremy 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) {
44edb2538eSJeremy 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));
53edb2538eSJeremy 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",
56edb2538eSJeremy 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:
61edb2538eSJeremy 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,
63edb2538eSJeremy 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);
641*2c7e7413SJeremy L Thompson   {
642*2c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
643*2c7e7413SJeremy L Thompson 
644*2c7e7413SJeremy L Thompson     CeedCall(CeedElemRestrictionGetType(r, &rstr_type));
645*2c7e7413SJeremy L Thompson     CeedCheck(rstr_type != CEED_RESTRICTION_POINTS, op->ceed, CEED_ERROR_UNSUPPORTED,
646*2c7e7413SJeremy L Thompson               "CeedElemRestrictionAtPoints not supported for standard operator fields");
647*2c7e7413SJeremy L Thompson   }
648d7b241e6Sjeremylt 
649356036faSJeremy L Thompson   if (b == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts));
6509a18a30cSJeremy L Thompson   else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
651528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
652528a22edSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added Bases. Found %" CeedInt_FMT
653528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
654356036faSJeremy L Thompson             b == CEED_BASIS_NONE ? "ElemRestriction" : "Basis", num_qpts, op->num_qpts);
655d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
656d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
657d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
658d1d35e2fSjeremylt       op_field = &op->input_fields[i];
659d7b241e6Sjeremylt       goto found;
660d7b241e6Sjeremylt     }
661d7b241e6Sjeremylt   }
6622b104005SJeremy L Thompson   is_input = false;
663d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
664d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
665d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
666d1d35e2fSjeremylt       op_field = &op->output_fields[i];
667d7b241e6Sjeremylt       goto found;
668d7b241e6Sjeremylt     }
669d7b241e6Sjeremylt   }
670c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6712b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
672c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
673d7b241e6Sjeremylt found:
6742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
676e15f9bd0SJeremy L Thompson 
6772b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6782b104005SJeremy L Thompson     CeedSize l_size;
6791c66c397SJeremy L Thompson 
6802b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6812b104005SJeremy L Thompson     if (is_input) {
6822b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
6836574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6846574a04fSJeremy L Thompson                 op->input_size);
6852b104005SJeremy L Thompson     } else {
6862b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
6876574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6886574a04fSJeremy L Thompson                 op->output_size);
6892b104005SJeremy L Thompson     }
6902b730f8bSJeremy L Thompson   }
6912b104005SJeremy L Thompson 
692393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
693393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
694e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
695d1d35e2fSjeremylt     op->num_elem        = num_elem;
696d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
697e15f9bd0SJeremy L Thompson   }
698393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
6995a72c492SJeremy L Thompson   if (op->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
700e15f9bd0SJeremy L Thompson 
701d1d35e2fSjeremylt   op->num_fields += 1;
7022b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
703e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
704d7b241e6Sjeremylt }
705d7b241e6Sjeremylt 
706d7b241e6Sjeremylt /**
70743bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
70843bbe138SJeremy L Thompson 
709ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
710f04ea552SJeremy L Thompson 
711ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
712f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
71343bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
714f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
71543bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
71643bbe138SJeremy L Thompson 
71743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71843bbe138SJeremy L Thompson 
719e9b533fbSJeremy L Thompson   @ref Advanced
72043bbe138SJeremy L Thompson **/
7212b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
72243bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7236574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7242b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
72543bbe138SJeremy L Thompson 
72643bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
72743bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
72843bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
72943bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
73043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73143bbe138SJeremy L Thompson }
73243bbe138SJeremy L Thompson 
73343bbe138SJeremy L Thompson /**
734de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
735de5900adSJames Wright 
736de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
737de5900adSJames Wright 
738de5900adSJames Wright   @param[in]  op         CeedOperator
739de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
740de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
741de5900adSJames Wright 
742de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
743de5900adSJames Wright 
744de5900adSJames Wright   @ref Advanced
745de5900adSJames Wright **/
746de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
7471c66c397SJeremy L Thompson   char              *name;
748de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
749de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
750de5900adSJames Wright 
7511c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
752de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
753de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
754de5900adSJames Wright     if (!strcmp(name, field_name)) {
755de5900adSJames Wright       *op_field = input_fields[i];
756de5900adSJames Wright       return CEED_ERROR_SUCCESS;
757de5900adSJames Wright     }
758de5900adSJames Wright   }
759de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
760de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
761de5900adSJames Wright     if (!strcmp(name, field_name)) {
762de5900adSJames Wright       *op_field = output_fields[i];
763de5900adSJames Wright       return CEED_ERROR_SUCCESS;
764de5900adSJames Wright     }
765de5900adSJames Wright   }
766de5900adSJames Wright   // LCOV_EXCL_START
7671c66c397SJeremy L Thompson   bool has_name = op->name;
7681c66c397SJeremy L Thompson 
769de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
770de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
771de5900adSJames Wright   // LCOV_EXCL_STOP
772de5900adSJames Wright }
773de5900adSJames Wright 
774de5900adSJames Wright /**
77528567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
77628567f8fSJeremy L Thompson 
777ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
77828567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
77928567f8fSJeremy L Thompson 
78028567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78128567f8fSJeremy L Thompson 
782e9b533fbSJeremy L Thompson   @ref Advanced
78328567f8fSJeremy L Thompson **/
78428567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
78528567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
78628567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78728567f8fSJeremy L Thompson }
78828567f8fSJeremy L Thompson 
78928567f8fSJeremy L Thompson /**
79043bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
79143bbe138SJeremy L Thompson 
792ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
79343bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
79443bbe138SJeremy L Thompson 
79543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79643bbe138SJeremy L Thompson 
797e9b533fbSJeremy L Thompson   @ref Advanced
79843bbe138SJeremy L Thompson **/
7992b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
800437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
80143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80243bbe138SJeremy L Thompson }
80343bbe138SJeremy L Thompson 
80443bbe138SJeremy L Thompson /**
80543bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
80643bbe138SJeremy L Thompson 
807ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
80843bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
80943bbe138SJeremy L Thompson 
81043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81143bbe138SJeremy L Thompson 
812e9b533fbSJeremy L Thompson   @ref Advanced
81343bbe138SJeremy L Thompson **/
81443bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
81543bbe138SJeremy L Thompson   *basis = op_field->basis;
81643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81743bbe138SJeremy L Thompson }
81843bbe138SJeremy L Thompson 
81943bbe138SJeremy L Thompson /**
82043bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
82143bbe138SJeremy L Thompson 
822ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
82343bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
82443bbe138SJeremy L Thompson 
82543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82643bbe138SJeremy L Thompson 
827e9b533fbSJeremy L Thompson   @ref Advanced
82843bbe138SJeremy L Thompson **/
82943bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
83043bbe138SJeremy L Thompson   *vec = op_field->vec;
83143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83243bbe138SJeremy L Thompson }
83343bbe138SJeremy L Thompson 
83443bbe138SJeremy L Thompson /**
83552d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
836288c0443SJeremy L Thompson 
837ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
838ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
83952d6035fSJeremy L Thompson 
84052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84152d6035fSJeremy L Thompson 
8427a982d89SJeremy L. Thompson   @ref User
84352d6035fSJeremy L Thompson  */
8442b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
8456574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
8466574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
8476574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8482b730f8bSJeremy L Thompson 
8492b730f8bSJeremy L Thompson   {
8502b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8511c66c397SJeremy L Thompson 
8522b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8532b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8542b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8552b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
8566574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
8576574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8582b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8592b730f8bSJeremy L Thompson               "shape (%td, %td)",
8602b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8612b730f8bSJeremy L Thompson   }
8622b730f8bSJeremy L Thompson 
863d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
865d1d35e2fSjeremylt   composite_op->num_suboperators++;
866e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86752d6035fSJeremy L Thompson }
86852d6035fSJeremy L Thompson 
86952d6035fSJeremy L Thompson /**
87075f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
87175f0d5a4SJeremy L Thompson 
87275f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
87375f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
87475f0d5a4SJeremy L Thompson 
87575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87675f0d5a4SJeremy L Thompson 
87775f0d5a4SJeremy L Thompson   @ref Backend
87875f0d5a4SJeremy L Thompson **/
879c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
8806574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
88175f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
88275f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88375f0d5a4SJeremy L Thompson }
88475f0d5a4SJeremy L Thompson 
88575f0d5a4SJeremy L Thompson /**
88675f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
88775f0d5a4SJeremy L Thompson 
88875f0d5a4SJeremy L Thompson   @param op                  CeedOperator
88975f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
89075f0d5a4SJeremy L Thompson 
89175f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89275f0d5a4SJeremy L Thompson 
89375f0d5a4SJeremy L Thompson   @ref Backend
89475f0d5a4SJeremy L Thompson **/
895c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
8966574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
89775f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
89875f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89975f0d5a4SJeremy L Thompson }
90075f0d5a4SJeremy L Thompson 
90175f0d5a4SJeremy L Thompson /**
9024db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
9034db537f9SJeremy L Thompson 
9044db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
9054db537f9SJeremy L Thompson 
9064db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9074db537f9SJeremy L Thompson 
9084db537f9SJeremy L Thompson   @ref User
9094db537f9SJeremy L Thompson **/
9104db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9114db537f9SJeremy L Thompson   Ceed ceed;
9121c66c397SJeremy L Thompson 
9132b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9144db537f9SJeremy L Thompson 
9152b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9164db537f9SJeremy L Thompson 
9174db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9184db537f9SJeremy L Thompson   if (op->is_composite) {
91943622462SJeremy L Thompson     if (!op->num_suboperators) {
92043622462SJeremy L Thompson       // Empty operator setup
92143622462SJeremy L Thompson       op->input_size  = 0;
92243622462SJeremy L Thompson       op->output_size = 0;
92343622462SJeremy L Thompson     } else {
9244db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9252b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9264db537f9SJeremy L Thompson       }
9272b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9282b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9292b104005SJeremy L Thompson       CeedSize input_size, output_size;
9302b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
93143622462SJeremy L Thompson     }
9324db537f9SJeremy L Thompson   } else {
9336574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9346574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9356574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9366574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9376574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9382b730f8bSJeremy L Thompson   }
9394db537f9SJeremy L Thompson 
9404db537f9SJeremy L Thompson   // Flag as immutable and ready
9414db537f9SJeremy L Thompson   op->is_interface_setup = true;
9422b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9432b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9442b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9454db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9464db537f9SJeremy L Thompson }
9474db537f9SJeremy L Thompson 
9484db537f9SJeremy L Thompson /**
949c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
9504385fb7fSSebastian Grimberg 
951ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
952c9366a6bSJeremy L Thompson 
953c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
954c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
955c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
956c9366a6bSJeremy L Thompson 
957c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
958c9366a6bSJeremy L Thompson 
959c9366a6bSJeremy L Thompson   @ref User
960c9366a6bSJeremy L Thompson **/
9612b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
962c9366a6bSJeremy L Thompson   bool is_composite;
963c9366a6bSJeremy L Thompson 
9642b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9652b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
966c9366a6bSJeremy L Thompson 
9672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9682b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
969c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
970c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9711c66c397SJeremy L Thompson 
9722b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9732b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9742b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9752b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
9766574a04fSJeremy 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,
9776574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9782b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9792b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9802b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
981c9366a6bSJeremy L Thompson     }
9822b730f8bSJeremy L Thompson   }
983c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
984c9366a6bSJeremy L Thompson }
985c9366a6bSJeremy L Thompson 
986c9366a6bSJeremy L Thompson /**
987beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
9884385fb7fSSebastian Grimberg 
989ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
9909fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
9919fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
9929fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9938b919e6bSJeremy L Thompson 
994beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
995beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9968b919e6bSJeremy L Thompson 
9978b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9988b919e6bSJeremy L Thompson 
9998b919e6bSJeremy L Thompson   @ref Advanced
10008b919e6bSJeremy L Thompson **/
10012b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
10028b919e6bSJeremy L Thompson   bool is_composite;
10038b919e6bSJeremy L Thompson 
10042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10058b919e6bSJeremy L Thompson   if (is_composite) {
10068b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10072b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10088b919e6bSJeremy L Thompson     }
10098b919e6bSJeremy L Thompson   } else {
10102b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1011beecbf24SJeremy L Thompson   }
1012beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1013beecbf24SJeremy L Thompson }
1014beecbf24SJeremy L Thompson 
1015beecbf24SJeremy L Thompson /**
1016beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1017beecbf24SJeremy L Thompson 
1018beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10196e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1020beecbf24SJeremy L Thompson 
1021beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1022beecbf24SJeremy L Thompson 
1023beecbf24SJeremy L Thompson   @ref Advanced
1024beecbf24SJeremy L Thompson **/
10252b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1026beecbf24SJeremy L Thompson   bool is_composite;
1027beecbf24SJeremy L Thompson 
10282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1029beecbf24SJeremy L Thompson   if (is_composite) {
1030beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1032beecbf24SJeremy L Thompson     }
1033beecbf24SJeremy L Thompson   } else {
10342b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10358b919e6bSJeremy L Thompson   }
10368b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10378b919e6bSJeremy L Thompson }
10388b919e6bSJeremy L Thompson 
10398b919e6bSJeremy L Thompson /**
1040cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
10414385fb7fSSebastian Grimberg 
1042ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1043ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1044cd4dfc48Sjeremylt 
1045ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1046ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1047cd4dfc48Sjeremylt 
1048cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1049cd4dfc48Sjeremylt 
1050e9b533fbSJeremy L Thompson   @ref Advanced
1051cd4dfc48Sjeremylt **/
1052cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
1053aca496beSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite CeedOperator");
10546574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1055aca496beSJeremy L Thompson   if (op->num_qpts > 0) {
1056aca496beSJeremy L Thompson     CeedWarn(
1057aca496beSJeremy L Thompson         "CeedOperatorSetNumQuadraturePoints will be removed from the libCEED interface in the next release.\n"
1058aca496beSJeremy L Thompson         "This function is reduntant and you can safely remove any calls to this function without replacing them.");
1059aca496beSJeremy L Thompson     CeedCheck(num_qpts == op->num_qpts, op->ceed, CEED_ERROR_DIMENSION, "Different number of quadrature points already defined for the CeedOperator");
1060aca496beSJeremy L Thompson   }
1061cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1062cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1063cd4dfc48Sjeremylt }
1064cd4dfc48Sjeremylt 
1065cd4dfc48Sjeremylt /**
1066ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1067ea6b5821SJeremy L Thompson 
1068ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1069ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1070ea6b5821SJeremy L Thompson 
1071ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1072ea6b5821SJeremy L Thompson 
1073ea6b5821SJeremy L Thompson   @ref User
1074ea6b5821SJeremy L Thompson **/
1075ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1076ea6b5821SJeremy L Thompson   char  *name_copy;
1077ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1078ea6b5821SJeremy L Thompson 
10792b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1080ea6b5821SJeremy L Thompson   if (name_len > 0) {
10812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1082ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1083ea6b5821SJeremy L Thompson     op->name = name_copy;
1084ea6b5821SJeremy L Thompson   }
1085ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1086ea6b5821SJeremy L Thompson }
1087ea6b5821SJeremy L Thompson 
1088ea6b5821SJeremy L Thompson /**
10897a982d89SJeremy L. Thompson   @brief View a CeedOperator
10907a982d89SJeremy L. Thompson 
10917a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
10927a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
10937a982d89SJeremy L. Thompson 
10947a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
10957a982d89SJeremy L. Thompson 
10967a982d89SJeremy L. Thompson   @ref User
10977a982d89SJeremy L. Thompson **/
10987a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1099ea6b5821SJeremy L Thompson   bool has_name = op->name;
11007a982d89SJeremy L. Thompson 
1101f04ea552SJeremy L Thompson   if (op->is_composite) {
11022b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11037a982d89SJeremy L. Thompson 
1104d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1105ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
11062b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
11072b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
11087a982d89SJeremy L. Thompson     }
11097a982d89SJeremy L. Thompson   } else {
11102b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11112b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11127a982d89SJeremy L. Thompson   }
1113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11147a982d89SJeremy L. Thompson }
11153bd813ffSjeremylt 
11163bd813ffSjeremylt /**
1117b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1118b7c9bbdaSJeremy L Thompson 
1119ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1120b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1121b7c9bbdaSJeremy L Thompson 
1122b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1123b7c9bbdaSJeremy L Thompson 
1124b7c9bbdaSJeremy L Thompson   @ref Advanced
1125b7c9bbdaSJeremy L Thompson **/
1126b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1127b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1128b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1129b7c9bbdaSJeremy L Thompson }
1130b7c9bbdaSJeremy L Thompson 
1131b7c9bbdaSJeremy L Thompson /**
1132b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1133b7c9bbdaSJeremy L Thompson 
1134ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1135b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1136b7c9bbdaSJeremy L Thompson 
1137b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1138b7c9bbdaSJeremy L Thompson 
1139b7c9bbdaSJeremy L Thompson   @ref Advanced
1140b7c9bbdaSJeremy L Thompson **/
1141b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
11426574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1143b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1144b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1145b7c9bbdaSJeremy L Thompson }
1146b7c9bbdaSJeremy L Thompson 
1147b7c9bbdaSJeremy L Thompson /**
1148b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1149b7c9bbdaSJeremy L Thompson 
1150ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1151b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1152b7c9bbdaSJeremy L Thompson 
1153b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1154b7c9bbdaSJeremy L Thompson 
1155b7c9bbdaSJeremy L Thompson   @ref Advanced
1156b7c9bbdaSJeremy L Thompson **/
1157b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
11586574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1159b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1160b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1161b7c9bbdaSJeremy L Thompson }
1162b7c9bbdaSJeremy L Thompson 
1163b7c9bbdaSJeremy L Thompson /**
11646e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
11656e15d496SJeremy L Thompson 
1166ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1167ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
11686e15d496SJeremy L Thompson 
11696e15d496SJeremy L Thompson   @ref Backend
11706e15d496SJeremy L Thompson **/
11719d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
11726e15d496SJeremy L Thompson   bool is_composite;
11731c66c397SJeremy L Thompson 
11742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11756e15d496SJeremy L Thompson 
11766e15d496SJeremy L Thompson   *flops = 0;
11772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11786e15d496SJeremy L Thompson   if (is_composite) {
11796e15d496SJeremy L Thompson     CeedInt num_suboperators;
11801c66c397SJeremy L Thompson 
1181c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11826e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1183c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11846e15d496SJeremy L Thompson 
11856e15d496SJeremy L Thompson     // FLOPs for each suboperator
11866e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11879d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11881c66c397SJeremy L Thompson 
11892b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11906e15d496SJeremy L Thompson       *flops += suboperator_flops;
11916e15d496SJeremy L Thompson     }
11926e15d496SJeremy L Thompson   } else {
11931c66c397SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
11946e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11951c66c397SJeremy L Thompson 
11962b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11972b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11984385fb7fSSebastian Grimberg 
11996e15d496SJeremy L Thompson     // Input FLOPs
12006e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
12016e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1202edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
12036e15d496SJeremy L Thompson 
1204edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1205edb2538eSJeremy L Thompson         *flops += rstr_flops;
12062b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
12076e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12086e15d496SJeremy L Thompson       }
12096e15d496SJeremy L Thompson     }
12106e15d496SJeremy L Thompson     // QF FLOPs
12111c66c397SJeremy L Thompson     {
12129d36ca50SJeremy L Thompson       CeedInt  num_qpts;
12139d36ca50SJeremy L Thompson       CeedSize qf_flops;
12141c66c397SJeremy L Thompson 
12152b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12162b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12176e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
12181c66c397SJeremy L Thompson     }
12191c66c397SJeremy L Thompson 
12206e15d496SJeremy L Thompson     // Output FLOPs
12216e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12226e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1223edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
12246e15d496SJeremy L Thompson 
1225edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1226edb2538eSJeremy L Thompson         *flops += rstr_flops;
12272b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12286e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12296e15d496SJeremy L Thompson       }
12306e15d496SJeremy L Thompson     }
12316e15d496SJeremy L Thompson   }
12326e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12336e15d496SJeremy L Thompson }
12346e15d496SJeremy L Thompson 
12356e15d496SJeremy L Thompson /**
12360126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12379fd66db6SSebastian Grimberg 
1238512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1239512bb800SJeremy L Thompson 
12409fd66db6SSebastian 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.
12419fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12420126412dSJeremy L Thompson 
1243859c15bbSJames Wright   @param[in]  op  CeedOperator
12440126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12450126412dSJeremy L Thompson 
12460126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12470126412dSJeremy L Thompson 
1248859c15bbSJames Wright   @ref Advanced
12490126412dSJeremy L Thompson **/
12500126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
12516574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12520126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12530126412dSJeremy L Thompson   else *ctx = NULL;
12540126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12550126412dSJeremy L Thompson }
12560126412dSJeremy L Thompson 
12570126412dSJeremy L Thompson /**
1258ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12593668ca4bSJeremy L Thompson 
1260859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1261859c15bbSJames Wright 
12623668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12633668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12643668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12653668ca4bSJeremy L Thompson 
12663668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12673668ca4bSJeremy L Thompson 
12683668ca4bSJeremy L Thompson   @ref User
12693668ca4bSJeremy L Thompson **/
127017b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12711c66c397SJeremy L Thompson   bool is_composite, field_found = false;
12721c66c397SJeremy L Thompson 
12732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12742b730f8bSJeremy L Thompson 
12753668ca4bSJeremy L Thompson   if (is_composite) {
1276a98a090bSJeremy L Thompson     // Composite operator
1277a98a090bSJeremy L Thompson     // -- Check if composite label already created
12783668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12793668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12803668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12813668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12823668ca4bSJeremy L Thompson       }
12833668ca4bSJeremy L Thompson     }
12843668ca4bSJeremy L Thompson 
1285a98a090bSJeremy L Thompson     // -- Create composite label if needed
12863668ca4bSJeremy L Thompson     CeedInt               num_sub;
12873668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12883668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12893668ca4bSJeremy L Thompson 
12902b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1291c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1292c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12932b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12943668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12953668ca4bSJeremy L Thompson 
12963668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12973668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12983668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12991c66c397SJeremy L Thompson 
13002b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
13013668ca4bSJeremy L Thompson         if (new_field_label_i) {
1302a98a090bSJeremy L Thompson           field_found                    = true;
13033668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
13043668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
13053668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
13062b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
13077bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13082b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13092b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
13102b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
13117bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13127bfe0f0eSJeremy L Thompson           } else {
13137bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
13147bfe0f0eSJeremy L Thompson           }
13152b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
13167bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13172b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13182b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13197bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13207bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13217bfe0f0eSJeremy L Thompson           } else {
13227bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13237bfe0f0eSJeremy L Thompson           }
13243668ca4bSJeremy L Thompson         }
13253668ca4bSJeremy L Thompson       }
13263668ca4bSJeremy L Thompson     }
1327a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1328a98a090bSJeremy L Thompson     if (field_found) {
1329a98a090bSJeremy L Thompson       *field_label = new_field_label;
1330a98a090bSJeremy L Thompson     } else {
13313668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13322b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13332b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13343668ca4bSJeremy L Thompson       *field_label = NULL;
13353668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13365ac9af79SJeremy L Thompson     }
13375ac9af79SJeremy L Thompson   } else {
1338a98a090bSJeremy L Thompson     // Single, non-composite operator
1339a98a090bSJeremy L Thompson     if (op->qf->ctx) {
13405ac9af79SJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1341a98a090bSJeremy L Thompson     } else {
1342a98a090bSJeremy L Thompson       *field_label = NULL;
1343a98a090bSJeremy L Thompson     }
13445ac9af79SJeremy L Thompson   }
13455ac9af79SJeremy L Thompson 
13465ac9af79SJeremy L Thompson   // Set label in operator
13475ac9af79SJeremy L Thompson   if (*field_label) {
13485ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13495ac9af79SJeremy L Thompson 
13503668ca4bSJeremy L Thompson     // Move new composite label to operator
13513668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13522b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13533668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13543668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13552b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13563668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13573668ca4bSJeremy L Thompson     }
13585ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13593668ca4bSJeremy L Thompson     op->num_context_labels++;
13603668ca4bSJeremy L Thompson   }
13613668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13623668ca4bSJeremy L Thompson }
13633668ca4bSJeremy L Thompson 
13643668ca4bSJeremy L Thompson /**
13652788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13664385fb7fSSebastian Grimberg 
13672788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1368d8dd9a91SJeremy L Thompson 
1369ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13702788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1371ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1372d8dd9a91SJeremy L Thompson 
1373d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1374d8dd9a91SJeremy L Thompson 
1375d8dd9a91SJeremy L Thompson   @ref User
1376d8dd9a91SJeremy L Thompson **/
137717b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13782b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1379d8dd9a91SJeremy L Thompson }
1380d8dd9a91SJeremy L Thompson 
1381d8dd9a91SJeremy L Thompson /**
13822788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13834385fb7fSSebastian Grimberg 
13842788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13852788fa27SJeremy L Thompson 
13862788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13872788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13882788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13892788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13902788fa27SJeremy L Thompson 
13912788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13922788fa27SJeremy L Thompson 
13932788fa27SJeremy L Thompson   @ref User
13942788fa27SJeremy L Thompson **/
139517b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13962788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13972788fa27SJeremy L Thompson }
13982788fa27SJeremy L Thompson 
13992788fa27SJeremy L Thompson /**
14002788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
14012788fa27SJeremy L Thompson 
14022788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14032788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
14042788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14052788fa27SJeremy L Thompson 
14062788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14072788fa27SJeremy L Thompson 
14082788fa27SJeremy L Thompson   @ref User
14092788fa27SJeremy L Thompson **/
141017b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
14112788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
14122788fa27SJeremy L Thompson }
14132788fa27SJeremy L Thompson 
14142788fa27SJeremy L Thompson /**
14152788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
14164385fb7fSSebastian Grimberg 
14172788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1418d8dd9a91SJeremy L Thompson 
1419ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1420ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1421ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1422d8dd9a91SJeremy L Thompson 
1423d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1424d8dd9a91SJeremy L Thompson 
1425d8dd9a91SJeremy L Thompson   @ref User
1426d8dd9a91SJeremy L Thompson **/
142717b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14282b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1429d8dd9a91SJeremy L Thompson }
1430d8dd9a91SJeremy L Thompson 
1431d8dd9a91SJeremy L Thompson /**
14322788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14334385fb7fSSebastian Grimberg 
14342788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14352788fa27SJeremy L Thompson 
14362788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14372788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1438c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14392788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14402788fa27SJeremy L Thompson 
14412788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14422788fa27SJeremy L Thompson 
14432788fa27SJeremy L Thompson   @ref User
14442788fa27SJeremy L Thompson **/
144517b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14462788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14472788fa27SJeremy L Thompson }
14482788fa27SJeremy L Thompson 
14492788fa27SJeremy L Thompson /**
14502788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14512788fa27SJeremy L Thompson 
14522788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14532788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14542788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14552788fa27SJeremy L Thompson 
14562788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14572788fa27SJeremy L Thompson 
14582788fa27SJeremy L Thompson   @ref User
14592788fa27SJeremy L Thompson **/
146017b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14612788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14622788fa27SJeremy L Thompson }
14632788fa27SJeremy L Thompson 
14642788fa27SJeremy L Thompson /**
14653bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1466d7b241e6Sjeremylt 
1467ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1468ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1469d7b241e6Sjeremylt 
1470ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1471f04ea552SJeremy L Thompson 
1472ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1473ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14745ac9af79SJeremy 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
14755ac9af79SJeremy L Thompson active outputs
1476ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1477b11c1e72Sjeremylt 
1478b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1479dfdf5a53Sjeremylt 
14807a982d89SJeremy L. Thompson   @ref User
1481b11c1e72Sjeremylt **/
14822b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1484d7b241e6Sjeremylt 
14853ca2b39bSJeremy L Thompson   if (op->is_composite) {
1486250756a7Sjeremylt     // Composite Operator
1487250756a7Sjeremylt     if (op->ApplyComposite) {
14882b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1489250756a7Sjeremylt     } else {
1490d1d35e2fSjeremylt       CeedInt       num_suboperators;
1491d1d35e2fSjeremylt       CeedOperator *sub_operators;
14921c66c397SJeremy L Thompson 
14931c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1494c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1495250756a7Sjeremylt 
1496250756a7Sjeremylt       // Zero all output vectors
14973ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1498d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1499d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1500d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
15011c66c397SJeremy L Thompson 
1502250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
15032b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1504250756a7Sjeremylt           }
1505250756a7Sjeremylt         }
1506250756a7Sjeremylt       }
1507250756a7Sjeremylt       // Apply
1508f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
1509f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1510cae8b89aSjeremylt       }
1511cae8b89aSjeremylt     }
15123ca2b39bSJeremy L Thompson   } else {
15133ca2b39bSJeremy L Thompson     // Standard Operator
15143ca2b39bSJeremy L Thompson     if (op->Apply) {
15153ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
15163ca2b39bSJeremy L Thompson     } else {
15173ca2b39bSJeremy L Thompson       // Zero all output vectors
15183ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
15191c66c397SJeremy L Thompson 
15203ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
15213ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
15221c66c397SJeremy L Thompson 
15233ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
15243ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
15253ca2b39bSJeremy L Thompson       }
15263ca2b39bSJeremy L Thompson       // Apply
15273ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15283ca2b39bSJeremy L Thompson     }
1529250756a7Sjeremylt   }
1530e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1531cae8b89aSjeremylt }
1532cae8b89aSjeremylt 
1533cae8b89aSjeremylt /**
1534cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1535cae8b89aSjeremylt 
1536ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1537ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1538cae8b89aSjeremylt 
1539ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1540ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1541ea61e9acSJeremy 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
1542ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1543cae8b89aSjeremylt 
1544cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1545cae8b89aSjeremylt 
15467a982d89SJeremy L. Thompson   @ref User
1547cae8b89aSjeremylt **/
15482b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1550cae8b89aSjeremylt 
15513ca2b39bSJeremy L Thompson   if (op->is_composite) {
1552250756a7Sjeremylt     // Composite Operator
1553250756a7Sjeremylt     if (op->ApplyAddComposite) {
15542b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1555cae8b89aSjeremylt     } else {
1556d1d35e2fSjeremylt       CeedInt       num_suboperators;
1557d1d35e2fSjeremylt       CeedOperator *sub_operators;
1558250756a7Sjeremylt 
15591c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15601c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1561d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15622b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15631d7d2407SJeremy L Thompson       }
1564250756a7Sjeremylt     }
15653ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
15663ca2b39bSJeremy L Thompson     // Standard Operator
15673ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1568250756a7Sjeremylt   }
1569e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1570d7b241e6Sjeremylt }
1571d7b241e6Sjeremylt 
1572d7b241e6Sjeremylt /**
1573b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1574d7b241e6Sjeremylt 
1575ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1576b11c1e72Sjeremylt 
1577b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1578dfdf5a53Sjeremylt 
15797a982d89SJeremy L. Thompson   @ref User
1580b11c1e72Sjeremylt **/
1581d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1582ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1583ad6481ceSJeremy L Thompson     *op = NULL;
1584ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1585ad6481ceSJeremy L Thompson   }
15862b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15872b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1588fe2413ffSjeremylt   // Free fields
15892b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1590d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1591437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1592437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
159315910d16Sjeremylt       }
1594356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
15952b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
159671352a93Sjeremylt       }
15972b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15982b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
159971352a93Sjeremylt       }
16002b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
16012b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1602fe2413ffSjeremylt     }
16032b730f8bSJeremy L Thompson   }
16042b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1605d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1606437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1607356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
16082b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
160971352a93Sjeremylt       }
16102b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
16112b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
161271352a93Sjeremylt       }
16132b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
16142b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
16152b730f8bSJeremy L Thompson     }
1616fe2413ffSjeremylt   }
1617d1d35e2fSjeremylt   // Destroy sub_operators
16182b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1619d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
16202b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
162152d6035fSJeremy L Thompson     }
16222b730f8bSJeremy L Thompson   }
16232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
16242b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
16252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16263668ca4bSJeremy L Thompson   // Destroy any composite labels
16275ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16283668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16292b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16302b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16313668ca4bSJeremy L Thompson     }
16325ac9af79SJeremy L Thompson   }
16332b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1634fe2413ffSjeremylt 
16355107b09fSJeremy L Thompson   // Destroy fallback
16362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16375107b09fSJeremy L Thompson 
1638ed9e99e6SJeremy L Thompson   // Destroy assembly data
16392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
164170a7ffb3SJeremy L Thompson 
16422b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16432b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16462b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1647e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1648d7b241e6Sjeremylt }
1649d7b241e6Sjeremylt 
1650d7b241e6Sjeremylt /// @}
1651