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