xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 6e536b992ff6bc401c55631f1bc4464446496b52)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
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     }
411*6e536b99SJeremy 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     }
476*6e536b99SJeremy 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));
486*6e536b99SJeremy 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));
534*6e536b99SJeremy 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));
624*6e536b99SJeremy 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"));
730ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support 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"));
773ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support 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));
1000*6e536b99SJeremy 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));
1080*6e536b99SJeremy 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));
1265*6e536b99SJeremy 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));
1284*6e536b99SJeremy 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
1381*6e536b99SJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size),
1382*6e536b99SJeremy 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 /**
1472ca94c3ddSJeremy L Thompson   @brief View a `CeedOperator`
14737a982d89SJeremy L. Thompson 
1474ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
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 **/
14817a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
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;
14951203703bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? sub_operators[i]->name : "");
14961203703bSJeremy L Thompson       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 : "");
15002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
15017a982d89SJeremy L. Thompson   }
1502e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15037a982d89SJeremy L. Thompson }
15043bd813ffSjeremylt 
15053bd813ffSjeremylt /**
1506ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1507b7c9bbdaSJeremy L Thompson 
1508ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1509ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1510b7c9bbdaSJeremy L Thompson 
1511b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1512b7c9bbdaSJeremy L Thompson 
1513b7c9bbdaSJeremy L Thompson   @ref Advanced
1514b7c9bbdaSJeremy L Thompson **/
1515b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1516*6e536b99SJeremy L Thompson   *ceed = CeedOperatorReturnCeed(op);
1517b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1518b7c9bbdaSJeremy L Thompson }
1519b7c9bbdaSJeremy L Thompson 
1520b7c9bbdaSJeremy L Thompson /**
1521*6e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedOperator`
1522*6e536b99SJeremy L Thompson 
1523*6e536b99SJeremy L Thompson   @param[in]  op `CeedOperator`
1524*6e536b99SJeremy L Thompson 
1525*6e536b99SJeremy L Thompson   @return `Ceed` associated with the `op`
1526*6e536b99SJeremy L Thompson 
1527*6e536b99SJeremy L Thompson   @ref Advanced
1528*6e536b99SJeremy L Thompson **/
1529*6e536b99SJeremy L Thompson Ceed CeedOperatorReturnCeed(CeedOperator op) { return op->ceed; }
1530*6e536b99SJeremy L Thompson 
1531*6e536b99SJeremy L Thompson /**
1532ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1533b7c9bbdaSJeremy L Thompson 
1534ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1535b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1536b7c9bbdaSJeremy L Thompson 
1537b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1538b7c9bbdaSJeremy L Thompson 
1539b7c9bbdaSJeremy L Thompson   @ref Advanced
1540b7c9bbdaSJeremy L Thompson **/
1541b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
15421203703bSJeremy L Thompson   bool is_composite;
15431203703bSJeremy L Thompson 
15441203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1545*6e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1546b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1547b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1548b7c9bbdaSJeremy L Thompson }
1549b7c9bbdaSJeremy L Thompson 
1550b7c9bbdaSJeremy L Thompson /**
1551ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1552b7c9bbdaSJeremy L Thompson 
1553ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1554b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1555b7c9bbdaSJeremy L Thompson 
1556b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1557b7c9bbdaSJeremy L Thompson 
1558b7c9bbdaSJeremy L Thompson   @ref Advanced
1559b7c9bbdaSJeremy L Thompson **/
1560b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
15611203703bSJeremy L Thompson   bool is_composite;
15621203703bSJeremy L Thompson 
15631203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1564*6e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator");
1565b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1566b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1567b7c9bbdaSJeremy L Thompson }
1568b7c9bbdaSJeremy L Thompson 
1569b7c9bbdaSJeremy L Thompson /**
1570ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
15716e15d496SJeremy L Thompson 
1572ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1573ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
15746e15d496SJeremy L Thompson 
15756e15d496SJeremy L Thompson   @ref Backend
15766e15d496SJeremy L Thompson **/
15779d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
15786e15d496SJeremy L Thompson   bool is_composite;
15791c66c397SJeremy L Thompson 
15802b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
15816e15d496SJeremy L Thompson 
15826e15d496SJeremy L Thompson   *flops = 0;
15832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
15846e15d496SJeremy L Thompson   if (is_composite) {
15856e15d496SJeremy L Thompson     CeedInt num_suboperators;
15861c66c397SJeremy L Thompson 
1587c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
15886e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1589c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
15906e15d496SJeremy L Thompson 
15916e15d496SJeremy L Thompson     // FLOPs for each suboperator
15926e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
15939d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
15941c66c397SJeremy L Thompson 
15952b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
15966e15d496SJeremy L Thompson       *flops += suboperator_flops;
15976e15d496SJeremy L Thompson     }
15986e15d496SJeremy L Thompson   } else {
15991c66c397SJeremy L Thompson     CeedInt             num_input_fields, num_output_fields, num_elem = 0;
16001203703bSJeremy L Thompson     CeedQFunction       qf;
16011203703bSJeremy L Thompson     CeedQFunctionField *qf_input_fields, *qf_output_fields;
16021203703bSJeremy L Thompson     CeedOperatorField  *op_input_fields, *op_output_fields;
16031c66c397SJeremy L Thompson 
16041203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
16051203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields));
16061203703bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields));
16072b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
16084385fb7fSSebastian Grimberg 
16096e15d496SJeremy L Thompson     // Input FLOPs
16106e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
16111203703bSJeremy L Thompson       CeedVector vec;
16126e15d496SJeremy L Thompson 
16131203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
16141203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
16151203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
16161203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
16171203703bSJeremy L Thompson         CeedElemRestriction rstr;
16181203703bSJeremy L Thompson         CeedBasis           basis;
16191203703bSJeremy L Thompson 
16201203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr));
16211203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_NOTRANSPOSE, &rstr_flops));
1622edb2538eSJeremy L Thompson         *flops += rstr_flops;
16231203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
16241203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
16251203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_NOTRANSPOSE, eval_mode, &basis_flops));
16266e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
16276e15d496SJeremy L Thompson       }
16286e15d496SJeremy L Thompson     }
16296e15d496SJeremy L Thompson     // QF FLOPs
16301c66c397SJeremy L Thompson     {
16319d36ca50SJeremy L Thompson       CeedInt       num_qpts;
16329d36ca50SJeremy L Thompson       CeedSize      qf_flops;
16331203703bSJeremy L Thompson       CeedQFunction qf;
16341c66c397SJeremy L Thompson 
16352b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
16361203703bSJeremy L Thompson       CeedCall(CeedOperatorGetQFunction(op, &qf));
16371203703bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops));
1638*6e536b99SJeremy L Thompson       CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE,
1639*6e536b99SJeremy L Thompson                 "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
16406e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
16411c66c397SJeremy L Thompson     }
16421c66c397SJeremy L Thompson 
16436e15d496SJeremy L Thompson     // Output FLOPs
16446e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
16451203703bSJeremy L Thompson       CeedVector vec;
16466e15d496SJeremy L Thompson 
16471203703bSJeremy L Thompson       CeedCall(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
16481203703bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
16491203703bSJeremy L Thompson         CeedEvalMode        eval_mode;
16501203703bSJeremy L Thompson         CeedSize            rstr_flops, basis_flops;
16511203703bSJeremy L Thompson         CeedElemRestriction rstr;
16521203703bSJeremy L Thompson         CeedBasis           basis;
16531203703bSJeremy L Thompson 
16541203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr));
16551203703bSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(rstr, CEED_TRANSPOSE, &rstr_flops));
1656edb2538eSJeremy L Thompson         *flops += rstr_flops;
16571203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
16581203703bSJeremy L Thompson         CeedCall(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
16591203703bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(basis, CEED_TRANSPOSE, eval_mode, &basis_flops));
16606e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
16616e15d496SJeremy L Thompson       }
16626e15d496SJeremy L Thompson     }
16636e15d496SJeremy L Thompson   }
16646e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16656e15d496SJeremy L Thompson }
16666e15d496SJeremy L Thompson 
16676e15d496SJeremy L Thompson /**
1668ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
16699fd66db6SSebastian Grimberg 
1670ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1671512bb800SJeremy L Thompson 
1672ca94c3ddSJeremy 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`.
1673ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
16740126412dSJeremy L Thompson 
1675ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1676ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
16770126412dSJeremy L Thompson 
16780126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16790126412dSJeremy L Thompson 
1680859c15bbSJames Wright   @ref Advanced
16810126412dSJeremy L Thompson **/
16820126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
16831203703bSJeremy L Thompson   bool                 is_composite;
16841203703bSJeremy L Thompson   CeedQFunction        qf;
16851203703bSJeremy L Thompson   CeedQFunctionContext qf_ctx;
16861203703bSJeremy L Thompson 
16871203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1688*6e536b99SJeremy L Thompson   CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
16891203703bSJeremy L Thompson   CeedCall(CeedOperatorGetQFunction(op, &qf));
16901203703bSJeremy L Thompson   CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx));
16911203703bSJeremy L Thompson   if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx));
16920126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
16930126412dSJeremy L Thompson }
16940126412dSJeremy L Thompson 
16950126412dSJeremy L Thompson /**
1696ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
16973668ca4bSJeremy L Thompson 
1698ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1699859c15bbSJames Wright 
1700ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
17013668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
17023668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
17033668ca4bSJeremy L Thompson 
17043668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17053668ca4bSJeremy L Thompson 
17063668ca4bSJeremy L Thompson   @ref User
17073668ca4bSJeremy L Thompson **/
170817b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
17091c66c397SJeremy L Thompson   bool is_composite, field_found = false;
17101c66c397SJeremy L Thompson 
17112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
17122b730f8bSJeremy L Thompson 
17133668ca4bSJeremy L Thompson   if (is_composite) {
1714a98a090bSJeremy L Thompson     // Composite operator
1715a98a090bSJeremy L Thompson     // -- Check if composite label already created
17163668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
17173668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
17183668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
17193668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
17203668ca4bSJeremy L Thompson       }
17213668ca4bSJeremy L Thompson     }
17223668ca4bSJeremy L Thompson 
1723a98a090bSJeremy L Thompson     // -- Create composite label if needed
17243668ca4bSJeremy L Thompson     CeedInt               num_sub;
17253668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
17263668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
17273668ca4bSJeremy L Thompson 
17282b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1729c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1730c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
17312b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
17323668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
17333668ca4bSJeremy L Thompson 
17343668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
17353668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
17363668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
17371c66c397SJeremy L Thompson 
17382b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
17393668ca4bSJeremy L Thompson         if (new_field_label_i) {
1740a98a090bSJeremy L Thompson           field_found                    = true;
17413668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
17423668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
17433668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
17442b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
17457bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
17462b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
1747*6e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
17482b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
17497bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
17507bfe0f0eSJeremy L Thompson           } else {
17517bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
17527bfe0f0eSJeremy L Thompson           }
17532b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
17547bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
17552b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
1756*6e536b99SJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE,
1757*6e536b99SJeremy L Thompson                              "Incompatible field number of values on sub-operator contexts. %zu != %zu", new_field_label->num_values,
1758*6e536b99SJeremy L Thompson                              new_field_label_i->num_values);
17597bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
17607bfe0f0eSJeremy L Thompson           } else {
17617bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
17627bfe0f0eSJeremy L Thompson           }
17633668ca4bSJeremy L Thompson         }
17643668ca4bSJeremy L Thompson       }
17653668ca4bSJeremy L Thompson     }
1766a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1767a98a090bSJeremy L Thompson     if (field_found) {
1768a98a090bSJeremy L Thompson       *field_label = new_field_label;
1769a98a090bSJeremy L Thompson     } else {
17703668ca4bSJeremy L Thompson       // LCOV_EXCL_START
17712b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
17722b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
17733668ca4bSJeremy L Thompson       *field_label = NULL;
17743668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
17755ac9af79SJeremy L Thompson     }
17765ac9af79SJeremy L Thompson   } else {
17771203703bSJeremy L Thompson     CeedQFunction        qf;
17781203703bSJeremy L Thompson     CeedQFunctionContext ctx;
17791203703bSJeremy L Thompson 
1780a98a090bSJeremy L Thompson     // Single, non-composite operator
17811203703bSJeremy L Thompson     CeedCall(CeedOperatorGetQFunction(op, &qf));
17821203703bSJeremy L Thompson     CeedCall(CeedQFunctionGetInnerContext(qf, &ctx));
17831203703bSJeremy L Thompson     if (ctx) {
17841203703bSJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label));
1785a98a090bSJeremy L Thompson     } else {
1786a98a090bSJeremy L Thompson       *field_label = NULL;
1787a98a090bSJeremy L Thompson     }
17885ac9af79SJeremy L Thompson   }
17895ac9af79SJeremy L Thompson 
17905ac9af79SJeremy L Thompson   // Set label in operator
17915ac9af79SJeremy L Thompson   if (*field_label) {
17925ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
17935ac9af79SJeremy L Thompson 
17943668ca4bSJeremy L Thompson     // Move new composite label to operator
17953668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
17962b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
17973668ca4bSJeremy L Thompson       op->max_context_labels = 1;
17983668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
17992b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
18003668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
18013668ca4bSJeremy L Thompson     }
18025ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
18033668ca4bSJeremy L Thompson     op->num_context_labels++;
18043668ca4bSJeremy L Thompson   }
18053668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
18063668ca4bSJeremy L Thompson }
18073668ca4bSJeremy L Thompson 
18083668ca4bSJeremy L Thompson /**
1809ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
18104385fb7fSSebastian Grimberg 
1811ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1812d8dd9a91SJeremy L Thompson 
1813ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
18142788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1815ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1816d8dd9a91SJeremy L Thompson 
1817d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1818d8dd9a91SJeremy L Thompson 
1819d8dd9a91SJeremy L Thompson   @ref User
1820d8dd9a91SJeremy L Thompson **/
182117b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
18222b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1823d8dd9a91SJeremy L Thompson }
1824d8dd9a91SJeremy L Thompson 
1825d8dd9a91SJeremy L Thompson /**
1826ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
18274385fb7fSSebastian Grimberg 
1828ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
18292788fa27SJeremy L Thompson 
1830ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18312788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
18322788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
18332788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18342788fa27SJeremy L Thompson 
18352788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18362788fa27SJeremy L Thompson 
18372788fa27SJeremy L Thompson   @ref User
18382788fa27SJeremy L Thompson **/
183917b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
18402788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
18412788fa27SJeremy L Thompson }
18422788fa27SJeremy L Thompson 
18432788fa27SJeremy L Thompson /**
1844ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
18452788fa27SJeremy L Thompson 
1846ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18472788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
18482788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18492788fa27SJeremy L Thompson 
18502788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18512788fa27SJeremy L Thompson 
18522788fa27SJeremy L Thompson   @ref User
18532788fa27SJeremy L Thompson **/
185417b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
18552788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
18562788fa27SJeremy L Thompson }
18572788fa27SJeremy L Thompson 
18582788fa27SJeremy L Thompson /**
1859ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
18604385fb7fSSebastian Grimberg 
1861ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1862d8dd9a91SJeremy L Thompson 
1863ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1864ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1865ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1866d8dd9a91SJeremy L Thompson 
1867d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1868d8dd9a91SJeremy L Thompson 
1869d8dd9a91SJeremy L Thompson   @ref User
1870d8dd9a91SJeremy L Thompson **/
187123dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
18722b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1873d8dd9a91SJeremy L Thompson }
1874d8dd9a91SJeremy L Thompson 
1875d8dd9a91SJeremy L Thompson /**
1876ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
18774385fb7fSSebastian Grimberg 
1878ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
18792788fa27SJeremy L Thompson 
1880ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18812788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1882ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
18832788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18842788fa27SJeremy L Thompson 
18852788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
18862788fa27SJeremy L Thompson 
18872788fa27SJeremy L Thompson   @ref User
18882788fa27SJeremy L Thompson **/
188923dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
18902788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
18912788fa27SJeremy L Thompson }
18922788fa27SJeremy L Thompson 
18932788fa27SJeremy L Thompson /**
1894ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
18952788fa27SJeremy L Thompson 
1896ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
18972788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
18982788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
18992788fa27SJeremy L Thompson 
19002788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19012788fa27SJeremy L Thompson 
19022788fa27SJeremy L Thompson   @ref User
19032788fa27SJeremy L Thompson **/
190423dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
19052788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
19062788fa27SJeremy L Thompson }
19072788fa27SJeremy L Thompson 
19082788fa27SJeremy L Thompson /**
1909ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
19105b6ec284SJeremy L Thompson 
1911ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
19125b6ec284SJeremy L Thompson 
1913ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
19145b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
19155b6ec284SJeremy L Thompson   @param[in]     values      Values to set
19165b6ec284SJeremy L Thompson 
19175b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19185b6ec284SJeremy L Thompson 
19195b6ec284SJeremy L Thompson   @ref User
19205b6ec284SJeremy L Thompson **/
19215b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
19225b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
19235b6ec284SJeremy L Thompson }
19245b6ec284SJeremy L Thompson 
19255b6ec284SJeremy L Thompson /**
1926ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
19275b6ec284SJeremy L Thompson 
1928ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
19295b6ec284SJeremy L Thompson 
1930ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19315b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
1932ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
19335b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
19345b6ec284SJeremy L Thompson 
19355b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19365b6ec284SJeremy L Thompson 
19375b6ec284SJeremy L Thompson   @ref User
19385b6ec284SJeremy L Thompson **/
19395b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
19405b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
19415b6ec284SJeremy L Thompson }
19425b6ec284SJeremy L Thompson 
19435b6ec284SJeremy L Thompson /**
1944ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
19455b6ec284SJeremy L Thompson 
1946ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
19475b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
19485b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
19495b6ec284SJeremy L Thompson 
19505b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
19515b6ec284SJeremy L Thompson 
19525b6ec284SJeremy L Thompson   @ref User
19535b6ec284SJeremy L Thompson **/
19545b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
19555b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
19565b6ec284SJeremy L Thompson }
19575b6ec284SJeremy L Thompson 
19585b6ec284SJeremy L Thompson /**
1959ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
1960d7b241e6Sjeremylt 
1961ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1962ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1963d7b241e6Sjeremylt 
1964ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1965f04ea552SJeremy L Thompson 
1966ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1967ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1968ca94c3ddSJeremy 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
1969ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1970b11c1e72Sjeremylt 
1971b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1972dfdf5a53Sjeremylt 
19737a982d89SJeremy L. Thompson   @ref User
1974b11c1e72Sjeremylt **/
19752b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
19761203703bSJeremy L Thompson   bool is_composite;
19771203703bSJeremy L Thompson 
19782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1979d7b241e6Sjeremylt 
19801203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
19811203703bSJeremy L Thompson   if (is_composite) {
1982250756a7Sjeremylt     // Composite Operator
1983250756a7Sjeremylt     if (op->ApplyComposite) {
19842b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1985250756a7Sjeremylt     } else {
1986d1d35e2fSjeremylt       CeedInt       num_suboperators;
1987d1d35e2fSjeremylt       CeedOperator *sub_operators;
19881c66c397SJeremy L Thompson 
19891c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1990c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1991250756a7Sjeremylt 
1992250756a7Sjeremylt       // Zero all output vectors
19933ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1994d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
19951203703bSJeremy L Thompson         CeedInt            num_output_fields;
19961203703bSJeremy L Thompson         CeedOperatorField *output_fields;
19971c66c397SJeremy L Thompson 
19981203703bSJeremy L Thompson         CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
19991203703bSJeremy L Thompson         for (CeedInt j = 0; j < num_output_fields; j++) {
20001203703bSJeremy L Thompson           CeedVector vec;
20011203703bSJeremy L Thompson 
20021203703bSJeremy L Thompson           CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2003250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
20042b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
2005250756a7Sjeremylt           }
2006250756a7Sjeremylt         }
2007250756a7Sjeremylt       }
2008250756a7Sjeremylt       // Apply
2009f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
2010f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
2011cae8b89aSjeremylt       }
2012cae8b89aSjeremylt     }
20133ca2b39bSJeremy L Thompson   } else {
20143ca2b39bSJeremy L Thompson     // Standard Operator
20153ca2b39bSJeremy L Thompson     if (op->Apply) {
20163ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
20173ca2b39bSJeremy L Thompson     } else {
20181203703bSJeremy L Thompson       CeedInt            num_output_fields;
20191203703bSJeremy L Thompson       CeedOperatorField *output_fields;
20201203703bSJeremy L Thompson 
20211203703bSJeremy L Thompson       CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
20223ca2b39bSJeremy L Thompson       // Zero all output vectors
20231203703bSJeremy L Thompson       for (CeedInt i = 0; i < num_output_fields; i++) {
20241203703bSJeremy L Thompson         CeedVector vec;
20251c66c397SJeremy L Thompson 
20261203703bSJeremy L Thompson         CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
20273ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
20283ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
20293ca2b39bSJeremy L Thompson       }
20303ca2b39bSJeremy L Thompson       // Apply
20311203703bSJeremy L Thompson       if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
20323ca2b39bSJeremy L Thompson     }
2033250756a7Sjeremylt   }
2034e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2035cae8b89aSjeremylt }
2036cae8b89aSjeremylt 
2037cae8b89aSjeremylt /**
2038ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
2039cae8b89aSjeremylt 
2040ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
2041ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
2042cae8b89aSjeremylt 
2043ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
2044ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2045ca94c3ddSJeremy 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
2046ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2047cae8b89aSjeremylt 
2048cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
2049cae8b89aSjeremylt 
20507a982d89SJeremy L. Thompson   @ref User
2051cae8b89aSjeremylt **/
20522b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
20531203703bSJeremy L Thompson   bool is_composite;
20541203703bSJeremy L Thompson 
20552b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
2056cae8b89aSjeremylt 
20571203703bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
20581203703bSJeremy L Thompson   if (is_composite) {
2059250756a7Sjeremylt     // Composite Operator
2060250756a7Sjeremylt     if (op->ApplyAddComposite) {
20612b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
2062cae8b89aSjeremylt     } else {
2063d1d35e2fSjeremylt       CeedInt       num_suboperators;
2064d1d35e2fSjeremylt       CeedOperator *sub_operators;
2065250756a7Sjeremylt 
20661c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
20671c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2068d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
20692b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
20701d7d2407SJeremy L Thompson       }
2071250756a7Sjeremylt     }
20721203703bSJeremy L Thompson   } else if (op->num_elem > 0) {
20733ca2b39bSJeremy L Thompson     // Standard Operator
20743ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
2075250756a7Sjeremylt   }
2076e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2077d7b241e6Sjeremylt }
2078d7b241e6Sjeremylt 
2079d7b241e6Sjeremylt /**
2080ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
2081d7b241e6Sjeremylt 
2082ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
2083b11c1e72Sjeremylt 
2084b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
2085dfdf5a53Sjeremylt 
20867a982d89SJeremy L. Thompson   @ref User
2087b11c1e72Sjeremylt **/
2088d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
2089ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
2090ad6481ceSJeremy L Thompson     *op = NULL;
2091ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2092ad6481ceSJeremy L Thompson   }
20932b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
20942b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
2095fe2413ffSjeremylt   // Free fields
20962b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2097d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
2098437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2099437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
210015910d16Sjeremylt       }
2101356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
21022b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
210371352a93Sjeremylt       }
21042b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
21052b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
210671352a93Sjeremylt       }
21072b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
21082b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
2109fe2413ffSjeremylt     }
21102b730f8bSJeremy L Thompson   }
21112b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
2112d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
2113437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
2114356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
21152b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
211671352a93Sjeremylt       }
21172b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
21182b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
211971352a93Sjeremylt       }
21202b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
21212b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
21222b730f8bSJeremy L Thompson     }
2123fe2413ffSjeremylt   }
212448acf710SJeremy L Thompson   // AtPoints data
212548acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
212648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
212748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
2128d1d35e2fSjeremylt   // Destroy sub_operators
21292b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
2130d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
21312b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
213252d6035fSJeremy L Thompson     }
21332b730f8bSJeremy L Thompson   }
21342b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
21352b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
21362b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
21373668ca4bSJeremy L Thompson   // Destroy any composite labels
21385ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
21393668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
21402b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
21412b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
21423668ca4bSJeremy L Thompson     }
21435ac9af79SJeremy L Thompson   }
21442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
2145fe2413ffSjeremylt 
21465107b09fSJeremy L Thompson   // Destroy fallback
21472b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
21485107b09fSJeremy L Thompson 
2149ed9e99e6SJeremy L Thompson   // Destroy assembly data
21502b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
21512b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
215270a7ffb3SJeremy L Thompson 
21532b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
21542b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
21552b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
21562b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
21572b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
2158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2159d7b241e6Sjeremylt }
2160d7b241e6Sjeremylt 
2161d7b241e6Sjeremylt /// @}
2162