xref: /libCEED/interface/ceed-operator.c (revision 528a22edf3a453934d97bb72cb8bcdb514cb8007)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25e15f9bd0SJeremy L Thompson   @brief Check if a CeedOperator Field matches the QFunction Field
26e15f9bd0SJeremy L Thompson 
27e15f9bd0SJeremy L Thompson   @param[in] ceed     Ceed object for error handling
28d1d35e2fSjeremylt   @param[in] qf_field QFunction Field matching Operator Field
29e15f9bd0SJeremy L Thompson   @param[in] r        Operator Field ElemRestriction
30e15f9bd0SJeremy L Thompson   @param[in] b        Operator Field Basis
31e15f9bd0SJeremy L Thompson 
32e15f9bd0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33e15f9bd0SJeremy L Thompson 
34e15f9bd0SJeremy L Thompson   @ref Developer
35e15f9bd0SJeremy L Thompson **/
362b730f8bSJeremy L Thompson static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction r, CeedBasis b) {
37d1d35e2fSjeremylt   CeedEvalMode eval_mode = qf_field->eval_mode;
38c4e3f59bSSebastian Grimberg   CeedInt      dim = 1, num_comp = 1, q_comp = 1, restr_num_comp = 1, size = qf_field->size;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
416574a04fSJeremy L Thompson   CeedCheck((r == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
426574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
442b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
476574a04fSJeremy L Thompson   CeedCheck((b == CEED_BASIS_COLLOCATED) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
486574a04fSJeremy L Thompson             "CEED_BASIS_COLLOCATED and CEED_EVAL_NONE must be used together.");
49e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
502b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
52c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp));
536574a04fSJeremy L Thompson     CeedCheck(r == CEED_ELEMRESTRICTION_NONE || restr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
546574a04fSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components, but Basis has %" CeedInt_FMT
556574a04fSJeremy L Thompson               " components",
562b730f8bSJeremy L Thompson               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp);
57e15f9bd0SJeremy L Thompson   }
58e15f9bd0SJeremy L Thompson   // Field size
59d1d35e2fSjeremylt   switch (eval_mode) {
60e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
616574a04fSJeremy L Thompson       CeedCheck(size == restr_num_comp, ceed, CEED_ERROR_DIMENSION,
62953dad27SJames Wright                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
632b730f8bSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp);
64e15f9bd0SJeremy L Thompson       break;
65e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
66c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
67c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
68c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
696574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
706574a04fSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components", qf_field->field_name,
716574a04fSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72e15f9bd0SJeremy L Thompson       break;
73e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
74d1d35e2fSjeremylt       // No additional checks required
75e15f9bd0SJeremy L Thompson       break;
76e15f9bd0SJeremy L Thompson   }
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
817a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
827a982d89SJeremy L. Thompson 
837a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
84d1d35e2fSjeremylt   @param[in] qf_field     QFunction field (carries field name)
85d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
864c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
887a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @ref Utility
937a982d89SJeremy L. Thompson **/
942b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
957a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
96d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
977a982d89SJeremy L. Thompson 
982b730f8bSJeremy L Thompson   fprintf(stream,
992b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1002b730f8bSJeremy L Thompson           ":\n"
1017a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
102d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
1037a982d89SJeremy L. Thompson 
104990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
105f5ebfb90SJeremy L Thompson 
1062b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
107f5ebfb90SJeremy L Thompson 
1082b730f8bSJeremy L Thompson   if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s      Collocated basis\n", pre);
1097a982d89SJeremy L. Thompson 
1102b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1112b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
112f5ebfb90SJeremy L Thompson 
113e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1147a982d89SJeremy L. Thompson }
1157a982d89SJeremy L. Thompson 
1167a982d89SJeremy L. Thompson /**
1177a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1187a982d89SJeremy L. Thompson 
1197a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1207a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1217a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
1227a982d89SJeremy L. Thompson 
1237a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1247a982d89SJeremy L. Thompson 
1257a982d89SJeremy L. Thompson   @ref Utility
1267a982d89SJeremy L. Thompson **/
1277a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1287a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1297a982d89SJeremy L. Thompson 
130381e6593SJeremy L Thompson   CeedInt num_elem, num_qpts;
1312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
133381e6593SJeremy L Thompson 
13478464608Sjeremylt   CeedInt total_fields = 0;
1352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1362b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1377a982d89SJeremy L. Thompson 
1382b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1397a982d89SJeremy L. Thompson 
1402b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
141d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1422b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1437a982d89SJeremy L. Thompson   }
1447a982d89SJeremy L. Thompson 
1452b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
146d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1487a982d89SJeremy L. Thompson   }
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1507a982d89SJeremy L. Thompson }
1517a982d89SJeremy L. Thompson 
152d99fa3c5SJeremy L Thompson /**
1530f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
154eaf62fffSJeremy L Thompson 
155eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
1560f60e0a8SJeremy L Thompson   @param[out] active_basis Basis for active input vector or NULL for composite operator
157eaf62fffSJeremy L Thompson 
158eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
159eaf62fffSJeremy L Thompson 
160eaf62fffSJeremy L Thompson   @ref Developer
161eaf62fffSJeremy L Thompson **/
162eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
1636aaed0edSJeremy L Thompson   Ceed ceed;
1646aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1656aaed0edSJeremy L Thompson 
166eaf62fffSJeremy L Thompson   *active_basis = NULL;
1670f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1682b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
169eaf62fffSJeremy L Thompson     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1706574a04fSJeremy L Thompson       CeedCheck(!*active_basis || *active_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found");
171eaf62fffSJeremy L Thompson       *active_basis = op->input_fields[i]->basis;
172eaf62fffSJeremy L Thompson     }
1732b730f8bSJeremy L Thompson   }
174eaf62fffSJeremy L Thompson 
1756574a04fSJeremy L Thompson   CeedCheck(*active_basis, ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found");
176eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
177eaf62fffSJeremy L Thompson }
178eaf62fffSJeremy L Thompson 
179eaf62fffSJeremy L Thompson /**
1800f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
181e2f04181SAndrew T. Barker 
182e2f04181SAndrew T. Barker   @param[in] op           CeedOperator to find active basis for
1830f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator
184e2f04181SAndrew T. Barker 
185e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
186e2f04181SAndrew T. Barker 
187e2f04181SAndrew T. Barker   @ref Utility
188e2f04181SAndrew T. Barker **/
1892b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
1906aaed0edSJeremy L Thompson   Ceed ceed;
1916aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1926aaed0edSJeremy L Thompson 
193d1d35e2fSjeremylt   *active_rstr = NULL;
1940f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1952b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
196d1d35e2fSjeremylt     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1976574a04fSJeremy L Thompson       CeedCheck(!*active_rstr || *active_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
1986574a04fSJeremy L Thompson                 "Multiple active CeedElemRestrictions found");
199437c7c90SJeremy L Thompson       *active_rstr = op->input_fields[i]->elem_rstr;
200e2f04181SAndrew T. Barker     }
2012b730f8bSJeremy L Thompson   }
202e2f04181SAndrew T. Barker 
2036574a04fSJeremy L Thompson   CeedCheck(*active_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found");
204e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
205e2f04181SAndrew T. Barker }
206e2f04181SAndrew T. Barker 
207d8dd9a91SJeremy L Thompson /**
2082788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
2094385fb7fSSebastian Grimberg 
210ea61e9acSJeremy L Thompson   For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
2119fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
2129fd66db6SSebastian Grimberg any field of a matching type.
213d8dd9a91SJeremy L Thompson 
214ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
215ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
216ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2172788fa27SJeremy L Thompson   @param[in]     values      Values to set
218d8dd9a91SJeremy L Thompson 
219d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
220d8dd9a91SJeremy L Thompson 
221d8dd9a91SJeremy L Thompson   @ref User
222d8dd9a91SJeremy L Thompson **/
2232788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2246574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2253668ca4bSJeremy L Thompson 
2265ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2275ac9af79SJeremy L Thompson   if (field_label->from_op) {
2285ac9af79SJeremy L Thompson     CeedInt index = -1;
2295ac9af79SJeremy L Thompson 
2305ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2315ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2325ac9af79SJeremy L Thompson     }
2336574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2345ac9af79SJeremy L Thompson   }
2355ac9af79SJeremy L Thompson 
2363668ca4bSJeremy L Thompson   bool is_composite = false;
2372b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
238d8dd9a91SJeremy L Thompson   if (is_composite) {
239d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
240d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
241d8dd9a91SJeremy L Thompson 
242c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
243c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2446574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2456574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
246d8dd9a91SJeremy L Thompson 
247d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
248d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
2493668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
2502788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
251d8dd9a91SJeremy L Thompson       }
252d8dd9a91SJeremy L Thompson     }
253d8dd9a91SJeremy L Thompson   } else {
2546574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
2552788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
2562788fa27SJeremy L Thompson   }
2572788fa27SJeremy L Thompson 
2582788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2592788fa27SJeremy L Thompson }
2602788fa27SJeremy L Thompson 
2612788fa27SJeremy L Thompson /**
2622788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
2634385fb7fSSebastian Grimberg 
2642788fa27SJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
2659fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
2669fd66db6SSebastian Grimberg any field of a matching type.
2672788fa27SJeremy L Thompson 
2682788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
2692788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
2702788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
271c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
272c5d0f995SJed Brown   @param[out]    values      Values in the label
2732788fa27SJeremy L Thompson 
2742788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2752788fa27SJeremy L Thompson 
2762788fa27SJeremy L Thompson   @ref User
2772788fa27SJeremy L Thompson **/
2782788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
2792788fa27SJeremy L Thompson                                              void *values) {
2806574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2812788fa27SJeremy L Thompson 
2822788fa27SJeremy L Thompson   *(void **)values = NULL;
2832788fa27SJeremy L Thompson   *num_values      = 0;
2842788fa27SJeremy L Thompson 
2855ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2865ac9af79SJeremy L Thompson   if (field_label->from_op) {
2875ac9af79SJeremy L Thompson     CeedInt index = -1;
2885ac9af79SJeremy L Thompson 
2895ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2905ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2915ac9af79SJeremy L Thompson     }
2926574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2935ac9af79SJeremy L Thompson   }
2945ac9af79SJeremy L Thompson 
2952788fa27SJeremy L Thompson   bool is_composite = false;
2962788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2972788fa27SJeremy L Thompson   if (is_composite) {
2982788fa27SJeremy L Thompson     CeedInt       num_sub;
2992788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3002788fa27SJeremy L Thompson 
3012788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3022788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3036574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3046574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3052788fa27SJeremy L Thompson 
3062788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3072788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3082788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3092788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3102788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3112788fa27SJeremy L Thompson       }
3122788fa27SJeremy L Thompson     }
3132788fa27SJeremy L Thompson   } else {
3146574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3152788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3162788fa27SJeremy L Thompson   }
3172788fa27SJeremy L Thompson 
3182788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3192788fa27SJeremy L Thompson }
3202788fa27SJeremy L Thompson 
3212788fa27SJeremy L Thompson /**
3222788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
3234385fb7fSSebastian Grimberg 
3242788fa27SJeremy L Thompson   For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
3259fd66db6SSebastian Grimberg   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have
3269fd66db6SSebastian Grimberg any field of a matching type.
3272788fa27SJeremy L Thompson 
3282788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3292788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3302788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
331c5d0f995SJed Brown   @param[in]     values      Values array to restore
3322788fa27SJeremy L Thompson 
3332788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3342788fa27SJeremy L Thompson 
3352788fa27SJeremy L Thompson   @ref User
3362788fa27SJeremy L Thompson **/
3372788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3386574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3392788fa27SJeremy L Thompson 
3405ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3415ac9af79SJeremy L Thompson   if (field_label->from_op) {
3425ac9af79SJeremy L Thompson     CeedInt index = -1;
3435ac9af79SJeremy L Thompson 
3445ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3455ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3465ac9af79SJeremy L Thompson     }
3476574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3485ac9af79SJeremy L Thompson   }
3495ac9af79SJeremy L Thompson 
3502788fa27SJeremy L Thompson   bool is_composite = false;
3512788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3522788fa27SJeremy L Thompson   if (is_composite) {
3532788fa27SJeremy L Thompson     CeedInt       num_sub;
3542788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3552788fa27SJeremy L Thompson 
3562788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3572788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3586574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3596574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3602788fa27SJeremy L Thompson 
3612788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3622788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3632788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3642788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
3652788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3662788fa27SJeremy L Thompson       }
3672788fa27SJeremy L Thompson     }
3682788fa27SJeremy L Thompson   } else {
3696574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3702788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
371d8dd9a91SJeremy L Thompson   }
372d8dd9a91SJeremy L Thompson 
373d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
374d8dd9a91SJeremy L Thompson }
375d8dd9a91SJeremy L Thompson 
3767a982d89SJeremy L. Thompson /// @}
3777a982d89SJeremy L. Thompson 
3787a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3797a982d89SJeremy L. Thompson /// CeedOperator Backend API
3807a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
3817a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
3827a982d89SJeremy L. Thompson /// @{
3837a982d89SJeremy L. Thompson 
3847a982d89SJeremy L. Thompson /**
3857a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
3867a982d89SJeremy L. Thompson 
387ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
388d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
3897a982d89SJeremy L. Thompson 
3907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3917a982d89SJeremy L. Thompson 
3927a982d89SJeremy L. Thompson   @ref Backend
3937a982d89SJeremy L. Thompson **/
394d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
3956574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
396d1d35e2fSjeremylt   *num_args = op->num_fields;
397e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3987a982d89SJeremy L. Thompson }
3997a982d89SJeremy L. Thompson 
4007a982d89SJeremy L. Thompson /**
4017a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4027a982d89SJeremy L. Thompson 
403ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
404d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4057a982d89SJeremy L. Thompson 
4067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4077a982d89SJeremy L. Thompson 
4087a982d89SJeremy L. Thompson   @ref Backend
4097a982d89SJeremy L. Thompson **/
410d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
411f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4137a982d89SJeremy L. Thompson }
4147a982d89SJeremy L. Thompson 
4157a982d89SJeremy L. Thompson /**
4167a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4177a982d89SJeremy L. Thompson 
418ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4197a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4207a982d89SJeremy L. Thompson 
4217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4227a982d89SJeremy L. Thompson 
4237a982d89SJeremy L. Thompson   @ref Backend
4247a982d89SJeremy L. Thompson **/
4257a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
4266574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
4277a982d89SJeremy L. Thompson   *qf = op->qf;
428e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4297a982d89SJeremy L. Thompson }
4307a982d89SJeremy L. Thompson 
4317a982d89SJeremy L. Thompson /**
432c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
433c04a41a7SJeremy L Thompson 
434ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
435d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
436c04a41a7SJeremy L Thompson 
437c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
438c04a41a7SJeremy L Thompson 
439c04a41a7SJeremy L Thompson   @ref Backend
440c04a41a7SJeremy L Thompson **/
441d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
442f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
443e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
444c04a41a7SJeremy L Thompson }
445c04a41a7SJeremy L Thompson 
446c04a41a7SJeremy L Thompson /**
4477a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
4487a982d89SJeremy L. Thompson 
449ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
4507a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson   @ref Backend
4557a982d89SJeremy L. Thompson **/
456777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
457777ff853SJeremy L Thompson   *(void **)data = op->data;
458e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4597a982d89SJeremy L. Thompson }
4607a982d89SJeremy L. Thompson 
4617a982d89SJeremy L. Thompson /**
4627a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
4637a982d89SJeremy L. Thompson 
464ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
465ea61e9acSJeremy L Thompson   @param[in]     data Data to set
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson   @ref Backend
4707a982d89SJeremy L. Thompson **/
471777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
472777ff853SJeremy L Thompson   op->data = data;
473e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4747a982d89SJeremy L. Thompson }
4757a982d89SJeremy L. Thompson 
4767a982d89SJeremy L. Thompson /**
47734359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
47834359f16Sjeremylt 
479ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
48034359f16Sjeremylt 
48134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
48234359f16Sjeremylt 
48334359f16Sjeremylt   @ref Backend
48434359f16Sjeremylt **/
4859560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
48634359f16Sjeremylt   op->ref_count++;
48734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
48834359f16Sjeremylt }
48934359f16Sjeremylt 
49034359f16Sjeremylt /**
4917a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
4927a982d89SJeremy L. Thompson 
493ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
4947a982d89SJeremy L. Thompson 
4957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4967a982d89SJeremy L. Thompson 
4977a982d89SJeremy L. Thompson   @ref Backend
4987a982d89SJeremy L. Thompson **/
4997a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
500f04ea552SJeremy L Thompson   op->is_backend_setup = true;
501e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5027a982d89SJeremy L. Thompson }
5037a982d89SJeremy L. Thompson 
5047a982d89SJeremy L. Thompson /// @}
5057a982d89SJeremy L. Thompson 
5067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5077a982d89SJeremy L. Thompson /// CeedOperator Public API
5087a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5097a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
510dfdf5a53Sjeremylt /// @{
511d7b241e6Sjeremylt 
512d7b241e6Sjeremylt /**
513ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
5144385fb7fSSebastian Grimberg 
515ea61e9acSJeremy L Thompson   A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
516d7b241e6Sjeremylt 
517ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
518ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
519ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
520ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
521ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
522b11c1e72Sjeremylt 
523b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
524dfdf5a53Sjeremylt 
5257a982d89SJeremy L. Thompson   @ref User
526d7b241e6Sjeremylt  */
5272b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5285fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5295fe0d4faSjeremylt     Ceed delegate;
5306574a04fSJeremy L Thompson 
5312b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
5326574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
5332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
534e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5355fe0d4faSjeremylt   }
5365fe0d4faSjeremylt 
5376574a04fSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
538db002c03SJeremy L Thompson 
5392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
540db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
541d1d35e2fSjeremylt   (*op)->ref_count   = 1;
5422b104005SJeremy L Thompson   (*op)->input_size  = -1;
5432b104005SJeremy L Thompson   (*op)->output_size = -1;
544db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
545db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
546db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
5472b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
5482b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
5492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
5502b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
551e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
552d7b241e6Sjeremylt }
553d7b241e6Sjeremylt 
554d7b241e6Sjeremylt /**
55552d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
55652d6035fSJeremy L Thompson 
557ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
558ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
55952d6035fSJeremy L Thompson 
56052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
56152d6035fSJeremy L Thompson 
5627a982d89SJeremy L. Thompson   @ref User
56352d6035fSJeremy L Thompson  */
56452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
56552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
56652d6035fSJeremy L Thompson     Ceed delegate;
5672b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
56852d6035fSJeremy L Thompson 
569250756a7Sjeremylt     if (delegate) {
5702b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
571e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
57252d6035fSJeremy L Thompson     }
573250756a7Sjeremylt   }
57452d6035fSJeremy L Thompson 
5752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
576db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
577996d9ab5SJed Brown   (*op)->ref_count    = 1;
578f04ea552SJeremy L Thompson   (*op)->is_composite = true;
5792b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
5802b104005SJeremy L Thompson   (*op)->input_size  = -1;
5812b104005SJeremy L Thompson   (*op)->output_size = -1;
582250756a7Sjeremylt 
583db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
584e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
58552d6035fSJeremy L Thompson }
58652d6035fSJeremy L Thompson 
58752d6035fSJeremy L Thompson /**
588ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
5894385fb7fSSebastian Grimberg 
590ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedOperatorDestroy()`.
591512bb800SJeremy L Thompson 
592512bb800SJeremy L Thompson   Note: If the value of `op_copy` passed to this function is non-NULL, then it is assumed that `op_copy` is a pointer to a CeedOperator.
593512bb800SJeremy L Thompson         This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
5949560d06aSjeremylt 
595ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
596ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
5979560d06aSjeremylt 
5989560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
5999560d06aSjeremylt 
6009560d06aSjeremylt   @ref User
6019560d06aSjeremylt **/
6029560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
6032b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
6042b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
6059560d06aSjeremylt   *op_copy = op;
6069560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6079560d06aSjeremylt }
6089560d06aSjeremylt 
6099560d06aSjeremylt /**
610ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
611d7b241e6Sjeremylt 
612ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
613ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
614ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
615d7b241e6Sjeremylt 
616ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
617ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
618d7b241e6Sjeremylt 
619*528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
620*528a22edSJeremy L Thompson   When using @ref CEED_BASIS_COLLOCATED, the number of quadrature points is determined by the element size of r.
621*528a22edSJeremy L Thompson 
622ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
623ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
624ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
625ea61e9acSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points
6265ac9af79SJeremy 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
6275ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
628b11c1e72Sjeremylt 
629b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
630dfdf5a53Sjeremylt 
6317a982d89SJeremy L. Thompson   @ref User
632b11c1e72Sjeremylt **/
6332b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
6346574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
6356574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
6366574a04fSJeremy L Thompson   CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
6376574a04fSJeremy L Thompson   CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
6386574a04fSJeremy L Thompson   CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
63952d6035fSJeremy L Thompson 
6406574a04fSJeremy L Thompson   CeedInt num_elem = 0;
6412b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
6426574a04fSJeremy L Thompson   CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
6432b730f8bSJeremy L Thompson             "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
644d7b241e6Sjeremylt 
64578464608Sjeremylt   CeedInt num_qpts = 0;
6469a18a30cSJeremy L Thompson   if (b == CEED_BASIS_COLLOCATED) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts));
6479a18a30cSJeremy L Thompson   else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
648*528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
649*528a22edSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added Bases. Found %" CeedInt_FMT
650*528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
651*528a22edSJeremy L Thompson             b == CEED_BASIS_COLLOCATED ? "ElemRestriction" : "Basis", num_qpts, op->num_qpts);
652d1d35e2fSjeremylt   CeedQFunctionField qf_field;
653d1d35e2fSjeremylt   CeedOperatorField *op_field;
6542b104005SJeremy L Thompson   bool               is_input = true;
655d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
656d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
657d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
658d1d35e2fSjeremylt       op_field = &op->input_fields[i];
659d7b241e6Sjeremylt       goto found;
660d7b241e6Sjeremylt     }
661d7b241e6Sjeremylt   }
6622b104005SJeremy L Thompson   is_input = false;
663d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
664d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
665d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
666d1d35e2fSjeremylt       op_field = &op->output_fields[i];
667d7b241e6Sjeremylt       goto found;
668d7b241e6Sjeremylt     }
669d7b241e6Sjeremylt   }
670c042f62fSJeremy L Thompson   // LCOV_EXCL_START
6712b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
672c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
673d7b241e6Sjeremylt found:
6742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
6752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
676e15f9bd0SJeremy L Thompson 
6772b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
6782b104005SJeremy L Thompson     CeedSize l_size;
6792b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
6802b104005SJeremy L Thompson     if (is_input) {
6812b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
6826574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6836574a04fSJeremy L Thompson                 op->input_size);
6842b104005SJeremy L Thompson     } else {
6852b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
6866574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
6876574a04fSJeremy L Thompson                 op->output_size);
6882b104005SJeremy L Thompson     }
6892b730f8bSJeremy L Thompson   }
6902b104005SJeremy L Thompson 
691393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
692393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
693e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
694d1d35e2fSjeremylt     op->num_elem        = num_elem;
695d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
696e15f9bd0SJeremy L Thompson   }
697393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
6985a72c492SJeremy L Thompson   if (op->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
699e15f9bd0SJeremy L Thompson 
700d1d35e2fSjeremylt   op->num_fields += 1;
7012b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
702e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
703d7b241e6Sjeremylt }
704d7b241e6Sjeremylt 
705d7b241e6Sjeremylt /**
70643bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
70743bbe138SJeremy L Thompson 
708ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
709f04ea552SJeremy L Thompson 
710ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
711f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
71243bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
713f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
71443bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
71543bbe138SJeremy L Thompson 
71643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
71743bbe138SJeremy L Thompson 
718e9b533fbSJeremy L Thompson   @ref Advanced
71943bbe138SJeremy L Thompson **/
7202b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
72143bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7226574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
72443bbe138SJeremy L Thompson 
72543bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
72643bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
72743bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
72843bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
72943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
73043bbe138SJeremy L Thompson }
73143bbe138SJeremy L Thompson 
73243bbe138SJeremy L Thompson /**
733de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
734de5900adSJames Wright 
735de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
736de5900adSJames Wright 
737de5900adSJames Wright   @param[in]  op         CeedOperator
738de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
739de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
740de5900adSJames Wright 
741de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
742de5900adSJames Wright 
743de5900adSJames Wright   @ref Advanced
744de5900adSJames Wright **/
745de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
746de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
747de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
748de5900adSJames Wright   char              *name;
749de5900adSJames Wright   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
750de5900adSJames Wright 
751de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
752de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
753de5900adSJames Wright     if (!strcmp(name, field_name)) {
754de5900adSJames Wright       *op_field = input_fields[i];
755de5900adSJames Wright       return CEED_ERROR_SUCCESS;
756de5900adSJames Wright     }
757de5900adSJames Wright   }
758de5900adSJames Wright 
759de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
760de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
761de5900adSJames Wright     if (!strcmp(name, field_name)) {
762de5900adSJames Wright       *op_field = output_fields[i];
763de5900adSJames Wright       return CEED_ERROR_SUCCESS;
764de5900adSJames Wright     }
765de5900adSJames Wright   }
766de5900adSJames Wright 
767de5900adSJames Wright   bool has_name = op->name;
768de5900adSJames Wright   // LCOV_EXCL_START
769de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
770de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
771de5900adSJames Wright   // LCOV_EXCL_STOP
772de5900adSJames Wright }
773de5900adSJames Wright 
774de5900adSJames Wright /**
77528567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
77628567f8fSJeremy L Thompson 
777ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
77828567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
77928567f8fSJeremy L Thompson 
78028567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78128567f8fSJeremy L Thompson 
782e9b533fbSJeremy L Thompson   @ref Advanced
78328567f8fSJeremy L Thompson **/
78428567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
78528567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
78628567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
78728567f8fSJeremy L Thompson }
78828567f8fSJeremy L Thompson 
78928567f8fSJeremy L Thompson /**
79043bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
79143bbe138SJeremy L Thompson 
792ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
79343bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
79443bbe138SJeremy L Thompson 
79543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
79643bbe138SJeremy L Thompson 
797e9b533fbSJeremy L Thompson   @ref Advanced
79843bbe138SJeremy L Thompson **/
7992b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
800437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
80143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
80243bbe138SJeremy L Thompson }
80343bbe138SJeremy L Thompson 
80443bbe138SJeremy L Thompson /**
80543bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
80643bbe138SJeremy L Thompson 
807ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
80843bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
80943bbe138SJeremy L Thompson 
81043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81143bbe138SJeremy L Thompson 
812e9b533fbSJeremy L Thompson   @ref Advanced
81343bbe138SJeremy L Thompson **/
81443bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
81543bbe138SJeremy L Thompson   *basis = op_field->basis;
81643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81743bbe138SJeremy L Thompson }
81843bbe138SJeremy L Thompson 
81943bbe138SJeremy L Thompson /**
82043bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
82143bbe138SJeremy L Thompson 
822ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
82343bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
82443bbe138SJeremy L Thompson 
82543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82643bbe138SJeremy L Thompson 
827e9b533fbSJeremy L Thompson   @ref Advanced
82843bbe138SJeremy L Thompson **/
82943bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
83043bbe138SJeremy L Thompson   *vec = op_field->vec;
83143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
83243bbe138SJeremy L Thompson }
83343bbe138SJeremy L Thompson 
83443bbe138SJeremy L Thompson /**
83552d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
836288c0443SJeremy L Thompson 
837ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
838ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
83952d6035fSJeremy L Thompson 
84052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84152d6035fSJeremy L Thompson 
8427a982d89SJeremy L. Thompson   @ref User
84352d6035fSJeremy L Thompson  */
8442b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
8456574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
8466574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
8476574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8482b730f8bSJeremy L Thompson 
8492b730f8bSJeremy L Thompson   {
8502b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
8512b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
8522b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
8532b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
8542b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
8556574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
8566574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
8572b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
8582b730f8bSJeremy L Thompson               "shape (%td, %td)",
8592b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
8602b730f8bSJeremy L Thompson   }
8612b730f8bSJeremy L Thompson 
862d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
8632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
864d1d35e2fSjeremylt   composite_op->num_suboperators++;
865e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86652d6035fSJeremy L Thompson }
86752d6035fSJeremy L Thompson 
86852d6035fSJeremy L Thompson /**
86975f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
87075f0d5a4SJeremy L Thompson 
87175f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
87275f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
87375f0d5a4SJeremy L Thompson 
87475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87575f0d5a4SJeremy L Thompson 
87675f0d5a4SJeremy L Thompson   @ref Backend
87775f0d5a4SJeremy L Thompson **/
878c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
8796574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
88075f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
88175f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88275f0d5a4SJeremy L Thompson }
88375f0d5a4SJeremy L Thompson 
88475f0d5a4SJeremy L Thompson /**
88575f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
88675f0d5a4SJeremy L Thompson 
88775f0d5a4SJeremy L Thompson   @param op                  CeedOperator
88875f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
88975f0d5a4SJeremy L Thompson 
89075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89175f0d5a4SJeremy L Thompson 
89275f0d5a4SJeremy L Thompson   @ref Backend
89375f0d5a4SJeremy L Thompson **/
894c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
8956574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
89675f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
89775f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89875f0d5a4SJeremy L Thompson }
89975f0d5a4SJeremy L Thompson 
90075f0d5a4SJeremy L Thompson /**
9014db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
9024db537f9SJeremy L Thompson 
9034db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
9044db537f9SJeremy L Thompson 
9054db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9064db537f9SJeremy L Thompson 
9074db537f9SJeremy L Thompson   @ref User
9084db537f9SJeremy L Thompson **/
9094db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9104db537f9SJeremy L Thompson   Ceed ceed;
9112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9124db537f9SJeremy L Thompson 
9132b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9144db537f9SJeremy L Thompson 
9154db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9164db537f9SJeremy L Thompson   if (op->is_composite) {
91743622462SJeremy L Thompson     if (!op->num_suboperators) {
91843622462SJeremy L Thompson       // Empty operator setup
91943622462SJeremy L Thompson       op->input_size  = 0;
92043622462SJeremy L Thompson       op->output_size = 0;
92143622462SJeremy L Thompson     } else {
9224db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9232b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9244db537f9SJeremy L Thompson       }
9252b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9262b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9272b104005SJeremy L Thompson       CeedSize input_size, output_size;
9282b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
92943622462SJeremy L Thompson     }
9304db537f9SJeremy L Thompson   } else {
9316574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9326574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9336574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9346574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9356574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9362b730f8bSJeremy L Thompson   }
9374db537f9SJeremy L Thompson 
9384db537f9SJeremy L Thompson   // Flag as immutable and ready
9394db537f9SJeremy L Thompson   op->is_interface_setup = true;
9402b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
9412b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
9422b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
9434db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9444db537f9SJeremy L Thompson }
9454db537f9SJeremy L Thompson 
9464db537f9SJeremy L Thompson /**
947c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
9484385fb7fSSebastian Grimberg 
949ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
950c9366a6bSJeremy L Thompson 
951c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
952c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
953c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
954c9366a6bSJeremy L Thompson 
955c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
956c9366a6bSJeremy L Thompson 
957c9366a6bSJeremy L Thompson   @ref User
958c9366a6bSJeremy L Thompson **/
9592b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
960c9366a6bSJeremy L Thompson   bool is_composite;
961c9366a6bSJeremy L Thompson 
9622b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
9632b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
964c9366a6bSJeremy L Thompson 
9652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9662b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
967c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
968c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
9692b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
9702b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
9712b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
9722b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
9736574a04fSJeremy 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,
9746574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
9752b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9762b730f8bSJeremy L Thompson                 "shape (%td, %td)",
9772b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
978c9366a6bSJeremy L Thompson     }
9792b730f8bSJeremy L Thompson   }
980c9366a6bSJeremy L Thompson 
981c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
982c9366a6bSJeremy L Thompson }
983c9366a6bSJeremy L Thompson 
984c9366a6bSJeremy L Thompson /**
985beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
9864385fb7fSSebastian Grimberg 
987ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
9889fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
9899fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
9909fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
9918b919e6bSJeremy L Thompson 
992beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
993beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
9948b919e6bSJeremy L Thompson 
9958b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9968b919e6bSJeremy L Thompson 
9978b919e6bSJeremy L Thompson   @ref Advanced
9988b919e6bSJeremy L Thompson **/
9992b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
10008b919e6bSJeremy L Thompson   bool is_composite;
10018b919e6bSJeremy L Thompson 
10022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10038b919e6bSJeremy L Thompson   if (is_composite) {
10048b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10052b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10068b919e6bSJeremy L Thompson     }
10078b919e6bSJeremy L Thompson   } else {
10082b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1009beecbf24SJeremy L Thompson   }
1010beecbf24SJeremy L Thompson 
1011beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1012beecbf24SJeremy L Thompson }
1013beecbf24SJeremy L Thompson 
1014beecbf24SJeremy L Thompson /**
1015beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1016beecbf24SJeremy L Thompson 
1017beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10186e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1019beecbf24SJeremy L Thompson 
1020beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1021beecbf24SJeremy L Thompson 
1022beecbf24SJeremy L Thompson   @ref Advanced
1023beecbf24SJeremy L Thompson **/
10242b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1025beecbf24SJeremy L Thompson   bool is_composite;
1026beecbf24SJeremy L Thompson 
10272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1028beecbf24SJeremy L Thompson   if (is_composite) {
1029beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10302b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1031beecbf24SJeremy L Thompson     }
1032beecbf24SJeremy L Thompson   } else {
10332b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10348b919e6bSJeremy L Thompson   }
10358b919e6bSJeremy L Thompson 
10368b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10378b919e6bSJeremy L Thompson }
10388b919e6bSJeremy L Thompson 
10398b919e6bSJeremy L Thompson /**
1040cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
10414385fb7fSSebastian Grimberg 
1042ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1043ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1044cd4dfc48Sjeremylt 
1045ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1046ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1047cd4dfc48Sjeremylt 
1048cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1049cd4dfc48Sjeremylt 
1050e9b533fbSJeremy L Thompson   @ref Advanced
1051cd4dfc48Sjeremylt **/
1052cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
10536574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
10546574a04fSJeremy L Thompson   CeedCheck(op->num_qpts == 0, op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
10556574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1056cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1057cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1058cd4dfc48Sjeremylt }
1059cd4dfc48Sjeremylt 
1060cd4dfc48Sjeremylt /**
1061ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1062ea6b5821SJeremy L Thompson 
1063ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1064ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1065ea6b5821SJeremy L Thompson 
1066ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1067ea6b5821SJeremy L Thompson 
1068ea6b5821SJeremy L Thompson   @ref User
1069ea6b5821SJeremy L Thompson **/
1070ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1071ea6b5821SJeremy L Thompson   char  *name_copy;
1072ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1073ea6b5821SJeremy L Thompson 
10742b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1075ea6b5821SJeremy L Thompson   if (name_len > 0) {
10762b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1077ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1078ea6b5821SJeremy L Thompson     op->name = name_copy;
1079ea6b5821SJeremy L Thompson   }
1080ea6b5821SJeremy L Thompson 
1081ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1082ea6b5821SJeremy L Thompson }
1083ea6b5821SJeremy L Thompson 
1084ea6b5821SJeremy L Thompson /**
10857a982d89SJeremy L. Thompson   @brief View a CeedOperator
10867a982d89SJeremy L. Thompson 
10877a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
10887a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
10897a982d89SJeremy L. Thompson 
10907a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
10917a982d89SJeremy L. Thompson 
10927a982d89SJeremy L. Thompson   @ref User
10937a982d89SJeremy L. Thompson **/
10947a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1095ea6b5821SJeremy L Thompson   bool has_name = op->name;
10967a982d89SJeremy L. Thompson 
1097f04ea552SJeremy L Thompson   if (op->is_composite) {
10982b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
10997a982d89SJeremy L. Thompson 
1100d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1101ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
11022b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
11032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
11047a982d89SJeremy L. Thompson     }
11057a982d89SJeremy L. Thompson   } else {
11062b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11072b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11087a982d89SJeremy L. Thompson   }
1109e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11107a982d89SJeremy L. Thompson }
11113bd813ffSjeremylt 
11123bd813ffSjeremylt /**
1113b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1114b7c9bbdaSJeremy L Thompson 
1115ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1116b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1117b7c9bbdaSJeremy L Thompson 
1118b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1119b7c9bbdaSJeremy L Thompson 
1120b7c9bbdaSJeremy L Thompson   @ref Advanced
1121b7c9bbdaSJeremy L Thompson **/
1122b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1123b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1124b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1125b7c9bbdaSJeremy L Thompson }
1126b7c9bbdaSJeremy L Thompson 
1127b7c9bbdaSJeremy L Thompson /**
1128b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1129b7c9bbdaSJeremy L Thompson 
1130ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1131b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1132b7c9bbdaSJeremy L Thompson 
1133b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1134b7c9bbdaSJeremy L Thompson 
1135b7c9bbdaSJeremy L Thompson   @ref Advanced
1136b7c9bbdaSJeremy L Thompson **/
1137b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
11386574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1139b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1140b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1141b7c9bbdaSJeremy L Thompson }
1142b7c9bbdaSJeremy L Thompson 
1143b7c9bbdaSJeremy L Thompson /**
1144b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1145b7c9bbdaSJeremy L Thompson 
1146ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1147b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1148b7c9bbdaSJeremy L Thompson 
1149b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1150b7c9bbdaSJeremy L Thompson 
1151b7c9bbdaSJeremy L Thompson   @ref Advanced
1152b7c9bbdaSJeremy L Thompson **/
1153b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
11546574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1155b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1156b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1157b7c9bbdaSJeremy L Thompson }
1158b7c9bbdaSJeremy L Thompson 
1159b7c9bbdaSJeremy L Thompson /**
11606e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
11616e15d496SJeremy L Thompson 
1162ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1163ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
11646e15d496SJeremy L Thompson 
11656e15d496SJeremy L Thompson   @ref Backend
11666e15d496SJeremy L Thompson **/
11679d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
11686e15d496SJeremy L Thompson   bool is_composite;
11692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
11706e15d496SJeremy L Thompson 
11716e15d496SJeremy L Thompson   *flops = 0;
11722b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11736e15d496SJeremy L Thompson   if (is_composite) {
11746e15d496SJeremy L Thompson     CeedInt num_suboperators;
1175c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
11766e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1177c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
11786e15d496SJeremy L Thompson 
11796e15d496SJeremy L Thompson     // FLOPs for each suboperator
11806e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
11819d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
11822b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
11836e15d496SJeremy L Thompson       *flops += suboperator_flops;
11846e15d496SJeremy L Thompson     }
11856e15d496SJeremy L Thompson   } else {
11866e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
11876e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
11882b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
11896e15d496SJeremy L Thompson     CeedInt num_elem = 0;
11902b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
11914385fb7fSSebastian Grimberg 
11926e15d496SJeremy L Thompson     // Input FLOPs
11936e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11946e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
11959d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
11966e15d496SJeremy L Thompson 
1197437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops));
11986e15d496SJeremy L Thompson         *flops += restr_flops;
11992b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
12006e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12016e15d496SJeremy L Thompson       }
12026e15d496SJeremy L Thompson     }
12036e15d496SJeremy L Thompson     // QF FLOPs
12049d36ca50SJeremy L Thompson     CeedInt  num_qpts;
12059d36ca50SJeremy L Thompson     CeedSize qf_flops;
12062b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12072b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12086e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
12096e15d496SJeremy L Thompson     // Output FLOPs
12106e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12116e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
12129d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
12136e15d496SJeremy L Thompson 
1214437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops));
12156e15d496SJeremy L Thompson         *flops += restr_flops;
12162b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12176e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12186e15d496SJeremy L Thompson       }
12196e15d496SJeremy L Thompson     }
12206e15d496SJeremy L Thompson   }
12216e15d496SJeremy L Thompson 
12226e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12236e15d496SJeremy L Thompson }
12246e15d496SJeremy L Thompson 
12256e15d496SJeremy L Thompson /**
12260126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12279fd66db6SSebastian Grimberg 
1228512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1229512bb800SJeremy L Thompson 
12309fd66db6SSebastian 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.
12319fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
12320126412dSJeremy L Thompson 
1233859c15bbSJames Wright   @param[in]  op  CeedOperator
12340126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
12350126412dSJeremy L Thompson 
12360126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12370126412dSJeremy L Thompson 
1238859c15bbSJames Wright   @ref Advanced
12390126412dSJeremy L Thompson **/
12400126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
12416574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
12420126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
12430126412dSJeremy L Thompson   else *ctx = NULL;
12440126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12450126412dSJeremy L Thompson }
12460126412dSJeremy L Thompson 
12470126412dSJeremy L Thompson /**
1248ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
12493668ca4bSJeremy L Thompson 
1250859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1251859c15bbSJames Wright 
12523668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
12533668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
12543668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
12553668ca4bSJeremy L Thompson 
12563668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12573668ca4bSJeremy L Thompson 
12583668ca4bSJeremy L Thompson   @ref User
12593668ca4bSJeremy L Thompson **/
126017b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
12613668ca4bSJeremy L Thompson   bool is_composite;
12622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12632b730f8bSJeremy L Thompson 
12643668ca4bSJeremy L Thompson   if (is_composite) {
12653668ca4bSJeremy L Thompson     // Check if composite label already created
12663668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
12673668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
12683668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
12693668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
12703668ca4bSJeremy L Thompson       }
12713668ca4bSJeremy L Thompson     }
12723668ca4bSJeremy L Thompson 
12733668ca4bSJeremy L Thompson     // Create composite label if needed
12743668ca4bSJeremy L Thompson     CeedInt               num_sub;
12753668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
12763668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
12773668ca4bSJeremy L Thompson 
12782b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1279c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1280c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12812b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
12823668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
12833668ca4bSJeremy L Thompson 
12843668ca4bSJeremy L Thompson     bool label_found = false;
12853668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
12863668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
12873668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
12882b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
12893668ca4bSJeremy L Thompson         if (new_field_label_i) {
12903668ca4bSJeremy L Thompson           label_found                    = true;
12913668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
12923668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
12933668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
12942b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
12957bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
12962b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
12972b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
12982b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
12997bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13007bfe0f0eSJeremy L Thompson           } else {
13017bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
13027bfe0f0eSJeremy L Thompson           }
13032b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
13047bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13052b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13062b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13077bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13087bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13097bfe0f0eSJeremy L Thompson           } else {
13107bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13117bfe0f0eSJeremy L Thompson           }
13123668ca4bSJeremy L Thompson         }
13133668ca4bSJeremy L Thompson       }
13143668ca4bSJeremy L Thompson     }
13153668ca4bSJeremy L Thompson     if (!label_found) {
13163668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13172b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13182b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13193668ca4bSJeremy L Thompson       *field_label = NULL;
13203668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13213668ca4bSJeremy L Thompson     } else {
13225ac9af79SJeremy L Thompson       *field_label = new_field_label;
13235ac9af79SJeremy L Thompson     }
13245ac9af79SJeremy L Thompson   } else {
13255ac9af79SJeremy L Thompson     CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
13265ac9af79SJeremy L Thompson   }
13275ac9af79SJeremy L Thompson 
13285ac9af79SJeremy L Thompson   // Set label in operator
13295ac9af79SJeremy L Thompson   if (*field_label) {
13305ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
13315ac9af79SJeremy L Thompson 
13323668ca4bSJeremy L Thompson     // Move new composite label to operator
13333668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
13342b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
13353668ca4bSJeremy L Thompson       op->max_context_labels = 1;
13363668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
13372b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
13383668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
13393668ca4bSJeremy L Thompson     }
13405ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
13413668ca4bSJeremy L Thompson     op->num_context_labels++;
13423668ca4bSJeremy L Thompson   }
13433668ca4bSJeremy L Thompson 
13443668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13453668ca4bSJeremy L Thompson }
13463668ca4bSJeremy L Thompson 
13473668ca4bSJeremy L Thompson /**
13482788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
13494385fb7fSSebastian Grimberg 
13502788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1351d8dd9a91SJeremy L Thompson 
1352ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
13532788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1354ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1355d8dd9a91SJeremy L Thompson 
1356d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1357d8dd9a91SJeremy L Thompson 
1358d8dd9a91SJeremy L Thompson   @ref User
1359d8dd9a91SJeremy L Thompson **/
136017b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
13612b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1362d8dd9a91SJeremy L Thompson }
1363d8dd9a91SJeremy L Thompson 
1364d8dd9a91SJeremy L Thompson /**
13652788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
13664385fb7fSSebastian Grimberg 
13672788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
13682788fa27SJeremy L Thompson 
13692788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13702788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
13712788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
13722788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13732788fa27SJeremy L Thompson 
13742788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13752788fa27SJeremy L Thompson 
13762788fa27SJeremy L Thompson   @ref User
13772788fa27SJeremy L Thompson **/
137817b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
13792788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
13802788fa27SJeremy L Thompson }
13812788fa27SJeremy L Thompson 
13822788fa27SJeremy L Thompson /**
13832788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
13842788fa27SJeremy L Thompson 
13852788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
13862788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
13872788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
13882788fa27SJeremy L Thompson 
13892788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13902788fa27SJeremy L Thompson 
13912788fa27SJeremy L Thompson   @ref User
13922788fa27SJeremy L Thompson **/
139317b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
13942788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
13952788fa27SJeremy L Thompson }
13962788fa27SJeremy L Thompson 
13972788fa27SJeremy L Thompson /**
13982788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
13994385fb7fSSebastian Grimberg 
14002788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1401d8dd9a91SJeremy L Thompson 
1402ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1403ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1404ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1405d8dd9a91SJeremy L Thompson 
1406d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1407d8dd9a91SJeremy L Thompson 
1408d8dd9a91SJeremy L Thompson   @ref User
1409d8dd9a91SJeremy L Thompson **/
141017b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14112b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1412d8dd9a91SJeremy L Thompson }
1413d8dd9a91SJeremy L Thompson 
1414d8dd9a91SJeremy L Thompson /**
14152788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14164385fb7fSSebastian Grimberg 
14172788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14182788fa27SJeremy L Thompson 
14192788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14202788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1421c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
14222788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14232788fa27SJeremy L Thompson 
14242788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14252788fa27SJeremy L Thompson 
14262788fa27SJeremy L Thompson   @ref User
14272788fa27SJeremy L Thompson **/
142817b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
14292788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
14302788fa27SJeremy L Thompson }
14312788fa27SJeremy L Thompson 
14322788fa27SJeremy L Thompson /**
14332788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
14342788fa27SJeremy L Thompson 
14352788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14362788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14372788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14382788fa27SJeremy L Thompson 
14392788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14402788fa27SJeremy L Thompson 
14412788fa27SJeremy L Thompson   @ref User
14422788fa27SJeremy L Thompson **/
144317b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
14442788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
14452788fa27SJeremy L Thompson }
14462788fa27SJeremy L Thompson 
14472788fa27SJeremy L Thompson /**
14483bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1449d7b241e6Sjeremylt 
1450ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1451ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1452d7b241e6Sjeremylt 
1453ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1454f04ea552SJeremy L Thompson 
1455ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1456ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
14575ac9af79SJeremy L Thompson   @param[out] out     CeedVector to store result of applying operator (must be distinct from @a in) or @ref CEED_VECTOR_NONE if there are no
14585ac9af79SJeremy L Thompson active outputs
1459ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1460b11c1e72Sjeremylt 
1461b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1462dfdf5a53Sjeremylt 
14637a982d89SJeremy L. Thompson   @ref User
1464b11c1e72Sjeremylt **/
14652b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
14662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1467d7b241e6Sjeremylt 
14683ca2b39bSJeremy L Thompson   if (op->is_composite) {
1469250756a7Sjeremylt     // Composite Operator
1470250756a7Sjeremylt     if (op->ApplyComposite) {
14712b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1472250756a7Sjeremylt     } else {
1473d1d35e2fSjeremylt       CeedInt num_suboperators;
1474c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1475d1d35e2fSjeremylt       CeedOperator *sub_operators;
1476c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1477250756a7Sjeremylt 
1478250756a7Sjeremylt       // Zero all output vectors
14793ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1480d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1481d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1482d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1483250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
14842b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1485250756a7Sjeremylt           }
1486250756a7Sjeremylt         }
1487250756a7Sjeremylt       }
1488250756a7Sjeremylt       // Apply
1489d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
14902b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1491cae8b89aSjeremylt       }
1492cae8b89aSjeremylt     }
14933ca2b39bSJeremy L Thompson   } else {
14943ca2b39bSJeremy L Thompson     // Standard Operator
14953ca2b39bSJeremy L Thompson     if (op->Apply) {
14963ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
14973ca2b39bSJeremy L Thompson     } else {
14983ca2b39bSJeremy L Thompson       // Zero all output vectors
14993ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
15003ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
15013ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
15023ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
15033ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
15043ca2b39bSJeremy L Thompson       }
15053ca2b39bSJeremy L Thompson       // Apply
15063ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15073ca2b39bSJeremy L Thompson     }
1508250756a7Sjeremylt   }
1509e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1510cae8b89aSjeremylt }
1511cae8b89aSjeremylt 
1512cae8b89aSjeremylt /**
1513cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1514cae8b89aSjeremylt 
1515ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1516ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1517cae8b89aSjeremylt 
1518ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1519ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1520ea61e9acSJeremy 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
1521ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1522cae8b89aSjeremylt 
1523cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1524cae8b89aSjeremylt 
15257a982d89SJeremy L. Thompson   @ref User
1526cae8b89aSjeremylt **/
15272b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1529cae8b89aSjeremylt 
15303ca2b39bSJeremy L Thompson   if (op->is_composite) {
1531250756a7Sjeremylt     // Composite Operator
1532250756a7Sjeremylt     if (op->ApplyAddComposite) {
15332b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1534cae8b89aSjeremylt     } else {
1535d1d35e2fSjeremylt       CeedInt num_suboperators;
1536c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1537d1d35e2fSjeremylt       CeedOperator *sub_operators;
1538c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1539250756a7Sjeremylt 
1540d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
15412b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
15421d7d2407SJeremy L Thompson       }
1543250756a7Sjeremylt     }
15443ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
15453ca2b39bSJeremy L Thompson     // Standard Operator
15463ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1547250756a7Sjeremylt   }
1548e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1549d7b241e6Sjeremylt }
1550d7b241e6Sjeremylt 
1551d7b241e6Sjeremylt /**
1552b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1553d7b241e6Sjeremylt 
1554ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1555b11c1e72Sjeremylt 
1556b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1557dfdf5a53Sjeremylt 
15587a982d89SJeremy L. Thompson   @ref User
1559b11c1e72Sjeremylt **/
1560d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1561ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1562ad6481ceSJeremy L Thompson     *op = NULL;
1563ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1564ad6481ceSJeremy L Thompson   }
15652b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
15662b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1567fe2413ffSjeremylt   // Free fields
15682b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1569d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1570437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1571437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
157215910d16Sjeremylt       }
1573d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15742b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
157571352a93Sjeremylt       }
15762b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
15772b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
157871352a93Sjeremylt       }
15792b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
15802b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1581fe2413ffSjeremylt     }
15822b730f8bSJeremy L Thompson   }
15832b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1584d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1585437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1586d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
15872b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
158871352a93Sjeremylt       }
15892b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
15902b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
159171352a93Sjeremylt       }
15922b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
15932b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
15942b730f8bSJeremy L Thompson     }
1595fe2413ffSjeremylt   }
1596d1d35e2fSjeremylt   // Destroy sub_operators
15972b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1598d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
15992b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
160052d6035fSJeremy L Thompson     }
16012b730f8bSJeremy L Thompson   }
16022b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
16032b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
16042b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16053668ca4bSJeremy L Thompson   // Destroy any composite labels
16065ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16073668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16082b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16092b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16103668ca4bSJeremy L Thompson     }
16115ac9af79SJeremy L Thompson   }
16122b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1613fe2413ffSjeremylt 
16145107b09fSJeremy L Thompson   // Destroy fallback
16152b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16165107b09fSJeremy L Thompson 
1617ed9e99e6SJeremy L Thompson   // Destroy assembly data
16182b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
16192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
162070a7ffb3SJeremy L Thompson 
16212b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
16222b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
16232b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
16242b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
16252b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1626e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1627d7b241e6Sjeremylt }
1628d7b241e6Sjeremylt 
1629d7b241e6Sjeremylt /// @}
1630