xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 9bc663991d6482bcb1d60b1f116148f11db83fa1)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` Field matches the `CeedQFunction` Field
26e15f9bd0SJeremy L Thompson 
27ca94c3ddSJeremy L Thompson   @param[in] ceed     `Ceed` object for error handling
28ca94c3ddSJeremy L Thompson   @param[in] qf_field `CeedQFunction` Field matching `CeedOperator` Field
29bafebce1SSebastian Grimberg   @param[in] rstr     `CeedOperator` Field `CeedElemRestriction`
30bafebce1SSebastian Grimberg   @param[in] basis    `CeedOperator` Field `CeedBasis`
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 **/
36bafebce1SSebastian Grimberg static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction rstr, CeedBasis basis) {
376f8994e9SJeremy L Thompson   const char  *field_name;
381203703bSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size;
391203703bSJeremy L Thompson   CeedEvalMode eval_mode;
401203703bSJeremy L Thompson 
411203703bSJeremy L Thompson   // Field data
42ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(qf_field, &field_name, &size, &eval_mode));
432b730f8bSJeremy L Thompson 
44e15f9bd0SJeremy L Thompson   // Restriction
45bafebce1SSebastian Grimberg   CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
466574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
47bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE) {
48bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp));
49e1e9e29dSJed Brown   }
50e15f9bd0SJeremy L Thompson   // Basis
51bafebce1SSebastian Grimberg   CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
52356036faSJeremy L Thompson             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
53bafebce1SSebastian Grimberg   if (basis != CEED_BASIS_NONE) {
54bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetDimension(basis, &dim));
55bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
56bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
57bafebce1SSebastian Grimberg     CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
58ca94c3ddSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT
59ca94c3ddSJeremy L Thompson               " components, but CeedBasis has %" CeedInt_FMT " components",
601203703bSJeremy L Thompson               field_name, size, CeedEvalModes[eval_mode], rstr_num_comp, num_comp);
61e15f9bd0SJeremy L Thompson   }
62e15f9bd0SJeremy L Thompson   // Field size
63d1d35e2fSjeremylt   switch (eval_mode) {
64e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
65edb2538eSJeremy L Thompson       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
661203703bSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", field_name, size,
671203703bSJeremy L Thompson                 CeedEvalModes[eval_mode], rstr_num_comp);
68e15f9bd0SJeremy L Thompson       break;
69e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
70c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
71c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
72c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
736574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
741203703bSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components", field_name, size,
751203703bSJeremy L Thompson                 CeedEvalModes[eval_mode], num_comp * q_comp);
76e15f9bd0SJeremy L Thompson       break;
77e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
78d1d35e2fSjeremylt       // No additional checks required
79e15f9bd0SJeremy L Thompson       break;
80e15f9bd0SJeremy L Thompson   }
81e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
827a982d89SJeremy L. Thompson }
837a982d89SJeremy L. Thompson 
847a982d89SJeremy L. Thompson /**
85ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedOperator`
867a982d89SJeremy L. Thompson 
871203703bSJeremy L Thompson   @param[in] op_field     `CeedOperator` Field to view
88ca94c3ddSJeremy L Thompson   @param[in] qf_field     `CeedQFunction` Field (carries field name)
89d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
904c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
91d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
92ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
937a982d89SJeremy L. Thompson 
947a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
957a982d89SJeremy L. Thompson 
967a982d89SJeremy L. Thompson   @ref Utility
977a982d89SJeremy L. Thompson **/
981203703bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
997a982d89SJeremy L. Thompson   const char  *pre    = sub ? "  " : "";
100d1d35e2fSjeremylt   const char  *in_out = input ? "Input" : "Output";
1016f8994e9SJeremy L Thompson   const char  *field_name;
1021203703bSJeremy L Thompson   CeedInt      size;
1031203703bSJeremy L Thompson   CeedEvalMode eval_mode;
1041203703bSJeremy L Thompson   CeedVector   vec;
1051203703bSJeremy L Thompson   CeedBasis    basis;
1061203703bSJeremy L Thompson 
1071203703bSJeremy L Thompson   // Field data
108ab747706SJeremy L Thompson   CeedCall(CeedQFunctionFieldGetData(qf_field, &field_name, &size, &eval_mode));
109ab747706SJeremy L Thompson   CeedCall(CeedOperatorFieldGetData(op_field, NULL, NULL, &basis, &vec));
1107a982d89SJeremy L. Thompson 
1112b730f8bSJeremy L Thompson   fprintf(stream,
1122b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1132b730f8bSJeremy L Thompson           ":\n"
1147a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
1151203703bSJeremy L Thompson           pre, in_out, field_number, pre, field_name);
1161203703bSJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, size);
1171203703bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[eval_mode]);
1181203703bSJeremy L Thompson   if (basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
1191203703bSJeremy L Thompson   if (vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1201203703bSJeremy L Thompson   else if (vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
121681d0ea7SJeremy L Thompson 
122681d0ea7SJeremy L Thompson   CeedCall(CeedVectorDestroy(&vec));
123681d0ea7SJeremy L Thompson   CeedCall(CeedBasisDestroy(&basis));
124e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1257a982d89SJeremy L. Thompson }
1267a982d89SJeremy L. Thompson 
1277a982d89SJeremy L. Thompson /**
128ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1297a982d89SJeremy L. Thompson 
130ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1317a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
132ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1337a982d89SJeremy L. Thompson 
1347a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1357a982d89SJeremy L. Thompson 
1367a982d89SJeremy L. Thompson   @ref Utility
1377a982d89SJeremy L. Thompson **/
1387a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1397a982d89SJeremy L. Thompson   const char         *pre = sub ? "  " : "";
1401203703bSJeremy L Thompson   CeedInt             num_elem, num_qpts, total_fields = 0, num_input_fields, num_output_fields;
1411203703bSJeremy L Thompson   CeedQFunction       qf;
1421203703bSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1431203703bSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1447a982d89SJeremy L. Thompson 
1452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1481203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1491203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1501203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1511c66c397SJeremy L Thompson 
1522b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1532b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1541203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, num_input_fields, num_input_fields > 1 ? "s" : "");
1551203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1561203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_input_fields[i], qf_input_fields[i], i, sub, 1, stream));
1577a982d89SJeremy L. Thompson   }
1581203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, num_output_fields, num_output_fields > 1 ? "s" : "");
1591203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1601203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_output_fields[i], qf_output_fields[i], i, sub, 0, stream));
1617a982d89SJeremy L. Thompson   }
162e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1637a982d89SJeremy L. Thompson }
1647a982d89SJeremy L. Thompson 
165d99fa3c5SJeremy L Thompson /**
166681d0ea7SJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`.
167681d0ea7SJeremy L Thompson 
168681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_basis` with @ref CeedBasisDestroy().
169eaf62fffSJeremy L Thompson 
170ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
171ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
172eaf62fffSJeremy L Thompson 
173eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
174eaf62fffSJeremy L Thompson 
175eaf62fffSJeremy L Thompson   @ref Developer
176eaf62fffSJeremy L Thompson **/
177eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
178506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
179506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
180506b1a0cSSebastian Grimberg }
181506b1a0cSSebastian Grimberg 
182506b1a0cSSebastian Grimberg /**
183681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`.
184681d0ea7SJeremy L Thompson 
185681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the bases with @ref CeedBasisDestroy().
186506b1a0cSSebastian Grimberg 
187ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
188ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
189ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
190506b1a0cSSebastian Grimberg 
191506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
192506b1a0cSSebastian Grimberg 
193506b1a0cSSebastian Grimberg   @ref Developer
194506b1a0cSSebastian Grimberg **/
195506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1961203703bSJeremy L Thompson   bool               is_composite;
1971203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
1981203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
1991c66c397SJeremy L Thompson 
2001203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2011203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2021203703bSJeremy L Thompson 
203506b1a0cSSebastian Grimberg   if (active_input_basis) {
204506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
2051203703bSJeremy L Thompson     if (!is_composite) {
2061203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2071203703bSJeremy L Thompson         CeedVector vec;
2081203703bSJeremy L Thompson 
2091203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2101203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2111203703bSJeremy L Thompson           CeedBasis basis;
2121203703bSJeremy L Thompson 
2131203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
214*9bc66399SJeremy L Thompson           CeedCheck(!*active_input_basis || *active_input_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
215*9bc66399SJeremy L Thompson                     "Multiple active input CeedBases found");
216681d0ea7SJeremy L Thompson           if (!*active_input_basis) CeedCall(CeedBasisReferenceCopy(basis, active_input_basis));
217681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
218eaf62fffSJeremy L Thompson         }
219681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
2202b730f8bSJeremy L Thompson       }
221*9bc66399SJeremy L Thompson       CeedCheck(*active_input_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
222506b1a0cSSebastian Grimberg     }
223506b1a0cSSebastian Grimberg   }
224506b1a0cSSebastian Grimberg   if (active_output_basis) {
225506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
2261203703bSJeremy L Thompson     if (!is_composite) {
2271203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2281203703bSJeremy L Thompson         CeedVector vec;
2291203703bSJeremy L Thompson 
2301203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2311203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2321203703bSJeremy L Thompson           CeedBasis basis;
2331203703bSJeremy L Thompson 
2341203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
235*9bc66399SJeremy L Thompson           CeedCheck(!*active_output_basis || *active_output_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
236*9bc66399SJeremy L Thompson                     "Multiple active output CeedBases found");
237681d0ea7SJeremy L Thompson           if (!*active_output_basis) CeedCall(CeedBasisReferenceCopy(basis, active_output_basis));
238681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
239506b1a0cSSebastian Grimberg         }
240681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
241506b1a0cSSebastian Grimberg       }
242*9bc66399SJeremy L Thompson       CeedCheck(*active_output_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
243506b1a0cSSebastian Grimberg     }
244506b1a0cSSebastian Grimberg   }
245eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
246eaf62fffSJeremy L Thompson }
247eaf62fffSJeremy L Thompson 
248eaf62fffSJeremy L Thompson /**
249681d0ea7SJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`.
250681d0ea7SJeremy L Thompson 
251681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_rstr` with @ref CeedElemRestrictionDestroy().
252e2f04181SAndrew T. Barker 
253ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
254ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
255e2f04181SAndrew T. Barker 
256e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
257e2f04181SAndrew T. Barker 
258e2f04181SAndrew T. Barker   @ref Utility
259e2f04181SAndrew T. Barker **/
2602b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
261506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
262506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
263506b1a0cSSebastian Grimberg }
264506b1a0cSSebastian Grimberg 
265506b1a0cSSebastian Grimberg /**
266681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`.
267681d0ea7SJeremy L Thompson 
268681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the restrictions with @ref CeedElemRestrictionDestroy().
269506b1a0cSSebastian Grimberg 
270ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
271ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
272ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
273506b1a0cSSebastian Grimberg 
274506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
275506b1a0cSSebastian Grimberg 
276506b1a0cSSebastian Grimberg   @ref Utility
277506b1a0cSSebastian Grimberg **/
278506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2791203703bSJeremy L Thompson   bool               is_composite;
2801203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2811203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2821c66c397SJeremy L Thompson 
2831203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2841203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2851203703bSJeremy L Thompson 
286506b1a0cSSebastian Grimberg   if (active_input_rstr) {
287506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
2881203703bSJeremy L Thompson     if (!is_composite) {
2891203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2901203703bSJeremy L Thompson         CeedVector vec;
2911203703bSJeremy L Thompson 
2921203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2931203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2941203703bSJeremy L Thompson           CeedElemRestriction rstr;
2951203703bSJeremy L Thompson 
2961203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
297*9bc66399SJeremy L Thompson           CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
298*9bc66399SJeremy L Thompson                     "Multiple active input CeedElemRestrictions found");
299681d0ea7SJeremy L Thompson           if (!*active_input_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_input_rstr));
300681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
301e2f04181SAndrew T. Barker         }
302681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
3032b730f8bSJeremy L Thompson       }
304*9bc66399SJeremy L Thompson       CeedCheck(*active_input_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
305506b1a0cSSebastian Grimberg     }
306506b1a0cSSebastian Grimberg   }
307506b1a0cSSebastian Grimberg   if (active_output_rstr) {
308506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
3091203703bSJeremy L Thompson     if (!is_composite) {
3101203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
3111203703bSJeremy L Thompson         CeedVector vec;
3121203703bSJeremy L Thompson 
3131203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
3141203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
3151203703bSJeremy L Thompson           CeedElemRestriction rstr;
3161203703bSJeremy L Thompson 
3171203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
318*9bc66399SJeremy L Thompson           CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
319*9bc66399SJeremy L Thompson                     "Multiple active output CeedElemRestrictions found");
320681d0ea7SJeremy L Thompson           if (!*active_output_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_output_rstr));
321681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
322506b1a0cSSebastian Grimberg         }
323681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
324506b1a0cSSebastian Grimberg       }
325*9bc66399SJeremy L Thompson       CeedCheck(*active_output_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
326506b1a0cSSebastian Grimberg     }
327506b1a0cSSebastian Grimberg   }
328e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
329e2f04181SAndrew T. Barker }
330e2f04181SAndrew T. Barker 
331d8dd9a91SJeremy L Thompson /**
332ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
3334385fb7fSSebastian Grimberg 
334ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
335ca94c3ddSJeremy 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 not have any field of a matching type.
336d8dd9a91SJeremy L Thompson 
337ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
338ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
339ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
3402788fa27SJeremy L Thompson   @param[in]     values      Values to set
341d8dd9a91SJeremy L Thompson 
342d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
343d8dd9a91SJeremy L Thompson 
344d8dd9a91SJeremy L Thompson   @ref User
345d8dd9a91SJeremy L Thompson **/
3462788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3471c66c397SJeremy L Thompson   bool is_composite = false;
3481c66c397SJeremy L Thompson 
349*9bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
3503668ca4bSJeremy L Thompson 
3515ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3525ac9af79SJeremy L Thompson   if (field_label->from_op) {
3535ac9af79SJeremy L Thompson     CeedInt index = -1;
3545ac9af79SJeremy L Thompson 
3555ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3565ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3575ac9af79SJeremy L Thompson     }
358*9bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3595ac9af79SJeremy L Thompson   }
3605ac9af79SJeremy L Thompson 
3612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
362d8dd9a91SJeremy L Thompson   if (is_composite) {
363d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
364d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
365d8dd9a91SJeremy L Thompson 
366c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
367c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
368*9bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
369*9bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
370d8dd9a91SJeremy L Thompson 
371d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3721203703bSJeremy L Thompson       CeedQFunction        qf;
3731203703bSJeremy L Thompson       CeedQFunctionContext ctx;
3741203703bSJeremy L Thompson 
3751203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
3761203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
377d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3781203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
3791203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values));
380d8dd9a91SJeremy L Thompson       }
381d8dd9a91SJeremy L Thompson     }
382d8dd9a91SJeremy L Thompson   } else {
3831203703bSJeremy L Thompson     CeedQFunction        qf;
3841203703bSJeremy L Thompson     CeedQFunctionContext ctx;
3851203703bSJeremy L Thompson 
3861203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
3871203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
388*9bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3891203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values));
3902788fa27SJeremy L Thompson   }
3916d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3922788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3932788fa27SJeremy L Thompson }
3942788fa27SJeremy L Thompson 
3952788fa27SJeremy L Thompson /**
396ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3974385fb7fSSebastian Grimberg 
398ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
399ca94c3ddSJeremy 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 not have any field of a matching type.
4002788fa27SJeremy L Thompson 
401ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4022788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4032788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
404c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
405c5d0f995SJed Brown   @param[out]    values      Values in the label
4062788fa27SJeremy L Thompson 
4072788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4082788fa27SJeremy L Thompson 
4092788fa27SJeremy L Thompson   @ref User
4102788fa27SJeremy L Thompson **/
4112788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
4122788fa27SJeremy L Thompson                                              void *values) {
4131c66c397SJeremy L Thompson   bool is_composite = false;
4141c66c397SJeremy L Thompson 
415*9bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4162788fa27SJeremy L Thompson 
4172788fa27SJeremy L Thompson   *(void **)values = NULL;
4182788fa27SJeremy L Thompson   *num_values      = 0;
4192788fa27SJeremy L Thompson 
4205ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4215ac9af79SJeremy L Thompson   if (field_label->from_op) {
4225ac9af79SJeremy L Thompson     CeedInt index = -1;
4235ac9af79SJeremy L Thompson 
4245ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4255ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4265ac9af79SJeremy L Thompson     }
427*9bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4285ac9af79SJeremy L Thompson   }
4295ac9af79SJeremy L Thompson 
4302788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4312788fa27SJeremy L Thompson   if (is_composite) {
4322788fa27SJeremy L Thompson     CeedInt       num_sub;
4332788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4342788fa27SJeremy L Thompson 
4352788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4362788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
437*9bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
438*9bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4392788fa27SJeremy L Thompson 
4402788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4411203703bSJeremy L Thompson       CeedQFunction        qf;
4421203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4431203703bSJeremy L Thompson 
4441203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
4451203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
4462788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4471203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
4481203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values));
4492788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4502788fa27SJeremy L Thompson       }
4512788fa27SJeremy L Thompson     }
4522788fa27SJeremy L Thompson   } else {
4531203703bSJeremy L Thompson     CeedQFunction        qf;
4541203703bSJeremy L Thompson     CeedQFunctionContext ctx;
4551203703bSJeremy L Thompson 
4561203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
4571203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
458*9bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4591203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values));
4602788fa27SJeremy L Thompson   }
4612788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4622788fa27SJeremy L Thompson }
4632788fa27SJeremy L Thompson 
4642788fa27SJeremy L Thompson /**
465ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
4664385fb7fSSebastian Grimberg 
467ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
468ca94c3ddSJeremy 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 not have any field of a matching type.
4692788fa27SJeremy L Thompson 
470ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4712788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4722788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
473c5d0f995SJed Brown   @param[in]     values      Values array to restore
4742788fa27SJeremy L Thompson 
4752788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4762788fa27SJeremy L Thompson 
4772788fa27SJeremy L Thompson   @ref User
4782788fa27SJeremy L Thompson **/
4792788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4801c66c397SJeremy L Thompson   bool is_composite = false;
4811c66c397SJeremy L Thompson 
482*9bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4832788fa27SJeremy L Thompson 
4845ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4855ac9af79SJeremy L Thompson   if (field_label->from_op) {
4865ac9af79SJeremy L Thompson     CeedInt index = -1;
4875ac9af79SJeremy L Thompson 
4885ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4895ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4905ac9af79SJeremy L Thompson     }
491*9bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4925ac9af79SJeremy L Thompson   }
4935ac9af79SJeremy L Thompson 
4942788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4952788fa27SJeremy L Thompson   if (is_composite) {
4962788fa27SJeremy L Thompson     CeedInt       num_sub;
4972788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4982788fa27SJeremy L Thompson 
4992788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
5002788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
501*9bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
502*9bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
5032788fa27SJeremy L Thompson 
5042788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
5051203703bSJeremy L Thompson       CeedQFunction        qf;
5061203703bSJeremy L Thompson       CeedQFunctionContext ctx;
5071203703bSJeremy L Thompson 
5081203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
5091203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
5102788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
5111203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
5121203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values));
5132788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
5142788fa27SJeremy L Thompson       }
5152788fa27SJeremy L Thompson     }
5162788fa27SJeremy L Thompson   } else {
5171203703bSJeremy L Thompson     CeedQFunction        qf;
5181203703bSJeremy L Thompson     CeedQFunctionContext ctx;
5191203703bSJeremy L Thompson 
5201203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
5211203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
522*9bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
5231203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values));
524d8dd9a91SJeremy L Thompson   }
525d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
526d8dd9a91SJeremy L Thompson }
527d8dd9a91SJeremy L Thompson 
5287a982d89SJeremy L. Thompson /// @}
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5317a982d89SJeremy L. Thompson /// CeedOperator Backend API
5327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5337a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
5347a982d89SJeremy L. Thompson /// @{
5357a982d89SJeremy L. Thompson 
5367a982d89SJeremy L. Thompson /**
537ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
5387a982d89SJeremy L. Thompson 
539ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
540d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
5417a982d89SJeremy L. Thompson 
5427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5437a982d89SJeremy L. Thompson 
5447a982d89SJeremy L. Thompson   @ref Backend
5457a982d89SJeremy L. Thompson **/
546d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
5471203703bSJeremy L Thompson   bool is_composite;
5481203703bSJeremy L Thompson 
5491203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5506e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operators");
551d1d35e2fSjeremylt   *num_args = op->num_fields;
552e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5537a982d89SJeremy L. Thompson }
5547a982d89SJeremy L. Thompson 
5557a982d89SJeremy L. Thompson /**
5569463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
5579463e855SJeremy L Thompson 
5589463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
5599463e855SJeremy L Thompson 
5609463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
5619463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
5629463e855SJeremy L Thompson 
5639463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5649463e855SJeremy L Thompson 
5659463e855SJeremy L Thompson   @ref Backend
5669463e855SJeremy L Thompson **/
5679463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
5689463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
5699463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
5709463e855SJeremy L Thompson 
5719463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
5729463e855SJeremy L Thompson   *has_tensor_bases = true;
5739463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
5749463e855SJeremy L Thompson     bool      is_tensor;
5759463e855SJeremy L Thompson     CeedBasis basis;
5769463e855SJeremy L Thompson 
5779463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
5789463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5799463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5809463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5819463e855SJeremy L Thompson     }
582681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
5839463e855SJeremy L Thompson   }
5849463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
5859463e855SJeremy L Thompson     bool      is_tensor;
5869463e855SJeremy L Thompson     CeedBasis basis;
5879463e855SJeremy L Thompson 
5889463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
5899463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5909463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5919463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5929463e855SJeremy L Thompson     }
593681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
5949463e855SJeremy L Thompson   }
5959463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5969463e855SJeremy L Thompson }
5979463e855SJeremy L Thompson 
5989463e855SJeremy L Thompson /**
5991203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is immutable
6001203703bSJeremy L Thompson 
6011203703bSJeremy L Thompson   @param[in]  op           `CeedOperator`
6021203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
6031203703bSJeremy L Thompson 
6041203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6051203703bSJeremy L Thompson 
6061203703bSJeremy L Thompson   @ref Backend
6071203703bSJeremy L Thompson **/
6081203703bSJeremy L Thompson int CeedOperatorIsImmutable(CeedOperator op, bool *is_immutable) {
6091203703bSJeremy L Thompson   *is_immutable = op->is_immutable;
6101203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6111203703bSJeremy L Thompson }
6121203703bSJeremy L Thompson 
6131203703bSJeremy L Thompson /**
614ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
6157a982d89SJeremy L. Thompson 
616ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
617d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
6187a982d89SJeremy L. Thompson 
6197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6207a982d89SJeremy L. Thompson 
6217a982d89SJeremy L. Thompson   @ref Backend
6227a982d89SJeremy L. Thompson **/
623d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
624f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
625e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6267a982d89SJeremy L. Thompson }
6277a982d89SJeremy L. Thompson 
6287a982d89SJeremy L. Thompson /**
629ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
6307a982d89SJeremy L. Thompson 
631ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
632ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
6337a982d89SJeremy L. Thompson 
6347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6357a982d89SJeremy L. Thompson 
6367a982d89SJeremy L. Thompson   @ref Backend
6377a982d89SJeremy L. Thompson **/
6387a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
6391203703bSJeremy L Thompson   bool is_composite;
6401203703bSJeremy L Thompson 
6411203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6426e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
6437a982d89SJeremy L. Thompson   *qf = op->qf;
644e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6457a982d89SJeremy L. Thompson }
6467a982d89SJeremy L. Thompson 
6477a982d89SJeremy L. Thompson /**
648ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
649c04a41a7SJeremy L Thompson 
650ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
651d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
652c04a41a7SJeremy L Thompson 
653c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
654c04a41a7SJeremy L Thompson 
655c04a41a7SJeremy L Thompson   @ref Backend
656c04a41a7SJeremy L Thompson **/
657d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
658f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
659e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
660c04a41a7SJeremy L Thompson }
661c04a41a7SJeremy L Thompson 
662c04a41a7SJeremy L Thompson /**
663ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
6647a982d89SJeremy L. Thompson 
665ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
6667a982d89SJeremy L. Thompson   @param[out] data Variable to store data
6677a982d89SJeremy L. Thompson 
6687a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6697a982d89SJeremy L. Thompson 
6707a982d89SJeremy L. Thompson   @ref Backend
6717a982d89SJeremy L. Thompson **/
672777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
673777ff853SJeremy L Thompson   *(void **)data = op->data;
674e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6757a982d89SJeremy L. Thompson }
6767a982d89SJeremy L. Thompson 
6777a982d89SJeremy L. Thompson /**
678ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
6797a982d89SJeremy L. Thompson 
680ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
681ea61e9acSJeremy L Thompson   @param[in]     data Data to set
6827a982d89SJeremy L. Thompson 
6837a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6847a982d89SJeremy L. Thompson 
6857a982d89SJeremy L. Thompson   @ref Backend
6867a982d89SJeremy L. Thompson **/
687777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
688777ff853SJeremy L Thompson   op->data = data;
689e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6907a982d89SJeremy L. Thompson }
6917a982d89SJeremy L. Thompson 
6927a982d89SJeremy L. Thompson /**
693ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
69434359f16Sjeremylt 
695ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
69634359f16Sjeremylt 
69734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
69834359f16Sjeremylt 
69934359f16Sjeremylt   @ref Backend
70034359f16Sjeremylt **/
7019560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
70234359f16Sjeremylt   op->ref_count++;
70334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
70434359f16Sjeremylt }
70534359f16Sjeremylt 
70634359f16Sjeremylt /**
707ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
7087a982d89SJeremy L. Thompson 
709ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
7107a982d89SJeremy L. Thompson 
7117a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
7127a982d89SJeremy L. Thompson 
7137a982d89SJeremy L. Thompson   @ref Backend
7147a982d89SJeremy L. Thompson **/
7157a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
716f04ea552SJeremy L Thompson   op->is_backend_setup = true;
717e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7187a982d89SJeremy L. Thompson }
7197a982d89SJeremy L. Thompson 
7207a982d89SJeremy L. Thompson /// @}
7217a982d89SJeremy L. Thompson 
7227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7237a982d89SJeremy L. Thompson /// CeedOperator Public API
7247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7257a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
726dfdf5a53Sjeremylt /// @{
727d7b241e6Sjeremylt 
728d7b241e6Sjeremylt /**
729ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
7304385fb7fSSebastian Grimberg 
731ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
732d7b241e6Sjeremylt 
733ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
734ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
735ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
736ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
737ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
738b11c1e72Sjeremylt 
739b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
740dfdf5a53Sjeremylt 
7417a982d89SJeremy L. Thompson   @ref User
742d7b241e6Sjeremylt  */
7432b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
7445fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
7455fe0d4faSjeremylt     Ceed delegate;
7466574a04fSJeremy L Thompson 
7472b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7481ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate");
7492b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
750*9bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
751e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7525fe0d4faSjeremylt   }
7535fe0d4faSjeremylt 
754ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
755db002c03SJeremy L Thompson 
7562b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
757db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
758d1d35e2fSjeremylt   (*op)->ref_count   = 1;
7592b104005SJeremy L Thompson   (*op)->input_size  = -1;
7602b104005SJeremy L Thompson   (*op)->output_size = -1;
761db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
762db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
763db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
7642b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
7652b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
7662b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
767e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
768d7b241e6Sjeremylt }
769d7b241e6Sjeremylt 
770d7b241e6Sjeremylt /**
771ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
77248acf710SJeremy L Thompson 
773ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
774ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
77548acf710SJeremy L Thompson 
776ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
777ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
778ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
779ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
78048acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
78148acf710SJeremy L Thompson 
78248acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78348acf710SJeremy L Thompson 
78448acf710SJeremy L Thompson   @ref User
78548acf710SJeremy L Thompson  */
78648acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
78748acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
78848acf710SJeremy L Thompson     Ceed delegate;
78948acf710SJeremy L Thompson 
79048acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7911ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints");
79248acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
793*9bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
79448acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
79548acf710SJeremy L Thompson   }
79648acf710SJeremy L Thompson 
797ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
79848acf710SJeremy L Thompson 
79948acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
80048acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
80148acf710SJeremy L Thompson   (*op)->ref_count    = 1;
80248acf710SJeremy L Thompson   (*op)->is_at_points = true;
80348acf710SJeremy L Thompson   (*op)->input_size   = -1;
80448acf710SJeremy L Thompson   (*op)->output_size  = -1;
80548acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
80648acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
80748acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
80848acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
80948acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
81048acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
81148acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81248acf710SJeremy L Thompson }
81348acf710SJeremy L Thompson 
81448acf710SJeremy L Thompson /**
815ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
81652d6035fSJeremy L Thompson 
817ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
818ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
81952d6035fSJeremy L Thompson 
82052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
82152d6035fSJeremy L Thompson 
8227a982d89SJeremy L. Thompson   @ref User
82352d6035fSJeremy L Thompson  */
82452d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
82552d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
82652d6035fSJeremy L Thompson     Ceed delegate;
82752d6035fSJeremy L Thompson 
8281c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
829250756a7Sjeremylt     if (delegate) {
8302b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
831*9bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&delegate));
832e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
83352d6035fSJeremy L Thompson     }
834250756a7Sjeremylt   }
83552d6035fSJeremy L Thompson 
8362b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
837db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
838996d9ab5SJed Brown   (*op)->ref_count    = 1;
839f04ea552SJeremy L Thompson   (*op)->is_composite = true;
8402b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
8412b104005SJeremy L Thompson   (*op)->input_size  = -1;
8422b104005SJeremy L Thompson   (*op)->output_size = -1;
843250756a7Sjeremylt 
844db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
845e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84652d6035fSJeremy L Thompson }
84752d6035fSJeremy L Thompson 
84852d6035fSJeremy L Thompson /**
849ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
8504385fb7fSSebastian Grimberg 
851ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
852512bb800SJeremy L Thompson 
853ca94c3ddSJeremy L Thompson   Note: If the value of `*op_copy` passed to this function is non-`NULL`, then it is assumed that `*op_copy` is a pointer to a `CeedOperator`.
854ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
8559560d06aSjeremylt 
856ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
857ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
8589560d06aSjeremylt 
8599560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8609560d06aSjeremylt 
8619560d06aSjeremylt   @ref User
8629560d06aSjeremylt **/
8639560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
8642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
8652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
8669560d06aSjeremylt   *op_copy = op;
8679560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8689560d06aSjeremylt }
8699560d06aSjeremylt 
8709560d06aSjeremylt /**
871ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
872d7b241e6Sjeremylt 
873ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
874bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
875ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
876d7b241e6Sjeremylt 
877ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
878ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
879d7b241e6Sjeremylt 
880528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
881bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
882528a22edSJeremy L Thompson 
883ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
884ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
885bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
886bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
887bafebce1SSebastian Grimberg   @param[in]     vec        `CeedVector` to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE if using @ref CEED_EVAL_WEIGHT in the `CeedQFunction`
888b11c1e72Sjeremylt 
889b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
890dfdf5a53Sjeremylt 
8917a982d89SJeremy L. Thompson   @ref User
892b11c1e72Sjeremylt **/
893bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
8941203703bSJeremy L Thompson   bool               is_input = true, is_at_points, is_composite, is_immutable;
8951203703bSJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields;
8961203703bSJeremy L Thompson   CeedQFunction      qf;
8971203703bSJeremy L Thompson   CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields;
8981c66c397SJeremy L Thompson   CeedOperatorField *op_field;
8991c66c397SJeremy L Thompson 
9001203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
9011203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9021203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
903*9bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
904*9bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
905*9bc66399SJeremy L Thompson   CeedCheck(rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
906*9bc66399SJeremy L Thompson   CeedCheck(basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
907*9bc66399SJeremy L Thompson   CeedCheck(vec, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
90852d6035fSJeremy L Thompson 
909bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
910*9bc66399SJeremy L Thompson   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
911ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
9122c7e7413SJeremy L Thompson   {
9132c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
9142c7e7413SJeremy L Thompson 
915bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
91648acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
917*9bc66399SJeremy L Thompson       CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
918*9bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints not supported for standard operator fields");
919*9bc66399SJeremy L Thompson       CeedCheck(basis == CEED_BASIS_NONE, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
920*9bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
92148acf710SJeremy L Thompson       if (!op->first_points_rstr) {
922bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
92348acf710SJeremy L Thompson       } else {
92448acf710SJeremy L Thompson         bool are_compatible;
92548acf710SJeremy L Thompson 
926bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
927*9bc66399SJeremy L Thompson         CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
928ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
92948acf710SJeremy L Thompson       }
93048acf710SJeremy L Thompson     }
9312c7e7413SJeremy L Thompson   }
932d7b241e6Sjeremylt 
933bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
934bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
935*9bc66399SJeremy L Thompson   CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
936ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
937528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
938bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
9391203703bSJeremy L Thompson 
9401203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
9411203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
9421203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9436f8994e9SJeremy L Thompson     const char *qf_field_name;
9441203703bSJeremy L Thompson 
9451203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_input_fields[i], &qf_field_name));
9461203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9471203703bSJeremy L Thompson       qf_field = qf_input_fields[i];
948d1d35e2fSjeremylt       op_field = &op->input_fields[i];
949d7b241e6Sjeremylt       goto found;
950d7b241e6Sjeremylt     }
951d7b241e6Sjeremylt   }
9522b104005SJeremy L Thompson   is_input = false;
9531203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9546f8994e9SJeremy L Thompson     const char *qf_field_name;
9551203703bSJeremy L Thompson 
9561203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_output_fields[i], &qf_field_name));
9571203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9581203703bSJeremy L Thompson       qf_field = qf_output_fields[i];
959d1d35e2fSjeremylt       op_field = &op->output_fields[i];
960d7b241e6Sjeremylt       goto found;
961d7b241e6Sjeremylt     }
962d7b241e6Sjeremylt   }
963c042f62fSJeremy L Thompson   // LCOV_EXCL_START
964*9bc66399SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
965c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
966d7b241e6Sjeremylt found:
967*9bc66399SJeremy L Thompson   CeedCall(CeedOperatorCheckField(CeedOperatorReturnCeed(op), qf_field, rstr, basis));
9682b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
969e15f9bd0SJeremy L Thompson 
970bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
9712b104005SJeremy L Thompson     CeedSize l_size;
9721c66c397SJeremy L Thompson 
973bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
9742b104005SJeremy L Thompson     if (is_input) {
9752b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
976*9bc66399SJeremy L Thompson       CeedCheck(l_size == op->input_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
977249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
9782b104005SJeremy L Thompson     } else {
9792b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
980*9bc66399SJeremy L Thompson       CeedCheck(l_size == op->output_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
981249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
9822b104005SJeremy L Thompson     }
9832b730f8bSJeremy L Thompson   }
9842b104005SJeremy L Thompson 
985bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
986bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
987bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
988d1d35e2fSjeremylt     op->num_elem        = num_elem;
989d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
990e15f9bd0SJeremy L Thompson   }
991bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
9922a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
993d1d35e2fSjeremylt   op->num_fields += 1;
9942b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
995e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
996d7b241e6Sjeremylt }
997d7b241e6Sjeremylt 
998d7b241e6Sjeremylt /**
999ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
100043bbe138SJeremy L Thompson 
1001ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1002f04ea552SJeremy L Thompson 
1003ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
1004f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
1005ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
1006f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
1007ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
100843bbe138SJeremy L Thompson 
100943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
101043bbe138SJeremy L Thompson 
1011e9b533fbSJeremy L Thompson   @ref Advanced
101243bbe138SJeremy L Thompson **/
10132b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
101443bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
10151203703bSJeremy L Thompson   bool          is_composite;
10161203703bSJeremy L Thompson   CeedQFunction qf;
10171203703bSJeremy L Thompson 
10181203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10196e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
10202b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
102143bbe138SJeremy L Thompson 
10221203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10231203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL));
102443bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
102543bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
102643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
102743bbe138SJeremy L Thompson }
102843bbe138SJeremy L Thompson 
102943bbe138SJeremy L Thompson /**
1030ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
103148acf710SJeremy L Thompson 
1032ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
103348acf710SJeremy L Thompson 
1034ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
1035ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
1036ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
103748acf710SJeremy L Thompson 
103848acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
103948acf710SJeremy L Thompson 
104048acf710SJeremy L Thompson   @ref Advanced
104148acf710SJeremy L Thompson **/
104248acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
10431203703bSJeremy L Thompson   bool is_at_points, is_immutable;
10442a3ff1c9SZach Atkins 
10452a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10461203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
1047*9bc66399SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
1048*9bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
104948acf710SJeremy L Thompson 
105048acf710SJeremy L Thompson   if (!op->first_points_rstr) {
105148acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
105248acf710SJeremy L Thompson   } else {
105348acf710SJeremy L Thompson     bool are_compatible;
105448acf710SJeremy L Thompson 
105548acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
1056*9bc66399SJeremy L Thompson     CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
1057ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
105848acf710SJeremy L Thompson   }
105948acf710SJeremy L Thompson 
106048acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
106148acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
106248acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106348acf710SJeremy L Thompson }
106448acf710SJeremy L Thompson 
106548acf710SJeremy L Thompson /**
1066b594f9faSZach Atkins   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
1067b594f9faSZach Atkins 
1068b594f9faSZach Atkins   @param[in]  op           `CeedOperator`
1069b594f9faSZach Atkins   @param[out] is_at_points Variable to store at points status
1070b594f9faSZach Atkins 
1071b594f9faSZach Atkins   @return An error code: 0 - success, otherwise - failure
1072b594f9faSZach Atkins 
1073b594f9faSZach Atkins   @ref User
1074b594f9faSZach Atkins **/
1075b594f9faSZach Atkins int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
1076b594f9faSZach Atkins   *is_at_points = op->is_at_points;
1077b594f9faSZach Atkins   return CEED_ERROR_SUCCESS;
1078b594f9faSZach Atkins }
1079b594f9faSZach Atkins 
1080b594f9faSZach Atkins /**
1081ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
108248acf710SJeremy L Thompson 
1083ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
108448acf710SJeremy L Thompson 
1085ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
1086ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
1087ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
108848acf710SJeremy L Thompson 
108948acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
109048acf710SJeremy L Thompson 
109148acf710SJeremy L Thompson   @ref Advanced
109248acf710SJeremy L Thompson **/
109348acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
10942a3ff1c9SZach Atkins   bool is_at_points;
10952a3ff1c9SZach Atkins 
10962a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10976e536b99SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
109848acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
109948acf710SJeremy L Thompson 
110048acf710SJeremy L Thompson   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
110148acf710SJeremy L Thompson   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
110248acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
110348acf710SJeremy L Thompson }
110448acf710SJeremy L Thompson 
110548acf710SJeremy L Thompson /**
1106be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
1107be9c6463SJeremy L Thompson 
1108be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
1109de5900adSJames Wright 
1110ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1111de5900adSJames Wright 
1112ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
1113ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
1114ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
1115de5900adSJames Wright 
1116de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
1117de5900adSJames Wright 
1118de5900adSJames Wright   @ref Advanced
1119de5900adSJames Wright **/
1120de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
11216f8994e9SJeremy L Thompson   const char        *name;
1122de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
1123de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
1124de5900adSJames Wright 
1125be9c6463SJeremy L Thompson   *op_field = NULL;
11261c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1127de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1128de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
1129de5900adSJames Wright     if (!strcmp(name, field_name)) {
1130de5900adSJames Wright       *op_field = input_fields[i];
1131de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1132de5900adSJames Wright     }
1133de5900adSJames Wright   }
1134de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1135de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
1136de5900adSJames Wright     if (!strcmp(name, field_name)) {
1137de5900adSJames Wright       *op_field = output_fields[i];
1138de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1139de5900adSJames Wright     }
1140de5900adSJames Wright   }
1141be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1142de5900adSJames Wright }
1143de5900adSJames Wright 
1144de5900adSJames Wright /**
1145ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
114628567f8fSJeremy L Thompson 
1147ca94c3ddSJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
114828567f8fSJeremy L Thompson   @param[out] field_name Variable to store the field name
114928567f8fSJeremy L Thompson 
115028567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
115128567f8fSJeremy L Thompson 
1152e9b533fbSJeremy L Thompson   @ref Advanced
115328567f8fSJeremy L Thompson **/
11546f8994e9SJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, const char **field_name) {
11556f8994e9SJeremy L Thompson   *field_name = op_field->field_name;
115628567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
115728567f8fSJeremy L Thompson }
115828567f8fSJeremy L Thompson 
115928567f8fSJeremy L Thompson /**
1160681d0ea7SJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field.
1161681d0ea7SJeremy L Thompson 
1162681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr` with @ref CeedElemRestrictionDestroy().
116343bbe138SJeremy L Thompson 
1164ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1165ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
116643bbe138SJeremy L Thompson 
116743bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
116843bbe138SJeremy L Thompson 
1169e9b533fbSJeremy L Thompson   @ref Advanced
117043bbe138SJeremy L Thompson **/
11712b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1172681d0ea7SJeremy L Thompson   *rstr = NULL;
1173681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(op_field->elem_rstr, rstr));
117443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
117543bbe138SJeremy L Thompson }
117643bbe138SJeremy L Thompson 
117743bbe138SJeremy L Thompson /**
1178681d0ea7SJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field.
1179681d0ea7SJeremy L Thompson 
1180681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `basis` with @ref CeedBasisDestroy().
118143bbe138SJeremy L Thompson 
1182ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1183ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
118443bbe138SJeremy L Thompson 
118543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
118643bbe138SJeremy L Thompson 
1187e9b533fbSJeremy L Thompson   @ref Advanced
118843bbe138SJeremy L Thompson **/
118943bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
1190681d0ea7SJeremy L Thompson   *basis = NULL;
1191681d0ea7SJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(op_field->basis, basis));
119243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
119343bbe138SJeremy L Thompson }
119443bbe138SJeremy L Thompson 
119543bbe138SJeremy L Thompson /**
1196681d0ea7SJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field.
1197681d0ea7SJeremy L Thompson 
1198681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `vec` with @ref CeedVectorDestroy().
119943bbe138SJeremy L Thompson 
1200ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1201ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
120243bbe138SJeremy L Thompson 
120343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
120443bbe138SJeremy L Thompson 
1205e9b533fbSJeremy L Thompson   @ref Advanced
120643bbe138SJeremy L Thompson **/
120743bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
1208681d0ea7SJeremy L Thompson   *vec = NULL;
1209681d0ea7SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(op_field->vec, vec));
121043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
121143bbe138SJeremy L Thompson }
121243bbe138SJeremy L Thompson 
121343bbe138SJeremy L Thompson /**
1214ab747706SJeremy L Thompson   @brief Get the data of a `CeedOperator` Field.
1215ab747706SJeremy L Thompson 
1216681d0ea7SJeremy L Thompson   Any arguments set as `NULL` are ignored..
1217681d0ea7SJeremy L Thompson 
1218681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr`, `basis`, and `vec`.
1219ab747706SJeremy L Thompson 
1220ab747706SJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
1221ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
1222ab747706SJeremy L Thompson   @param[out] rstr       Variable to store `CeedElemRestriction`
1223ab747706SJeremy L Thompson   @param[out] basis      Variable to store `CeedBasis`
1224ab747706SJeremy L Thompson   @param[out] vec        Variable to store `CeedVector`
1225ab747706SJeremy L Thompson 
1226ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1227ab747706SJeremy L Thompson 
1228ab747706SJeremy L Thompson   @ref Advanced
1229ab747706SJeremy L Thompson **/
12306f8994e9SJeremy L Thompson int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name, CeedElemRestriction *rstr, CeedBasis *basis, CeedVector *vec) {
1231ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedOperatorFieldGetName(op_field, field_name));
1232ab747706SJeremy L Thompson   if (rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_field, rstr));
1233ab747706SJeremy L Thompson   if (basis) CeedCall(CeedOperatorFieldGetBasis(op_field, basis));
1234ab747706SJeremy L Thompson   if (vec) CeedCall(CeedOperatorFieldGetVector(op_field, vec));
1235ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1236ab747706SJeremy L Thompson }
1237ab747706SJeremy L Thompson 
1238ab747706SJeremy L Thompson /**
1239ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1240288c0443SJeremy L Thompson 
1241ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1242ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
124352d6035fSJeremy L Thompson 
124452d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
124552d6035fSJeremy L Thompson 
12467a982d89SJeremy L. Thompson   @ref User
124752d6035fSJeremy L Thompson  */
12482b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
12491203703bSJeremy L Thompson   bool is_immutable;
12501203703bSJeremy L Thompson 
1251*9bc66399SJeremy L Thompson   CeedCheck(composite_op->is_composite, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
1252*9bc66399SJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, CeedOperatorReturnCeed(composite_op), CEED_ERROR_UNSUPPORTED,
1253*9bc66399SJeremy L Thompson             "Cannot add additional sub-operators");
12541203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable));
1255*9bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
12562b730f8bSJeremy L Thompson 
12572b730f8bSJeremy L Thompson   {
12582b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
12591c66c397SJeremy L Thompson 
12602b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
12612b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
12622b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
12632b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
1264*9bc66399SJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
1265*9bc66399SJeremy L Thompson               CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR,
1266249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1267249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1268249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12692b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
12702b730f8bSJeremy L Thompson   }
12712b730f8bSJeremy L Thompson 
1272d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
12732b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1274d1d35e2fSjeremylt   composite_op->num_suboperators++;
1275e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
127652d6035fSJeremy L Thompson }
127752d6035fSJeremy L Thompson 
127852d6035fSJeremy L Thompson /**
1279ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
128075f0d5a4SJeremy L Thompson 
1281ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1282ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
128375f0d5a4SJeremy L Thompson 
128475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
128575f0d5a4SJeremy L Thompson 
128675f0d5a4SJeremy L Thompson   @ref Backend
128775f0d5a4SJeremy L Thompson **/
1288c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
12891203703bSJeremy L Thompson   bool is_composite;
12901203703bSJeremy L Thompson 
12911203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12926e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
129375f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
129475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
129575f0d5a4SJeremy L Thompson }
129675f0d5a4SJeremy L Thompson 
129775f0d5a4SJeremy L Thompson /**
1298ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
129975f0d5a4SJeremy L Thompson 
1300ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1301ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
130275f0d5a4SJeremy L Thompson 
130375f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
130475f0d5a4SJeremy L Thompson 
130575f0d5a4SJeremy L Thompson   @ref Backend
130675f0d5a4SJeremy L Thompson **/
1307c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
13081203703bSJeremy L Thompson   bool is_composite;
13091203703bSJeremy L Thompson 
13101203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13116e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
131275f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
131375f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
131475f0d5a4SJeremy L Thompson }
131575f0d5a4SJeremy L Thompson 
131675f0d5a4SJeremy L Thompson /**
13178a297abdSJames Wright   @brief Get a sub `CeedOperator` of a composite `CeedOperator` from its name.
13188a297abdSJames Wright 
13198a297abdSJames Wright   `sub_op` is set to `NULL` if the sub operator is not found.
13208a297abdSJames Wright 
13218a297abdSJames Wright   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
13228a297abdSJames Wright 
13238a297abdSJames Wright   @param[in]  op      Composite `CeedOperator`
13248a297abdSJames Wright   @param[in]  op_name Name of desired sub `CeedOperator`
13258a297abdSJames Wright   @param[out] sub_op  Sub `CeedOperator` corresponding to the name
13268a297abdSJames Wright 
13278a297abdSJames Wright   @return An error code: 0 - success, otherwise - failure
13288a297abdSJames Wright 
13298a297abdSJames Wright   @ref Advanced
13308a297abdSJames Wright **/
13318a297abdSJames Wright int CeedCompositeOperatorGetSubByName(CeedOperator op, const char *op_name, CeedOperator *sub_op) {
13328a297abdSJames Wright   bool          is_composite;
13338a297abdSJames Wright   CeedInt       num_sub_ops;
13348a297abdSJames Wright   CeedOperator *sub_ops;
13358a297abdSJames Wright 
13368a297abdSJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13378a297abdSJames Wright   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
13388a297abdSJames Wright   *sub_op = NULL;
13398a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_ops));
13408a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_ops));
13418a297abdSJames Wright   for (CeedInt i = 0; i < num_sub_ops; i++) {
13428a297abdSJames Wright     if (sub_ops[i]->name && !strcmp(op_name, sub_ops[i]->name)) {
13438a297abdSJames Wright       *sub_op = sub_ops[i];
13448a297abdSJames Wright       return CEED_ERROR_SUCCESS;
13458a297abdSJames Wright     }
13468a297abdSJames Wright   }
13478a297abdSJames Wright   return CEED_ERROR_SUCCESS;
13488a297abdSJames Wright }
13498a297abdSJames Wright 
13508a297abdSJames Wright /**
1351ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
13524db537f9SJeremy L Thompson 
1353ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
13544db537f9SJeremy L Thompson 
13554db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13564db537f9SJeremy L Thompson 
13574db537f9SJeremy L Thompson   @ref User
13584db537f9SJeremy L Thompson **/
13594db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
13601203703bSJeremy L Thompson   bool          is_at_points, is_composite;
13611203703bSJeremy L Thompson   CeedQFunction qf = NULL;
13624db537f9SJeremy L Thompson 
13632b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
13644db537f9SJeremy L Thompson 
13651203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
13661203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13671203703bSJeremy L Thompson   if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf));
13681203703bSJeremy L Thompson   if (is_composite) {
13691203703bSJeremy L Thompson     CeedInt num_suboperators;
13701203703bSJeremy L Thompson 
13711203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13721203703bSJeremy L Thompson     if (!num_suboperators) {
137343622462SJeremy L Thompson       // Empty operator setup
137443622462SJeremy L Thompson       op->input_size  = 0;
137543622462SJeremy L Thompson       op->output_size = 0;
137643622462SJeremy L Thompson     } else {
13771203703bSJeremy L Thompson       CeedOperator *sub_operators;
13781203703bSJeremy L Thompson 
13791203703bSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13801203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_suboperators; i++) {
13811203703bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(sub_operators[i]));
13824db537f9SJeremy L Thompson       }
13832b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
13842b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
13852b104005SJeremy L Thompson       CeedSize input_size, output_size;
13862b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
138743622462SJeremy L Thompson     }
13884db537f9SJeremy L Thompson   } else {
13891203703bSJeremy L Thompson     CeedInt num_input_fields, num_output_fields;
13901203703bSJeremy L Thompson 
1391*9bc66399SJeremy L Thompson     CeedCheck(op->num_fields > 0, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No operator fields set");
13921203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL));
1393*9bc66399SJeremy L Thompson     CeedCheck(op->num_fields == num_input_fields + num_output_fields, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
1394*9bc66399SJeremy L Thompson               "Not all operator fields set");
1395*9bc66399SJeremy L Thompson     CeedCheck(op->has_restriction, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "At least one restriction required");
1396*9bc66399SJeremy L Thompson     CeedCheck(op->num_qpts > 0 || is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
1397ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
13982b730f8bSJeremy L Thompson   }
13994db537f9SJeremy L Thompson 
14004db537f9SJeremy L Thompson   // Flag as immutable and ready
14014db537f9SJeremy L Thompson   op->is_interface_setup = true;
14021203703bSJeremy L Thompson   if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf));
14031203703bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf));
14041203703bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT));
14054db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14064db537f9SJeremy L Thompson }
14074db537f9SJeremy L Thompson 
14084db537f9SJeremy L Thompson /**
1409ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
14104385fb7fSSebastian Grimberg 
1411ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1412c9366a6bSJeremy L Thompson 
1413ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1414ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1415ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1416c9366a6bSJeremy L Thompson 
1417c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1418c9366a6bSJeremy L Thompson 
1419c9366a6bSJeremy L Thompson   @ref User
1420c9366a6bSJeremy L Thompson **/
14212b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1422c9366a6bSJeremy L Thompson   bool is_composite;
1423c9366a6bSJeremy L Thompson 
14242b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
14252b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1426c9366a6bSJeremy L Thompson 
14272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14282b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
14291203703bSJeremy L Thompson     CeedInt       num_suboperators;
14301203703bSJeremy L Thompson     CeedOperator *sub_operators;
14311203703bSJeremy L Thompson 
14321203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14331203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14341203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
1435c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
14361c66c397SJeremy L Thompson 
14371203703bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(sub_operators[i], &sub_input_size, &sub_output_size));
14382b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
14392b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
14402b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
14416e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
14426e536b99SJeremy L Thompson                 CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR,
1443249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1444249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1445249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
14462b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1447c9366a6bSJeremy L Thompson     }
14482b730f8bSJeremy L Thompson   }
1449c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1450c9366a6bSJeremy L Thompson }
1451c9366a6bSJeremy L Thompson 
1452c9366a6bSJeremy L Thompson /**
1453ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
14544385fb7fSSebastian Grimberg 
1455ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1456ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
14578b919e6bSJeremy L Thompson 
1458ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1459beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
14608b919e6bSJeremy L Thompson 
14618b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14628b919e6bSJeremy L Thompson 
14638b919e6bSJeremy L Thompson   @ref Advanced
14648b919e6bSJeremy L Thompson **/
14652b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
14668b919e6bSJeremy L Thompson   bool is_composite;
14678b919e6bSJeremy L Thompson 
14682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14698b919e6bSJeremy L Thompson   if (is_composite) {
14708b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
14712b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
14728b919e6bSJeremy L Thompson     }
14738b919e6bSJeremy L Thompson   } else {
14747d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14757d5185d7SSebastian Grimberg 
14767d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
14777d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetReuse(data, reuse_assembly_data));
1478beecbf24SJeremy L Thompson   }
1479beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1480beecbf24SJeremy L Thompson }
1481beecbf24SJeremy L Thompson 
1482beecbf24SJeremy L Thompson /**
1483ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1484beecbf24SJeremy L Thompson 
1485ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
14866e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1487beecbf24SJeremy L Thompson 
1488beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1489beecbf24SJeremy L Thompson 
1490beecbf24SJeremy L Thompson   @ref Advanced
1491beecbf24SJeremy L Thompson **/
14922b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1493beecbf24SJeremy L Thompson   bool is_composite;
1494beecbf24SJeremy L Thompson 
14952b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1496beecbf24SJeremy L Thompson   if (is_composite) {
14971203703bSJeremy L Thompson     CeedInt       num_suboperators;
14981203703bSJeremy L Thompson     CeedOperator *sub_operators;
14991203703bSJeremy L Thompson 
15001203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15011203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15021203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15031203703bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(sub_operators[i], needs_data_update));
1504beecbf24SJeremy L Thompson     }
1505beecbf24SJeremy L Thompson   } else {
15067d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
15077d5185d7SSebastian Grimberg 
15087d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
15097d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, needs_data_update));
15108b919e6bSJeremy L Thompson   }
15118b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15128b919e6bSJeremy L Thompson }
15138b919e6bSJeremy L Thompson 
15148b919e6bSJeremy L Thompson /**
1515ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1516ea6b5821SJeremy L Thompson 
1517ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1518ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1519ea6b5821SJeremy L Thompson 
1520ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1521ea6b5821SJeremy L Thompson 
1522ea6b5821SJeremy L Thompson   @ref User
1523ea6b5821SJeremy L Thompson **/
1524ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1525ea6b5821SJeremy L Thompson   char  *name_copy;
1526ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1527ea6b5821SJeremy L Thompson 
15282b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1529ea6b5821SJeremy L Thompson   if (name_len > 0) {
15302b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1531ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1532ea6b5821SJeremy L Thompson     op->name = name_copy;
1533ea6b5821SJeremy L Thompson   }
1534ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1535ea6b5821SJeremy L Thompson }
1536ea6b5821SJeremy L Thompson 
1537ea6b5821SJeremy L Thompson /**
1538935f026aSJeremy L Thompson   @brief Core logic for viewing a `CeedOperator`
15397a982d89SJeremy L. Thompson 
1540935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1541ca94c3ddSJeremy L Thompson   @param[in] stream  Stream to write; typically `stdout` or a file
15428bf1b130SJames Wright   @param[in] is_full Whether to write full operator view or terse
15437a982d89SJeremy L. Thompson 
15447a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
15457a982d89SJeremy L. Thompson **/
15468bf1b130SJames Wright static int CeedOperatorView_Core(CeedOperator op, FILE *stream, bool is_full) {
15471203703bSJeremy L Thompson   bool has_name = op->name, is_composite;
15487a982d89SJeremy L. Thompson 
15491203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15501203703bSJeremy L Thompson   if (is_composite) {
15511203703bSJeremy L Thompson     CeedInt       num_suboperators;
15521203703bSJeremy L Thompson     CeedOperator *sub_operators;
15531203703bSJeremy L Thompson 
15541203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15551203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15562b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
15577a982d89SJeremy L. Thompson 
15581203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15591203703bSJeremy L Thompson       has_name = sub_operators[i]->name;
1560935f026aSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s%s\n", i, has_name ? " - " : "", has_name ? sub_operators[i]->name : "", is_full ? ":" : "");
1561935f026aSJeremy L Thompson       if (is_full) CeedCall(CeedOperatorSingleView(sub_operators[i], 1, stream));
15627a982d89SJeremy L. Thompson     }
15637a982d89SJeremy L. Thompson   } else {
15642b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1565935f026aSJeremy L Thompson     if (is_full) CeedCall(CeedOperatorSingleView(op, 0, stream));
15667a982d89SJeremy L. Thompson   }
1567e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15687a982d89SJeremy L. Thompson }
15693bd813ffSjeremylt 
15703bd813ffSjeremylt /**
1571935f026aSJeremy L Thompson   @brief View a `CeedOperator`
1572935f026aSJeremy L Thompson 
1573935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1574935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1575935f026aSJeremy L Thompson 
1576935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1577935f026aSJeremy L Thompson 
1578935f026aSJeremy L Thompson   @ref User
1579935f026aSJeremy L Thompson **/
1580935f026aSJeremy L Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1581935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, true));
1582935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1583935f026aSJeremy L Thompson }
1584935f026aSJeremy L Thompson 
1585935f026aSJeremy L Thompson /**
1586935f026aSJeremy L Thompson   @brief View a brief summary `CeedOperator`
1587935f026aSJeremy L Thompson 
1588935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1589935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1590935f026aSJeremy L Thompson 
1591935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1592935f026aSJeremy L Thompson 
1593935f026aSJeremy L Thompson   @ref User
1594935f026aSJeremy L Thompson **/
1595935f026aSJeremy L Thompson int CeedOperatorViewTerse(CeedOperator op, FILE *stream) {
1596935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, false));
1597935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1598935f026aSJeremy L Thompson }
1599935f026aSJeremy L Thompson 
1600935f026aSJeremy L Thompson /**
1601ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1602b7c9bbdaSJeremy L Thompson 
1603ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1604ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1605b7c9bbdaSJeremy L Thompson 
1606b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1607b7c9bbdaSJeremy L Thompson 
1608b7c9bbdaSJeremy L Thompson   @ref Advanced
1609b7c9bbdaSJeremy L Thompson **/
1610b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1611*9bc66399SJeremy L Thompson   *ceed = NULL;
1612*9bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), ceed));
1613b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1614b7c9bbdaSJeremy L Thompson }
1615b7c9bbdaSJeremy L Thompson 
1616b7c9bbdaSJeremy L Thompson /**
16176e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
16186e536b99SJeremy L Thompson 
16196e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
16206e536b99SJeremy L Thompson 
16216e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
16226e536b99SJeremy L Thompson 
16236e536b99SJeremy L Thompson   @ref Advanced
16246e536b99SJeremy L Thompson **/
16256e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
16266e536b99SJeremy L Thompson 
16276e536b99SJeremy L Thompson /**
1628ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1629b7c9bbdaSJeremy L Thompson 
1630ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1631b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1632b7c9bbdaSJeremy L Thompson 
1633b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1634b7c9bbdaSJeremy L Thompson 
1635b7c9bbdaSJeremy L Thompson   @ref Advanced
1636b7c9bbdaSJeremy L Thompson **/
1637b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
16381203703bSJeremy L Thompson   bool is_composite;
16391203703bSJeremy L Thompson 
16401203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16416e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1642b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1643b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1644b7c9bbdaSJeremy L Thompson }
1645b7c9bbdaSJeremy L Thompson 
1646b7c9bbdaSJeremy L Thompson /**
1647ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1648b7c9bbdaSJeremy L Thompson 
1649ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1650b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1651b7c9bbdaSJeremy L Thompson 
1652b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1653b7c9bbdaSJeremy L Thompson 
1654b7c9bbdaSJeremy L Thompson   @ref Advanced
1655b7c9bbdaSJeremy L Thompson **/
1656b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
16571203703bSJeremy L Thompson   bool is_composite;
16581203703bSJeremy L Thompson 
16591203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16606e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1661b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1662b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1663b7c9bbdaSJeremy L Thompson }
1664b7c9bbdaSJeremy L Thompson 
1665b7c9bbdaSJeremy L Thompson /**
1666ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
16676e15d496SJeremy L Thompson 
1668ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1669ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
16706e15d496SJeremy L Thompson 
16716e15d496SJeremy L Thompson   @ref Backend
16726e15d496SJeremy L Thompson **/
16739d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
16746e15d496SJeremy L Thompson   bool is_composite;
16751c66c397SJeremy L Thompson 
16762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
16776e15d496SJeremy L Thompson 
16786e15d496SJeremy L Thompson   *flops = 0;
16792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16806e15d496SJeremy L Thompson   if (is_composite) {
16816e15d496SJeremy L Thompson     CeedInt num_suboperators;
16821c66c397SJeremy L Thompson 
1683c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
16846e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1685c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
16866e15d496SJeremy L Thompson 
16876e15d496SJeremy L Thompson     // FLOPs for each suboperator
16886e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
16899d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
16901c66c397SJeremy L Thompson 
16912b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
16926e15d496SJeremy L Thompson       *flops += suboperator_flops;
16936e15d496SJeremy L Thompson     }
16946e15d496SJeremy L Thompson   } else {
16951c66c397SJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0;
16961203703bSJeremy L Thompson     CeedQFunction       qf;
16971203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
16981203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
16991c66c397SJeremy L Thompson 
17001203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
17011203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
17021203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
17032b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
17044385fb7fSSebastian Grimberg 
17056e15d496SJeremy L Thompson     // Input FLOPs
17066e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
17071203703bSJeremy L Thompson       CeedVector vec;
17086e15d496SJeremy L Thompson 
17091203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
17101203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17111203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17121203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17131203703bSJeremy L Thompson         CeedElemRestriction rstr;
17141203703bSJeremy L Thompson         CeedBasis           basis;
17151203703bSJeremy L Thompson 
17161203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
17171203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1718681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1719edb2538eSJeremy L Thompson         *flops += rstr_flops;
17201203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
17211203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
17221203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, &basis_flops));
1723681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
17246e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17256e15d496SJeremy L Thompson       }
1726681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
17276e15d496SJeremy L Thompson     }
17286e15d496SJeremy L Thompson     // QF FLOPs
17291c66c397SJeremy L Thompson     {
17309d36ca50SJeremy L Thompson       CeedInt       num_qpts;
17319d36ca50SJeremy L Thompson       CeedSize      qf_flops;
17321203703bSJeremy L Thompson       CeedQFunction qf;
17331c66c397SJeremy L Thompson 
17342b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
17351203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
17361203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
17376e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
17386e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
17396e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
17401c66c397SJeremy L Thompson     }
17411c66c397SJeremy L Thompson 
17426e15d496SJeremy L Thompson     // Output FLOPs
17436e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
17441203703bSJeremy L Thompson       CeedVector vec;
17456e15d496SJeremy L Thompson 
17461203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
17471203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17481203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17491203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17501203703bSJeremy L Thompson         CeedElemRestriction rstr;
17511203703bSJeremy L Thompson         CeedBasis           basis;
17521203703bSJeremy L Thompson 
17531203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
17541203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1755681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1756edb2538eSJeremy L Thompson         *flops += rstr_flops;
17571203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
17581203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
17591203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, &basis_flops));
1760681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
17616e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17626e15d496SJeremy L Thompson       }
1763681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
17646e15d496SJeremy L Thompson     }
17656e15d496SJeremy L Thompson   }
17666e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17676e15d496SJeremy L Thompson }
17686e15d496SJeremy L Thompson 
17696e15d496SJeremy L Thompson /**
1770ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
17719fd66db6SSebastian Grimberg 
1772ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1773512bb800SJeremy L Thompson 
1774ca94c3ddSJeremy L Thompson   Note: If the value of `ctx` passed into this function is non-`NULL`, then it is assumed that `ctx` is a pointer to a `CeedQFunctionContext`.
1775ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
17760126412dSJeremy L Thompson 
1777ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1778ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
17790126412dSJeremy L Thompson 
17800126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17810126412dSJeremy L Thompson 
1782859c15bbSJames Wright   @ref Advanced
17830126412dSJeremy L Thompson **/
17840126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
17851203703bSJeremy L Thompson   bool                 is_composite;
17861203703bSJeremy L Thompson   CeedQFunction        qf;
17871203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
17881203703bSJeremy L Thompson 
17891203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17906e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
17911203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
17921203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
17931203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
17940126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
17950126412dSJeremy L Thompson }
17960126412dSJeremy L Thompson 
17970126412dSJeremy L Thompson /**
1798ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
17993668ca4bSJeremy L Thompson 
1800ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1801859c15bbSJames Wright 
1802ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18033668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
18043668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
18053668ca4bSJeremy L Thompson 
18063668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18073668ca4bSJeremy L Thompson 
18083668ca4bSJeremy L Thompson   @ref User
18093668ca4bSJeremy L Thompson **/
181017b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
18111c66c397SJeremy L Thompson   bool is_composite, field_found = false;
18121c66c397SJeremy L Thompson 
18132b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
18142b730f8bSJeremy L Thompson 
18153668ca4bSJeremy L Thompson   if (is_composite) {
1816a98a090bSJeremy L Thompson     // Composite operator
1817a98a090bSJeremy L Thompson     // -- Check if composite label already created
18183668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
18193668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
18203668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
18213668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
18223668ca4bSJeremy L Thompson       }
18233668ca4bSJeremy L Thompson     }
18243668ca4bSJeremy L Thompson 
1825a98a090bSJeremy L Thompson     // -- Create composite label if needed
18263668ca4bSJeremy L Thompson     CeedInt               num_sub;
18273668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
18283668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
18293668ca4bSJeremy L Thompson 
18302b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1831c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1832c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
18332b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
18343668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
18353668ca4bSJeremy L Thompson 
18363668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
18373668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
18383668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
18391c66c397SJeremy L Thompson 
18402b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
18413668ca4bSJeremy L Thompson         if (new_field_label_i) {
1842a98a090bSJeremy L Thompson           field_found                    = true;
18433668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
18443668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
18453668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
18462b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
18477bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18482b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18496e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
18502b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
18517bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18527bfe0f0eSJeremy L Thompson           } else {
18537bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
18547bfe0f0eSJeremy L Thompson           }
18552b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
18567bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18572b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18586e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
18596e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
18606e536b99SJeremy L Thompson                              new_field_label_i->num_values);
18617bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18627bfe0f0eSJeremy L Thompson           } else {
18637bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
18647bfe0f0eSJeremy L Thompson           }
18653668ca4bSJeremy L Thompson         }
18663668ca4bSJeremy L Thompson       }
18673668ca4bSJeremy L Thompson     }
1868a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1869a98a090bSJeremy L Thompson     if (field_found) {
1870a98a090bSJeremy L Thompson       *field_label = new_field_label;
1871a98a090bSJeremy L Thompson     } else {
18723668ca4bSJeremy L Thompson       // LCOV_EXCL_START
18732b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
18742b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
18753668ca4bSJeremy L Thompson       *field_label = NULL;
18763668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
18775ac9af79SJeremy L Thompson     }
18785ac9af79SJeremy L Thompson   } else {
18791203703bSJeremy L Thompson     CeedQFunction        qf;
18801203703bSJeremy L Thompson     CeedQFunctionContext ctx;
18811203703bSJeremy L Thompson 
1882a98a090bSJeremy L Thompson     // Single, non-composite operator
18831203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
18841203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
18851203703bSJeremy L Thompson     if (ctx) {
18861203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1887a98a090bSJeremy L Thompson     } else {
1888a98a090bSJeremy L Thompson       *field_label = NULL;
1889a98a090bSJeremy L Thompson     }
18905ac9af79SJeremy L Thompson   }
18915ac9af79SJeremy L Thompson 
18925ac9af79SJeremy L Thompson   // Set label in operator
18935ac9af79SJeremy L Thompson   if (*field_label) {
18945ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
18955ac9af79SJeremy L Thompson 
18963668ca4bSJeremy L Thompson     // Move new composite label to operator
18973668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
18982b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
18993668ca4bSJeremy L Thompson       op->max_context_labels = 1;
19003668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
19012b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
19023668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
19033668ca4bSJeremy L Thompson     }
19045ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
19053668ca4bSJeremy L Thompson     op->num_context_labels++;
19063668ca4bSJeremy L Thompson   }
19073668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
19083668ca4bSJeremy L Thompson }
19093668ca4bSJeremy L Thompson 
19103668ca4bSJeremy L Thompson /**
1911ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
19124385fb7fSSebastian Grimberg 
1913ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1914d8dd9a91SJeremy L Thompson 
1915ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19162788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1917ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1918d8dd9a91SJeremy L Thompson 
1919d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1920d8dd9a91SJeremy L Thompson 
1921d8dd9a91SJeremy L Thompson   @ref User
1922d8dd9a91SJeremy L Thompson **/
192317b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
19242b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1925d8dd9a91SJeremy L Thompson }
1926d8dd9a91SJeremy L Thompson 
1927d8dd9a91SJeremy L Thompson /**
1928ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
19294385fb7fSSebastian Grimberg 
1930ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19312788fa27SJeremy L Thompson 
1932ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19332788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
19342788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
19352788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19362788fa27SJeremy L Thompson 
19372788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19382788fa27SJeremy L Thompson 
19392788fa27SJeremy L Thompson   @ref User
19402788fa27SJeremy L Thompson **/
194117b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
19422788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
19432788fa27SJeremy L Thompson }
19442788fa27SJeremy L Thompson 
19452788fa27SJeremy L Thompson /**
1946ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
19472788fa27SJeremy L Thompson 
1948ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19492788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
19502788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19512788fa27SJeremy L Thompson 
19522788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19532788fa27SJeremy L Thompson 
19542788fa27SJeremy L Thompson   @ref User
19552788fa27SJeremy L Thompson **/
195617b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
19572788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
19582788fa27SJeremy L Thompson }
19592788fa27SJeremy L Thompson 
19602788fa27SJeremy L Thompson /**
1961ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
19624385fb7fSSebastian Grimberg 
1963ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1964d8dd9a91SJeremy L Thompson 
1965ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1966ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1967ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1968d8dd9a91SJeremy L Thompson 
1969d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1970d8dd9a91SJeremy L Thompson 
1971d8dd9a91SJeremy L Thompson   @ref User
1972d8dd9a91SJeremy L Thompson **/
197323dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
19742b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1975d8dd9a91SJeremy L Thompson }
1976d8dd9a91SJeremy L Thompson 
1977d8dd9a91SJeremy L Thompson /**
1978ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
19794385fb7fSSebastian Grimberg 
1980ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19812788fa27SJeremy L Thompson 
1982ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19832788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1984ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
19852788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19862788fa27SJeremy L Thompson 
19872788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19882788fa27SJeremy L Thompson 
19892788fa27SJeremy L Thompson   @ref User
19902788fa27SJeremy L Thompson **/
199123dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
19922788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
19932788fa27SJeremy L Thompson }
19942788fa27SJeremy L Thompson 
19952788fa27SJeremy L Thompson /**
1996ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
19972788fa27SJeremy L Thompson 
1998ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19992788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
20002788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20012788fa27SJeremy L Thompson 
20022788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20032788fa27SJeremy L Thompson 
20042788fa27SJeremy L Thompson   @ref User
20052788fa27SJeremy L Thompson **/
200623dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
20072788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
20082788fa27SJeremy L Thompson }
20092788fa27SJeremy L Thompson 
20102788fa27SJeremy L Thompson /**
2011ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
20125b6ec284SJeremy L Thompson 
2013ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
20145b6ec284SJeremy L Thompson 
2015ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
20165b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
20175b6ec284SJeremy L Thompson   @param[in]     values      Values to set
20185b6ec284SJeremy L Thompson 
20195b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20205b6ec284SJeremy L Thompson 
20215b6ec284SJeremy L Thompson   @ref User
20225b6ec284SJeremy L Thompson **/
20235b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
20245b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20255b6ec284SJeremy L Thompson }
20265b6ec284SJeremy L Thompson 
20275b6ec284SJeremy L Thompson /**
2028ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
20295b6ec284SJeremy L Thompson 
2030ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
20315b6ec284SJeremy L Thompson 
2032ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20335b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
2034ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
20355b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20365b6ec284SJeremy L Thompson 
20375b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20385b6ec284SJeremy L Thompson 
20395b6ec284SJeremy L Thompson   @ref User
20405b6ec284SJeremy L Thompson **/
20415b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
20425b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
20435b6ec284SJeremy L Thompson }
20445b6ec284SJeremy L Thompson 
20455b6ec284SJeremy L Thompson /**
2046ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
20475b6ec284SJeremy L Thompson 
2048ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20495b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
20505b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20515b6ec284SJeremy L Thompson 
20525b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20535b6ec284SJeremy L Thompson 
20545b6ec284SJeremy L Thompson   @ref User
20555b6ec284SJeremy L Thompson **/
20565b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
20575b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20585b6ec284SJeremy L Thompson }
20595b6ec284SJeremy L Thompson 
20605b6ec284SJeremy L Thompson /**
2061ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
2062d7b241e6Sjeremylt 
2063ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2064ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2065d7b241e6Sjeremylt 
2066ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2067f04ea552SJeremy L Thompson 
2068ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2069ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2070ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to store result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
2071ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2072b11c1e72Sjeremylt 
2073b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2074dfdf5a53Sjeremylt 
20757a982d89SJeremy L. Thompson   @ref User
2076b11c1e72Sjeremylt **/
20772b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
20781203703bSJeremy L Thompson   bool is_composite;
20791203703bSJeremy L Thompson 
20802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2081d7b241e6Sjeremylt 
20821203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
20831203703bSJeremy L Thompson   if (is_composite) {
2084250756a7Sjeremylt     // Composite Operator
2085250756a7Sjeremylt     if (op->ApplyComposite) {
20862b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
2087250756a7Sjeremylt     } else {
2088d1d35e2fSjeremylt       CeedInt       num_suboperators;
2089d1d35e2fSjeremylt       CeedOperator *sub_operators;
20901c66c397SJeremy L Thompson 
20911c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2092c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2093250756a7Sjeremylt 
2094250756a7Sjeremylt       // Zero all output vectors
20953ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2096d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
20971203703bSJeremy L Thompson         CeedInt            num_output_fields;
20981203703bSJeremy L Thompson         CeedOperatorField *output_fields;
20991c66c397SJeremy L Thompson 
21001203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
21011203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
21021203703bSJeremy L Thompson           CeedVector vec;
21031203703bSJeremy L Thompson 
21041203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2105250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
21062b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2107250756a7Sjeremylt           }
2108681d0ea7SJeremy L Thompson           CeedCall(CeedVectorDestroy(&vec));
2109250756a7Sjeremylt         }
2110250756a7Sjeremylt       }
2111250756a7Sjeremylt       // Apply
2112f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2113f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2114cae8b89aSjeremylt       }
2115cae8b89aSjeremylt     }
21163ca2b39bSJeremy L Thompson   } else {
21173ca2b39bSJeremy L Thompson     // Standard Operator
21183ca2b39bSJeremy L Thompson     if (op->Apply) {
21193ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
21203ca2b39bSJeremy L Thompson     } else {
21211203703bSJeremy L Thompson       CeedInt            num_output_fields;
21221203703bSJeremy L Thompson       CeedOperatorField *output_fields;
21231203703bSJeremy L Thompson 
21241203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
21253ca2b39bSJeremy L Thompson       // Zero all output vectors
21261203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2127681d0ea7SJeremy L Thompson         bool       is_active;
21281203703bSJeremy L Thompson         CeedVector vec;
21291c66c397SJeremy L Thompson 
21301203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
2131681d0ea7SJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
2132681d0ea7SJeremy L Thompson         if (is_active) vec = out;
21333ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2134681d0ea7SJeremy L Thompson         if (!is_active) CeedCall(CeedVectorDestroy(&vec));
21353ca2b39bSJeremy L Thompson       }
21363ca2b39bSJeremy L Thompson       // Apply
21371203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
21383ca2b39bSJeremy L Thompson     }
2139250756a7Sjeremylt   }
2140e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2141cae8b89aSjeremylt }
2142cae8b89aSjeremylt 
2143cae8b89aSjeremylt /**
2144ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2145cae8b89aSjeremylt 
2146ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2147ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2148cae8b89aSjeremylt 
2149ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2150ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2151ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
2152ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2153cae8b89aSjeremylt 
2154cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2155cae8b89aSjeremylt 
21567a982d89SJeremy L. Thompson   @ref User
2157cae8b89aSjeremylt **/
21582b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
21591203703bSJeremy L Thompson   bool is_composite;
21601203703bSJeremy L Thompson 
21612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2162cae8b89aSjeremylt 
21631203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
21641203703bSJeremy L Thompson   if (is_composite) {
2165250756a7Sjeremylt     // Composite Operator
2166250756a7Sjeremylt     if (op->ApplyAddComposite) {
21672b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2168cae8b89aSjeremylt     } else {
2169d1d35e2fSjeremylt       CeedInt       num_suboperators;
2170d1d35e2fSjeremylt       CeedOperator *sub_operators;
2171250756a7Sjeremylt 
21721c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
21731c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2174d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
21752b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
21761d7d2407SJeremy L Thompson       }
2177250756a7Sjeremylt     }
21781203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
21793ca2b39bSJeremy L Thompson     // Standard Operator
21803ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2181250756a7Sjeremylt   }
2182e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2183d7b241e6Sjeremylt }
2184d7b241e6Sjeremylt 
2185d7b241e6Sjeremylt /**
2186536b928cSSebastian Grimberg   @brief Destroy temporary assembly data associated with a `CeedOperator`
2187536b928cSSebastian Grimberg 
2188536b928cSSebastian Grimberg   @param[in,out] op `CeedOperator` whose assembly data to destroy
2189536b928cSSebastian Grimberg 
2190536b928cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2191536b928cSSebastian Grimberg 
2192536b928cSSebastian Grimberg   @ref User
2193536b928cSSebastian Grimberg **/
2194536b928cSSebastian Grimberg int CeedOperatorAssemblyDataStrip(CeedOperator op) {
2195536b928cSSebastian Grimberg   bool is_composite;
2196536b928cSSebastian Grimberg 
2197536b928cSSebastian Grimberg   CeedCall(CeedQFunctionAssemblyDataDestroy(&op->qf_assembled));
2198536b928cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataDestroy(&op->op_assembled));
2199536b928cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2200536b928cSSebastian Grimberg   if (is_composite) {
2201536b928cSSebastian Grimberg     CeedInt       num_suboperators;
2202536b928cSSebastian Grimberg     CeedOperator *sub_operators;
2203536b928cSSebastian Grimberg 
2204536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2205536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2206536b928cSSebastian Grimberg     for (CeedInt i = 0; i < num_suboperators; i++) {
2207536b928cSSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataDestroy(&sub_operators[i]->qf_assembled));
2208536b928cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataDestroy(&sub_operators[i]->op_assembled));
2209536b928cSSebastian Grimberg     }
2210536b928cSSebastian Grimberg   }
2211536b928cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2212536b928cSSebastian Grimberg }
2213536b928cSSebastian Grimberg 
2214536b928cSSebastian Grimberg /**
2215ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2216d7b241e6Sjeremylt 
2217ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2218b11c1e72Sjeremylt 
2219b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2220dfdf5a53Sjeremylt 
22217a982d89SJeremy L. Thompson   @ref User
2222b11c1e72Sjeremylt **/
2223d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2224ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2225ad6481ceSJeremy L Thompson     *op = NULL;
2226ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2227ad6481ceSJeremy L Thompson   }
2228f883f0a5SSebastian Grimberg   // Backend destroy
2229f883f0a5SSebastian Grimberg   if ((*op)->Destroy) {
2230f883f0a5SSebastian Grimberg     CeedCall((*op)->Destroy(*op));
2231f883f0a5SSebastian Grimberg   }
2232fe2413ffSjeremylt   // Free fields
22332b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2234d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2235437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2236437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
223715910d16Sjeremylt       }
2238356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
22392b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
224071352a93Sjeremylt       }
22412b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
22422b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
224371352a93Sjeremylt       }
22442b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
22452b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2246fe2413ffSjeremylt     }
22472b730f8bSJeremy L Thompson   }
22482b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2249d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2250437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2251356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
22522b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
225371352a93Sjeremylt       }
22542b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
22552b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
225671352a93Sjeremylt       }
22572b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
22582b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
22592b730f8bSJeremy L Thompson     }
2260fe2413ffSjeremylt   }
2261f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->input_fields));
2262f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->output_fields));
2263f883f0a5SSebastian Grimberg   // Destroy AtPoints data
226448acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
226548acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
226648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2267c6b536a8SSebastian Grimberg   // Destroy assembly data (must happen before destroying sub_operators)
2268f883f0a5SSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataStrip(*op));
2269d1d35e2fSjeremylt   // Destroy sub_operators
22702b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2271d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
22722b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
227352d6035fSJeremy L Thompson     }
22742b730f8bSJeremy L Thompson   }
2275f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->sub_operators));
22762b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
22772b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
22782b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
22793668ca4bSJeremy L Thompson   // Destroy any composite labels
22805ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
22813668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
22822b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
22832b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
22843668ca4bSJeremy L Thompson     }
22855ac9af79SJeremy L Thompson   }
22862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2287fe2413ffSjeremylt 
22885107b09fSJeremy L Thompson   // Destroy fallback
22892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
22905107b09fSJeremy L Thompson 
22912b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
2292f883f0a5SSebastian Grimberg   CeedCall(CeedDestroy(&(*op)->ceed));
22932b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2294e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2295d7b241e6Sjeremylt }
2296d7b241e6Sjeremylt 
2297d7b241e6Sjeremylt /// @}
2298