xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision ef7f882e93b9bc65eaaf386e2d001a2e8e95cfc8)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed-impl.h>
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 /// @file
16 /// Implementation of CeedOperator interfaces
17 
18 /// ----------------------------------------------------------------------------
19 /// CeedOperator Library Internal Functions
20 /// ----------------------------------------------------------------------------
21 /// @addtogroup CeedOperatorDeveloper
22 /// @{
23 
24 /**
25   @brief Check if a `CeedOperator` Field matches the `CeedQFunction` Field
26 
27   @param[in] ceed     `Ceed` object for error handling
28   @param[in] qf_field `CeedQFunction` Field matching `CeedOperator` Field
29   @param[in] rstr     `CeedOperator` Field `CeedElemRestriction`
30   @param[in] basis    `CeedOperator` Field `CeedBasis`
31 
32   @return An error code: 0 - success, otherwise - failure
33 
34   @ref Developer
35 **/
36 static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction rstr, CeedBasis basis) {
37   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size = qf_field->size;
38   CeedEvalMode eval_mode = qf_field->eval_mode;
39 
40   // Restriction
41   CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
42             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43   if (rstr != CEED_ELEMRESTRICTION_NONE) {
44     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp));
45   }
46   // Basis
47   CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
48             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
49   if (basis != CEED_BASIS_NONE) {
50     CeedCall(CeedBasisGetDimension(basis, &dim));
51     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
52     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
53     CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
54               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT
55               " components, but CeedBasis has %" CeedInt_FMT " components",
56               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp, num_comp);
57   }
58   // Field size
59   switch (eval_mode) {
60     case CEED_EVAL_NONE:
61       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
62                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
63                 qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp);
64       break;
65     case CEED_EVAL_INTERP:
66     case CEED_EVAL_GRAD:
67     case CEED_EVAL_DIV:
68     case CEED_EVAL_CURL:
69       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
70                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components",
71                 qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72       break;
73     case CEED_EVAL_WEIGHT:
74       // No additional checks required
75       break;
76   }
77   return CEED_ERROR_SUCCESS;
78 }
79 
80 /**
81   @brief View a field of a `CeedOperator`
82 
83   @param[in] field        `CeedOperator` Field to view
84   @param[in] qf_field     `CeedQFunction` Field (carries field name)
85   @param[in] field_number Number of field being viewed
86   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87   @param[in] input        true for an input field; false for output field
88   @param[in] stream       Stream to view to, e.g., `stdout`
89 
90   @return An error code: 0 - success, otherwise - failure
91 
92   @ref Utility
93 **/
94 static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
95   const char *pre    = sub ? "  " : "";
96   const char *in_out = input ? "Input" : "Output";
97 
98   fprintf(stream,
99           "%s    %s field %" CeedInt_FMT
100           ":\n"
101           "%s      Name: \"%s\"\n",
102           pre, in_out, field_number, pre, qf_field->field_name);
103   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
104   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
105   if (field->basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
106   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
107   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
108   return CEED_ERROR_SUCCESS;
109 }
110 
111 /**
112   @brief View a single `CeedOperator`
113 
114   @param[in] op     `CeedOperator` to view
115   @param[in] sub    Boolean flag for sub-operator
116   @param[in] stream Stream to write; typically `stdout` or a file
117 
118   @return Error code: 0 - success, otherwise - failure
119 
120   @ref Utility
121 **/
122 int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
123   const char *pre = sub ? "  " : "";
124   CeedInt     num_elem, num_qpts, total_fields = 0;
125 
126   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
127   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
128   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
129 
130   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
131   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
132   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
133   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
134     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
135   }
136   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
137   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
138     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
139   }
140   return CEED_ERROR_SUCCESS;
141 }
142 
143 /**
144   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`
145 
146   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
147   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
148 
149   @return An error code: 0 - success, otherwise - failure
150 
151   @ref Developer
152 **/
153 int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
154   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
155   return CEED_ERROR_SUCCESS;
156 }
157 
158 /**
159   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`
160 
161   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
162   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
163   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
164 
165   @return An error code: 0 - success, otherwise - failure
166 
167   @ref Developer
168 **/
169 int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
170   Ceed ceed;
171 
172   CeedCall(CeedOperatorGetCeed(op, &ceed));
173   if (active_input_basis) {
174     *active_input_basis = NULL;
175     if (!op->is_composite) {
176       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
177         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
178           CeedCheck(!*active_input_basis || *active_input_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR,
179                     "Multiple active input CeedBases found");
180           *active_input_basis = op->input_fields[i]->basis;
181         }
182       }
183       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
184     }
185   }
186   if (active_output_basis) {
187     *active_output_basis = NULL;
188     if (!op->is_composite) {
189       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
190         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
191           CeedCheck(!*active_output_basis || *active_output_basis == op->output_fields[i]->basis, ceed, CEED_ERROR_MINOR,
192                     "Multiple active output CeedBases found");
193           *active_output_basis = op->output_fields[i]->basis;
194         }
195       }
196       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
197     }
198   }
199   return CEED_ERROR_SUCCESS;
200 }
201 
202 /**
203   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`
204 
205   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
206   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
207 
208   @return An error code: 0 - success, otherwise - failure
209 
210   @ref Utility
211 **/
212 int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
213   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
214   return CEED_ERROR_SUCCESS;
215 }
216 
217 /**
218   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`
219 
220   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
221   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
222   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
223 
224   @return An error code: 0 - success, otherwise - failure
225 
226   @ref Utility
227 **/
228 int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
229   Ceed ceed;
230 
231   CeedCall(CeedOperatorGetCeed(op, &ceed));
232   if (active_input_rstr) {
233     *active_input_rstr = NULL;
234     if (!op->is_composite) {
235       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
236         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
237           CeedCheck(!*active_input_rstr || *active_input_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
238                     "Multiple active input CeedElemRestrictions found");
239           *active_input_rstr = op->input_fields[i]->elem_rstr;
240         }
241       }
242       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
243     }
244   }
245   if (active_output_rstr) {
246     *active_output_rstr = NULL;
247     if (!op->is_composite) {
248       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
249         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
250           CeedCheck(!*active_output_rstr || *active_output_rstr == op->output_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
251                     "Multiple active output CeedElemRestrictions found");
252           *active_output_rstr = op->output_fields[i]->elem_rstr;
253         }
254       }
255       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
256     }
257   }
258   return CEED_ERROR_SUCCESS;
259 }
260 
261 /**
262   @brief Set `CeedQFunctionContext` field values of the specified type.
263 
264   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
265   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.
266 
267   @param[in,out] op          `CeedOperator`
268   @param[in]     field_label Label of field to set
269   @param[in]     field_type  Type of field to set
270   @param[in]     values      Values to set
271 
272   @return An error code: 0 - success, otherwise - failure
273 
274   @ref User
275 **/
276 static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
277   bool is_composite = false;
278 
279   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
280 
281   // Check if field_label and op correspond
282   if (field_label->from_op) {
283     CeedInt index = -1;
284 
285     for (CeedInt i = 0; i < op->num_context_labels; i++) {
286       if (op->context_labels[i] == field_label) index = i;
287     }
288     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
289   }
290 
291   CeedCall(CeedOperatorIsComposite(op, &is_composite));
292   if (is_composite) {
293     CeedInt       num_sub;
294     CeedOperator *sub_operators;
295 
296     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
297     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
298     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
299               "Composite operator modified after ContextFieldLabel created");
300 
301     for (CeedInt i = 0; i < num_sub; i++) {
302       // Try every sub-operator, ok if some sub-operators do not have field
303       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
304         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
305       }
306     }
307   } else {
308     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
309     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
310   }
311   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
312   return CEED_ERROR_SUCCESS;
313 }
314 
315 /**
316   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
317 
318   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
319   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.
320 
321   @param[in,out] op          `CeedOperator`
322   @param[in]     field_label Label of field to set
323   @param[in]     field_type  Type of field to set
324   @param[out]    num_values  Number of values of type `field_type` in array `values`
325   @param[out]    values      Values in the label
326 
327   @return An error code: 0 - success, otherwise - failure
328 
329   @ref User
330 **/
331 static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
332                                              void *values) {
333   bool is_composite = false;
334 
335   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
336 
337   *(void **)values = NULL;
338   *num_values      = 0;
339 
340   // Check if field_label and op correspond
341   if (field_label->from_op) {
342     CeedInt index = -1;
343 
344     for (CeedInt i = 0; i < op->num_context_labels; i++) {
345       if (op->context_labels[i] == field_label) index = i;
346     }
347     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
348   }
349 
350   CeedCall(CeedOperatorIsComposite(op, &is_composite));
351   if (is_composite) {
352     CeedInt       num_sub;
353     CeedOperator *sub_operators;
354 
355     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
356     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
357     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
358               "Composite operator modified after ContextFieldLabel created");
359 
360     for (CeedInt i = 0; i < num_sub; i++) {
361       // Try every sub-operator, ok if some sub-operators do not have field
362       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
363         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
364         return CEED_ERROR_SUCCESS;
365       }
366     }
367   } else {
368     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
369     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
370   }
371   return CEED_ERROR_SUCCESS;
372 }
373 
374 /**
375   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
376 
377   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
378   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.
379 
380   @param[in,out] op          `CeedOperator`
381   @param[in]     field_label Label of field to set
382   @param[in]     field_type  Type of field to set
383   @param[in]     values      Values array to restore
384 
385   @return An error code: 0 - success, otherwise - failure
386 
387   @ref User
388 **/
389 static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
390   bool is_composite = false;
391 
392   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
393 
394   // Check if field_label and op correspond
395   if (field_label->from_op) {
396     CeedInt index = -1;
397 
398     for (CeedInt i = 0; i < op->num_context_labels; i++) {
399       if (op->context_labels[i] == field_label) index = i;
400     }
401     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
402   }
403 
404   CeedCall(CeedOperatorIsComposite(op, &is_composite));
405   if (is_composite) {
406     CeedInt       num_sub;
407     CeedOperator *sub_operators;
408 
409     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
410     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
411     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
412               "Composite operator modified after ContextFieldLabel created");
413 
414     for (CeedInt i = 0; i < num_sub; i++) {
415       // Try every sub-operator, ok if some sub-operators do not have field
416       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
417         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
418         return CEED_ERROR_SUCCESS;
419       }
420     }
421   } else {
422     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
423     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
424   }
425   return CEED_ERROR_SUCCESS;
426 }
427 
428 /// @}
429 
430 /// ----------------------------------------------------------------------------
431 /// CeedOperator Backend API
432 /// ----------------------------------------------------------------------------
433 /// @addtogroup CeedOperatorBackend
434 /// @{
435 
436 /**
437   @brief Get the number of arguments associated with a `CeedOperator`
438 
439   @param[in]  op        `CeedOperator`
440   @param[out] num_args  Variable to store vector number of arguments
441 
442   @return An error code: 0 - success, otherwise - failure
443 
444   @ref Backend
445 **/
446 int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
447   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
448   *num_args = op->num_fields;
449   return CEED_ERROR_SUCCESS;
450 }
451 
452 /**
453   @brief Get the tensor product status of all bases for a `CeedOperator`.
454 
455   `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis.
456 
457   @param[in]  op               `CeedOperator`
458   @param[out] has_tensor_bases Variable to store tensor bases status
459 
460   @return An error code: 0 - success, otherwise - failure
461 
462   @ref Backend
463 **/
464 int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) {
465   CeedInt            num_inputs, num_outputs;
466   CeedOperatorField *input_fields, *output_fields;
467 
468   CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields));
469   *has_tensor_bases = true;
470   for (CeedInt i = 0; i < num_inputs; i++) {
471     bool      is_tensor;
472     CeedBasis basis;
473 
474     CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis));
475     if (basis != CEED_BASIS_NONE) {
476       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
477       *has_tensor_bases &= is_tensor;
478     }
479   }
480   for (CeedInt i = 0; i < num_outputs; i++) {
481     bool      is_tensor;
482     CeedBasis basis;
483 
484     CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis));
485     if (basis != CEED_BASIS_NONE) {
486       CeedCall(CeedBasisIsTensor(basis, &is_tensor));
487       *has_tensor_bases &= is_tensor;
488     }
489   }
490   return CEED_ERROR_SUCCESS;
491 }
492 
493 /**
494   @brief Get the setup status of a `CeedOperator`
495 
496   @param[in]  op            `CeedOperator`
497   @param[out] is_setup_done Variable to store setup status
498 
499   @return An error code: 0 - success, otherwise - failure
500 
501   @ref Backend
502 **/
503 int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
504   *is_setup_done = op->is_backend_setup;
505   return CEED_ERROR_SUCCESS;
506 }
507 
508 /**
509   @brief Get the `CeedQFunction` associated with a `CeedOperator`
510 
511   @param[in]  op `CeedOperator`
512   @param[out] qf Variable to store `CeedQFunction`
513 
514   @return An error code: 0 - success, otherwise - failure
515 
516   @ref Backend
517 **/
518 int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
519   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
520   *qf = op->qf;
521   return CEED_ERROR_SUCCESS;
522 }
523 
524 /**
525   @brief Get a boolean value indicating if the `CeedOperator` is composite
526 
527   @param[in]  op           `CeedOperator`
528   @param[out] is_composite Variable to store composite status
529 
530   @return An error code: 0 - success, otherwise - failure
531 
532   @ref Backend
533 **/
534 int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
535   *is_composite = op->is_composite;
536   return CEED_ERROR_SUCCESS;
537 }
538 
539 /**
540   @brief Get the backend data of a `CeedOperator`
541 
542   @param[in]  op   `CeedOperator`
543   @param[out] data Variable to store data
544 
545   @return An error code: 0 - success, otherwise - failure
546 
547   @ref Backend
548 **/
549 int CeedOperatorGetData(CeedOperator op, void *data) {
550   *(void **)data = op->data;
551   return CEED_ERROR_SUCCESS;
552 }
553 
554 /**
555   @brief Set the backend data of a `CeedOperator`
556 
557   @param[in,out] op   `CeedOperator`
558   @param[in]     data Data to set
559 
560   @return An error code: 0 - success, otherwise - failure
561 
562   @ref Backend
563 **/
564 int CeedOperatorSetData(CeedOperator op, void *data) {
565   op->data = data;
566   return CEED_ERROR_SUCCESS;
567 }
568 
569 /**
570   @brief Increment the reference counter for a `CeedOperator`
571 
572   @param[in,out] op `CeedOperator` to increment the reference counter
573 
574   @return An error code: 0 - success, otherwise - failure
575 
576   @ref Backend
577 **/
578 int CeedOperatorReference(CeedOperator op) {
579   op->ref_count++;
580   return CEED_ERROR_SUCCESS;
581 }
582 
583 /**
584   @brief Set the setup flag of a `CeedOperator` to `true`
585 
586   @param[in,out] op `CeedOperator`
587 
588   @return An error code: 0 - success, otherwise - failure
589 
590   @ref Backend
591 **/
592 int CeedOperatorSetSetupDone(CeedOperator op) {
593   op->is_backend_setup = true;
594   return CEED_ERROR_SUCCESS;
595 }
596 
597 /// @}
598 
599 /// ----------------------------------------------------------------------------
600 /// CeedOperator Public API
601 /// ----------------------------------------------------------------------------
602 /// @addtogroup CeedOperatorUser
603 /// @{
604 
605 /**
606   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
607 
608   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
609 
610   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
611   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
612   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
613   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
614   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
615 
616   @return An error code: 0 - success, otherwise - failure
617 
618   @ref User
619  */
620 int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
621   if (!ceed->OperatorCreate) {
622     Ceed delegate;
623 
624     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
625     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreate");
626     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
627     return CEED_ERROR_SUCCESS;
628   }
629 
630   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
631 
632   CeedCall(CeedCalloc(1, op));
633   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
634   (*op)->ref_count   = 1;
635   (*op)->input_size  = -1;
636   (*op)->output_size = -1;
637   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
638   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
639   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
640   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
641   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
642   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
643   CeedCall(ceed->OperatorCreate(*op));
644   return CEED_ERROR_SUCCESS;
645 }
646 
647 /**
648   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
649 
650   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
651   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
652 
653   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
654   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
655   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
656   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
657   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
658 
659   @return An error code: 0 - success, otherwise - failure
660 
661   @ref User
662  */
663 int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
664   if (!ceed->OperatorCreateAtPoints) {
665     Ceed delegate;
666 
667     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
668     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreateAtPoints");
669     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
670     return CEED_ERROR_SUCCESS;
671   }
672 
673   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
674 
675   CeedCall(CeedCalloc(1, op));
676   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
677   (*op)->ref_count    = 1;
678   (*op)->is_at_points = true;
679   (*op)->input_size   = -1;
680   (*op)->output_size  = -1;
681   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
682   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
683   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
684   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
685   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
686   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
687   CeedCall(ceed->OperatorCreateAtPoints(*op));
688   return CEED_ERROR_SUCCESS;
689 }
690 
691 /**
692   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
693 
694   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
695   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
696 
697   @return An error code: 0 - success, otherwise - failure
698 
699   @ref User
700  */
701 int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
702   if (!ceed->CompositeOperatorCreate) {
703     Ceed delegate;
704 
705     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
706     if (delegate) {
707       CeedCall(CeedCompositeOperatorCreate(delegate, op));
708       return CEED_ERROR_SUCCESS;
709     }
710   }
711 
712   CeedCall(CeedCalloc(1, op));
713   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
714   (*op)->ref_count    = 1;
715   (*op)->is_composite = true;
716   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
717   (*op)->input_size  = -1;
718   (*op)->output_size = -1;
719 
720   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
721   return CEED_ERROR_SUCCESS;
722 }
723 
724 /**
725   @brief Copy the pointer to a `CeedOperator`.
726 
727   Both pointers should be destroyed with @ref CeedOperatorDestroy().
728 
729   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`.
730         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
731 
732   @param[in]     op      `CeedOperator` to copy reference to
733   @param[in,out] op_copy Variable to store copied reference
734 
735   @return An error code: 0 - success, otherwise - failure
736 
737   @ref User
738 **/
739 int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
740   CeedCall(CeedOperatorReference(op));
741   CeedCall(CeedOperatorDestroy(op_copy));
742   *op_copy = op;
743   return CEED_ERROR_SUCCESS;
744 }
745 
746 /**
747   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
748 
749   This function is used to specify both active and passive fields to a `CeedOperator`.
750   For passive fields, a `CeedVector` `vec` must be provided.
751   Passive fields can inputs or outputs (updated in-place when operator is applied).
752 
753   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
754   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
755 
756   The number of quadrature points must agree across all points.
757   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
758 
759   @param[in,out] op         `CeedOperator` on which to provide the field
760   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
761   @param[in]     rstr       `CeedElemRestriction`
762   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
763   @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`
764 
765   @return An error code: 0 - success, otherwise - failure
766 
767   @ref User
768 **/
769 int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
770   bool               is_input = true, is_at_points;
771   CeedInt            num_elem = 0, num_qpts = 0;
772   CeedQFunctionField qf_field;
773   CeedOperatorField *op_field;
774 
775   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
776   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
777   CeedCheck(rstr, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
778   CeedCheck(basis, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
779   CeedCheck(vec, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
780 
781   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
782   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
783   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
784             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
785   {
786     CeedRestrictionType rstr_type;
787 
788     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
789     if (rstr_type == CEED_RESTRICTION_POINTS) {
790       CeedCheck(is_at_points, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields");
791       CeedCheck(basis == CEED_BASIS_NONE, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
792       if (!op->first_points_rstr) {
793         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
794       } else {
795         bool are_compatible;
796 
797         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
798         CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
799                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
800       }
801     }
802   }
803 
804   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
805   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
806   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
807             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
808             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
809             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
810   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
811     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
812       qf_field = op->qf->input_fields[i];
813       op_field = &op->input_fields[i];
814       goto found;
815     }
816   }
817   is_input = false;
818   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
819     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
820       qf_field = op->qf->output_fields[i];
821       op_field = &op->output_fields[i];
822       goto found;
823     }
824   }
825   // LCOV_EXCL_START
826   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
827   // LCOV_EXCL_STOP
828 found:
829   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, rstr, basis));
830   CeedCall(CeedCalloc(1, op_field));
831 
832   if (vec == CEED_VECTOR_ACTIVE) {
833     CeedSize l_size;
834 
835     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
836     if (is_input) {
837       if (op->input_size == -1) op->input_size = l_size;
838       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE,
839                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size);
840     } else {
841       if (op->output_size == -1) op->output_size = l_size;
842       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE,
843                 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size);
844     }
845   }
846 
847   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
848   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
849   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
850     op->num_elem        = num_elem;
851     op->has_restriction = true;  // Restriction set, but num_elem may be 0
852   }
853   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
854   if (op->num_qpts == 0 && !is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
855   op->num_fields += 1;
856   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
857   return CEED_ERROR_SUCCESS;
858 }
859 
860 /**
861   @brief Get the `CeedOperator` Field of a `CeedOperator`.
862 
863   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
864 
865   @param[in]  op                `CeedOperator`
866   @param[out] num_input_fields  Variable to store number of input fields
867   @param[out] input_fields      Variable to store input fields
868   @param[out] num_output_fields Variable to store number of output fields
869   @param[out] output_fields     Variable to store output fields
870 
871   @return An error code: 0 - success, otherwise - failure
872 
873   @ref Advanced
874 **/
875 int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
876                           CeedOperatorField **output_fields) {
877   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
878   CeedCall(CeedOperatorCheckReady(op));
879 
880   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
881   if (input_fields) *input_fields = op->input_fields;
882   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
883   if (output_fields) *output_fields = op->output_fields;
884   return CEED_ERROR_SUCCESS;
885 }
886 
887 /**
888   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
889 
890   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
891 
892   @param[in,out] op           `CeedOperator` at points
893   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
894   @param[in]     point_coords `CeedVector` holding coordinates of each point
895 
896   @return An error code: 0 - success, otherwise - failure
897 
898   @ref Advanced
899 **/
900 int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
901   bool is_at_points;
902 
903   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
904 
905   CeedCheck(is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
906   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
907 
908   if (!op->first_points_rstr) {
909     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
910   } else {
911     bool are_compatible;
912 
913     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
914     CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
915               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
916   }
917 
918   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
919   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
920   return CEED_ERROR_SUCCESS;
921 }
922 
923 /**
924   @brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
925 
926   @param[in]  op           `CeedOperator`
927   @param[out] is_at_points Variable to store at points status
928 
929   @return An error code: 0 - success, otherwise - failure
930 
931   @ref User
932 **/
933 int CeedOperatorIsAtPoints(CeedOperator op, bool *is_at_points) {
934   *is_at_points = op->is_at_points;
935   return CEED_ERROR_SUCCESS;
936 }
937 
938 /**
939   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
940 
941   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
942 
943   @param[in]  op           `CeedOperator` at points
944   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
945   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
946 
947   @return An error code: 0 - success, otherwise - failure
948 
949   @ref Advanced
950 **/
951 int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
952   bool is_at_points;
953 
954   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
955   CeedCheck(is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
956   CeedCall(CeedOperatorCheckReady(op));
957 
958   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
959   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
960   return CEED_ERROR_SUCCESS;
961 }
962 
963 /**
964   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name.
965 
966   `op_field` is set to `NULL` if the field is not found.
967 
968   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
969 
970   @param[in]  op         `CeedOperator`
971   @param[in]  field_name Name of desired `CeedOperator` Field
972   @param[out] op_field   `CeedOperator` Field corresponding to the name
973 
974   @return An error code: 0 - success, otherwise - failure
975 
976   @ref Advanced
977 **/
978 int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
979   char              *name;
980   CeedInt            num_input_fields, num_output_fields;
981   CeedOperatorField *input_fields, *output_fields;
982 
983   *op_field = NULL;
984   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
985   for (CeedInt i = 0; i < num_input_fields; i++) {
986     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
987     if (!strcmp(name, field_name)) {
988       *op_field = input_fields[i];
989       return CEED_ERROR_SUCCESS;
990     }
991   }
992   for (CeedInt i = 0; i < num_output_fields; i++) {
993     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
994     if (!strcmp(name, field_name)) {
995       *op_field = output_fields[i];
996       return CEED_ERROR_SUCCESS;
997     }
998   }
999   return CEED_ERROR_SUCCESS;
1000 }
1001 
1002 /**
1003   @brief Get the name of a `CeedOperator` Field
1004 
1005   @param[in]  op_field    `CeedOperator` Field
1006   @param[out] field_name  Variable to store the field name
1007 
1008   @return An error code: 0 - success, otherwise - failure
1009 
1010   @ref Advanced
1011 **/
1012 int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
1013   *field_name = (char *)op_field->field_name;
1014   return CEED_ERROR_SUCCESS;
1015 }
1016 
1017 /**
1018   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field
1019 
1020   @param[in]  op_field `CeedOperator` Field
1021   @param[out] rstr     Variable to store `CeedElemRestriction`
1022 
1023   @return An error code: 0 - success, otherwise - failure
1024 
1025   @ref Advanced
1026 **/
1027 int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
1028   *rstr = op_field->elem_rstr;
1029   return CEED_ERROR_SUCCESS;
1030 }
1031 
1032 /**
1033   @brief Get the `CeedBasis` of a `CeedOperator` Field
1034 
1035   @param[in]  op_field `CeedOperator` Field
1036   @param[out] basis    Variable to store `CeedBasis`
1037 
1038   @return An error code: 0 - success, otherwise - failure
1039 
1040   @ref Advanced
1041 **/
1042 int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
1043   *basis = op_field->basis;
1044   return CEED_ERROR_SUCCESS;
1045 }
1046 
1047 /**
1048   @brief Get the `CeedVector` of a `CeedOperator` Field
1049 
1050   @param[in]  op_field `CeedOperator` Field
1051   @param[out] vec      Variable to store `CeedVector`
1052 
1053   @return An error code: 0 - success, otherwise - failure
1054 
1055   @ref Advanced
1056 **/
1057 int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
1058   *vec = op_field->vec;
1059   return CEED_ERROR_SUCCESS;
1060 }
1061 
1062 /**
1063   @brief Add a sub-operator to a composite `CeedOperator`
1064 
1065   @param[in,out] composite_op Composite `CeedOperator`
1066   @param[in]     sub_op       Sub-operator `CeedOperator`
1067 
1068   @return An error code: 0 - success, otherwise - failure
1069 
1070   @ref User
1071  */
1072 int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
1073   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
1074   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
1075   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1076 
1077   {
1078     CeedSize input_size, output_size;
1079 
1080     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
1081     if (composite_op->input_size == -1) composite_op->input_size = input_size;
1082     if (composite_op->output_size == -1) composite_op->output_size = output_size;
1083     // Note, a size of -1 means no active vector restriction set, so no incompatibility
1084     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
1085               composite_op->ceed, CEED_ERROR_MAJOR,
1086               "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1087               ") not compatible with sub-operator of "
1088               "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
1089               composite_op->input_size, composite_op->output_size, input_size, output_size);
1090   }
1091 
1092   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
1093   CeedCall(CeedOperatorReference(sub_op));
1094   composite_op->num_suboperators++;
1095   return CEED_ERROR_SUCCESS;
1096 }
1097 
1098 /**
1099   @brief Get the number of sub-operators associated with a `CeedOperator`
1100 
1101   @param[in]  op               `CeedOperator`
1102   @param[out] num_suboperators Variable to store number of sub-operators
1103 
1104   @return An error code: 0 - success, otherwise - failure
1105 
1106   @ref Backend
1107 **/
1108 int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
1109   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
1110   *num_suboperators = op->num_suboperators;
1111   return CEED_ERROR_SUCCESS;
1112 }
1113 
1114 /**
1115   @brief Get the list of sub-operators associated with a `CeedOperator`
1116 
1117   @param[in]  op             `CeedOperator`
1118   @param[out] sub_operators  Variable to store list of sub-operators
1119 
1120   @return An error code: 0 - success, otherwise - failure
1121 
1122   @ref Backend
1123 **/
1124 int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
1125   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
1126   *sub_operators = op->sub_operators;
1127   return CEED_ERROR_SUCCESS;
1128 }
1129 
1130 /**
1131   @brief Check if a `CeedOperator` is ready to be used.
1132 
1133   @param[in] op `CeedOperator` to check
1134 
1135   @return An error code: 0 - success, otherwise - failure
1136 
1137   @ref User
1138 **/
1139 int CeedOperatorCheckReady(CeedOperator op) {
1140   Ceed ceed;
1141   bool is_at_points;
1142 
1143   CeedCall(CeedOperatorGetCeed(op, &ceed));
1144   CeedCall(CeedOperatorIsAtPoints(op, &is_at_points));
1145 
1146   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
1147 
1148   CeedQFunction qf = op->qf;
1149   if (op->is_composite) {
1150     if (!op->num_suboperators) {
1151       // Empty operator setup
1152       op->input_size  = 0;
1153       op->output_size = 0;
1154     } else {
1155       for (CeedInt i = 0; i < op->num_suboperators; i++) {
1156         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
1157       }
1158       // Sub-operators could be modified after adding to composite operator
1159       // Need to verify no lvec incompatibility from any changes
1160       CeedSize input_size, output_size;
1161       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
1162     }
1163   } else {
1164     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
1165     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
1166     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
1167     CeedCheck(op->num_qpts > 0 || is_at_points, ceed, CEED_ERROR_INCOMPLETE,
1168               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
1169   }
1170 
1171   // Flag as immutable and ready
1172   op->is_interface_setup = true;
1173   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
1174   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
1175   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
1176   return CEED_ERROR_SUCCESS;
1177 }
1178 
1179 /**
1180   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
1181 
1182   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1183 
1184   @param[in]  op          `CeedOperator`
1185   @param[out] input_size  Variable to store active input vector length, or `NULL`
1186   @param[out] output_size Variable to store active output vector length, or `NULL`
1187 
1188   @return An error code: 0 - success, otherwise - failure
1189 
1190   @ref User
1191 **/
1192 int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1193   bool is_composite;
1194 
1195   if (input_size) *input_size = op->input_size;
1196   if (output_size) *output_size = op->output_size;
1197 
1198   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1199   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1200     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1201       CeedSize sub_input_size, sub_output_size;
1202 
1203       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
1204       if (op->input_size == -1) op->input_size = sub_input_size;
1205       if (op->output_size == -1) op->output_size = sub_output_size;
1206       // Note, a size of -1 means no active vector restriction set, so no incompatibility
1207       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed,
1208                 CEED_ERROR_MAJOR,
1209                 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT
1210                 ") not compatible with sub-operator of "
1211                 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")",
1212                 op->input_size, op->output_size, input_size, output_size);
1213     }
1214   }
1215   return CEED_ERROR_SUCCESS;
1216 }
1217 
1218 /**
1219   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
1220 
1221   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1222   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
1223 
1224   @param[in] op                  `CeedOperator`
1225   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
1226 
1227   @return An error code: 0 - success, otherwise - failure
1228 
1229   @ref Advanced
1230 **/
1231 int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
1232   bool is_composite;
1233 
1234   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1235   if (is_composite) {
1236     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1237       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
1238     }
1239   } else {
1240     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1241   }
1242   return CEED_ERROR_SUCCESS;
1243 }
1244 
1245 /**
1246   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1247 
1248   @param[in] op                `CeedOperator`
1249   @param[in] needs_data_update Boolean flag setting assembly data reuse
1250 
1251   @return An error code: 0 - success, otherwise - failure
1252 
1253   @ref Advanced
1254 **/
1255 int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1256   bool is_composite;
1257 
1258   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1259   if (is_composite) {
1260     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1261       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1262     }
1263   } else {
1264     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
1265   }
1266   return CEED_ERROR_SUCCESS;
1267 }
1268 
1269 /**
1270   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1271 
1272   @param[in,out] op   `CeedOperator`
1273   @param[in]     name Name to set, or NULL to remove previously set name
1274 
1275   @return An error code: 0 - success, otherwise - failure
1276 
1277   @ref User
1278 **/
1279 int CeedOperatorSetName(CeedOperator op, const char *name) {
1280   char  *name_copy;
1281   size_t name_len = name ? strlen(name) : 0;
1282 
1283   CeedCall(CeedFree(&op->name));
1284   if (name_len > 0) {
1285     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1286     memcpy(name_copy, name, name_len);
1287     op->name = name_copy;
1288   }
1289   return CEED_ERROR_SUCCESS;
1290 }
1291 
1292 /**
1293   @brief View a `CeedOperator`
1294 
1295   @param[in] op     `CeedOperator` to view
1296   @param[in] stream Stream to write; typically `stdout` or a file
1297 
1298   @return Error code: 0 - success, otherwise - failure
1299 
1300   @ref User
1301 **/
1302 int CeedOperatorView(CeedOperator op, FILE *stream) {
1303   bool has_name = op->name;
1304 
1305   if (op->is_composite) {
1306     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1307 
1308     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1309       has_name = op->sub_operators[i]->name;
1310       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
1311       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
1312     }
1313   } else {
1314     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
1315     CeedCall(CeedOperatorSingleView(op, 0, stream));
1316   }
1317   return CEED_ERROR_SUCCESS;
1318 }
1319 
1320 /**
1321   @brief Get the `Ceed` associated with a `CeedOperator`
1322 
1323   @param[in]  op   `CeedOperator`
1324   @param[out] ceed Variable to store `Ceed`
1325 
1326   @return An error code: 0 - success, otherwise - failure
1327 
1328   @ref Advanced
1329 **/
1330 int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1331   *ceed = op->ceed;
1332   return CEED_ERROR_SUCCESS;
1333 }
1334 
1335 /**
1336   @brief Get the number of elements associated with a `CeedOperator`
1337 
1338   @param[in]  op       `CeedOperator`
1339   @param[out] num_elem Variable to store number of elements
1340 
1341   @return An error code: 0 - success, otherwise - failure
1342 
1343   @ref Advanced
1344 **/
1345 int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
1346   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1347   *num_elem = op->num_elem;
1348   return CEED_ERROR_SUCCESS;
1349 }
1350 
1351 /**
1352   @brief Get the number of quadrature points associated with a `CeedOperator`
1353 
1354   @param[in]  op       `CeedOperator`
1355   @param[out] num_qpts Variable to store vector number of quadrature points
1356 
1357   @return An error code: 0 - success, otherwise - failure
1358 
1359   @ref Advanced
1360 **/
1361 int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
1362   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1363   *num_qpts = op->num_qpts;
1364   return CEED_ERROR_SUCCESS;
1365 }
1366 
1367 /**
1368   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
1369 
1370   @param[in]  op    `CeedOperator` to estimate FLOPs for
1371   @param[out] flops Address of variable to hold FLOPs estimate
1372 
1373   @ref Backend
1374 **/
1375 int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
1376   bool is_composite;
1377 
1378   CeedCall(CeedOperatorCheckReady(op));
1379 
1380   *flops = 0;
1381   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1382   if (is_composite) {
1383     CeedInt num_suboperators;
1384 
1385     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1386     CeedOperator *sub_operators;
1387     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1388 
1389     // FLOPs for each suboperator
1390     for (CeedInt i = 0; i < num_suboperators; i++) {
1391       CeedSize suboperator_flops;
1392 
1393       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
1394       *flops += suboperator_flops;
1395     }
1396   } else {
1397     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
1398     CeedOperatorField *input_fields, *output_fields;
1399 
1400     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1401     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1402 
1403     // Input FLOPs
1404     for (CeedInt i = 0; i < num_input_fields; i++) {
1405       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1406         CeedSize rstr_flops, basis_flops;
1407 
1408         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1409         *flops += rstr_flops;
1410         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
1411         *flops += basis_flops * num_elem;
1412       }
1413     }
1414     // QF FLOPs
1415     {
1416       CeedInt  num_qpts;
1417       CeedSize qf_flops;
1418 
1419       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1420       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
1421       CeedCheck(qf_flops > -1, op->ceed, CEED_ERROR_INCOMPLETE, "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate");
1422       *flops += num_elem * num_qpts * qf_flops;
1423     }
1424 
1425     // Output FLOPs
1426     for (CeedInt i = 0; i < num_output_fields; i++) {
1427       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1428         CeedSize rstr_flops, basis_flops;
1429 
1430         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1431         *flops += rstr_flops;
1432         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
1433         *flops += basis_flops * num_elem;
1434       }
1435     }
1436   }
1437   return CEED_ERROR_SUCCESS;
1438 }
1439 
1440 /**
1441   @brief Get `CeedQFunction` global context for a `CeedOperator`.
1442 
1443   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1444 
1445   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`.
1446         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
1447 
1448   @param[in]  op  `CeedOperator`
1449   @param[out] ctx Variable to store `CeedQFunctionContext`
1450 
1451   @return An error code: 0 - success, otherwise - failure
1452 
1453   @ref Advanced
1454 **/
1455 int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
1456   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
1457   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
1458   else *ctx = NULL;
1459   return CEED_ERROR_SUCCESS;
1460 }
1461 
1462 /**
1463   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
1464 
1465   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1466 
1467   @param[in]  op          `CeedOperator`
1468   @param[in]  field_name  Name of field to retrieve label
1469   @param[out] field_label Variable to field label
1470 
1471   @return An error code: 0 - success, otherwise - failure
1472 
1473   @ref User
1474 **/
1475 int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
1476   bool is_composite, field_found = false;
1477 
1478   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1479 
1480   if (is_composite) {
1481     // Composite operator
1482     // -- Check if composite label already created
1483     for (CeedInt i = 0; i < op->num_context_labels; i++) {
1484       if (!strcmp(op->context_labels[i]->name, field_name)) {
1485         *field_label = op->context_labels[i];
1486         return CEED_ERROR_SUCCESS;
1487       }
1488     }
1489 
1490     // -- Create composite label if needed
1491     CeedInt               num_sub;
1492     CeedOperator         *sub_operators;
1493     CeedContextFieldLabel new_field_label;
1494 
1495     CeedCall(CeedCalloc(1, &new_field_label));
1496     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1497     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1498     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
1499     new_field_label->num_sub_labels = num_sub;
1500 
1501     for (CeedInt i = 0; i < num_sub; i++) {
1502       if (sub_operators[i]->qf->ctx) {
1503         CeedContextFieldLabel new_field_label_i;
1504 
1505         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
1506         if (new_field_label_i) {
1507           field_found                    = true;
1508           new_field_label->sub_labels[i] = new_field_label_i;
1509           new_field_label->name          = new_field_label_i->name;
1510           new_field_label->description   = new_field_label_i->description;
1511           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
1512             // LCOV_EXCL_START
1513             CeedCall(CeedFree(&new_field_label));
1514             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
1515                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
1516             // LCOV_EXCL_STOP
1517           } else {
1518             new_field_label->type = new_field_label_i->type;
1519           }
1520           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
1521             // LCOV_EXCL_START
1522             CeedCall(CeedFree(&new_field_label));
1523             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %zu != %zu",
1524                              new_field_label->num_values, new_field_label_i->num_values);
1525             // LCOV_EXCL_STOP
1526           } else {
1527             new_field_label->num_values = new_field_label_i->num_values;
1528           }
1529         }
1530       }
1531     }
1532     // -- Cleanup if field was found
1533     if (field_found) {
1534       *field_label = new_field_label;
1535     } else {
1536       // LCOV_EXCL_START
1537       CeedCall(CeedFree(&new_field_label->sub_labels));
1538       CeedCall(CeedFree(&new_field_label));
1539       *field_label = NULL;
1540       // LCOV_EXCL_STOP
1541     }
1542   } else {
1543     // Single, non-composite operator
1544     if (op->qf->ctx) {
1545       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1546     } else {
1547       *field_label = NULL;
1548     }
1549   }
1550 
1551   // Set label in operator
1552   if (*field_label) {
1553     (*field_label)->from_op = true;
1554 
1555     // Move new composite label to operator
1556     if (op->num_context_labels == 0) {
1557       CeedCall(CeedCalloc(1, &op->context_labels));
1558       op->max_context_labels = 1;
1559     } else if (op->num_context_labels == op->max_context_labels) {
1560       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
1561       op->max_context_labels *= 2;
1562     }
1563     op->context_labels[op->num_context_labels] = *field_label;
1564     op->num_context_labels++;
1565   }
1566   return CEED_ERROR_SUCCESS;
1567 }
1568 
1569 /**
1570   @brief Set `CeedQFunctionContext` field holding double precision values.
1571 
1572   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1573 
1574   @param[in,out] op          `CeedOperator`
1575   @param[in]     field_label Label of field to set
1576   @param[in]     values      Values to set
1577 
1578   @return An error code: 0 - success, otherwise - failure
1579 
1580   @ref User
1581 **/
1582 int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
1583   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1584 }
1585 
1586 /**
1587   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
1588 
1589   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
1590 
1591   @param[in]  op          `CeedOperator`
1592   @param[in]  field_label Label of field to get
1593   @param[out] num_values  Number of values in the field label
1594   @param[out] values      Pointer to context values
1595 
1596   @return An error code: 0 - success, otherwise - failure
1597 
1598   @ref User
1599 **/
1600 int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
1601   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
1602 }
1603 
1604 /**
1605   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
1606 
1607   @param[in]  op          `CeedOperator`
1608   @param[in]  field_label Label of field to restore
1609   @param[out] values      Pointer to context values
1610 
1611   @return An error code: 0 - success, otherwise - failure
1612 
1613   @ref User
1614 **/
1615 int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
1616   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1617 }
1618 
1619 /**
1620   @brief Set `CeedQFunctionContext` field holding `int32` values.
1621 
1622   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1623 
1624   @param[in,out] op          `CeedOperator`
1625   @param[in]     field_label Label of field to set
1626   @param[in]     values      Values to set
1627 
1628   @return An error code: 0 - success, otherwise - failure
1629 
1630   @ref User
1631 **/
1632 int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
1633   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1634 }
1635 
1636 /**
1637   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
1638 
1639   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
1640 
1641   @param[in]  op          `CeedOperator`
1642   @param[in]  field_label Label of field to get
1643   @param[out] num_values  Number of `int32` values in `values`
1644   @param[out] values      Pointer to context values
1645 
1646   @return An error code: 0 - success, otherwise - failure
1647 
1648   @ref User
1649 **/
1650 int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
1651   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
1652 }
1653 
1654 /**
1655   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
1656 
1657   @param[in]  op          `CeedOperator`
1658   @param[in]  field_label Label of field to get
1659   @param[out] values      Pointer to context values
1660 
1661   @return An error code: 0 - success, otherwise - failure
1662 
1663   @ref User
1664 **/
1665 int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
1666   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1667 }
1668 
1669 /**
1670   @brief Set `CeedQFunctionContext` field holding boolean values.
1671 
1672   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1673 
1674   @param[in,out] op          `CeedOperator`
1675   @param[in]     field_label Label of field to set
1676   @param[in]     values      Values to set
1677 
1678   @return An error code: 0 - success, otherwise - failure
1679 
1680   @ref User
1681 **/
1682 int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
1683   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
1684 }
1685 
1686 /**
1687   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
1688 
1689   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
1690 
1691   @param[in]  op          `CeedOperator`
1692   @param[in]  field_label Label of field to get
1693   @param[out] num_values  Number of boolean values in `values`
1694   @param[out] values      Pointer to context values
1695 
1696   @return An error code: 0 - success, otherwise - failure
1697 
1698   @ref User
1699 **/
1700 int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
1701   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
1702 }
1703 
1704 /**
1705   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
1706 
1707   @param[in]  op          `CeedOperator`
1708   @param[in]  field_label Label of field to get
1709   @param[out] values      Pointer to context values
1710 
1711   @return An error code: 0 - success, otherwise - failure
1712 
1713   @ref User
1714 **/
1715 int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
1716   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
1717 }
1718 
1719 /**
1720   @brief Apply `CeedOperator` to a `CeedVector`.
1721 
1722   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1723   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1724 
1725   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1726 
1727   @param[in]  op      `CeedOperator` to apply
1728   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1729   @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
1730   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1731 
1732   @return An error code: 0 - success, otherwise - failure
1733 
1734   @ref User
1735 **/
1736 int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
1737   CeedCall(CeedOperatorCheckReady(op));
1738 
1739   if (op->is_composite) {
1740     // Composite Operator
1741     if (op->ApplyComposite) {
1742       CeedCall(op->ApplyComposite(op, in, out, request));
1743     } else {
1744       CeedInt       num_suboperators;
1745       CeedOperator *sub_operators;
1746 
1747       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1748       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1749 
1750       // Zero all output vectors
1751       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1752       for (CeedInt i = 0; i < num_suboperators; i++) {
1753         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1754           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1755 
1756           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
1757             CeedCall(CeedVectorSetValue(vec, 0.0));
1758           }
1759         }
1760       }
1761       // Apply
1762       for (CeedInt i = 0; i < num_suboperators; i++) {
1763         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1764       }
1765     }
1766   } else {
1767     // Standard Operator
1768     if (op->Apply) {
1769       CeedCall(op->Apply(op, in, out, request));
1770     } else {
1771       // Zero all output vectors
1772       CeedQFunction qf = op->qf;
1773 
1774       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1775         CeedVector vec = op->output_fields[i]->vec;
1776 
1777         if (vec == CEED_VECTOR_ACTIVE) vec = out;
1778         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
1779       }
1780       // Apply
1781       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
1782     }
1783   }
1784   return CEED_ERROR_SUCCESS;
1785 }
1786 
1787 /**
1788   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
1789 
1790   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1791   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1792 
1793   @param[in]  op      `CeedOperator` to apply
1794   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1795   @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
1796   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1797 
1798   @return An error code: 0 - success, otherwise - failure
1799 
1800   @ref User
1801 **/
1802 int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
1803   CeedCall(CeedOperatorCheckReady(op));
1804 
1805   if (op->is_composite) {
1806     // Composite Operator
1807     if (op->ApplyAddComposite) {
1808       CeedCall(op->ApplyAddComposite(op, in, out, request));
1809     } else {
1810       CeedInt       num_suboperators;
1811       CeedOperator *sub_operators;
1812 
1813       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1814       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1815       for (CeedInt i = 0; i < num_suboperators; i++) {
1816         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1817       }
1818     }
1819   } else if (op->num_elem) {
1820     // Standard Operator
1821     CeedCall(op->ApplyAdd(op, in, out, request));
1822   }
1823   return CEED_ERROR_SUCCESS;
1824 }
1825 
1826 /**
1827   @brief Destroy a `CeedOperator`
1828 
1829   @param[in,out] op `CeedOperator` to destroy
1830 
1831   @return An error code: 0 - success, otherwise - failure
1832 
1833   @ref User
1834 **/
1835 int CeedOperatorDestroy(CeedOperator *op) {
1836   if (!*op || --(*op)->ref_count > 0) {
1837     *op = NULL;
1838     return CEED_ERROR_SUCCESS;
1839   }
1840   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
1841   CeedCall(CeedDestroy(&(*op)->ceed));
1842   // Free fields
1843   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1844     if ((*op)->input_fields[i]) {
1845       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1846         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
1847       }
1848       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
1849         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
1850       }
1851       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
1852         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
1853       }
1854       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
1855       CeedCall(CeedFree(&(*op)->input_fields[i]));
1856     }
1857   }
1858   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1859     if ((*op)->output_fields[i]) {
1860       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1861       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
1862         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
1863       }
1864       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
1865         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
1866       }
1867       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
1868       CeedCall(CeedFree(&(*op)->output_fields[i]));
1869     }
1870   }
1871   // AtPoints data
1872   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
1873   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
1874   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
1875   // Destroy sub_operators
1876   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1877     if ((*op)->sub_operators[i]) {
1878       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
1879     }
1880   }
1881   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
1882   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
1883   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
1884   // Destroy any composite labels
1885   if ((*op)->is_composite) {
1886     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
1887       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
1888       CeedCall(CeedFree(&(*op)->context_labels[i]));
1889     }
1890   }
1891   CeedCall(CeedFree(&(*op)->context_labels));
1892 
1893   // Destroy fallback
1894   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
1895 
1896   // Destroy assembly data
1897   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
1898   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
1899 
1900   CeedCall(CeedFree(&(*op)->input_fields));
1901   CeedCall(CeedFree(&(*op)->output_fields));
1902   CeedCall(CeedFree(&(*op)->sub_operators));
1903   CeedCall(CeedFree(&(*op)->name));
1904   CeedCall(CeedFree(op));
1905   return CEED_ERROR_SUCCESS;
1906 }
1907 
1908 /// @}
1909