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