xref: /libCEED/interface/ceed-operator.c (revision db002c03923317a1c3814dcd861330002c00a8ea) !
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.");
538*db002c03SJeremy L Thompson 
5392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
540*db002c03SJeremy 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;
544*db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
545*db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
546*db002c03SJeremy 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));
576*db002c03SJeremy 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 
583*db002c03SJeremy 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;
6432b730f8bSJeremy L Thompson   CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
6446574a04fSJeremy L Thompson   CeedCheck(b == CEED_BASIS_COLLOCATED || !op->num_qpts || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
6452b730f8bSJeremy L Thompson             "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts);
646d1d35e2fSjeremylt   CeedQFunctionField qf_field;
647d1d35e2fSjeremylt   CeedOperatorField *op_field;
6482b104005SJeremy L Thompson   bool               is_input = true;
649d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
650d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
651d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
652d1d35e2fSjeremylt       op_field = &op->input_fields[i];
653d7b241e6Sjeremylt       goto found;
654d7b241e6Sjeremylt     }
655d7b241e6Sjeremylt   }
6562b104005SJeremy L Thompson   is_input = false;
657d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
658d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
659d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
660d1d35e2fSjeremylt       op_field = &op->output_fields[i];
661d7b241e6Sjeremylt       goto found;
662d7b241e6Sjeremylt     }
663d7b241e6Sjeremylt   }
664c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6652b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
666c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
667d7b241e6Sjeremylt found:
6682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6692b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
670e15f9bd0SJeremy L Thompson 
6712b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6722b104005SJeremy L Thompson     CeedSize l_size;
6732b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6742b104005SJeremy L Thompson     if (is_input) {
6752b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
6766574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6776574a04fSJeremy L Thompson                 op->input_size);
6782b104005SJeremy L Thompson     } else {
6792b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
6806574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6816574a04fSJeremy L Thompson                 op->output_size);
6822b104005SJeremy L Thompson     }
6832b730f8bSJeremy L Thompson   }
6842b104005SJeremy L Thompson 
685393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
686393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
687e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
688d1d35e2fSjeremylt     op->num_elem        = num_elem;
689d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
690e15f9bd0SJeremy L Thompson   }
691393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
692393ac2cdSJeremy L Thompson   if (!op->num_qpts && b != CEED_BASIS_COLLOCATED) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
693e15f9bd0SJeremy L Thompson 
694d1d35e2fSjeremylt   op->num_fields += 1;
6952b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
696e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
697d7b241e6Sjeremylt }
698d7b241e6Sjeremylt 
699d7b241e6Sjeremylt /**
70043bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
70143bbe138SJeremy L Thompson 
702ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
703f04ea552SJeremy L Thompson 
704ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
705f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
70643bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
707f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
70843bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
70943bbe138SJeremy L Thompson 
71043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71143bbe138SJeremy L Thompson 
712e9b533fbSJeremy L Thompson   @ref Advanced
71343bbe138SJeremy L Thompson **/
7142b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
71543bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7166574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
71843bbe138SJeremy L Thompson 
71943bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
72043bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
72143bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
72243bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
72343bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72443bbe138SJeremy L Thompson }
72543bbe138SJeremy L Thompson 
72643bbe138SJeremy L Thompson /**
727de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
728de5900adSJames Wright 
729de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
730de5900adSJames Wright 
731de5900adSJames Wright   @param[in]  op         CeedOperator
732de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
733de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
734de5900adSJames Wright 
735de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
736de5900adSJames Wright 
737de5900adSJames Wright   @ref Advanced
738de5900adSJames Wright **/
739de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
740de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
741de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
742de5900adSJames Wright   char              *name;
743de5900adSJames Wright   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
744de5900adSJames Wright 
745de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
746de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
747de5900adSJames Wright     if (!strcmp(name, field_name)) {
748de5900adSJames Wright       *op_field = input_fields[i];
749de5900adSJames Wright       return CEED_ERROR_SUCCESS;
750de5900adSJames Wright     }
751de5900adSJames Wright   }
752de5900adSJames Wright 
753de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
754de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
755de5900adSJames Wright     if (!strcmp(name, field_name)) {
756de5900adSJames Wright       *op_field = output_fields[i];
757de5900adSJames Wright       return CEED_ERROR_SUCCESS;
758de5900adSJames Wright     }
759de5900adSJames Wright   }
760de5900adSJames Wright 
761de5900adSJames Wright   bool has_name = op->name;
762de5900adSJames Wright   // LCOV_EXCL_START
763de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
764de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
765de5900adSJames Wright   // LCOV_EXCL_STOP
766de5900adSJames Wright }
767de5900adSJames Wright 
768de5900adSJames Wright /**
76928567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
77028567f8fSJeremy L Thompson 
771ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
77228567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
77328567f8fSJeremy L Thompson 
77428567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
77528567f8fSJeremy L Thompson 
776e9b533fbSJeremy L Thompson   @ref Advanced
77728567f8fSJeremy L Thompson **/
77828567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
77928567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
78028567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78128567f8fSJeremy L Thompson }
78228567f8fSJeremy L Thompson 
78328567f8fSJeremy L Thompson /**
78443bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
78543bbe138SJeremy L Thompson 
786ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
78743bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
78843bbe138SJeremy L Thompson 
78943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79043bbe138SJeremy L Thompson 
791e9b533fbSJeremy L Thompson   @ref Advanced
79243bbe138SJeremy L Thompson **/
7932b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
794437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
79543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79643bbe138SJeremy L Thompson }
79743bbe138SJeremy L Thompson 
79843bbe138SJeremy L Thompson /**
79943bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
80043bbe138SJeremy L Thompson 
801ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
80243bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
80343bbe138SJeremy L Thompson 
80443bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80543bbe138SJeremy L Thompson 
806e9b533fbSJeremy L Thompson   @ref Advanced
80743bbe138SJeremy L Thompson **/
80843bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
80943bbe138SJeremy L Thompson   *basis = op_field->basis;
81043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81143bbe138SJeremy L Thompson }
81243bbe138SJeremy L Thompson 
81343bbe138SJeremy L Thompson /**
81443bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
81543bbe138SJeremy L Thompson 
816ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
81743bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
81843bbe138SJeremy L Thompson 
81943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82043bbe138SJeremy L Thompson 
821e9b533fbSJeremy L Thompson   @ref Advanced
82243bbe138SJeremy L Thompson **/
82343bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
82443bbe138SJeremy L Thompson   *vec = op_field->vec;
82543bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82643bbe138SJeremy L Thompson }
82743bbe138SJeremy L Thompson 
82843bbe138SJeremy L Thompson /**
82952d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
830288c0443SJeremy L Thompson 
831ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
832ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
83352d6035fSJeremy L Thompson 
83452d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83552d6035fSJeremy L Thompson 
8367a982d89SJeremy L. Thompson   @ref User
83752d6035fSJeremy L Thompson  */
8382b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
8396574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
8406574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
8416574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8422b730f8bSJeremy L Thompson 
8432b730f8bSJeremy L Thompson   {
8442b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8452b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8462b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8472b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8482b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
8496574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
8506574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8512b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8522b730f8bSJeremy L Thompson               "shape (%td, %td)",
8532b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8542b730f8bSJeremy L Thompson   }
8552b730f8bSJeremy L Thompson 
856d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
858d1d35e2fSjeremylt   composite_op->num_suboperators++;
859e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86052d6035fSJeremy L Thompson }
86152d6035fSJeremy L Thompson 
86252d6035fSJeremy L Thompson /**
86375f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
86475f0d5a4SJeremy L Thompson 
86575f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
86675f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
86775f0d5a4SJeremy L Thompson 
86875f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86975f0d5a4SJeremy L Thompson 
87075f0d5a4SJeremy L Thompson   @ref Backend
87175f0d5a4SJeremy L Thompson **/
872c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
8736574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
87475f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
87575f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87675f0d5a4SJeremy L Thompson }
87775f0d5a4SJeremy L Thompson 
87875f0d5a4SJeremy L Thompson /**
87975f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
88075f0d5a4SJeremy L Thompson 
88175f0d5a4SJeremy L Thompson   @param op                  CeedOperator
88275f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
88375f0d5a4SJeremy L Thompson 
88475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88575f0d5a4SJeremy L Thompson 
88675f0d5a4SJeremy L Thompson   @ref Backend
88775f0d5a4SJeremy L Thompson **/
888c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
8896574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
89075f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
89175f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89275f0d5a4SJeremy L Thompson }
89375f0d5a4SJeremy L Thompson 
89475f0d5a4SJeremy L Thompson /**
8954db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
8964db537f9SJeremy L Thompson 
8974db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
8984db537f9SJeremy L Thompson 
8994db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9004db537f9SJeremy L Thompson 
9014db537f9SJeremy L Thompson   @ref User
9024db537f9SJeremy L Thompson **/
9034db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9044db537f9SJeremy L Thompson   Ceed ceed;
9052b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9064db537f9SJeremy L Thompson 
9072b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9084db537f9SJeremy L Thompson 
9094db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9104db537f9SJeremy L Thompson   if (op->is_composite) {
91143622462SJeremy L Thompson     if (!op->num_suboperators) {
91243622462SJeremy L Thompson       // Empty operator setup
91343622462SJeremy L Thompson       op->input_size  = 0;
91443622462SJeremy L Thompson       op->output_size = 0;
91543622462SJeremy L Thompson     } else {
9164db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9172b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9184db537f9SJeremy L Thompson       }
9192b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9202b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9212b104005SJeremy L Thompson       CeedSize input_size, output_size;
9222b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
92343622462SJeremy L Thompson     }
9244db537f9SJeremy L Thompson   } else {
9256574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9266574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9276574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9286574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9296574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9302b730f8bSJeremy L Thompson   }
9314db537f9SJeremy L Thompson 
9324db537f9SJeremy L Thompson   // Flag as immutable and ready
9334db537f9SJeremy L Thompson   op->is_interface_setup = true;
9342b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9352b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9362b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9374db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9384db537f9SJeremy L Thompson }
9394db537f9SJeremy L Thompson 
9404db537f9SJeremy L Thompson /**
941c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
9424385fb7fSSebastian Grimberg 
943ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
944c9366a6bSJeremy L Thompson 
945c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
946c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
947c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
948c9366a6bSJeremy L Thompson 
949c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
950c9366a6bSJeremy L Thompson 
951c9366a6bSJeremy L Thompson   @ref User
952c9366a6bSJeremy L Thompson **/
9532b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
954c9366a6bSJeremy L Thompson   bool is_composite;
955c9366a6bSJeremy L Thompson 
9562b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9572b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
958c9366a6bSJeremy L Thompson 
9592b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9602b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
961c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
962c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9632b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9642b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9652b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9662b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
9676574a04fSJeremy 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,
9686574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9692b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9702b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9712b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
972c9366a6bSJeremy L Thompson     }
9732b730f8bSJeremy L Thompson   }
974c9366a6bSJeremy L Thompson 
975c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
976c9366a6bSJeremy L Thompson }
977c9366a6bSJeremy L Thompson 
978c9366a6bSJeremy L Thompson /**
979beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
9804385fb7fSSebastian Grimberg 
981ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
9829fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
9839fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
9849fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9858b919e6bSJeremy L Thompson 
986beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
987beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9888b919e6bSJeremy L Thompson 
9898b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9908b919e6bSJeremy L Thompson 
9918b919e6bSJeremy L Thompson   @ref Advanced
9928b919e6bSJeremy L Thompson **/
9932b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
9948b919e6bSJeremy L Thompson   bool is_composite;
9958b919e6bSJeremy L Thompson 
9962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9978b919e6bSJeremy L Thompson   if (is_composite) {
9988b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
9992b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10008b919e6bSJeremy L Thompson     }
10018b919e6bSJeremy L Thompson   } else {
10022b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1003beecbf24SJeremy L Thompson   }
1004beecbf24SJeremy L Thompson 
1005beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1006beecbf24SJeremy L Thompson }
1007beecbf24SJeremy L Thompson 
1008beecbf24SJeremy L Thompson /**
1009beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1010beecbf24SJeremy L Thompson 
1011beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10126e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1013beecbf24SJeremy L Thompson 
1014beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1015beecbf24SJeremy L Thompson 
1016beecbf24SJeremy L Thompson   @ref Advanced
1017beecbf24SJeremy L Thompson **/
10182b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1019beecbf24SJeremy L Thompson   bool is_composite;
1020beecbf24SJeremy L Thompson 
10212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1022beecbf24SJeremy L Thompson   if (is_composite) {
1023beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10242b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1025beecbf24SJeremy L Thompson     }
1026beecbf24SJeremy L Thompson   } else {
10272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10288b919e6bSJeremy L Thompson   }
10298b919e6bSJeremy L Thompson 
10308b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10318b919e6bSJeremy L Thompson }
10328b919e6bSJeremy L Thompson 
10338b919e6bSJeremy L Thompson /**
1034cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
10354385fb7fSSebastian Grimberg 
1036ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1037ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1038cd4dfc48Sjeremylt 
1039ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1040ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1041cd4dfc48Sjeremylt 
1042cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1043cd4dfc48Sjeremylt 
1044e9b533fbSJeremy L Thompson   @ref Advanced
1045cd4dfc48Sjeremylt **/
1046cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
10476574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
10486574a04fSJeremy L Thompson   CeedCheck(op->num_qpts == 0, op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
10496574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1050cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1051cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1052cd4dfc48Sjeremylt }
1053cd4dfc48Sjeremylt 
1054cd4dfc48Sjeremylt /**
1055ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1056ea6b5821SJeremy L Thompson 
1057ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1058ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1059ea6b5821SJeremy L Thompson 
1060ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1061ea6b5821SJeremy L Thompson 
1062ea6b5821SJeremy L Thompson   @ref User
1063ea6b5821SJeremy L Thompson **/
1064ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1065ea6b5821SJeremy L Thompson   char  *name_copy;
1066ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1067ea6b5821SJeremy L Thompson 
10682b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1069ea6b5821SJeremy L Thompson   if (name_len > 0) {
10702b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1071ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1072ea6b5821SJeremy L Thompson     op->name = name_copy;
1073ea6b5821SJeremy L Thompson   }
1074ea6b5821SJeremy L Thompson 
1075ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1076ea6b5821SJeremy L Thompson }
1077ea6b5821SJeremy L Thompson 
1078ea6b5821SJeremy L Thompson /**
10797a982d89SJeremy L. Thompson   @brief View a CeedOperator
10807a982d89SJeremy L. Thompson 
10817a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
10827a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
10837a982d89SJeremy L. Thompson 
10847a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
10857a982d89SJeremy L. Thompson 
10867a982d89SJeremy L. Thompson   @ref User
10877a982d89SJeremy L. Thompson **/
10887a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1089ea6b5821SJeremy L Thompson   bool has_name = op->name;
10907a982d89SJeremy L. Thompson 
1091f04ea552SJeremy L Thompson   if (op->is_composite) {
10922b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
10937a982d89SJeremy L. Thompson 
1094d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1095ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
10962b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
10972b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
10987a982d89SJeremy L. Thompson     }
10997a982d89SJeremy L. Thompson   } else {
11002b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11012b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11027a982d89SJeremy L. Thompson   }
1103e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11047a982d89SJeremy L. Thompson }
11053bd813ffSjeremylt 
11063bd813ffSjeremylt /**
1107b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1108b7c9bbdaSJeremy L Thompson 
1109ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1110b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1111b7c9bbdaSJeremy L Thompson 
1112b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1113b7c9bbdaSJeremy L Thompson 
1114b7c9bbdaSJeremy L Thompson   @ref Advanced
1115b7c9bbdaSJeremy L Thompson **/
1116b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1117b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1118b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1119b7c9bbdaSJeremy L Thompson }
1120b7c9bbdaSJeremy L Thompson 
1121b7c9bbdaSJeremy L Thompson /**
1122b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1123b7c9bbdaSJeremy L Thompson 
1124ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1125b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1126b7c9bbdaSJeremy L Thompson 
1127b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1128b7c9bbdaSJeremy L Thompson 
1129b7c9bbdaSJeremy L Thompson   @ref Advanced
1130b7c9bbdaSJeremy L Thompson **/
1131b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
11326574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1133b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1134b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1135b7c9bbdaSJeremy L Thompson }
1136b7c9bbdaSJeremy L Thompson 
1137b7c9bbdaSJeremy L Thompson /**
1138b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1139b7c9bbdaSJeremy L Thompson 
1140ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1141b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1142b7c9bbdaSJeremy L Thompson 
1143b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1144b7c9bbdaSJeremy L Thompson 
1145b7c9bbdaSJeremy L Thompson   @ref Advanced
1146b7c9bbdaSJeremy L Thompson **/
1147b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
11486574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1149b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1150b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1151b7c9bbdaSJeremy L Thompson }
1152b7c9bbdaSJeremy L Thompson 
1153b7c9bbdaSJeremy L Thompson /**
11546e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
11556e15d496SJeremy L Thompson 
1156ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1157ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
11586e15d496SJeremy L Thompson 
11596e15d496SJeremy L Thompson   @ref Backend
11606e15d496SJeremy L Thompson **/
11619d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
11626e15d496SJeremy L Thompson   bool is_composite;
11632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11646e15d496SJeremy L Thompson 
11656e15d496SJeremy L Thompson   *flops = 0;
11662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11676e15d496SJeremy L Thompson   if (is_composite) {
11686e15d496SJeremy L Thompson     CeedInt num_suboperators;
1169c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11706e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1171c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11726e15d496SJeremy L Thompson 
11736e15d496SJeremy L Thompson     // FLOPs for each suboperator
11746e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11759d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11762b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11776e15d496SJeremy L Thompson       *flops += suboperator_flops;
11786e15d496SJeremy L Thompson     }
11796e15d496SJeremy L Thompson   } else {
11806e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
11816e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11822b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11836e15d496SJeremy L Thompson     CeedInt num_elem = 0;
11842b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11854385fb7fSSebastian Grimberg 
11866e15d496SJeremy L Thompson     // Input FLOPs
11876e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11886e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
11899d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
11906e15d496SJeremy L Thompson 
1191437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops));
11926e15d496SJeremy L Thompson         *flops += restr_flops;
11932b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
11946e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
11956e15d496SJeremy L Thompson       }
11966e15d496SJeremy L Thompson     }
11976e15d496SJeremy L Thompson     // QF FLOPs
11989d36ca50SJeremy L Thompson     CeedInt  num_qpts;
11999d36ca50SJeremy L Thompson     CeedSize qf_flops;
12002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12012b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12026e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
12036e15d496SJeremy L Thompson     // Output FLOPs
12046e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12056e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
12069d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
12076e15d496SJeremy L Thompson 
1208437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops));
12096e15d496SJeremy L Thompson         *flops += restr_flops;
12102b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12116e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12126e15d496SJeremy L Thompson       }
12136e15d496SJeremy L Thompson     }
12146e15d496SJeremy L Thompson   }
12156e15d496SJeremy L Thompson 
12166e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12176e15d496SJeremy L Thompson }
12186e15d496SJeremy L Thompson 
12196e15d496SJeremy L Thompson /**
12200126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12219fd66db6SSebastian Grimberg 
1222512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1223512bb800SJeremy L Thompson 
12249fd66db6SSebastian 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.
12259fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12260126412dSJeremy L Thompson 
1227859c15bbSJames Wright   @param[in]  op  CeedOperator
12280126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12290126412dSJeremy L Thompson 
12300126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12310126412dSJeremy L Thompson 
1232859c15bbSJames Wright   @ref Advanced
12330126412dSJeremy L Thompson **/
12340126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
12356574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12360126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12370126412dSJeremy L Thompson   else *ctx = NULL;
12380126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12390126412dSJeremy L Thompson }
12400126412dSJeremy L Thompson 
12410126412dSJeremy L Thompson /**
1242ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12433668ca4bSJeremy L Thompson 
1244859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1245859c15bbSJames Wright 
12463668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12473668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12483668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12493668ca4bSJeremy L Thompson 
12503668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12513668ca4bSJeremy L Thompson 
12523668ca4bSJeremy L Thompson   @ref User
12533668ca4bSJeremy L Thompson **/
125417b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12553668ca4bSJeremy L Thompson   bool is_composite;
12562b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12572b730f8bSJeremy L Thompson 
12583668ca4bSJeremy L Thompson   if (is_composite) {
12593668ca4bSJeremy L Thompson     // Check if composite label already created
12603668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12613668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12623668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12633668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12643668ca4bSJeremy L Thompson       }
12653668ca4bSJeremy L Thompson     }
12663668ca4bSJeremy L Thompson 
12673668ca4bSJeremy L Thompson     // Create composite label if needed
12683668ca4bSJeremy L Thompson     CeedInt               num_sub;
12693668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12703668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12713668ca4bSJeremy L Thompson 
12722b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1273c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1274c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12752b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12763668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12773668ca4bSJeremy L Thompson 
12783668ca4bSJeremy L Thompson     bool label_found = false;
12793668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12803668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12813668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12822b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
12833668ca4bSJeremy L Thompson         if (new_field_label_i) {
12843668ca4bSJeremy L Thompson           label_found                    = true;
12853668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
12863668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
12873668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
12882b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
12897bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
12902b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
12912b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
12922b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
12937bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
12947bfe0f0eSJeremy L Thompson           } else {
12957bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
12967bfe0f0eSJeremy L Thompson           }
12972b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
12987bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
12992b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13002b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13017bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13027bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13037bfe0f0eSJeremy L Thompson           } else {
13047bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13057bfe0f0eSJeremy L Thompson           }
13063668ca4bSJeremy L Thompson         }
13073668ca4bSJeremy L Thompson       }
13083668ca4bSJeremy L Thompson     }
13093668ca4bSJeremy L Thompson     if (!label_found) {
13103668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13112b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13122b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13133668ca4bSJeremy L Thompson       *field_label = NULL;
13143668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13153668ca4bSJeremy L Thompson     } else {
13165ac9af79SJeremy L Thompson       *field_label = new_field_label;
13175ac9af79SJeremy L Thompson     }
13185ac9af79SJeremy L Thompson   } else {
13195ac9af79SJeremy L Thompson     CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
13205ac9af79SJeremy L Thompson   }
13215ac9af79SJeremy L Thompson 
13225ac9af79SJeremy L Thompson   // Set label in operator
13235ac9af79SJeremy L Thompson   if (*field_label) {
13245ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13255ac9af79SJeremy L Thompson 
13263668ca4bSJeremy L Thompson     // Move new composite label to operator
13273668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13282b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13293668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13303668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13312b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13323668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13333668ca4bSJeremy L Thompson     }
13345ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13353668ca4bSJeremy L Thompson     op->num_context_labels++;
13363668ca4bSJeremy L Thompson   }
13373668ca4bSJeremy L Thompson 
13383668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13393668ca4bSJeremy L Thompson }
13403668ca4bSJeremy L Thompson 
13413668ca4bSJeremy L Thompson /**
13422788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13434385fb7fSSebastian Grimberg 
13442788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1345d8dd9a91SJeremy L Thompson 
1346ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13472788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1348ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1349d8dd9a91SJeremy L Thompson 
1350d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1351d8dd9a91SJeremy L Thompson 
1352d8dd9a91SJeremy L Thompson   @ref User
1353d8dd9a91SJeremy L Thompson **/
135417b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13552b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1356d8dd9a91SJeremy L Thompson }
1357d8dd9a91SJeremy L Thompson 
1358d8dd9a91SJeremy L Thompson /**
13592788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13604385fb7fSSebastian Grimberg 
13612788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13622788fa27SJeremy L Thompson 
13632788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13642788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13652788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13662788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13672788fa27SJeremy L Thompson 
13682788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13692788fa27SJeremy L Thompson 
13702788fa27SJeremy L Thompson   @ref User
13712788fa27SJeremy L Thompson **/
137217b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13732788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13742788fa27SJeremy L Thompson }
13752788fa27SJeremy L Thompson 
13762788fa27SJeremy L Thompson /**
13772788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
13782788fa27SJeremy L Thompson 
13792788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13802788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
13812788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13822788fa27SJeremy L Thompson 
13832788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13842788fa27SJeremy L Thompson 
13852788fa27SJeremy L Thompson   @ref User
13862788fa27SJeremy L Thompson **/
138717b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
13882788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
13892788fa27SJeremy L Thompson }
13902788fa27SJeremy L Thompson 
13912788fa27SJeremy L Thompson /**
13922788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
13934385fb7fSSebastian Grimberg 
13942788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1395d8dd9a91SJeremy L Thompson 
1396ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1397ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1398ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1399d8dd9a91SJeremy L Thompson 
1400d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1401d8dd9a91SJeremy L Thompson 
1402d8dd9a91SJeremy L Thompson   @ref User
1403d8dd9a91SJeremy L Thompson **/
140417b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14052b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1406d8dd9a91SJeremy L Thompson }
1407d8dd9a91SJeremy L Thompson 
1408d8dd9a91SJeremy L Thompson /**
14092788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14104385fb7fSSebastian Grimberg 
14112788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14122788fa27SJeremy L Thompson 
14132788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14142788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1415c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14162788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14172788fa27SJeremy L Thompson 
14182788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14192788fa27SJeremy L Thompson 
14202788fa27SJeremy L Thompson   @ref User
14212788fa27SJeremy L Thompson **/
142217b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14232788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14242788fa27SJeremy L Thompson }
14252788fa27SJeremy L Thompson 
14262788fa27SJeremy L Thompson /**
14272788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14282788fa27SJeremy L Thompson 
14292788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14302788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14312788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14322788fa27SJeremy L Thompson 
14332788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14342788fa27SJeremy L Thompson 
14352788fa27SJeremy L Thompson   @ref User
14362788fa27SJeremy L Thompson **/
143717b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14382788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14392788fa27SJeremy L Thompson }
14402788fa27SJeremy L Thompson 
14412788fa27SJeremy L Thompson /**
14423bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1443d7b241e6Sjeremylt 
1444ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1445ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1446d7b241e6Sjeremylt 
1447ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1448f04ea552SJeremy L Thompson 
1449ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1450ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14515ac9af79SJeremy 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
14525ac9af79SJeremy L Thompson active outputs
1453ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1454b11c1e72Sjeremylt 
1455b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1456dfdf5a53Sjeremylt 
14577a982d89SJeremy L. Thompson   @ref User
1458b11c1e72Sjeremylt **/
14592b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1461d7b241e6Sjeremylt 
14623ca2b39bSJeremy L Thompson   if (op->is_composite) {
1463250756a7Sjeremylt     // Composite Operator
1464250756a7Sjeremylt     if (op->ApplyComposite) {
14652b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1466250756a7Sjeremylt     } else {
1467d1d35e2fSjeremylt       CeedInt num_suboperators;
1468c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1469d1d35e2fSjeremylt       CeedOperator *sub_operators;
1470c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1471250756a7Sjeremylt 
1472250756a7Sjeremylt       // Zero all output vectors
14733ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1474d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1475d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1476d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1477250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
14782b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1479250756a7Sjeremylt           }
1480250756a7Sjeremylt         }
1481250756a7Sjeremylt       }
1482250756a7Sjeremylt       // Apply
1483d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
14842b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1485cae8b89aSjeremylt       }
1486cae8b89aSjeremylt     }
14873ca2b39bSJeremy L Thompson   } else {
14883ca2b39bSJeremy L Thompson     // Standard Operator
14893ca2b39bSJeremy L Thompson     if (op->Apply) {
14903ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
14913ca2b39bSJeremy L Thompson     } else {
14923ca2b39bSJeremy L Thompson       // Zero all output vectors
14933ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
14943ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
14953ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
14963ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
14973ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
14983ca2b39bSJeremy L Thompson       }
14993ca2b39bSJeremy L Thompson       // Apply
15003ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15013ca2b39bSJeremy L Thompson     }
1502250756a7Sjeremylt   }
1503e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1504cae8b89aSjeremylt }
1505cae8b89aSjeremylt 
1506cae8b89aSjeremylt /**
1507cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1508cae8b89aSjeremylt 
1509ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1510ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1511cae8b89aSjeremylt 
1512ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1513ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1514ea61e9acSJeremy 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
1515ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1516cae8b89aSjeremylt 
1517cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1518cae8b89aSjeremylt 
15197a982d89SJeremy L. Thompson   @ref User
1520cae8b89aSjeremylt **/
15212b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1523cae8b89aSjeremylt 
15243ca2b39bSJeremy L Thompson   if (op->is_composite) {
1525250756a7Sjeremylt     // Composite Operator
1526250756a7Sjeremylt     if (op->ApplyAddComposite) {
15272b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1528cae8b89aSjeremylt     } else {
1529d1d35e2fSjeremylt       CeedInt num_suboperators;
1530c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1531d1d35e2fSjeremylt       CeedOperator *sub_operators;
1532c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1533250756a7Sjeremylt 
1534d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15352b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15361d7d2407SJeremy L Thompson       }
1537250756a7Sjeremylt     }
15383ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
15393ca2b39bSJeremy L Thompson     // Standard Operator
15403ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1541250756a7Sjeremylt   }
1542e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1543d7b241e6Sjeremylt }
1544d7b241e6Sjeremylt 
1545d7b241e6Sjeremylt /**
1546b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1547d7b241e6Sjeremylt 
1548ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1549b11c1e72Sjeremylt 
1550b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1551dfdf5a53Sjeremylt 
15527a982d89SJeremy L. Thompson   @ref User
1553b11c1e72Sjeremylt **/
1554d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1555ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1556ad6481ceSJeremy L Thompson     *op = NULL;
1557ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1558ad6481ceSJeremy L Thompson   }
15592b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15602b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1561fe2413ffSjeremylt   // Free fields
15622b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1563d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1564437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1565437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
156615910d16Sjeremylt       }
1567d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15682b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
156971352a93Sjeremylt       }
15702b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15712b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
157271352a93Sjeremylt       }
15732b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
15742b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1575fe2413ffSjeremylt     }
15762b730f8bSJeremy L Thompson   }
15772b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1578d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1579437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1580d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15812b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
158271352a93Sjeremylt       }
15832b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
15842b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
158571352a93Sjeremylt       }
15862b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
15872b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
15882b730f8bSJeremy L Thompson     }
1589fe2413ffSjeremylt   }
1590d1d35e2fSjeremylt   // Destroy sub_operators
15912b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1592d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
15932b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
159452d6035fSJeremy L Thompson     }
15952b730f8bSJeremy L Thompson   }
15962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
15972b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
15982b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
15993668ca4bSJeremy L Thompson   // Destroy any composite labels
16005ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16013668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16022b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16032b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16043668ca4bSJeremy L Thompson     }
16055ac9af79SJeremy L Thompson   }
16062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1607fe2413ffSjeremylt 
16085107b09fSJeremy L Thompson   // Destroy fallback
16092b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16105107b09fSJeremy L Thompson 
1611ed9e99e6SJeremy L Thompson   // Destroy assembly data
16122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16132b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
161470a7ffb3SJeremy L Thompson 
16152b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16162b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16182b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16192b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1620e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1621d7b241e6Sjeremylt }
1622d7b241e6Sjeremylt 
1623d7b241e6Sjeremylt /// @}
1624