xref: /libCEED/interface/ceed-operator.c (revision 6574a04ff2135c3834f1b6ef9a4ec7566c4782db)
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) {
37d1d35e2fSjeremylt   CeedEvalMode eval_mode = qf_field->eval_mode;
38c4e3f59bSSebastian Grimberg   CeedInt      dim = 1, num_comp = 1, q_comp = 1, restr_num_comp = 1, size = qf_field->size;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
41*6574a04fSJeremy L Thompson   CeedCheck((r == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
42*6574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
442b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
47*6574a04fSJeremy L Thompson   CeedCheck((b == CEED_BASIS_COLLOCATED) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
48*6574a04fSJeremy L Thompson             "CEED_BASIS_COLLOCATED and CEED_EVAL_NONE must be used together.");
49e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
502b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
52c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp));
53*6574a04fSJeremy L Thompson     CeedCheck(r == CEED_ELEMRESTRICTION_NONE || restr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
54*6574a04fSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components, but Basis has %" CeedInt_FMT
55*6574a04fSJeremy L Thompson               " components",
562b730f8bSJeremy L Thompson               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp);
57e15f9bd0SJeremy L Thompson   }
58e15f9bd0SJeremy L Thompson   // Field size
59d1d35e2fSjeremylt   switch (eval_mode) {
60e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
61*6574a04fSJeremy L Thompson       CeedCheck(size == restr_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,
632b730f8bSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_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:
69*6574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
70*6574a04fSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components", qf_field->field_name,
71*6574a04fSJeremy 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);
1037a982d89SJeremy L. Thompson 
104990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
105f5ebfb90SJeremy L Thompson 
1062b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
107f5ebfb90SJeremy L Thompson 
1082b730f8bSJeremy L Thompson   if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s      Collocated basis\n", pre);
1097a982d89SJeremy L. Thompson 
1102b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1112b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
112f5ebfb90SJeremy L Thompson 
113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1147a982d89SJeremy L. Thompson }
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /**
1177a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1187a982d89SJeremy L. Thompson 
1197a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1207a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1217a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
1277a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1297a982d89SJeremy L. Thompson 
130381e6593SJeremy L Thompson   CeedInt num_elem, num_qpts;
1312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
133381e6593SJeremy L Thompson 
13478464608Sjeremylt   CeedInt total_fields = 0;
1352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1362b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1377a982d89SJeremy L. Thompson 
1382b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1397a982d89SJeremy L. Thompson 
1402b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
141d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1437a982d89SJeremy L. Thompson   }
1447a982d89SJeremy L. Thompson 
1452b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
146d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1487a982d89SJeremy L. Thompson   }
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1507a982d89SJeremy L. Thompson }
1517a982d89SJeremy L. Thompson 
152d99fa3c5SJeremy L Thompson /**
1530f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
154eaf62fffSJeremy L Thompson 
155eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
1560f60e0a8SJeremy L Thompson   @param[out] active_basis Basis for active input vector or NULL for composite operator
157eaf62fffSJeremy L Thompson 
158eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
159eaf62fffSJeremy L Thompson 
160eaf62fffSJeremy L Thompson   @ref Developer
161eaf62fffSJeremy L Thompson **/
162eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
1636aaed0edSJeremy L Thompson   Ceed ceed;
1646aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1656aaed0edSJeremy L Thompson 
166eaf62fffSJeremy L Thompson   *active_basis = NULL;
1670f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1682b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
169eaf62fffSJeremy L Thompson     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
170*6574a04fSJeremy L Thompson       CeedCheck(!*active_basis || *active_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found");
171eaf62fffSJeremy L Thompson       *active_basis = op->input_fields[i]->basis;
172eaf62fffSJeremy L Thompson     }
1732b730f8bSJeremy L Thompson   }
174eaf62fffSJeremy L Thompson 
175*6574a04fSJeremy L Thompson   CeedCheck(*active_basis, ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found");
176eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
177eaf62fffSJeremy L Thompson }
178eaf62fffSJeremy L Thompson 
179eaf62fffSJeremy L Thompson /**
1800f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
181e2f04181SAndrew T. Barker 
182e2f04181SAndrew T. Barker   @param[in] op           CeedOperator to find active basis for
1830f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator
184e2f04181SAndrew T. Barker 
185e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
186e2f04181SAndrew T. Barker 
187e2f04181SAndrew T. Barker   @ref Utility
188e2f04181SAndrew T. Barker **/
1892b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
1906aaed0edSJeremy L Thompson   Ceed ceed;
1916aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1926aaed0edSJeremy L Thompson 
193d1d35e2fSjeremylt   *active_rstr = NULL;
1940f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1952b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
196d1d35e2fSjeremylt     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
197*6574a04fSJeremy L Thompson       CeedCheck(!*active_rstr || *active_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
198*6574a04fSJeremy L Thompson                 "Multiple active CeedElemRestrictions found");
199437c7c90SJeremy L Thompson       *active_rstr = op->input_fields[i]->elem_rstr;
200e2f04181SAndrew T. Barker     }
2012b730f8bSJeremy L Thompson   }
202e2f04181SAndrew T. Barker 
203*6574a04fSJeremy L Thompson   CeedCheck(*active_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found");
204e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
205e2f04181SAndrew T. Barker }
206e2f04181SAndrew T. Barker 
207d8dd9a91SJeremy L Thompson /**
2082788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
209ea61e9acSJeremy L Thompson            For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
210ea61e9acSJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do
211ea61e9acSJeremy L Thompson not have any field of a matching type.
212d8dd9a91SJeremy L Thompson 
213ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
214ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
215ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2162788fa27SJeremy L Thompson   @param[in]     values      Values to set
217d8dd9a91SJeremy L Thompson 
218d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
219d8dd9a91SJeremy L Thompson 
220d8dd9a91SJeremy L Thompson   @ref User
221d8dd9a91SJeremy L Thompson **/
2222788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
223*6574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2243668ca4bSJeremy L Thompson 
2255ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2265ac9af79SJeremy L Thompson   if (field_label->from_op) {
2275ac9af79SJeremy L Thompson     CeedInt index = -1;
2285ac9af79SJeremy L Thompson 
2295ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2305ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2315ac9af79SJeremy L Thompson     }
232*6574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2335ac9af79SJeremy L Thompson   }
2345ac9af79SJeremy L Thompson 
2353668ca4bSJeremy L Thompson   bool is_composite = false;
2362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
237d8dd9a91SJeremy L Thompson   if (is_composite) {
238d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
239d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
240d8dd9a91SJeremy L Thompson 
241c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
242c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
243*6574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
244*6574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
245d8dd9a91SJeremy L Thompson 
246d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
247d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
2483668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
2492788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
250d8dd9a91SJeremy L Thompson       }
251d8dd9a91SJeremy L Thompson     }
252d8dd9a91SJeremy L Thompson   } else {
253*6574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
2542788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
2552788fa27SJeremy L Thompson   }
2562788fa27SJeremy L Thompson 
2572788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2582788fa27SJeremy L Thompson }
2592788fa27SJeremy L Thompson 
2602788fa27SJeremy L Thompson /**
2612788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
2622788fa27SJeremy L Thompson            For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
2635ac9af79SJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that
2645ac9af79SJeremy L Thompson            do not have any field of a matching type.
2652788fa27SJeremy L Thompson 
2662788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
2672788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
2682788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
269c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
270c5d0f995SJed Brown   @param[out]    values      Values in the label
2712788fa27SJeremy L Thompson 
2722788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2732788fa27SJeremy L Thompson 
2742788fa27SJeremy L Thompson   @ref User
2752788fa27SJeremy L Thompson **/
2762788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
2772788fa27SJeremy L Thompson                                              void *values) {
278*6574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2792788fa27SJeremy L Thompson 
2802788fa27SJeremy L Thompson   *(void **)values = NULL;
2812788fa27SJeremy L Thompson   *num_values      = 0;
2822788fa27SJeremy L Thompson 
2835ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2845ac9af79SJeremy L Thompson   if (field_label->from_op) {
2855ac9af79SJeremy L Thompson     CeedInt index = -1;
2865ac9af79SJeremy L Thompson 
2875ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2885ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2895ac9af79SJeremy L Thompson     }
290*6574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2915ac9af79SJeremy L Thompson   }
2925ac9af79SJeremy L Thompson 
2932788fa27SJeremy L Thompson   bool is_composite = false;
2942788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2952788fa27SJeremy L Thompson   if (is_composite) {
2962788fa27SJeremy L Thompson     CeedInt       num_sub;
2972788fa27SJeremy L Thompson     CeedOperator *sub_operators;
2982788fa27SJeremy L Thompson 
2992788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3002788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
301*6574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
302*6574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3032788fa27SJeremy L Thompson 
3042788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3052788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3062788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3072788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3082788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3092788fa27SJeremy L Thompson       }
3102788fa27SJeremy L Thompson     }
3112788fa27SJeremy L Thompson   } else {
312*6574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3132788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3142788fa27SJeremy L Thompson   }
3152788fa27SJeremy L Thompson 
3162788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3172788fa27SJeremy L Thompson }
3182788fa27SJeremy L Thompson 
3192788fa27SJeremy L Thompson /**
3202788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
3212788fa27SJeremy L Thompson            For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
3225ac9af79SJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators
3235ac9af79SJeremy L Thompson            that do not have any field of a matching type.
3242788fa27SJeremy L Thompson 
3252788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3262788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3272788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
328c5d0f995SJed Brown   @param[in]     values      Values array to restore
3292788fa27SJeremy L Thompson 
3302788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3312788fa27SJeremy L Thompson 
3322788fa27SJeremy L Thompson   @ref User
3332788fa27SJeremy L Thompson **/
3342788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
335*6574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3362788fa27SJeremy L Thompson 
3375ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3385ac9af79SJeremy L Thompson   if (field_label->from_op) {
3395ac9af79SJeremy L Thompson     CeedInt index = -1;
3405ac9af79SJeremy L Thompson 
3415ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3425ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3435ac9af79SJeremy L Thompson     }
344*6574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3455ac9af79SJeremy L Thompson   }
3465ac9af79SJeremy L Thompson 
3472788fa27SJeremy L Thompson   bool is_composite = false;
3482788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3492788fa27SJeremy L Thompson   if (is_composite) {
3502788fa27SJeremy L Thompson     CeedInt       num_sub;
3512788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3522788fa27SJeremy L Thompson 
3532788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3542788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
355*6574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
356*6574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3572788fa27SJeremy L Thompson 
3582788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3592788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3602788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3612788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
3622788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3632788fa27SJeremy L Thompson       }
3642788fa27SJeremy L Thompson     }
3652788fa27SJeremy L Thompson   } else {
366*6574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3672788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
368d8dd9a91SJeremy L Thompson   }
369d8dd9a91SJeremy L Thompson 
370d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
371d8dd9a91SJeremy L Thompson }
372d8dd9a91SJeremy L Thompson 
3737a982d89SJeremy L. Thompson /// @}
3747a982d89SJeremy L. Thompson 
3757a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3767a982d89SJeremy L. Thompson /// CeedOperator Backend API
3777a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3787a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
3797a982d89SJeremy L. Thompson /// @{
3807a982d89SJeremy L. Thompson 
3817a982d89SJeremy L. Thompson /**
3827a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
3837a982d89SJeremy L. Thompson 
384ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
385d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
3867a982d89SJeremy L. Thompson 
3877a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3887a982d89SJeremy L. Thompson 
3897a982d89SJeremy L. Thompson   @ref Backend
3907a982d89SJeremy L. Thompson **/
3917a982d89SJeremy L. Thompson 
392d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
393*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
394d1d35e2fSjeremylt   *num_args = op->num_fields;
395e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3967a982d89SJeremy L. Thompson }
3977a982d89SJeremy L. Thompson 
3987a982d89SJeremy L. Thompson /**
3997a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4007a982d89SJeremy L. Thompson 
401ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
402d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4037a982d89SJeremy L. Thompson 
4047a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4057a982d89SJeremy L. Thompson 
4067a982d89SJeremy L. Thompson   @ref Backend
4077a982d89SJeremy L. Thompson **/
4087a982d89SJeremy L. Thompson 
409d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
410f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
411e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4127a982d89SJeremy L. Thompson }
4137a982d89SJeremy L. Thompson 
4147a982d89SJeremy L. Thompson /**
4157a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4167a982d89SJeremy L. Thompson 
417ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4187a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4197a982d89SJeremy L. Thompson 
4207a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4217a982d89SJeremy L. Thompson 
4227a982d89SJeremy L. Thompson   @ref Backend
4237a982d89SJeremy L. Thompson **/
4247a982d89SJeremy L. Thompson 
4257a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
426*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
4277a982d89SJeremy L. Thompson   *qf = op->qf;
428e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4297a982d89SJeremy L. Thompson }
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson /**
432c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
433c04a41a7SJeremy L Thompson 
434ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
435d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
436c04a41a7SJeremy L Thompson 
437c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
438c04a41a7SJeremy L Thompson 
439c04a41a7SJeremy L Thompson   @ref Backend
440c04a41a7SJeremy L Thompson **/
441c04a41a7SJeremy L Thompson 
442d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
443f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
444e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
445c04a41a7SJeremy L Thompson }
446c04a41a7SJeremy L Thompson 
447c04a41a7SJeremy L Thompson /**
4487a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
4497a982d89SJeremy L. Thompson 
450ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
4517a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4547a982d89SJeremy L. Thompson 
4557a982d89SJeremy L. Thompson   @ref Backend
4567a982d89SJeremy L. Thompson **/
4577a982d89SJeremy L. Thompson 
458777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
459777ff853SJeremy L Thompson   *(void **)data = op->data;
460e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4617a982d89SJeremy L. Thompson }
4627a982d89SJeremy L. Thompson 
4637a982d89SJeremy L. Thompson /**
4647a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
4657a982d89SJeremy L. Thompson 
466ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
467ea61e9acSJeremy L Thompson   @param[in]     data Data to set
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4707a982d89SJeremy L. Thompson 
4717a982d89SJeremy L. Thompson   @ref Backend
4727a982d89SJeremy L. Thompson **/
4737a982d89SJeremy L. Thompson 
474777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
475777ff853SJeremy L Thompson   op->data = data;
476e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4777a982d89SJeremy L. Thompson }
4787a982d89SJeremy L. Thompson 
4797a982d89SJeremy L. Thompson /**
48034359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
48134359f16Sjeremylt 
482ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
48334359f16Sjeremylt 
48434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
48534359f16Sjeremylt 
48634359f16Sjeremylt   @ref Backend
48734359f16Sjeremylt **/
4889560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
48934359f16Sjeremylt   op->ref_count++;
49034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
49134359f16Sjeremylt }
49234359f16Sjeremylt 
49334359f16Sjeremylt /**
4947a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
4957a982d89SJeremy L. Thompson 
496ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
4977a982d89SJeremy L. Thompson 
4987a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4997a982d89SJeremy L. Thompson 
5007a982d89SJeremy L. Thompson   @ref Backend
5017a982d89SJeremy L. Thompson **/
5027a982d89SJeremy L. Thompson 
5037a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
504f04ea552SJeremy L Thompson   op->is_backend_setup = true;
505e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5067a982d89SJeremy L. Thompson }
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson /// @}
5097a982d89SJeremy L. Thompson 
5107a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5117a982d89SJeremy L. Thompson /// CeedOperator Public API
5127a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5137a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
514dfdf5a53Sjeremylt /// @{
515d7b241e6Sjeremylt 
516d7b241e6Sjeremylt /**
517ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
518ea61e9acSJeremy L Thompson            A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
519d7b241e6Sjeremylt 
520ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
521ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
522ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
523ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
524ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
525b11c1e72Sjeremylt 
526b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
527dfdf5a53Sjeremylt 
5287a982d89SJeremy L. Thompson   @ref User
529d7b241e6Sjeremylt  */
5302b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5315fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5325fe0d4faSjeremylt     Ceed delegate;
533*6574a04fSJeremy L Thompson 
5342b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
535*6574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
5362b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
537e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5385fe0d4faSjeremylt   }
5395fe0d4faSjeremylt 
540*6574a04fSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
5412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
542d7b241e6Sjeremylt   (*op)->ceed = ceed;
5432b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
544d1d35e2fSjeremylt   (*op)->ref_count   = 1;
545d7b241e6Sjeremylt   (*op)->qf          = qf;
5462b104005SJeremy L Thompson   (*op)->input_size  = -1;
5472b104005SJeremy L Thompson   (*op)->output_size = -1;
5482b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
549442e7f0bSjeremylt   if (dqf && dqf != CEED_QFUNCTION_NONE) {
550d7b241e6Sjeremylt     (*op)->dqf = dqf;
5512b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqf));
552442e7f0bSjeremylt   }
553442e7f0bSjeremylt   if (dqfT && dqfT != CEED_QFUNCTION_NONE) {
554d7b241e6Sjeremylt     (*op)->dqfT = dqfT;
5552b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqfT));
556442e7f0bSjeremylt   }
5572b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
5582b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
5592b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
5602b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
561e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
562d7b241e6Sjeremylt }
563d7b241e6Sjeremylt 
564d7b241e6Sjeremylt /**
56552d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
56652d6035fSJeremy L Thompson 
567ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
568ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
56952d6035fSJeremy L Thompson 
57052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
57152d6035fSJeremy L Thompson 
5727a982d89SJeremy L. Thompson   @ref User
57352d6035fSJeremy L Thompson  */
57452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
57552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
57652d6035fSJeremy L Thompson     Ceed delegate;
5772b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
57852d6035fSJeremy L Thompson 
579250756a7Sjeremylt     if (delegate) {
5802b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
581e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
58252d6035fSJeremy L Thompson     }
583250756a7Sjeremylt   }
58452d6035fSJeremy L Thompson 
5852b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
58652d6035fSJeremy L Thompson   (*op)->ceed = ceed;
5872b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
588996d9ab5SJed Brown   (*op)->ref_count    = 1;
589f04ea552SJeremy L Thompson   (*op)->is_composite = true;
5902b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
5912b104005SJeremy L Thompson   (*op)->input_size  = -1;
5922b104005SJeremy L Thompson   (*op)->output_size = -1;
593250756a7Sjeremylt 
594250756a7Sjeremylt   if (ceed->CompositeOperatorCreate) {
5952b730f8bSJeremy L Thompson     CeedCall(ceed->CompositeOperatorCreate(*op));
596250756a7Sjeremylt   }
597e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
59852d6035fSJeremy L Thompson }
59952d6035fSJeremy L Thompson 
60052d6035fSJeremy L Thompson /**
601ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
602ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedOperatorDestroy()`.
603512bb800SJeremy L Thompson 
604512bb800SJeremy 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.
605512bb800SJeremy L Thompson              This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
6069560d06aSjeremylt 
607ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
608ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
6099560d06aSjeremylt 
6109560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6119560d06aSjeremylt 
6129560d06aSjeremylt   @ref User
6139560d06aSjeremylt **/
6149560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
6152b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
6162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
6179560d06aSjeremylt   *op_copy = op;
6189560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6199560d06aSjeremylt }
6209560d06aSjeremylt 
6219560d06aSjeremylt /**
622ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
623d7b241e6Sjeremylt 
624ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
625ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
626ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
627d7b241e6Sjeremylt 
628ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
629ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
630d7b241e6Sjeremylt 
631ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
632ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
633ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
634ea61e9acSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points
6355ac9af79SJeremy 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
6365ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
637b11c1e72Sjeremylt 
638b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
639dfdf5a53Sjeremylt 
6407a982d89SJeremy L. Thompson   @ref User
641b11c1e72Sjeremylt **/
6422b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
643*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
644*6574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
645*6574a04fSJeremy L Thompson   CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
646*6574a04fSJeremy L Thompson   CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
647*6574a04fSJeremy L Thompson   CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
64852d6035fSJeremy L Thompson 
649*6574a04fSJeremy L Thompson   CeedInt num_elem = 0;
6502b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
651*6574a04fSJeremy L Thompson   CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
6522b730f8bSJeremy L Thompson             "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
653d7b241e6Sjeremylt 
65478464608Sjeremylt   CeedInt num_qpts = 0;
6552b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
656*6574a04fSJeremy L Thompson   CeedCheck(b == CEED_BASIS_COLLOCATED || !op->num_qpts || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
6572b730f8bSJeremy L Thompson             "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts);
658d1d35e2fSjeremylt   CeedQFunctionField qf_field;
659d1d35e2fSjeremylt   CeedOperatorField *op_field;
6602b104005SJeremy L Thompson   bool               is_input = true;
661d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
662d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
663d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
664d1d35e2fSjeremylt       op_field = &op->input_fields[i];
665d7b241e6Sjeremylt       goto found;
666d7b241e6Sjeremylt     }
667d7b241e6Sjeremylt   }
6682b104005SJeremy L Thompson   is_input = false;
669d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
670d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
671d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
672d1d35e2fSjeremylt       op_field = &op->output_fields[i];
673d7b241e6Sjeremylt       goto found;
674d7b241e6Sjeremylt     }
675d7b241e6Sjeremylt   }
676c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6772b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
678c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
679d7b241e6Sjeremylt found:
6802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6812b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
682e15f9bd0SJeremy L Thompson 
6832b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6842b104005SJeremy L Thompson     CeedSize l_size;
6852b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6862b104005SJeremy L Thompson     if (is_input) {
6872b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
688*6574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
689*6574a04fSJeremy L Thompson                 op->input_size);
6902b104005SJeremy L Thompson     } else {
6912b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
692*6574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
693*6574a04fSJeremy L Thompson                 op->output_size);
6942b104005SJeremy L Thompson     }
6952b730f8bSJeremy L Thompson   }
6962b104005SJeremy L Thompson 
697393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
698393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
699e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
700d1d35e2fSjeremylt     op->num_elem        = num_elem;
701d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
702e15f9bd0SJeremy L Thompson   }
703393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
704393ac2cdSJeremy L Thompson   if (!op->num_qpts && b != CEED_BASIS_COLLOCATED) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
705e15f9bd0SJeremy L Thompson 
706d1d35e2fSjeremylt   op->num_fields += 1;
7072b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
708e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
709d7b241e6Sjeremylt }
710d7b241e6Sjeremylt 
711d7b241e6Sjeremylt /**
71243bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
71343bbe138SJeremy L Thompson 
714ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
715f04ea552SJeremy L Thompson 
716ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
717f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
71843bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
719f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
72043bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
72143bbe138SJeremy L Thompson 
72243bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
72343bbe138SJeremy L Thompson 
724e9b533fbSJeremy L Thompson   @ref Advanced
72543bbe138SJeremy L Thompson **/
7262b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
72743bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
728*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7292b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
73043bbe138SJeremy L Thompson 
73143bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
73243bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
73343bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
73443bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
73543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73643bbe138SJeremy L Thompson }
73743bbe138SJeremy L Thompson 
73843bbe138SJeremy L Thompson /**
739de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
740de5900adSJames Wright 
741de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
742de5900adSJames Wright 
743de5900adSJames Wright   @param[in]  op         CeedOperator
744de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
745de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
746de5900adSJames Wright 
747de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
748de5900adSJames Wright 
749de5900adSJames Wright   @ref Advanced
750de5900adSJames Wright **/
751de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
752de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
753de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
754de5900adSJames Wright   char              *name;
755de5900adSJames Wright   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
756de5900adSJames Wright 
757de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
758de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
759de5900adSJames Wright     if (!strcmp(name, field_name)) {
760de5900adSJames Wright       *op_field = input_fields[i];
761de5900adSJames Wright       return CEED_ERROR_SUCCESS;
762de5900adSJames Wright     }
763de5900adSJames Wright   }
764de5900adSJames Wright 
765de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
766de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
767de5900adSJames Wright     if (!strcmp(name, field_name)) {
768de5900adSJames Wright       *op_field = output_fields[i];
769de5900adSJames Wright       return CEED_ERROR_SUCCESS;
770de5900adSJames Wright     }
771de5900adSJames Wright   }
772de5900adSJames Wright 
773de5900adSJames Wright   bool has_name = op->name;
774de5900adSJames Wright   // LCOV_EXCL_START
775de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
776de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
777de5900adSJames Wright   // LCOV_EXCL_STOP
778de5900adSJames Wright }
779de5900adSJames Wright 
780de5900adSJames Wright /**
78128567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
78228567f8fSJeremy L Thompson 
783ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
78428567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
78528567f8fSJeremy L Thompson 
78628567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78728567f8fSJeremy L Thompson 
788e9b533fbSJeremy L Thompson   @ref Advanced
78928567f8fSJeremy L Thompson **/
79028567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
79128567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
79228567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
79328567f8fSJeremy L Thompson }
79428567f8fSJeremy L Thompson 
79528567f8fSJeremy L Thompson /**
79643bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
79743bbe138SJeremy L Thompson 
798ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
79943bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
80043bbe138SJeremy L Thompson 
80143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80243bbe138SJeremy L Thompson 
803e9b533fbSJeremy L Thompson   @ref Advanced
80443bbe138SJeremy L Thompson **/
8052b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
806437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
80743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80843bbe138SJeremy L Thompson }
80943bbe138SJeremy L Thompson 
81043bbe138SJeremy L Thompson /**
81143bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
81243bbe138SJeremy L Thompson 
813ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
81443bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
81543bbe138SJeremy L Thompson 
81643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81743bbe138SJeremy L Thompson 
818e9b533fbSJeremy L Thompson   @ref Advanced
81943bbe138SJeremy L Thompson **/
82043bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
82143bbe138SJeremy L Thompson   *basis = op_field->basis;
82243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82343bbe138SJeremy L Thompson }
82443bbe138SJeremy L Thompson 
82543bbe138SJeremy L Thompson /**
82643bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
82743bbe138SJeremy L Thompson 
828ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
82943bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
83043bbe138SJeremy L Thompson 
83143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83243bbe138SJeremy L Thompson 
833e9b533fbSJeremy L Thompson   @ref Advanced
83443bbe138SJeremy L Thompson **/
83543bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
83643bbe138SJeremy L Thompson   *vec = op_field->vec;
83743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83843bbe138SJeremy L Thompson }
83943bbe138SJeremy L Thompson 
84043bbe138SJeremy L Thompson /**
84152d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
842288c0443SJeremy L Thompson 
843ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
844ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
84552d6035fSJeremy L Thompson 
84652d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84752d6035fSJeremy L Thompson 
8487a982d89SJeremy L. Thompson   @ref User
84952d6035fSJeremy L Thompson  */
8502b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
851*6574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
852*6574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
853*6574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8542b730f8bSJeremy L Thompson 
8552b730f8bSJeremy L Thompson   {
8562b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8572b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8582b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8592b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8602b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
861*6574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
862*6574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8632b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8642b730f8bSJeremy L Thompson               "shape (%td, %td)",
8652b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8662b730f8bSJeremy L Thompson   }
8672b730f8bSJeremy L Thompson 
868d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
870d1d35e2fSjeremylt   composite_op->num_suboperators++;
871e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87252d6035fSJeremy L Thompson }
87352d6035fSJeremy L Thompson 
87452d6035fSJeremy L Thompson /**
87575f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
87675f0d5a4SJeremy L Thompson 
87775f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
87875f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
87975f0d5a4SJeremy L Thompson 
88075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88175f0d5a4SJeremy L Thompson 
88275f0d5a4SJeremy L Thompson   @ref Backend
88375f0d5a4SJeremy L Thompson **/
88475f0d5a4SJeremy L Thompson 
885c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
886*6574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
88775f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
88875f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88975f0d5a4SJeremy L Thompson }
89075f0d5a4SJeremy L Thompson 
89175f0d5a4SJeremy L Thompson /**
89275f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
89375f0d5a4SJeremy L Thompson 
89475f0d5a4SJeremy L Thompson   @param op                  CeedOperator
89575f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
89675f0d5a4SJeremy L Thompson 
89775f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89875f0d5a4SJeremy L Thompson 
89975f0d5a4SJeremy L Thompson   @ref Backend
90075f0d5a4SJeremy L Thompson **/
90175f0d5a4SJeremy L Thompson 
902c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
903*6574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
90475f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
90575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90675f0d5a4SJeremy L Thompson }
90775f0d5a4SJeremy L Thompson 
90875f0d5a4SJeremy L Thompson /**
9094db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
9104db537f9SJeremy L Thompson 
9114db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
9124db537f9SJeremy L Thompson 
9134db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9144db537f9SJeremy L Thompson 
9154db537f9SJeremy L Thompson   @ref User
9164db537f9SJeremy L Thompson **/
9174db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9184db537f9SJeremy L Thompson   Ceed ceed;
9192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9204db537f9SJeremy L Thompson 
9212b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9224db537f9SJeremy L Thompson 
9234db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9244db537f9SJeremy L Thompson   if (op->is_composite) {
92543622462SJeremy L Thompson     if (!op->num_suboperators) {
92643622462SJeremy L Thompson       // Empty operator setup
92743622462SJeremy L Thompson       op->input_size  = 0;
92843622462SJeremy L Thompson       op->output_size = 0;
92943622462SJeremy L Thompson     } else {
9304db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9312b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9324db537f9SJeremy L Thompson       }
9332b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9342b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9352b104005SJeremy L Thompson       CeedSize input_size, output_size;
9362b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
93743622462SJeremy L Thompson     }
9384db537f9SJeremy L Thompson   } else {
939*6574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
940*6574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
941*6574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
942*6574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
943*6574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9442b730f8bSJeremy L Thompson   }
9454db537f9SJeremy L Thompson 
9464db537f9SJeremy L Thompson   // Flag as immutable and ready
9474db537f9SJeremy L Thompson   op->is_interface_setup = true;
9482b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9492b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9502b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9514db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9524db537f9SJeremy L Thompson }
9534db537f9SJeremy L Thompson 
9544db537f9SJeremy L Thompson /**
955c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
956ea61e9acSJeremy L Thompson            Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
957c9366a6bSJeremy L Thompson 
958c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
959c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
960c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
961c9366a6bSJeremy L Thompson 
962c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
963c9366a6bSJeremy L Thompson 
964c9366a6bSJeremy L Thompson   @ref User
965c9366a6bSJeremy L Thompson **/
9662b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
967c9366a6bSJeremy L Thompson   bool is_composite;
968c9366a6bSJeremy L Thompson 
9692b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9702b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
971c9366a6bSJeremy L Thompson 
9722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9732b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
974c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
975c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9762b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9772b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9782b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9792b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
980*6574a04fSJeremy 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,
981*6574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9822b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9832b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9842b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
985c9366a6bSJeremy L Thompson     }
9862b730f8bSJeremy L Thompson   }
987c9366a6bSJeremy L Thompson 
988c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
989c9366a6bSJeremy L Thompson }
990c9366a6bSJeremy L Thompson 
991c9366a6bSJeremy L Thompson /**
992beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
993ea61e9acSJeremy L Thompson            When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
994ea61e9acSJeremy L Thompson `CeedOperatorLinearAssemble*` function is called. When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused
995ea61e9acSJeremy L Thompson between calls to `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9968b919e6bSJeremy L Thompson 
997beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
998beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9998b919e6bSJeremy L Thompson 
10008b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10018b919e6bSJeremy L Thompson 
10028b919e6bSJeremy L Thompson   @ref Advanced
10038b919e6bSJeremy L Thompson **/
10042b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
10058b919e6bSJeremy L Thompson   bool is_composite;
10068b919e6bSJeremy L Thompson 
10072b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10088b919e6bSJeremy L Thompson   if (is_composite) {
10098b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10118b919e6bSJeremy L Thompson     }
10128b919e6bSJeremy L Thompson   } else {
10132b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1014beecbf24SJeremy L Thompson   }
1015beecbf24SJeremy L Thompson 
1016beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1017beecbf24SJeremy L Thompson }
1018beecbf24SJeremy L Thompson 
1019beecbf24SJeremy L Thompson /**
1020beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1021beecbf24SJeremy L Thompson 
1022beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10236e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1024beecbf24SJeremy L Thompson 
1025beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1026beecbf24SJeremy L Thompson 
1027beecbf24SJeremy L Thompson   @ref Advanced
1028beecbf24SJeremy L Thompson **/
10292b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1030beecbf24SJeremy L Thompson   bool is_composite;
1031beecbf24SJeremy L Thompson 
10322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1033beecbf24SJeremy L Thompson   if (is_composite) {
1034beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10352b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1036beecbf24SJeremy L Thompson     }
1037beecbf24SJeremy L Thompson   } else {
10382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10398b919e6bSJeremy L Thompson   }
10408b919e6bSJeremy L Thompson 
10418b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10428b919e6bSJeremy L Thompson }
10438b919e6bSJeremy L Thompson 
10448b919e6bSJeremy L Thompson /**
1045cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
1046ea61e9acSJeremy L Thompson            This should be used when creating a CeedOperator where every field has a collocated basis.
1047ea61e9acSJeremy L Thompson            This function cannot be used for composite CeedOperators.
1048cd4dfc48Sjeremylt 
1049ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1050ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1051cd4dfc48Sjeremylt 
1052cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1053cd4dfc48Sjeremylt 
1054e9b533fbSJeremy L Thompson   @ref Advanced
1055cd4dfc48Sjeremylt **/
1056cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
1057*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1058*6574a04fSJeremy L Thompson   CeedCheck(op->num_qpts == 0, op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
1059*6574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1060cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1061cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1062cd4dfc48Sjeremylt }
1063cd4dfc48Sjeremylt 
1064cd4dfc48Sjeremylt /**
1065ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1066ea6b5821SJeremy L Thompson 
1067ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1068ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1069ea6b5821SJeremy L Thompson 
1070ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1071ea6b5821SJeremy L Thompson 
1072ea6b5821SJeremy L Thompson   @ref User
1073ea6b5821SJeremy L Thompson **/
1074ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1075ea6b5821SJeremy L Thompson   char  *name_copy;
1076ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1077ea6b5821SJeremy L Thompson 
10782b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1079ea6b5821SJeremy L Thompson   if (name_len > 0) {
10802b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1081ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1082ea6b5821SJeremy L Thompson     op->name = name_copy;
1083ea6b5821SJeremy L Thompson   }
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) {
1142*6574a04fSJeremy 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) {
1158*6574a04fSJeremy 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;
11732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11746e15d496SJeremy L Thompson 
11756e15d496SJeremy L Thompson   *flops = 0;
11762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11776e15d496SJeremy L Thompson   if (is_composite) {
11786e15d496SJeremy L Thompson     CeedInt num_suboperators;
1179c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11806e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1181c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11826e15d496SJeremy L Thompson 
11836e15d496SJeremy L Thompson     // FLOPs for each suboperator
11846e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11859d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11862b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11876e15d496SJeremy L Thompson       *flops += suboperator_flops;
11886e15d496SJeremy L Thompson     }
11896e15d496SJeremy L Thompson   } else {
11906e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
11916e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11926e15d496SJeremy L Thompson 
11932b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11946e15d496SJeremy L Thompson 
11956e15d496SJeremy L Thompson     CeedInt num_elem = 0;
11962b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11976e15d496SJeremy L Thompson     // Input FLOPs
11986e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11996e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
12009d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
12016e15d496SJeremy L Thompson 
1202437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops));
12036e15d496SJeremy L Thompson         *flops += restr_flops;
12042b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
12056e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12066e15d496SJeremy L Thompson       }
12076e15d496SJeremy L Thompson     }
12086e15d496SJeremy L Thompson     // QF FLOPs
12099d36ca50SJeremy L Thompson     CeedInt  num_qpts;
12109d36ca50SJeremy L Thompson     CeedSize qf_flops;
12112b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12122b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12136e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
12146e15d496SJeremy L Thompson     // Output FLOPs
12156e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12166e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
12179d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
12186e15d496SJeremy L Thompson 
1219437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops));
12206e15d496SJeremy L Thompson         *flops += restr_flops;
12212b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12226e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12236e15d496SJeremy L Thompson       }
12246e15d496SJeremy L Thompson     }
12256e15d496SJeremy L Thompson   }
12266e15d496SJeremy L Thompson 
12276e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12286e15d496SJeremy L Thompson }
12296e15d496SJeremy L Thompson 
12306e15d496SJeremy L Thompson /**
12310126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
1232512bb800SJeremy L Thompson            The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1233512bb800SJeremy L Thompson 
1234512bb800SJeremy L Thompson            Note: If the value of `ctx` passed into this function is non-NULL, then it is assumed that `ctx` is a pointer to a
12350126412dSJeremy L Thompson              CeedQFunctionContext. This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12360126412dSJeremy L Thompson 
1237859c15bbSJames Wright   @param[in]  op  CeedOperator
12380126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12390126412dSJeremy L Thompson 
12400126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12410126412dSJeremy L Thompson 
1242859c15bbSJames Wright   @ref Advanced
12430126412dSJeremy L Thompson **/
12440126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
1245*6574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12460126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12470126412dSJeremy L Thompson   else *ctx = NULL;
12480126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12490126412dSJeremy L Thompson }
12500126412dSJeremy L Thompson 
12510126412dSJeremy L Thompson /**
1252ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12533668ca4bSJeremy L Thompson 
1254859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1255859c15bbSJames Wright 
12563668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12573668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12583668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12593668ca4bSJeremy L Thompson 
12603668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12613668ca4bSJeremy L Thompson 
12623668ca4bSJeremy L Thompson   @ref User
12633668ca4bSJeremy L Thompson **/
126417b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12653668ca4bSJeremy L Thompson   bool is_composite;
12662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12672b730f8bSJeremy L Thompson 
12683668ca4bSJeremy L Thompson   if (is_composite) {
12693668ca4bSJeremy L Thompson     // Check if composite label already created
12703668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12713668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12723668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12733668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12743668ca4bSJeremy L Thompson       }
12753668ca4bSJeremy L Thompson     }
12763668ca4bSJeremy L Thompson 
12773668ca4bSJeremy L Thompson     // Create composite label if needed
12783668ca4bSJeremy L Thompson     CeedInt               num_sub;
12793668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12803668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12813668ca4bSJeremy L Thompson 
12822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1283c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1284c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12852b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12863668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12873668ca4bSJeremy L Thompson 
12883668ca4bSJeremy L Thompson     bool label_found = false;
12893668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12903668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12913668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12922b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
12933668ca4bSJeremy L Thompson         if (new_field_label_i) {
12943668ca4bSJeremy L Thompson           label_found                    = true;
12953668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
12963668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
12973668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
12982b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
12997bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13002b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13012b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
13022b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
13037bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13047bfe0f0eSJeremy L Thompson           } else {
13057bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
13067bfe0f0eSJeremy L Thompson           }
13072b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
13087bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13092b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13102b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13117bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13127bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13137bfe0f0eSJeremy L Thompson           } else {
13147bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13157bfe0f0eSJeremy L Thompson           }
13163668ca4bSJeremy L Thompson         }
13173668ca4bSJeremy L Thompson       }
13183668ca4bSJeremy L Thompson     }
13193668ca4bSJeremy L Thompson     if (!label_found) {
13203668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13212b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13222b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13233668ca4bSJeremy L Thompson       *field_label = NULL;
13243668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13253668ca4bSJeremy L Thompson     } else {
13265ac9af79SJeremy L Thompson       *field_label = new_field_label;
13275ac9af79SJeremy L Thompson     }
13285ac9af79SJeremy L Thompson   } else {
13295ac9af79SJeremy L Thompson     CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
13305ac9af79SJeremy L Thompson   }
13315ac9af79SJeremy L Thompson 
13325ac9af79SJeremy L Thompson   // Set label in operator
13335ac9af79SJeremy L Thompson   if (*field_label) {
13345ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13355ac9af79SJeremy L Thompson 
13363668ca4bSJeremy L Thompson     // Move new composite label to operator
13373668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13382b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13393668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13403668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13412b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13423668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13433668ca4bSJeremy L Thompson     }
13445ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13453668ca4bSJeremy L Thompson     op->num_context_labels++;
13463668ca4bSJeremy L Thompson   }
13473668ca4bSJeremy L Thompson 
13483668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13493668ca4bSJeremy L Thompson }
13503668ca4bSJeremy L Thompson 
13513668ca4bSJeremy L Thompson /**
13522788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13532788fa27SJeremy L Thompson            For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1354d8dd9a91SJeremy L Thompson 
1355ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13562788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1357ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1358d8dd9a91SJeremy L Thompson 
1359d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1360d8dd9a91SJeremy L Thompson 
1361d8dd9a91SJeremy L Thompson   @ref User
1362d8dd9a91SJeremy L Thompson **/
136317b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13642b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1365d8dd9a91SJeremy L Thompson }
1366d8dd9a91SJeremy L Thompson 
1367d8dd9a91SJeremy L Thompson /**
13682788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13692788fa27SJeremy L Thompson            For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13702788fa27SJeremy L Thompson 
13712788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13722788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13732788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13742788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13752788fa27SJeremy L Thompson 
13762788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13772788fa27SJeremy L Thompson 
13782788fa27SJeremy L Thompson   @ref User
13792788fa27SJeremy L Thompson **/
138017b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13812788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13822788fa27SJeremy L Thompson }
13832788fa27SJeremy L Thompson 
13842788fa27SJeremy L Thompson /**
13852788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
13862788fa27SJeremy L Thompson 
13872788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13882788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
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 CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
13962788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
13972788fa27SJeremy L Thompson }
13982788fa27SJeremy L Thompson 
13992788fa27SJeremy L Thompson /**
14002788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
14012788fa27SJeremy L Thompson            For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1402d8dd9a91SJeremy L Thompson 
1403ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1404ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1405ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1406d8dd9a91SJeremy L Thompson 
1407d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1408d8dd9a91SJeremy L Thompson 
1409d8dd9a91SJeremy L Thompson   @ref User
1410d8dd9a91SJeremy L Thompson **/
141117b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14122b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1413d8dd9a91SJeremy L Thompson }
1414d8dd9a91SJeremy L Thompson 
1415d8dd9a91SJeremy L Thompson /**
14162788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14172788fa27SJeremy L Thompson            For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14182788fa27SJeremy L Thompson 
14192788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14202788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1421c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14222788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14232788fa27SJeremy L Thompson 
14242788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14252788fa27SJeremy L Thompson 
14262788fa27SJeremy L Thompson   @ref User
14272788fa27SJeremy L Thompson **/
142817b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14292788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14302788fa27SJeremy L Thompson }
14312788fa27SJeremy L Thompson 
14322788fa27SJeremy L Thompson /**
14332788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14342788fa27SJeremy L Thompson 
14352788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14362788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14372788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14382788fa27SJeremy L Thompson 
14392788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14402788fa27SJeremy L Thompson 
14412788fa27SJeremy L Thompson   @ref User
14422788fa27SJeremy L Thompson **/
144317b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14442788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14452788fa27SJeremy L Thompson }
14462788fa27SJeremy L Thompson 
14472788fa27SJeremy L Thompson /**
14483bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1449d7b241e6Sjeremylt 
1450ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1451ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1452d7b241e6Sjeremylt 
1453ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1454f04ea552SJeremy L Thompson 
1455ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1456ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14575ac9af79SJeremy 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
14585ac9af79SJeremy L Thompson active outputs
1459ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1460b11c1e72Sjeremylt 
1461b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1462dfdf5a53Sjeremylt 
14637a982d89SJeremy L. Thompson   @ref User
1464b11c1e72Sjeremylt **/
14652b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1467d7b241e6Sjeremylt 
1468d1d35e2fSjeremylt   if (op->num_elem) {
1469250756a7Sjeremylt     // Standard Operator
1470cae8b89aSjeremylt     if (op->Apply) {
14712b730f8bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
1472cae8b89aSjeremylt     } else {
1473cae8b89aSjeremylt       // Zero all output vectors
1474250756a7Sjeremylt       CeedQFunction qf = op->qf;
1475d1d35e2fSjeremylt       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1476d1d35e2fSjeremylt         CeedVector vec = op->output_fields[i]->vec;
14772b730f8bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
1478cae8b89aSjeremylt         if (vec != CEED_VECTOR_NONE) {
14792b730f8bSJeremy L Thompson           CeedCall(CeedVectorSetValue(vec, 0.0));
1480cae8b89aSjeremylt         }
1481cae8b89aSjeremylt       }
1482250756a7Sjeremylt       // Apply
14832b730f8bSJeremy L Thompson       CeedCall(op->ApplyAdd(op, in, out, request));
1484250756a7Sjeremylt     }
1485f04ea552SJeremy L Thompson   } else 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;
1491c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1492d1d35e2fSjeremylt       CeedOperator *sub_operators;
1493c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1494250756a7Sjeremylt 
1495250756a7Sjeremylt       // Zero all output vectors
1496250756a7Sjeremylt       if (out != CEED_VECTOR_NONE) {
14972b730f8bSJeremy L Thompson         CeedCall(CeedVectorSetValue(out, 0.0));
1498cae8b89aSjeremylt       }
1499d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1500d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1501d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1502250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
15032b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1504250756a7Sjeremylt           }
1505250756a7Sjeremylt         }
1506250756a7Sjeremylt       }
1507250756a7Sjeremylt       // Apply
1508d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
15092b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1510cae8b89aSjeremylt       }
1511cae8b89aSjeremylt     }
1512250756a7Sjeremylt   }
1513e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1514cae8b89aSjeremylt }
1515cae8b89aSjeremylt 
1516cae8b89aSjeremylt /**
1517cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1518cae8b89aSjeremylt 
1519ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1520ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1521cae8b89aSjeremylt 
1522ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1523ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1524ea61e9acSJeremy 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
1525ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1526cae8b89aSjeremylt 
1527cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1528cae8b89aSjeremylt 
15297a982d89SJeremy L. Thompson   @ref User
1530cae8b89aSjeremylt **/
15312b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1533cae8b89aSjeremylt 
1534d1d35e2fSjeremylt   if (op->num_elem) {
1535250756a7Sjeremylt     // Standard Operator
15362b730f8bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1537f04ea552SJeremy L Thompson   } else if (op->is_composite) {
1538250756a7Sjeremylt     // Composite Operator
1539250756a7Sjeremylt     if (op->ApplyAddComposite) {
15402b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1541cae8b89aSjeremylt     } else {
1542d1d35e2fSjeremylt       CeedInt num_suboperators;
1543c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1544d1d35e2fSjeremylt       CeedOperator *sub_operators;
1545c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1546250756a7Sjeremylt 
1547d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15482b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15491d7d2407SJeremy L Thompson       }
1550250756a7Sjeremylt     }
1551250756a7Sjeremylt   }
1552e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1553d7b241e6Sjeremylt }
1554d7b241e6Sjeremylt 
1555d7b241e6Sjeremylt /**
1556b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1557d7b241e6Sjeremylt 
1558ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1559b11c1e72Sjeremylt 
1560b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1561dfdf5a53Sjeremylt 
15627a982d89SJeremy L. Thompson   @ref User
1563b11c1e72Sjeremylt **/
1564d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1565ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1566ad6481ceSJeremy L Thompson     *op = NULL;
1567ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1568ad6481ceSJeremy L Thompson   }
15692b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15702b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1571fe2413ffSjeremylt   // Free fields
15722b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1573d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1574437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1575437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
157615910d16Sjeremylt       }
1577d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15782b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
157971352a93Sjeremylt       }
15802b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15812b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
158271352a93Sjeremylt       }
15832b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
15842b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1585fe2413ffSjeremylt     }
15862b730f8bSJeremy L Thompson   }
15872b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1588d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1589437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1590d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15912b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
159271352a93Sjeremylt       }
15932b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
15942b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
159571352a93Sjeremylt       }
15962b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
15972b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
15982b730f8bSJeremy L Thompson     }
1599fe2413ffSjeremylt   }
1600d1d35e2fSjeremylt   // Destroy sub_operators
16012b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1602d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
16032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
160452d6035fSJeremy L Thompson     }
16052b730f8bSJeremy L Thompson   }
16062b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
16072b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
16082b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16093668ca4bSJeremy L Thompson   // Destroy any composite labels
16105ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16113668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16122b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16132b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16143668ca4bSJeremy L Thompson     }
16155ac9af79SJeremy L Thompson   }
16162b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1617fe2413ffSjeremylt 
16185107b09fSJeremy L Thompson   // Destroy fallback
16192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16205107b09fSJeremy L Thompson 
1621ed9e99e6SJeremy L Thompson   // Destroy assembly data
16222b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
162470a7ffb3SJeremy L Thompson 
16252b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16262b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16272b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16282b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16292b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1630e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1631d7b241e6Sjeremylt }
1632d7b241e6Sjeremylt 
1633d7b241e6Sjeremylt /// @}
1634