xref: /libCEED/interface/ceed-operator.c (revision 2788fa279822b86b73c7d9989c768f3b4c4cdffb)
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>
92b730f8bSJeremy L Thompson #include <ceed/backend.h>
102b730f8bSJeremy L Thompson #include <ceed/ceed.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;
382b730f8bSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, Q_comp = 1, restr_num_comp = 1, size = qf_field->size;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
41e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
42d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {
43e15f9bd0SJeremy L Thompson       // LCOV_EXCL_START
442b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE should be used for a field with eval mode CEED_EVAL_WEIGHT");
45e15f9bd0SJeremy L Thompson       // LCOV_EXCL_STOP
46e15f9bd0SJeremy L Thompson     }
472b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp));
48e1e9e29dSJed Brown   }
49d1d35e2fSjeremylt   if ((r == CEED_ELEMRESTRICTION_NONE) != (eval_mode == CEED_EVAL_WEIGHT)) {
50e1e9e29dSJed Brown     // LCOV_EXCL_START
512b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
52e1e9e29dSJed Brown     // LCOV_EXCL_STOP
53e1e9e29dSJed Brown   }
54e15f9bd0SJeremy L Thompson   // Basis
55e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
562b730f8bSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
578229195eSjeremylt       // LCOV_EXCL_START
582b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with CEED_EVAL_NONE must be used with CEED_BASIS_COLLOCATED",
59d1d35e2fSjeremylt                        qf_field->field_name);
6041bdf1c9SJeremy L Thompson       // LCOV_EXCL_STOP
612b730f8bSJeremy L Thompson     }
622b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
642b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadratureComponents(b, &Q_comp));
65d1d35e2fSjeremylt     if (r != CEED_ELEMRESTRICTION_NONE && restr_num_comp != num_comp) {
66e15f9bd0SJeremy L Thompson       // LCOV_EXCL_START
67e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_DIMENSION,
682b730f8bSJeremy L Thompson                        "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT
692b730f8bSJeremy L Thompson                        " components, but Basis has %" CeedInt_FMT " components",
702b730f8bSJeremy L Thompson                        qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp);
71e15f9bd0SJeremy L Thompson       // LCOV_EXCL_STOP
72e15f9bd0SJeremy L Thompson     }
7341bdf1c9SJeremy L Thompson   } else if (eval_mode != CEED_EVAL_NONE) {
7441bdf1c9SJeremy L Thompson     // LCOV_EXCL_START
752b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with %s cannot be used with CEED_BASIS_COLLOCATED", qf_field->field_name,
762b730f8bSJeremy L Thompson                      CeedEvalModes[eval_mode]);
7741bdf1c9SJeremy L Thompson     // LCOV_EXCL_STOP
78e15f9bd0SJeremy L Thompson   }
79e15f9bd0SJeremy L Thompson   // Field size
80d1d35e2fSjeremylt   switch (eval_mode) {
81e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
822b730f8bSJeremy L Thompson       if (size != restr_num_comp) {
83e15f9bd0SJeremy L Thompson         // LCOV_EXCL_START
84e15f9bd0SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_DIMENSION,
85953dad27SJames Wright                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
862b730f8bSJeremy L Thompson                          qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp);
87e15f9bd0SJeremy L Thompson         // LCOV_EXCL_STOP
882b730f8bSJeremy L Thompson       }
89e15f9bd0SJeremy L Thompson       break;
90e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
912b730f8bSJeremy L Thompson       if (size != num_comp * Q_comp) {
92e15f9bd0SJeremy L Thompson         // LCOV_EXCL_START
93e15f9bd0SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_DIMENSION,
94953dad27SJames Wright                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components",
952b730f8bSJeremy L Thompson                          qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * Q_comp);
96e15f9bd0SJeremy L Thompson         // LCOV_EXCL_STOP
972b730f8bSJeremy L Thompson       }
98e15f9bd0SJeremy L Thompson       break;
99e15f9bd0SJeremy L Thompson     case CEED_EVAL_GRAD:
1002b730f8bSJeremy L Thompson       if (size != num_comp * dim) {
101e15f9bd0SJeremy L Thompson         // LCOV_EXCL_START
102e15f9bd0SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_DIMENSION,
1032b730f8bSJeremy L Thompson                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s in %" CeedInt_FMT " dimensions: ElemRestriction/Basis has %" CeedInt_FMT
1042b730f8bSJeremy L Thompson                          " components",
1052b730f8bSJeremy L Thompson                          qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], dim, num_comp);
106e15f9bd0SJeremy L Thompson         // LCOV_EXCL_STOP
1072b730f8bSJeremy L Thompson       }
108e15f9bd0SJeremy L Thompson       break;
109e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
110d1d35e2fSjeremylt       // No additional checks required
111e15f9bd0SJeremy L Thompson       break;
112e15f9bd0SJeremy L Thompson     case CEED_EVAL_DIV:
1132b730f8bSJeremy L Thompson       if (size != num_comp) {
114a0c16c2cSrezgarshakeri         // LCOV_EXCL_START
115a0c16c2cSrezgarshakeri         return CeedError(ceed, CEED_ERROR_DIMENSION,
116953dad27SJames Wright                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components",
1172b730f8bSJeremy L Thompson                          qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp);
118a0c16c2cSrezgarshakeri         // LCOV_EXCL_STOP
1192b730f8bSJeremy L Thompson       }
120e15f9bd0SJeremy L Thompson       break;
121e15f9bd0SJeremy L Thompson     case CEED_EVAL_CURL:
122e15f9bd0SJeremy L Thompson       // Not implemented
123e15f9bd0SJeremy L Thompson       break;
124e15f9bd0SJeremy L Thompson   }
125e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1267a982d89SJeremy L. Thompson }
1277a982d89SJeremy L. Thompson 
1287a982d89SJeremy L. Thompson /**
1297a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
132d1d35e2fSjeremylt   @param[in] qf_field     QFunction field (carries field name)
133d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1344c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
135d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
1367a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1377a982d89SJeremy L. Thompson 
1387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1397a982d89SJeremy L. Thompson 
1407a982d89SJeremy L. Thompson   @ref Utility
1417a982d89SJeremy L. Thompson **/
1422b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
1437a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
144d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
1457a982d89SJeremy L. Thompson 
1462b730f8bSJeremy L Thompson   fprintf(stream,
1472b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1482b730f8bSJeremy L Thompson           ":\n"
1497a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
150d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
1517a982d89SJeremy L. Thompson 
152990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
153f5ebfb90SJeremy L Thompson 
1542b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
155f5ebfb90SJeremy L Thompson 
1562b730f8bSJeremy L Thompson   if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s      Collocated basis\n", pre);
1577a982d89SJeremy L. Thompson 
1582b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1592b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
160f5ebfb90SJeremy L Thompson 
161e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1627a982d89SJeremy L. Thompson }
1637a982d89SJeremy L. Thompson 
1647a982d89SJeremy L. Thompson /**
1657a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1667a982d89SJeremy L. Thompson 
1677a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1687a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1697a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
1707a982d89SJeremy L. Thompson 
1717a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1727a982d89SJeremy L. Thompson 
1737a982d89SJeremy L. Thompson   @ref Utility
1747a982d89SJeremy L. Thompson **/
1757a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1767a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1777a982d89SJeremy L. Thompson 
178381e6593SJeremy L Thompson   CeedInt num_elem, num_qpts;
1792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
181381e6593SJeremy L Thompson 
18278464608Sjeremylt   CeedInt total_fields = 0;
1832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1842b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1857a982d89SJeremy L. Thompson 
1862b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1877a982d89SJeremy L. Thompson 
1882b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
189d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1902b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1917a982d89SJeremy L. Thompson   }
1927a982d89SJeremy L. Thompson 
1932b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
194d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1952b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1967a982d89SJeremy L. Thompson   }
197e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1987a982d89SJeremy L. Thompson }
1997a982d89SJeremy L. Thompson 
200d99fa3c5SJeremy L Thompson /**
2010f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
202eaf62fffSJeremy L Thompson 
203eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
2040f60e0a8SJeremy L Thompson   @param[out] active_basis Basis for active input vector or NULL for composite operator
205eaf62fffSJeremy L Thompson 
206eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
207eaf62fffSJeremy L Thompson 
208eaf62fffSJeremy L Thompson   @ ref Developer
209eaf62fffSJeremy L Thompson **/
210eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
2116aaed0edSJeremy L Thompson   Ceed ceed;
2126aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2136aaed0edSJeremy L Thompson 
214eaf62fffSJeremy L Thompson   *active_basis = NULL;
2150f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
2162b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
217eaf62fffSJeremy L Thompson     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
2186aaed0edSJeremy L Thompson       if (*active_basis && *active_basis != op->input_fields[i]->basis) {
2196aaed0edSJeremy L Thompson         // LCOV_EXCL_START
2206aaed0edSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found");
2216aaed0edSJeremy L Thompson         // LCOV_EXCL_STOP
2226aaed0edSJeremy L Thompson       }
223eaf62fffSJeremy L Thompson       *active_basis = op->input_fields[i]->basis;
224eaf62fffSJeremy L Thompson       break;
225eaf62fffSJeremy L Thompson     }
2262b730f8bSJeremy L Thompson   }
227eaf62fffSJeremy L Thompson 
228eaf62fffSJeremy L Thompson   if (!*active_basis) {
229eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
2306aaed0edSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found");
231eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
232eaf62fffSJeremy L Thompson   }
233eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
234eaf62fffSJeremy L Thompson }
235eaf62fffSJeremy L Thompson 
236eaf62fffSJeremy L Thompson /**
2370f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
238e2f04181SAndrew T. Barker 
239e2f04181SAndrew T. Barker   @param[in] op           CeedOperator to find active basis for
2400f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator
241e2f04181SAndrew T. Barker 
242e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
243e2f04181SAndrew T. Barker 
244e2f04181SAndrew T. Barker   @ref Utility
245e2f04181SAndrew T. Barker **/
2462b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
2476aaed0edSJeremy L Thompson   Ceed ceed;
2486aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2496aaed0edSJeremy L Thompson 
250d1d35e2fSjeremylt   *active_rstr = NULL;
2510f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
2522b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
253d1d35e2fSjeremylt     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
2546aaed0edSJeremy L Thompson       if (*active_rstr && *active_rstr != op->input_fields[i]->elem_restr) {
2556aaed0edSJeremy L Thompson         // LCOV_EXCL_START
2566aaed0edSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_MINOR, "Multiple active CeedElemRestrictions found");
2576aaed0edSJeremy L Thompson         // LCOV_EXCL_STOP
2586aaed0edSJeremy L Thompson       }
259d1d35e2fSjeremylt       *active_rstr = op->input_fields[i]->elem_restr;
260e2f04181SAndrew T. Barker       break;
261e2f04181SAndrew T. Barker     }
2622b730f8bSJeremy L Thompson   }
263e2f04181SAndrew T. Barker 
264d1d35e2fSjeremylt   if (!*active_rstr) {
265e2f04181SAndrew T. Barker     // LCOV_EXCL_START
2662b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found");
267e2f04181SAndrew T. Barker     // LCOV_EXCL_STOP
268e2f04181SAndrew T. Barker   }
269e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
270e2f04181SAndrew T. Barker }
271e2f04181SAndrew T. Barker 
272d8dd9a91SJeremy L Thompson /**
273*2788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
274ea61e9acSJeremy L Thompson            For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
275ea61e9acSJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do
276ea61e9acSJeremy L Thompson not have any field of a matching type.
277d8dd9a91SJeremy L Thompson 
278ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
279ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
280ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
281*2788fa27SJeremy L Thompson   @param[in]     values      Values to set
282d8dd9a91SJeremy L Thompson 
283d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
284d8dd9a91SJeremy L Thompson 
285d8dd9a91SJeremy L Thompson   @ref User
286d8dd9a91SJeremy L Thompson **/
287*2788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2882b730f8bSJeremy L Thompson   if (!field_label) {
2893668ca4bSJeremy L Thompson     // LCOV_EXCL_START
2902b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2913668ca4bSJeremy L Thompson     // LCOV_EXCL_STOP
2922b730f8bSJeremy L Thompson   }
2933668ca4bSJeremy L Thompson 
2943668ca4bSJeremy L Thompson   bool is_composite = false;
2952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
296d8dd9a91SJeremy L Thompson   if (is_composite) {
297d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
298d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
299d8dd9a91SJeremy L Thompson 
300c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
301c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3022b730f8bSJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
3033668ca4bSJeremy L Thompson       // LCOV_EXCL_START
304*2788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextLabel does not correspond to the composite operator");
3053668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
3062b730f8bSJeremy L Thompson     }
307d8dd9a91SJeremy L Thompson 
308d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
309d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3103668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
311*2788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
312d8dd9a91SJeremy L Thompson       }
313d8dd9a91SJeremy L Thompson     }
314d8dd9a91SJeremy L Thompson   } else {
3152b730f8bSJeremy L Thompson     if (!op->qf->ctx) {
316d8dd9a91SJeremy L Thompson       // LCOV_EXCL_START
3172b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
318d8dd9a91SJeremy L Thompson       // LCOV_EXCL_STOP
3192b730f8bSJeremy L Thompson     }
320d8dd9a91SJeremy L Thompson 
321*2788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
322*2788fa27SJeremy L Thompson   }
323*2788fa27SJeremy L Thompson 
324*2788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
325*2788fa27SJeremy L Thompson }
326*2788fa27SJeremy L Thompson 
327*2788fa27SJeremy L Thompson /**
328*2788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
329*2788fa27SJeremy L Thompson            For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
330*2788fa27SJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do
331*2788fa27SJeremy L Thompson not have any field of a matching type.
332*2788fa27SJeremy L Thompson 
333*2788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
334*2788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
335*2788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
336*2788fa27SJeremy L Thompson   @param[in]     value       Value to set
337*2788fa27SJeremy L Thompson 
338*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
339*2788fa27SJeremy L Thompson 
340*2788fa27SJeremy L Thompson   @ref User
341*2788fa27SJeremy L Thompson **/
342*2788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
343*2788fa27SJeremy L Thompson                                              void *values) {
344*2788fa27SJeremy L Thompson   if (!field_label) {
345*2788fa27SJeremy L Thompson     // LCOV_EXCL_START
346*2788fa27SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
347*2788fa27SJeremy L Thompson     // LCOV_EXCL_STOP
348*2788fa27SJeremy L Thompson   }
349*2788fa27SJeremy L Thompson 
350*2788fa27SJeremy L Thompson   *(void **)values = NULL;
351*2788fa27SJeremy L Thompson   *num_values      = 0;
352*2788fa27SJeremy L Thompson 
353*2788fa27SJeremy L Thompson   bool is_composite = false;
354*2788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
355*2788fa27SJeremy L Thompson   if (is_composite) {
356*2788fa27SJeremy L Thompson     CeedInt       num_sub;
357*2788fa27SJeremy L Thompson     CeedOperator *sub_operators;
358*2788fa27SJeremy L Thompson 
359*2788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
360*2788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
361*2788fa27SJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
362*2788fa27SJeremy L Thompson       // LCOV_EXCL_START
363*2788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextLabel does not correspond to composite operator");
364*2788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
365*2788fa27SJeremy L Thompson     }
366*2788fa27SJeremy L Thompson 
367*2788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
368*2788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
369*2788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
370*2788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
371*2788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
372*2788fa27SJeremy L Thompson       }
373*2788fa27SJeremy L Thompson     }
374*2788fa27SJeremy L Thompson   } else {
375*2788fa27SJeremy L Thompson     if (!op->qf->ctx) {
376*2788fa27SJeremy L Thompson       // LCOV_EXCL_START
377*2788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
378*2788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
379*2788fa27SJeremy L Thompson     }
380*2788fa27SJeremy L Thompson 
381*2788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
382*2788fa27SJeremy L Thompson   }
383*2788fa27SJeremy L Thompson 
384*2788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
385*2788fa27SJeremy L Thompson }
386*2788fa27SJeremy L Thompson 
387*2788fa27SJeremy L Thompson /**
388*2788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
389*2788fa27SJeremy L Thompson            For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
390*2788fa27SJeremy L Thompson            A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do
391*2788fa27SJeremy L Thompson not have any field of a matching type.
392*2788fa27SJeremy L Thompson 
393*2788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
394*2788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
395*2788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
396*2788fa27SJeremy L Thompson   @param[in]     value       Value to set
397*2788fa27SJeremy L Thompson 
398*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
399*2788fa27SJeremy L Thompson 
400*2788fa27SJeremy L Thompson   @ref User
401*2788fa27SJeremy L Thompson **/
402*2788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
403*2788fa27SJeremy L Thompson   if (!field_label) {
404*2788fa27SJeremy L Thompson     // LCOV_EXCL_START
405*2788fa27SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
406*2788fa27SJeremy L Thompson     // LCOV_EXCL_STOP
407*2788fa27SJeremy L Thompson   }
408*2788fa27SJeremy L Thompson 
409*2788fa27SJeremy L Thompson   bool is_composite = false;
410*2788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
411*2788fa27SJeremy L Thompson   if (is_composite) {
412*2788fa27SJeremy L Thompson     CeedInt       num_sub;
413*2788fa27SJeremy L Thompson     CeedOperator *sub_operators;
414*2788fa27SJeremy L Thompson 
415*2788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
416*2788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
417*2788fa27SJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
418*2788fa27SJeremy L Thompson       // LCOV_EXCL_START
419*2788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextLabel does not correspond to composite operator");
420*2788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
421*2788fa27SJeremy L Thompson     }
422*2788fa27SJeremy L Thompson 
423*2788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
424*2788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
425*2788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
426*2788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
427*2788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
428*2788fa27SJeremy L Thompson       }
429*2788fa27SJeremy L Thompson     }
430*2788fa27SJeremy L Thompson   } else {
431*2788fa27SJeremy L Thompson     if (!op->qf->ctx) {
432*2788fa27SJeremy L Thompson       // LCOV_EXCL_START
433*2788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
434*2788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
435*2788fa27SJeremy L Thompson     }
436*2788fa27SJeremy L Thompson 
437*2788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
438d8dd9a91SJeremy L Thompson   }
439d8dd9a91SJeremy L Thompson 
440d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
441d8dd9a91SJeremy L Thompson }
442d8dd9a91SJeremy L Thompson 
4437a982d89SJeremy L. Thompson /// @}
4447a982d89SJeremy L. Thompson 
4457a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4467a982d89SJeremy L. Thompson /// CeedOperator Backend API
4477a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4487a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
4497a982d89SJeremy L. Thompson /// @{
4507a982d89SJeremy L. Thompson 
4517a982d89SJeremy L. Thompson /**
4527a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
4537a982d89SJeremy L. Thompson 
454ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
455d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
4567a982d89SJeremy L. Thompson 
4577a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4587a982d89SJeremy L. Thompson 
4597a982d89SJeremy L. Thompson   @ref Backend
4607a982d89SJeremy L. Thompson **/
4617a982d89SJeremy L. Thompson 
462d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
4632b730f8bSJeremy L Thompson   if (op->is_composite) {
4647a982d89SJeremy L. Thompson     // LCOV_EXCL_START
4652b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
4667a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4672b730f8bSJeremy L Thompson   }
468d1d35e2fSjeremylt   *num_args = op->num_fields;
469e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4707a982d89SJeremy L. Thompson }
4717a982d89SJeremy L. Thompson 
4727a982d89SJeremy L. Thompson /**
4737a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4747a982d89SJeremy L. Thompson 
475ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
476d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4777a982d89SJeremy L. Thompson 
4787a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4797a982d89SJeremy L. Thompson 
4807a982d89SJeremy L. Thompson   @ref Backend
4817a982d89SJeremy L. Thompson **/
4827a982d89SJeremy L. Thompson 
483d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
484f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
485e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4867a982d89SJeremy L. Thompson }
4877a982d89SJeremy L. Thompson 
4887a982d89SJeremy L. Thompson /**
4897a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4907a982d89SJeremy L. Thompson 
491ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4927a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4937a982d89SJeremy L. Thompson 
4947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4957a982d89SJeremy L. Thompson 
4967a982d89SJeremy L. Thompson   @ref Backend
4977a982d89SJeremy L. Thompson **/
4987a982d89SJeremy L. Thompson 
4997a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
5002b730f8bSJeremy L Thompson   if (op->is_composite) {
5017a982d89SJeremy L. Thompson     // LCOV_EXCL_START
5022b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
5037a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
5042b730f8bSJeremy L Thompson   }
5057a982d89SJeremy L. Thompson   *qf = op->qf;
506e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5077a982d89SJeremy L. Thompson }
5087a982d89SJeremy L. Thompson 
5097a982d89SJeremy L. Thompson /**
510c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
511c04a41a7SJeremy L Thompson 
512ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
513d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
514c04a41a7SJeremy L Thompson 
515c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
516c04a41a7SJeremy L Thompson 
517c04a41a7SJeremy L Thompson   @ref Backend
518c04a41a7SJeremy L Thompson **/
519c04a41a7SJeremy L Thompson 
520d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
521f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
522e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
523c04a41a7SJeremy L Thompson }
524c04a41a7SJeremy L Thompson 
525c04a41a7SJeremy L Thompson /**
5267a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
5277a982d89SJeremy L. Thompson 
528ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
5297a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5327a982d89SJeremy L. Thompson 
5337a982d89SJeremy L. Thompson   @ref Backend
5347a982d89SJeremy L. Thompson **/
5357a982d89SJeremy L. Thompson 
536777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
537777ff853SJeremy L Thompson   *(void **)data = op->data;
538e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5397a982d89SJeremy L. Thompson }
5407a982d89SJeremy L. Thompson 
5417a982d89SJeremy L. Thompson /**
5427a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
5437a982d89SJeremy L. Thompson 
544ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
545ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5487a982d89SJeremy L. Thompson 
5497a982d89SJeremy L. Thompson   @ref Backend
5507a982d89SJeremy L. Thompson **/
5517a982d89SJeremy L. Thompson 
552777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
553777ff853SJeremy L Thompson   op->data = data;
554e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5557a982d89SJeremy L. Thompson }
5567a982d89SJeremy L. Thompson 
5577a982d89SJeremy L. Thompson /**
55834359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
55934359f16Sjeremylt 
560ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
56134359f16Sjeremylt 
56234359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
56334359f16Sjeremylt 
56434359f16Sjeremylt   @ref Backend
56534359f16Sjeremylt **/
5669560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
56734359f16Sjeremylt   op->ref_count++;
56834359f16Sjeremylt   return CEED_ERROR_SUCCESS;
56934359f16Sjeremylt }
57034359f16Sjeremylt 
57134359f16Sjeremylt /**
5727a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
5737a982d89SJeremy L. Thompson 
574ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
5757a982d89SJeremy L. Thompson 
5767a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5777a982d89SJeremy L. Thompson 
5787a982d89SJeremy L. Thompson   @ref Backend
5797a982d89SJeremy L. Thompson **/
5807a982d89SJeremy L. Thompson 
5817a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
582f04ea552SJeremy L Thompson   op->is_backend_setup = true;
583e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5847a982d89SJeremy L. Thompson }
5857a982d89SJeremy L. Thompson 
5867a982d89SJeremy L. Thompson /// @}
5877a982d89SJeremy L. Thompson 
5887a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5897a982d89SJeremy L. Thompson /// CeedOperator Public API
5907a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5917a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
592dfdf5a53Sjeremylt /// @{
593d7b241e6Sjeremylt 
594d7b241e6Sjeremylt /**
595ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
596ea61e9acSJeremy L Thompson            A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
597d7b241e6Sjeremylt 
598ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
599ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
600ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
601ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
602ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
603b11c1e72Sjeremylt 
604b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
605dfdf5a53Sjeremylt 
6067a982d89SJeremy L. Thompson   @ref User
607d7b241e6Sjeremylt  */
6082b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
6095fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
6105fe0d4faSjeremylt     Ceed delegate;
6112b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
6125fe0d4faSjeremylt 
6132b730f8bSJeremy L Thompson     if (!delegate) {
614c042f62fSJeremy L Thompson       // LCOV_EXCL_START
6152b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
616c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
6172b730f8bSJeremy L Thompson     }
6185fe0d4faSjeremylt 
6192b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
620e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6215fe0d4faSjeremylt   }
6225fe0d4faSjeremylt 
6232b730f8bSJeremy L Thompson   if (!qf || qf == CEED_QFUNCTION_NONE) {
624b3b7035fSJeremy L Thompson     // LCOV_EXCL_START
6252b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
626b3b7035fSJeremy L Thompson     // LCOV_EXCL_STOP
6272b730f8bSJeremy L Thompson   }
6282b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
629d7b241e6Sjeremylt   (*op)->ceed = ceed;
6302b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
631d1d35e2fSjeremylt   (*op)->ref_count   = 1;
632d7b241e6Sjeremylt   (*op)->qf          = qf;
6332b104005SJeremy L Thompson   (*op)->input_size  = -1;
6342b104005SJeremy L Thompson   (*op)->output_size = -1;
6352b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
636442e7f0bSjeremylt   if (dqf && dqf != CEED_QFUNCTION_NONE) {
637d7b241e6Sjeremylt     (*op)->dqf = dqf;
6382b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqf));
639442e7f0bSjeremylt   }
640442e7f0bSjeremylt   if (dqfT && dqfT != CEED_QFUNCTION_NONE) {
641d7b241e6Sjeremylt     (*op)->dqfT = dqfT;
6422b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqfT));
643442e7f0bSjeremylt   }
6442b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
6452b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
6462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
6472b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
648e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
649d7b241e6Sjeremylt }
650d7b241e6Sjeremylt 
651d7b241e6Sjeremylt /**
65252d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
65352d6035fSJeremy L Thompson 
654ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
655ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
65652d6035fSJeremy L Thompson 
65752d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
65852d6035fSJeremy L Thompson 
6597a982d89SJeremy L. Thompson   @ref User
66052d6035fSJeremy L Thompson  */
66152d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
66252d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
66352d6035fSJeremy L Thompson     Ceed delegate;
6642b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
66552d6035fSJeremy L Thompson 
666250756a7Sjeremylt     if (delegate) {
6672b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
668e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
66952d6035fSJeremy L Thompson     }
670250756a7Sjeremylt   }
67152d6035fSJeremy L Thompson 
6722b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
67352d6035fSJeremy L Thompson   (*op)->ceed = ceed;
6742b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
675996d9ab5SJed Brown   (*op)->ref_count    = 1;
676f04ea552SJeremy L Thompson   (*op)->is_composite = true;
6772b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
6782b104005SJeremy L Thompson   (*op)->input_size  = -1;
6792b104005SJeremy L Thompson   (*op)->output_size = -1;
680250756a7Sjeremylt 
681250756a7Sjeremylt   if (ceed->CompositeOperatorCreate) {
6822b730f8bSJeremy L Thompson     CeedCall(ceed->CompositeOperatorCreate(*op));
683250756a7Sjeremylt   }
684e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68552d6035fSJeremy L Thompson }
68652d6035fSJeremy L Thompson 
68752d6035fSJeremy L Thompson /**
688ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
689ea61e9acSJeremy L Thompson            Both pointers should be destroyed with `CeedOperatorDestroy()`.
690ea61e9acSJeremy L Thompson            Note: If `*op_copy` is non-NULL, then it is assumed that `*op_copy` is a pointer to a CeedOperator.
691ea61e9acSJeremy L Thompson              This CeedOperator will be destroyed if `*op_copy` is the only reference to this CeedOperator.
6929560d06aSjeremylt 
693ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
694ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
6959560d06aSjeremylt 
6969560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6979560d06aSjeremylt 
6989560d06aSjeremylt   @ref User
6999560d06aSjeremylt **/
7009560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
7012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
7022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
7039560d06aSjeremylt   *op_copy = op;
7049560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7059560d06aSjeremylt }
7069560d06aSjeremylt 
7079560d06aSjeremylt /**
708ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
709d7b241e6Sjeremylt 
710ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
711ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
712ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
713d7b241e6Sjeremylt 
714ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
715ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
716d7b241e6Sjeremylt 
717ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
718ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
719ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
720ea61e9acSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points
721ea61e9acSJeremy 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 if using @ref
722ea61e9acSJeremy L Thompson CEED_EVAL_WEIGHT in the QFunction
723b11c1e72Sjeremylt 
724b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
725dfdf5a53Sjeremylt 
7267a982d89SJeremy L. Thompson   @ref User
727b11c1e72Sjeremylt **/
7282b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
7292b730f8bSJeremy L Thompson   if (op->is_composite) {
730c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7312b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
732c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7332b730f8bSJeremy L Thompson   }
7342b730f8bSJeremy L Thompson   if (op->is_immutable) {
735f04ea552SJeremy L Thompson     // LCOV_EXCL_START
7362b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
737f04ea552SJeremy L Thompson     // LCOV_EXCL_STOP
7382b730f8bSJeremy L Thompson   }
7392b730f8bSJeremy L Thompson   if (!r) {
740c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7412b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
742c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7432b730f8bSJeremy L Thompson   }
7442b730f8bSJeremy L Thompson   if (!b) {
745c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7462b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
747c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7482b730f8bSJeremy L Thompson   }
7492b730f8bSJeremy L Thompson   if (!v) {
750c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7512b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
752c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7532b730f8bSJeremy L Thompson   }
75452d6035fSJeremy L Thompson 
755d1d35e2fSjeremylt   CeedInt num_elem;
7562b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
7572b730f8bSJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE && op->has_restriction && op->num_elem != num_elem) {
758c042f62fSJeremy L Thompson     // LCOV_EXCL_START
759e15f9bd0SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION,
7602b730f8bSJeremy L Thompson                      "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
761c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7622b730f8bSJeremy L Thompson   }
763d7b241e6Sjeremylt 
76478464608Sjeremylt   CeedInt num_qpts = 0;
765e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
7662b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
7672b730f8bSJeremy L Thompson     if (op->num_qpts && op->num_qpts != num_qpts) {
768c042f62fSJeremy L Thompson       // LCOV_EXCL_START
769e15f9bd0SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_DIMENSION,
7702b730f8bSJeremy L Thompson                        "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts);
771c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
772d7b241e6Sjeremylt     }
7732b730f8bSJeremy L Thompson   }
774d1d35e2fSjeremylt   CeedQFunctionField qf_field;
775d1d35e2fSjeremylt   CeedOperatorField *op_field;
7762b104005SJeremy L Thompson   bool               is_input = true;
777d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
778d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
779d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
780d1d35e2fSjeremylt       op_field = &op->input_fields[i];
781d7b241e6Sjeremylt       goto found;
782d7b241e6Sjeremylt     }
783d7b241e6Sjeremylt   }
7842b104005SJeremy L Thompson   is_input = false;
785d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
786d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
787d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
788d1d35e2fSjeremylt       op_field = &op->output_fields[i];
789d7b241e6Sjeremylt       goto found;
790d7b241e6Sjeremylt     }
791d7b241e6Sjeremylt   }
792c042f62fSJeremy L Thompson   // LCOV_EXCL_START
7932b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
794c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
795d7b241e6Sjeremylt found:
7962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
7972b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
798e15f9bd0SJeremy L Thompson 
7992b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
8002b104005SJeremy L Thompson     CeedSize l_size;
8012b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
8022b104005SJeremy L Thompson     if (is_input) {
8032b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
8042b730f8bSJeremy L Thompson       if (l_size != op->input_size) {
8052b104005SJeremy L Thompson         // LCOV_EXCL_START
8062b730f8bSJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->input_size);
8072b104005SJeremy L Thompson         // LCOV_EXCL_STOP
8082b730f8bSJeremy L Thompson       }
8092b104005SJeremy L Thompson     } else {
8102b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
8112b730f8bSJeremy L Thompson       if (l_size != op->output_size) {
8122b104005SJeremy L Thompson         // LCOV_EXCL_START
8132b730f8bSJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->output_size);
8142b104005SJeremy L Thompson         // LCOV_EXCL_STOP
8152b104005SJeremy L Thompson       }
8162b104005SJeremy L Thompson     }
8172b730f8bSJeremy L Thompson   }
8182b104005SJeremy L Thompson 
819d1d35e2fSjeremylt   (*op_field)->vec = v;
820e15f9bd0SJeremy L Thompson   if (v != CEED_VECTOR_ACTIVE && v != CEED_VECTOR_NONE) {
8212b730f8bSJeremy L Thompson     CeedCall(CeedVectorReference(v));
822e15f9bd0SJeremy L Thompson   }
823e15f9bd0SJeremy L Thompson 
824d1d35e2fSjeremylt   (*op_field)->elem_restr = r;
8252b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionReference(r));
826e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
827d1d35e2fSjeremylt     op->num_elem        = num_elem;
828d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
829e15f9bd0SJeremy L Thompson   }
830d99fa3c5SJeremy L Thompson 
831d1d35e2fSjeremylt   (*op_field)->basis = b;
832e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
833cd4dfc48Sjeremylt     if (!op->num_qpts) {
8342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
835cd4dfc48Sjeremylt     }
8362b730f8bSJeremy L Thompson     CeedCall(CeedBasisReference(b));
837e15f9bd0SJeremy L Thompson   }
838e15f9bd0SJeremy L Thompson 
839d1d35e2fSjeremylt   op->num_fields += 1;
8402b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
841e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
842d7b241e6Sjeremylt }
843d7b241e6Sjeremylt 
844d7b241e6Sjeremylt /**
84543bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
84643bbe138SJeremy L Thompson 
847ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
848f04ea552SJeremy L Thompson 
849ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
850f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
85143bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
852f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
85343bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
85443bbe138SJeremy L Thompson 
85543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85643bbe138SJeremy L Thompson 
857e9b533fbSJeremy L Thompson   @ref Advanced
85843bbe138SJeremy L Thompson **/
8592b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
86043bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
8612b730f8bSJeremy L Thompson   if (op->is_composite) {
86243bbe138SJeremy L Thompson     // LCOV_EXCL_START
8632b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
86443bbe138SJeremy L Thompson     // LCOV_EXCL_STOP
8652b730f8bSJeremy L Thompson   }
8662b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
86743bbe138SJeremy L Thompson 
86843bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
86943bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
87043bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
87143bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
87243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87343bbe138SJeremy L Thompson }
87443bbe138SJeremy L Thompson 
87543bbe138SJeremy L Thompson /**
87628567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
87728567f8fSJeremy L Thompson 
878ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
87928567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
88028567f8fSJeremy L Thompson 
88128567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88228567f8fSJeremy L Thompson 
883e9b533fbSJeremy L Thompson   @ref Advanced
88428567f8fSJeremy L Thompson **/
88528567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
88628567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
88728567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
88828567f8fSJeremy L Thompson }
88928567f8fSJeremy L Thompson 
89028567f8fSJeremy L Thompson /**
89143bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
89243bbe138SJeremy L Thompson 
893ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
89443bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
89543bbe138SJeremy L Thompson 
89643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89743bbe138SJeremy L Thompson 
898e9b533fbSJeremy L Thompson   @ref Advanced
89943bbe138SJeremy L Thompson **/
9002b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
90143bbe138SJeremy L Thompson   *rstr = op_field->elem_restr;
90243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
90343bbe138SJeremy L Thompson }
90443bbe138SJeremy L Thompson 
90543bbe138SJeremy L Thompson /**
90643bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
90743bbe138SJeremy L Thompson 
908ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
90943bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
91043bbe138SJeremy L Thompson 
91143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
91243bbe138SJeremy L Thompson 
913e9b533fbSJeremy L Thompson   @ref Advanced
91443bbe138SJeremy L Thompson **/
91543bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
91643bbe138SJeremy L Thompson   *basis = op_field->basis;
91743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
91843bbe138SJeremy L Thompson }
91943bbe138SJeremy L Thompson 
92043bbe138SJeremy L Thompson /**
92143bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
92243bbe138SJeremy L Thompson 
923ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
92443bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
92543bbe138SJeremy L Thompson 
92643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
92743bbe138SJeremy L Thompson 
928e9b533fbSJeremy L Thompson   @ref Advanced
92943bbe138SJeremy L Thompson **/
93043bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
93143bbe138SJeremy L Thompson   *vec = op_field->vec;
93243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
93343bbe138SJeremy L Thompson }
93443bbe138SJeremy L Thompson 
93543bbe138SJeremy L Thompson /**
93652d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
937288c0443SJeremy L Thompson 
938ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
939ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
94052d6035fSJeremy L Thompson 
94152d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94252d6035fSJeremy L Thompson 
9437a982d89SJeremy L. Thompson   @ref User
94452d6035fSJeremy L Thompson  */
9452b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
9462b730f8bSJeremy L Thompson   if (!composite_op->is_composite) {
947c042f62fSJeremy L Thompson     // LCOV_EXCL_START
9482b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
9492b104005SJeremy L Thompson     // LCOV_EXCL_STOP
9502b104005SJeremy L Thompson   }
9512b104005SJeremy L Thompson 
9522b730f8bSJeremy L Thompson   if (composite_op->num_suboperators == CEED_COMPOSITE_MAX) {
9532b730f8bSJeremy L Thompson     // LCOV_EXCL_START
9542b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
9552b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
9562b730f8bSJeremy L Thompson   }
9572b730f8bSJeremy L Thompson   if (composite_op->is_immutable) {
9582b730f8bSJeremy L Thompson     // LCOV_EXCL_START
9592b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
9602b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
9612b730f8bSJeremy L Thompson   }
9622b730f8bSJeremy L Thompson 
9632b730f8bSJeremy L Thompson   {
9642b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
9652b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
9662b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
9672b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
9682b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
9692b730f8bSJeremy L Thompson     if ((input_size != -1 && input_size != composite_op->input_size) || (output_size != -1 && output_size != composite_op->output_size)) {
9702b730f8bSJeremy L Thompson       // LCOV_EXCL_START
9712b730f8bSJeremy L Thompson       return CeedError(composite_op->ceed, CEED_ERROR_MAJOR,
9722b730f8bSJeremy L Thompson                        "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9732b730f8bSJeremy L Thompson                        "shape (%td, %td)",
9742b730f8bSJeremy L Thompson                        composite_op->input_size, composite_op->output_size, input_size, output_size);
9752b730f8bSJeremy L Thompson       // LCOV_EXCL_STOP
9762b730f8bSJeremy L Thompson     }
9772b730f8bSJeremy L Thompson   }
9782b730f8bSJeremy L Thompson 
979d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
9802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
981d1d35e2fSjeremylt   composite_op->num_suboperators++;
982e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98352d6035fSJeremy L Thompson }
98452d6035fSJeremy L Thompson 
98552d6035fSJeremy L Thompson /**
98675f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
98775f0d5a4SJeremy L Thompson 
98875f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
98975f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
99075f0d5a4SJeremy L Thompson 
99175f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
99275f0d5a4SJeremy L Thompson 
99375f0d5a4SJeremy L Thompson   @ref Backend
99475f0d5a4SJeremy L Thompson **/
99575f0d5a4SJeremy L Thompson 
996c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
99775f0d5a4SJeremy L Thompson   if (!op->is_composite) {
99875f0d5a4SJeremy L Thompson     // LCOV_EXCL_START
99975f0d5a4SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator");
100075f0d5a4SJeremy L Thompson     // LCOV_EXCL_STOP
100175f0d5a4SJeremy L Thompson   }
100275f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
100375f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
100475f0d5a4SJeremy L Thompson }
100575f0d5a4SJeremy L Thompson 
100675f0d5a4SJeremy L Thompson /**
100775f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
100875f0d5a4SJeremy L Thompson 
100975f0d5a4SJeremy L Thompson   @param op                  CeedOperator
101075f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
101175f0d5a4SJeremy L Thompson 
101275f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
101375f0d5a4SJeremy L Thompson 
101475f0d5a4SJeremy L Thompson   @ref Backend
101575f0d5a4SJeremy L Thompson **/
101675f0d5a4SJeremy L Thompson 
1017c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
101875f0d5a4SJeremy L Thompson   if (!op->is_composite) {
101975f0d5a4SJeremy L Thompson     // LCOV_EXCL_START
102075f0d5a4SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator");
102175f0d5a4SJeremy L Thompson     // LCOV_EXCL_STOP
102275f0d5a4SJeremy L Thompson   }
102375f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
102475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
102575f0d5a4SJeremy L Thompson }
102675f0d5a4SJeremy L Thompson 
102775f0d5a4SJeremy L Thompson /**
10284db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
10294db537f9SJeremy L Thompson 
10304db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
10314db537f9SJeremy L Thompson 
10324db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10334db537f9SJeremy L Thompson 
10344db537f9SJeremy L Thompson   @ref User
10354db537f9SJeremy L Thompson **/
10364db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
10374db537f9SJeremy L Thompson   Ceed ceed;
10382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
10394db537f9SJeremy L Thompson 
10402b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
10414db537f9SJeremy L Thompson 
10424db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
10434db537f9SJeremy L Thompson   if (op->is_composite) {
104443622462SJeremy L Thompson     if (!op->num_suboperators) {
104543622462SJeremy L Thompson       // Empty operator setup
104643622462SJeremy L Thompson       op->input_size  = 0;
104743622462SJeremy L Thompson       op->output_size = 0;
104843622462SJeremy L Thompson     } else {
10494db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
10502b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
10514db537f9SJeremy L Thompson       }
10522b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
10532b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
10542b104005SJeremy L Thompson       CeedSize input_size, output_size;
10552b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
105643622462SJeremy L Thompson     }
10574db537f9SJeremy L Thompson   } else {
10582b730f8bSJeremy L Thompson     if (op->num_fields == 0) {
10594db537f9SJeremy L Thompson       // LCOV_EXCL_START
10604db537f9SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
10614db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
10622b730f8bSJeremy L Thompson     }
10632b730f8bSJeremy L Thompson     if (op->num_fields < qf->num_input_fields + qf->num_output_fields) {
10644db537f9SJeremy L Thompson       // LCOV_EXCL_START
10654db537f9SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
10664db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
10672b730f8bSJeremy L Thompson     }
10682b730f8bSJeremy L Thompson     if (!op->has_restriction) {
10694db537f9SJeremy L Thompson       // LCOV_EXCL_START
10702b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
10714db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
10722b730f8bSJeremy L Thompson     }
10732b730f8bSJeremy L Thompson     if (op->num_qpts == 0) {
10744db537f9SJeremy L Thompson       // LCOV_EXCL_START
10752b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one non-collocated basis is required or the number of quadrature points must be set");
10764db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
10774db537f9SJeremy L Thompson     }
10782b730f8bSJeremy L Thompson   }
10794db537f9SJeremy L Thompson 
10804db537f9SJeremy L Thompson   // Flag as immutable and ready
10814db537f9SJeremy L Thompson   op->is_interface_setup = true;
10822b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
10832b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
10842b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
10854db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10864db537f9SJeremy L Thompson }
10874db537f9SJeremy L Thompson 
10884db537f9SJeremy L Thompson /**
1089c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
1090ea61e9acSJeremy L Thompson            Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
1091c9366a6bSJeremy L Thompson 
1092c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
1093c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
1094c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
1095c9366a6bSJeremy L Thompson 
1096c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1097c9366a6bSJeremy L Thompson 
1098c9366a6bSJeremy L Thompson   @ref User
1099c9366a6bSJeremy L Thompson **/
11002b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1101c9366a6bSJeremy L Thompson   bool is_composite;
1102c9366a6bSJeremy L Thompson 
11032b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
11042b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1105c9366a6bSJeremy L Thompson 
11062b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11072b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1108c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1109c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
11102b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
11112b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
11122b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
11132b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
11142b730f8bSJeremy L Thompson       if ((sub_input_size != -1 && sub_input_size != op->input_size) || (sub_output_size != -1 && sub_output_size != op->output_size)) {
11152b104005SJeremy L Thompson         // LCOV_EXCL_START
11162b104005SJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_MAJOR,
11172b730f8bSJeremy L Thompson                          "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
11182b730f8bSJeremy L Thompson                          "shape (%td, %td)",
11192b104005SJeremy L Thompson                          op->input_size, op->output_size, input_size, output_size);
11202b104005SJeremy L Thompson         // LCOV_EXCL_STOP
1121c9366a6bSJeremy L Thompson       }
1122c9366a6bSJeremy L Thompson     }
11232b730f8bSJeremy L Thompson   }
1124c9366a6bSJeremy L Thompson 
1125c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1126c9366a6bSJeremy L Thompson }
1127c9366a6bSJeremy L Thompson 
1128c9366a6bSJeremy L Thompson /**
1129beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
1130ea61e9acSJeremy L Thompson            When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
1131ea61e9acSJeremy L Thompson `CeedOperatorLinearAssemble*` function is called. When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused
1132ea61e9acSJeremy L Thompson between calls to `CeedOperatorSetQFunctionAssemblyDataUpdated`.
11338b919e6bSJeremy L Thompson 
1134beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
1135beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
11368b919e6bSJeremy L Thompson 
11378b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11388b919e6bSJeremy L Thompson 
11398b919e6bSJeremy L Thompson   @ref Advanced
11408b919e6bSJeremy L Thompson **/
11412b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
11428b919e6bSJeremy L Thompson   bool is_composite;
11438b919e6bSJeremy L Thompson 
11442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11458b919e6bSJeremy L Thompson   if (is_composite) {
11468b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
11472b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
11488b919e6bSJeremy L Thompson     }
11498b919e6bSJeremy L Thompson   } else {
11502b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1151beecbf24SJeremy L Thompson   }
1152beecbf24SJeremy L Thompson 
1153beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1154beecbf24SJeremy L Thompson }
1155beecbf24SJeremy L Thompson 
1156beecbf24SJeremy L Thompson /**
1157beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1158beecbf24SJeremy L Thompson 
1159beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
11606e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1161beecbf24SJeremy L Thompson 
1162beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1163beecbf24SJeremy L Thompson 
1164beecbf24SJeremy L Thompson   @ref Advanced
1165beecbf24SJeremy L Thompson **/
11662b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1167beecbf24SJeremy L Thompson   bool is_composite;
1168beecbf24SJeremy L Thompson 
11692b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1170beecbf24SJeremy L Thompson   if (is_composite) {
1171beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
11722b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1173beecbf24SJeremy L Thompson     }
1174beecbf24SJeremy L Thompson   } else {
11752b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
11768b919e6bSJeremy L Thompson   }
11778b919e6bSJeremy L Thompson 
11788b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
11798b919e6bSJeremy L Thompson }
11808b919e6bSJeremy L Thompson 
11818b919e6bSJeremy L Thompson /**
1182cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
1183ea61e9acSJeremy L Thompson            This should be used when creating a CeedOperator where every field has a collocated basis.
1184ea61e9acSJeremy L Thompson            This function cannot be used for composite CeedOperators.
1185cd4dfc48Sjeremylt 
1186ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1187ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1188cd4dfc48Sjeremylt 
1189cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1190cd4dfc48Sjeremylt 
1191e9b533fbSJeremy L Thompson   @ref Advanced
1192cd4dfc48Sjeremylt **/
1193cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
11942b730f8bSJeremy L Thompson   if (op->is_composite) {
1195cd4dfc48Sjeremylt     // LCOV_EXCL_START
11962b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1197cd4dfc48Sjeremylt     // LCOV_EXCL_STOP
11982b730f8bSJeremy L Thompson   }
11992b730f8bSJeremy L Thompson   if (op->num_qpts) {
1200cd4dfc48Sjeremylt     // LCOV_EXCL_START
12012b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
1202cd4dfc48Sjeremylt     // LCOV_EXCL_STOP
12032b730f8bSJeremy L Thompson   }
12042b730f8bSJeremy L Thompson   if (op->is_immutable) {
1205f04ea552SJeremy L Thompson     // LCOV_EXCL_START
12062b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1207f04ea552SJeremy L Thompson     // LCOV_EXCL_STOP
12082b730f8bSJeremy L Thompson   }
1209cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1210cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1211cd4dfc48Sjeremylt }
1212cd4dfc48Sjeremylt 
1213cd4dfc48Sjeremylt /**
1214ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1215ea6b5821SJeremy L Thompson 
1216ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1217ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1218ea6b5821SJeremy L Thompson 
1219ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1220ea6b5821SJeremy L Thompson 
1221ea6b5821SJeremy L Thompson   @ref User
1222ea6b5821SJeremy L Thompson **/
1223ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1224ea6b5821SJeremy L Thompson   char  *name_copy;
1225ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1226ea6b5821SJeremy L Thompson 
12272b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1228ea6b5821SJeremy L Thompson   if (name_len > 0) {
12292b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1230ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1231ea6b5821SJeremy L Thompson     op->name = name_copy;
1232ea6b5821SJeremy L Thompson   }
1233ea6b5821SJeremy L Thompson 
1234ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1235ea6b5821SJeremy L Thompson }
1236ea6b5821SJeremy L Thompson 
1237ea6b5821SJeremy L Thompson /**
12387a982d89SJeremy L. Thompson   @brief View a CeedOperator
12397a982d89SJeremy L. Thompson 
12407a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
12417a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
12427a982d89SJeremy L. Thompson 
12437a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
12447a982d89SJeremy L. Thompson 
12457a982d89SJeremy L. Thompson   @ref User
12467a982d89SJeremy L. Thompson **/
12477a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1248ea6b5821SJeremy L Thompson   bool has_name = op->name;
12497a982d89SJeremy L. Thompson 
1250f04ea552SJeremy L Thompson   if (op->is_composite) {
12512b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
12527a982d89SJeremy L. Thompson 
1253d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1254ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
12552b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
12562b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
12577a982d89SJeremy L. Thompson     }
12587a982d89SJeremy L. Thompson   } else {
12592b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
12602b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
12617a982d89SJeremy L. Thompson   }
1262e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12637a982d89SJeremy L. Thompson }
12643bd813ffSjeremylt 
12653bd813ffSjeremylt /**
1266b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1267b7c9bbdaSJeremy L Thompson 
1268ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1269b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1270b7c9bbdaSJeremy L Thompson 
1271b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1272b7c9bbdaSJeremy L Thompson 
1273b7c9bbdaSJeremy L Thompson   @ref Advanced
1274b7c9bbdaSJeremy L Thompson **/
1275b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1276b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1277b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1278b7c9bbdaSJeremy L Thompson }
1279b7c9bbdaSJeremy L Thompson 
1280b7c9bbdaSJeremy L Thompson /**
1281b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1282b7c9bbdaSJeremy L Thompson 
1283ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1284b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1285b7c9bbdaSJeremy L Thompson 
1286b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1287b7c9bbdaSJeremy L Thompson 
1288b7c9bbdaSJeremy L Thompson   @ref Advanced
1289b7c9bbdaSJeremy L Thompson **/
1290b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
12912b730f8bSJeremy L Thompson   if (op->is_composite) {
1292b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_START
12932b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1294b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_STOP
12952b730f8bSJeremy L Thompson   }
1296b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1297b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1298b7c9bbdaSJeremy L Thompson }
1299b7c9bbdaSJeremy L Thompson 
1300b7c9bbdaSJeremy L Thompson /**
1301b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1302b7c9bbdaSJeremy L Thompson 
1303ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1304b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1305b7c9bbdaSJeremy L Thompson 
1306b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1307b7c9bbdaSJeremy L Thompson 
1308b7c9bbdaSJeremy L Thompson   @ref Advanced
1309b7c9bbdaSJeremy L Thompson **/
1310b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
13112b730f8bSJeremy L Thompson   if (op->is_composite) {
1312b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_START
13132b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1314b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_STOP
13152b730f8bSJeremy L Thompson   }
1316b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1317b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1318b7c9bbdaSJeremy L Thompson }
1319b7c9bbdaSJeremy L Thompson 
1320b7c9bbdaSJeremy L Thompson /**
13216e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
13226e15d496SJeremy L Thompson 
1323ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1324ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
13256e15d496SJeremy L Thompson 
13266e15d496SJeremy L Thompson   @ref Backend
13276e15d496SJeremy L Thompson **/
13289d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
13296e15d496SJeremy L Thompson   bool is_composite;
13302b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
13316e15d496SJeremy L Thompson 
13326e15d496SJeremy L Thompson   *flops = 0;
13332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13346e15d496SJeremy L Thompson   if (is_composite) {
13356e15d496SJeremy L Thompson     CeedInt num_suboperators;
1336c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13376e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1338c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13396e15d496SJeremy L Thompson 
13406e15d496SJeremy L Thompson     // FLOPs for each suboperator
13416e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
13429d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
13432b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
13446e15d496SJeremy L Thompson       *flops += suboperator_flops;
13456e15d496SJeremy L Thompson     }
13466e15d496SJeremy L Thompson   } else {
13476e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
13486e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
13496e15d496SJeremy L Thompson 
13502b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
13516e15d496SJeremy L Thompson 
13526e15d496SJeremy L Thompson     CeedInt num_elem = 0;
13532b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
13546e15d496SJeremy L Thompson     // Input FLOPs
13556e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
13566e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
13579d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
13586e15d496SJeremy L Thompson 
13592b730f8bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_restr, CEED_NOTRANSPOSE, &restr_flops));
13606e15d496SJeremy L Thompson         *flops += restr_flops;
13612b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
13626e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
13636e15d496SJeremy L Thompson       }
13646e15d496SJeremy L Thompson     }
13656e15d496SJeremy L Thompson     // QF FLOPs
13669d36ca50SJeremy L Thompson     CeedInt  num_qpts;
13679d36ca50SJeremy L Thompson     CeedSize qf_flops;
13682b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
13692b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
13706e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
13716e15d496SJeremy L Thompson     // Output FLOPs
13726e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
13736e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
13749d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
13756e15d496SJeremy L Thompson 
13762b730f8bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_restr, CEED_TRANSPOSE, &restr_flops));
13776e15d496SJeremy L Thompson         *flops += restr_flops;
13782b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
13796e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
13806e15d496SJeremy L Thompson       }
13816e15d496SJeremy L Thompson     }
13826e15d496SJeremy L Thompson   }
13836e15d496SJeremy L Thompson 
13846e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13856e15d496SJeremy L Thompson }
13866e15d496SJeremy L Thompson 
13876e15d496SJeremy L Thompson /**
1388ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
13893668ca4bSJeremy L Thompson 
13903668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
13913668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
13923668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
13933668ca4bSJeremy L Thompson 
13943668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13953668ca4bSJeremy L Thompson 
13963668ca4bSJeremy L Thompson   @ref User
13973668ca4bSJeremy L Thompson **/
13982b730f8bSJeremy L Thompson int CeedOperatorContextGetFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
13993668ca4bSJeremy L Thompson   bool is_composite;
14002b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14012b730f8bSJeremy L Thompson 
14023668ca4bSJeremy L Thompson   if (is_composite) {
14033668ca4bSJeremy L Thompson     // Check if composite label already created
14043668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
14053668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
14063668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
14073668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
14083668ca4bSJeremy L Thompson       }
14093668ca4bSJeremy L Thompson     }
14103668ca4bSJeremy L Thompson 
14113668ca4bSJeremy L Thompson     // Create composite label if needed
14123668ca4bSJeremy L Thompson     CeedInt               num_sub;
14133668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
14143668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
14153668ca4bSJeremy L Thompson 
14162b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1417c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1418c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
14203668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
14213668ca4bSJeremy L Thompson 
14223668ca4bSJeremy L Thompson     bool label_found = false;
14233668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
14243668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
14253668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
14262b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
14273668ca4bSJeremy L Thompson         if (new_field_label_i) {
14283668ca4bSJeremy L Thompson           label_found                    = true;
14293668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
14303668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
14313668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
14322b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
14337bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
14342b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
14352b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
14362b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
14377bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
14387bfe0f0eSJeremy L Thompson           } else {
14397bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
14407bfe0f0eSJeremy L Thompson           }
14412b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
14427bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
14432b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
14442b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
14457bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
14467bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
14477bfe0f0eSJeremy L Thompson           } else {
14487bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
14497bfe0f0eSJeremy L Thompson           }
14503668ca4bSJeremy L Thompson         }
14513668ca4bSJeremy L Thompson       }
14523668ca4bSJeremy L Thompson     }
14533668ca4bSJeremy L Thompson     if (!label_found) {
14543668ca4bSJeremy L Thompson       // LCOV_EXCL_START
14552b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
14562b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
14573668ca4bSJeremy L Thompson       *field_label = NULL;
14583668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
14593668ca4bSJeremy L Thompson     } else {
14603668ca4bSJeremy L Thompson       // Move new composite label to operator
14613668ca4bSJeremy L Thompson       if (op->num_context_labels == 0) {
14622b730f8bSJeremy L Thompson         CeedCall(CeedCalloc(1, &op->context_labels));
14633668ca4bSJeremy L Thompson         op->max_context_labels = 1;
14643668ca4bSJeremy L Thompson       } else if (op->num_context_labels == op->max_context_labels) {
14652b730f8bSJeremy L Thompson         CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
14663668ca4bSJeremy L Thompson         op->max_context_labels *= 2;
14673668ca4bSJeremy L Thompson       }
14683668ca4bSJeremy L Thompson       op->context_labels[op->num_context_labels] = new_field_label;
14693668ca4bSJeremy L Thompson       *field_label                               = new_field_label;
14703668ca4bSJeremy L Thompson       op->num_context_labels++;
14713668ca4bSJeremy L Thompson     }
14723668ca4bSJeremy L Thompson 
14733668ca4bSJeremy L Thompson     return CEED_ERROR_SUCCESS;
14743668ca4bSJeremy L Thompson   } else {
14753668ca4bSJeremy L Thompson     return CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label);
14763668ca4bSJeremy L Thompson   }
14773668ca4bSJeremy L Thompson }
14783668ca4bSJeremy L Thompson 
14793668ca4bSJeremy L Thompson /**
1480*2788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
1481*2788fa27SJeremy L Thompson            For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1482d8dd9a91SJeremy L Thompson 
1483ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1484*2788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1485ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1486d8dd9a91SJeremy L Thompson 
1487d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1488d8dd9a91SJeremy L Thompson 
1489d8dd9a91SJeremy L Thompson   @ref User
1490d8dd9a91SJeremy L Thompson **/
14912b730f8bSJeremy L Thompson int CeedOperatorContextSetDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
14922b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1493d8dd9a91SJeremy L Thompson }
1494d8dd9a91SJeremy L Thompson 
1495d8dd9a91SJeremy L Thompson /**
1496*2788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
1497*2788fa27SJeremy L Thompson            For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
1498*2788fa27SJeremy L Thompson 
1499*2788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
1500*2788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1501*2788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
1502*2788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
1503*2788fa27SJeremy L Thompson 
1504*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1505*2788fa27SJeremy L Thompson 
1506*2788fa27SJeremy L Thompson   @ref User
1507*2788fa27SJeremy L Thompson **/
1508*2788fa27SJeremy L Thompson int CeedOperatorContextGetDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
1509*2788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
1510*2788fa27SJeremy L Thompson }
1511*2788fa27SJeremy L Thompson 
1512*2788fa27SJeremy L Thompson /**
1513*2788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
1514*2788fa27SJeremy L Thompson 
1515*2788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
1516*2788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
1517*2788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
1518*2788fa27SJeremy L Thompson 
1519*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1520*2788fa27SJeremy L Thompson 
1521*2788fa27SJeremy L Thompson   @ref User
1522*2788fa27SJeremy L Thompson **/
1523*2788fa27SJeremy L Thompson int CeedOperatorContextRestoreDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
1524*2788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1525*2788fa27SJeremy L Thompson }
1526*2788fa27SJeremy L Thompson 
1527*2788fa27SJeremy L Thompson /**
1528*2788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
1529*2788fa27SJeremy L Thompson            For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1530d8dd9a91SJeremy L Thompson 
1531ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1532ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1533ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1534d8dd9a91SJeremy L Thompson 
1535d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1536d8dd9a91SJeremy L Thompson 
1537d8dd9a91SJeremy L Thompson   @ref User
1538d8dd9a91SJeremy L Thompson **/
15392b730f8bSJeremy L Thompson int CeedOperatorContextSetInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
15402b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1541d8dd9a91SJeremy L Thompson }
1542d8dd9a91SJeremy L Thompson 
1543d8dd9a91SJeremy L Thompson /**
1544*2788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
1545*2788fa27SJeremy L Thompson            For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
1546*2788fa27SJeremy L Thompson 
1547*2788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
1548*2788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1549*2788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
1550*2788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
1551*2788fa27SJeremy L Thompson 
1552*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1553*2788fa27SJeremy L Thompson 
1554*2788fa27SJeremy L Thompson   @ref User
1555*2788fa27SJeremy L Thompson **/
1556*2788fa27SJeremy L Thompson int CeedOperatorContextGetInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
1557*2788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
1558*2788fa27SJeremy L Thompson }
1559*2788fa27SJeremy L Thompson 
1560*2788fa27SJeremy L Thompson /**
1561*2788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
1562*2788fa27SJeremy L Thompson 
1563*2788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
1564*2788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1565*2788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
1566*2788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
1567*2788fa27SJeremy L Thompson 
1568*2788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1569*2788fa27SJeremy L Thompson 
1570*2788fa27SJeremy L Thompson   @ref User
1571*2788fa27SJeremy L Thompson **/
1572*2788fa27SJeremy L Thompson int CeedOperatorContextRestoreInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
1573*2788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1574*2788fa27SJeremy L Thompson }
1575*2788fa27SJeremy L Thompson 
1576*2788fa27SJeremy L Thompson /**
15773bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1578d7b241e6Sjeremylt 
1579ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1580ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1581d7b241e6Sjeremylt 
1582ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1583f04ea552SJeremy L Thompson 
1584ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1585ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1586ea61e9acSJeremy 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 active
1587ea61e9acSJeremy L Thompson outputs
1588ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1589b11c1e72Sjeremylt 
1590b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1591dfdf5a53Sjeremylt 
15927a982d89SJeremy L. Thompson   @ref User
1593b11c1e72Sjeremylt **/
15942b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1596d7b241e6Sjeremylt 
1597d1d35e2fSjeremylt   if (op->num_elem) {
1598250756a7Sjeremylt     // Standard Operator
1599cae8b89aSjeremylt     if (op->Apply) {
16002b730f8bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
1601cae8b89aSjeremylt     } else {
1602cae8b89aSjeremylt       // Zero all output vectors
1603250756a7Sjeremylt       CeedQFunction qf = op->qf;
1604d1d35e2fSjeremylt       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1605d1d35e2fSjeremylt         CeedVector vec = op->output_fields[i]->vec;
16062b730f8bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
1607cae8b89aSjeremylt         if (vec != CEED_VECTOR_NONE) {
16082b730f8bSJeremy L Thompson           CeedCall(CeedVectorSetValue(vec, 0.0));
1609cae8b89aSjeremylt         }
1610cae8b89aSjeremylt       }
1611250756a7Sjeremylt       // Apply
16122b730f8bSJeremy L Thompson       CeedCall(op->ApplyAdd(op, in, out, request));
1613250756a7Sjeremylt     }
1614f04ea552SJeremy L Thompson   } else if (op->is_composite) {
1615250756a7Sjeremylt     // Composite Operator
1616250756a7Sjeremylt     if (op->ApplyComposite) {
16172b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1618250756a7Sjeremylt     } else {
1619d1d35e2fSjeremylt       CeedInt num_suboperators;
1620c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1621d1d35e2fSjeremylt       CeedOperator *sub_operators;
1622c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1623250756a7Sjeremylt 
1624250756a7Sjeremylt       // Zero all output vectors
1625250756a7Sjeremylt       if (out != CEED_VECTOR_NONE) {
16262b730f8bSJeremy L Thompson         CeedCall(CeedVectorSetValue(out, 0.0));
1627cae8b89aSjeremylt       }
1628d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1629d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1630d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1631250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
16322b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1633250756a7Sjeremylt           }
1634250756a7Sjeremylt         }
1635250756a7Sjeremylt       }
1636250756a7Sjeremylt       // Apply
1637d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
16382b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1639cae8b89aSjeremylt       }
1640cae8b89aSjeremylt     }
1641250756a7Sjeremylt   }
1642e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1643cae8b89aSjeremylt }
1644cae8b89aSjeremylt 
1645cae8b89aSjeremylt /**
1646cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1647cae8b89aSjeremylt 
1648ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1649ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1650cae8b89aSjeremylt 
1651ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1652ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1653ea61e9acSJeremy 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
1654ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1655cae8b89aSjeremylt 
1656cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1657cae8b89aSjeremylt 
16587a982d89SJeremy L. Thompson   @ref User
1659cae8b89aSjeremylt **/
16602b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
16612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1662cae8b89aSjeremylt 
1663d1d35e2fSjeremylt   if (op->num_elem) {
1664250756a7Sjeremylt     // Standard Operator
16652b730f8bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1666f04ea552SJeremy L Thompson   } else if (op->is_composite) {
1667250756a7Sjeremylt     // Composite Operator
1668250756a7Sjeremylt     if (op->ApplyAddComposite) {
16692b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1670cae8b89aSjeremylt     } else {
1671d1d35e2fSjeremylt       CeedInt num_suboperators;
1672c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1673d1d35e2fSjeremylt       CeedOperator *sub_operators;
1674c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1675250756a7Sjeremylt 
1676d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
16772b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
16781d7d2407SJeremy L Thompson       }
1679250756a7Sjeremylt     }
1680250756a7Sjeremylt   }
1681e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1682d7b241e6Sjeremylt }
1683d7b241e6Sjeremylt 
1684d7b241e6Sjeremylt /**
1685b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1686d7b241e6Sjeremylt 
1687ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1688b11c1e72Sjeremylt 
1689b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1690dfdf5a53Sjeremylt 
16917a982d89SJeremy L. Thompson   @ref User
1692b11c1e72Sjeremylt **/
1693d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1694d1d35e2fSjeremylt   if (!*op || --(*op)->ref_count > 0) return CEED_ERROR_SUCCESS;
16952b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
16962b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1697fe2413ffSjeremylt   // Free fields
16982b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1699d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1700d1d35e2fSjeremylt       if ((*op)->input_fields[i]->elem_restr != CEED_ELEMRESTRICTION_NONE) {
17012b730f8bSJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_restr));
170215910d16Sjeremylt       }
1703d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
17042b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
170571352a93Sjeremylt       }
17062b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
17072b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
170871352a93Sjeremylt       }
17092b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
17102b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1711fe2413ffSjeremylt     }
17122b730f8bSJeremy L Thompson   }
17132b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1714d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
17152b730f8bSJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_restr));
1716d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
17172b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
171871352a93Sjeremylt       }
17192b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
17202b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
172171352a93Sjeremylt       }
17222b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
17232b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
17242b730f8bSJeremy L Thompson     }
1725fe2413ffSjeremylt   }
1726d1d35e2fSjeremylt   // Destroy sub_operators
17272b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1728d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
17292b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
173052d6035fSJeremy L Thompson     }
17312b730f8bSJeremy L Thompson   }
17322b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
17332b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
17342b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
17353668ca4bSJeremy L Thompson   // Destroy any composite labels
17363668ca4bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
17372b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
17382b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*op)->context_labels[i]));
17393668ca4bSJeremy L Thompson   }
17402b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1741fe2413ffSjeremylt 
17425107b09fSJeremy L Thompson   // Destroy fallback
17432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
17445107b09fSJeremy L Thompson 
1745ed9e99e6SJeremy L Thompson   // Destroy assembly data
17462b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
17472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
174870a7ffb3SJeremy L Thompson 
17492b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
17502b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
17512b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
17522b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
17532b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1754e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1755d7b241e6Sjeremylt }
1756d7b241e6Sjeremylt 
1757d7b241e6Sjeremylt /// @}
1758