xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 2a3ff1c966deef86235c8d36f0c17626ea5f9ad1)
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) {
37edb2538eSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size = qf_field->size;
381c66c397SJeremy L Thompson   CeedEvalMode eval_mode = qf_field->eval_mode;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
41bafebce1SSebastian Grimberg   CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
426574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE) {
44bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
47bafebce1SSebastian Grimberg   CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
48356036faSJeremy L Thompson             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
49bafebce1SSebastian Grimberg   if (basis != CEED_BASIS_NONE) {
50bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetDimension(basis, &dim));
51bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
52bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
53bafebce1SSebastian Grimberg     CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
54ca94c3ddSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT
55ca94c3ddSJeremy L Thompson               " components, but CeedBasis has %" CeedInt_FMT " components",
56edb2538eSJeremy L Thompson               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp, num_comp);
57e15f9bd0SJeremy L Thompson   }
58e15f9bd0SJeremy L Thompson   // Field size
59d1d35e2fSjeremylt   switch (eval_mode) {
60e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
61edb2538eSJeremy L Thompson       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
62ca94c3ddSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
63edb2538eSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp);
64e15f9bd0SJeremy L Thompson       break;
65e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
66c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
67c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
68c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
696574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
70ca94c3ddSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components",
71ca94c3ddSJeremy L Thompson                 qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72e15f9bd0SJeremy L Thompson       break;
73e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
74d1d35e2fSjeremylt       // No additional checks required
75e15f9bd0SJeremy L Thompson       break;
76e15f9bd0SJeremy L Thompson   }
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
81ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedOperator`
827a982d89SJeremy L. Thompson 
83ca94c3ddSJeremy L Thompson   @param[in] field        `CeedOperator` Field to view
84ca94c3ddSJeremy L Thompson   @param[in] qf_field     `CeedQFunction` Field (carries field name)
85d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
864c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
88ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @ref Utility
937a982d89SJeremy L. Thompson **/
942b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
957a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
96d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
977a982d89SJeremy L. Thompson 
982b730f8bSJeremy L Thompson   fprintf(stream,
992b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1002b730f8bSJeremy L Thompson           ":\n"
1017a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
102d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
103990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
1042b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
105356036faSJeremy L Thompson   if (field->basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
1062b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1072b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
112ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1137a982d89SJeremy L. Thompson 
114ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1157a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
116ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1197a982d89SJeremy L. Thompson 
1207a982d89SJeremy L. Thompson   @ref Utility
1217a982d89SJeremy L. Thompson **/
1227a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1237a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1241c66c397SJeremy L Thompson   CeedInt     num_elem, num_qpts, total_fields = 0;
1257a982d89SJeremy L. Thompson 
1262b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1291c66c397SJeremy L Thompson 
1302b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1312b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1322b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
133d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1357a982d89SJeremy L. Thompson   }
1362b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
137d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1382b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1397a982d89SJeremy L. Thompson   }
140e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1417a982d89SJeremy L. Thompson }
1427a982d89SJeremy L. Thompson 
143d99fa3c5SJeremy L Thompson /**
144ca94c3ddSJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`
145eaf62fffSJeremy L Thompson 
146ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
147ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
148eaf62fffSJeremy L Thompson 
149eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150eaf62fffSJeremy L Thompson 
151eaf62fffSJeremy L Thompson   @ref Developer
152eaf62fffSJeremy L Thompson **/
153eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
154506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
155506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
156506b1a0cSSebastian Grimberg }
157506b1a0cSSebastian Grimberg 
158506b1a0cSSebastian Grimberg /**
159ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`
160506b1a0cSSebastian Grimberg 
161ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
162ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
163ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
164506b1a0cSSebastian Grimberg 
165506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
166506b1a0cSSebastian Grimberg 
167506b1a0cSSebastian Grimberg   @ref Developer
168506b1a0cSSebastian Grimberg **/
169506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1706aaed0edSJeremy L Thompson   Ceed ceed;
1711c66c397SJeremy L Thompson 
1726aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
173506b1a0cSSebastian Grimberg   if (active_input_basis) {
174506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
175506b1a0cSSebastian Grimberg     if (!op->is_composite) {
1762b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
177eaf62fffSJeremy L Thompson         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
178506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_basis || *active_input_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR,
179506b1a0cSSebastian Grimberg                     "Multiple active input CeedBases found");
180506b1a0cSSebastian Grimberg           *active_input_basis = op->input_fields[i]->basis;
181eaf62fffSJeremy L Thompson         }
1822b730f8bSJeremy L Thompson       }
183506b1a0cSSebastian Grimberg       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
184506b1a0cSSebastian Grimberg     }
185506b1a0cSSebastian Grimberg   }
186506b1a0cSSebastian Grimberg   if (active_output_basis) {
187506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
188506b1a0cSSebastian Grimberg     if (!op->is_composite) {
189506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
190506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
191506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_basis || *active_output_basis == op->output_fields[i]->basis, ceed, CEED_ERROR_MINOR,
192506b1a0cSSebastian Grimberg                     "Multiple active output CeedBases found");
193506b1a0cSSebastian Grimberg           *active_output_basis = op->output_fields[i]->basis;
194506b1a0cSSebastian Grimberg         }
195506b1a0cSSebastian Grimberg       }
196506b1a0cSSebastian Grimberg       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
197506b1a0cSSebastian Grimberg     }
198506b1a0cSSebastian Grimberg   }
199eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
200eaf62fffSJeremy L Thompson }
201eaf62fffSJeremy L Thompson 
202eaf62fffSJeremy L Thompson /**
203ca94c3ddSJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`
204e2f04181SAndrew T. Barker 
205ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
206ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
207e2f04181SAndrew T. Barker 
208e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
209e2f04181SAndrew T. Barker 
210e2f04181SAndrew T. Barker   @ref Utility
211e2f04181SAndrew T. Barker **/
2122b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
213506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
214506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
215506b1a0cSSebastian Grimberg }
216506b1a0cSSebastian Grimberg 
217506b1a0cSSebastian Grimberg /**
218ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`
219506b1a0cSSebastian Grimberg 
220ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
221ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
222ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
223506b1a0cSSebastian Grimberg 
224506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
225506b1a0cSSebastian Grimberg 
226506b1a0cSSebastian Grimberg   @ref Utility
227506b1a0cSSebastian Grimberg **/
228506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2296aaed0edSJeremy L Thompson   Ceed ceed;
2301c66c397SJeremy L Thompson 
2316aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
232506b1a0cSSebastian Grimberg   if (active_input_rstr) {
233506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
234506b1a0cSSebastian Grimberg     if (!op->is_composite) {
2352b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
236d1d35e2fSjeremylt         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
237506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_rstr || *active_input_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
238506b1a0cSSebastian Grimberg                     "Multiple active input CeedElemRestrictions found");
239506b1a0cSSebastian Grimberg           *active_input_rstr = op->input_fields[i]->elem_rstr;
240e2f04181SAndrew T. Barker         }
2412b730f8bSJeremy L Thompson       }
242506b1a0cSSebastian Grimberg       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
243506b1a0cSSebastian Grimberg     }
244506b1a0cSSebastian Grimberg   }
245506b1a0cSSebastian Grimberg   if (active_output_rstr) {
246506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
247506b1a0cSSebastian Grimberg     if (!op->is_composite) {
248506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
249506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
250506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_rstr || *active_output_rstr == op->output_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
251506b1a0cSSebastian Grimberg                     "Multiple active output CeedElemRestrictions found");
252506b1a0cSSebastian Grimberg           *active_output_rstr = op->output_fields[i]->elem_rstr;
253506b1a0cSSebastian Grimberg         }
254506b1a0cSSebastian Grimberg       }
255506b1a0cSSebastian Grimberg       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
256506b1a0cSSebastian Grimberg     }
257506b1a0cSSebastian Grimberg   }
258e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
259e2f04181SAndrew T. Barker }
260e2f04181SAndrew T. Barker 
261d8dd9a91SJeremy L Thompson /**
262ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
2634385fb7fSSebastian Grimberg 
264ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
265ca94c3ddSJeremy 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.
266d8dd9a91SJeremy L Thompson 
267ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
268ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
269ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2702788fa27SJeremy L Thompson   @param[in]     values      Values to set
271d8dd9a91SJeremy L Thompson 
272d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
273d8dd9a91SJeremy L Thompson 
274d8dd9a91SJeremy L Thompson   @ref User
275d8dd9a91SJeremy L Thompson **/
2762788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2771c66c397SJeremy L Thompson   bool is_composite = false;
2781c66c397SJeremy L Thompson 
2796574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2803668ca4bSJeremy L Thompson 
2815ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2825ac9af79SJeremy L Thompson   if (field_label->from_op) {
2835ac9af79SJeremy L Thompson     CeedInt index = -1;
2845ac9af79SJeremy L Thompson 
2855ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2865ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2875ac9af79SJeremy L Thompson     }
2886574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2895ac9af79SJeremy L Thompson   }
2905ac9af79SJeremy L Thompson 
2912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
292d8dd9a91SJeremy L Thompson   if (is_composite) {
293d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
294d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
295d8dd9a91SJeremy L Thompson 
296c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
297c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2986574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2996574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
300d8dd9a91SJeremy L Thompson 
301d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
302d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3033668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3042788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
305d8dd9a91SJeremy L Thompson       }
306d8dd9a91SJeremy L Thompson     }
307d8dd9a91SJeremy L Thompson   } else {
3086574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3092788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
3102788fa27SJeremy L Thompson   }
3116d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3122788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3132788fa27SJeremy L Thompson }
3142788fa27SJeremy L Thompson 
3152788fa27SJeremy L Thompson /**
316ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3174385fb7fSSebastian Grimberg 
318ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
319ca94c3ddSJeremy 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.
3202788fa27SJeremy L Thompson 
321ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3222788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3232788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
324c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
325c5d0f995SJed Brown   @param[out]    values      Values in the label
3262788fa27SJeremy L Thompson 
3272788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3282788fa27SJeremy L Thompson 
3292788fa27SJeremy L Thompson   @ref User
3302788fa27SJeremy L Thompson **/
3312788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3322788fa27SJeremy L Thompson                                              void *values) {
3331c66c397SJeremy L Thompson   bool is_composite = false;
3341c66c397SJeremy L Thompson 
3356574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3362788fa27SJeremy L Thompson 
3372788fa27SJeremy L Thompson   *(void **)values = NULL;
3382788fa27SJeremy L Thompson   *num_values      = 0;
3392788fa27SJeremy L Thompson 
3405ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3415ac9af79SJeremy L Thompson   if (field_label->from_op) {
3425ac9af79SJeremy L Thompson     CeedInt index = -1;
3435ac9af79SJeremy L Thompson 
3445ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3455ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3465ac9af79SJeremy L Thompson     }
3476574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3485ac9af79SJeremy L Thompson   }
3495ac9af79SJeremy L Thompson 
3502788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3512788fa27SJeremy L Thompson   if (is_composite) {
3522788fa27SJeremy L Thompson     CeedInt       num_sub;
3532788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3542788fa27SJeremy L Thompson 
3552788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3562788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3576574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3586574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3592788fa27SJeremy L Thompson 
3602788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3612788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3622788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3632788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3642788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3652788fa27SJeremy L Thompson       }
3662788fa27SJeremy L Thompson     }
3672788fa27SJeremy L Thompson   } else {
3686574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3692788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3702788fa27SJeremy L Thompson   }
3712788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3722788fa27SJeremy L Thompson }
3732788fa27SJeremy L Thompson 
3742788fa27SJeremy L Thompson /**
375ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
3764385fb7fSSebastian Grimberg 
377ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
378ca94c3ddSJeremy 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.
3792788fa27SJeremy L Thompson 
380ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3812788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3822788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
383c5d0f995SJed Brown   @param[in]     values      Values array to restore
3842788fa27SJeremy L Thompson 
3852788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3862788fa27SJeremy L Thompson 
3872788fa27SJeremy L Thompson   @ref User
3882788fa27SJeremy L Thompson **/
3892788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3901c66c397SJeremy L Thompson   bool is_composite = false;
3911c66c397SJeremy L Thompson 
3926574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3932788fa27SJeremy L Thompson 
3945ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3955ac9af79SJeremy L Thompson   if (field_label->from_op) {
3965ac9af79SJeremy L Thompson     CeedInt index = -1;
3975ac9af79SJeremy L Thompson 
3985ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3995ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4005ac9af79SJeremy L Thompson     }
4016574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4025ac9af79SJeremy L Thompson   }
4035ac9af79SJeremy L Thompson 
4042788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4052788fa27SJeremy L Thompson   if (is_composite) {
4062788fa27SJeremy L Thompson     CeedInt       num_sub;
4072788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4082788fa27SJeremy L Thompson 
4092788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4102788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4116574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
4126574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4132788fa27SJeremy L Thompson 
4142788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4152788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4162788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
4172788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
4182788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4192788fa27SJeremy L Thompson       }
4202788fa27SJeremy L Thompson     }
4212788fa27SJeremy L Thompson   } else {
4226574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4232788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
424d8dd9a91SJeremy L Thompson   }
425d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
426d8dd9a91SJeremy L Thompson }
427d8dd9a91SJeremy L Thompson 
4287a982d89SJeremy L. Thompson /// @}
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4317a982d89SJeremy L. Thompson /// CeedOperator Backend API
4327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4337a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
4347a982d89SJeremy L. Thompson /// @{
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson /**
437ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
4387a982d89SJeremy L. Thompson 
439ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
440d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @ref Backend
4457a982d89SJeremy L. Thompson **/
446d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
4476574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
448d1d35e2fSjeremylt   *num_args = op->num_fields;
449e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4507a982d89SJeremy L. Thompson }
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson /**
4539463e855SJeremy L Thompson   @brief Get the tensor product status of all bases for a `CeedOperator`.
4549463e855SJeremy L Thompson 
4559463e855SJeremy L Thompson   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
4569463e855SJeremy L Thompson 
4579463e855SJeremy L Thompson   @param[in]  op               `CeedOperator`
4589463e855SJeremy L Thompson   @param[out] has_tensor_bases Variable to store tensor bases status
4599463e855SJeremy L Thompson 
4609463e855SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4619463e855SJeremy L Thompson 
4629463e855SJeremy L Thompson   @ref Backend
4639463e855SJeremy L Thompson **/
4649463e855SJeremy L Thompson int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
4659463e855SJeremy L Thompson   CeedInt            num_inputs, num_outputs;
4669463e855SJeremy L Thompson   CeedOperatorField *input_fields, *output_fields;
4679463e855SJeremy L Thompson 
4689463e855SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
4699463e855SJeremy L Thompson   *has_tensor_bases = true;
4709463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_inputs; i++) {
4719463e855SJeremy L Thompson     bool      is_tensor;
4729463e855SJeremy L Thompson     CeedBasis basis;
4739463e855SJeremy L Thompson 
4749463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
4759463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
4769463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4779463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
4789463e855SJeremy L Thompson     }
4799463e855SJeremy L Thompson   }
4809463e855SJeremy L Thompson   for (CeedInt i = 0; i < num_outputs; i++) {
4819463e855SJeremy L Thompson     bool      is_tensor;
4829463e855SJeremy L Thompson     CeedBasis basis;
4839463e855SJeremy L Thompson 
4849463e855SJeremy L Thompson     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
4859463e855SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
4869463e855SJeremy L Thompson       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
4879463e855SJeremy L Thompson       *has_tensor_bases &= is_tensor;
4889463e855SJeremy L Thompson     }
4899463e855SJeremy L Thompson   }
4909463e855SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4919463e855SJeremy L Thompson }
4929463e855SJeremy L Thompson 
4939463e855SJeremy L Thompson /**
494ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
4957a982d89SJeremy L. Thompson 
496ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
497d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4987a982d89SJeremy L. Thompson 
4997a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5007a982d89SJeremy L. Thompson 
5017a982d89SJeremy L. Thompson   @ref Backend
5027a982d89SJeremy L. Thompson **/
503d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
504f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
505e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5067a982d89SJeremy L. Thompson }
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson /**
509ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
5107a982d89SJeremy L. Thompson 
511ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
512ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
5137a982d89SJeremy L. Thompson 
5147a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5157a982d89SJeremy L. Thompson 
5167a982d89SJeremy L. Thompson   @ref Backend
5177a982d89SJeremy L. Thompson **/
5187a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
5196574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
5207a982d89SJeremy L. Thompson   *qf = op->qf;
521e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5227a982d89SJeremy L. Thompson }
5237a982d89SJeremy L. Thompson 
5247a982d89SJeremy L. Thompson /**
525ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
526c04a41a7SJeremy L Thompson 
527ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
528d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
529c04a41a7SJeremy L Thompson 
530c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
531c04a41a7SJeremy L Thompson 
532c04a41a7SJeremy L Thompson   @ref Backend
533c04a41a7SJeremy L Thompson **/
534d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
535f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
536e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
537c04a41a7SJeremy L Thompson }
538c04a41a7SJeremy L Thompson 
539c04a41a7SJeremy L Thompson /**
540ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
5417a982d89SJeremy L. Thompson 
542ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
5437a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5447a982d89SJeremy L. Thompson 
5457a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   @ref Backend
5487a982d89SJeremy L. Thompson **/
549777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
550777ff853SJeremy L Thompson   *(void **)data = op->data;
551e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5527a982d89SJeremy L. Thompson }
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson /**
555ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
5567a982d89SJeremy L. Thompson 
557ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
558ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5617a982d89SJeremy L. Thompson 
5627a982d89SJeremy L. Thompson   @ref Backend
5637a982d89SJeremy L. Thompson **/
564777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
565777ff853SJeremy L Thompson   op->data = data;
566e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5677a982d89SJeremy L. Thompson }
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson /**
570ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
57134359f16Sjeremylt 
572ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
57334359f16Sjeremylt 
57434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
57534359f16Sjeremylt 
57634359f16Sjeremylt   @ref Backend
57734359f16Sjeremylt **/
5789560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
57934359f16Sjeremylt   op->ref_count++;
58034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
58134359f16Sjeremylt }
58234359f16Sjeremylt 
58334359f16Sjeremylt /**
584ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
5857a982d89SJeremy L. Thompson 
586ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
5877a982d89SJeremy L. Thompson 
5887a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5897a982d89SJeremy L. Thompson 
5907a982d89SJeremy L. Thompson   @ref Backend
5917a982d89SJeremy L. Thompson **/
5927a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
593f04ea552SJeremy L Thompson   op->is_backend_setup = true;
594e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5957a982d89SJeremy L. Thompson }
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson /// @}
5987a982d89SJeremy L. Thompson 
5997a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6007a982d89SJeremy L. Thompson /// CeedOperator Public API
6017a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6027a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
603dfdf5a53Sjeremylt /// @{
604d7b241e6Sjeremylt 
605d7b241e6Sjeremylt /**
606ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
6074385fb7fSSebastian Grimberg 
608ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
609d7b241e6Sjeremylt 
610ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
611ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
612ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
613ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
614ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
615b11c1e72Sjeremylt 
616b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
617dfdf5a53Sjeremylt 
6187a982d89SJeremy L. Thompson   @ref User
619d7b241e6Sjeremylt  */
6202b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
6215fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
6225fe0d4faSjeremylt     Ceed delegate;
6236574a04fSJeremy L Thompson 
6242b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
625ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreate");
6262b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
627e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6285fe0d4faSjeremylt   }
6295fe0d4faSjeremylt 
630ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
631db002c03SJeremy L Thompson 
6322b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
633db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
634d1d35e2fSjeremylt   (*op)->ref_count   = 1;
6352b104005SJeremy L Thompson   (*op)->input_size  = -1;
6362b104005SJeremy L Thompson   (*op)->output_size = -1;
637db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
638db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
639db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
6402b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
6412b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
6422b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
6432b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
644e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
645d7b241e6Sjeremylt }
646d7b241e6Sjeremylt 
647d7b241e6Sjeremylt /**
648ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
64948acf710SJeremy L Thompson 
650ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
651ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
65248acf710SJeremy L Thompson 
653ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
654ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
655ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
656ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
65748acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
65848acf710SJeremy L Thompson 
65948acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66048acf710SJeremy L Thompson 
66148acf710SJeremy L Thompson   @ref User
66248acf710SJeremy L Thompson  */
66348acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
66448acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
66548acf710SJeremy L Thompson     Ceed delegate;
66648acf710SJeremy L Thompson 
66748acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
668ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreateAtPoints");
66948acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
67048acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
67148acf710SJeremy L Thompson   }
67248acf710SJeremy L Thompson 
673ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
67448acf710SJeremy L Thompson 
67548acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
67648acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
67748acf710SJeremy L Thompson   (*op)->ref_count    = 1;
67848acf710SJeremy L Thompson   (*op)->is_at_points = true;
67948acf710SJeremy L Thompson   (*op)->input_size   = -1;
68048acf710SJeremy L Thompson   (*op)->output_size  = -1;
68148acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
68248acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
68348acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
68448acf710SJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
68548acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
68648acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
68748acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
68848acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68948acf710SJeremy L Thompson }
69048acf710SJeremy L Thompson 
69148acf710SJeremy L Thompson /**
692ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
69352d6035fSJeremy L Thompson 
694ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
695ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
69652d6035fSJeremy L Thompson 
69752d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
69852d6035fSJeremy L Thompson 
6997a982d89SJeremy L. Thompson   @ref User
70052d6035fSJeremy L Thompson  */
70152d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
70252d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
70352d6035fSJeremy L Thompson     Ceed delegate;
70452d6035fSJeremy L Thompson 
7051c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
706250756a7Sjeremylt     if (delegate) {
7072b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
708e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
70952d6035fSJeremy L Thompson     }
710250756a7Sjeremylt   }
71152d6035fSJeremy L Thompson 
7122b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
713db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
714996d9ab5SJed Brown   (*op)->ref_count    = 1;
715f04ea552SJeremy L Thompson   (*op)->is_composite = true;
7162b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
7172b104005SJeremy L Thompson   (*op)->input_size  = -1;
7182b104005SJeremy L Thompson   (*op)->output_size = -1;
719250756a7Sjeremylt 
720db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
721e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
72252d6035fSJeremy L Thompson }
72352d6035fSJeremy L Thompson 
72452d6035fSJeremy L Thompson /**
725ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
7264385fb7fSSebastian Grimberg 
727ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
728512bb800SJeremy L Thompson 
729ca94c3ddSJeremy 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`.
730ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
7319560d06aSjeremylt 
732ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
733ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
7349560d06aSjeremylt 
7359560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7369560d06aSjeremylt 
7379560d06aSjeremylt   @ref User
7389560d06aSjeremylt **/
7399560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
7402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
7412b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
7429560d06aSjeremylt   *op_copy = op;
7439560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7449560d06aSjeremylt }
7459560d06aSjeremylt 
7469560d06aSjeremylt /**
747ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
748d7b241e6Sjeremylt 
749ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
750bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
751ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
752d7b241e6Sjeremylt 
753ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
754ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
755d7b241e6Sjeremylt 
756528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
757bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
758528a22edSJeremy L Thompson 
759ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
760ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
761bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
762bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
763bafebce1SSebastian 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`
764b11c1e72Sjeremylt 
765b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
766dfdf5a53Sjeremylt 
7677a982d89SJeremy L. Thompson   @ref User
768b11c1e72Sjeremylt **/
769bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
770*2a3ff1c9SZach Atkins   bool               is_input = true, is_at_points;
7711c66c397SJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0;
7721c66c397SJeremy L Thompson   CeedQFunctionField qf_field;
7731c66c397SJeremy L Thompson   CeedOperatorField *op_field;
7741c66c397SJeremy L Thompson 
7756574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
7766574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
777bafebce1SSebastian Grimberg   CeedCheck(rstr, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
778bafebce1SSebastian Grimberg   CeedCheck(basis, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
779bafebce1SSebastian Grimberg   CeedCheck(vec, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
78052d6035fSJeremy L Thompson 
781bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
782*2a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
783bafebce1SSebastian Grimberg   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
784ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
7852c7e7413SJeremy L Thompson   {
7862c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
7872c7e7413SJeremy L Thompson 
788bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
78948acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
790*2a3ff1c9SZach Atkins       CeedCheck(is_at_points, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields");
791bafebce1SSebastian Grimberg       CeedCheck(basis == CEED_BASIS_NONE, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
79248acf710SJeremy L Thompson       if (!op->first_points_rstr) {
793bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
79448acf710SJeremy L Thompson       } else {
79548acf710SJeremy L Thompson         bool are_compatible;
79648acf710SJeremy L Thompson 
797bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
79848acf710SJeremy L Thompson         CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
799ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
80048acf710SJeremy L Thompson       }
80148acf710SJeremy L Thompson     }
8022c7e7413SJeremy L Thompson   }
803d7b241e6Sjeremylt 
804bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
805bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
806528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
807ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
808528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
809bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
810d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
811d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
812d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
813d1d35e2fSjeremylt       op_field = &op->input_fields[i];
814d7b241e6Sjeremylt       goto found;
815d7b241e6Sjeremylt     }
816d7b241e6Sjeremylt   }
8172b104005SJeremy L Thompson   is_input = false;
818d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
819d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
820d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
821d1d35e2fSjeremylt       op_field = &op->output_fields[i];
822d7b241e6Sjeremylt       goto found;
823d7b241e6Sjeremylt     }
824d7b241e6Sjeremylt   }
825c042f62fSJeremy L Thompson   // LCOV_EXCL_START
826ca94c3ddSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
827c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
828d7b241e6Sjeremylt found:
829bafebce1SSebastian Grimberg   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, rstr, basis));
8302b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
831e15f9bd0SJeremy L Thompson 
832bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
8332b104005SJeremy L Thompson     CeedSize l_size;
8341c66c397SJeremy L Thompson 
835bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
8362b104005SJeremy L Thompson     if (is_input) {
8372b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
838249f8407SJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE,
839249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
8402b104005SJeremy L Thompson     } else {
8412b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
842249f8407SJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE,
843249f8407SJeremy L Thompson                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
8442b104005SJeremy L Thompson     }
8452b730f8bSJeremy L Thompson   }
8462b104005SJeremy L Thompson 
847bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
848bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
849bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
850d1d35e2fSjeremylt     op->num_elem        = num_elem;
851d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
852e15f9bd0SJeremy L Thompson   }
853bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
854*2a3ff1c9SZach Atkins   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
855d1d35e2fSjeremylt   op->num_fields += 1;
8562b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
857e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
858d7b241e6Sjeremylt }
859d7b241e6Sjeremylt 
860d7b241e6Sjeremylt /**
861ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
86243bbe138SJeremy L Thompson 
863ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
864f04ea552SJeremy L Thompson 
865ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
866f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
867ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
868f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
869ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
87043bbe138SJeremy L Thompson 
87143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87243bbe138SJeremy L Thompson 
873e9b533fbSJeremy L Thompson   @ref Advanced
87443bbe138SJeremy L Thompson **/
8752b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
87643bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
8776574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
8782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
87943bbe138SJeremy L Thompson 
88043bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
88143bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
88243bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
88343bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
88443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88543bbe138SJeremy L Thompson }
88643bbe138SJeremy L Thompson 
88743bbe138SJeremy L Thompson /**
888ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
88948acf710SJeremy L Thompson 
890ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
89148acf710SJeremy L Thompson 
892ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
893ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
894ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
89548acf710SJeremy L Thompson 
89648acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
89748acf710SJeremy L Thompson 
89848acf710SJeremy L Thompson   @ref Advanced
89948acf710SJeremy L Thompson **/
90048acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
901*2a3ff1c9SZach Atkins   bool is_at_points;
902*2a3ff1c9SZach Atkins 
903*2a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
904*2a3ff1c9SZach Atkins 
905*2a3ff1c9SZach Atkins   CeedCheck(is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
90648acf710SJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
90748acf710SJeremy L Thompson 
90848acf710SJeremy L Thompson   if (!op->first_points_rstr) {
90948acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
91048acf710SJeremy L Thompson   } else {
91148acf710SJeremy L Thompson     bool are_compatible;
91248acf710SJeremy L Thompson 
91348acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
91448acf710SJeremy L Thompson     CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
915ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
91648acf710SJeremy L Thompson   }
91748acf710SJeremy L Thompson 
91848acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
91948acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
92048acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92148acf710SJeremy L Thompson }
92248acf710SJeremy L Thompson 
92348acf710SJeremy L Thompson /**
924b594f9faSZach Atkins   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
925b594f9faSZach Atkins 
926b594f9faSZach Atkins   @param[in]  op           `CeedOperator`
927b594f9faSZach Atkins   @param[out] is_at_points Variable to store at points status
928b594f9faSZach Atkins 
929b594f9faSZach Atkins   @return An error code: 0 - success, otherwise - failure
930b594f9faSZach Atkins 
931b594f9faSZach Atkins   @ref User
932b594f9faSZach Atkins **/
933b594f9faSZach Atkins int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
934b594f9faSZach Atkins   *is_at_points = op->is_at_points;
935b594f9faSZach Atkins   return CEED_ERROR_SUCCESS;
936b594f9faSZach Atkins }
937b594f9faSZach Atkins 
938b594f9faSZach Atkins /**
939ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
94048acf710SJeremy L Thompson 
941ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
94248acf710SJeremy L Thompson 
943ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
944ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
945ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
94648acf710SJeremy L Thompson 
94748acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94848acf710SJeremy L Thompson 
94948acf710SJeremy L Thompson   @ref Advanced
95048acf710SJeremy L Thompson **/
95148acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
952*2a3ff1c9SZach Atkins   bool is_at_points;
953*2a3ff1c9SZach Atkins 
954*2a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
955*2a3ff1c9SZach Atkins   CeedCheck(is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
95648acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
95748acf710SJeremy L Thompson 
95848acf710SJeremy L Thompson   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
95948acf710SJeremy L Thompson   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
96048acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96148acf710SJeremy L Thompson }
96248acf710SJeremy L Thompson 
96348acf710SJeremy L Thompson /**
964be9c6463SJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
965be9c6463SJeremy L Thompson 
966be9c6463SJeremy L Thompson   `op_field` is set to `NULL` if the field is not found.
967de5900adSJames Wright 
968ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
969de5900adSJames Wright 
970ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
971ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
972ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
973de5900adSJames Wright 
974de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
975de5900adSJames Wright 
976de5900adSJames Wright   @ref Advanced
977de5900adSJames Wright **/
978de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
9791c66c397SJeremy L Thompson   char              *name;
980de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
981de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
982de5900adSJames Wright 
983be9c6463SJeremy L Thompson   *op_field = NULL;
9841c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
985de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
986de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
987de5900adSJames Wright     if (!strcmp(name, field_name)) {
988de5900adSJames Wright       *op_field = input_fields[i];
989de5900adSJames Wright       return CEED_ERROR_SUCCESS;
990de5900adSJames Wright     }
991de5900adSJames Wright   }
992de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
993de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
994de5900adSJames Wright     if (!strcmp(name, field_name)) {
995de5900adSJames Wright       *op_field = output_fields[i];
996de5900adSJames Wright       return CEED_ERROR_SUCCESS;
997de5900adSJames Wright     }
998de5900adSJames Wright   }
999be9c6463SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1000de5900adSJames Wright }
1001de5900adSJames Wright 
1002de5900adSJames Wright /**
1003ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
100428567f8fSJeremy L Thompson 
1005ca94c3ddSJeremy L Thompson   @param[in]  op_field    `CeedOperator` Field
100628567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
100728567f8fSJeremy L Thompson 
100828567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
100928567f8fSJeremy L Thompson 
1010e9b533fbSJeremy L Thompson   @ref Advanced
101128567f8fSJeremy L Thompson **/
101228567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
101328567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
101428567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
101528567f8fSJeremy L Thompson }
101628567f8fSJeremy L Thompson 
101728567f8fSJeremy L Thompson /**
1018ca94c3ddSJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field
101943bbe138SJeremy L Thompson 
1020ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1021ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
102243bbe138SJeremy L Thompson 
102343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
102443bbe138SJeremy L Thompson 
1025e9b533fbSJeremy L Thompson   @ref Advanced
102643bbe138SJeremy L Thompson **/
10272b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1028437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
102943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103043bbe138SJeremy L Thompson }
103143bbe138SJeremy L Thompson 
103243bbe138SJeremy L Thompson /**
1033ca94c3ddSJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field
103443bbe138SJeremy L Thompson 
1035ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1036ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
103743bbe138SJeremy L Thompson 
103843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
103943bbe138SJeremy L Thompson 
1040e9b533fbSJeremy L Thompson   @ref Advanced
104143bbe138SJeremy L Thompson **/
104243bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
104343bbe138SJeremy L Thompson   *basis = op_field->basis;
104443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
104543bbe138SJeremy L Thompson }
104643bbe138SJeremy L Thompson 
104743bbe138SJeremy L Thompson /**
1048ca94c3ddSJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field
104943bbe138SJeremy L Thompson 
1050ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
1051ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
105243bbe138SJeremy L Thompson 
105343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105443bbe138SJeremy L Thompson 
1055e9b533fbSJeremy L Thompson   @ref Advanced
105643bbe138SJeremy L Thompson **/
105743bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
105843bbe138SJeremy L Thompson   *vec = op_field->vec;
105943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106043bbe138SJeremy L Thompson }
106143bbe138SJeremy L Thompson 
106243bbe138SJeremy L Thompson /**
1063ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1064288c0443SJeremy L Thompson 
1065ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1066ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
106752d6035fSJeremy L Thompson 
106852d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
106952d6035fSJeremy L Thompson 
10707a982d89SJeremy L. Thompson   @ref User
107152d6035fSJeremy L Thompson  */
10722b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
10736574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
10746574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
10756574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
10762b730f8bSJeremy L Thompson 
10772b730f8bSJeremy L Thompson   {
10782b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
10791c66c397SJeremy L Thompson 
10802b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
10812b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
10822b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
10832b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
10846574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
10856574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
1086249f8407SJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1087249f8407SJeremy L Thompson               ") not compatible with sub-operator of "
1088249f8407SJeremy L Thompson               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
10892b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
10902b730f8bSJeremy L Thompson   }
10912b730f8bSJeremy L Thompson 
1092d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
10932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1094d1d35e2fSjeremylt   composite_op->num_suboperators++;
1095e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
109652d6035fSJeremy L Thompson }
109752d6035fSJeremy L Thompson 
109852d6035fSJeremy L Thompson /**
1099ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
110075f0d5a4SJeremy L Thompson 
1101ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1102ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
110375f0d5a4SJeremy L Thompson 
110475f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
110575f0d5a4SJeremy L Thompson 
110675f0d5a4SJeremy L Thompson   @ref Backend
110775f0d5a4SJeremy L Thompson **/
1108c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
11096574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
111075f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
111175f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
111275f0d5a4SJeremy L Thompson }
111375f0d5a4SJeremy L Thompson 
111475f0d5a4SJeremy L Thompson /**
1115ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
111675f0d5a4SJeremy L Thompson 
1117ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1118ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
111975f0d5a4SJeremy L Thompson 
112075f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
112175f0d5a4SJeremy L Thompson 
112275f0d5a4SJeremy L Thompson   @ref Backend
112375f0d5a4SJeremy L Thompson **/
1124c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
11256574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
112675f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
112775f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
112875f0d5a4SJeremy L Thompson }
112975f0d5a4SJeremy L Thompson 
113075f0d5a4SJeremy L Thompson /**
1131ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
11324db537f9SJeremy L Thompson 
1133ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
11344db537f9SJeremy L Thompson 
11354db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11364db537f9SJeremy L Thompson 
11374db537f9SJeremy L Thompson   @ref User
11384db537f9SJeremy L Thompson **/
11394db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
11404db537f9SJeremy L Thompson   Ceed ceed;
1141*2a3ff1c9SZach Atkins   bool is_at_points;
11421c66c397SJeremy L Thompson 
11432b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1144*2a3ff1c9SZach Atkins   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
11454db537f9SJeremy L Thompson 
11462b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
11474db537f9SJeremy L Thompson 
11484db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
11494db537f9SJeremy L Thompson   if (op->is_composite) {
115043622462SJeremy L Thompson     if (!op->num_suboperators) {
115143622462SJeremy L Thompson       // Empty operator setup
115243622462SJeremy L Thompson       op->input_size  = 0;
115343622462SJeremy L Thompson       op->output_size = 0;
115443622462SJeremy L Thompson     } else {
11554db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
11562b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
11574db537f9SJeremy L Thompson       }
11582b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
11592b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
11602b104005SJeremy L Thompson       CeedSize input_size, output_size;
11612b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
116243622462SJeremy L Thompson     }
11634db537f9SJeremy L Thompson   } else {
11646574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
11656574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
11666574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
1167*2a3ff1c9SZach Atkins     CeedCheck(op->num_qpts > 0 || is_at_points, ceed, CEED_ERROR_INCOMPLETE,
1168ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
11692b730f8bSJeremy L Thompson   }
11704db537f9SJeremy L Thompson 
11714db537f9SJeremy L Thompson   // Flag as immutable and ready
11724db537f9SJeremy L Thompson   op->is_interface_setup = true;
11732b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
11742b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
11752b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
11764db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11774db537f9SJeremy L Thompson }
11784db537f9SJeremy L Thompson 
11794db537f9SJeremy L Thompson /**
1180ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
11814385fb7fSSebastian Grimberg 
1182ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1183c9366a6bSJeremy L Thompson 
1184ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1185ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1186ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1187c9366a6bSJeremy L Thompson 
1188c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1189c9366a6bSJeremy L Thompson 
1190c9366a6bSJeremy L Thompson   @ref User
1191c9366a6bSJeremy L Thompson **/
11922b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1193c9366a6bSJeremy L Thompson   bool is_composite;
1194c9366a6bSJeremy L Thompson 
11952b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
11962b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1197c9366a6bSJeremy L Thompson 
11982b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11992b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1200c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1201c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
12021c66c397SJeremy L Thompson 
12032b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
12042b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
12052b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
12062b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
12076574a04fSJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed,
12086574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
1209249f8407SJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1210249f8407SJeremy L Thompson                 ") not compatible with sub-operator of "
1211249f8407SJeremy L Thompson                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
12122b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1213c9366a6bSJeremy L Thompson     }
12142b730f8bSJeremy L Thompson   }
1215c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1216c9366a6bSJeremy L Thompson }
1217c9366a6bSJeremy L Thompson 
1218c9366a6bSJeremy L Thompson /**
1219ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
12204385fb7fSSebastian Grimberg 
1221ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1222ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
12238b919e6bSJeremy L Thompson 
1224ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1225beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
12268b919e6bSJeremy L Thompson 
12278b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12288b919e6bSJeremy L Thompson 
12298b919e6bSJeremy L Thompson   @ref Advanced
12308b919e6bSJeremy L Thompson **/
12312b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
12328b919e6bSJeremy L Thompson   bool is_composite;
12338b919e6bSJeremy L Thompson 
12342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12358b919e6bSJeremy L Thompson   if (is_composite) {
12368b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
12372b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
12388b919e6bSJeremy L Thompson     }
12398b919e6bSJeremy L Thompson   } else {
12402b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1241beecbf24SJeremy L Thompson   }
1242beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1243beecbf24SJeremy L Thompson }
1244beecbf24SJeremy L Thompson 
1245beecbf24SJeremy L Thompson /**
1246ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1247beecbf24SJeremy L Thompson 
1248ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
12496e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1250beecbf24SJeremy L Thompson 
1251beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1252beecbf24SJeremy L Thompson 
1253beecbf24SJeremy L Thompson   @ref Advanced
1254beecbf24SJeremy L Thompson **/
12552b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1256beecbf24SJeremy L Thompson   bool is_composite;
1257beecbf24SJeremy L Thompson 
12582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1259beecbf24SJeremy L Thompson   if (is_composite) {
1260beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
12612b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1262beecbf24SJeremy L Thompson     }
1263beecbf24SJeremy L Thompson   } else {
12642b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
12658b919e6bSJeremy L Thompson   }
12668b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12678b919e6bSJeremy L Thompson }
12688b919e6bSJeremy L Thompson 
12698b919e6bSJeremy L Thompson /**
1270ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1271ea6b5821SJeremy L Thompson 
1272ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1273ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1274ea6b5821SJeremy L Thompson 
1275ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1276ea6b5821SJeremy L Thompson 
1277ea6b5821SJeremy L Thompson   @ref User
1278ea6b5821SJeremy L Thompson **/
1279ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1280ea6b5821SJeremy L Thompson   char  *name_copy;
1281ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1282ea6b5821SJeremy L Thompson 
12832b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1284ea6b5821SJeremy L Thompson   if (name_len > 0) {
12852b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1286ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1287ea6b5821SJeremy L Thompson     op->name = name_copy;
1288ea6b5821SJeremy L Thompson   }
1289ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1290ea6b5821SJeremy L Thompson }
1291ea6b5821SJeremy L Thompson 
1292ea6b5821SJeremy L Thompson /**
1293ca94c3ddSJeremy L Thompson   @brief View a `CeedOperator`
12947a982d89SJeremy L. Thompson 
1295ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1296ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
12977a982d89SJeremy L. Thompson 
12987a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
12997a982d89SJeremy L. Thompson 
13007a982d89SJeremy L. Thompson   @ref User
13017a982d89SJeremy L. Thompson **/
13027a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1303ea6b5821SJeremy L Thompson   bool has_name = op->name;
13047a982d89SJeremy L. Thompson 
1305f04ea552SJeremy L Thompson   if (op->is_composite) {
13062b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
13077a982d89SJeremy L. Thompson 
1308d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1309ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
13102b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
13112b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
13127a982d89SJeremy L. Thompson     }
13137a982d89SJeremy L. Thompson   } else {
13142b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
13152b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
13167a982d89SJeremy L. Thompson   }
1317e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13187a982d89SJeremy L. Thompson }
13193bd813ffSjeremylt 
13203bd813ffSjeremylt /**
1321ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1322b7c9bbdaSJeremy L Thompson 
1323ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1324ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1325b7c9bbdaSJeremy L Thompson 
1326b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1327b7c9bbdaSJeremy L Thompson 
1328b7c9bbdaSJeremy L Thompson   @ref Advanced
1329b7c9bbdaSJeremy L Thompson **/
1330b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1331b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1332b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1333b7c9bbdaSJeremy L Thompson }
1334b7c9bbdaSJeremy L Thompson 
1335b7c9bbdaSJeremy L Thompson /**
1336ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1337b7c9bbdaSJeremy L Thompson 
1338ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1339b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1340b7c9bbdaSJeremy L Thompson 
1341b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1342b7c9bbdaSJeremy L Thompson 
1343b7c9bbdaSJeremy L Thompson   @ref Advanced
1344b7c9bbdaSJeremy L Thompson **/
1345b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
13466574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1347b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1348b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1349b7c9bbdaSJeremy L Thompson }
1350b7c9bbdaSJeremy L Thompson 
1351b7c9bbdaSJeremy L Thompson /**
1352ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1353b7c9bbdaSJeremy L Thompson 
1354ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1355b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1356b7c9bbdaSJeremy L Thompson 
1357b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1358b7c9bbdaSJeremy L Thompson 
1359b7c9bbdaSJeremy L Thompson   @ref Advanced
1360b7c9bbdaSJeremy L Thompson **/
1361b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
13626574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1363b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1364b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1365b7c9bbdaSJeremy L Thompson }
1366b7c9bbdaSJeremy L Thompson 
1367b7c9bbdaSJeremy L Thompson /**
1368ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
13696e15d496SJeremy L Thompson 
1370ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1371ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
13726e15d496SJeremy L Thompson 
13736e15d496SJeremy L Thompson   @ref Backend
13746e15d496SJeremy L Thompson **/
13759d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
13766e15d496SJeremy L Thompson   bool is_composite;
13771c66c397SJeremy L Thompson 
13782b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
13796e15d496SJeremy L Thompson 
13806e15d496SJeremy L Thompson   *flops = 0;
13812b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13826e15d496SJeremy L Thompson   if (is_composite) {
13836e15d496SJeremy L Thompson     CeedInt num_suboperators;
13841c66c397SJeremy L Thompson 
1385c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13866e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1387c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13886e15d496SJeremy L Thompson 
13896e15d496SJeremy L Thompson     // FLOPs for each suboperator
13906e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
13919d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
13921c66c397SJeremy L Thompson 
13932b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
13946e15d496SJeremy L Thompson       *flops += suboperator_flops;
13956e15d496SJeremy L Thompson     }
13966e15d496SJeremy L Thompson   } else {
13971c66c397SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
13986e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
13991c66c397SJeremy L Thompson 
14002b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
14012b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
14024385fb7fSSebastian Grimberg 
14036e15d496SJeremy L Thompson     // Input FLOPs
14046e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
14056e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1406edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
14076e15d496SJeremy L Thompson 
1408edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1409edb2538eSJeremy L Thompson         *flops += rstr_flops;
14102b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
14116e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
14126e15d496SJeremy L Thompson       }
14136e15d496SJeremy L Thompson     }
14146e15d496SJeremy L Thompson     // QF FLOPs
14151c66c397SJeremy L Thompson     {
14169d36ca50SJeremy L Thompson       CeedInt  num_qpts;
14179d36ca50SJeremy L Thompson       CeedSize qf_flops;
14181c66c397SJeremy L Thompson 
14192b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
14202b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
14216e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
14221c66c397SJeremy L Thompson     }
14231c66c397SJeremy L Thompson 
14246e15d496SJeremy L Thompson     // Output FLOPs
14256e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
14266e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1427edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
14286e15d496SJeremy L Thompson 
1429edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1430edb2538eSJeremy L Thompson         *flops += rstr_flops;
14312b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
14326e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
14336e15d496SJeremy L Thompson       }
14346e15d496SJeremy L Thompson     }
14356e15d496SJeremy L Thompson   }
14366e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14376e15d496SJeremy L Thompson }
14386e15d496SJeremy L Thompson 
14396e15d496SJeremy L Thompson /**
1440ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
14419fd66db6SSebastian Grimberg 
1442ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1443512bb800SJeremy L Thompson 
1444ca94c3ddSJeremy 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`.
1445ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
14460126412dSJeremy L Thompson 
1447ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1448ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
14490126412dSJeremy L Thompson 
14500126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14510126412dSJeremy L Thompson 
1452859c15bbSJames Wright   @ref Advanced
14530126412dSJeremy L Thompson **/
14540126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
1455ca94c3ddSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
14560126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
14570126412dSJeremy L Thompson   else *ctx = NULL;
14580126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14590126412dSJeremy L Thompson }
14600126412dSJeremy L Thompson 
14610126412dSJeremy L Thompson /**
1462ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
14633668ca4bSJeremy L Thompson 
1464ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1465859c15bbSJames Wright 
1466ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
14673668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
14683668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
14693668ca4bSJeremy L Thompson 
14703668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14713668ca4bSJeremy L Thompson 
14723668ca4bSJeremy L Thompson   @ref User
14733668ca4bSJeremy L Thompson **/
147417b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
14751c66c397SJeremy L Thompson   bool is_composite, field_found = false;
14761c66c397SJeremy L Thompson 
14772b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14782b730f8bSJeremy L Thompson 
14793668ca4bSJeremy L Thompson   if (is_composite) {
1480a98a090bSJeremy L Thompson     // Composite operator
1481a98a090bSJeremy L Thompson     // -- Check if composite label already created
14823668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
14833668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
14843668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
14853668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
14863668ca4bSJeremy L Thompson       }
14873668ca4bSJeremy L Thompson     }
14883668ca4bSJeremy L Thompson 
1489a98a090bSJeremy L Thompson     // -- Create composite label if needed
14903668ca4bSJeremy L Thompson     CeedInt               num_sub;
14913668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
14923668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
14933668ca4bSJeremy L Thompson 
14942b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1495c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1496c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14972b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
14983668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
14993668ca4bSJeremy L Thompson 
15003668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
15013668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
15023668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
15031c66c397SJeremy L Thompson 
15042b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
15053668ca4bSJeremy L Thompson         if (new_field_label_i) {
1506a98a090bSJeremy L Thompson           field_found                    = true;
15073668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
15083668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
15093668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
15102b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
15117bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
15122b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
15132b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
15142b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
15157bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
15167bfe0f0eSJeremy L Thompson           } else {
15177bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
15187bfe0f0eSJeremy L Thompson           }
15192b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
15207bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
15212b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
1522249f8407SJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %zu != %zu",
15237bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
15247bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
15257bfe0f0eSJeremy L Thompson           } else {
15267bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
15277bfe0f0eSJeremy L Thompson           }
15283668ca4bSJeremy L Thompson         }
15293668ca4bSJeremy L Thompson       }
15303668ca4bSJeremy L Thompson     }
1531a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1532a98a090bSJeremy L Thompson     if (field_found) {
1533a98a090bSJeremy L Thompson       *field_label = new_field_label;
1534a98a090bSJeremy L Thompson     } else {
15353668ca4bSJeremy L Thompson       // LCOV_EXCL_START
15362b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
15372b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
15383668ca4bSJeremy L Thompson       *field_label = NULL;
15393668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
15405ac9af79SJeremy L Thompson     }
15415ac9af79SJeremy L Thompson   } else {
1542a98a090bSJeremy L Thompson     // Single, non-composite operator
1543a98a090bSJeremy L Thompson     if (op->qf->ctx) {
15445ac9af79SJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1545a98a090bSJeremy L Thompson     } else {
1546a98a090bSJeremy L Thompson       *field_label = NULL;
1547a98a090bSJeremy L Thompson     }
15485ac9af79SJeremy L Thompson   }
15495ac9af79SJeremy L Thompson 
15505ac9af79SJeremy L Thompson   // Set label in operator
15515ac9af79SJeremy L Thompson   if (*field_label) {
15525ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
15535ac9af79SJeremy L Thompson 
15543668ca4bSJeremy L Thompson     // Move new composite label to operator
15553668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
15562b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
15573668ca4bSJeremy L Thompson       op->max_context_labels = 1;
15583668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
15592b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
15603668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
15613668ca4bSJeremy L Thompson     }
15625ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
15633668ca4bSJeremy L Thompson     op->num_context_labels++;
15643668ca4bSJeremy L Thompson   }
15653668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15663668ca4bSJeremy L Thompson }
15673668ca4bSJeremy L Thompson 
15683668ca4bSJeremy L Thompson /**
1569ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
15704385fb7fSSebastian Grimberg 
1571ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1572d8dd9a91SJeremy L Thompson 
1573ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
15742788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1575ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1576d8dd9a91SJeremy L Thompson 
1577d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1578d8dd9a91SJeremy L Thompson 
1579d8dd9a91SJeremy L Thompson   @ref User
1580d8dd9a91SJeremy L Thompson **/
158117b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
15822b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1583d8dd9a91SJeremy L Thompson }
1584d8dd9a91SJeremy L Thompson 
1585d8dd9a91SJeremy L Thompson /**
1586ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
15874385fb7fSSebastian Grimberg 
1588ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
15892788fa27SJeremy L Thompson 
1590ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
15912788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
15922788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
15932788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15942788fa27SJeremy L Thompson 
15952788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15962788fa27SJeremy L Thompson 
15972788fa27SJeremy L Thompson   @ref User
15982788fa27SJeremy L Thompson **/
159917b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
16002788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
16012788fa27SJeremy L Thompson }
16022788fa27SJeremy L Thompson 
16032788fa27SJeremy L Thompson /**
1604ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
16052788fa27SJeremy L Thompson 
1606ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16072788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
16082788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16092788fa27SJeremy L Thompson 
16102788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16112788fa27SJeremy L Thompson 
16122788fa27SJeremy L Thompson   @ref User
16132788fa27SJeremy L Thompson **/
161417b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
16152788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
16162788fa27SJeremy L Thompson }
16172788fa27SJeremy L Thompson 
16182788fa27SJeremy L Thompson /**
1619ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
16204385fb7fSSebastian Grimberg 
1621ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1622d8dd9a91SJeremy L Thompson 
1623ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1624ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1625ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1626d8dd9a91SJeremy L Thompson 
1627d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1628d8dd9a91SJeremy L Thompson 
1629d8dd9a91SJeremy L Thompson   @ref User
1630d8dd9a91SJeremy L Thompson **/
163123dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
16322b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1633d8dd9a91SJeremy L Thompson }
1634d8dd9a91SJeremy L Thompson 
1635d8dd9a91SJeremy L Thompson /**
1636ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
16374385fb7fSSebastian Grimberg 
1638ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
16392788fa27SJeremy L Thompson 
1640ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16412788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1642ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
16432788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16442788fa27SJeremy L Thompson 
16452788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16462788fa27SJeremy L Thompson 
16472788fa27SJeremy L Thompson   @ref User
16482788fa27SJeremy L Thompson **/
164923dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
16502788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
16512788fa27SJeremy L Thompson }
16522788fa27SJeremy L Thompson 
16532788fa27SJeremy L Thompson /**
1654ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
16552788fa27SJeremy L Thompson 
1656ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16572788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
16582788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16592788fa27SJeremy L Thompson 
16602788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16612788fa27SJeremy L Thompson 
16622788fa27SJeremy L Thompson   @ref User
16632788fa27SJeremy L Thompson **/
166423dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
16652788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
16662788fa27SJeremy L Thompson }
16672788fa27SJeremy L Thompson 
16682788fa27SJeremy L Thompson /**
1669ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
16705b6ec284SJeremy L Thompson 
1671ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
16725b6ec284SJeremy L Thompson 
1673ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
16745b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
16755b6ec284SJeremy L Thompson   @param[in]     values      Values to set
16765b6ec284SJeremy L Thompson 
16775b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16785b6ec284SJeremy L Thompson 
16795b6ec284SJeremy L Thompson   @ref User
16805b6ec284SJeremy L Thompson **/
16815b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
16825b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
16835b6ec284SJeremy L Thompson }
16845b6ec284SJeremy L Thompson 
16855b6ec284SJeremy L Thompson /**
1686ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
16875b6ec284SJeremy L Thompson 
1688ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
16895b6ec284SJeremy L Thompson 
1690ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16915b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
1692ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
16935b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
16945b6ec284SJeremy L Thompson 
16955b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16965b6ec284SJeremy L Thompson 
16975b6ec284SJeremy L Thompson   @ref User
16985b6ec284SJeremy L Thompson **/
16995b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
17005b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
17015b6ec284SJeremy L Thompson }
17025b6ec284SJeremy L Thompson 
17035b6ec284SJeremy L Thompson /**
1704ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
17055b6ec284SJeremy L Thompson 
1706ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
17075b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
17085b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
17095b6ec284SJeremy L Thompson 
17105b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
17115b6ec284SJeremy L Thompson 
17125b6ec284SJeremy L Thompson   @ref User
17135b6ec284SJeremy L Thompson **/
17145b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
17155b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
17165b6ec284SJeremy L Thompson }
17175b6ec284SJeremy L Thompson 
17185b6ec284SJeremy L Thompson /**
1719ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
1720d7b241e6Sjeremylt 
1721ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1722ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1723d7b241e6Sjeremylt 
1724ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1725f04ea552SJeremy L Thompson 
1726ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1727ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1728ca94c3ddSJeremy 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
1729ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1730b11c1e72Sjeremylt 
1731b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1732dfdf5a53Sjeremylt 
17337a982d89SJeremy L. Thompson   @ref User
1734b11c1e72Sjeremylt **/
17352b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
17362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1737d7b241e6Sjeremylt 
17383ca2b39bSJeremy L Thompson   if (op->is_composite) {
1739250756a7Sjeremylt     // Composite Operator
1740250756a7Sjeremylt     if (op->ApplyComposite) {
17412b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1742250756a7Sjeremylt     } else {
1743d1d35e2fSjeremylt       CeedInt       num_suboperators;
1744d1d35e2fSjeremylt       CeedOperator *sub_operators;
17451c66c397SJeremy L Thompson 
17461c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1747c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1748250756a7Sjeremylt 
1749250756a7Sjeremylt       // Zero all output vectors
17503ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1751d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1752d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1753d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
17541c66c397SJeremy L Thompson 
1755250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
17562b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1757250756a7Sjeremylt           }
1758250756a7Sjeremylt         }
1759250756a7Sjeremylt       }
1760250756a7Sjeremylt       // Apply
1761f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
1762f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1763cae8b89aSjeremylt       }
1764cae8b89aSjeremylt     }
17653ca2b39bSJeremy L Thompson   } else {
17663ca2b39bSJeremy L Thompson     // Standard Operator
17673ca2b39bSJeremy L Thompson     if (op->Apply) {
17683ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
17693ca2b39bSJeremy L Thompson     } else {
17703ca2b39bSJeremy L Thompson       // Zero all output vectors
17713ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
17721c66c397SJeremy L Thompson 
17733ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
17743ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
17751c66c397SJeremy L Thompson 
17763ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
17773ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
17783ca2b39bSJeremy L Thompson       }
17793ca2b39bSJeremy L Thompson       // Apply
17803ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
17813ca2b39bSJeremy L Thompson     }
1782250756a7Sjeremylt   }
1783e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1784cae8b89aSjeremylt }
1785cae8b89aSjeremylt 
1786cae8b89aSjeremylt /**
1787ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
1788cae8b89aSjeremylt 
1789ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1790ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1791cae8b89aSjeremylt 
1792ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1793ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1794ca94c3ddSJeremy 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
1795ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1796cae8b89aSjeremylt 
1797cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1798cae8b89aSjeremylt 
17997a982d89SJeremy L. Thompson   @ref User
1800cae8b89aSjeremylt **/
18012b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
18022b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1803cae8b89aSjeremylt 
18043ca2b39bSJeremy L Thompson   if (op->is_composite) {
1805250756a7Sjeremylt     // Composite Operator
1806250756a7Sjeremylt     if (op->ApplyAddComposite) {
18072b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1808cae8b89aSjeremylt     } else {
1809d1d35e2fSjeremylt       CeedInt       num_suboperators;
1810d1d35e2fSjeremylt       CeedOperator *sub_operators;
1811250756a7Sjeremylt 
18121c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
18131c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1814d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
18152b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
18161d7d2407SJeremy L Thompson       }
1817250756a7Sjeremylt     }
18183ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
18193ca2b39bSJeremy L Thompson     // Standard Operator
18203ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1821250756a7Sjeremylt   }
1822e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1823d7b241e6Sjeremylt }
1824d7b241e6Sjeremylt 
1825d7b241e6Sjeremylt /**
1826ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
1827d7b241e6Sjeremylt 
1828ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
1829b11c1e72Sjeremylt 
1830b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1831dfdf5a53Sjeremylt 
18327a982d89SJeremy L. Thompson   @ref User
1833b11c1e72Sjeremylt **/
1834d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1835ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1836ad6481ceSJeremy L Thompson     *op = NULL;
1837ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1838ad6481ceSJeremy L Thompson   }
18392b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
18402b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1841fe2413ffSjeremylt   // Free fields
18422b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1843d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1844437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1845437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
184615910d16Sjeremylt       }
1847356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
18482b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
184971352a93Sjeremylt       }
18502b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
18512b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
185271352a93Sjeremylt       }
18532b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
18542b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1855fe2413ffSjeremylt     }
18562b730f8bSJeremy L Thompson   }
18572b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1858d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1859437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1860356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
18612b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
186271352a93Sjeremylt       }
18632b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
18642b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
186571352a93Sjeremylt       }
18662b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
18672b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
18682b730f8bSJeremy L Thompson     }
1869fe2413ffSjeremylt   }
187048acf710SJeremy L Thompson   // AtPoints data
187148acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
187248acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
187348acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
1874d1d35e2fSjeremylt   // Destroy sub_operators
18752b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1876d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
18772b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
187852d6035fSJeremy L Thompson     }
18792b730f8bSJeremy L Thompson   }
18802b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
18812b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
18822b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
18833668ca4bSJeremy L Thompson   // Destroy any composite labels
18845ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
18853668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
18862b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
18872b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
18883668ca4bSJeremy L Thompson     }
18895ac9af79SJeremy L Thompson   }
18902b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1891fe2413ffSjeremylt 
18925107b09fSJeremy L Thompson   // Destroy fallback
18932b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
18945107b09fSJeremy L Thompson 
1895ed9e99e6SJeremy L Thompson   // Destroy assembly data
18962b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
18972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
189870a7ffb3SJeremy L Thompson 
18992b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
19002b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
19012b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
19022b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
19032b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1904e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1905d7b241e6Sjeremylt }
1906d7b241e6Sjeremylt 
1907d7b241e6Sjeremylt /// @}
1908