xref: /libCEED/interface/ceed-operator.c (revision 3f919cbc23e59285304a4fc152ac9ef29c71e01d)
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));
151c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
1521c66c397SJeremy L Thompson 
1532b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1542b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1551203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, num_input_fields, num_input_fields > 1 ? "s" : "");
1561203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1571203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_input_fields[i], qf_input_fields[i], i, sub, 1, stream));
1587a982d89SJeremy L. Thompson   }
1591203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, num_output_fields, num_output_fields > 1 ? "s" : "");
1601203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1611203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_output_fields[i], qf_output_fields[i], i, sub, 0, stream));
1627a982d89SJeremy L. Thompson   }
163e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1647a982d89SJeremy L. Thompson }
1657a982d89SJeremy L. Thompson 
166d99fa3c5SJeremy L Thompson /**
167681d0ea7SJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`.
168681d0ea7SJeremy L Thompson 
169681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_basis` with @ref CeedBasisDestroy().
170eaf62fffSJeremy L Thompson 
171ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
172ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
173eaf62fffSJeremy L Thompson 
174eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
175eaf62fffSJeremy L Thompson 
176eaf62fffSJeremy L Thompson   @ref Developer
177eaf62fffSJeremy L Thompson **/
178eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
179506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
180506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
181506b1a0cSSebastian Grimberg }
182506b1a0cSSebastian Grimberg 
183506b1a0cSSebastian Grimberg /**
184681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`.
185681d0ea7SJeremy L Thompson 
186681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the bases with @ref CeedBasisDestroy().
187506b1a0cSSebastian Grimberg 
188ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
189ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
190ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
191506b1a0cSSebastian Grimberg 
192506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
193506b1a0cSSebastian Grimberg 
194506b1a0cSSebastian Grimberg   @ref Developer
195506b1a0cSSebastian Grimberg **/
196506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1971203703bSJeremy L Thompson   bool               is_composite;
1981203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
1991203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2001c66c397SJeremy L Thompson 
2011203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2021203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2031203703bSJeremy L Thompson 
204506b1a0cSSebastian Grimberg   if (active_input_basis) {
205506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
2061203703bSJeremy L Thompson     if (!is_composite) {
2071203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2081203703bSJeremy L Thompson         CeedVector vec;
2091203703bSJeremy L Thompson 
2101203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2111203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2121203703bSJeremy L Thompson           CeedBasis basis;
2131203703bSJeremy L Thompson 
2141203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
2159bc66399SJeremy L Thompson           CeedCheck(!*active_input_basis || *active_input_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
2169bc66399SJeremy L Thompson                     "Multiple active input CeedBases found");
217681d0ea7SJeremy L Thompson           if (!*active_input_basis) CeedCall(CeedBasisReferenceCopy(basis, active_input_basis));
218681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
219eaf62fffSJeremy L Thompson         }
220681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
2212b730f8bSJeremy L Thompson       }
2229bc66399SJeremy L Thompson       CeedCheck(*active_input_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
223506b1a0cSSebastian Grimberg     }
224506b1a0cSSebastian Grimberg   }
225506b1a0cSSebastian Grimberg   if (active_output_basis) {
226506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
2271203703bSJeremy L Thompson     if (!is_composite) {
2281203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2291203703bSJeremy L Thompson         CeedVector vec;
2301203703bSJeremy L Thompson 
2311203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2321203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2331203703bSJeremy L Thompson           CeedBasis basis;
2341203703bSJeremy L Thompson 
2351203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
2369bc66399SJeremy L Thompson           CeedCheck(!*active_output_basis || *active_output_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
2379bc66399SJeremy L Thompson                     "Multiple active output CeedBases found");
238681d0ea7SJeremy L Thompson           if (!*active_output_basis) CeedCall(CeedBasisReferenceCopy(basis, active_output_basis));
239681d0ea7SJeremy L Thompson           CeedCall(CeedBasisDestroy(&basis));
240506b1a0cSSebastian Grimberg         }
241681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
242506b1a0cSSebastian Grimberg       }
2439bc66399SJeremy L Thompson       CeedCheck(*active_output_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
244506b1a0cSSebastian Grimberg     }
245506b1a0cSSebastian Grimberg   }
246eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
247eaf62fffSJeremy L Thompson }
248eaf62fffSJeremy L Thompson 
249eaf62fffSJeremy L Thompson /**
250681d0ea7SJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`.
251681d0ea7SJeremy L Thompson 
252681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `active_rstr` with @ref CeedElemRestrictionDestroy().
253e2f04181SAndrew T. Barker 
254ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
255ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
256e2f04181SAndrew T. Barker 
257e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
258e2f04181SAndrew T. Barker 
259e2f04181SAndrew T. Barker   @ref Utility
260e2f04181SAndrew T. Barker **/
2612b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
262506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
263506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
264506b1a0cSSebastian Grimberg }
265506b1a0cSSebastian Grimberg 
266506b1a0cSSebastian Grimberg /**
267681d0ea7SJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`.
268681d0ea7SJeremy L Thompson 
269681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the restrictions with @ref CeedElemRestrictionDestroy().
270506b1a0cSSebastian Grimberg 
271ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
272ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
273ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
274506b1a0cSSebastian Grimberg 
275506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
276506b1a0cSSebastian Grimberg 
277506b1a0cSSebastian Grimberg   @ref Utility
278506b1a0cSSebastian Grimberg **/
279506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2801203703bSJeremy L Thompson   bool               is_composite;
2811203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2821203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2831c66c397SJeremy L Thompson 
2841203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2851203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2861203703bSJeremy L Thompson 
287506b1a0cSSebastian Grimberg   if (active_input_rstr) {
288506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
2891203703bSJeremy L Thompson     if (!is_composite) {
2901203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2911203703bSJeremy L Thompson         CeedVector vec;
2921203703bSJeremy L Thompson 
2931203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2941203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2951203703bSJeremy L Thompson           CeedElemRestriction rstr;
2961203703bSJeremy L Thompson 
2971203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
2989bc66399SJeremy L Thompson           CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
2999bc66399SJeremy L Thompson                     "Multiple active input CeedElemRestrictions found");
300681d0ea7SJeremy L Thompson           if (!*active_input_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_input_rstr));
301681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
302e2f04181SAndrew T. Barker         }
303681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
3042b730f8bSJeremy L Thompson       }
3059bc66399SJeremy L Thompson       CeedCheck(*active_input_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
306506b1a0cSSebastian Grimberg     }
307506b1a0cSSebastian Grimberg   }
308506b1a0cSSebastian Grimberg   if (active_output_rstr) {
309506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
3101203703bSJeremy L Thompson     if (!is_composite) {
3111203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
3121203703bSJeremy L Thompson         CeedVector vec;
3131203703bSJeremy L Thompson 
3141203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
3151203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
3161203703bSJeremy L Thompson           CeedElemRestriction rstr;
3171203703bSJeremy L Thompson 
3181203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
3199bc66399SJeremy L Thompson           CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR,
3209bc66399SJeremy L Thompson                     "Multiple active output CeedElemRestrictions found");
321681d0ea7SJeremy L Thompson           if (!*active_output_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_output_rstr));
322681d0ea7SJeremy L Thompson           CeedCall(CeedElemRestrictionDestroy(&rstr));
323506b1a0cSSebastian Grimberg         }
324681d0ea7SJeremy L Thompson         CeedCall(CeedVectorDestroy(&vec));
325506b1a0cSSebastian Grimberg       }
3269bc66399SJeremy L Thompson       CeedCheck(*active_output_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
327506b1a0cSSebastian Grimberg     }
328506b1a0cSSebastian Grimberg   }
329e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
330e2f04181SAndrew T. Barker }
331e2f04181SAndrew T. Barker 
332d8dd9a91SJeremy L Thompson /**
333ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
3344385fb7fSSebastian Grimberg 
335ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
336ca94c3ddSJeremy 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.
337d8dd9a91SJeremy L Thompson 
338ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
339ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
340ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
3412788fa27SJeremy L Thompson   @param[in]     values      Values to set
342d8dd9a91SJeremy L Thompson 
343d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
344d8dd9a91SJeremy L Thompson 
345d8dd9a91SJeremy L Thompson   @ref User
346d8dd9a91SJeremy L Thompson **/
3472788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3481c66c397SJeremy L Thompson   bool is_composite = false;
3491c66c397SJeremy L Thompson 
3509bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
3513668ca4bSJeremy L Thompson 
3525ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3535ac9af79SJeremy L Thompson   if (field_label->from_op) {
3545ac9af79SJeremy L Thompson     CeedInt index = -1;
3555ac9af79SJeremy L Thompson 
3565ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3575ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3585ac9af79SJeremy L Thompson     }
3599bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3605ac9af79SJeremy L Thompson   }
3615ac9af79SJeremy L Thompson 
3622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
363d8dd9a91SJeremy L Thompson   if (is_composite) {
364d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
365d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
366d8dd9a91SJeremy L Thompson 
367c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
368c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3699bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
3709bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
371d8dd9a91SJeremy L Thompson 
372d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3731203703bSJeremy L Thompson       CeedQFunctionContext ctx;
3741203703bSJeremy L Thompson 
3751485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
376d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3771485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
3781203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values));
379d8dd9a91SJeremy L Thompson       }
3801485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
381d8dd9a91SJeremy L Thompson     }
382d8dd9a91SJeremy L Thompson   } else {
3831203703bSJeremy L Thompson     CeedQFunctionContext ctx;
3841203703bSJeremy L Thompson 
3851485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
3869bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3871203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values));
3881485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
3892788fa27SJeremy L Thompson   }
3906d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3912788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3922788fa27SJeremy L Thompson }
3932788fa27SJeremy L Thompson 
3942788fa27SJeremy L Thompson /**
395ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3964385fb7fSSebastian Grimberg 
397ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
398ca94c3ddSJeremy 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.
3992788fa27SJeremy L Thompson 
400ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4012788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4022788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
403c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
404c5d0f995SJed Brown   @param[out]    values      Values in the label
4052788fa27SJeremy L Thompson 
4062788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4072788fa27SJeremy L Thompson 
4082788fa27SJeremy L Thompson   @ref User
4092788fa27SJeremy L Thompson **/
4102788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
4112788fa27SJeremy L Thompson                                              void *values) {
4121c66c397SJeremy L Thompson   bool is_composite = false;
4131c66c397SJeremy L Thompson 
4149bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4152788fa27SJeremy L Thompson 
4162788fa27SJeremy L Thompson   *(void **)values = NULL;
4172788fa27SJeremy L Thompson   *num_values      = 0;
4182788fa27SJeremy L Thompson 
4195ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4205ac9af79SJeremy L Thompson   if (field_label->from_op) {
4215ac9af79SJeremy L Thompson     CeedInt index = -1;
4225ac9af79SJeremy L Thompson 
4235ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4245ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4255ac9af79SJeremy L Thompson     }
4269bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4275ac9af79SJeremy L Thompson   }
4285ac9af79SJeremy L Thompson 
4292788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4302788fa27SJeremy L Thompson   if (is_composite) {
4312788fa27SJeremy L Thompson     CeedInt       num_sub;
4322788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4332788fa27SJeremy L Thompson 
4342788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4352788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4369bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
4379bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4382788fa27SJeremy L Thompson 
4392788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4401203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4411203703bSJeremy L Thompson 
4421485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
4432788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4441485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
4451203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values));
4461485364cSJeremy L Thompson         CeedCall(CeedQFunctionContextDestroy(&ctx));
4472788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4482788fa27SJeremy L Thompson       }
4491485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
4502788fa27SJeremy L Thompson     }
4512788fa27SJeremy L Thompson   } else {
4521203703bSJeremy L Thompson     CeedQFunctionContext ctx;
4531203703bSJeremy L Thompson 
4541485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
4559bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4561203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values));
4571485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
4582788fa27SJeremy L Thompson   }
4592788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4602788fa27SJeremy L Thompson }
4612788fa27SJeremy L Thompson 
4622788fa27SJeremy L Thompson /**
463ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
4644385fb7fSSebastian Grimberg 
465ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
466ca94c3ddSJeremy 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.
4672788fa27SJeremy L Thompson 
468ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4692788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4702788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
471c5d0f995SJed Brown   @param[in]     values      Values array to restore
4722788fa27SJeremy L Thompson 
4732788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4742788fa27SJeremy L Thompson 
4752788fa27SJeremy L Thompson   @ref User
4762788fa27SJeremy L Thompson **/
4772788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4781c66c397SJeremy L Thompson   bool is_composite = false;
4791c66c397SJeremy L Thompson 
4809bc66399SJeremy L Thompson   CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4812788fa27SJeremy L Thompson 
4825ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4835ac9af79SJeremy L Thompson   if (field_label->from_op) {
4845ac9af79SJeremy L Thompson     CeedInt index = -1;
4855ac9af79SJeremy L Thompson 
4865ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4875ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4885ac9af79SJeremy L Thompson     }
4899bc66399SJeremy L Thompson     CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4905ac9af79SJeremy L Thompson   }
4915ac9af79SJeremy L Thompson 
4922788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4932788fa27SJeremy L Thompson   if (is_composite) {
4942788fa27SJeremy L Thompson     CeedInt       num_sub;
4952788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4962788fa27SJeremy L Thompson 
4972788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4982788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4999bc66399SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
5009bc66399SJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
5012788fa27SJeremy L Thompson 
5022788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
5031203703bSJeremy L Thompson       CeedQFunctionContext ctx;
5041203703bSJeremy L Thompson 
5051485364cSJeremy L Thompson       CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx));
5062788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
5071485364cSJeremy L Thompson       if (ctx && field_label->sub_labels[i]) {
5081203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values));
5091485364cSJeremy L Thompson         CeedCall(CeedQFunctionContextDestroy(&ctx));
5102788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
5112788fa27SJeremy L Thompson       }
5121485364cSJeremy L Thompson       CeedCall(CeedQFunctionContextDestroy(&ctx));
5132788fa27SJeremy L Thompson     }
5142788fa27SJeremy L Thompson   } else {
5151203703bSJeremy L Thompson     CeedQFunctionContext ctx;
5161203703bSJeremy L Thompson 
5171485364cSJeremy L Thompson     CeedCall(CeedOperatorGetContext(op, &ctx));
5189bc66399SJeremy L Thompson     CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
5191203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values));
5201485364cSJeremy L Thompson     CeedCall(CeedQFunctionContextDestroy(&ctx));
521d8dd9a91SJeremy L Thompson   }
522d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
523d8dd9a91SJeremy L Thompson }
524d8dd9a91SJeremy L Thompson 
5257a982d89SJeremy L. Thompson /// @}
5267a982d89SJeremy L. Thompson 
5277a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5287a982d89SJeremy L. Thompson /// CeedOperator Backend API
5297a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5307a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
5317a982d89SJeremy L. Thompson /// @{
5327a982d89SJeremy L. Thompson 
5337a982d89SJeremy L. Thompson /**
534ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
5357a982d89SJeremy L. Thompson 
536ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
537d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5407a982d89SJeremy L. Thompson 
5417a982d89SJeremy L. Thompson   @ref Backend
5427a982d89SJeremy L. Thompson **/
543d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
5441203703bSJeremy L Thompson   bool is_composite;
5451203703bSJeremy L Thompson 
5461203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5476e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operators");
548d1d35e2fSjeremylt   *num_args = op->num_fields;
549e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5507a982d89SJeremy L. Thompson }
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson /**
5539463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
5549463e855SJeremy L Thompson 
5559463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
5569463e855SJeremy L Thompson 
5579463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
5589463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
5599463e855SJeremy L Thompson 
5609463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5619463e855SJeremy L Thompson 
5629463e855SJeremy L Thompson   @ref Backend
5639463e855SJeremy L Thompson **/
5649463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
5659463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
5669463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
5679463e855SJeremy L Thompson 
5689463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
5699463e855SJeremy L Thompson   *has_tensor_bases = true;
5709463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
5719463e855SJeremy L Thompson     bool      is_tensor;
5729463e855SJeremy L Thompson     CeedBasis basis;
5739463e855SJeremy L Thompson 
5749463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
5759463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5769463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5779463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5789463e855SJeremy L Thompson     }
579681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
5809463e855SJeremy L Thompson   }
5819463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
5829463e855SJeremy L Thompson     bool      is_tensor;
5839463e855SJeremy L Thompson     CeedBasis basis;
5849463e855SJeremy L Thompson 
5859463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
5869463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5879463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5889463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5899463e855SJeremy L Thompson     }
590681d0ea7SJeremy L Thompson     CeedCall(CeedBasisDestroy(&basis));
5919463e855SJeremy L Thompson   }
5929463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5939463e855SJeremy L Thompson }
5949463e855SJeremy L Thompson 
5959463e855SJeremy L Thompson /**
5961203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is immutable
5971203703bSJeremy L Thompson 
5981203703bSJeremy L Thompson   @param[in]  op           `CeedOperator`
5991203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
6001203703bSJeremy L Thompson 
6011203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6021203703bSJeremy L Thompson 
6031203703bSJeremy L Thompson   @ref Backend
6041203703bSJeremy L Thompson **/
6051203703bSJeremy L Thompson int CeedOperatorIsImmutable(CeedOperator op, bool *is_immutable) {
6061203703bSJeremy L Thompson   *is_immutable = op->is_immutable;
6071203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
6081203703bSJeremy L Thompson }
6091203703bSJeremy L Thompson 
6101203703bSJeremy L Thompson /**
611ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
6127a982d89SJeremy L. Thompson 
613ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
614d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
6157a982d89SJeremy L. Thompson 
6167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6177a982d89SJeremy L. Thompson 
6187a982d89SJeremy L. Thompson   @ref Backend
6197a982d89SJeremy L. Thompson **/
620d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
621f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
622e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6237a982d89SJeremy L. Thompson }
6247a982d89SJeremy L. Thompson 
6257a982d89SJeremy L. Thompson /**
626ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
6277a982d89SJeremy L. Thompson 
628ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
629ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
6307a982d89SJeremy L. Thompson 
6317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6327a982d89SJeremy L. Thompson 
6337a982d89SJeremy L. Thompson   @ref Backend
6347a982d89SJeremy L. Thompson **/
6357a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
6361203703bSJeremy L Thompson   bool is_composite;
6371203703bSJeremy L Thompson 
6381203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6396e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
640c11e12f4SJeremy L Thompson   *qf = NULL;
641c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(op->qf, qf));
642e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6437a982d89SJeremy L. Thompson }
6447a982d89SJeremy L. Thompson 
6457a982d89SJeremy L. Thompson /**
646ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
647c04a41a7SJeremy L Thompson 
648ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
649d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
650c04a41a7SJeremy L Thompson 
651c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
652c04a41a7SJeremy L Thompson 
653c04a41a7SJeremy L Thompson   @ref Backend
654c04a41a7SJeremy L Thompson **/
655d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
656f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
657e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
658c04a41a7SJeremy L Thompson }
659c04a41a7SJeremy L Thompson 
660c04a41a7SJeremy L Thompson /**
661ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
6627a982d89SJeremy L. Thompson 
663ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
6647a982d89SJeremy L. Thompson   @param[out] data Variable to store data
6657a982d89SJeremy L. Thompson 
6667a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6677a982d89SJeremy L. Thompson 
6687a982d89SJeremy L. Thompson   @ref Backend
6697a982d89SJeremy L. Thompson **/
670777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
671777ff853SJeremy L Thompson   *(void **)data = op->data;
672e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6737a982d89SJeremy L. Thompson }
6747a982d89SJeremy L. Thompson 
6757a982d89SJeremy L. Thompson /**
676ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
6777a982d89SJeremy L. Thompson 
678ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
679ea61e9acSJeremy L Thompson   @param[in]     data Data to set
6807a982d89SJeremy L. Thompson 
6817a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6827a982d89SJeremy L. Thompson 
6837a982d89SJeremy L. Thompson   @ref Backend
6847a982d89SJeremy L. Thompson **/
685777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
686777ff853SJeremy L Thompson   op->data = data;
687e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6887a982d89SJeremy L. Thompson }
6897a982d89SJeremy L. Thompson 
6907a982d89SJeremy L. Thompson /**
691ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
69234359f16Sjeremylt 
693ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
69434359f16Sjeremylt 
69534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
69634359f16Sjeremylt 
69734359f16Sjeremylt   @ref Backend
69834359f16Sjeremylt **/
6999560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
70034359f16Sjeremylt   op->ref_count++;
70134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
70234359f16Sjeremylt }
70334359f16Sjeremylt 
70434359f16Sjeremylt /**
705ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
7067a982d89SJeremy L. Thompson 
707ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
7087a982d89SJeremy L. Thompson 
7097a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
7107a982d89SJeremy L. Thompson 
7117a982d89SJeremy L. Thompson   @ref Backend
7127a982d89SJeremy L. Thompson **/
7137a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
714f04ea552SJeremy L Thompson   op->is_backend_setup = true;
715e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7167a982d89SJeremy L. Thompson }
7177a982d89SJeremy L. Thompson 
7187a982d89SJeremy L. Thompson /// @}
7197a982d89SJeremy L. Thompson 
7207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7217a982d89SJeremy L. Thompson /// CeedOperator Public API
7227a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7237a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
724dfdf5a53Sjeremylt /// @{
725d7b241e6Sjeremylt 
726d7b241e6Sjeremylt /**
727ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
7284385fb7fSSebastian Grimberg 
729ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
730d7b241e6Sjeremylt 
731ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
732ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
733ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
734ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
735ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
736b11c1e72Sjeremylt 
737b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
738dfdf5a53Sjeremylt 
7397a982d89SJeremy L. Thompson   @ref User
740d7b241e6Sjeremylt  */
7412b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
7425fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
7435fe0d4faSjeremylt     Ceed delegate;
7446574a04fSJeremy L Thompson 
7452b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7461ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate");
7472b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
7489bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
749e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7505fe0d4faSjeremylt   }
7515fe0d4faSjeremylt 
752ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
753db002c03SJeremy L Thompson 
7542b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
755db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
756d1d35e2fSjeremylt   (*op)->ref_count   = 1;
7572b104005SJeremy L Thompson   (*op)->input_size  = -1;
7582b104005SJeremy L Thompson   (*op)->output_size = -1;
759db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
760db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
761db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
7622b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
7632b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
7642b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
765e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
766d7b241e6Sjeremylt }
767d7b241e6Sjeremylt 
768d7b241e6Sjeremylt /**
769ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
77048acf710SJeremy L Thompson 
771ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
772ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
77348acf710SJeremy L Thompson 
774ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
775ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
776ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
777ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
77848acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
77948acf710SJeremy L Thompson 
78048acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
78148acf710SJeremy L Thompson 
78248acf710SJeremy L Thompson   @ref User
78348acf710SJeremy L Thompson  */
78448acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
78548acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
78648acf710SJeremy L Thompson     Ceed delegate;
78748acf710SJeremy L Thompson 
78848acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7891ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints");
79048acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
7919bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
79248acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
79348acf710SJeremy L Thompson   }
79448acf710SJeremy L Thompson 
795ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
79648acf710SJeremy L Thompson 
79748acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
79848acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
79948acf710SJeremy L Thompson   (*op)->ref_count    = 1;
80048acf710SJeremy L Thompson   (*op)->is_at_points = true;
80148acf710SJeremy L Thompson   (*op)->input_size   = -1;
80248acf710SJeremy L Thompson   (*op)->output_size  = -1;
80348acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
80448acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
80548acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
80648acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
80748acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
80848acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
80948acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
81048acf710SJeremy L Thompson }
81148acf710SJeremy L Thompson 
81248acf710SJeremy L Thompson /**
813ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
81452d6035fSJeremy L Thompson 
815ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
816ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
81752d6035fSJeremy L Thompson 
81852d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
81952d6035fSJeremy L Thompson 
8207a982d89SJeremy L. Thompson   @ref User
82152d6035fSJeremy L Thompson  */
82252d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
82352d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
82452d6035fSJeremy L Thompson     Ceed delegate;
82552d6035fSJeremy L Thompson 
8261c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
827250756a7Sjeremylt     if (delegate) {
8282b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
8299bc66399SJeremy L Thompson       CeedCall(CeedDestroy(&delegate));
830e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
83152d6035fSJeremy L Thompson     }
832250756a7Sjeremylt   }
83352d6035fSJeremy L Thompson 
8342b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
835db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
836996d9ab5SJed Brown   (*op)->ref_count    = 1;
837f04ea552SJeremy L Thompson   (*op)->is_composite = true;
8382b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
8392b104005SJeremy L Thompson   (*op)->input_size  = -1;
8402b104005SJeremy L Thompson   (*op)->output_size = -1;
841250756a7Sjeremylt 
842db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
843e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84452d6035fSJeremy L Thompson }
84552d6035fSJeremy L Thompson 
84652d6035fSJeremy L Thompson /**
847ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
8484385fb7fSSebastian Grimberg 
849ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
850512bb800SJeremy L Thompson 
851ca94c3ddSJeremy 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`.
852ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
8539560d06aSjeremylt 
854ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
855ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
8569560d06aSjeremylt 
8579560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8589560d06aSjeremylt 
8599560d06aSjeremylt   @ref User
8609560d06aSjeremylt **/
8619560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
8622b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
8632b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
8649560d06aSjeremylt   *op_copy = op;
8659560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8669560d06aSjeremylt }
8679560d06aSjeremylt 
8689560d06aSjeremylt /**
869ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
870d7b241e6Sjeremylt 
871ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
872bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
873ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
874d7b241e6Sjeremylt 
875ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
876ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
877d7b241e6Sjeremylt 
878528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
879bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
880528a22edSJeremy L Thompson 
881ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
882ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
883bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
884bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
885bafebce1SSebastian 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`
886b11c1e72Sjeremylt 
887b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
888dfdf5a53Sjeremylt 
8897a982d89SJeremy L. Thompson   @ref User
890b11c1e72Sjeremylt **/
891bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
8921203703bSJeremy L Thompson   bool               is_input = true, is_at_points, is_composite, is_immutable;
8931203703bSJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields;
8941203703bSJeremy L Thompson   CeedQFunction      qf;
8951203703bSJeremy L Thompson   CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields;
8961c66c397SJeremy L Thompson   CeedOperatorField *op_field;
8971c66c397SJeremy L Thompson 
8981203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
8991203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
9001203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
9019bc66399SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
9029bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
9039bc66399SJeremy L Thompson   CeedCheck(rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
9049bc66399SJeremy L Thompson   CeedCheck(basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
9059bc66399SJeremy L Thompson   CeedCheck(vec, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
90652d6035fSJeremy L Thompson 
907bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
9089bc66399SJeremy L Thompson   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
909ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
9102c7e7413SJeremy L Thompson   {
9112c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
9122c7e7413SJeremy L Thompson 
913bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
91448acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
9159bc66399SJeremy L Thompson       CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
9169bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints not supported for standard operator fields");
9179bc66399SJeremy L Thompson       CeedCheck(basis == CEED_BASIS_NONE, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED,
9189bc66399SJeremy L Thompson                 "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
91948acf710SJeremy L Thompson       if (!op->first_points_rstr) {
920bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
92148acf710SJeremy L Thompson       } else {
92248acf710SJeremy L Thompson         bool are_compatible;
92348acf710SJeremy L Thompson 
924bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
9259bc66399SJeremy L Thompson         CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
926ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
92748acf710SJeremy L Thompson       }
92848acf710SJeremy L Thompson     }
9292c7e7413SJeremy L Thompson   }
930d7b241e6Sjeremylt 
931bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
932bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
9339bc66399SJeremy L Thompson   CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION,
934ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
935528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
936bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
9371203703bSJeremy L Thompson 
9381203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
9391203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
940c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
9411203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9426f8994e9SJeremy L Thompson     const char *qf_field_name;
9431203703bSJeremy L Thompson 
9441203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_input_fields[i], &qf_field_name));
9451203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9461203703bSJeremy L Thompson       qf_field = qf_input_fields[i];
947d1d35e2fSjeremylt       op_field = &op->input_fields[i];
948d7b241e6Sjeremylt       goto found;
949d7b241e6Sjeremylt     }
950d7b241e6Sjeremylt   }
9512b104005SJeremy L Thompson   is_input = false;
9521203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9536f8994e9SJeremy L Thompson     const char *qf_field_name;
9541203703bSJeremy L Thompson 
9551203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_output_fields[i], &qf_field_name));
9561203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9571203703bSJeremy L Thompson       qf_field = qf_output_fields[i];
958d1d35e2fSjeremylt       op_field = &op->output_fields[i];
959d7b241e6Sjeremylt       goto found;
960d7b241e6Sjeremylt     }
961d7b241e6Sjeremylt   }
962c042f62fSJeremy L Thompson   // LCOV_EXCL_START
9639bc66399SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
964c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
965d7b241e6Sjeremylt found:
9669bc66399SJeremy L Thompson   CeedCall(CeedOperatorCheckField(CeedOperatorReturnCeed(op), qf_field, rstr, basis));
9672b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
968e15f9bd0SJeremy L Thompson 
969bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
9702b104005SJeremy L Thompson     CeedSize l_size;
9711c66c397SJeremy L Thompson 
972bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
9732b104005SJeremy L Thompson     if (is_input) {
9742b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
9759bc66399SJeremy L Thompson       CeedCheck(l_size == op->input_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
976249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
9772b104005SJeremy L Thompson     } else {
9782b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
9799bc66399SJeremy L Thompson       CeedCheck(l_size == op->output_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
980249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
9812b104005SJeremy L Thompson     }
9822b730f8bSJeremy L Thompson   }
9832b104005SJeremy L Thompson 
984bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
985bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
986bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
987d1d35e2fSjeremylt     op->num_elem        = num_elem;
988d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
989e15f9bd0SJeremy L Thompson   }
990bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
9912a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
992d1d35e2fSjeremylt   op->num_fields += 1;
9932b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
994e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
995d7b241e6Sjeremylt }
996d7b241e6Sjeremylt 
997d7b241e6Sjeremylt /**
998ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
99943bbe138SJeremy L Thompson 
1000ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1001f04ea552SJeremy L Thompson 
1002ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
1003f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
1004ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
1005f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
1006ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
100743bbe138SJeremy L Thompson 
100843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
100943bbe138SJeremy L Thompson 
1010e9b533fbSJeremy L Thompson   @ref Advanced
101143bbe138SJeremy L Thompson **/
10122b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
101343bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
10141203703bSJeremy L Thompson   bool          is_composite;
10151203703bSJeremy L Thompson   CeedQFunction qf;
10161203703bSJeremy L Thompson 
10171203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10186e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
10192b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
102043bbe138SJeremy L Thompson 
10211203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10221203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL));
1023c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
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));
10479bc66399SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
10489bc66399SJeremy 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));
10569bc66399SJeremy 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 
1100*3f919cbcSJeremy L Thompson   if (rstr_points) {
1101*3f919cbcSJeremy L Thompson     *rstr_points = NULL;
1102*3f919cbcSJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
1103*3f919cbcSJeremy L Thompson   }
1104*3f919cbcSJeremy L Thompson   if (point_coords) {
1105*3f919cbcSJeremy L Thompson     *point_coords = NULL;
1106*3f919cbcSJeremy L Thompson     CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
1107*3f919cbcSJeremy L Thompson   }
110848acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
110948acf710SJeremy L Thompson }
111048acf710SJeremy L Thompson 
111148acf710SJeremy L Thompson /**
1112be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
1113be9c6463SJeremy L Thompson 
1114be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
1115de5900adSJames Wright 
1116ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1117de5900adSJames Wright 
1118ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
1119ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
1120ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
1121de5900adSJames Wright 
1122de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
1123de5900adSJames Wright 
1124de5900adSJames Wright   @ref Advanced
1125de5900adSJames Wright **/
1126de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
11276f8994e9SJeremy L Thompson   const char        *name;
1128de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
1129de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
1130de5900adSJames Wright 
1131be9c6463SJeremy L Thompson   *op_field = NULL;
11321c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1133de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1134de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
1135de5900adSJames Wright     if (!strcmp(name, field_name)) {
1136de5900adSJames Wright       *op_field = input_fields[i];
1137de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1138de5900adSJames Wright     }
1139de5900adSJames Wright   }
1140de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1141de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
1142de5900adSJames Wright     if (!strcmp(name, field_name)) {
1143de5900adSJames Wright       *op_field = output_fields[i];
1144de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1145de5900adSJames Wright     }
1146de5900adSJames Wright   }
1147be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1148de5900adSJames Wright }
1149de5900adSJames Wright 
1150de5900adSJames Wright /**
1151ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
115228567f8fSJeremy L Thompson 
1153ca94c3ddSJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
115428567f8fSJeremy L Thompson   @param[out] field_name Variable to store the field name
115528567f8fSJeremy L Thompson 
115628567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
115728567f8fSJeremy L Thompson 
1158e9b533fbSJeremy L Thompson   @ref Advanced
115928567f8fSJeremy L Thompson **/
11606f8994e9SJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, const char **field_name) {
11616f8994e9SJeremy L Thompson   *field_name = op_field->field_name;
116228567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
116328567f8fSJeremy L Thompson }
116428567f8fSJeremy L Thompson 
116528567f8fSJeremy L Thompson /**
1166681d0ea7SJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field.
1167681d0ea7SJeremy L Thompson 
1168681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr` with @ref CeedElemRestrictionDestroy().
116943bbe138SJeremy L Thompson 
1170ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1171ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
117243bbe138SJeremy L Thompson 
117343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
117443bbe138SJeremy L Thompson 
1175e9b533fbSJeremy L Thompson   @ref Advanced
117643bbe138SJeremy L Thompson **/
11772b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1178681d0ea7SJeremy L Thompson   *rstr = NULL;
1179681d0ea7SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(op_field->elem_rstr, rstr));
118043bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
118143bbe138SJeremy L Thompson }
118243bbe138SJeremy L Thompson 
118343bbe138SJeremy L Thompson /**
1184681d0ea7SJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field.
1185681d0ea7SJeremy L Thompson 
1186681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `basis` with @ref CeedBasisDestroy().
118743bbe138SJeremy L Thompson 
1188ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1189ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
119043bbe138SJeremy L Thompson 
119143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
119243bbe138SJeremy L Thompson 
1193e9b533fbSJeremy L Thompson   @ref Advanced
119443bbe138SJeremy L Thompson **/
119543bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
1196681d0ea7SJeremy L Thompson   *basis = NULL;
1197681d0ea7SJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(op_field->basis, basis));
119843bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
119943bbe138SJeremy L Thompson }
120043bbe138SJeremy L Thompson 
120143bbe138SJeremy L Thompson /**
1202681d0ea7SJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field.
1203681d0ea7SJeremy L Thompson 
1204681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `vec` with @ref CeedVectorDestroy().
120543bbe138SJeremy L Thompson 
1206ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1207ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
120843bbe138SJeremy L Thompson 
120943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
121043bbe138SJeremy L Thompson 
1211e9b533fbSJeremy L Thompson   @ref Advanced
121243bbe138SJeremy L Thompson **/
121343bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
1214681d0ea7SJeremy L Thompson   *vec = NULL;
1215681d0ea7SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(op_field->vec, vec));
121643bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
121743bbe138SJeremy L Thompson }
121843bbe138SJeremy L Thompson 
121943bbe138SJeremy L Thompson /**
1220ab747706SJeremy L Thompson   @brief Get the data of a `CeedOperator` Field.
1221ab747706SJeremy L Thompson 
1222681d0ea7SJeremy L Thompson   Any arguments set as `NULL` are ignored..
1223681d0ea7SJeremy L Thompson 
1224681d0ea7SJeremy L Thompson   Note: Caller is responsible for destroying the `rstr`, `basis`, and `vec`.
1225ab747706SJeremy L Thompson 
1226ab747706SJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
1227ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
1228ab747706SJeremy L Thompson   @param[out] rstr       Variable to store `CeedElemRestriction`
1229ab747706SJeremy L Thompson   @param[out] basis      Variable to store `CeedBasis`
1230ab747706SJeremy L Thompson   @param[out] vec        Variable to store `CeedVector`
1231ab747706SJeremy L Thompson 
1232ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1233ab747706SJeremy L Thompson 
1234ab747706SJeremy L Thompson   @ref Advanced
1235ab747706SJeremy L Thompson **/
12366f8994e9SJeremy L Thompson int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name, CeedElemRestriction *rstr, CeedBasis *basis, CeedVector *vec) {
1237ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedOperatorFieldGetName(op_field, field_name));
1238ab747706SJeremy L Thompson   if (rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_field, rstr));
1239ab747706SJeremy L Thompson   if (basis) CeedCall(CeedOperatorFieldGetBasis(op_field, basis));
1240ab747706SJeremy L Thompson   if (vec) CeedCall(CeedOperatorFieldGetVector(op_field, vec));
1241ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1242ab747706SJeremy L Thompson }
1243ab747706SJeremy L Thompson 
1244ab747706SJeremy L Thompson /**
1245ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1246288c0443SJeremy L Thompson 
1247ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1248ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
124952d6035fSJeremy L Thompson 
125052d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
125152d6035fSJeremy L Thompson 
12527a982d89SJeremy L. Thompson   @ref User
125352d6035fSJeremy L Thompson  */
12542b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
12551203703bSJeremy L Thompson   bool is_immutable;
12561203703bSJeremy L Thompson 
12579bc66399SJeremy L Thompson   CeedCheck(composite_op->is_composite, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
12589bc66399SJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, CeedOperatorReturnCeed(composite_op), CEED_ERROR_UNSUPPORTED,
12599bc66399SJeremy L Thompson             "Cannot add additional sub-operators");
12601203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable));
12619bc66399SJeremy L Thompson   CeedCheck(!is_immutable, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
12622b730f8bSJeremy L Thompson 
12632b730f8bSJeremy L Thompson   {
12642b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
12651c66c397SJeremy L Thompson 
12662b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
12672b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
12682b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
12692b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
12709bc66399SJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
12719bc66399SJeremy L Thompson               CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR,
1272249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1273249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1274249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12752b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
12762b730f8bSJeremy L Thompson   }
12772b730f8bSJeremy L Thompson 
1278d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
12792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1280d1d35e2fSjeremylt   composite_op->num_suboperators++;
1281e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
128252d6035fSJeremy L Thompson }
128352d6035fSJeremy L Thompson 
128452d6035fSJeremy L Thompson /**
1285ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
128675f0d5a4SJeremy L Thompson 
1287ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1288ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
128975f0d5a4SJeremy L Thompson 
129075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
129175f0d5a4SJeremy L Thompson 
129275f0d5a4SJeremy L Thompson   @ref Backend
129375f0d5a4SJeremy L Thompson **/
1294c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
12951203703bSJeremy L Thompson   bool is_composite;
12961203703bSJeremy L Thompson 
12971203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12986e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
129975f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
130075f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
130175f0d5a4SJeremy L Thompson }
130275f0d5a4SJeremy L Thompson 
130375f0d5a4SJeremy L Thompson /**
1304ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
130575f0d5a4SJeremy L Thompson 
1306ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1307ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
130875f0d5a4SJeremy L Thompson 
130975f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
131075f0d5a4SJeremy L Thompson 
131175f0d5a4SJeremy L Thompson   @ref Backend
131275f0d5a4SJeremy L Thompson **/
1313c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
13141203703bSJeremy L Thompson   bool is_composite;
13151203703bSJeremy L Thompson 
13161203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13176e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
131875f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
131975f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
132075f0d5a4SJeremy L Thompson }
132175f0d5a4SJeremy L Thompson 
132275f0d5a4SJeremy L Thompson /**
13238a297abdSJames Wright   @brief Get a sub `CeedOperator` of a composite `CeedOperator` from its name.
13248a297abdSJames Wright 
13258a297abdSJames Wright   `sub_op` is set to `NULL` if the sub operator is not found.
13268a297abdSJames Wright 
13278a297abdSJames Wright   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
13288a297abdSJames Wright 
13298a297abdSJames Wright   @param[in]  op      Composite `CeedOperator`
13308a297abdSJames Wright   @param[in]  op_name Name of desired sub `CeedOperator`
13318a297abdSJames Wright   @param[out] sub_op  Sub `CeedOperator` corresponding to the name
13328a297abdSJames Wright 
13338a297abdSJames Wright   @return An error code: 0 - success, otherwise - failure
13348a297abdSJames Wright 
13358a297abdSJames Wright   @ref Advanced
13368a297abdSJames Wright **/
13378a297abdSJames Wright int CeedCompositeOperatorGetSubByName(CeedOperator op, const char *op_name, CeedOperator *sub_op) {
13388a297abdSJames Wright   bool          is_composite;
13398a297abdSJames Wright   CeedInt       num_sub_ops;
13408a297abdSJames Wright   CeedOperator *sub_ops;
13418a297abdSJames Wright 
13428a297abdSJames Wright   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13438a297abdSJames Wright   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
13448a297abdSJames Wright   *sub_op = NULL;
13458a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_ops));
13468a297abdSJames Wright   CeedCall(CeedCompositeOperatorGetSubList(op, &sub_ops));
13478a297abdSJames Wright   for (CeedInt i = 0; i < num_sub_ops; i++) {
13488a297abdSJames Wright     if (sub_ops[i]->name && !strcmp(op_name, sub_ops[i]->name)) {
13498a297abdSJames Wright       *sub_op = sub_ops[i];
13508a297abdSJames Wright       return CEED_ERROR_SUCCESS;
13518a297abdSJames Wright     }
13528a297abdSJames Wright   }
13538a297abdSJames Wright   return CEED_ERROR_SUCCESS;
13548a297abdSJames Wright }
13558a297abdSJames Wright 
13568a297abdSJames Wright /**
1357ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
13584db537f9SJeremy L Thompson 
1359ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
13604db537f9SJeremy L Thompson 
13614db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13624db537f9SJeremy L Thompson 
13634db537f9SJeremy L Thompson   @ref User
13644db537f9SJeremy L Thompson **/
13654db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
13661203703bSJeremy L Thompson   bool          is_at_points, is_composite;
13671203703bSJeremy L Thompson   CeedQFunction qf = NULL;
13684db537f9SJeremy L Thompson 
13692b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
13704db537f9SJeremy L Thompson 
13711203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
13721203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13731203703bSJeremy L Thompson   if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf));
13741203703bSJeremy L Thompson   if (is_composite) {
13751203703bSJeremy L Thompson     CeedInt num_suboperators;
13761203703bSJeremy L Thompson 
13771203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13781203703bSJeremy L Thompson     if (!num_suboperators) {
137943622462SJeremy L Thompson       // Empty operator setup
138043622462SJeremy L Thompson       op->input_size  = 0;
138143622462SJeremy L Thompson       op->output_size = 0;
138243622462SJeremy L Thompson     } else {
13831203703bSJeremy L Thompson       CeedOperator *sub_operators;
13841203703bSJeremy L Thompson 
13851203703bSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13861203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_suboperators; i++) {
13871203703bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(sub_operators[i]));
13884db537f9SJeremy L Thompson       }
13892b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
13902b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
13912b104005SJeremy L Thompson       CeedSize input_size, output_size;
13922b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
139343622462SJeremy L Thompson     }
13944db537f9SJeremy L Thompson   } else {
13951203703bSJeremy L Thompson     CeedInt num_input_fields, num_output_fields;
13961203703bSJeremy L Thompson 
13979bc66399SJeremy L Thompson     CeedCheck(op->num_fields > 0, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No operator fields set");
13981203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL));
13999bc66399SJeremy L Thompson     CeedCheck(op->num_fields == num_input_fields + num_output_fields, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
14009bc66399SJeremy L Thompson               "Not all operator fields set");
14019bc66399SJeremy L Thompson     CeedCheck(op->has_restriction, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "At least one restriction required");
14029bc66399SJeremy L Thompson     CeedCheck(op->num_qpts > 0 || is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
1403ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
14042b730f8bSJeremy L Thompson   }
14054db537f9SJeremy L Thompson 
14064db537f9SJeremy L Thompson   // Flag as immutable and ready
14074db537f9SJeremy L Thompson   op->is_interface_setup = true;
14081203703bSJeremy L Thompson   if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf));
1409c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
14101203703bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf));
14111203703bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT));
14124db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14134db537f9SJeremy L Thompson }
14144db537f9SJeremy L Thompson 
14154db537f9SJeremy L Thompson /**
1416ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
14174385fb7fSSebastian Grimberg 
1418ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1419c9366a6bSJeremy L Thompson 
1420ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1421ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1422ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1423c9366a6bSJeremy L Thompson 
1424c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1425c9366a6bSJeremy L Thompson 
1426c9366a6bSJeremy L Thompson   @ref User
1427c9366a6bSJeremy L Thompson **/
14282b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1429c9366a6bSJeremy L Thompson   bool is_composite;
1430c9366a6bSJeremy L Thompson 
14312b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
14322b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1433c9366a6bSJeremy L Thompson 
14342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14352b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
14361203703bSJeremy L Thompson     CeedInt       num_suboperators;
14371203703bSJeremy L Thompson     CeedOperator *sub_operators;
14381203703bSJeremy L Thompson 
14391203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14401203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14411203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
1442c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
14431c66c397SJeremy L Thompson 
14441203703bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(sub_operators[i], &sub_input_size, &sub_output_size));
14452b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
14462b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
14472b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
14486e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
14496e536b99SJeremy L Thompson                 CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR,
1450249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1451249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1452249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
14532b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1454c9366a6bSJeremy L Thompson     }
14552b730f8bSJeremy L Thompson   }
1456c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1457c9366a6bSJeremy L Thompson }
1458c9366a6bSJeremy L Thompson 
1459c9366a6bSJeremy L Thompson /**
1460ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
14614385fb7fSSebastian Grimberg 
1462ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1463ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
14648b919e6bSJeremy L Thompson 
1465ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1466beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
14678b919e6bSJeremy L Thompson 
14688b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14698b919e6bSJeremy L Thompson 
14708b919e6bSJeremy L Thompson   @ref Advanced
14718b919e6bSJeremy L Thompson **/
14722b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
14738b919e6bSJeremy L Thompson   bool is_composite;
14748b919e6bSJeremy L Thompson 
14752b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14768b919e6bSJeremy L Thompson   if (is_composite) {
14778b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
14782b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
14798b919e6bSJeremy L Thompson     }
14808b919e6bSJeremy L Thompson   } else {
14817d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
14827d5185d7SSebastian Grimberg 
14837d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
14847d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetReuse(data, reuse_assembly_data));
1485beecbf24SJeremy L Thompson   }
1486beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1487beecbf24SJeremy L Thompson }
1488beecbf24SJeremy L Thompson 
1489beecbf24SJeremy L Thompson /**
1490ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1491beecbf24SJeremy L Thompson 
1492ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
14936e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1494beecbf24SJeremy L Thompson 
1495beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1496beecbf24SJeremy L Thompson 
1497beecbf24SJeremy L Thompson   @ref Advanced
1498beecbf24SJeremy L Thompson **/
14992b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1500beecbf24SJeremy L Thompson   bool is_composite;
1501beecbf24SJeremy L Thompson 
15022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1503beecbf24SJeremy L Thompson   if (is_composite) {
15041203703bSJeremy L Thompson     CeedInt       num_suboperators;
15051203703bSJeremy L Thompson     CeedOperator *sub_operators;
15061203703bSJeremy L Thompson 
15071203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15081203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15091203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15101203703bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(sub_operators[i], needs_data_update));
1511beecbf24SJeremy L Thompson     }
1512beecbf24SJeremy L Thompson   } else {
15137d5185d7SSebastian Grimberg     CeedQFunctionAssemblyData data;
15147d5185d7SSebastian Grimberg 
15157d5185d7SSebastian Grimberg     CeedCall(CeedOperatorGetQFunctionAssemblyData(op, &data));
15167d5185d7SSebastian Grimberg     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(data, needs_data_update));
15178b919e6bSJeremy L Thompson   }
15188b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15198b919e6bSJeremy L Thompson }
15208b919e6bSJeremy L Thompson 
15218b919e6bSJeremy L Thompson /**
1522ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1523ea6b5821SJeremy L Thompson 
1524ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1525ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1526ea6b5821SJeremy L Thompson 
1527ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1528ea6b5821SJeremy L Thompson 
1529ea6b5821SJeremy L Thompson   @ref User
1530ea6b5821SJeremy L Thompson **/
1531ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1532ea6b5821SJeremy L Thompson   char  *name_copy;
1533ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1534ea6b5821SJeremy L Thompson 
15352b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1536ea6b5821SJeremy L Thompson   if (name_len > 0) {
15372b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1538ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1539ea6b5821SJeremy L Thompson     op->name = name_copy;
1540ea6b5821SJeremy L Thompson   }
1541ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1542ea6b5821SJeremy L Thompson }
1543ea6b5821SJeremy L Thompson 
1544ea6b5821SJeremy L Thompson /**
1545935f026aSJeremy L Thompson   @brief Core logic for viewing a `CeedOperator`
15467a982d89SJeremy L. Thompson 
1547935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1548ca94c3ddSJeremy L Thompson   @param[in] stream  Stream to write; typically `stdout` or a file
15498bf1b130SJames Wright   @param[in] is_full Whether to write full operator view or terse
15507a982d89SJeremy L. Thompson 
15517a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
15527a982d89SJeremy L. Thompson **/
15538bf1b130SJames Wright static int CeedOperatorView_Core(CeedOperator op, FILE *stream, bool is_full) {
15541203703bSJeremy L Thompson   bool has_name = op->name, is_composite;
15557a982d89SJeremy L. Thompson 
15561203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15571203703bSJeremy L Thompson   if (is_composite) {
15581203703bSJeremy L Thompson     CeedInt       num_suboperators;
15591203703bSJeremy L Thompson     CeedOperator *sub_operators;
15601203703bSJeremy L Thompson 
15611203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15621203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15632b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
15647a982d89SJeremy L. Thompson 
15651203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15661203703bSJeremy L Thompson       has_name = sub_operators[i]->name;
1567935f026aSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s%s\n", i, has_name ? " - " : "", has_name ? sub_operators[i]->name : "", is_full ? ":" : "");
1568935f026aSJeremy L Thompson       if (is_full) CeedCall(CeedOperatorSingleView(sub_operators[i], 1, stream));
15697a982d89SJeremy L. Thompson     }
15707a982d89SJeremy L. Thompson   } else {
15712b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1572935f026aSJeremy L Thompson     if (is_full) CeedCall(CeedOperatorSingleView(op, 0, stream));
15737a982d89SJeremy L. Thompson   }
1574e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15757a982d89SJeremy L. Thompson }
15763bd813ffSjeremylt 
15773bd813ffSjeremylt /**
1578935f026aSJeremy L Thompson   @brief View a `CeedOperator`
1579935f026aSJeremy L Thompson 
1580935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1581935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1582935f026aSJeremy L Thompson 
1583935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1584935f026aSJeremy L Thompson 
1585935f026aSJeremy L Thompson   @ref User
1586935f026aSJeremy L Thompson **/
1587935f026aSJeremy L Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1588935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, true));
1589935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1590935f026aSJeremy L Thompson }
1591935f026aSJeremy L Thompson 
1592935f026aSJeremy L Thompson /**
1593935f026aSJeremy L Thompson   @brief View a brief summary `CeedOperator`
1594935f026aSJeremy L Thompson 
1595935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1596935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1597935f026aSJeremy L Thompson 
1598935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1599935f026aSJeremy L Thompson 
1600935f026aSJeremy L Thompson   @ref User
1601935f026aSJeremy L Thompson **/
1602935f026aSJeremy L Thompson int CeedOperatorViewTerse(CeedOperator op, FILE *stream) {
1603935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, false));
1604935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1605935f026aSJeremy L Thompson }
1606935f026aSJeremy L Thompson 
1607935f026aSJeremy L Thompson /**
1608ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1609b7c9bbdaSJeremy L Thompson 
1610ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1611ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1612b7c9bbdaSJeremy L Thompson 
1613b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1614b7c9bbdaSJeremy L Thompson 
1615b7c9bbdaSJeremy L Thompson   @ref Advanced
1616b7c9bbdaSJeremy L Thompson **/
1617b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
16189bc66399SJeremy L Thompson   *ceed = NULL;
16199bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), ceed));
1620b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1621b7c9bbdaSJeremy L Thompson }
1622b7c9bbdaSJeremy L Thompson 
1623b7c9bbdaSJeremy L Thompson /**
16246e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
16256e536b99SJeremy L Thompson 
16266e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
16276e536b99SJeremy L Thompson 
16286e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
16296e536b99SJeremy L Thompson 
16306e536b99SJeremy L Thompson   @ref Advanced
16316e536b99SJeremy L Thompson **/
16326e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
16336e536b99SJeremy L Thompson 
16346e536b99SJeremy L Thompson /**
1635ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1636b7c9bbdaSJeremy L Thompson 
1637ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1638b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1639b7c9bbdaSJeremy L Thompson 
1640b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1641b7c9bbdaSJeremy L Thompson 
1642b7c9bbdaSJeremy L Thompson   @ref Advanced
1643b7c9bbdaSJeremy L Thompson **/
1644b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
16451203703bSJeremy L Thompson   bool is_composite;
16461203703bSJeremy L Thompson 
16471203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16486e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1649b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1650b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1651b7c9bbdaSJeremy L Thompson }
1652b7c9bbdaSJeremy L Thompson 
1653b7c9bbdaSJeremy L Thompson /**
1654ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1655b7c9bbdaSJeremy L Thompson 
1656ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1657b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1658b7c9bbdaSJeremy L Thompson 
1659b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1660b7c9bbdaSJeremy L Thompson 
1661b7c9bbdaSJeremy L Thompson   @ref Advanced
1662b7c9bbdaSJeremy L Thompson **/
1663b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
16641203703bSJeremy L Thompson   bool is_composite;
16651203703bSJeremy L Thompson 
16661203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16676e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1668b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1669b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1670b7c9bbdaSJeremy L Thompson }
1671b7c9bbdaSJeremy L Thompson 
1672b7c9bbdaSJeremy L Thompson /**
1673ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
16746e15d496SJeremy L Thompson 
1675ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1676ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
16776e15d496SJeremy L Thompson 
16786e15d496SJeremy L Thompson   @ref Backend
16796e15d496SJeremy L Thompson **/
16809d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
16816e15d496SJeremy L Thompson   bool is_composite;
16821c66c397SJeremy L Thompson 
16832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
16846e15d496SJeremy L Thompson 
16856e15d496SJeremy L Thompson   *flops = 0;
16862b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16876e15d496SJeremy L Thompson   if (is_composite) {
16886e15d496SJeremy L Thompson     CeedInt num_suboperators;
16891c66c397SJeremy L Thompson 
1690c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
16916e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1692c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
16936e15d496SJeremy L Thompson 
16946e15d496SJeremy L Thompson     // FLOPs for each suboperator
16956e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
16969d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
16971c66c397SJeremy L Thompson 
16982b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
16996e15d496SJeremy L Thompson       *flops += suboperator_flops;
17006e15d496SJeremy L Thompson     }
17016e15d496SJeremy L Thompson   } else {
1702*3f919cbcSJeremy L Thompson     bool                is_at_points;
1703*3f919cbcSJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0, num_points = 0;
17041203703bSJeremy L Thompson     CeedQFunction       qf;
17051203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
17061203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
17071c66c397SJeremy L Thompson 
1708*3f919cbcSJeremy L Thompson     CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
1709*3f919cbcSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1710*3f919cbcSJeremy L Thompson     if (is_at_points) {
1711*3f919cbcSJeremy L Thompson       CeedMemType         mem_type;
1712*3f919cbcSJeremy L Thompson       CeedElemRestriction rstr_points = NULL;
1713*3f919cbcSJeremy L Thompson 
1714*3f919cbcSJeremy L Thompson       CeedCall(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
1715*3f919cbcSJeremy L Thompson       CeedCall(CeedGetPreferredMemType(CeedOperatorReturnCeed(op), &mem_type));
1716*3f919cbcSJeremy L Thompson       if (mem_type == CEED_MEM_DEVICE) {
1717*3f919cbcSJeremy L Thompson         // Device backends pad out to the same number of points per element
1718*3f919cbcSJeremy L Thompson         CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &num_points));
1719*3f919cbcSJeremy L Thompson       } else {
1720*3f919cbcSJeremy L Thompson         num_points = 0;
1721*3f919cbcSJeremy L Thompson         for (CeedInt i = 0; i < num_elem; i++) {
1722*3f919cbcSJeremy L Thompson           CeedInt points_in_elem = 0;
1723*3f919cbcSJeremy L Thompson 
1724*3f919cbcSJeremy L Thompson           CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr_points, i, &points_in_elem));
1725*3f919cbcSJeremy L Thompson           num_points += points_in_elem;
1726*3f919cbcSJeremy L Thompson         }
1727*3f919cbcSJeremy L Thompson         num_points = num_points / num_elem + (num_points % num_elem > 0);
1728*3f919cbcSJeremy L Thompson       }
1729*3f919cbcSJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&rstr_points));
1730*3f919cbcSJeremy L Thompson     }
17311203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
17321203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
1733c11e12f4SJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf));
17341203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
17354385fb7fSSebastian Grimberg 
17366e15d496SJeremy L Thompson     // Input FLOPs
17376e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
17381203703bSJeremy L Thompson       CeedVector vec;
17396e15d496SJeremy L Thompson 
17401203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
17411203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17421203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17431203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17441203703bSJeremy L Thompson         CeedElemRestriction rstr;
17451203703bSJeremy L Thompson         CeedBasis           basis;
17461203703bSJeremy L Thompson 
17471203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
17481203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1749681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1750edb2538eSJeremy L Thompson         *flops += rstr_flops;
17511203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
17521203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1753*3f919cbcSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
1754681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
17556e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17566e15d496SJeremy L Thompson       }
1757681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
17586e15d496SJeremy L Thompson     }
17596e15d496SJeremy L Thompson     // QF FLOPs
17601c66c397SJeremy L Thompson     {
17619d36ca50SJeremy L Thompson       CeedInt       num_qpts;
17629d36ca50SJeremy L Thompson       CeedSize      qf_flops;
17631203703bSJeremy L Thompson       CeedQFunction qf;
17641c66c397SJeremy L Thompson 
1765*3f919cbcSJeremy L Thompson       if (is_at_points) num_qpts = num_points;
1766*3f919cbcSJeremy L Thompson       else CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
17671203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
17681203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
1769c11e12f4SJeremy L Thompson       CeedCall(CeedQFunctionDestroy(&qf));
17706e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
17716e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
17726e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
17731c66c397SJeremy L Thompson     }
17741c66c397SJeremy L Thompson 
17756e15d496SJeremy L Thompson     // Output FLOPs
17766e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
17771203703bSJeremy L Thompson       CeedVector vec;
17786e15d496SJeremy L Thompson 
17791203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
17801203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
17811203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
17821203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
17831203703bSJeremy L Thompson         CeedElemRestriction rstr;
17841203703bSJeremy L Thompson         CeedBasis           basis;
17851203703bSJeremy L Thompson 
17861203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
17871203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1788681d0ea7SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&rstr));
1789edb2538eSJeremy L Thompson         *flops += rstr_flops;
17901203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
17911203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1792*3f919cbcSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, is_at_points, num_points, &basis_flops));
1793681d0ea7SJeremy L Thompson         CeedCall(CeedBasisDestroy(&basis));
17946e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
17956e15d496SJeremy L Thompson       }
1796681d0ea7SJeremy L Thompson       CeedCall(CeedVectorDestroy(&vec));
17976e15d496SJeremy L Thompson     }
17986e15d496SJeremy L Thompson   }
17996e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18006e15d496SJeremy L Thompson }
18016e15d496SJeremy L Thompson 
18026e15d496SJeremy L Thompson /**
1803ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
18049fd66db6SSebastian Grimberg 
1805ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1806512bb800SJeremy L Thompson 
1807ca94c3ddSJeremy 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`.
1808ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
18090126412dSJeremy L Thompson 
1810ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1811ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
18120126412dSJeremy L Thompson 
18130126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18140126412dSJeremy L Thompson 
1815859c15bbSJames Wright   @ref Advanced
18160126412dSJeremy L Thompson **/
18170126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
18181203703bSJeremy L Thompson   bool                 is_composite;
18191203703bSJeremy L Thompson   CeedQFunction        qf;
18201203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
18211203703bSJeremy L Thompson 
18221203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
18236e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
18241203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
18251203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
1826c11e12f4SJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&qf));
18271485364cSJeremy L Thompson   *ctx = NULL;
18281203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
18290126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
18300126412dSJeremy L Thompson }
18310126412dSJeremy L Thompson 
18320126412dSJeremy L Thompson /**
1833ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
18343668ca4bSJeremy L Thompson 
1835ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1836859c15bbSJames Wright 
1837ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18383668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
18393668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
18403668ca4bSJeremy L Thompson 
18413668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18423668ca4bSJeremy L Thompson 
18433668ca4bSJeremy L Thompson   @ref User
18443668ca4bSJeremy L Thompson **/
184517b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
18461c66c397SJeremy L Thompson   bool is_composite, field_found = false;
18471c66c397SJeremy L Thompson 
18482b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
18492b730f8bSJeremy L Thompson 
18503668ca4bSJeremy L Thompson   if (is_composite) {
1851a98a090bSJeremy L Thompson     // Composite operator
1852a98a090bSJeremy L Thompson     // -- Check if composite label already created
18533668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
18543668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
18553668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
18563668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
18573668ca4bSJeremy L Thompson       }
18583668ca4bSJeremy L Thompson     }
18593668ca4bSJeremy L Thompson 
1860a98a090bSJeremy L Thompson     // -- Create composite label if needed
18613668ca4bSJeremy L Thompson     CeedInt               num_sub;
18623668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
18633668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
18643668ca4bSJeremy L Thompson 
18652b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1866c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1867c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
18682b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
18693668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
18703668ca4bSJeremy L Thompson 
18713668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
18723668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
18733668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
18741c66c397SJeremy L Thompson 
18752b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
18763668ca4bSJeremy L Thompson         if (new_field_label_i) {
1877a98a090bSJeremy L Thompson           field_found                    = true;
18783668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
18793668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
18803668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
18812b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
18827bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18832b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18846e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
18852b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
18867bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18877bfe0f0eSJeremy L Thompson           } else {
18887bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
18897bfe0f0eSJeremy L Thompson           }
18902b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
18917bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
18922b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
18936e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
18946e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
18956e536b99SJeremy L Thompson                              new_field_label_i->num_values);
18967bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
18977bfe0f0eSJeremy L Thompson           } else {
18987bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
18997bfe0f0eSJeremy L Thompson           }
19003668ca4bSJeremy L Thompson         }
19013668ca4bSJeremy L Thompson       }
19023668ca4bSJeremy L Thompson     }
1903a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1904a98a090bSJeremy L Thompson     if (field_found) {
1905a98a090bSJeremy L Thompson       *field_label = new_field_label;
1906a98a090bSJeremy L Thompson     } else {
19073668ca4bSJeremy L Thompson       // LCOV_EXCL_START
19082b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
19092b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
19103668ca4bSJeremy L Thompson       *field_label = NULL;
19113668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
19125ac9af79SJeremy L Thompson     }
19135ac9af79SJeremy L Thompson   } else {
19141203703bSJeremy L Thompson     CeedQFunction        qf;
19151203703bSJeremy L Thompson     CeedQFunctionContext ctx;
19161203703bSJeremy L Thompson 
1917a98a090bSJeremy L Thompson     // Single, non-composite operator
19181203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
19191203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
1920c11e12f4SJeremy L Thompson     CeedCall(CeedQFunctionDestroy(&qf));
19211203703bSJeremy L Thompson     if (ctx) {
19221203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1923a98a090bSJeremy L Thompson     } else {
1924a98a090bSJeremy L Thompson       *field_label = NULL;
1925a98a090bSJeremy L Thompson     }
19265ac9af79SJeremy L Thompson   }
19275ac9af79SJeremy L Thompson 
19285ac9af79SJeremy L Thompson   // Set label in operator
19295ac9af79SJeremy L Thompson   if (*field_label) {
19305ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
19315ac9af79SJeremy L Thompson 
19323668ca4bSJeremy L Thompson     // Move new composite label to operator
19333668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
19342b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
19353668ca4bSJeremy L Thompson       op->max_context_labels = 1;
19363668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
19372b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
19383668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
19393668ca4bSJeremy L Thompson     }
19405ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
19413668ca4bSJeremy L Thompson     op->num_context_labels++;
19423668ca4bSJeremy L Thompson   }
19433668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
19443668ca4bSJeremy L Thompson }
19453668ca4bSJeremy L Thompson 
19463668ca4bSJeremy L Thompson /**
1947ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
19484385fb7fSSebastian Grimberg 
1949ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1950d8dd9a91SJeremy L Thompson 
1951ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19522788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1953ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1954d8dd9a91SJeremy L Thompson 
1955d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1956d8dd9a91SJeremy L Thompson 
1957d8dd9a91SJeremy L Thompson   @ref User
1958d8dd9a91SJeremy L Thompson **/
195917b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
19602b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1961d8dd9a91SJeremy L Thompson }
1962d8dd9a91SJeremy L Thompson 
1963d8dd9a91SJeremy L Thompson /**
1964ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
19654385fb7fSSebastian Grimberg 
1966ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19672788fa27SJeremy L Thompson 
1968ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19692788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
19702788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
19712788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19722788fa27SJeremy L Thompson 
19732788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19742788fa27SJeremy L Thompson 
19752788fa27SJeremy L Thompson   @ref User
19762788fa27SJeremy L Thompson **/
197717b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
19782788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
19792788fa27SJeremy L Thompson }
19802788fa27SJeremy L Thompson 
19812788fa27SJeremy L Thompson /**
1982ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
19832788fa27SJeremy L Thompson 
1984ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19852788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
19862788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19872788fa27SJeremy L Thompson 
19882788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19892788fa27SJeremy L Thompson 
19902788fa27SJeremy L Thompson   @ref User
19912788fa27SJeremy L Thompson **/
199217b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
19932788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
19942788fa27SJeremy L Thompson }
19952788fa27SJeremy L Thompson 
19962788fa27SJeremy L Thompson /**
1997ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
19984385fb7fSSebastian Grimberg 
1999ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
2000d8dd9a91SJeremy L Thompson 
2001ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
2002ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
2003ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
2004d8dd9a91SJeremy L Thompson 
2005d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2006d8dd9a91SJeremy L Thompson 
2007d8dd9a91SJeremy L Thompson   @ref User
2008d8dd9a91SJeremy L Thompson **/
200923dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
20102b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
2011d8dd9a91SJeremy L Thompson }
2012d8dd9a91SJeremy L Thompson 
2013d8dd9a91SJeremy L Thompson /**
2014ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
20154385fb7fSSebastian Grimberg 
2016ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
20172788fa27SJeremy L Thompson 
2018ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20192788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
2020ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
20212788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20222788fa27SJeremy L Thompson 
20232788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20242788fa27SJeremy L Thompson 
20252788fa27SJeremy L Thompson   @ref User
20262788fa27SJeremy L Thompson **/
202723dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
20282788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
20292788fa27SJeremy L Thompson }
20302788fa27SJeremy L Thompson 
20312788fa27SJeremy L Thompson /**
2032ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
20332788fa27SJeremy L Thompson 
2034ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20352788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
20362788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
20372788fa27SJeremy L Thompson 
20382788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20392788fa27SJeremy L Thompson 
20402788fa27SJeremy L Thompson   @ref User
20412788fa27SJeremy L Thompson **/
204223dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
20432788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
20442788fa27SJeremy L Thompson }
20452788fa27SJeremy L Thompson 
20462788fa27SJeremy L Thompson /**
2047ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
20485b6ec284SJeremy L Thompson 
2049ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
20505b6ec284SJeremy L Thompson 
2051ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
20525b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
20535b6ec284SJeremy L Thompson   @param[in]     values      Values to set
20545b6ec284SJeremy L Thompson 
20555b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20565b6ec284SJeremy L Thompson 
20575b6ec284SJeremy L Thompson   @ref User
20585b6ec284SJeremy L Thompson **/
20595b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
20605b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20615b6ec284SJeremy L Thompson }
20625b6ec284SJeremy L Thompson 
20635b6ec284SJeremy L Thompson /**
2064ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
20655b6ec284SJeremy L Thompson 
2066ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
20675b6ec284SJeremy L Thompson 
2068ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20695b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
2070ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
20715b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20725b6ec284SJeremy L Thompson 
20735b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20745b6ec284SJeremy L Thompson 
20755b6ec284SJeremy L Thompson   @ref User
20765b6ec284SJeremy L Thompson **/
20775b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
20785b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
20795b6ec284SJeremy L Thompson }
20805b6ec284SJeremy L Thompson 
20815b6ec284SJeremy L Thompson /**
2082ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
20835b6ec284SJeremy L Thompson 
2084ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
20855b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
20865b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
20875b6ec284SJeremy L Thompson 
20885b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
20895b6ec284SJeremy L Thompson 
20905b6ec284SJeremy L Thompson   @ref User
20915b6ec284SJeremy L Thompson **/
20925b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
20935b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
20945b6ec284SJeremy L Thompson }
20955b6ec284SJeremy L Thompson 
20965b6ec284SJeremy L Thompson /**
2097ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
2098d7b241e6Sjeremylt 
2099ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2100ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2101d7b241e6Sjeremylt 
2102ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2103f04ea552SJeremy L Thompson 
2104ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2105ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2106ca94c3ddSJeremy 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
2107ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2108b11c1e72Sjeremylt 
2109b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2110dfdf5a53Sjeremylt 
21117a982d89SJeremy L. Thompson   @ref User
2112b11c1e72Sjeremylt **/
21132b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
21141203703bSJeremy L Thompson   bool is_composite;
21151203703bSJeremy L Thompson 
21162b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2117d7b241e6Sjeremylt 
21181203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
21191203703bSJeremy L Thompson   if (is_composite) {
2120250756a7Sjeremylt     // Composite Operator
2121250756a7Sjeremylt     if (op->ApplyComposite) {
21222b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
2123250756a7Sjeremylt     } else {
2124d1d35e2fSjeremylt       CeedInt       num_suboperators;
2125d1d35e2fSjeremylt       CeedOperator *sub_operators;
21261c66c397SJeremy L Thompson 
21271c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2128c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2129250756a7Sjeremylt 
2130250756a7Sjeremylt       // Zero all output vectors
21313ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2132d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
21331203703bSJeremy L Thompson         CeedInt            num_output_fields;
21341203703bSJeremy L Thompson         CeedOperatorField *output_fields;
21351c66c397SJeremy L Thompson 
21361203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
21371203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
21381203703bSJeremy L Thompson           CeedVector vec;
21391203703bSJeremy L Thompson 
21401203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2141250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
21422b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2143250756a7Sjeremylt           }
2144681d0ea7SJeremy L Thompson           CeedCall(CeedVectorDestroy(&vec));
2145250756a7Sjeremylt         }
2146250756a7Sjeremylt       }
2147250756a7Sjeremylt       // Apply
2148f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2149f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2150cae8b89aSjeremylt       }
2151cae8b89aSjeremylt     }
21523ca2b39bSJeremy L Thompson   } else {
21533ca2b39bSJeremy L Thompson     // Standard Operator
21543ca2b39bSJeremy L Thompson     if (op->Apply) {
21553ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
21563ca2b39bSJeremy L Thompson     } else {
21571203703bSJeremy L Thompson       CeedInt            num_output_fields;
21581203703bSJeremy L Thompson       CeedOperatorField *output_fields;
21591203703bSJeremy L Thompson 
21601203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
21613ca2b39bSJeremy L Thompson       // Zero all output vectors
21621203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2163681d0ea7SJeremy L Thompson         bool       is_active;
21641203703bSJeremy L Thompson         CeedVector vec;
21651c66c397SJeremy L Thompson 
21661203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
2167681d0ea7SJeremy L Thompson         is_active = vec == CEED_VECTOR_ACTIVE;
2168681d0ea7SJeremy L Thompson         if (is_active) vec = out;
21693ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2170681d0ea7SJeremy L Thompson         if (!is_active) CeedCall(CeedVectorDestroy(&vec));
21713ca2b39bSJeremy L Thompson       }
21723ca2b39bSJeremy L Thompson       // Apply
21731203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
21743ca2b39bSJeremy L Thompson     }
2175250756a7Sjeremylt   }
2176e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2177cae8b89aSjeremylt }
2178cae8b89aSjeremylt 
2179cae8b89aSjeremylt /**
2180ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2181cae8b89aSjeremylt 
2182ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2183ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2184cae8b89aSjeremylt 
2185ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2186ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2187ca94c3ddSJeremy 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
2188ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2189cae8b89aSjeremylt 
2190cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2191cae8b89aSjeremylt 
21927a982d89SJeremy L. Thompson   @ref User
2193cae8b89aSjeremylt **/
21942b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
21951203703bSJeremy L Thompson   bool is_composite;
21961203703bSJeremy L Thompson 
21972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2198cae8b89aSjeremylt 
21991203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
22001203703bSJeremy L Thompson   if (is_composite) {
2201250756a7Sjeremylt     // Composite Operator
2202250756a7Sjeremylt     if (op->ApplyAddComposite) {
22032b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2204cae8b89aSjeremylt     } else {
2205d1d35e2fSjeremylt       CeedInt       num_suboperators;
2206d1d35e2fSjeremylt       CeedOperator *sub_operators;
2207250756a7Sjeremylt 
22081c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
22091c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2210d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
22112b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
22121d7d2407SJeremy L Thompson       }
2213250756a7Sjeremylt     }
22141203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
22153ca2b39bSJeremy L Thompson     // Standard Operator
22163ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2217250756a7Sjeremylt   }
2218e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2219d7b241e6Sjeremylt }
2220d7b241e6Sjeremylt 
2221d7b241e6Sjeremylt /**
2222536b928cSSebastian Grimberg   @brief Destroy temporary assembly data associated with a `CeedOperator`
2223536b928cSSebastian Grimberg 
2224536b928cSSebastian Grimberg   @param[in,out] op `CeedOperator` whose assembly data to destroy
2225536b928cSSebastian Grimberg 
2226536b928cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
2227536b928cSSebastian Grimberg 
2228536b928cSSebastian Grimberg   @ref User
2229536b928cSSebastian Grimberg **/
2230536b928cSSebastian Grimberg int CeedOperatorAssemblyDataStrip(CeedOperator op) {
2231536b928cSSebastian Grimberg   bool is_composite;
2232536b928cSSebastian Grimberg 
2233536b928cSSebastian Grimberg   CeedCall(CeedQFunctionAssemblyDataDestroy(&op->qf_assembled));
2234536b928cSSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataDestroy(&op->op_assembled));
2235536b928cSSebastian Grimberg   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2236536b928cSSebastian Grimberg   if (is_composite) {
2237536b928cSSebastian Grimberg     CeedInt       num_suboperators;
2238536b928cSSebastian Grimberg     CeedOperator *sub_operators;
2239536b928cSSebastian Grimberg 
2240536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2241536b928cSSebastian Grimberg     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2242536b928cSSebastian Grimberg     for (CeedInt i = 0; i < num_suboperators; i++) {
2243536b928cSSebastian Grimberg       CeedCall(CeedQFunctionAssemblyDataDestroy(&sub_operators[i]->qf_assembled));
2244536b928cSSebastian Grimberg       CeedCall(CeedOperatorAssemblyDataDestroy(&sub_operators[i]->op_assembled));
2245536b928cSSebastian Grimberg     }
2246536b928cSSebastian Grimberg   }
2247536b928cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
2248536b928cSSebastian Grimberg }
2249536b928cSSebastian Grimberg 
2250536b928cSSebastian Grimberg /**
2251ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2252d7b241e6Sjeremylt 
2253ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2254b11c1e72Sjeremylt 
2255b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2256dfdf5a53Sjeremylt 
22577a982d89SJeremy L. Thompson   @ref User
2258b11c1e72Sjeremylt **/
2259d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2260ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2261ad6481ceSJeremy L Thompson     *op = NULL;
2262ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2263ad6481ceSJeremy L Thompson   }
2264f883f0a5SSebastian Grimberg   // Backend destroy
2265f883f0a5SSebastian Grimberg   if ((*op)->Destroy) {
2266f883f0a5SSebastian Grimberg     CeedCall((*op)->Destroy(*op));
2267f883f0a5SSebastian Grimberg   }
2268fe2413ffSjeremylt   // Free fields
22692b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2270d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2271437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2272437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
227315910d16Sjeremylt       }
2274356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
22752b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
227671352a93Sjeremylt       }
22772b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
22782b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
227971352a93Sjeremylt       }
22802b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
22812b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2282fe2413ffSjeremylt     }
22832b730f8bSJeremy L Thompson   }
22842b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2285d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2286437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2287356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
22882b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
228971352a93Sjeremylt       }
22902b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
22912b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
229271352a93Sjeremylt       }
22932b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
22942b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
22952b730f8bSJeremy L Thompson     }
2296fe2413ffSjeremylt   }
2297f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->input_fields));
2298f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->output_fields));
2299f883f0a5SSebastian Grimberg   // Destroy AtPoints data
230048acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
230148acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
230248acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2303c6b536a8SSebastian Grimberg   // Destroy assembly data (must happen before destroying sub_operators)
2304f883f0a5SSebastian Grimberg   CeedCall(CeedOperatorAssemblyDataStrip(*op));
2305d1d35e2fSjeremylt   // Destroy sub_operators
23062b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2307d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
23082b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
230952d6035fSJeremy L Thompson     }
23102b730f8bSJeremy L Thompson   }
2311f883f0a5SSebastian Grimberg   CeedCall(CeedFree(&(*op)->sub_operators));
23122b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
23132b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
23142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
23153668ca4bSJeremy L Thompson   // Destroy any composite labels
23165ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
23173668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
23182b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
23192b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
23203668ca4bSJeremy L Thompson     }
23215ac9af79SJeremy L Thompson   }
23222b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2323fe2413ffSjeremylt 
23245107b09fSJeremy L Thompson   // Destroy fallback
23252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
23265107b09fSJeremy L Thompson 
23272b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
2328f883f0a5SSebastian Grimberg   CeedCall(CeedDestroy(&(*op)->ceed));
23292b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2330e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2331d7b241e6Sjeremylt }
2332d7b241e6Sjeremylt 
2333d7b241e6Sjeremylt /// @}
2334