xref: /libCEED/interface/ceed-operator.c (revision 935f026aeac4afd1a5641109d8d2197022f564c1)
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);
121e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1227a982d89SJeremy L. Thompson }
1237a982d89SJeremy L. Thompson 
1247a982d89SJeremy L. Thompson /**
125ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1267a982d89SJeremy L. Thompson 
127ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1287a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
129ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1307a982d89SJeremy L. Thompson 
1317a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1327a982d89SJeremy L. Thompson 
1337a982d89SJeremy L. Thompson   @ref Utility
1347a982d89SJeremy L. Thompson **/
1357a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1367a982d89SJeremy L. Thompson   const char         *pre = sub ? "  " : "";
1371203703bSJeremy L Thompson   CeedInt             num_elem, num_qpts, total_fields = 0, num_input_fields, num_output_fields;
1381203703bSJeremy L Thompson   CeedQFunction       qf;
1391203703bSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1401203703bSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1417a982d89SJeremy L. Thompson 
1422b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1451203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1461203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
1471203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1481c66c397SJeremy L Thompson 
1492b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1502b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1511203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, num_input_fields, num_input_fields > 1 ? "s" : "");
1521203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1531203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_input_fields[i], qf_input_fields[i], i, sub, 1, stream));
1547a982d89SJeremy L. Thompson   }
1551203703bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, num_output_fields, num_output_fields > 1 ? "s" : "");
1561203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1571203703bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op_output_fields[i], qf_output_fields[i], i, sub, 0, stream));
1587a982d89SJeremy L. Thompson   }
159e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1607a982d89SJeremy L. Thompson }
1617a982d89SJeremy L. Thompson 
162d99fa3c5SJeremy L Thompson /**
163ca94c3ddSJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`
164eaf62fffSJeremy L Thompson 
165ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
166ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
167eaf62fffSJeremy L Thompson 
168eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
169eaf62fffSJeremy L Thompson 
170eaf62fffSJeremy L Thompson   @ref Developer
171eaf62fffSJeremy L Thompson **/
172eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
173506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
174506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
175506b1a0cSSebastian Grimberg }
176506b1a0cSSebastian Grimberg 
177506b1a0cSSebastian Grimberg /**
178ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`
179506b1a0cSSebastian Grimberg 
180ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
181ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
182ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
183506b1a0cSSebastian Grimberg 
184506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
185506b1a0cSSebastian Grimberg 
186506b1a0cSSebastian Grimberg   @ref Developer
187506b1a0cSSebastian Grimberg **/
188506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1891203703bSJeremy L Thompson   bool               is_composite;
1901203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
1916aaed0edSJeremy L Thompson   Ceed               ceed;
1921203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
1931c66c397SJeremy L Thompson 
1946aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1951203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1961203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1971203703bSJeremy L Thompson 
198506b1a0cSSebastian Grimberg   if (active_input_basis) {
199506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
2001203703bSJeremy L Thompson     if (!is_composite) {
2011203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2021203703bSJeremy L Thompson         CeedVector vec;
2031203703bSJeremy L Thompson 
2041203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2051203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2061203703bSJeremy L Thompson           CeedBasis basis;
2071203703bSJeremy L Thompson 
2081203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
2091203703bSJeremy L Thompson           CeedCheck(!*active_input_basis || *active_input_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active input CeedBases found");
2101203703bSJeremy L Thompson           *active_input_basis = basis;
211eaf62fffSJeremy L Thompson         }
2122b730f8bSJeremy L Thompson       }
213506b1a0cSSebastian Grimberg       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
214506b1a0cSSebastian Grimberg     }
215506b1a0cSSebastian Grimberg   }
216506b1a0cSSebastian Grimberg   if (active_output_basis) {
217506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
2181203703bSJeremy L Thompson     if (!is_composite) {
2191203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2201203703bSJeremy L Thompson         CeedVector vec;
2211203703bSJeremy L Thompson 
2221203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2231203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2241203703bSJeremy L Thompson           CeedBasis basis;
2251203703bSJeremy L Thompson 
2261203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
2271203703bSJeremy L Thompson           CeedCheck(!*active_output_basis || *active_output_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active output CeedBases found");
2281203703bSJeremy L Thompson           *active_output_basis = basis;
229506b1a0cSSebastian Grimberg         }
230506b1a0cSSebastian Grimberg       }
231506b1a0cSSebastian Grimberg       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
232506b1a0cSSebastian Grimberg     }
233506b1a0cSSebastian Grimberg   }
234eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
235eaf62fffSJeremy L Thompson }
236eaf62fffSJeremy L Thompson 
237eaf62fffSJeremy L Thompson /**
238ca94c3ddSJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`
239e2f04181SAndrew T. Barker 
240ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
241ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
242e2f04181SAndrew T. Barker 
243e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
244e2f04181SAndrew T. Barker 
245e2f04181SAndrew T. Barker   @ref Utility
246e2f04181SAndrew T. Barker **/
2472b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
248506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
249506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
250506b1a0cSSebastian Grimberg }
251506b1a0cSSebastian Grimberg 
252506b1a0cSSebastian Grimberg /**
253ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`
254506b1a0cSSebastian Grimberg 
255ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
256ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
257ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
258506b1a0cSSebastian Grimberg 
259506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
260506b1a0cSSebastian Grimberg 
261506b1a0cSSebastian Grimberg   @ref Utility
262506b1a0cSSebastian Grimberg **/
263506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2641203703bSJeremy L Thompson   bool               is_composite;
2651203703bSJeremy L Thompson   CeedInt            num_input_fields, num_output_fields;
2666aaed0edSJeremy L Thompson   Ceed               ceed;
2671203703bSJeremy L Thompson   CeedOperatorField *op_input_fields, *op_output_fields;
2681c66c397SJeremy L Thompson 
2696aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2701203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
2711203703bSJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
2721203703bSJeremy L Thompson 
273506b1a0cSSebastian Grimberg   if (active_input_rstr) {
274506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
2751203703bSJeremy L Thompson     if (!is_composite) {
2761203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_input_fields; i++) {
2771203703bSJeremy L Thompson         CeedVector vec;
2781203703bSJeremy L Thompson 
2791203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2801203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2811203703bSJeremy L Thompson           CeedElemRestriction rstr;
2821203703bSJeremy L Thompson 
2831203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
2841203703bSJeremy L Thompson           CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active input CeedElemRestrictions found");
2851203703bSJeremy L Thompson           *active_input_rstr = rstr;
286e2f04181SAndrew T. Barker         }
2872b730f8bSJeremy L Thompson       }
288506b1a0cSSebastian Grimberg       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
289506b1a0cSSebastian Grimberg     }
290506b1a0cSSebastian Grimberg   }
291506b1a0cSSebastian Grimberg   if (active_output_rstr) {
292506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
2931203703bSJeremy L Thompson     if (!is_composite) {
2941203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
2951203703bSJeremy L Thompson         CeedVector vec;
2961203703bSJeremy L Thompson 
2971203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
2981203703bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) {
2991203703bSJeremy L Thompson           CeedElemRestriction rstr;
3001203703bSJeremy L Thompson 
3011203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
3021203703bSJeremy L Thompson           CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active output CeedElemRestrictions found");
3031203703bSJeremy L Thompson           *active_output_rstr = rstr;
304506b1a0cSSebastian Grimberg         }
305506b1a0cSSebastian Grimberg       }
306506b1a0cSSebastian Grimberg       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
307506b1a0cSSebastian Grimberg     }
308506b1a0cSSebastian Grimberg   }
309e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
310e2f04181SAndrew T. Barker }
311e2f04181SAndrew T. Barker 
312d8dd9a91SJeremy L Thompson /**
313ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
3144385fb7fSSebastian Grimberg 
315ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
316ca94c3ddSJeremy 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.
317d8dd9a91SJeremy L Thompson 
318ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
319ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
320ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
3212788fa27SJeremy L Thompson   @param[in]     values      Values to set
322d8dd9a91SJeremy L Thompson 
323d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
324d8dd9a91SJeremy L Thompson 
325d8dd9a91SJeremy L Thompson   @ref User
326d8dd9a91SJeremy L Thompson **/
3272788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3281c66c397SJeremy L Thompson   bool is_composite = false;
3291203703bSJeremy L Thompson   Ceed ceed;
3301c66c397SJeremy L Thompson 
3311203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3321203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3333668ca4bSJeremy L Thompson 
3345ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3355ac9af79SJeremy L Thompson   if (field_label->from_op) {
3365ac9af79SJeremy L Thompson     CeedInt index = -1;
3375ac9af79SJeremy L Thompson 
3385ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3395ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3405ac9af79SJeremy L Thompson     }
3411203703bSJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3425ac9af79SJeremy L Thompson   }
3435ac9af79SJeremy L Thompson 
3442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
345d8dd9a91SJeremy L Thompson   if (is_composite) {
346d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
347d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
348d8dd9a91SJeremy L Thompson 
349c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
350c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3511203703bSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
352d8dd9a91SJeremy L Thompson 
353d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3541203703bSJeremy L Thompson       CeedQFunction        qf;
3551203703bSJeremy L Thompson       CeedQFunctionContext ctx;
3561203703bSJeremy L Thompson 
3571203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
3581203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
359d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3601203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
3611203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values));
362d8dd9a91SJeremy L Thompson       }
363d8dd9a91SJeremy L Thompson     }
364d8dd9a91SJeremy L Thompson   } else {
3651203703bSJeremy L Thompson     CeedQFunction        qf;
3661203703bSJeremy L Thompson     CeedQFunctionContext ctx;
3671203703bSJeremy L Thompson 
3681203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
3691203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
3701203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3711203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values));
3722788fa27SJeremy L Thompson   }
3736d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3742788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3752788fa27SJeremy L Thompson }
3762788fa27SJeremy L Thompson 
3772788fa27SJeremy L Thompson /**
378ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3794385fb7fSSebastian Grimberg 
380ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
381ca94c3ddSJeremy 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.
3822788fa27SJeremy L Thompson 
383ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3842788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3852788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
386c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
387c5d0f995SJed Brown   @param[out]    values      Values in the label
3882788fa27SJeremy L Thompson 
3892788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3902788fa27SJeremy L Thompson 
3912788fa27SJeremy L Thompson   @ref User
3922788fa27SJeremy L Thompson **/
3932788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3942788fa27SJeremy L Thompson                                              void *values) {
3951c66c397SJeremy L Thompson   bool is_composite = false;
3961203703bSJeremy L Thompson   Ceed ceed;
3971c66c397SJeremy L Thompson 
3981203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
3991203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4002788fa27SJeremy L Thompson 
4012788fa27SJeremy L Thompson   *(void **)values = NULL;
4022788fa27SJeremy L Thompson   *num_values      = 0;
4032788fa27SJeremy L Thompson 
4045ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4055ac9af79SJeremy L Thompson   if (field_label->from_op) {
4065ac9af79SJeremy L Thompson     CeedInt index = -1;
4075ac9af79SJeremy L Thompson 
4085ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4095ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4105ac9af79SJeremy L Thompson     }
4116e536b99SJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4125ac9af79SJeremy L Thompson   }
4135ac9af79SJeremy L Thompson 
4142788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4152788fa27SJeremy L Thompson   if (is_composite) {
4162788fa27SJeremy L Thompson     CeedInt       num_sub;
4172788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4182788fa27SJeremy L Thompson 
4192788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4202788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4211203703bSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
4222788fa27SJeremy L Thompson 
4232788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4241203703bSJeremy L Thompson       CeedQFunction        qf;
4251203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4261203703bSJeremy L Thompson 
4271203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
4281203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
4292788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4301203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
4311203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values));
4322788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4332788fa27SJeremy L Thompson       }
4342788fa27SJeremy L Thompson     }
4352788fa27SJeremy L Thompson   } else {
4361203703bSJeremy L Thompson     CeedQFunction        qf;
4371203703bSJeremy L Thompson     CeedQFunctionContext ctx;
4381203703bSJeremy L Thompson 
4391203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
4401203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
4411203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4421203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values));
4432788fa27SJeremy L Thompson   }
4442788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4452788fa27SJeremy L Thompson }
4462788fa27SJeremy L Thompson 
4472788fa27SJeremy L Thompson /**
448ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
4494385fb7fSSebastian Grimberg 
450ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
451ca94c3ddSJeremy 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.
4522788fa27SJeremy L Thompson 
453ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
4542788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4552788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
456c5d0f995SJed Brown   @param[in]     values      Values array to restore
4572788fa27SJeremy L Thompson 
4582788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4592788fa27SJeremy L Thompson 
4602788fa27SJeremy L Thompson   @ref User
4612788fa27SJeremy L Thompson **/
4622788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4631c66c397SJeremy L Thompson   bool is_composite = false;
4641203703bSJeremy L Thompson   Ceed ceed;
4651c66c397SJeremy L Thompson 
4661203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
4671203703bSJeremy L Thompson   CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4682788fa27SJeremy L Thompson 
4695ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4705ac9af79SJeremy L Thompson   if (field_label->from_op) {
4715ac9af79SJeremy L Thompson     CeedInt index = -1;
4725ac9af79SJeremy L Thompson 
4735ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4745ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4755ac9af79SJeremy L Thompson     }
4766e536b99SJeremy L Thompson     CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4775ac9af79SJeremy L Thompson   }
4785ac9af79SJeremy L Thompson 
4792788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4802788fa27SJeremy L Thompson   if (is_composite) {
4812788fa27SJeremy L Thompson     CeedInt       num_sub;
4822788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4832788fa27SJeremy L Thompson 
4842788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4852788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4866e536b99SJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
4872788fa27SJeremy L Thompson 
4882788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4891203703bSJeremy L Thompson       CeedQFunction        qf;
4901203703bSJeremy L Thompson       CeedQFunctionContext ctx;
4911203703bSJeremy L Thompson 
4921203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf));
4931203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetContext(qf, &ctx));
4942788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4951203703bSJeremy L Thompson       if (field_label->sub_labels[i] && ctx) {
4961203703bSJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values));
4972788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4982788fa27SJeremy L Thompson       }
4992788fa27SJeremy L Thompson     }
5002788fa27SJeremy L Thompson   } else {
5011203703bSJeremy L Thompson     CeedQFunction        qf;
5021203703bSJeremy L Thompson     CeedQFunctionContext ctx;
5031203703bSJeremy L Thompson 
5041203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
5051203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetContext(qf, &ctx));
5061203703bSJeremy L Thompson     CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
5071203703bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values));
508d8dd9a91SJeremy L Thompson   }
509d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
510d8dd9a91SJeremy L Thompson }
511d8dd9a91SJeremy L Thompson 
5127a982d89SJeremy L. Thompson /// @}
5137a982d89SJeremy L. Thompson 
5147a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5157a982d89SJeremy L. Thompson /// CeedOperator Backend API
5167a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5177a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
5187a982d89SJeremy L. Thompson /// @{
5197a982d89SJeremy L. Thompson 
5207a982d89SJeremy L. Thompson /**
521ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
5227a982d89SJeremy L. Thompson 
523ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
524d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
5257a982d89SJeremy L. Thompson 
5267a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson   @ref Backend
5297a982d89SJeremy L. Thompson **/
530d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
5311203703bSJeremy L Thompson   bool is_composite;
5321203703bSJeremy L Thompson 
5331203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
5346e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operators");
535d1d35e2fSjeremylt   *num_args = op->num_fields;
536e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5377a982d89SJeremy L. Thompson }
5387a982d89SJeremy L. Thompson 
5397a982d89SJeremy L. Thompson /**
5409463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
5419463e855SJeremy L Thompson 
5429463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
5439463e855SJeremy L Thompson 
5449463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
5459463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
5469463e855SJeremy L Thompson 
5479463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5489463e855SJeremy L Thompson 
5499463e855SJeremy L Thompson   @ref Backend
5509463e855SJeremy L Thompson **/
5519463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
5529463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
5539463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
5549463e855SJeremy L Thompson 
5559463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
5569463e855SJeremy L Thompson   *has_tensor_bases = true;
5579463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
5589463e855SJeremy L Thompson     bool      is_tensor;
5599463e855SJeremy L Thompson     CeedBasis basis;
5609463e855SJeremy L Thompson 
5619463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
5629463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5639463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5649463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5659463e855SJeremy L Thompson     }
5669463e855SJeremy L Thompson   }
5679463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
5689463e855SJeremy L Thompson     bool      is_tensor;
5699463e855SJeremy L Thompson     CeedBasis basis;
5709463e855SJeremy L Thompson 
5719463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
5729463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
5739463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
5749463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
5759463e855SJeremy L Thompson     }
5769463e855SJeremy L Thompson   }
5779463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5789463e855SJeremy L Thompson }
5799463e855SJeremy L Thompson 
5809463e855SJeremy L Thompson /**
5811203703bSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is immutable
5821203703bSJeremy L Thompson 
5831203703bSJeremy L Thompson   @param[in]  op           `CeedOperator`
5841203703bSJeremy L Thompson   @param[out] is_immutable Variable to store immutability status
5851203703bSJeremy L Thompson 
5861203703bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5871203703bSJeremy L Thompson 
5881203703bSJeremy L Thompson   @ref Backend
5891203703bSJeremy L Thompson **/
5901203703bSJeremy L Thompson int CeedOperatorIsImmutable(CeedOperator op, bool *is_immutable) {
5911203703bSJeremy L Thompson   *is_immutable = op->is_immutable;
5921203703bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
5931203703bSJeremy L Thompson }
5941203703bSJeremy L Thompson 
5951203703bSJeremy L Thompson /**
596ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
5977a982d89SJeremy L. Thompson 
598ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
599d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
6007a982d89SJeremy L. Thompson 
6017a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6027a982d89SJeremy L. Thompson 
6037a982d89SJeremy L. Thompson   @ref Backend
6047a982d89SJeremy L. Thompson **/
605d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
606f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
607e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6087a982d89SJeremy L. Thompson }
6097a982d89SJeremy L. Thompson 
6107a982d89SJeremy L. Thompson /**
611ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
6127a982d89SJeremy L. Thompson 
613ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
614ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
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 **/
6207a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
6211203703bSJeremy L Thompson   bool is_composite;
6221203703bSJeremy L Thompson 
6231203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
6246e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
6257a982d89SJeremy L. Thompson   *qf = op->qf;
626e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6277a982d89SJeremy L. Thompson }
6287a982d89SJeremy L. Thompson 
6297a982d89SJeremy L. Thompson /**
630ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
631c04a41a7SJeremy L Thompson 
632ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
633d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
634c04a41a7SJeremy L Thompson 
635c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
636c04a41a7SJeremy L Thompson 
637c04a41a7SJeremy L Thompson   @ref Backend
638c04a41a7SJeremy L Thompson **/
639d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
640f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
641e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
642c04a41a7SJeremy L Thompson }
643c04a41a7SJeremy L Thompson 
644c04a41a7SJeremy L Thompson /**
645ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
6467a982d89SJeremy L. Thompson 
647ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
6487a982d89SJeremy L. Thompson   @param[out] data Variable to store data
6497a982d89SJeremy L. Thompson 
6507a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6517a982d89SJeremy L. Thompson 
6527a982d89SJeremy L. Thompson   @ref Backend
6537a982d89SJeremy L. Thompson **/
654777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
655777ff853SJeremy L Thompson   *(void **)data = op->data;
656e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6577a982d89SJeremy L. Thompson }
6587a982d89SJeremy L. Thompson 
6597a982d89SJeremy L. Thompson /**
660ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
6617a982d89SJeremy L. Thompson 
662ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
663ea61e9acSJeremy L Thompson   @param[in]     data Data to set
6647a982d89SJeremy L. Thompson 
6657a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6667a982d89SJeremy L. Thompson 
6677a982d89SJeremy L. Thompson   @ref Backend
6687a982d89SJeremy L. Thompson **/
669777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
670777ff853SJeremy L Thompson   op->data = data;
671e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6727a982d89SJeremy L. Thompson }
6737a982d89SJeremy L. Thompson 
6747a982d89SJeremy L. Thompson /**
675ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
67634359f16Sjeremylt 
677ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
67834359f16Sjeremylt 
67934359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
68034359f16Sjeremylt 
68134359f16Sjeremylt   @ref Backend
68234359f16Sjeremylt **/
6839560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
68434359f16Sjeremylt   op->ref_count++;
68534359f16Sjeremylt   return CEED_ERROR_SUCCESS;
68634359f16Sjeremylt }
68734359f16Sjeremylt 
68834359f16Sjeremylt /**
689ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
6907a982d89SJeremy L. Thompson 
691ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
6927a982d89SJeremy L. Thompson 
6937a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
6947a982d89SJeremy L. Thompson 
6957a982d89SJeremy L. Thompson   @ref Backend
6967a982d89SJeremy L. Thompson **/
6977a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
698f04ea552SJeremy L Thompson   op->is_backend_setup = true;
699e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7007a982d89SJeremy L. Thompson }
7017a982d89SJeremy L. Thompson 
7027a982d89SJeremy L. Thompson /// @}
7037a982d89SJeremy L. Thompson 
7047a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7057a982d89SJeremy L. Thompson /// CeedOperator Public API
7067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
7077a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
708dfdf5a53Sjeremylt /// @{
709d7b241e6Sjeremylt 
710d7b241e6Sjeremylt /**
711ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
7124385fb7fSSebastian Grimberg 
713ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
714d7b241e6Sjeremylt 
715ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
716ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
717ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
718ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
719ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
720b11c1e72Sjeremylt 
721b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
722dfdf5a53Sjeremylt 
7237a982d89SJeremy L. Thompson   @ref User
724d7b241e6Sjeremylt  */
7252b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
7265fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
7275fe0d4faSjeremylt     Ceed delegate;
7286574a04fSJeremy L Thompson 
7292b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7301ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate");
7312b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
732e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7335fe0d4faSjeremylt   }
7345fe0d4faSjeremylt 
735ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
736db002c03SJeremy L Thompson 
7372b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
738db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
739d1d35e2fSjeremylt   (*op)->ref_count   = 1;
7402b104005SJeremy L Thompson   (*op)->input_size  = -1;
7412b104005SJeremy L Thompson   (*op)->output_size = -1;
742db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
743db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
744db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
7452b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
7462b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
7472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
7482b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
749e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
750d7b241e6Sjeremylt }
751d7b241e6Sjeremylt 
752d7b241e6Sjeremylt /**
753ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
75448acf710SJeremy L Thompson 
755ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
756ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
75748acf710SJeremy L Thompson 
758ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
759ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
760ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
761ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
76248acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
76348acf710SJeremy L Thompson 
76448acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
76548acf710SJeremy L Thompson 
76648acf710SJeremy L Thompson   @ref User
76748acf710SJeremy L Thompson  */
76848acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
76948acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
77048acf710SJeremy L Thompson     Ceed delegate;
77148acf710SJeremy L Thompson 
77248acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
7731ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints");
77448acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
77548acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
77648acf710SJeremy L Thompson   }
77748acf710SJeremy L Thompson 
778ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
77948acf710SJeremy L Thompson 
78048acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
78148acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
78248acf710SJeremy L Thompson   (*op)->ref_count    = 1;
78348acf710SJeremy L Thompson   (*op)->is_at_points = true;
78448acf710SJeremy L Thompson   (*op)->input_size   = -1;
78548acf710SJeremy L Thompson   (*op)->output_size  = -1;
78648acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
78748acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
78848acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
78948acf710SJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
79048acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
79148acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
79248acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
79348acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79448acf710SJeremy L Thompson }
79548acf710SJeremy L Thompson 
79648acf710SJeremy L Thompson /**
797ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
79852d6035fSJeremy L Thompson 
799ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
800ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
80152d6035fSJeremy L Thompson 
80252d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
80352d6035fSJeremy L Thompson 
8047a982d89SJeremy L. Thompson   @ref User
80552d6035fSJeremy L Thompson  */
80652d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
80752d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
80852d6035fSJeremy L Thompson     Ceed delegate;
80952d6035fSJeremy L Thompson 
8101c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
811250756a7Sjeremylt     if (delegate) {
8122b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
813e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
81452d6035fSJeremy L Thompson     }
815250756a7Sjeremylt   }
81652d6035fSJeremy L Thompson 
8172b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
818db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
819996d9ab5SJed Brown   (*op)->ref_count    = 1;
820f04ea552SJeremy L Thompson   (*op)->is_composite = true;
8212b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
8222b104005SJeremy L Thompson   (*op)->input_size  = -1;
8232b104005SJeremy L Thompson   (*op)->output_size = -1;
824250756a7Sjeremylt 
825db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
826e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
82752d6035fSJeremy L Thompson }
82852d6035fSJeremy L Thompson 
82952d6035fSJeremy L Thompson /**
830ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
8314385fb7fSSebastian Grimberg 
832ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
833512bb800SJeremy L Thompson 
834ca94c3ddSJeremy 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`.
835ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
8369560d06aSjeremylt 
837ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
838ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
8399560d06aSjeremylt 
8409560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
8419560d06aSjeremylt 
8429560d06aSjeremylt   @ref User
8439560d06aSjeremylt **/
8449560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
8452b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
8462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
8479560d06aSjeremylt   *op_copy = op;
8489560d06aSjeremylt   return CEED_ERROR_SUCCESS;
8499560d06aSjeremylt }
8509560d06aSjeremylt 
8519560d06aSjeremylt /**
852ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
853d7b241e6Sjeremylt 
854ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
855bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
856ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
857d7b241e6Sjeremylt 
858ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
859ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
860d7b241e6Sjeremylt 
861528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
862bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
863528a22edSJeremy L Thompson 
864ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
865ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
866bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
867bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
868bafebce1SSebastian 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`
869b11c1e72Sjeremylt 
870b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
871dfdf5a53Sjeremylt 
8727a982d89SJeremy L. Thompson   @ref User
873b11c1e72Sjeremylt **/
874bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
8751203703bSJeremy L Thompson   bool               is_input = true, is_at_points, is_composite, is_immutable;
8761203703bSJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields;
8771203703bSJeremy L Thompson   Ceed               ceed;
8781203703bSJeremy L Thompson   CeedQFunction      qf;
8791203703bSJeremy L Thompson   CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields;
8801c66c397SJeremy L Thompson   CeedOperatorField *op_field;
8811c66c397SJeremy L Thompson 
8821203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
8831203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
8841203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
8851203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
8861203703bSJeremy L Thompson   CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
8871203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
8881203703bSJeremy L Thompson   CeedCheck(rstr, ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
8891203703bSJeremy L Thompson   CeedCheck(basis, ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
8901203703bSJeremy L Thompson   CeedCheck(vec, ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
89152d6035fSJeremy L Thompson 
892bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
8931203703bSJeremy L Thompson   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, ceed, CEED_ERROR_DIMENSION,
894ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
8952c7e7413SJeremy L Thompson   {
8962c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
8972c7e7413SJeremy L Thompson 
898bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
89948acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
9001203703bSJeremy L Thompson       CeedCheck(is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields");
9011203703bSJeremy L Thompson       CeedCheck(basis == CEED_BASIS_NONE, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
90248acf710SJeremy L Thompson       if (!op->first_points_rstr) {
903bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
90448acf710SJeremy L Thompson       } else {
90548acf710SJeremy L Thompson         bool are_compatible;
90648acf710SJeremy L Thompson 
907bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
9081203703bSJeremy L Thompson         CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE,
909ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
91048acf710SJeremy L Thompson       }
91148acf710SJeremy L Thompson     }
9122c7e7413SJeremy L Thompson   }
913d7b241e6Sjeremylt 
914bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
915bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
9161203703bSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, ceed, CEED_ERROR_DIMENSION,
917ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
918528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
919bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
9201203703bSJeremy L Thompson 
9211203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
9221203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
9231203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9246f8994e9SJeremy L Thompson     const char *qf_field_name;
9251203703bSJeremy L Thompson 
9261203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_input_fields[i], &qf_field_name));
9271203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9281203703bSJeremy L Thompson       qf_field = qf_input_fields[i];
929d1d35e2fSjeremylt       op_field = &op->input_fields[i];
930d7b241e6Sjeremylt       goto found;
931d7b241e6Sjeremylt     }
932d7b241e6Sjeremylt   }
9332b104005SJeremy L Thompson   is_input = false;
9341203703bSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9356f8994e9SJeremy L Thompson     const char *qf_field_name;
9361203703bSJeremy L Thompson 
9371203703bSJeremy L Thompson     CeedCall(CeedQFunctionFieldGetName(qf_output_fields[i], &qf_field_name));
9381203703bSJeremy L Thompson     if (!strcmp(field_name, qf_field_name)) {
9391203703bSJeremy L Thompson       qf_field = qf_output_fields[i];
940d1d35e2fSjeremylt       op_field = &op->output_fields[i];
941d7b241e6Sjeremylt       goto found;
942d7b241e6Sjeremylt     }
943d7b241e6Sjeremylt   }
944c042f62fSJeremy L Thompson   // LCOV_EXCL_START
9451203703bSJeremy L Thompson   return CeedError(ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
946c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
947d7b241e6Sjeremylt found:
9481203703bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(ceed, qf_field, rstr, basis));
9492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
950e15f9bd0SJeremy L Thompson 
951bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
9522b104005SJeremy L Thompson     CeedSize l_size;
9531c66c397SJeremy L Thompson 
954bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
9552b104005SJeremy L Thompson     if (is_input) {
9562b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
9571203703bSJeremy L Thompson       CeedCheck(l_size == op->input_size, ceed, CEED_ERROR_INCOMPATIBLE,
958249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
9592b104005SJeremy L Thompson     } else {
9602b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
9611203703bSJeremy L Thompson       CeedCheck(l_size == op->output_size, ceed, CEED_ERROR_INCOMPATIBLE,
962249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
9632b104005SJeremy L Thompson     }
9642b730f8bSJeremy L Thompson   }
9652b104005SJeremy L Thompson 
966bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
967bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
968bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
969d1d35e2fSjeremylt     op->num_elem        = num_elem;
970d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
971e15f9bd0SJeremy L Thompson   }
972bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
9732a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
974d1d35e2fSjeremylt   op->num_fields += 1;
9752b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
976e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
977d7b241e6Sjeremylt }
978d7b241e6Sjeremylt 
979d7b241e6Sjeremylt /**
980ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
98143bbe138SJeremy L Thompson 
982ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
983f04ea552SJeremy L Thompson 
984ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
985f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
986ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
987f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
988ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
98943bbe138SJeremy L Thompson 
99043bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
99143bbe138SJeremy L Thompson 
992e9b533fbSJeremy L Thompson   @ref Advanced
99343bbe138SJeremy L Thompson **/
9942b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
99543bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
9961203703bSJeremy L Thompson   bool          is_composite;
9971203703bSJeremy L Thompson   CeedQFunction qf;
9981203703bSJeremy L Thompson 
9991203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10006e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
10012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
100243bbe138SJeremy L Thompson 
10031203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
10041203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL));
100543bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
100643bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
100743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
100843bbe138SJeremy L Thompson }
100943bbe138SJeremy L Thompson 
101043bbe138SJeremy L Thompson /**
1011ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
101248acf710SJeremy L Thompson 
1013ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
101448acf710SJeremy L Thompson 
1015ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
1016ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
1017ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
101848acf710SJeremy L Thompson 
101948acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
102048acf710SJeremy L Thompson 
102148acf710SJeremy L Thompson   @ref Advanced
102248acf710SJeremy L Thompson **/
102348acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
10241203703bSJeremy L Thompson   bool is_at_points, is_immutable;
10251203703bSJeremy L Thompson   Ceed ceed;
10262a3ff1c9SZach Atkins 
10271203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
10282a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10291203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(op, &is_immutable));
10301203703bSJeremy L Thompson   CeedCheck(is_at_points, ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
10311203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
103248acf710SJeremy L Thompson 
103348acf710SJeremy L Thompson   if (!op->first_points_rstr) {
103448acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
103548acf710SJeremy L Thompson   } else {
103648acf710SJeremy L Thompson     bool are_compatible;
103748acf710SJeremy L Thompson 
103848acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
10391203703bSJeremy L Thompson     CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE,
1040ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
104148acf710SJeremy L Thompson   }
104248acf710SJeremy L Thompson 
104348acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
104448acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
104548acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
104648acf710SJeremy L Thompson }
104748acf710SJeremy L Thompson 
104848acf710SJeremy L Thompson /**
1049b594f9faSZach Atkins   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
1050b594f9faSZach Atkins 
1051b594f9faSZach Atkins   @param[in]  op           `CeedOperator`
1052b594f9faSZach Atkins   @param[out] is_at_points Variable to store at points status
1053b594f9faSZach Atkins 
1054b594f9faSZach Atkins   @return An error code: 0 - success, otherwise - failure
1055b594f9faSZach Atkins 
1056b594f9faSZach Atkins   @ref User
1057b594f9faSZach Atkins **/
1058b594f9faSZach Atkins int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
1059b594f9faSZach Atkins   *is_at_points = op->is_at_points;
1060b594f9faSZach Atkins   return CEED_ERROR_SUCCESS;
1061b594f9faSZach Atkins }
1062b594f9faSZach Atkins 
1063b594f9faSZach Atkins /**
1064ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
106548acf710SJeremy L Thompson 
1066ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
106748acf710SJeremy L Thompson 
1068ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
1069ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
1070ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
107148acf710SJeremy L Thompson 
107248acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
107348acf710SJeremy L Thompson 
107448acf710SJeremy L Thompson   @ref Advanced
107548acf710SJeremy L Thompson **/
107648acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
10772a3ff1c9SZach Atkins   bool is_at_points;
10782a3ff1c9SZach Atkins 
10792a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
10806e536b99SJeremy L Thompson   CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points");
108148acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
108248acf710SJeremy L Thompson 
108348acf710SJeremy L Thompson   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
108448acf710SJeremy L Thompson   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
108548acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
108648acf710SJeremy L Thompson }
108748acf710SJeremy L Thompson 
108848acf710SJeremy L Thompson /**
1089be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
1090be9c6463SJeremy L Thompson 
1091be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
1092de5900adSJames Wright 
1093ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1094de5900adSJames Wright 
1095ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
1096ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
1097ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
1098de5900adSJames Wright 
1099de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
1100de5900adSJames Wright 
1101de5900adSJames Wright   @ref Advanced
1102de5900adSJames Wright **/
1103de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
11046f8994e9SJeremy L Thompson   const char        *name;
1105de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
1106de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
1107de5900adSJames Wright 
1108be9c6463SJeremy L Thompson   *op_field = NULL;
11091c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1110de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
1111de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
1112de5900adSJames Wright     if (!strcmp(name, field_name)) {
1113de5900adSJames Wright       *op_field = input_fields[i];
1114de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1115de5900adSJames Wright     }
1116de5900adSJames Wright   }
1117de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
1118de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
1119de5900adSJames Wright     if (!strcmp(name, field_name)) {
1120de5900adSJames Wright       *op_field = output_fields[i];
1121de5900adSJames Wright       return CEED_ERROR_SUCCESS;
1122de5900adSJames Wright     }
1123de5900adSJames Wright   }
1124be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1125de5900adSJames Wright }
1126de5900adSJames Wright 
1127de5900adSJames Wright /**
1128ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
112928567f8fSJeremy L Thompson 
1130ca94c3ddSJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
113128567f8fSJeremy L Thompson   @param[out] field_name Variable to store the field name
113228567f8fSJeremy L Thompson 
113328567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
113428567f8fSJeremy L Thompson 
1135e9b533fbSJeremy L Thompson   @ref Advanced
113628567f8fSJeremy L Thompson **/
11376f8994e9SJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, const char **field_name) {
11386f8994e9SJeremy L Thompson   *field_name = op_field->field_name;
113928567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
114028567f8fSJeremy L Thompson }
114128567f8fSJeremy L Thompson 
114228567f8fSJeremy L Thompson /**
1143ca94c3ddSJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field
114443bbe138SJeremy L Thompson 
1145ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1146ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
114743bbe138SJeremy L Thompson 
114843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
114943bbe138SJeremy L Thompson 
1150e9b533fbSJeremy L Thompson   @ref Advanced
115143bbe138SJeremy L Thompson **/
11522b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1153437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
115443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
115543bbe138SJeremy L Thompson }
115643bbe138SJeremy L Thompson 
115743bbe138SJeremy L Thompson /**
1158ca94c3ddSJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field
115943bbe138SJeremy L Thompson 
1160ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1161ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
116243bbe138SJeremy L Thompson 
116343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
116443bbe138SJeremy L Thompson 
1165e9b533fbSJeremy L Thompson   @ref Advanced
116643bbe138SJeremy L Thompson **/
116743bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
116843bbe138SJeremy L Thompson   *basis = op_field->basis;
116943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
117043bbe138SJeremy L Thompson }
117143bbe138SJeremy L Thompson 
117243bbe138SJeremy L Thompson /**
1173ca94c3ddSJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field
117443bbe138SJeremy L Thompson 
1175ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1176ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
117743bbe138SJeremy L Thompson 
117843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
117943bbe138SJeremy L Thompson 
1180e9b533fbSJeremy L Thompson   @ref Advanced
118143bbe138SJeremy L Thompson **/
118243bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
118343bbe138SJeremy L Thompson   *vec = op_field->vec;
118443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
118543bbe138SJeremy L Thompson }
118643bbe138SJeremy L Thompson 
118743bbe138SJeremy L Thompson /**
1188ab747706SJeremy L Thompson   @brief Get the data of a `CeedOperator` Field.
1189ab747706SJeremy L Thompson 
1190ab747706SJeremy L Thompson   Any arguments set as `NULL` are ignored.
1191ab747706SJeremy L Thompson 
1192ab747706SJeremy L Thompson   @param[in]  op_field   `CeedOperator` Field
1193ab747706SJeremy L Thompson   @param[out] field_name Variable to store the field name
1194ab747706SJeremy L Thompson   @param[out] rstr       Variable to store `CeedElemRestriction`
1195ab747706SJeremy L Thompson   @param[out] basis      Variable to store `CeedBasis`
1196ab747706SJeremy L Thompson   @param[out] vec        Variable to store `CeedVector`
1197ab747706SJeremy L Thompson 
1198ab747706SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1199ab747706SJeremy L Thompson 
1200ab747706SJeremy L Thompson   @ref Advanced
1201ab747706SJeremy L Thompson **/
12026f8994e9SJeremy L Thompson int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name, CeedElemRestriction *rstr, CeedBasis *basis, CeedVector *vec) {
1203ab747706SJeremy L Thompson   if (field_name) CeedCall(CeedOperatorFieldGetName(op_field, field_name));
1204ab747706SJeremy L Thompson   if (rstr) CeedCall(CeedOperatorFieldGetElemRestriction(op_field, rstr));
1205ab747706SJeremy L Thompson   if (basis) CeedCall(CeedOperatorFieldGetBasis(op_field, basis));
1206ab747706SJeremy L Thompson   if (vec) CeedCall(CeedOperatorFieldGetVector(op_field, vec));
1207ab747706SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1208ab747706SJeremy L Thompson }
1209ab747706SJeremy L Thompson 
1210ab747706SJeremy L Thompson /**
1211ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1212288c0443SJeremy L Thompson 
1213ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1214ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
121552d6035fSJeremy L Thompson 
121652d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
121752d6035fSJeremy L Thompson 
12187a982d89SJeremy L. Thompson   @ref User
121952d6035fSJeremy L Thompson  */
12202b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
12211203703bSJeremy L Thompson   bool is_immutable;
12221203703bSJeremy L Thompson   Ceed ceed;
12231203703bSJeremy L Thompson 
12241203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(composite_op, &ceed));
12251203703bSJeremy L Thompson   CeedCheck(composite_op->is_composite, ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
12261203703bSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
12271203703bSJeremy L Thompson   CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable));
12281203703bSJeremy L Thompson   CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
12292b730f8bSJeremy L Thompson 
12302b730f8bSJeremy L Thompson   {
12312b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
12321c66c397SJeremy L Thompson 
12332b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
12342b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
12352b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
12362b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
12371203703bSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), ceed,
12381203703bSJeremy L Thompson               CEED_ERROR_MAJOR,
1239249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1240249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1241249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12422b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
12432b730f8bSJeremy L Thompson   }
12442b730f8bSJeremy L Thompson 
1245d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
12462b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1247d1d35e2fSjeremylt   composite_op->num_suboperators++;
1248e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
124952d6035fSJeremy L Thompson }
125052d6035fSJeremy L Thompson 
125152d6035fSJeremy L Thompson /**
1252ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
125375f0d5a4SJeremy L Thompson 
1254ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1255ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
125675f0d5a4SJeremy L Thompson 
125775f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
125875f0d5a4SJeremy L Thompson 
125975f0d5a4SJeremy L Thompson   @ref Backend
126075f0d5a4SJeremy L Thompson **/
1261c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
12621203703bSJeremy L Thompson   bool is_composite;
12631203703bSJeremy L Thompson 
12641203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12656e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
126675f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
126775f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
126875f0d5a4SJeremy L Thompson }
126975f0d5a4SJeremy L Thompson 
127075f0d5a4SJeremy L Thompson /**
1271ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
127275f0d5a4SJeremy L Thompson 
1273ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1274ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
127575f0d5a4SJeremy L Thompson 
127675f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
127775f0d5a4SJeremy L Thompson 
127875f0d5a4SJeremy L Thompson   @ref Backend
127975f0d5a4SJeremy L Thompson **/
1280c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
12811203703bSJeremy L Thompson   bool is_composite;
12821203703bSJeremy L Thompson 
12831203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12846e536b99SJeremy L Thompson   CeedCheck(is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for a composite operator");
128575f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
128675f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
128775f0d5a4SJeremy L Thompson }
128875f0d5a4SJeremy L Thompson 
128975f0d5a4SJeremy L Thompson /**
1290ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
12914db537f9SJeremy L Thompson 
1292ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
12934db537f9SJeremy L Thompson 
12944db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12954db537f9SJeremy L Thompson 
12964db537f9SJeremy L Thompson   @ref User
12974db537f9SJeremy L Thompson **/
12984db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
12991203703bSJeremy L Thompson   bool          is_at_points, is_composite;
13004db537f9SJeremy L Thompson   Ceed          ceed;
13011203703bSJeremy L Thompson   CeedQFunction qf = NULL;
13024db537f9SJeremy L Thompson 
13032b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
13044db537f9SJeremy L Thompson 
13051203703bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
13061203703bSJeremy L Thompson   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
13071203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13081203703bSJeremy L Thompson   if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf));
13091203703bSJeremy L Thompson   if (is_composite) {
13101203703bSJeremy L Thompson     CeedInt num_suboperators;
13111203703bSJeremy L Thompson 
13121203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13131203703bSJeremy L Thompson     if (!num_suboperators) {
131443622462SJeremy L Thompson       // Empty operator setup
131543622462SJeremy L Thompson       op->input_size  = 0;
131643622462SJeremy L Thompson       op->output_size = 0;
131743622462SJeremy L Thompson     } else {
13181203703bSJeremy L Thompson       CeedOperator *sub_operators;
13191203703bSJeremy L Thompson 
13201203703bSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13211203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_suboperators; i++) {
13221203703bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(sub_operators[i]));
13234db537f9SJeremy L Thompson       }
13242b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
13252b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
13262b104005SJeremy L Thompson       CeedSize input_size, output_size;
13272b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
132843622462SJeremy L Thompson     }
13294db537f9SJeremy L Thompson   } else {
13301203703bSJeremy L Thompson     CeedInt num_input_fields, num_output_fields;
13311203703bSJeremy L Thompson 
13326574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
13331203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL));
13341203703bSJeremy L Thompson     CeedCheck(op->num_fields == num_input_fields + num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
13356574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
13362a3ff1c9SZach Atkins     CeedCheck(op->num_qpts > 0 || is_at_points, ceed, CEED_ERROR_INCOMPLETE,
1337ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
13382b730f8bSJeremy L Thompson   }
13394db537f9SJeremy L Thompson 
13404db537f9SJeremy L Thompson   // Flag as immutable and ready
13414db537f9SJeremy L Thompson   op->is_interface_setup = true;
13421203703bSJeremy L Thompson   if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf));
13431203703bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf));
13441203703bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT));
13454db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13464db537f9SJeremy L Thompson }
13474db537f9SJeremy L Thompson 
13484db537f9SJeremy L Thompson /**
1349ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
13504385fb7fSSebastian Grimberg 
1351ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1352c9366a6bSJeremy L Thompson 
1353ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1354ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1355ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1356c9366a6bSJeremy L Thompson 
1357c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1358c9366a6bSJeremy L Thompson 
1359c9366a6bSJeremy L Thompson   @ref User
1360c9366a6bSJeremy L Thompson **/
13612b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1362c9366a6bSJeremy L Thompson   bool is_composite;
1363c9366a6bSJeremy L Thompson 
13642b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
13652b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1366c9366a6bSJeremy L Thompson 
13672b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13682b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
13691203703bSJeremy L Thompson     CeedInt       num_suboperators;
13701203703bSJeremy L Thompson     CeedOperator *sub_operators;
13711203703bSJeremy L Thompson 
13721203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13731203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13741203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
1375c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
13761c66c397SJeremy L Thompson 
13771203703bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(sub_operators[i], &sub_input_size, &sub_output_size));
13782b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
13792b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
13802b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
13816e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
13826e536b99SJeremy L Thompson                 CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR,
1383249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1384249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1385249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
13862b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1387c9366a6bSJeremy L Thompson     }
13882b730f8bSJeremy L Thompson   }
1389c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1390c9366a6bSJeremy L Thompson }
1391c9366a6bSJeremy L Thompson 
1392c9366a6bSJeremy L Thompson /**
1393ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
13944385fb7fSSebastian Grimberg 
1395ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1396ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
13978b919e6bSJeremy L Thompson 
1398ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1399beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
14008b919e6bSJeremy L Thompson 
14018b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14028b919e6bSJeremy L Thompson 
14038b919e6bSJeremy L Thompson   @ref Advanced
14048b919e6bSJeremy L Thompson **/
14052b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
14068b919e6bSJeremy L Thompson   bool is_composite;
14078b919e6bSJeremy L Thompson 
14082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14098b919e6bSJeremy L Thompson   if (is_composite) {
14108b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
14112b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
14128b919e6bSJeremy L Thompson     }
14138b919e6bSJeremy L Thompson   } else {
14142b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1415beecbf24SJeremy L Thompson   }
1416beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1417beecbf24SJeremy L Thompson }
1418beecbf24SJeremy L Thompson 
1419beecbf24SJeremy L Thompson /**
1420ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1421beecbf24SJeremy L Thompson 
1422ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
14236e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1424beecbf24SJeremy L Thompson 
1425beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1426beecbf24SJeremy L Thompson 
1427beecbf24SJeremy L Thompson   @ref Advanced
1428beecbf24SJeremy L Thompson **/
14292b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1430beecbf24SJeremy L Thompson   bool is_composite;
1431beecbf24SJeremy L Thompson 
14322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1433beecbf24SJeremy L Thompson   if (is_composite) {
14341203703bSJeremy L Thompson     CeedInt       num_suboperators;
14351203703bSJeremy L Thompson     CeedOperator *sub_operators;
14361203703bSJeremy L Thompson 
14371203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14381203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14391203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
14401203703bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(sub_operators[i], needs_data_update));
1441beecbf24SJeremy L Thompson     }
1442beecbf24SJeremy L Thompson   } else {
14432b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
14448b919e6bSJeremy L Thompson   }
14458b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14468b919e6bSJeremy L Thompson }
14478b919e6bSJeremy L Thompson 
14488b919e6bSJeremy L Thompson /**
1449ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1450ea6b5821SJeremy L Thompson 
1451ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1452ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1453ea6b5821SJeremy L Thompson 
1454ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1455ea6b5821SJeremy L Thompson 
1456ea6b5821SJeremy L Thompson   @ref User
1457ea6b5821SJeremy L Thompson **/
1458ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1459ea6b5821SJeremy L Thompson   char  *name_copy;
1460ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1461ea6b5821SJeremy L Thompson 
14622b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1463ea6b5821SJeremy L Thompson   if (name_len > 0) {
14642b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1465ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1466ea6b5821SJeremy L Thompson     op->name = name_copy;
1467ea6b5821SJeremy L Thompson   }
1468ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1469ea6b5821SJeremy L Thompson }
1470ea6b5821SJeremy L Thompson 
1471ea6b5821SJeremy L Thompson /**
1472*935f026aSJeremy L Thompson   @brief Core logic for viewing a `CeedOperator`
14737a982d89SJeremy L. Thompson 
1474*935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1475ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
14767a982d89SJeremy L. Thompson 
14777a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
14787a982d89SJeremy L. Thompson 
14797a982d89SJeremy L. Thompson   @ref User
14807a982d89SJeremy L. Thompson **/
1481*935f026aSJeremy L Thompson int CeedOperatorView_Core(CeedOperator op, FILE *stream, bool is_full) {
14821203703bSJeremy L Thompson   bool has_name = op->name, is_composite;
14837a982d89SJeremy L. Thompson 
14841203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14851203703bSJeremy L Thompson   if (is_composite) {
14861203703bSJeremy L Thompson     CeedInt       num_suboperators;
14871203703bSJeremy L Thompson     CeedOperator *sub_operators;
14881203703bSJeremy L Thompson 
14891203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
14901203703bSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14912b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
14927a982d89SJeremy L. Thompson 
14931203703bSJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
14941203703bSJeremy L Thompson       has_name = sub_operators[i]->name;
1495*935f026aSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s%s\n", i, has_name ? " - " : "", has_name ? sub_operators[i]->name : "", is_full ? ":" : "");
1496*935f026aSJeremy L Thompson       if (is_full) CeedCall(CeedOperatorSingleView(sub_operators[i], 1, stream));
14977a982d89SJeremy L. Thompson     }
14987a982d89SJeremy L. Thompson   } else {
14992b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1500*935f026aSJeremy L Thompson     if (is_full) CeedCall(CeedOperatorSingleView(op, 0, stream));
15017a982d89SJeremy L. Thompson   }
1502e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15037a982d89SJeremy L. Thompson }
15043bd813ffSjeremylt 
15053bd813ffSjeremylt /**
1506*935f026aSJeremy L Thompson   @brief View a `CeedOperator`
1507*935f026aSJeremy L Thompson 
1508*935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1509*935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1510*935f026aSJeremy L Thompson 
1511*935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1512*935f026aSJeremy L Thompson 
1513*935f026aSJeremy L Thompson   @ref User
1514*935f026aSJeremy L Thompson **/
1515*935f026aSJeremy L Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1516*935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, true));
1517*935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1518*935f026aSJeremy L Thompson }
1519*935f026aSJeremy L Thompson 
1520*935f026aSJeremy L Thompson /**
1521*935f026aSJeremy L Thompson   @brief View a brief summary `CeedOperator`
1522*935f026aSJeremy L Thompson 
1523*935f026aSJeremy L Thompson   @param[in] op     `CeedOperator` to view brief summary
1524*935f026aSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1525*935f026aSJeremy L Thompson 
1526*935f026aSJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1527*935f026aSJeremy L Thompson 
1528*935f026aSJeremy L Thompson   @ref User
1529*935f026aSJeremy L Thompson **/
1530*935f026aSJeremy L Thompson int CeedOperatorViewTerse(CeedOperator op, FILE *stream) {
1531*935f026aSJeremy L Thompson   CeedCall(CeedOperatorView_Core(op, stream, false));
1532*935f026aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1533*935f026aSJeremy L Thompson }
1534*935f026aSJeremy L Thompson 
1535*935f026aSJeremy L Thompson /**
1536ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1537b7c9bbdaSJeremy L Thompson 
1538ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1539ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1540b7c9bbdaSJeremy L Thompson 
1541b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1542b7c9bbdaSJeremy L Thompson 
1543b7c9bbdaSJeremy L Thompson   @ref Advanced
1544b7c9bbdaSJeremy L Thompson **/
1545b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
15466e536b99SJeremy L Thompson   *ceed = CeedOperatorReturnCeed(op);
1547b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1548b7c9bbdaSJeremy L Thompson }
1549b7c9bbdaSJeremy L Thompson 
1550b7c9bbdaSJeremy L Thompson /**
15516e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
15526e536b99SJeremy L Thompson 
15536e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
15546e536b99SJeremy L Thompson 
15556e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
15566e536b99SJeremy L Thompson 
15576e536b99SJeremy L Thompson   @ref Advanced
15586e536b99SJeremy L Thompson **/
15596e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
15606e536b99SJeremy L Thompson 
15616e536b99SJeremy L Thompson /**
1562ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1563b7c9bbdaSJeremy L Thompson 
1564ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1565b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1566b7c9bbdaSJeremy L Thompson 
1567b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1568b7c9bbdaSJeremy L Thompson 
1569b7c9bbdaSJeremy L Thompson   @ref Advanced
1570b7c9bbdaSJeremy L Thompson **/
1571b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
15721203703bSJeremy L Thompson   bool is_composite;
15731203703bSJeremy L Thompson 
15741203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15756e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1576b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1577b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1578b7c9bbdaSJeremy L Thompson }
1579b7c9bbdaSJeremy L Thompson 
1580b7c9bbdaSJeremy L Thompson /**
1581ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1582b7c9bbdaSJeremy L Thompson 
1583ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1584b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1585b7c9bbdaSJeremy L Thompson 
1586b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1587b7c9bbdaSJeremy L Thompson 
1588b7c9bbdaSJeremy L Thompson   @ref Advanced
1589b7c9bbdaSJeremy L Thompson **/
1590b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
15911203703bSJeremy L Thompson   bool is_composite;
15921203703bSJeremy L Thompson 
15931203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15946e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1595b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1596b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1597b7c9bbdaSJeremy L Thompson }
1598b7c9bbdaSJeremy L Thompson 
1599b7c9bbdaSJeremy L Thompson /**
1600ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
16016e15d496SJeremy L Thompson 
1602ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1603ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
16046e15d496SJeremy L Thompson 
16056e15d496SJeremy L Thompson   @ref Backend
16066e15d496SJeremy L Thompson **/
16079d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
16086e15d496SJeremy L Thompson   bool is_composite;
16091c66c397SJeremy L Thompson 
16102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
16116e15d496SJeremy L Thompson 
16126e15d496SJeremy L Thompson   *flops = 0;
16132b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
16146e15d496SJeremy L Thompson   if (is_composite) {
16156e15d496SJeremy L Thompson     CeedInt num_suboperators;
16161c66c397SJeremy L Thompson 
1617c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
16186e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1619c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
16206e15d496SJeremy L Thompson 
16216e15d496SJeremy L Thompson     // FLOPs for each suboperator
16226e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
16239d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
16241c66c397SJeremy L Thompson 
16252b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
16266e15d496SJeremy L Thompson       *flops += suboperator_flops;
16276e15d496SJeremy L Thompson     }
16286e15d496SJeremy L Thompson   } else {
16291c66c397SJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0;
16301203703bSJeremy L Thompson     CeedQFunction       qf;
16311203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
16321203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
16331c66c397SJeremy L Thompson 
16341203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
16351203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
16361203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
16372b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
16384385fb7fSSebastian Grimberg 
16396e15d496SJeremy L Thompson     // Input FLOPs
16406e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
16411203703bSJeremy L Thompson       CeedVector vec;
16426e15d496SJeremy L Thompson 
16431203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
16441203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
16451203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
16461203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
16471203703bSJeremy L Thompson         CeedElemRestriction rstr;
16481203703bSJeremy L Thompson         CeedBasis           basis;
16491203703bSJeremy L Thompson 
16501203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
16511203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1652edb2538eSJeremy L Thompson         *flops += rstr_flops;
16531203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
16541203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
16551203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, &basis_flops));
16566e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
16576e15d496SJeremy L Thompson       }
16586e15d496SJeremy L Thompson     }
16596e15d496SJeremy L Thompson     // QF FLOPs
16601c66c397SJeremy L Thompson     {
16619d36ca50SJeremy L Thompson       CeedInt       num_qpts;
16629d36ca50SJeremy L Thompson       CeedSize      qf_flops;
16631203703bSJeremy L Thompson       CeedQFunction qf;
16641c66c397SJeremy L Thompson 
16652b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
16661203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
16671203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
16686e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
16696e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
16706e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
16711c66c397SJeremy L Thompson     }
16721c66c397SJeremy L Thompson 
16736e15d496SJeremy L Thompson     // Output FLOPs
16746e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
16751203703bSJeremy L Thompson       CeedVector vec;
16766e15d496SJeremy L Thompson 
16771203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
16781203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
16791203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
16801203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
16811203703bSJeremy L Thompson         CeedElemRestriction rstr;
16821203703bSJeremy L Thompson         CeedBasis           basis;
16831203703bSJeremy L Thompson 
16841203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
16851203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1686edb2538eSJeremy L Thompson         *flops += rstr_flops;
16871203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
16881203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
16891203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, &basis_flops));
16906e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
16916e15d496SJeremy L Thompson       }
16926e15d496SJeremy L Thompson     }
16936e15d496SJeremy L Thompson   }
16946e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16956e15d496SJeremy L Thompson }
16966e15d496SJeremy L Thompson 
16976e15d496SJeremy L Thompson /**
1698ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
16999fd66db6SSebastian Grimberg 
1700ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1701512bb800SJeremy L Thompson 
1702ca94c3ddSJeremy 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`.
1703ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
17040126412dSJeremy L Thompson 
1705ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1706ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
17070126412dSJeremy L Thompson 
17080126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17090126412dSJeremy L Thompson 
1710859c15bbSJames Wright   @ref Advanced
17110126412dSJeremy L Thompson **/
17120126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
17131203703bSJeremy L Thompson   bool                 is_composite;
17141203703bSJeremy L Thompson   CeedQFunction        qf;
17151203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
17161203703bSJeremy L Thompson 
17171203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17186e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
17191203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
17201203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
17211203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
17220126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
17230126412dSJeremy L Thompson }
17240126412dSJeremy L Thompson 
17250126412dSJeremy L Thompson /**
1726ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
17273668ca4bSJeremy L Thompson 
1728ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1729859c15bbSJames Wright 
1730ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
17313668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
17323668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
17333668ca4bSJeremy L Thompson 
17343668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17353668ca4bSJeremy L Thompson 
17363668ca4bSJeremy L Thompson   @ref User
17373668ca4bSJeremy L Thompson **/
173817b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
17391c66c397SJeremy L Thompson   bool is_composite, field_found = false;
17401c66c397SJeremy L Thompson 
17412b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17422b730f8bSJeremy L Thompson 
17433668ca4bSJeremy L Thompson   if (is_composite) {
1744a98a090bSJeremy L Thompson     // Composite operator
1745a98a090bSJeremy L Thompson     // -- Check if composite label already created
17463668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
17473668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
17483668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
17493668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
17503668ca4bSJeremy L Thompson       }
17513668ca4bSJeremy L Thompson     }
17523668ca4bSJeremy L Thompson 
1753a98a090bSJeremy L Thompson     // -- Create composite label if needed
17543668ca4bSJeremy L Thompson     CeedInt               num_sub;
17553668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
17563668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
17573668ca4bSJeremy L Thompson 
17582b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1759c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1760c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
17612b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
17623668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
17633668ca4bSJeremy L Thompson 
17643668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
17653668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
17663668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
17671c66c397SJeremy L Thompson 
17682b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
17693668ca4bSJeremy L Thompson         if (new_field_label_i) {
1770a98a090bSJeremy L Thompson           field_found                    = true;
17713668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
17723668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
17733668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
17742b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
17757bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
17762b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
17776e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
17782b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
17797bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
17807bfe0f0eSJeremy L Thompson           } else {
17817bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
17827bfe0f0eSJeremy L Thompson           }
17832b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
17847bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
17852b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
17866e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
17876e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
17886e536b99SJeremy L Thompson                              new_field_label_i->num_values);
17897bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
17907bfe0f0eSJeremy L Thompson           } else {
17917bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
17927bfe0f0eSJeremy L Thompson           }
17933668ca4bSJeremy L Thompson         }
17943668ca4bSJeremy L Thompson       }
17953668ca4bSJeremy L Thompson     }
1796a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1797a98a090bSJeremy L Thompson     if (field_found) {
1798a98a090bSJeremy L Thompson       *field_label = new_field_label;
1799a98a090bSJeremy L Thompson     } else {
18003668ca4bSJeremy L Thompson       // LCOV_EXCL_START
18012b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
18022b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
18033668ca4bSJeremy L Thompson       *field_label = NULL;
18043668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
18055ac9af79SJeremy L Thompson     }
18065ac9af79SJeremy L Thompson   } else {
18071203703bSJeremy L Thompson     CeedQFunction        qf;
18081203703bSJeremy L Thompson     CeedQFunctionContext ctx;
18091203703bSJeremy L Thompson 
1810a98a090bSJeremy L Thompson     // Single, non-composite operator
18111203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
18121203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
18131203703bSJeremy L Thompson     if (ctx) {
18141203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1815a98a090bSJeremy L Thompson     } else {
1816a98a090bSJeremy L Thompson       *field_label = NULL;
1817a98a090bSJeremy L Thompson     }
18185ac9af79SJeremy L Thompson   }
18195ac9af79SJeremy L Thompson 
18205ac9af79SJeremy L Thompson   // Set label in operator
18215ac9af79SJeremy L Thompson   if (*field_label) {
18225ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
18235ac9af79SJeremy L Thompson 
18243668ca4bSJeremy L Thompson     // Move new composite label to operator
18253668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
18262b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
18273668ca4bSJeremy L Thompson       op->max_context_labels = 1;
18283668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
18292b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
18303668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
18313668ca4bSJeremy L Thompson     }
18325ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
18333668ca4bSJeremy L Thompson     op->num_context_labels++;
18343668ca4bSJeremy L Thompson   }
18353668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
18363668ca4bSJeremy L Thompson }
18373668ca4bSJeremy L Thompson 
18383668ca4bSJeremy L Thompson /**
1839ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
18404385fb7fSSebastian Grimberg 
1841ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1842d8dd9a91SJeremy L Thompson 
1843ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
18442788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1845ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1846d8dd9a91SJeremy L Thompson 
1847d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1848d8dd9a91SJeremy L Thompson 
1849d8dd9a91SJeremy L Thompson   @ref User
1850d8dd9a91SJeremy L Thompson **/
185117b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
18522b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1853d8dd9a91SJeremy L Thompson }
1854d8dd9a91SJeremy L Thompson 
1855d8dd9a91SJeremy L Thompson /**
1856ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
18574385fb7fSSebastian Grimberg 
1858ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
18592788fa27SJeremy L Thompson 
1860ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18612788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
18622788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
18632788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18642788fa27SJeremy L Thompson 
18652788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18662788fa27SJeremy L Thompson 
18672788fa27SJeremy L Thompson   @ref User
18682788fa27SJeremy L Thompson **/
186917b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
18702788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
18712788fa27SJeremy L Thompson }
18722788fa27SJeremy L Thompson 
18732788fa27SJeremy L Thompson /**
1874ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
18752788fa27SJeremy L Thompson 
1876ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18772788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
18782788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18792788fa27SJeremy L Thompson 
18802788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18812788fa27SJeremy L Thompson 
18822788fa27SJeremy L Thompson   @ref User
18832788fa27SJeremy L Thompson **/
188417b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
18852788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
18862788fa27SJeremy L Thompson }
18872788fa27SJeremy L Thompson 
18882788fa27SJeremy L Thompson /**
1889ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
18904385fb7fSSebastian Grimberg 
1891ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1892d8dd9a91SJeremy L Thompson 
1893ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1894ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1895ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1896d8dd9a91SJeremy L Thompson 
1897d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1898d8dd9a91SJeremy L Thompson 
1899d8dd9a91SJeremy L Thompson   @ref User
1900d8dd9a91SJeremy L Thompson **/
190123dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
19022b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1903d8dd9a91SJeremy L Thompson }
1904d8dd9a91SJeremy L Thompson 
1905d8dd9a91SJeremy L Thompson /**
1906ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
19074385fb7fSSebastian Grimberg 
1908ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19092788fa27SJeremy L Thompson 
1910ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19112788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1912ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
19132788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19142788fa27SJeremy L Thompson 
19152788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19162788fa27SJeremy L Thompson 
19172788fa27SJeremy L Thompson   @ref User
19182788fa27SJeremy L Thompson **/
191923dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
19202788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
19212788fa27SJeremy L Thompson }
19222788fa27SJeremy L Thompson 
19232788fa27SJeremy L Thompson /**
1924ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
19252788fa27SJeremy L Thompson 
1926ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19272788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
19282788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
19292788fa27SJeremy L Thompson 
19302788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19312788fa27SJeremy L Thompson 
19322788fa27SJeremy L Thompson   @ref User
19332788fa27SJeremy L Thompson **/
193423dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
19352788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
19362788fa27SJeremy L Thompson }
19372788fa27SJeremy L Thompson 
19382788fa27SJeremy L Thompson /**
1939ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
19405b6ec284SJeremy L Thompson 
1941ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
19425b6ec284SJeremy L Thompson 
1943ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19445b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
19455b6ec284SJeremy L Thompson   @param[in]     values      Values to set
19465b6ec284SJeremy L Thompson 
19475b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19485b6ec284SJeremy L Thompson 
19495b6ec284SJeremy L Thompson   @ref User
19505b6ec284SJeremy L Thompson **/
19515b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
19525b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
19535b6ec284SJeremy L Thompson }
19545b6ec284SJeremy L Thompson 
19555b6ec284SJeremy L Thompson /**
1956ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
19575b6ec284SJeremy L Thompson 
1958ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19595b6ec284SJeremy L Thompson 
1960ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19615b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
1962ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
19635b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
19645b6ec284SJeremy L Thompson 
19655b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19665b6ec284SJeremy L Thompson 
19675b6ec284SJeremy L Thompson   @ref User
19685b6ec284SJeremy L Thompson **/
19695b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
19705b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
19715b6ec284SJeremy L Thompson }
19725b6ec284SJeremy L Thompson 
19735b6ec284SJeremy L Thompson /**
1974ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
19755b6ec284SJeremy L Thompson 
1976ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19775b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
19785b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
19795b6ec284SJeremy L Thompson 
19805b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19815b6ec284SJeremy L Thompson 
19825b6ec284SJeremy L Thompson   @ref User
19835b6ec284SJeremy L Thompson **/
19845b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
19855b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
19865b6ec284SJeremy L Thompson }
19875b6ec284SJeremy L Thompson 
19885b6ec284SJeremy L Thompson /**
1989ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
1990d7b241e6Sjeremylt 
1991ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1992ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1993d7b241e6Sjeremylt 
1994ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1995f04ea552SJeremy L Thompson 
1996ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1997ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1998ca94c3ddSJeremy 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
1999ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2000b11c1e72Sjeremylt 
2001b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2002dfdf5a53Sjeremylt 
20037a982d89SJeremy L. Thompson   @ref User
2004b11c1e72Sjeremylt **/
20052b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
20061203703bSJeremy L Thompson   bool is_composite;
20071203703bSJeremy L Thompson 
20082b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2009d7b241e6Sjeremylt 
20101203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
20111203703bSJeremy L Thompson   if (is_composite) {
2012250756a7Sjeremylt     // Composite Operator
2013250756a7Sjeremylt     if (op->ApplyComposite) {
20142b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
2015250756a7Sjeremylt     } else {
2016d1d35e2fSjeremylt       CeedInt       num_suboperators;
2017d1d35e2fSjeremylt       CeedOperator *sub_operators;
20181c66c397SJeremy L Thompson 
20191c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2020c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2021250756a7Sjeremylt 
2022250756a7Sjeremylt       // Zero all output vectors
20233ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2024d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
20251203703bSJeremy L Thompson         CeedInt            num_output_fields;
20261203703bSJeremy L Thompson         CeedOperatorField *output_fields;
20271c66c397SJeremy L Thompson 
20281203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
20291203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
20301203703bSJeremy L Thompson           CeedVector vec;
20311203703bSJeremy L Thompson 
20321203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2033250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
20342b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2035250756a7Sjeremylt           }
2036250756a7Sjeremylt         }
2037250756a7Sjeremylt       }
2038250756a7Sjeremylt       // Apply
2039f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2040f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2041cae8b89aSjeremylt       }
2042cae8b89aSjeremylt     }
20433ca2b39bSJeremy L Thompson   } else {
20443ca2b39bSJeremy L Thompson     // Standard Operator
20453ca2b39bSJeremy L Thompson     if (op->Apply) {
20463ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
20473ca2b39bSJeremy L Thompson     } else {
20481203703bSJeremy L Thompson       CeedInt            num_output_fields;
20491203703bSJeremy L Thompson       CeedOperatorField *output_fields;
20501203703bSJeremy L Thompson 
20511203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
20523ca2b39bSJeremy L Thompson       // Zero all output vectors
20531203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
20541203703bSJeremy L Thompson         CeedVector vec;
20551c66c397SJeremy L Thompson 
20561203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
20573ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
20583ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
20593ca2b39bSJeremy L Thompson       }
20603ca2b39bSJeremy L Thompson       // Apply
20611203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
20623ca2b39bSJeremy L Thompson     }
2063250756a7Sjeremylt   }
2064e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2065cae8b89aSjeremylt }
2066cae8b89aSjeremylt 
2067cae8b89aSjeremylt /**
2068ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2069cae8b89aSjeremylt 
2070ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2071ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2072cae8b89aSjeremylt 
2073ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2074ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2075ca94c3ddSJeremy 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
2076ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2077cae8b89aSjeremylt 
2078cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2079cae8b89aSjeremylt 
20807a982d89SJeremy L. Thompson   @ref User
2081cae8b89aSjeremylt **/
20822b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
20831203703bSJeremy L Thompson   bool is_composite;
20841203703bSJeremy L Thompson 
20852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2086cae8b89aSjeremylt 
20871203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
20881203703bSJeremy L Thompson   if (is_composite) {
2089250756a7Sjeremylt     // Composite Operator
2090250756a7Sjeremylt     if (op->ApplyAddComposite) {
20912b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2092cae8b89aSjeremylt     } else {
2093d1d35e2fSjeremylt       CeedInt       num_suboperators;
2094d1d35e2fSjeremylt       CeedOperator *sub_operators;
2095250756a7Sjeremylt 
20961c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
20971c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2098d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
20992b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
21001d7d2407SJeremy L Thompson       }
2101250756a7Sjeremylt     }
21021203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
21033ca2b39bSJeremy L Thompson     // Standard Operator
21043ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2105250756a7Sjeremylt   }
2106e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2107d7b241e6Sjeremylt }
2108d7b241e6Sjeremylt 
2109d7b241e6Sjeremylt /**
2110ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2111d7b241e6Sjeremylt 
2112ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2113b11c1e72Sjeremylt 
2114b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2115dfdf5a53Sjeremylt 
21167a982d89SJeremy L. Thompson   @ref User
2117b11c1e72Sjeremylt **/
2118d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2119ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2120ad6481ceSJeremy L Thompson     *op = NULL;
2121ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2122ad6481ceSJeremy L Thompson   }
21232b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
21242b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
2125fe2413ffSjeremylt   // Free fields
21262b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2127d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2128437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2129437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
213015910d16Sjeremylt       }
2131356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
21322b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
213371352a93Sjeremylt       }
21342b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
21352b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
213671352a93Sjeremylt       }
21372b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
21382b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2139fe2413ffSjeremylt     }
21402b730f8bSJeremy L Thompson   }
21412b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2142d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2143437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2144356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
21452b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
214671352a93Sjeremylt       }
21472b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
21482b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
214971352a93Sjeremylt       }
21502b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
21512b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
21522b730f8bSJeremy L Thompson     }
2153fe2413ffSjeremylt   }
215448acf710SJeremy L Thompson   // AtPoints data
215548acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
215648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
215748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2158d1d35e2fSjeremylt   // Destroy sub_operators
21592b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2160d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
21612b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
216252d6035fSJeremy L Thompson     }
21632b730f8bSJeremy L Thompson   }
21642b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
21652b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
21662b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
21673668ca4bSJeremy L Thompson   // Destroy any composite labels
21685ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
21693668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
21702b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
21712b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
21723668ca4bSJeremy L Thompson     }
21735ac9af79SJeremy L Thompson   }
21742b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2175fe2413ffSjeremylt 
21765107b09fSJeremy L Thompson   // Destroy fallback
21772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
21785107b09fSJeremy L Thompson 
2179ed9e99e6SJeremy L Thompson   // Destroy assembly data
21802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
21812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
218270a7ffb3SJeremy L Thompson 
21832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
21842b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
21852b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
21862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
21872b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2188e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2189d7b241e6Sjeremylt }
2190d7b241e6Sjeremylt 
2191d7b241e6Sjeremylt /// @}
2192