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