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