xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 5a72c492250c2cd76f15d4719221c1af2dfce317)
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
416574a04fSJeremy L Thompson   CeedCheck((r == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
426574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
442b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
476574a04fSJeremy L Thompson   CeedCheck((b == CEED_BASIS_COLLOCATED) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
486574a04fSJeremy 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));
536574a04fSJeremy L Thompson     CeedCheck(r == CEED_ELEMRESTRICTION_NONE || restr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
546574a04fSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components, but Basis has %" CeedInt_FMT
556574a04fSJeremy L Thompson               " components",
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:
616574a04fSJeremy 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:
696574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
706574a04fSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components", qf_field->field_name,
716574a04fSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72e15f9bd0SJeremy L Thompson       break;
73e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
74d1d35e2fSjeremylt       // No additional checks required
75e15f9bd0SJeremy L Thompson       break;
76e15f9bd0SJeremy L Thompson   }
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
817a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
827a982d89SJeremy L. Thompson 
837a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
84d1d35e2fSjeremylt   @param[in] qf_field     QFunction field (carries field name)
85d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
864c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
887a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @ref Utility
937a982d89SJeremy L. Thompson **/
942b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
957a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
96d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
977a982d89SJeremy L. Thompson 
982b730f8bSJeremy L Thompson   fprintf(stream,
992b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1002b730f8bSJeremy L Thompson           ":\n"
1017a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
102d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
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) {
1706574a04fSJeremy 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 
1756574a04fSJeremy 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) {
1976574a04fSJeremy L Thompson       CeedCheck(!*active_rstr || *active_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
1986574a04fSJeremy 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 
2036574a04fSJeremy 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.
2094385fb7fSSebastian Grimberg 
210ea61e9acSJeremy L Thompson   For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
2119fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
2129fd66db6SSebastian Grimberg any field of a matching type.
213d8dd9a91SJeremy L Thompson 
214ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
215ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
216ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2172788fa27SJeremy L Thompson   @param[in]     values      Values to set
218d8dd9a91SJeremy L Thompson 
219d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
220d8dd9a91SJeremy L Thompson 
221d8dd9a91SJeremy L Thompson   @ref User
222d8dd9a91SJeremy L Thompson **/
2232788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2246574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2253668ca4bSJeremy L Thompson 
2265ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2275ac9af79SJeremy L Thompson   if (field_label->from_op) {
2285ac9af79SJeremy L Thompson     CeedInt index = -1;
2295ac9af79SJeremy L Thompson 
2305ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2315ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2325ac9af79SJeremy L Thompson     }
2336574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2345ac9af79SJeremy L Thompson   }
2355ac9af79SJeremy L Thompson 
2363668ca4bSJeremy L Thompson   bool is_composite = false;
2372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
238d8dd9a91SJeremy L Thompson   if (is_composite) {
239d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
240d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
241d8dd9a91SJeremy L Thompson 
242c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
243c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2446574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2456574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
246d8dd9a91SJeremy L Thompson 
247d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
248d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
2493668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
2502788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
251d8dd9a91SJeremy L Thompson       }
252d8dd9a91SJeremy L Thompson     }
253d8dd9a91SJeremy L Thompson   } else {
2546574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
2552788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
2562788fa27SJeremy L Thompson   }
2572788fa27SJeremy L Thompson 
2582788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2592788fa27SJeremy L Thompson }
2602788fa27SJeremy L Thompson 
2612788fa27SJeremy L Thompson /**
2622788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
2634385fb7fSSebastian Grimberg 
2642788fa27SJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
2659fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
2669fd66db6SSebastian Grimberg any field of a matching type.
2672788fa27SJeremy L Thompson 
2682788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
2692788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
2702788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
271c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
272c5d0f995SJed Brown   @param[out]    values      Values in the label
2732788fa27SJeremy L Thompson 
2742788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2752788fa27SJeremy L Thompson 
2762788fa27SJeremy L Thompson   @ref User
2772788fa27SJeremy L Thompson **/
2782788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
2792788fa27SJeremy L Thompson                                              void *values) {
2806574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2812788fa27SJeremy L Thompson 
2822788fa27SJeremy L Thompson   *(void **)values = NULL;
2832788fa27SJeremy L Thompson   *num_values      = 0;
2842788fa27SJeremy L Thompson 
2855ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2865ac9af79SJeremy L Thompson   if (field_label->from_op) {
2875ac9af79SJeremy L Thompson     CeedInt index = -1;
2885ac9af79SJeremy L Thompson 
2895ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2905ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2915ac9af79SJeremy L Thompson     }
2926574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2935ac9af79SJeremy L Thompson   }
2945ac9af79SJeremy L Thompson 
2952788fa27SJeremy L Thompson   bool is_composite = false;
2962788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2972788fa27SJeremy L Thompson   if (is_composite) {
2982788fa27SJeremy L Thompson     CeedInt       num_sub;
2992788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3002788fa27SJeremy L Thompson 
3012788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3022788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3036574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3046574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3052788fa27SJeremy L Thompson 
3062788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3072788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3082788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3092788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3102788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3112788fa27SJeremy L Thompson       }
3122788fa27SJeremy L Thompson     }
3132788fa27SJeremy L Thompson   } else {
3146574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3152788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3162788fa27SJeremy L Thompson   }
3172788fa27SJeremy L Thompson 
3182788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3192788fa27SJeremy L Thompson }
3202788fa27SJeremy L Thompson 
3212788fa27SJeremy L Thompson /**
3222788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
3234385fb7fSSebastian Grimberg 
3242788fa27SJeremy L Thompson   For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
3259fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
3269fd66db6SSebastian Grimberg any field of a matching type.
3272788fa27SJeremy L Thompson 
3282788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3292788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3302788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
331c5d0f995SJed Brown   @param[in]     values      Values array to restore
3322788fa27SJeremy L Thompson 
3332788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3342788fa27SJeremy L Thompson 
3352788fa27SJeremy L Thompson   @ref User
3362788fa27SJeremy L Thompson **/
3372788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3386574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3392788fa27SJeremy L Thompson 
3405ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3415ac9af79SJeremy L Thompson   if (field_label->from_op) {
3425ac9af79SJeremy L Thompson     CeedInt index = -1;
3435ac9af79SJeremy L Thompson 
3445ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3455ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3465ac9af79SJeremy L Thompson     }
3476574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3485ac9af79SJeremy L Thompson   }
3495ac9af79SJeremy L Thompson 
3502788fa27SJeremy L Thompson   bool is_composite = false;
3512788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3522788fa27SJeremy L Thompson   if (is_composite) {
3532788fa27SJeremy L Thompson     CeedInt       num_sub;
3542788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3552788fa27SJeremy L Thompson 
3562788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3572788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3586574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3596574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3602788fa27SJeremy L Thompson 
3612788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3622788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3632788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3642788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
3652788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3662788fa27SJeremy L Thompson       }
3672788fa27SJeremy L Thompson     }
3682788fa27SJeremy L Thompson   } else {
3696574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3702788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
371d8dd9a91SJeremy L Thompson   }
372d8dd9a91SJeremy L Thompson 
373d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
374d8dd9a91SJeremy L Thompson }
375d8dd9a91SJeremy L Thompson 
3767a982d89SJeremy L. Thompson /// @}
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3797a982d89SJeremy L. Thompson /// CeedOperator Backend API
3807a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3817a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
3827a982d89SJeremy L. Thompson /// @{
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson /**
3857a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
3867a982d89SJeremy L. Thompson 
387ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
388d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   @ref Backend
3937a982d89SJeremy L. Thompson **/
394d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
3956574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
396d1d35e2fSjeremylt   *num_args = op->num_fields;
397e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3987a982d89SJeremy L. Thompson }
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson /**
4017a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4027a982d89SJeremy L. Thompson 
403ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
404d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4057a982d89SJeremy L. Thompson 
4067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson   @ref Backend
4097a982d89SJeremy L. Thompson **/
410d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
411f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4137a982d89SJeremy L. Thompson }
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson /**
4167a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4177a982d89SJeremy L. Thompson 
418ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4197a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4207a982d89SJeremy L. Thompson 
4217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4227a982d89SJeremy L. Thompson 
4237a982d89SJeremy L. Thompson   @ref Backend
4247a982d89SJeremy L. Thompson **/
4257a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
4266574a04fSJeremy 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 **/
441d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
442f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
443e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
444c04a41a7SJeremy L Thompson }
445c04a41a7SJeremy L Thompson 
446c04a41a7SJeremy L Thompson /**
4477a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
4487a982d89SJeremy L. Thompson 
449ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
4507a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson   @ref Backend
4557a982d89SJeremy L. Thompson **/
456777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
457777ff853SJeremy L Thompson   *(void **)data = op->data;
458e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4597a982d89SJeremy L. Thompson }
4607a982d89SJeremy L. Thompson 
4617a982d89SJeremy L. Thompson /**
4627a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
4637a982d89SJeremy L. Thompson 
464ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
465ea61e9acSJeremy L Thompson   @param[in]     data Data to set
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @ref Backend
4707a982d89SJeremy L. Thompson **/
471777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
472777ff853SJeremy L Thompson   op->data = data;
473e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4747a982d89SJeremy L. Thompson }
4757a982d89SJeremy L. Thompson 
4767a982d89SJeremy L. Thompson /**
47734359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
47834359f16Sjeremylt 
479ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
48034359f16Sjeremylt 
48134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
48234359f16Sjeremylt 
48334359f16Sjeremylt   @ref Backend
48434359f16Sjeremylt **/
4859560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
48634359f16Sjeremylt   op->ref_count++;
48734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
48834359f16Sjeremylt }
48934359f16Sjeremylt 
49034359f16Sjeremylt /**
4917a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
4927a982d89SJeremy L. Thompson 
493ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
4947a982d89SJeremy L. Thompson 
4957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson   @ref Backend
4987a982d89SJeremy L. Thompson **/
4997a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
500f04ea552SJeremy L Thompson   op->is_backend_setup = true;
501e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5027a982d89SJeremy L. Thompson }
5037a982d89SJeremy L. Thompson 
5047a982d89SJeremy L. Thompson /// @}
5057a982d89SJeremy L. Thompson 
5067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5077a982d89SJeremy L. Thompson /// CeedOperator Public API
5087a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5097a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
510dfdf5a53Sjeremylt /// @{
511d7b241e6Sjeremylt 
512d7b241e6Sjeremylt /**
513ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
5144385fb7fSSebastian Grimberg 
515ea61e9acSJeremy L Thompson   A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
516d7b241e6Sjeremylt 
517ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
518ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
519ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
520ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
521ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
522b11c1e72Sjeremylt 
523b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
524dfdf5a53Sjeremylt 
5257a982d89SJeremy L. Thompson   @ref User
526d7b241e6Sjeremylt  */
5272b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5285fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5295fe0d4faSjeremylt     Ceed delegate;
5306574a04fSJeremy L Thompson 
5312b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
5326574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
5332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
534e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5355fe0d4faSjeremylt   }
5365fe0d4faSjeremylt 
5376574a04fSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
538db002c03SJeremy L Thompson 
5392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
540db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
541d1d35e2fSjeremylt   (*op)->ref_count   = 1;
5422b104005SJeremy L Thompson   (*op)->input_size  = -1;
5432b104005SJeremy L Thompson   (*op)->output_size = -1;
544db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
545db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
546db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
5472b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
5482b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
5492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
5502b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
551e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
552d7b241e6Sjeremylt }
553d7b241e6Sjeremylt 
554d7b241e6Sjeremylt /**
55552d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
55652d6035fSJeremy L Thompson 
557ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
558ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
55952d6035fSJeremy L Thompson 
56052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
56152d6035fSJeremy L Thompson 
5627a982d89SJeremy L. Thompson   @ref User
56352d6035fSJeremy L Thompson  */
56452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
56552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
56652d6035fSJeremy L Thompson     Ceed delegate;
5672b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
56852d6035fSJeremy L Thompson 
569250756a7Sjeremylt     if (delegate) {
5702b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
571e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
57252d6035fSJeremy L Thompson     }
573250756a7Sjeremylt   }
57452d6035fSJeremy L Thompson 
5752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
576db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
577996d9ab5SJed Brown   (*op)->ref_count    = 1;
578f04ea552SJeremy L Thompson   (*op)->is_composite = true;
5792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
5802b104005SJeremy L Thompson   (*op)->input_size  = -1;
5812b104005SJeremy L Thompson   (*op)->output_size = -1;
582250756a7Sjeremylt 
583db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
584e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
58552d6035fSJeremy L Thompson }
58652d6035fSJeremy L Thompson 
58752d6035fSJeremy L Thompson /**
588ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
5894385fb7fSSebastian Grimberg 
590ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedOperatorDestroy()`.
591512bb800SJeremy L Thompson 
592512bb800SJeremy 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.
593512bb800SJeremy L Thompson         This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
5949560d06aSjeremylt 
595ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
596ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
5979560d06aSjeremylt 
5989560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5999560d06aSjeremylt 
6009560d06aSjeremylt   @ref User
6019560d06aSjeremylt **/
6029560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
6032b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
6042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
6059560d06aSjeremylt   *op_copy = op;
6069560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6079560d06aSjeremylt }
6089560d06aSjeremylt 
6099560d06aSjeremylt /**
610ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
611d7b241e6Sjeremylt 
612ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
613ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
614ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
615d7b241e6Sjeremylt 
616ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
617ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
618d7b241e6Sjeremylt 
619ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
620ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
621ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
622ea61e9acSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points
6235ac9af79SJeremy 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
6245ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
625b11c1e72Sjeremylt 
626b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
627dfdf5a53Sjeremylt 
6287a982d89SJeremy L. Thompson   @ref User
629b11c1e72Sjeremylt **/
6302b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
6316574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
6326574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
6336574a04fSJeremy L Thompson   CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
6346574a04fSJeremy L Thompson   CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
6356574a04fSJeremy L Thompson   CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
63652d6035fSJeremy L Thompson 
6376574a04fSJeremy L Thompson   CeedInt num_elem = 0;
6382b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
6396574a04fSJeremy L Thompson   CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
6402b730f8bSJeremy L Thompson             "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
641d7b241e6Sjeremylt 
64278464608Sjeremylt   CeedInt num_qpts = 0;
6439a18a30cSJeremy L Thompson   if (b == CEED_BASIS_COLLOCATED) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts));
6449a18a30cSJeremy L Thompson   else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
6459a18a30cSJeremy L Thompson   CeedCheck(!op->num_qpts || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
6462b730f8bSJeremy L Thompson             "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts);
647d1d35e2fSjeremylt   CeedQFunctionField qf_field;
648d1d35e2fSjeremylt   CeedOperatorField *op_field;
6492b104005SJeremy L Thompson   bool               is_input = true;
650d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
651d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
652d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
653d1d35e2fSjeremylt       op_field = &op->input_fields[i];
654d7b241e6Sjeremylt       goto found;
655d7b241e6Sjeremylt     }
656d7b241e6Sjeremylt   }
6572b104005SJeremy L Thompson   is_input = false;
658d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
659d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
660d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
661d1d35e2fSjeremylt       op_field = &op->output_fields[i];
662d7b241e6Sjeremylt       goto found;
663d7b241e6Sjeremylt     }
664d7b241e6Sjeremylt   }
665c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6662b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
667c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
668d7b241e6Sjeremylt found:
6692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6702b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
671e15f9bd0SJeremy L Thompson 
6722b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6732b104005SJeremy L Thompson     CeedSize l_size;
6742b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6752b104005SJeremy L Thompson     if (is_input) {
6762b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
6776574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6786574a04fSJeremy L Thompson                 op->input_size);
6792b104005SJeremy L Thompson     } else {
6802b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
6816574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6826574a04fSJeremy L Thompson                 op->output_size);
6832b104005SJeremy L Thompson     }
6842b730f8bSJeremy L Thompson   }
6852b104005SJeremy L Thompson 
686393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
687393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
688e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
689d1d35e2fSjeremylt     op->num_elem        = num_elem;
690d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
691e15f9bd0SJeremy L Thompson   }
692393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
693*5a72c492SJeremy L Thompson   if (op->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
694e15f9bd0SJeremy L Thompson 
695d1d35e2fSjeremylt   op->num_fields += 1;
6962b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
697e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
698d7b241e6Sjeremylt }
699d7b241e6Sjeremylt 
700d7b241e6Sjeremylt /**
70143bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
70243bbe138SJeremy L Thompson 
703ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
704f04ea552SJeremy L Thompson 
705ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
706f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
70743bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
708f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
70943bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
71043bbe138SJeremy L Thompson 
71143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71243bbe138SJeremy L Thompson 
713e9b533fbSJeremy L Thompson   @ref Advanced
71443bbe138SJeremy L Thompson **/
7152b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
71643bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7176574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7182b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
71943bbe138SJeremy L Thompson 
72043bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
72143bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
72243bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
72343bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
72443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72543bbe138SJeremy L Thompson }
72643bbe138SJeremy L Thompson 
72743bbe138SJeremy L Thompson /**
728de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
729de5900adSJames Wright 
730de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
731de5900adSJames Wright 
732de5900adSJames Wright   @param[in]  op         CeedOperator
733de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
734de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
735de5900adSJames Wright 
736de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
737de5900adSJames Wright 
738de5900adSJames Wright   @ref Advanced
739de5900adSJames Wright **/
740de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
741de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
742de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
743de5900adSJames Wright   char              *name;
744de5900adSJames Wright   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
745de5900adSJames Wright 
746de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
747de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
748de5900adSJames Wright     if (!strcmp(name, field_name)) {
749de5900adSJames Wright       *op_field = input_fields[i];
750de5900adSJames Wright       return CEED_ERROR_SUCCESS;
751de5900adSJames Wright     }
752de5900adSJames Wright   }
753de5900adSJames Wright 
754de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
755de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
756de5900adSJames Wright     if (!strcmp(name, field_name)) {
757de5900adSJames Wright       *op_field = output_fields[i];
758de5900adSJames Wright       return CEED_ERROR_SUCCESS;
759de5900adSJames Wright     }
760de5900adSJames Wright   }
761de5900adSJames Wright 
762de5900adSJames Wright   bool has_name = op->name;
763de5900adSJames Wright   // LCOV_EXCL_START
764de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
765de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
766de5900adSJames Wright   // LCOV_EXCL_STOP
767de5900adSJames Wright }
768de5900adSJames Wright 
769de5900adSJames Wright /**
77028567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
77128567f8fSJeremy L Thompson 
772ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
77328567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
77428567f8fSJeremy L Thompson 
77528567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
77628567f8fSJeremy L Thompson 
777e9b533fbSJeremy L Thompson   @ref Advanced
77828567f8fSJeremy L Thompson **/
77928567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
78028567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
78128567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78228567f8fSJeremy L Thompson }
78328567f8fSJeremy L Thompson 
78428567f8fSJeremy L Thompson /**
78543bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
78643bbe138SJeremy L Thompson 
787ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
78843bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
78943bbe138SJeremy L Thompson 
79043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79143bbe138SJeremy L Thompson 
792e9b533fbSJeremy L Thompson   @ref Advanced
79343bbe138SJeremy L Thompson **/
7942b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
795437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
79643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79743bbe138SJeremy L Thompson }
79843bbe138SJeremy L Thompson 
79943bbe138SJeremy L Thompson /**
80043bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
80143bbe138SJeremy L Thompson 
802ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
80343bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
80443bbe138SJeremy L Thompson 
80543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80643bbe138SJeremy L Thompson 
807e9b533fbSJeremy L Thompson   @ref Advanced
80843bbe138SJeremy L Thompson **/
80943bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
81043bbe138SJeremy L Thompson   *basis = op_field->basis;
81143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81243bbe138SJeremy L Thompson }
81343bbe138SJeremy L Thompson 
81443bbe138SJeremy L Thompson /**
81543bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
81643bbe138SJeremy L Thompson 
817ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
81843bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
81943bbe138SJeremy L Thompson 
82043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82143bbe138SJeremy L Thompson 
822e9b533fbSJeremy L Thompson   @ref Advanced
82343bbe138SJeremy L Thompson **/
82443bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
82543bbe138SJeremy L Thompson   *vec = op_field->vec;
82643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82743bbe138SJeremy L Thompson }
82843bbe138SJeremy L Thompson 
82943bbe138SJeremy L Thompson /**
83052d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
831288c0443SJeremy L Thompson 
832ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
833ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
83452d6035fSJeremy L Thompson 
83552d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83652d6035fSJeremy L Thompson 
8377a982d89SJeremy L. Thompson   @ref User
83852d6035fSJeremy L Thompson  */
8392b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
8406574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
8416574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
8426574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8432b730f8bSJeremy L Thompson 
8442b730f8bSJeremy L Thompson   {
8452b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8462b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8472b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8482b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8492b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
8506574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
8516574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8522b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8532b730f8bSJeremy L Thompson               "shape (%td, %td)",
8542b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8552b730f8bSJeremy L Thompson   }
8562b730f8bSJeremy L Thompson 
857d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
859d1d35e2fSjeremylt   composite_op->num_suboperators++;
860e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86152d6035fSJeremy L Thompson }
86252d6035fSJeremy L Thompson 
86352d6035fSJeremy L Thompson /**
86475f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
86575f0d5a4SJeremy L Thompson 
86675f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
86775f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
86875f0d5a4SJeremy L Thompson 
86975f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87075f0d5a4SJeremy L Thompson 
87175f0d5a4SJeremy L Thompson   @ref Backend
87275f0d5a4SJeremy L Thompson **/
873c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
8746574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
87575f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
87675f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87775f0d5a4SJeremy L Thompson }
87875f0d5a4SJeremy L Thompson 
87975f0d5a4SJeremy L Thompson /**
88075f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
88175f0d5a4SJeremy L Thompson 
88275f0d5a4SJeremy L Thompson   @param op                  CeedOperator
88375f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
88475f0d5a4SJeremy L Thompson 
88575f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88675f0d5a4SJeremy L Thompson 
88775f0d5a4SJeremy L Thompson   @ref Backend
88875f0d5a4SJeremy L Thompson **/
889c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
8906574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
89175f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
89275f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89375f0d5a4SJeremy L Thompson }
89475f0d5a4SJeremy L Thompson 
89575f0d5a4SJeremy L Thompson /**
8964db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
8974db537f9SJeremy L Thompson 
8984db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
8994db537f9SJeremy L Thompson 
9004db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9014db537f9SJeremy L Thompson 
9024db537f9SJeremy L Thompson   @ref User
9034db537f9SJeremy L Thompson **/
9044db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9054db537f9SJeremy L Thompson   Ceed ceed;
9062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9074db537f9SJeremy L Thompson 
9082b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9094db537f9SJeremy L Thompson 
9104db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9114db537f9SJeremy L Thompson   if (op->is_composite) {
91243622462SJeremy L Thompson     if (!op->num_suboperators) {
91343622462SJeremy L Thompson       // Empty operator setup
91443622462SJeremy L Thompson       op->input_size  = 0;
91543622462SJeremy L Thompson       op->output_size = 0;
91643622462SJeremy L Thompson     } else {
9174db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9182b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9194db537f9SJeremy L Thompson       }
9202b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9212b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9222b104005SJeremy L Thompson       CeedSize input_size, output_size;
9232b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
92443622462SJeremy L Thompson     }
9254db537f9SJeremy L Thompson   } else {
9266574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9276574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9286574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9296574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9306574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9312b730f8bSJeremy L Thompson   }
9324db537f9SJeremy L Thompson 
9334db537f9SJeremy L Thompson   // Flag as immutable and ready
9344db537f9SJeremy L Thompson   op->is_interface_setup = true;
9352b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9362b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9372b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9384db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9394db537f9SJeremy L Thompson }
9404db537f9SJeremy L Thompson 
9414db537f9SJeremy L Thompson /**
942c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
9434385fb7fSSebastian Grimberg 
944ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
945c9366a6bSJeremy L Thompson 
946c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
947c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
948c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
949c9366a6bSJeremy L Thompson 
950c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
951c9366a6bSJeremy L Thompson 
952c9366a6bSJeremy L Thompson   @ref User
953c9366a6bSJeremy L Thompson **/
9542b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
955c9366a6bSJeremy L Thompson   bool is_composite;
956c9366a6bSJeremy L Thompson 
9572b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9582b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
959c9366a6bSJeremy L Thompson 
9602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9612b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
962c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
963c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9642b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9652b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9662b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9672b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
9686574a04fSJeremy 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,
9696574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9702b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9712b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9722b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
973c9366a6bSJeremy L Thompson     }
9742b730f8bSJeremy L Thompson   }
975c9366a6bSJeremy L Thompson 
976c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
977c9366a6bSJeremy L Thompson }
978c9366a6bSJeremy L Thompson 
979c9366a6bSJeremy L Thompson /**
980beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
9814385fb7fSSebastian Grimberg 
982ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
9839fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
9849fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
9859fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9868b919e6bSJeremy L Thompson 
987beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
988beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9898b919e6bSJeremy L Thompson 
9908b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9918b919e6bSJeremy L Thompson 
9928b919e6bSJeremy L Thompson   @ref Advanced
9938b919e6bSJeremy L Thompson **/
9942b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
9958b919e6bSJeremy L Thompson   bool is_composite;
9968b919e6bSJeremy L Thompson 
9972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9988b919e6bSJeremy L Thompson   if (is_composite) {
9998b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10002b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10018b919e6bSJeremy L Thompson     }
10028b919e6bSJeremy L Thompson   } else {
10032b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1004beecbf24SJeremy L Thompson   }
1005beecbf24SJeremy L Thompson 
1006beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1007beecbf24SJeremy L Thompson }
1008beecbf24SJeremy L Thompson 
1009beecbf24SJeremy L Thompson /**
1010beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1011beecbf24SJeremy L Thompson 
1012beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10136e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1014beecbf24SJeremy L Thompson 
1015beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1016beecbf24SJeremy L Thompson 
1017beecbf24SJeremy L Thompson   @ref Advanced
1018beecbf24SJeremy L Thompson **/
10192b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1020beecbf24SJeremy L Thompson   bool is_composite;
1021beecbf24SJeremy L Thompson 
10222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1023beecbf24SJeremy L Thompson   if (is_composite) {
1024beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10252b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1026beecbf24SJeremy L Thompson     }
1027beecbf24SJeremy L Thompson   } else {
10282b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10298b919e6bSJeremy L Thompson   }
10308b919e6bSJeremy L Thompson 
10318b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10328b919e6bSJeremy L Thompson }
10338b919e6bSJeremy L Thompson 
10348b919e6bSJeremy L Thompson /**
1035cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
10364385fb7fSSebastian Grimberg 
1037ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1038ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1039cd4dfc48Sjeremylt 
1040ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1041ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1042cd4dfc48Sjeremylt 
1043cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1044cd4dfc48Sjeremylt 
1045e9b533fbSJeremy L Thompson   @ref Advanced
1046cd4dfc48Sjeremylt **/
1047cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
10486574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
10496574a04fSJeremy L Thompson   CeedCheck(op->num_qpts == 0, op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
10506574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1051cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1052cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1053cd4dfc48Sjeremylt }
1054cd4dfc48Sjeremylt 
1055cd4dfc48Sjeremylt /**
1056ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1057ea6b5821SJeremy L Thompson 
1058ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1059ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1060ea6b5821SJeremy L Thompson 
1061ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1062ea6b5821SJeremy L Thompson 
1063ea6b5821SJeremy L Thompson   @ref User
1064ea6b5821SJeremy L Thompson **/
1065ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1066ea6b5821SJeremy L Thompson   char  *name_copy;
1067ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1068ea6b5821SJeremy L Thompson 
10692b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1070ea6b5821SJeremy L Thompson   if (name_len > 0) {
10712b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1072ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1073ea6b5821SJeremy L Thompson     op->name = name_copy;
1074ea6b5821SJeremy L Thompson   }
1075ea6b5821SJeremy L Thompson 
1076ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1077ea6b5821SJeremy L Thompson }
1078ea6b5821SJeremy L Thompson 
1079ea6b5821SJeremy L Thompson /**
10807a982d89SJeremy L. Thompson   @brief View a CeedOperator
10817a982d89SJeremy L. Thompson 
10827a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
10837a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
10847a982d89SJeremy L. Thompson 
10857a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
10867a982d89SJeremy L. Thompson 
10877a982d89SJeremy L. Thompson   @ref User
10887a982d89SJeremy L. Thompson **/
10897a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1090ea6b5821SJeremy L Thompson   bool has_name = op->name;
10917a982d89SJeremy L. Thompson 
1092f04ea552SJeremy L Thompson   if (op->is_composite) {
10932b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
10947a982d89SJeremy L. Thompson 
1095d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1096ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
10972b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
10982b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
10997a982d89SJeremy L. Thompson     }
11007a982d89SJeremy L. Thompson   } else {
11012b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11037a982d89SJeremy L. Thompson   }
1104e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11057a982d89SJeremy L. Thompson }
11063bd813ffSjeremylt 
11073bd813ffSjeremylt /**
1108b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1109b7c9bbdaSJeremy L Thompson 
1110ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1111b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1112b7c9bbdaSJeremy L Thompson 
1113b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1114b7c9bbdaSJeremy L Thompson 
1115b7c9bbdaSJeremy L Thompson   @ref Advanced
1116b7c9bbdaSJeremy L Thompson **/
1117b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1118b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1119b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1120b7c9bbdaSJeremy L Thompson }
1121b7c9bbdaSJeremy L Thompson 
1122b7c9bbdaSJeremy L Thompson /**
1123b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1124b7c9bbdaSJeremy L Thompson 
1125ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1126b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1127b7c9bbdaSJeremy L Thompson 
1128b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1129b7c9bbdaSJeremy L Thompson 
1130b7c9bbdaSJeremy L Thompson   @ref Advanced
1131b7c9bbdaSJeremy L Thompson **/
1132b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
11336574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1134b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1135b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1136b7c9bbdaSJeremy L Thompson }
1137b7c9bbdaSJeremy L Thompson 
1138b7c9bbdaSJeremy L Thompson /**
1139b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1140b7c9bbdaSJeremy L Thompson 
1141ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1142b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1143b7c9bbdaSJeremy L Thompson 
1144b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1145b7c9bbdaSJeremy L Thompson 
1146b7c9bbdaSJeremy L Thompson   @ref Advanced
1147b7c9bbdaSJeremy L Thompson **/
1148b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
11496574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1150b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1151b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1152b7c9bbdaSJeremy L Thompson }
1153b7c9bbdaSJeremy L Thompson 
1154b7c9bbdaSJeremy L Thompson /**
11556e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
11566e15d496SJeremy L Thompson 
1157ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1158ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
11596e15d496SJeremy L Thompson 
11606e15d496SJeremy L Thompson   @ref Backend
11616e15d496SJeremy L Thompson **/
11629d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
11636e15d496SJeremy L Thompson   bool is_composite;
11642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11656e15d496SJeremy L Thompson 
11666e15d496SJeremy L Thompson   *flops = 0;
11672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11686e15d496SJeremy L Thompson   if (is_composite) {
11696e15d496SJeremy L Thompson     CeedInt num_suboperators;
1170c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11716e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1172c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11736e15d496SJeremy L Thompson 
11746e15d496SJeremy L Thompson     // FLOPs for each suboperator
11756e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11769d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11772b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11786e15d496SJeremy L Thompson       *flops += suboperator_flops;
11796e15d496SJeremy L Thompson     }
11806e15d496SJeremy L Thompson   } else {
11816e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
11826e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11832b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11846e15d496SJeremy L Thompson     CeedInt num_elem = 0;
11852b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11864385fb7fSSebastian Grimberg 
11876e15d496SJeremy L Thompson     // Input FLOPs
11886e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11896e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
11909d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
11916e15d496SJeremy L Thompson 
1192437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops));
11936e15d496SJeremy L Thompson         *flops += restr_flops;
11942b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
11956e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
11966e15d496SJeremy L Thompson       }
11976e15d496SJeremy L Thompson     }
11986e15d496SJeremy L Thompson     // QF FLOPs
11999d36ca50SJeremy L Thompson     CeedInt  num_qpts;
12009d36ca50SJeremy L Thompson     CeedSize qf_flops;
12012b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12022b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12036e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
12046e15d496SJeremy L Thompson     // Output FLOPs
12056e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12066e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
12079d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
12086e15d496SJeremy L Thompson 
1209437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops));
12106e15d496SJeremy L Thompson         *flops += restr_flops;
12112b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12126e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12136e15d496SJeremy L Thompson       }
12146e15d496SJeremy L Thompson     }
12156e15d496SJeremy L Thompson   }
12166e15d496SJeremy L Thompson 
12176e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12186e15d496SJeremy L Thompson }
12196e15d496SJeremy L Thompson 
12206e15d496SJeremy L Thompson /**
12210126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12229fd66db6SSebastian Grimberg 
1223512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1224512bb800SJeremy L Thompson 
12259fd66db6SSebastian Grimberg   Note: If the value of `ctx` passed into this function is non-NULL, then it is assumed that `ctx` is a pointer to a CeedQFunctionContext.
12269fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12270126412dSJeremy L Thompson 
1228859c15bbSJames Wright   @param[in]  op  CeedOperator
12290126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12300126412dSJeremy L Thompson 
12310126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12320126412dSJeremy L Thompson 
1233859c15bbSJames Wright   @ref Advanced
12340126412dSJeremy L Thompson **/
12350126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
12366574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12370126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12380126412dSJeremy L Thompson   else *ctx = NULL;
12390126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12400126412dSJeremy L Thompson }
12410126412dSJeremy L Thompson 
12420126412dSJeremy L Thompson /**
1243ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12443668ca4bSJeremy L Thompson 
1245859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1246859c15bbSJames Wright 
12473668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12483668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12493668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12503668ca4bSJeremy L Thompson 
12513668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12523668ca4bSJeremy L Thompson 
12533668ca4bSJeremy L Thompson   @ref User
12543668ca4bSJeremy L Thompson **/
125517b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12563668ca4bSJeremy L Thompson   bool is_composite;
12572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12582b730f8bSJeremy L Thompson 
12593668ca4bSJeremy L Thompson   if (is_composite) {
12603668ca4bSJeremy L Thompson     // Check if composite label already created
12613668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12623668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12633668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12643668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12653668ca4bSJeremy L Thompson       }
12663668ca4bSJeremy L Thompson     }
12673668ca4bSJeremy L Thompson 
12683668ca4bSJeremy L Thompson     // Create composite label if needed
12693668ca4bSJeremy L Thompson     CeedInt               num_sub;
12703668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12713668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12723668ca4bSJeremy L Thompson 
12732b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1274c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1275c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12762b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12773668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12783668ca4bSJeremy L Thompson 
12793668ca4bSJeremy L Thompson     bool label_found = false;
12803668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12813668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12823668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12832b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
12843668ca4bSJeremy L Thompson         if (new_field_label_i) {
12853668ca4bSJeremy L Thompson           label_found                    = true;
12863668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
12873668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
12883668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
12892b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
12907bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
12912b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
12922b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
12932b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
12947bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
12957bfe0f0eSJeremy L Thompson           } else {
12967bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
12977bfe0f0eSJeremy L Thompson           }
12982b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
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 number of values on sub-operator contexts. %ld != %ld",
13027bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13037bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13047bfe0f0eSJeremy L Thompson           } else {
13057bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13067bfe0f0eSJeremy L Thompson           }
13073668ca4bSJeremy L Thompson         }
13083668ca4bSJeremy L Thompson       }
13093668ca4bSJeremy L Thompson     }
13103668ca4bSJeremy L Thompson     if (!label_found) {
13113668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13122b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13132b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13143668ca4bSJeremy L Thompson       *field_label = NULL;
13153668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13163668ca4bSJeremy L Thompson     } else {
13175ac9af79SJeremy L Thompson       *field_label = new_field_label;
13185ac9af79SJeremy L Thompson     }
13195ac9af79SJeremy L Thompson   } else {
13205ac9af79SJeremy L Thompson     CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
13215ac9af79SJeremy L Thompson   }
13225ac9af79SJeremy L Thompson 
13235ac9af79SJeremy L Thompson   // Set label in operator
13245ac9af79SJeremy L Thompson   if (*field_label) {
13255ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13265ac9af79SJeremy L Thompson 
13273668ca4bSJeremy L Thompson     // Move new composite label to operator
13283668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13292b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13303668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13313668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13322b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13333668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13343668ca4bSJeremy L Thompson     }
13355ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13363668ca4bSJeremy L Thompson     op->num_context_labels++;
13373668ca4bSJeremy L Thompson   }
13383668ca4bSJeremy L Thompson 
13393668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13403668ca4bSJeremy L Thompson }
13413668ca4bSJeremy L Thompson 
13423668ca4bSJeremy L Thompson /**
13432788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13444385fb7fSSebastian Grimberg 
13452788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1346d8dd9a91SJeremy L Thompson 
1347ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13482788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1349ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1350d8dd9a91SJeremy L Thompson 
1351d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1352d8dd9a91SJeremy L Thompson 
1353d8dd9a91SJeremy L Thompson   @ref User
1354d8dd9a91SJeremy L Thompson **/
135517b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13562b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1357d8dd9a91SJeremy L Thompson }
1358d8dd9a91SJeremy L Thompson 
1359d8dd9a91SJeremy L Thompson /**
13602788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13614385fb7fSSebastian Grimberg 
13622788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13632788fa27SJeremy L Thompson 
13642788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13652788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13662788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13672788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13682788fa27SJeremy L Thompson 
13692788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13702788fa27SJeremy L Thompson 
13712788fa27SJeremy L Thompson   @ref User
13722788fa27SJeremy L Thompson **/
137317b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13742788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13752788fa27SJeremy L Thompson }
13762788fa27SJeremy L Thompson 
13772788fa27SJeremy L Thompson /**
13782788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
13792788fa27SJeremy L Thompson 
13802788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13812788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
13822788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13832788fa27SJeremy L Thompson 
13842788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13852788fa27SJeremy L Thompson 
13862788fa27SJeremy L Thompson   @ref User
13872788fa27SJeremy L Thompson **/
138817b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
13892788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
13902788fa27SJeremy L Thompson }
13912788fa27SJeremy L Thompson 
13922788fa27SJeremy L Thompson /**
13932788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
13944385fb7fSSebastian Grimberg 
13952788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1396d8dd9a91SJeremy L Thompson 
1397ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1398ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1399ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1400d8dd9a91SJeremy L Thompson 
1401d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1402d8dd9a91SJeremy L Thompson 
1403d8dd9a91SJeremy L Thompson   @ref User
1404d8dd9a91SJeremy L Thompson **/
140517b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14062b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1407d8dd9a91SJeremy L Thompson }
1408d8dd9a91SJeremy L Thompson 
1409d8dd9a91SJeremy L Thompson /**
14102788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14114385fb7fSSebastian Grimberg 
14122788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14132788fa27SJeremy L Thompson 
14142788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14152788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1416c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14172788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14182788fa27SJeremy L Thompson 
14192788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14202788fa27SJeremy L Thompson 
14212788fa27SJeremy L Thompson   @ref User
14222788fa27SJeremy L Thompson **/
142317b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14242788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14252788fa27SJeremy L Thompson }
14262788fa27SJeremy L Thompson 
14272788fa27SJeremy L Thompson /**
14282788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14292788fa27SJeremy L Thompson 
14302788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14312788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14322788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14332788fa27SJeremy L Thompson 
14342788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14352788fa27SJeremy L Thompson 
14362788fa27SJeremy L Thompson   @ref User
14372788fa27SJeremy L Thompson **/
143817b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14392788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14402788fa27SJeremy L Thompson }
14412788fa27SJeremy L Thompson 
14422788fa27SJeremy L Thompson /**
14433bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1444d7b241e6Sjeremylt 
1445ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1446ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1447d7b241e6Sjeremylt 
1448ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1449f04ea552SJeremy L Thompson 
1450ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1451ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14525ac9af79SJeremy 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
14535ac9af79SJeremy L Thompson active outputs
1454ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1455b11c1e72Sjeremylt 
1456b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1457dfdf5a53Sjeremylt 
14587a982d89SJeremy L. Thompson   @ref User
1459b11c1e72Sjeremylt **/
14602b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1462d7b241e6Sjeremylt 
14633ca2b39bSJeremy L Thompson   if (op->is_composite) {
1464250756a7Sjeremylt     // Composite Operator
1465250756a7Sjeremylt     if (op->ApplyComposite) {
14662b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1467250756a7Sjeremylt     } else {
1468d1d35e2fSjeremylt       CeedInt num_suboperators;
1469c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1470d1d35e2fSjeremylt       CeedOperator *sub_operators;
1471c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1472250756a7Sjeremylt 
1473250756a7Sjeremylt       // Zero all output vectors
14743ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1475d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1476d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1477d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1478250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
14792b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1480250756a7Sjeremylt           }
1481250756a7Sjeremylt         }
1482250756a7Sjeremylt       }
1483250756a7Sjeremylt       // Apply
1484d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
14852b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1486cae8b89aSjeremylt       }
1487cae8b89aSjeremylt     }
14883ca2b39bSJeremy L Thompson   } else {
14893ca2b39bSJeremy L Thompson     // Standard Operator
14903ca2b39bSJeremy L Thompson     if (op->Apply) {
14913ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
14923ca2b39bSJeremy L Thompson     } else {
14933ca2b39bSJeremy L Thompson       // Zero all output vectors
14943ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
14953ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
14963ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
14973ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
14983ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
14993ca2b39bSJeremy L Thompson       }
15003ca2b39bSJeremy L Thompson       // Apply
15013ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15023ca2b39bSJeremy L Thompson     }
1503250756a7Sjeremylt   }
1504e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1505cae8b89aSjeremylt }
1506cae8b89aSjeremylt 
1507cae8b89aSjeremylt /**
1508cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1509cae8b89aSjeremylt 
1510ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1511ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1512cae8b89aSjeremylt 
1513ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1514ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1515ea61e9acSJeremy 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
1516ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1517cae8b89aSjeremylt 
1518cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1519cae8b89aSjeremylt 
15207a982d89SJeremy L. Thompson   @ref User
1521cae8b89aSjeremylt **/
15222b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1524cae8b89aSjeremylt 
15253ca2b39bSJeremy L Thompson   if (op->is_composite) {
1526250756a7Sjeremylt     // Composite Operator
1527250756a7Sjeremylt     if (op->ApplyAddComposite) {
15282b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1529cae8b89aSjeremylt     } else {
1530d1d35e2fSjeremylt       CeedInt num_suboperators;
1531c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1532d1d35e2fSjeremylt       CeedOperator *sub_operators;
1533c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1534250756a7Sjeremylt 
1535d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15362b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15371d7d2407SJeremy L Thompson       }
1538250756a7Sjeremylt     }
15393ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
15403ca2b39bSJeremy L Thompson     // Standard Operator
15413ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1542250756a7Sjeremylt   }
1543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1544d7b241e6Sjeremylt }
1545d7b241e6Sjeremylt 
1546d7b241e6Sjeremylt /**
1547b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1548d7b241e6Sjeremylt 
1549ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1550b11c1e72Sjeremylt 
1551b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1552dfdf5a53Sjeremylt 
15537a982d89SJeremy L. Thompson   @ref User
1554b11c1e72Sjeremylt **/
1555d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1556ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1557ad6481ceSJeremy L Thompson     *op = NULL;
1558ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1559ad6481ceSJeremy L Thompson   }
15602b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15612b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1562fe2413ffSjeremylt   // Free fields
15632b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1564d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1565437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1566437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
156715910d16Sjeremylt       }
1568d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15692b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
157071352a93Sjeremylt       }
15712b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15722b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
157371352a93Sjeremylt       }
15742b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
15752b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1576fe2413ffSjeremylt     }
15772b730f8bSJeremy L Thompson   }
15782b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1579d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1580437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1581d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15822b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
158371352a93Sjeremylt       }
15842b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
15852b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
158671352a93Sjeremylt       }
15872b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
15882b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
15892b730f8bSJeremy L Thompson     }
1590fe2413ffSjeremylt   }
1591d1d35e2fSjeremylt   // Destroy sub_operators
15922b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1593d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
15942b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
159552d6035fSJeremy L Thompson     }
15962b730f8bSJeremy L Thompson   }
15972b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
15982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
15992b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16003668ca4bSJeremy L Thompson   // Destroy any composite labels
16015ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16023668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16032b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16042b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16053668ca4bSJeremy L Thompson     }
16065ac9af79SJeremy L Thompson   }
16072b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1608fe2413ffSjeremylt 
16095107b09fSJeremy L Thompson   // Destroy fallback
16102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16115107b09fSJeremy L Thompson 
1612ed9e99e6SJeremy L Thompson   // Destroy assembly data
16132b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16142b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
161570a7ffb3SJeremy L Thompson 
16162b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16182b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16192b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16202b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1621e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1622d7b241e6Sjeremylt }
1623d7b241e6Sjeremylt 
1624d7b241e6Sjeremylt /// @}
1625