xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 4385fb7f4421da9c7ce289d92dc27dadf16843ac)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25e15f9bd0SJeremy L Thompson   @brief Check if a CeedOperator Field matches the QFunction Field
26e15f9bd0SJeremy L Thompson 
27e15f9bd0SJeremy L Thompson   @param[in] ceed     Ceed object for error handling
28d1d35e2fSjeremylt   @param[in] qf_field QFunction Field matching Operator Field
29e15f9bd0SJeremy L Thompson   @param[in] r        Operator Field ElemRestriction
30e15f9bd0SJeremy L Thompson   @param[in] b        Operator Field Basis
31e15f9bd0SJeremy L Thompson 
32e15f9bd0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33e15f9bd0SJeremy L Thompson 
34e15f9bd0SJeremy L Thompson   @ref Developer
35e15f9bd0SJeremy L Thompson **/
362b730f8bSJeremy L Thompson static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction r, CeedBasis b) {
37d1d35e2fSjeremylt   CeedEvalMode eval_mode = qf_field->eval_mode;
38c4e3f59bSSebastian Grimberg   CeedInt      dim = 1, num_comp = 1, q_comp = 1, restr_num_comp = 1, size = qf_field->size;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
41e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
42d1d35e2fSjeremylt     if (eval_mode == CEED_EVAL_WEIGHT) {
43e15f9bd0SJeremy L Thompson       // LCOV_EXCL_START
442b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE should be used for a field with eval mode CEED_EVAL_WEIGHT");
45e15f9bd0SJeremy L Thompson       // LCOV_EXCL_STOP
46e15f9bd0SJeremy L Thompson     }
472b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp));
48e1e9e29dSJed Brown   }
49d1d35e2fSjeremylt   if ((r == CEED_ELEMRESTRICTION_NONE) != (eval_mode == CEED_EVAL_WEIGHT)) {
50e1e9e29dSJed Brown     // LCOV_EXCL_START
512b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
52e1e9e29dSJed Brown     // LCOV_EXCL_STOP
53e1e9e29dSJed Brown   }
54e15f9bd0SJeremy L Thompson   // Basis
55e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
562b730f8bSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
578229195eSjeremylt       // LCOV_EXCL_START
582b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with CEED_EVAL_NONE must be used with CEED_BASIS_COLLOCATED",
59d1d35e2fSjeremylt                        qf_field->field_name);
6041bdf1c9SJeremy L Thompson       // LCOV_EXCL_STOP
612b730f8bSJeremy L Thompson     }
622b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
632b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
64c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp));
65d1d35e2fSjeremylt     if (r != CEED_ELEMRESTRICTION_NONE && restr_num_comp != num_comp) {
66e15f9bd0SJeremy L Thompson       // LCOV_EXCL_START
67e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_DIMENSION,
682b730f8bSJeremy L Thompson                        "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT
692b730f8bSJeremy L Thompson                        " components, but Basis has %" CeedInt_FMT " components",
702b730f8bSJeremy L Thompson                        qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp);
71e15f9bd0SJeremy L Thompson       // LCOV_EXCL_STOP
72e15f9bd0SJeremy L Thompson     }
7341bdf1c9SJeremy L Thompson   } else if (eval_mode != CEED_EVAL_NONE) {
7441bdf1c9SJeremy L Thompson     // LCOV_EXCL_START
752b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with %s cannot be used with CEED_BASIS_COLLOCATED", qf_field->field_name,
762b730f8bSJeremy L Thompson                      CeedEvalModes[eval_mode]);
7741bdf1c9SJeremy L Thompson     // LCOV_EXCL_STOP
78e15f9bd0SJeremy L Thompson   }
79e15f9bd0SJeremy L Thompson   // Field size
80d1d35e2fSjeremylt   switch (eval_mode) {
81e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
822b730f8bSJeremy L Thompson       if (size != restr_num_comp) {
83e15f9bd0SJeremy L Thompson         // LCOV_EXCL_START
84e15f9bd0SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_DIMENSION,
85953dad27SJames Wright                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
862b730f8bSJeremy L Thompson                          qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp);
87e15f9bd0SJeremy L Thompson         // LCOV_EXCL_STOP
882b730f8bSJeremy L Thompson       }
89e15f9bd0SJeremy L Thompson       break;
90e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
91c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
92c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
93c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
94c4e3f59bSSebastian Grimberg       if (size != num_comp * q_comp) {
95e15f9bd0SJeremy L Thompson         // LCOV_EXCL_START
96e15f9bd0SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_DIMENSION,
97953dad27SJames Wright                          "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components",
98c4e3f59bSSebastian Grimberg                          qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
99e15f9bd0SJeremy L Thompson         // LCOV_EXCL_STOP
1002b730f8bSJeremy L Thompson       }
101e15f9bd0SJeremy L Thompson       break;
102e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
103d1d35e2fSjeremylt       // No additional checks required
104e15f9bd0SJeremy L Thompson       break;
105e15f9bd0SJeremy L Thompson   }
106e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1077a982d89SJeremy L. Thompson }
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson /**
1107a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
1117a982d89SJeremy L. Thompson 
1127a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
113d1d35e2fSjeremylt   @param[in] qf_field     QFunction field (carries field name)
114d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
1154c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
116d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
1177a982d89SJeremy L. Thompson   @param[in] stream       Stream to view to, e.g., stdout
1187a982d89SJeremy L. Thompson 
1197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
1207a982d89SJeremy L. Thompson 
1217a982d89SJeremy L. Thompson   @ref Utility
1227a982d89SJeremy L. Thompson **/
1232b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
1247a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
125d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
1267a982d89SJeremy L. Thompson 
1272b730f8bSJeremy L Thompson   fprintf(stream,
1282b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1292b730f8bSJeremy L Thompson           ":\n"
1307a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
131d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
1327a982d89SJeremy L. Thompson 
133990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
134f5ebfb90SJeremy L Thompson 
1352b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
136f5ebfb90SJeremy L Thompson 
1372b730f8bSJeremy L Thompson   if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s      Collocated basis\n", pre);
1387a982d89SJeremy L. Thompson 
1392b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1402b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
141f5ebfb90SJeremy L Thompson 
142e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1437a982d89SJeremy L. Thompson }
1447a982d89SJeremy L. Thompson 
1457a982d89SJeremy L. Thompson /**
1467a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1477a982d89SJeremy L. Thompson 
1487a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1497a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1507a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
1517a982d89SJeremy L. Thompson 
1527a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1537a982d89SJeremy L. Thompson 
1547a982d89SJeremy L. Thompson   @ref Utility
1557a982d89SJeremy L. Thompson **/
1567a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1577a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1587a982d89SJeremy L. Thompson 
159381e6593SJeremy L Thompson   CeedInt num_elem, num_qpts;
1602b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1612b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
162381e6593SJeremy L Thompson 
16378464608Sjeremylt   CeedInt total_fields = 0;
1642b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1652b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1667a982d89SJeremy L. Thompson 
1672b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1687a982d89SJeremy L. Thompson 
1692b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
170d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1712b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1727a982d89SJeremy L. Thompson   }
1737a982d89SJeremy L. Thompson 
1742b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
175d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1762b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1777a982d89SJeremy L. Thompson   }
178e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1797a982d89SJeremy L. Thompson }
1807a982d89SJeremy L. Thompson 
181d99fa3c5SJeremy L Thompson /**
1820f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
183eaf62fffSJeremy L Thompson 
184eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
1850f60e0a8SJeremy L Thompson   @param[out] active_basis Basis for active input vector or NULL for composite operator
186eaf62fffSJeremy L Thompson 
187eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
188eaf62fffSJeremy L Thompson 
189eaf62fffSJeremy L Thompson   @ref Developer
190eaf62fffSJeremy L Thompson **/
191eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
1926aaed0edSJeremy L Thompson   Ceed ceed;
1936aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
1946aaed0edSJeremy L Thompson 
195eaf62fffSJeremy L Thompson   *active_basis = NULL;
1960f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
1972b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
198eaf62fffSJeremy L Thompson     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1996aaed0edSJeremy L Thompson       if (*active_basis && *active_basis != op->input_fields[i]->basis) {
2006aaed0edSJeremy L Thompson         // LCOV_EXCL_START
2016aaed0edSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found");
2026aaed0edSJeremy L Thompson         // LCOV_EXCL_STOP
2036aaed0edSJeremy L Thompson       }
204eaf62fffSJeremy L Thompson       *active_basis = op->input_fields[i]->basis;
205eaf62fffSJeremy L Thompson     }
2062b730f8bSJeremy L Thompson   }
207eaf62fffSJeremy L Thompson 
208eaf62fffSJeremy L Thompson   if (!*active_basis) {
209eaf62fffSJeremy L Thompson     // LCOV_EXCL_START
2106aaed0edSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found");
211eaf62fffSJeremy L Thompson     // LCOV_EXCL_STOP
212eaf62fffSJeremy L Thompson   }
213eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
214eaf62fffSJeremy L Thompson }
215eaf62fffSJeremy L Thompson 
216eaf62fffSJeremy L Thompson /**
2170f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
218e2f04181SAndrew T. Barker 
219e2f04181SAndrew T. Barker   @param[in] op           CeedOperator to find active basis for
2200f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator
221e2f04181SAndrew T. Barker 
222e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
223e2f04181SAndrew T. Barker 
224e2f04181SAndrew T. Barker   @ref Utility
225e2f04181SAndrew T. Barker **/
2262b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
2276aaed0edSJeremy L Thompson   Ceed ceed;
2286aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
2296aaed0edSJeremy L Thompson 
230d1d35e2fSjeremylt   *active_rstr = NULL;
2310f60e0a8SJeremy L Thompson   if (op->is_composite) return CEED_ERROR_SUCCESS;
2322b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
233d1d35e2fSjeremylt     if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
234437c7c90SJeremy L Thompson       if (*active_rstr && *active_rstr != op->input_fields[i]->elem_rstr) {
2356aaed0edSJeremy L Thompson         // LCOV_EXCL_START
2366aaed0edSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_MINOR, "Multiple active CeedElemRestrictions found");
2376aaed0edSJeremy L Thompson         // LCOV_EXCL_STOP
2386aaed0edSJeremy L Thompson       }
239437c7c90SJeremy L Thompson       *active_rstr = op->input_fields[i]->elem_rstr;
240e2f04181SAndrew T. Barker     }
2412b730f8bSJeremy L Thompson   }
242e2f04181SAndrew T. Barker 
243d1d35e2fSjeremylt   if (!*active_rstr) {
244e2f04181SAndrew T. Barker     // LCOV_EXCL_START
2452b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found");
246e2f04181SAndrew T. Barker     // LCOV_EXCL_STOP
247e2f04181SAndrew T. Barker   }
248e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
249e2f04181SAndrew T. Barker }
250e2f04181SAndrew T. Barker 
251d8dd9a91SJeremy L Thompson /**
2522788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
253*4385fb7fSSebastian Grimberg 
254ea61e9acSJeremy L Thompson   For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
255ea61e9acSJeremy L Thompson   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
256ea61e9acSJeremy L Thompson not have any field of a matching type.
257d8dd9a91SJeremy L Thompson 
258ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
259ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
260ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2612788fa27SJeremy L Thompson   @param[in]     values      Values to set
262d8dd9a91SJeremy L Thompson 
263d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
264d8dd9a91SJeremy L Thompson 
265d8dd9a91SJeremy L Thompson   @ref User
266d8dd9a91SJeremy L Thompson **/
2672788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2682b730f8bSJeremy L Thompson   if (!field_label) {
2693668ca4bSJeremy L Thompson     // LCOV_EXCL_START
2702b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2713668ca4bSJeremy L Thompson     // LCOV_EXCL_STOP
2722b730f8bSJeremy L Thompson   }
2733668ca4bSJeremy L Thompson 
2745ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2755ac9af79SJeremy L Thompson   if (field_label->from_op) {
2765ac9af79SJeremy L Thompson     CeedInt index = -1;
2775ac9af79SJeremy L Thompson 
2785ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2795ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2805ac9af79SJeremy L Thompson     }
2815ac9af79SJeremy L Thompson     if (index == -1) {
2825ac9af79SJeremy L Thompson       // LCOV_EXCL_START
2835ac9af79SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2845ac9af79SJeremy L Thompson       // LCOV_EXCL_STOP
2855ac9af79SJeremy L Thompson     }
2865ac9af79SJeremy L Thompson   }
2875ac9af79SJeremy L Thompson 
2883668ca4bSJeremy L Thompson   bool is_composite = false;
2892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
290d8dd9a91SJeremy L Thompson   if (is_composite) {
291d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
292d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
293d8dd9a91SJeremy L Thompson 
294c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
295c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2962b730f8bSJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
2973668ca4bSJeremy L Thompson       // LCOV_EXCL_START
2982c2ea1dbSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
2993668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
3002b730f8bSJeremy L Thompson     }
301d8dd9a91SJeremy L Thompson 
302d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
303d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3043668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3052788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
306d8dd9a91SJeremy L Thompson       }
307d8dd9a91SJeremy L Thompson     }
308d8dd9a91SJeremy L Thompson   } else {
3092b730f8bSJeremy L Thompson     if (!op->qf->ctx) {
310d8dd9a91SJeremy L Thompson       // LCOV_EXCL_START
3112b730f8bSJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
312d8dd9a91SJeremy L Thompson       // LCOV_EXCL_STOP
3132b730f8bSJeremy L Thompson     }
314d8dd9a91SJeremy L Thompson 
3152788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
3162788fa27SJeremy L Thompson   }
3172788fa27SJeremy L Thompson 
3182788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3192788fa27SJeremy L Thompson }
3202788fa27SJeremy L Thompson 
3212788fa27SJeremy L Thompson /**
3222788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
323*4385fb7fSSebastian Grimberg 
3242788fa27SJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
3255ac9af79SJeremy L Thompson   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
3265ac9af79SJeremy L Thompson   do not have any field of a matching type.
3272788fa27SJeremy L Thompson 
3282788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3292788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3302788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
331c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
332c5d0f995SJed Brown   @param[out]    values      Values in the label
3332788fa27SJeremy L Thompson 
3342788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3352788fa27SJeremy L Thompson 
3362788fa27SJeremy L Thompson   @ref User
3372788fa27SJeremy L Thompson **/
3382788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3392788fa27SJeremy L Thompson                                              void *values) {
3402788fa27SJeremy L Thompson   if (!field_label) {
3412788fa27SJeremy L Thompson     // LCOV_EXCL_START
3422788fa27SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3432788fa27SJeremy L Thompson     // LCOV_EXCL_STOP
3442788fa27SJeremy L Thompson   }
3452788fa27SJeremy L Thompson 
3462788fa27SJeremy L Thompson   *(void **)values = NULL;
3472788fa27SJeremy L Thompson   *num_values      = 0;
3482788fa27SJeremy L Thompson 
3495ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3505ac9af79SJeremy L Thompson   if (field_label->from_op) {
3515ac9af79SJeremy L Thompson     CeedInt index = -1;
3525ac9af79SJeremy L Thompson 
3535ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3545ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3555ac9af79SJeremy L Thompson     }
3565ac9af79SJeremy L Thompson     if (index == -1) {
3575ac9af79SJeremy L Thompson       // LCOV_EXCL_START
3585ac9af79SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3595ac9af79SJeremy L Thompson       // LCOV_EXCL_STOP
3605ac9af79SJeremy L Thompson     }
3615ac9af79SJeremy L Thompson   }
3625ac9af79SJeremy L Thompson 
3632788fa27SJeremy L Thompson   bool is_composite = false;
3642788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3652788fa27SJeremy L Thompson   if (is_composite) {
3662788fa27SJeremy L Thompson     CeedInt       num_sub;
3672788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3682788fa27SJeremy L Thompson 
3692788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3702788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3712788fa27SJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
3722788fa27SJeremy L Thompson       // LCOV_EXCL_START
3735ac9af79SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
3742788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
3752788fa27SJeremy L Thompson     }
3762788fa27SJeremy L Thompson 
3772788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3782788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3792788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3802788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3812788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3822788fa27SJeremy L Thompson       }
3832788fa27SJeremy L Thompson     }
3842788fa27SJeremy L Thompson   } else {
3852788fa27SJeremy L Thompson     if (!op->qf->ctx) {
3862788fa27SJeremy L Thompson       // LCOV_EXCL_START
3872788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3882788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
3892788fa27SJeremy L Thompson     }
3902788fa27SJeremy L Thompson 
3912788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3922788fa27SJeremy L Thompson   }
3932788fa27SJeremy L Thompson 
3942788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3952788fa27SJeremy L Thompson }
3962788fa27SJeremy L Thompson 
3972788fa27SJeremy L Thompson /**
3982788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
399*4385fb7fSSebastian Grimberg 
4002788fa27SJeremy L Thompson   For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
4015ac9af79SJeremy L Thompson   A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators
4025ac9af79SJeremy L Thompson   that do not have any field of a matching type.
4032788fa27SJeremy L Thompson 
4042788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
4052788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
4062788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
407c5d0f995SJed Brown   @param[in]     values      Values array to restore
4082788fa27SJeremy L Thompson 
4092788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4102788fa27SJeremy L Thompson 
4112788fa27SJeremy L Thompson   @ref User
4122788fa27SJeremy L Thompson **/
4132788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
4142788fa27SJeremy L Thompson   if (!field_label) {
4152788fa27SJeremy L Thompson     // LCOV_EXCL_START
4162788fa27SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
4172788fa27SJeremy L Thompson     // LCOV_EXCL_STOP
4182788fa27SJeremy L Thompson   }
4192788fa27SJeremy L Thompson 
4205ac9af79SJeremy L Thompson   // Check if field_label and op correspond
4215ac9af79SJeremy L Thompson   if (field_label->from_op) {
4225ac9af79SJeremy L Thompson     CeedInt index = -1;
4235ac9af79SJeremy L Thompson 
4245ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4255ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4265ac9af79SJeremy L Thompson     }
4275ac9af79SJeremy L Thompson     if (index == -1) {
4285ac9af79SJeremy L Thompson       // LCOV_EXCL_START
4295ac9af79SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4305ac9af79SJeremy L Thompson       // LCOV_EXCL_STOP
4315ac9af79SJeremy L Thompson     }
4325ac9af79SJeremy L Thompson   }
4335ac9af79SJeremy L Thompson 
4342788fa27SJeremy L Thompson   bool is_composite = false;
4352788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4362788fa27SJeremy L Thompson   if (is_composite) {
4372788fa27SJeremy L Thompson     CeedInt       num_sub;
4382788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4392788fa27SJeremy L Thompson 
4402788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4412788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4422788fa27SJeremy L Thompson     if (num_sub != field_label->num_sub_labels) {
4432788fa27SJeremy L Thompson       // LCOV_EXCL_START
4445ac9af79SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created");
4452788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
4462788fa27SJeremy L Thompson     }
4472788fa27SJeremy L Thompson 
4482788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4492788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4502788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
4512788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
4522788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4532788fa27SJeremy L Thompson       }
4542788fa27SJeremy L Thompson     }
4552788fa27SJeremy L Thompson   } else {
4562788fa27SJeremy L Thompson     if (!op->qf->ctx) {
4572788fa27SJeremy L Thompson       // LCOV_EXCL_START
4582788fa27SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4592788fa27SJeremy L Thompson       // LCOV_EXCL_STOP
4602788fa27SJeremy L Thompson     }
4612788fa27SJeremy L Thompson 
4622788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
463d8dd9a91SJeremy L Thompson   }
464d8dd9a91SJeremy L Thompson 
465d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
466d8dd9a91SJeremy L Thompson }
467d8dd9a91SJeremy L Thompson 
4687a982d89SJeremy L. Thompson /// @}
4697a982d89SJeremy L. Thompson 
4707a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4717a982d89SJeremy L. Thompson /// CeedOperator Backend API
4727a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4737a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
4747a982d89SJeremy L. Thompson /// @{
4757a982d89SJeremy L. Thompson 
4767a982d89SJeremy L. Thompson /**
4777a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
4787a982d89SJeremy L. Thompson 
479ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
480d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
4817a982d89SJeremy L. Thompson 
4827a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4837a982d89SJeremy L. Thompson 
4847a982d89SJeremy L. Thompson   @ref Backend
4857a982d89SJeremy L. Thompson **/
486d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
4872b730f8bSJeremy L Thompson   if (op->is_composite) {
4887a982d89SJeremy L. Thompson     // LCOV_EXCL_START
4892b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
4907a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
4912b730f8bSJeremy L Thompson   }
492d1d35e2fSjeremylt   *num_args = op->num_fields;
493e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4947a982d89SJeremy L. Thompson }
4957a982d89SJeremy L. Thompson 
4967a982d89SJeremy L. Thompson /**
4977a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4987a982d89SJeremy L. Thompson 
499ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
500d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
5017a982d89SJeremy L. Thompson 
5027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5037a982d89SJeremy L. Thompson 
5047a982d89SJeremy L. Thompson   @ref Backend
5057a982d89SJeremy L. Thompson **/
506d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
507f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
508e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5097a982d89SJeremy L. Thompson }
5107a982d89SJeremy L. Thompson 
5117a982d89SJeremy L. Thompson /**
5127a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
5137a982d89SJeremy L. Thompson 
514ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
5157a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5187a982d89SJeremy L. Thompson 
5197a982d89SJeremy L. Thompson   @ref Backend
5207a982d89SJeremy L. Thompson **/
5217a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
5222b730f8bSJeremy L Thompson   if (op->is_composite) {
5237a982d89SJeremy L. Thompson     // LCOV_EXCL_START
5242b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
5257a982d89SJeremy L. Thompson     // LCOV_EXCL_STOP
5262b730f8bSJeremy L Thompson   }
5277a982d89SJeremy L. Thompson   *qf = op->qf;
528e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5297a982d89SJeremy L. Thompson }
5307a982d89SJeremy L. Thompson 
5317a982d89SJeremy L. Thompson /**
532c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
533c04a41a7SJeremy L Thompson 
534ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
535d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
536c04a41a7SJeremy L Thompson 
537c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
538c04a41a7SJeremy L Thompson 
539c04a41a7SJeremy L Thompson   @ref Backend
540c04a41a7SJeremy L Thompson **/
541d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
542f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
544c04a41a7SJeremy L Thompson }
545c04a41a7SJeremy L Thompson 
546c04a41a7SJeremy L Thompson /**
5477a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
5487a982d89SJeremy L. Thompson 
549ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
5507a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5517a982d89SJeremy L. Thompson 
5527a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5537a982d89SJeremy L. Thompson 
5547a982d89SJeremy L. Thompson   @ref Backend
5557a982d89SJeremy L. Thompson **/
556777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
557777ff853SJeremy L Thompson   *(void **)data = op->data;
558e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5597a982d89SJeremy L. Thompson }
5607a982d89SJeremy L. Thompson 
5617a982d89SJeremy L. Thompson /**
5627a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
5637a982d89SJeremy L. Thompson 
564ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
565ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5667a982d89SJeremy L. Thompson 
5677a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5687a982d89SJeremy L. Thompson 
5697a982d89SJeremy L. Thompson   @ref Backend
5707a982d89SJeremy L. Thompson **/
571777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
572777ff853SJeremy L Thompson   op->data = data;
573e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5747a982d89SJeremy L. Thompson }
5757a982d89SJeremy L. Thompson 
5767a982d89SJeremy L. Thompson /**
57734359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
57834359f16Sjeremylt 
579ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
58034359f16Sjeremylt 
58134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
58234359f16Sjeremylt 
58334359f16Sjeremylt   @ref Backend
58434359f16Sjeremylt **/
5859560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
58634359f16Sjeremylt   op->ref_count++;
58734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
58834359f16Sjeremylt }
58934359f16Sjeremylt 
59034359f16Sjeremylt /**
5917a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
5927a982d89SJeremy L. Thompson 
593ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
5947a982d89SJeremy L. Thompson 
5957a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5967a982d89SJeremy L. Thompson 
5977a982d89SJeremy L. Thompson   @ref Backend
5987a982d89SJeremy L. Thompson **/
5997a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
600f04ea552SJeremy L Thompson   op->is_backend_setup = true;
601e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6027a982d89SJeremy L. Thompson }
6037a982d89SJeremy L. Thompson 
6047a982d89SJeremy L. Thompson /// @}
6057a982d89SJeremy L. Thompson 
6067a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6077a982d89SJeremy L. Thompson /// CeedOperator Public API
6087a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6097a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
610dfdf5a53Sjeremylt /// @{
611d7b241e6Sjeremylt 
612d7b241e6Sjeremylt /**
613ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
614*4385fb7fSSebastian Grimberg 
615ea61e9acSJeremy L Thompson   A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
616d7b241e6Sjeremylt 
617ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
618ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
619ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
620ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
621ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
622b11c1e72Sjeremylt 
623b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
624dfdf5a53Sjeremylt 
6257a982d89SJeremy L. Thompson   @ref User
626d7b241e6Sjeremylt  */
6272b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
6285fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
6295fe0d4faSjeremylt     Ceed delegate;
6302b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
6315fe0d4faSjeremylt 
6322b730f8bSJeremy L Thompson     if (!delegate) {
633c042f62fSJeremy L Thompson       // LCOV_EXCL_START
6342b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
635c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
6362b730f8bSJeremy L Thompson     }
6375fe0d4faSjeremylt 
6382b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
639e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6405fe0d4faSjeremylt   }
6415fe0d4faSjeremylt 
6422b730f8bSJeremy L Thompson   if (!qf || qf == CEED_QFUNCTION_NONE) {
643b3b7035fSJeremy L Thompson     // LCOV_EXCL_START
6442b730f8bSJeremy L Thompson     return CeedError(ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
645b3b7035fSJeremy L Thompson     // LCOV_EXCL_STOP
6462b730f8bSJeremy L Thompson   }
6472b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
648d7b241e6Sjeremylt   (*op)->ceed = ceed;
6492b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
650d1d35e2fSjeremylt   (*op)->ref_count   = 1;
651d7b241e6Sjeremylt   (*op)->qf          = qf;
6522b104005SJeremy L Thompson   (*op)->input_size  = -1;
6532b104005SJeremy L Thompson   (*op)->output_size = -1;
6542b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionReference(qf));
655442e7f0bSjeremylt   if (dqf && dqf != CEED_QFUNCTION_NONE) {
656d7b241e6Sjeremylt     (*op)->dqf = dqf;
6572b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqf));
658442e7f0bSjeremylt   }
659442e7f0bSjeremylt   if (dqfT && dqfT != CEED_QFUNCTION_NONE) {
660d7b241e6Sjeremylt     (*op)->dqfT = dqfT;
6612b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionReference(dqfT));
662442e7f0bSjeremylt   }
6632b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
6642b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
6652b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
6662b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
667e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
668d7b241e6Sjeremylt }
669d7b241e6Sjeremylt 
670d7b241e6Sjeremylt /**
67152d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
67252d6035fSJeremy L Thompson 
673ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
674ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
67552d6035fSJeremy L Thompson 
67652d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
67752d6035fSJeremy L Thompson 
6787a982d89SJeremy L. Thompson   @ref User
67952d6035fSJeremy L Thompson  */
68052d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
68152d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
68252d6035fSJeremy L Thompson     Ceed delegate;
6832b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
68452d6035fSJeremy L Thompson 
685250756a7Sjeremylt     if (delegate) {
6862b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
687e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
68852d6035fSJeremy L Thompson     }
689250756a7Sjeremylt   }
69052d6035fSJeremy L Thompson 
6912b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
69252d6035fSJeremy L Thompson   (*op)->ceed = ceed;
6932b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
694996d9ab5SJed Brown   (*op)->ref_count    = 1;
695f04ea552SJeremy L Thompson   (*op)->is_composite = true;
6962b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
6972b104005SJeremy L Thompson   (*op)->input_size  = -1;
6982b104005SJeremy L Thompson   (*op)->output_size = -1;
699250756a7Sjeremylt 
700250756a7Sjeremylt   if (ceed->CompositeOperatorCreate) {
7012b730f8bSJeremy L Thompson     CeedCall(ceed->CompositeOperatorCreate(*op));
702250756a7Sjeremylt   }
703e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
70452d6035fSJeremy L Thompson }
70552d6035fSJeremy L Thompson 
70652d6035fSJeremy L Thompson /**
707ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
708*4385fb7fSSebastian Grimberg 
709ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedOperatorDestroy()`.
710512bb800SJeremy L Thompson 
711512bb800SJeremy L Thompson   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.
712512bb800SJeremy L Thompson         This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
7139560d06aSjeremylt 
714ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
715ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
7169560d06aSjeremylt 
7179560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
7189560d06aSjeremylt 
7199560d06aSjeremylt   @ref User
7209560d06aSjeremylt **/
7219560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
7222b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
7232b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
7249560d06aSjeremylt   *op_copy = op;
7259560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7269560d06aSjeremylt }
7279560d06aSjeremylt 
7289560d06aSjeremylt /**
729ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
730d7b241e6Sjeremylt 
731ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
732ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
733ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
734d7b241e6Sjeremylt 
735ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
736ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
737d7b241e6Sjeremylt 
738ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
739ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
740ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
741ea61e9acSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points
7425ac9af79SJeremy L Thompson   @param[in]     v          CeedVector to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE
7435ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
744b11c1e72Sjeremylt 
745b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
746dfdf5a53Sjeremylt 
7477a982d89SJeremy L. Thompson   @ref User
748b11c1e72Sjeremylt **/
7492b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
7502b730f8bSJeremy L Thompson   if (op->is_composite) {
751c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7522b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
753c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7542b730f8bSJeremy L Thompson   }
7552b730f8bSJeremy L Thompson   if (op->is_immutable) {
756f04ea552SJeremy L Thompson     // LCOV_EXCL_START
7572b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
758f04ea552SJeremy L Thompson     // LCOV_EXCL_STOP
7592b730f8bSJeremy L Thompson   }
7602b730f8bSJeremy L Thompson   if (!r) {
761c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7622b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
763c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7642b730f8bSJeremy L Thompson   }
7652b730f8bSJeremy L Thompson   if (!b) {
766c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7672b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
768c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7692b730f8bSJeremy L Thompson   }
7702b730f8bSJeremy L Thompson   if (!v) {
771c042f62fSJeremy L Thompson     // LCOV_EXCL_START
7722b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
773c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7742b730f8bSJeremy L Thompson   }
77552d6035fSJeremy L Thompson 
776d1d35e2fSjeremylt   CeedInt num_elem;
7772b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
7782b730f8bSJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE && op->has_restriction && op->num_elem != num_elem) {
779c042f62fSJeremy L Thompson     // LCOV_EXCL_START
780e15f9bd0SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_DIMENSION,
7812b730f8bSJeremy L Thompson                      "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
782c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
7832b730f8bSJeremy L Thompson   }
784d7b241e6Sjeremylt 
78578464608Sjeremylt   CeedInt num_qpts = 0;
786e15f9bd0SJeremy L Thompson   if (b != CEED_BASIS_COLLOCATED) {
7872b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
7882b730f8bSJeremy L Thompson     if (op->num_qpts && op->num_qpts != num_qpts) {
789c042f62fSJeremy L Thompson       // LCOV_EXCL_START
790e15f9bd0SJeremy L Thompson       return CeedError(op->ceed, CEED_ERROR_DIMENSION,
7912b730f8bSJeremy L Thompson                        "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts);
792c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
793d7b241e6Sjeremylt     }
7942b730f8bSJeremy L Thompson   }
795d1d35e2fSjeremylt   CeedQFunctionField qf_field;
796d1d35e2fSjeremylt   CeedOperatorField *op_field;
7972b104005SJeremy L Thompson   bool               is_input = true;
798d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
799d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
800d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
801d1d35e2fSjeremylt       op_field = &op->input_fields[i];
802d7b241e6Sjeremylt       goto found;
803d7b241e6Sjeremylt     }
804d7b241e6Sjeremylt   }
8052b104005SJeremy L Thompson   is_input = false;
806d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
807d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
808d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
809d1d35e2fSjeremylt       op_field = &op->output_fields[i];
810d7b241e6Sjeremylt       goto found;
811d7b241e6Sjeremylt     }
812d7b241e6Sjeremylt   }
813c042f62fSJeremy L Thompson   // LCOV_EXCL_START
8142b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
815c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
816d7b241e6Sjeremylt found:
8172b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
8182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
819e15f9bd0SJeremy L Thompson 
8202b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
8212b104005SJeremy L Thompson     CeedSize l_size;
8222b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
8232b104005SJeremy L Thompson     if (is_input) {
8242b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
8252b730f8bSJeremy L Thompson       if (l_size != op->input_size) {
8262b104005SJeremy L Thompson         // LCOV_EXCL_START
8272b730f8bSJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->input_size);
8282b104005SJeremy L Thompson         // LCOV_EXCL_STOP
8292b730f8bSJeremy L Thompson       }
8302b104005SJeremy L Thompson     } else {
8312b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
8322b730f8bSJeremy L Thompson       if (l_size != op->output_size) {
8332b104005SJeremy L Thompson         // LCOV_EXCL_START
8342b730f8bSJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->output_size);
8352b104005SJeremy L Thompson         // LCOV_EXCL_STOP
8362b104005SJeremy L Thompson       }
8372b104005SJeremy L Thompson     }
8382b730f8bSJeremy L Thompson   }
8392b104005SJeremy L Thompson 
840393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
841393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
842e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
843d1d35e2fSjeremylt     op->num_elem        = num_elem;
844d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
845e15f9bd0SJeremy L Thompson   }
846393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
847393ac2cdSJeremy L Thompson   if (!op->num_qpts && b != CEED_BASIS_COLLOCATED) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
848e15f9bd0SJeremy L Thompson 
849d1d35e2fSjeremylt   op->num_fields += 1;
8502b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
851e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
852d7b241e6Sjeremylt }
853d7b241e6Sjeremylt 
854d7b241e6Sjeremylt /**
85543bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
85643bbe138SJeremy L Thompson 
857ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
858f04ea552SJeremy L Thompson 
859ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
860f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
86143bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
862f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
86343bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
86443bbe138SJeremy L Thompson 
86543bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
86643bbe138SJeremy L Thompson 
867e9b533fbSJeremy L Thompson   @ref Advanced
86843bbe138SJeremy L Thompson **/
8692b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
87043bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
8712b730f8bSJeremy L Thompson   if (op->is_composite) {
87243bbe138SJeremy L Thompson     // LCOV_EXCL_START
8732b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
87443bbe138SJeremy L Thompson     // LCOV_EXCL_STOP
8752b730f8bSJeremy L Thompson   }
8762b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
87743bbe138SJeremy L Thompson 
87843bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
87943bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
88043bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
88143bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
88243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
88343bbe138SJeremy L Thompson }
88443bbe138SJeremy L Thompson 
88543bbe138SJeremy L Thompson /**
886de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
887de5900adSJames Wright 
888de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
889de5900adSJames Wright 
890de5900adSJames Wright   @param[in]  op         CeedOperator
891de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
892de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
893de5900adSJames Wright 
894de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
895de5900adSJames Wright 
896de5900adSJames Wright   @ref Advanced
897de5900adSJames Wright **/
898de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
899de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
900de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
901de5900adSJames Wright   char              *name;
902de5900adSJames Wright   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
903de5900adSJames Wright 
904de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
905de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
906de5900adSJames Wright     if (!strcmp(name, field_name)) {
907de5900adSJames Wright       *op_field = input_fields[i];
908de5900adSJames Wright       return CEED_ERROR_SUCCESS;
909de5900adSJames Wright     }
910de5900adSJames Wright   }
911de5900adSJames Wright 
912de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
913de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
914de5900adSJames Wright     if (!strcmp(name, field_name)) {
915de5900adSJames Wright       *op_field = output_fields[i];
916de5900adSJames Wright       return CEED_ERROR_SUCCESS;
917de5900adSJames Wright     }
918de5900adSJames Wright   }
919de5900adSJames Wright 
920de5900adSJames Wright   bool has_name = op->name;
921de5900adSJames Wright   // LCOV_EXCL_START
922de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
923de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
924de5900adSJames Wright   // LCOV_EXCL_STOP
925de5900adSJames Wright }
926de5900adSJames Wright 
927de5900adSJames Wright /**
92828567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
92928567f8fSJeremy L Thompson 
930ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
93128567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
93228567f8fSJeremy L Thompson 
93328567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
93428567f8fSJeremy L Thompson 
935e9b533fbSJeremy L Thompson   @ref Advanced
93628567f8fSJeremy L Thompson **/
93728567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
93828567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
93928567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
94028567f8fSJeremy L Thompson }
94128567f8fSJeremy L Thompson 
94228567f8fSJeremy L Thompson /**
94343bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
94443bbe138SJeremy L Thompson 
945ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
94643bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
94743bbe138SJeremy L Thompson 
94843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94943bbe138SJeremy L Thompson 
950e9b533fbSJeremy L Thompson   @ref Advanced
95143bbe138SJeremy L Thompson **/
9522b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
953437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
95443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
95543bbe138SJeremy L Thompson }
95643bbe138SJeremy L Thompson 
95743bbe138SJeremy L Thompson /**
95843bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
95943bbe138SJeremy L Thompson 
960ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
96143bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
96243bbe138SJeremy L Thompson 
96343bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
96443bbe138SJeremy L Thompson 
965e9b533fbSJeremy L Thompson   @ref Advanced
96643bbe138SJeremy L Thompson **/
96743bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
96843bbe138SJeremy L Thompson   *basis = op_field->basis;
96943bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
97043bbe138SJeremy L Thompson }
97143bbe138SJeremy L Thompson 
97243bbe138SJeremy L Thompson /**
97343bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
97443bbe138SJeremy L Thompson 
975ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
97643bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
97743bbe138SJeremy L Thompson 
97843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
97943bbe138SJeremy L Thompson 
980e9b533fbSJeremy L Thompson   @ref Advanced
98143bbe138SJeremy L Thompson **/
98243bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
98343bbe138SJeremy L Thompson   *vec = op_field->vec;
98443bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98543bbe138SJeremy L Thompson }
98643bbe138SJeremy L Thompson 
98743bbe138SJeremy L Thompson /**
98852d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
989288c0443SJeremy L Thompson 
990ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
991ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
99252d6035fSJeremy L Thompson 
99352d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
99452d6035fSJeremy L Thompson 
9957a982d89SJeremy L. Thompson   @ref User
99652d6035fSJeremy L Thompson  */
9972b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
9982b730f8bSJeremy L Thompson   if (!composite_op->is_composite) {
999c042f62fSJeremy L Thompson     // LCOV_EXCL_START
10002b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
10012b104005SJeremy L Thompson     // LCOV_EXCL_STOP
10022b104005SJeremy L Thompson   }
10032b104005SJeremy L Thompson 
10042b730f8bSJeremy L Thompson   if (composite_op->num_suboperators == CEED_COMPOSITE_MAX) {
10052b730f8bSJeremy L Thompson     // LCOV_EXCL_START
10062b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
10072b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
10082b730f8bSJeremy L Thompson   }
10092b730f8bSJeremy L Thompson   if (composite_op->is_immutable) {
10102b730f8bSJeremy L Thompson     // LCOV_EXCL_START
10112b730f8bSJeremy L Thompson     return CeedError(composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
10122b730f8bSJeremy L Thompson     // LCOV_EXCL_STOP
10132b730f8bSJeremy L Thompson   }
10142b730f8bSJeremy L Thompson 
10152b730f8bSJeremy L Thompson   {
10162b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
10172b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
10182b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
10192b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
10202b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
10212b730f8bSJeremy L Thompson     if ((input_size != -1 && input_size != composite_op->input_size) || (output_size != -1 && output_size != composite_op->output_size)) {
10222b730f8bSJeremy L Thompson       // LCOV_EXCL_START
10232b730f8bSJeremy L Thompson       return CeedError(composite_op->ceed, CEED_ERROR_MAJOR,
10242b730f8bSJeremy L Thompson                        "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
10252b730f8bSJeremy L Thompson                        "shape (%td, %td)",
10262b730f8bSJeremy L Thompson                        composite_op->input_size, composite_op->output_size, input_size, output_size);
10272b730f8bSJeremy L Thompson       // LCOV_EXCL_STOP
10282b730f8bSJeremy L Thompson     }
10292b730f8bSJeremy L Thompson   }
10302b730f8bSJeremy L Thompson 
1031d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
10322b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1033d1d35e2fSjeremylt   composite_op->num_suboperators++;
1034e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103552d6035fSJeremy L Thompson }
103652d6035fSJeremy L Thompson 
103752d6035fSJeremy L Thompson /**
103875f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
103975f0d5a4SJeremy L Thompson 
104075f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
104175f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
104275f0d5a4SJeremy L Thompson 
104375f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
104475f0d5a4SJeremy L Thompson 
104575f0d5a4SJeremy L Thompson   @ref Backend
104675f0d5a4SJeremy L Thompson **/
1047c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
104875f0d5a4SJeremy L Thompson   if (!op->is_composite) {
104975f0d5a4SJeremy L Thompson     // LCOV_EXCL_START
105075f0d5a4SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator");
105175f0d5a4SJeremy L Thompson     // LCOV_EXCL_STOP
105275f0d5a4SJeremy L Thompson   }
105375f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
105475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
105575f0d5a4SJeremy L Thompson }
105675f0d5a4SJeremy L Thompson 
105775f0d5a4SJeremy L Thompson /**
105875f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
105975f0d5a4SJeremy L Thompson 
106075f0d5a4SJeremy L Thompson   @param op                  CeedOperator
106175f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
106275f0d5a4SJeremy L Thompson 
106375f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
106475f0d5a4SJeremy L Thompson 
106575f0d5a4SJeremy L Thompson   @ref Backend
106675f0d5a4SJeremy L Thompson **/
1067c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
106875f0d5a4SJeremy L Thompson   if (!op->is_composite) {
106975f0d5a4SJeremy L Thompson     // LCOV_EXCL_START
107075f0d5a4SJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator");
107175f0d5a4SJeremy L Thompson     // LCOV_EXCL_STOP
107275f0d5a4SJeremy L Thompson   }
107375f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
107475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
107575f0d5a4SJeremy L Thompson }
107675f0d5a4SJeremy L Thompson 
107775f0d5a4SJeremy L Thompson /**
10784db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
10794db537f9SJeremy L Thompson 
10804db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
10814db537f9SJeremy L Thompson 
10824db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10834db537f9SJeremy L Thompson 
10844db537f9SJeremy L Thompson   @ref User
10854db537f9SJeremy L Thompson **/
10864db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
10874db537f9SJeremy L Thompson   Ceed ceed;
10882b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
10894db537f9SJeremy L Thompson 
10902b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
10914db537f9SJeremy L Thompson 
10924db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
10934db537f9SJeremy L Thompson   if (op->is_composite) {
109443622462SJeremy L Thompson     if (!op->num_suboperators) {
109543622462SJeremy L Thompson       // Empty operator setup
109643622462SJeremy L Thompson       op->input_size  = 0;
109743622462SJeremy L Thompson       op->output_size = 0;
109843622462SJeremy L Thompson     } else {
10994db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
11002b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
11014db537f9SJeremy L Thompson       }
11022b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
11032b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
11042b104005SJeremy L Thompson       CeedSize input_size, output_size;
11052b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
110643622462SJeremy L Thompson     }
11074db537f9SJeremy L Thompson   } else {
11082b730f8bSJeremy L Thompson     if (op->num_fields == 0) {
11094db537f9SJeremy L Thompson       // LCOV_EXCL_START
11104db537f9SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
11114db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
11122b730f8bSJeremy L Thompson     }
11132b730f8bSJeremy L Thompson     if (op->num_fields < qf->num_input_fields + qf->num_output_fields) {
11144db537f9SJeremy L Thompson       // LCOV_EXCL_START
11154db537f9SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
11164db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
11172b730f8bSJeremy L Thompson     }
11182b730f8bSJeremy L Thompson     if (!op->has_restriction) {
11194db537f9SJeremy L Thompson       // LCOV_EXCL_START
11202b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
11214db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
11222b730f8bSJeremy L Thompson     }
11232b730f8bSJeremy L Thompson     if (op->num_qpts == 0) {
11244db537f9SJeremy L Thompson       // LCOV_EXCL_START
11252b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one non-collocated basis is required or the number of quadrature points must be set");
11264db537f9SJeremy L Thompson       // LCOV_EXCL_STOP
11274db537f9SJeremy L Thompson     }
11282b730f8bSJeremy L Thompson   }
11294db537f9SJeremy L Thompson 
11304db537f9SJeremy L Thompson   // Flag as immutable and ready
11314db537f9SJeremy L Thompson   op->is_interface_setup = true;
11322b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
11332b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
11342b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
11354db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11364db537f9SJeremy L Thompson }
11374db537f9SJeremy L Thompson 
11384db537f9SJeremy L Thompson /**
1139c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
1140*4385fb7fSSebastian Grimberg 
1141ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
1142c9366a6bSJeremy L Thompson 
1143c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
1144c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
1145c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
1146c9366a6bSJeremy L Thompson 
1147c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1148c9366a6bSJeremy L Thompson 
1149c9366a6bSJeremy L Thompson   @ref User
1150c9366a6bSJeremy L Thompson **/
11512b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1152c9366a6bSJeremy L Thompson   bool is_composite;
1153c9366a6bSJeremy L Thompson 
11542b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
11552b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1156c9366a6bSJeremy L Thompson 
11572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11582b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1159c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1160c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
11612b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
11622b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
11632b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
11642b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
11652b730f8bSJeremy L Thompson       if ((sub_input_size != -1 && sub_input_size != op->input_size) || (sub_output_size != -1 && sub_output_size != op->output_size)) {
11662b104005SJeremy L Thompson         // LCOV_EXCL_START
11672b104005SJeremy L Thompson         return CeedError(op->ceed, CEED_ERROR_MAJOR,
11682b730f8bSJeremy L Thompson                          "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
11692b730f8bSJeremy L Thompson                          "shape (%td, %td)",
11702b104005SJeremy L Thompson                          op->input_size, op->output_size, input_size, output_size);
11712b104005SJeremy L Thompson         // LCOV_EXCL_STOP
1172c9366a6bSJeremy L Thompson       }
1173c9366a6bSJeremy L Thompson     }
11742b730f8bSJeremy L Thompson   }
1175c9366a6bSJeremy L Thompson 
1176c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1177c9366a6bSJeremy L Thompson }
1178c9366a6bSJeremy L Thompson 
1179c9366a6bSJeremy L Thompson /**
1180beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
1181*4385fb7fSSebastian Grimberg 
1182ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
1183ea61e9acSJeremy L Thompson `CeedOperatorLinearAssemble*` function is called. When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused
1184ea61e9acSJeremy L Thompson between calls to `CeedOperatorSetQFunctionAssemblyDataUpdated`.
11858b919e6bSJeremy L Thompson 
1186beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
1187beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
11888b919e6bSJeremy L Thompson 
11898b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11908b919e6bSJeremy L Thompson 
11918b919e6bSJeremy L Thompson   @ref Advanced
11928b919e6bSJeremy L Thompson **/
11932b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
11948b919e6bSJeremy L Thompson   bool is_composite;
11958b919e6bSJeremy L Thompson 
11962b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11978b919e6bSJeremy L Thompson   if (is_composite) {
11988b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
11992b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
12008b919e6bSJeremy L Thompson     }
12018b919e6bSJeremy L Thompson   } else {
12022b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1203beecbf24SJeremy L Thompson   }
1204beecbf24SJeremy L Thompson 
1205beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1206beecbf24SJeremy L Thompson }
1207beecbf24SJeremy L Thompson 
1208beecbf24SJeremy L Thompson /**
1209beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1210beecbf24SJeremy L Thompson 
1211beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
12126e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1213beecbf24SJeremy L Thompson 
1214beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1215beecbf24SJeremy L Thompson 
1216beecbf24SJeremy L Thompson   @ref Advanced
1217beecbf24SJeremy L Thompson **/
12182b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1219beecbf24SJeremy L Thompson   bool is_composite;
1220beecbf24SJeremy L Thompson 
12212b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1222beecbf24SJeremy L Thompson   if (is_composite) {
1223beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
12242b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1225beecbf24SJeremy L Thompson     }
1226beecbf24SJeremy L Thompson   } else {
12272b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
12288b919e6bSJeremy L Thompson   }
12298b919e6bSJeremy L Thompson 
12308b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12318b919e6bSJeremy L Thompson }
12328b919e6bSJeremy L Thompson 
12338b919e6bSJeremy L Thompson /**
1234cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
1235*4385fb7fSSebastian Grimberg 
1236ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1237ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1238cd4dfc48Sjeremylt 
1239ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1240ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1241cd4dfc48Sjeremylt 
1242cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1243cd4dfc48Sjeremylt 
1244e9b533fbSJeremy L Thompson   @ref Advanced
1245cd4dfc48Sjeremylt **/
1246cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
12472b730f8bSJeremy L Thompson   if (op->is_composite) {
1248cd4dfc48Sjeremylt     // LCOV_EXCL_START
12492b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1250cd4dfc48Sjeremylt     // LCOV_EXCL_STOP
12512b730f8bSJeremy L Thompson   }
12522b730f8bSJeremy L Thompson   if (op->num_qpts) {
1253cd4dfc48Sjeremylt     // LCOV_EXCL_START
12542b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined");
1255cd4dfc48Sjeremylt     // LCOV_EXCL_STOP
12562b730f8bSJeremy L Thompson   }
12572b730f8bSJeremy L Thompson   if (op->is_immutable) {
1258f04ea552SJeremy L Thompson     // LCOV_EXCL_START
12592b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1260f04ea552SJeremy L Thompson     // LCOV_EXCL_STOP
12612b730f8bSJeremy L Thompson   }
1262cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1263cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1264cd4dfc48Sjeremylt }
1265cd4dfc48Sjeremylt 
1266cd4dfc48Sjeremylt /**
1267ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1268ea6b5821SJeremy L Thompson 
1269ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1270ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1271ea6b5821SJeremy L Thompson 
1272ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1273ea6b5821SJeremy L Thompson 
1274ea6b5821SJeremy L Thompson   @ref User
1275ea6b5821SJeremy L Thompson **/
1276ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1277ea6b5821SJeremy L Thompson   char  *name_copy;
1278ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1279ea6b5821SJeremy L Thompson 
12802b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1281ea6b5821SJeremy L Thompson   if (name_len > 0) {
12822b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1283ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1284ea6b5821SJeremy L Thompson     op->name = name_copy;
1285ea6b5821SJeremy L Thompson   }
1286ea6b5821SJeremy L Thompson 
1287ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1288ea6b5821SJeremy L Thompson }
1289ea6b5821SJeremy L Thompson 
1290ea6b5821SJeremy L Thompson /**
12917a982d89SJeremy L. Thompson   @brief View a CeedOperator
12927a982d89SJeremy L. Thompson 
12937a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
12947a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
12957a982d89SJeremy L. Thompson 
12967a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
12977a982d89SJeremy L. Thompson 
12987a982d89SJeremy L. Thompson   @ref User
12997a982d89SJeremy L. Thompson **/
13007a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1301ea6b5821SJeremy L Thompson   bool has_name = op->name;
13027a982d89SJeremy L. Thompson 
1303f04ea552SJeremy L Thompson   if (op->is_composite) {
13042b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
13057a982d89SJeremy L. Thompson 
1306d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1307ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
13082b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
13092b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
13107a982d89SJeremy L. Thompson     }
13117a982d89SJeremy L. Thompson   } else {
13122b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
13132b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
13147a982d89SJeremy L. Thompson   }
1315e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13167a982d89SJeremy L. Thompson }
13173bd813ffSjeremylt 
13183bd813ffSjeremylt /**
1319b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1320b7c9bbdaSJeremy L Thompson 
1321ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1322b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1323b7c9bbdaSJeremy L Thompson 
1324b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1325b7c9bbdaSJeremy L Thompson 
1326b7c9bbdaSJeremy L Thompson   @ref Advanced
1327b7c9bbdaSJeremy L Thompson **/
1328b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1329b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1330b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1331b7c9bbdaSJeremy L Thompson }
1332b7c9bbdaSJeremy L Thompson 
1333b7c9bbdaSJeremy L Thompson /**
1334b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1335b7c9bbdaSJeremy L Thompson 
1336ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1337b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1338b7c9bbdaSJeremy L Thompson 
1339b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1340b7c9bbdaSJeremy L Thompson 
1341b7c9bbdaSJeremy L Thompson   @ref Advanced
1342b7c9bbdaSJeremy L Thompson **/
1343b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
13442b730f8bSJeremy L Thompson   if (op->is_composite) {
1345b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_START
13462b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1347b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_STOP
13482b730f8bSJeremy L Thompson   }
1349b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1350b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1351b7c9bbdaSJeremy L Thompson }
1352b7c9bbdaSJeremy L Thompson 
1353b7c9bbdaSJeremy L Thompson /**
1354b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1355b7c9bbdaSJeremy L Thompson 
1356ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1357b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1358b7c9bbdaSJeremy L Thompson 
1359b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1360b7c9bbdaSJeremy L Thompson 
1361b7c9bbdaSJeremy L Thompson   @ref Advanced
1362b7c9bbdaSJeremy L Thompson **/
1363b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
13642b730f8bSJeremy L Thompson   if (op->is_composite) {
1365b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_START
13662b730f8bSJeremy L Thompson     return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1367b7c9bbdaSJeremy L Thompson     // LCOV_EXCL_STOP
13682b730f8bSJeremy L Thompson   }
1369b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1370b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1371b7c9bbdaSJeremy L Thompson }
1372b7c9bbdaSJeremy L Thompson 
1373b7c9bbdaSJeremy L Thompson /**
13746e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
13756e15d496SJeremy L Thompson 
1376ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1377ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
13786e15d496SJeremy L Thompson 
13796e15d496SJeremy L Thompson   @ref Backend
13806e15d496SJeremy L Thompson **/
13819d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
13826e15d496SJeremy L Thompson   bool is_composite;
13832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
13846e15d496SJeremy L Thompson 
13856e15d496SJeremy L Thompson   *flops = 0;
13862b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13876e15d496SJeremy L Thompson   if (is_composite) {
13886e15d496SJeremy L Thompson     CeedInt num_suboperators;
1389c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13906e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1391c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13926e15d496SJeremy L Thompson 
13936e15d496SJeremy L Thompson     // FLOPs for each suboperator
13946e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
13959d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
13962b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
13976e15d496SJeremy L Thompson       *flops += suboperator_flops;
13986e15d496SJeremy L Thompson     }
13996e15d496SJeremy L Thompson   } else {
14006e15d496SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields;
14016e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
14022b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
14036e15d496SJeremy L Thompson     CeedInt num_elem = 0;
14042b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1405*4385fb7fSSebastian Grimberg 
14066e15d496SJeremy L Thompson     // Input FLOPs
14076e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
14086e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
14099d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
14106e15d496SJeremy L Thompson 
1411437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops));
14126e15d496SJeremy L Thompson         *flops += restr_flops;
14132b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
14146e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
14156e15d496SJeremy L Thompson       }
14166e15d496SJeremy L Thompson     }
14176e15d496SJeremy L Thompson     // QF FLOPs
14189d36ca50SJeremy L Thompson     CeedInt  num_qpts;
14199d36ca50SJeremy L Thompson     CeedSize qf_flops;
14202b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
14212b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
14226e15d496SJeremy L Thompson     *flops += num_elem * num_qpts * qf_flops;
14236e15d496SJeremy L Thompson     // Output FLOPs
14246e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
14256e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
14269d36ca50SJeremy L Thompson         CeedSize restr_flops, basis_flops;
14276e15d496SJeremy L Thompson 
1428437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops));
14296e15d496SJeremy L Thompson         *flops += restr_flops;
14302b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
14316e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
14326e15d496SJeremy L Thompson       }
14336e15d496SJeremy L Thompson     }
14346e15d496SJeremy L Thompson   }
14356e15d496SJeremy L Thompson 
14366e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14376e15d496SJeremy L Thompson }
14386e15d496SJeremy L Thompson 
14396e15d496SJeremy L Thompson /**
14400126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
1441512bb800SJeremy L Thompson            The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1442512bb800SJeremy L Thompson 
1443512bb800SJeremy L Thompson            Note: If the value of `ctx` passed into this function is non-NULL, then it is assumed that `ctx` is a pointer to a
14440126412dSJeremy L Thompson              CeedQFunctionContext. This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
14450126412dSJeremy L Thompson 
1446859c15bbSJames Wright   @param[in]  op  CeedOperator
14470126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
14480126412dSJeremy L Thompson 
14490126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14500126412dSJeremy L Thompson 
1451859c15bbSJames Wright   @ref Advanced
14520126412dSJeremy L Thompson **/
14530126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
14540126412dSJeremy L Thompson   bool is_composite;
14550126412dSJeremy L Thompson 
14560126412dSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14570126412dSJeremy L Thompson   if (is_composite) return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
14580126412dSJeremy L Thompson 
14590126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
14600126412dSJeremy L Thompson   else *ctx = NULL;
14610126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14620126412dSJeremy L Thompson }
14630126412dSJeremy L Thompson 
14640126412dSJeremy L Thompson /**
1465ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
14663668ca4bSJeremy L Thompson 
1467859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1468859c15bbSJames Wright 
14693668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
14703668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
14713668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
14723668ca4bSJeremy L Thompson 
14733668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14743668ca4bSJeremy L Thompson 
14753668ca4bSJeremy L Thompson   @ref User
14763668ca4bSJeremy L Thompson **/
147717b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
14783668ca4bSJeremy L Thompson   bool is_composite;
14792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14802b730f8bSJeremy L Thompson 
14813668ca4bSJeremy L Thompson   if (is_composite) {
14823668ca4bSJeremy L Thompson     // Check if composite label already created
14833668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
14843668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
14853668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
14863668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
14873668ca4bSJeremy L Thompson       }
14883668ca4bSJeremy L Thompson     }
14893668ca4bSJeremy L Thompson 
14903668ca4bSJeremy L Thompson     // Create composite label if needed
14913668ca4bSJeremy L Thompson     CeedInt               num_sub;
14923668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
14933668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
14943668ca4bSJeremy L Thompson 
14952b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1496c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1497c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14982b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
14993668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
15003668ca4bSJeremy L Thompson 
15013668ca4bSJeremy L Thompson     bool label_found = false;
15023668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
15033668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
15043668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
15052b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
15063668ca4bSJeremy L Thompson         if (new_field_label_i) {
15073668ca4bSJeremy L Thompson           label_found                    = true;
15083668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
15093668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
15103668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
15112b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
15127bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
15132b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
15142b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
15152b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
15167bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
15177bfe0f0eSJeremy L Thompson           } else {
15187bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
15197bfe0f0eSJeremy L Thompson           }
15202b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
15217bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
15222b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
15232b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
15247bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
15257bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
15267bfe0f0eSJeremy L Thompson           } else {
15277bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
15287bfe0f0eSJeremy L Thompson           }
15293668ca4bSJeremy L Thompson         }
15303668ca4bSJeremy L Thompson       }
15313668ca4bSJeremy L Thompson     }
15323668ca4bSJeremy L Thompson     if (!label_found) {
15333668ca4bSJeremy L Thompson       // LCOV_EXCL_START
15342b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
15352b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
15363668ca4bSJeremy L Thompson       *field_label = NULL;
15373668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
15383668ca4bSJeremy L Thompson     } else {
15395ac9af79SJeremy L Thompson       *field_label = new_field_label;
15405ac9af79SJeremy L Thompson     }
15415ac9af79SJeremy L Thompson   } else {
15425ac9af79SJeremy L Thompson     CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
15435ac9af79SJeremy L Thompson   }
15445ac9af79SJeremy L Thompson 
15455ac9af79SJeremy L Thompson   // Set label in operator
15465ac9af79SJeremy L Thompson   if (*field_label) {
15475ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
15485ac9af79SJeremy L Thompson 
15493668ca4bSJeremy L Thompson     // Move new composite label to operator
15503668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
15512b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
15523668ca4bSJeremy L Thompson       op->max_context_labels = 1;
15533668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
15542b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
15553668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
15563668ca4bSJeremy L Thompson     }
15575ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
15583668ca4bSJeremy L Thompson     op->num_context_labels++;
15593668ca4bSJeremy L Thompson   }
15603668ca4bSJeremy L Thompson 
15613668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15623668ca4bSJeremy L Thompson }
15633668ca4bSJeremy L Thompson 
15643668ca4bSJeremy L Thompson /**
15652788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
1566*4385fb7fSSebastian Grimberg 
15672788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1568d8dd9a91SJeremy L Thompson 
1569ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
15702788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1571ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1572d8dd9a91SJeremy L Thompson 
1573d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1574d8dd9a91SJeremy L Thompson 
1575d8dd9a91SJeremy L Thompson   @ref User
1576d8dd9a91SJeremy L Thompson **/
157717b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
15782b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1579d8dd9a91SJeremy L Thompson }
1580d8dd9a91SJeremy L Thompson 
1581d8dd9a91SJeremy L Thompson /**
15822788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
1583*4385fb7fSSebastian Grimberg 
15842788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
15852788fa27SJeremy L Thompson 
15862788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
15872788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
15882788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
15892788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15902788fa27SJeremy L Thompson 
15912788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15922788fa27SJeremy L Thompson 
15932788fa27SJeremy L Thompson   @ref User
15942788fa27SJeremy L Thompson **/
159517b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
15962788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
15972788fa27SJeremy L Thompson }
15982788fa27SJeremy L Thompson 
15992788fa27SJeremy L Thompson /**
16002788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
16012788fa27SJeremy L Thompson 
16022788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
16032788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
16042788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16052788fa27SJeremy L Thompson 
16062788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16072788fa27SJeremy L Thompson 
16082788fa27SJeremy L Thompson   @ref User
16092788fa27SJeremy L Thompson **/
161017b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
16112788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
16122788fa27SJeremy L Thompson }
16132788fa27SJeremy L Thompson 
16142788fa27SJeremy L Thompson /**
16152788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
1616*4385fb7fSSebastian Grimberg 
16172788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1618d8dd9a91SJeremy L Thompson 
1619ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1620ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1621ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1622d8dd9a91SJeremy L Thompson 
1623d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1624d8dd9a91SJeremy L Thompson 
1625d8dd9a91SJeremy L Thompson   @ref User
1626d8dd9a91SJeremy L Thompson **/
162717b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
16282b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1629d8dd9a91SJeremy L Thompson }
1630d8dd9a91SJeremy L Thompson 
1631d8dd9a91SJeremy L Thompson /**
16322788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
1633*4385fb7fSSebastian Grimberg 
16342788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
16352788fa27SJeremy L Thompson 
16362788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
16372788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1638c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
16392788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16402788fa27SJeremy L Thompson 
16412788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16422788fa27SJeremy L Thompson 
16432788fa27SJeremy L Thompson   @ref User
16442788fa27SJeremy L Thompson **/
164517b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
16462788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
16472788fa27SJeremy L Thompson }
16482788fa27SJeremy L Thompson 
16492788fa27SJeremy L Thompson /**
16502788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
16512788fa27SJeremy L Thompson 
16522788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
16532788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
16542788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
16552788fa27SJeremy L Thompson 
16562788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16572788fa27SJeremy L Thompson 
16582788fa27SJeremy L Thompson   @ref User
16592788fa27SJeremy L Thompson **/
166017b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
16612788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
16622788fa27SJeremy L Thompson }
16632788fa27SJeremy L Thompson 
16642788fa27SJeremy L Thompson /**
16653bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1666d7b241e6Sjeremylt 
1667ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1668ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1669d7b241e6Sjeremylt 
1670ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1671f04ea552SJeremy L Thompson 
1672ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1673ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
16745ac9af79SJeremy L Thompson   @param[out] out     CeedVector to store result of applying operator (must be distinct from @a in) or @ref CEED_VECTOR_NONE if there are no
16755ac9af79SJeremy L Thompson active outputs
1676ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1677b11c1e72Sjeremylt 
1678b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1679dfdf5a53Sjeremylt 
16807a982d89SJeremy L. Thompson   @ref User
1681b11c1e72Sjeremylt **/
16822b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
16832b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1684d7b241e6Sjeremylt 
1685d1d35e2fSjeremylt   if (op->num_elem) {
1686250756a7Sjeremylt     // Standard Operator
1687cae8b89aSjeremylt     if (op->Apply) {
16882b730f8bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
1689cae8b89aSjeremylt     } else {
1690cae8b89aSjeremylt       // Zero all output vectors
1691250756a7Sjeremylt       CeedQFunction qf = op->qf;
1692d1d35e2fSjeremylt       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
1693d1d35e2fSjeremylt         CeedVector vec = op->output_fields[i]->vec;
16942b730f8bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
1695cae8b89aSjeremylt         if (vec != CEED_VECTOR_NONE) {
16962b730f8bSJeremy L Thompson           CeedCall(CeedVectorSetValue(vec, 0.0));
1697cae8b89aSjeremylt         }
1698cae8b89aSjeremylt       }
1699250756a7Sjeremylt       // Apply
17002b730f8bSJeremy L Thompson       CeedCall(op->ApplyAdd(op, in, out, request));
1701250756a7Sjeremylt     }
1702f04ea552SJeremy L Thompson   } else if (op->is_composite) {
1703250756a7Sjeremylt     // Composite Operator
1704250756a7Sjeremylt     if (op->ApplyComposite) {
17052b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1706250756a7Sjeremylt     } else {
1707d1d35e2fSjeremylt       CeedInt num_suboperators;
1708c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1709d1d35e2fSjeremylt       CeedOperator *sub_operators;
1710c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1711250756a7Sjeremylt 
1712250756a7Sjeremylt       // Zero all output vectors
1713250756a7Sjeremylt       if (out != CEED_VECTOR_NONE) {
17142b730f8bSJeremy L Thompson         CeedCall(CeedVectorSetValue(out, 0.0));
1715cae8b89aSjeremylt       }
1716d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1717d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1718d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
1719250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
17202b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1721250756a7Sjeremylt           }
1722250756a7Sjeremylt         }
1723250756a7Sjeremylt       }
1724250756a7Sjeremylt       // Apply
1725d1d35e2fSjeremylt       for (CeedInt i = 0; i < op->num_suboperators; i++) {
17262b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request));
1727cae8b89aSjeremylt       }
1728cae8b89aSjeremylt     }
1729250756a7Sjeremylt   }
1730e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1731cae8b89aSjeremylt }
1732cae8b89aSjeremylt 
1733cae8b89aSjeremylt /**
1734cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1735cae8b89aSjeremylt 
1736ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1737ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1738cae8b89aSjeremylt 
1739ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1740ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1741ea61e9acSJeremy L Thompson   @param[out] out     CeedVector to sum in result of applying operator (must be distinct from @a in) or NULL if there are no active outputs
1742ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1743cae8b89aSjeremylt 
1744cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1745cae8b89aSjeremylt 
17467a982d89SJeremy L. Thompson   @ref User
1747cae8b89aSjeremylt **/
17482b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
17492b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1750cae8b89aSjeremylt 
1751d1d35e2fSjeremylt   if (op->num_elem) {
1752250756a7Sjeremylt     // Standard Operator
17532b730f8bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1754f04ea552SJeremy L Thompson   } else if (op->is_composite) {
1755250756a7Sjeremylt     // Composite Operator
1756250756a7Sjeremylt     if (op->ApplyAddComposite) {
17572b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1758cae8b89aSjeremylt     } else {
1759d1d35e2fSjeremylt       CeedInt num_suboperators;
1760c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1761d1d35e2fSjeremylt       CeedOperator *sub_operators;
1762c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1763250756a7Sjeremylt 
1764d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
17652b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
17661d7d2407SJeremy L Thompson       }
1767250756a7Sjeremylt     }
1768250756a7Sjeremylt   }
1769e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1770d7b241e6Sjeremylt }
1771d7b241e6Sjeremylt 
1772d7b241e6Sjeremylt /**
1773b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1774d7b241e6Sjeremylt 
1775ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1776b11c1e72Sjeremylt 
1777b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1778dfdf5a53Sjeremylt 
17797a982d89SJeremy L. Thompson   @ref User
1780b11c1e72Sjeremylt **/
1781d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1782ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1783ad6481ceSJeremy L Thompson     *op = NULL;
1784ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1785ad6481ceSJeremy L Thompson   }
17862b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
17872b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1788fe2413ffSjeremylt   // Free fields
17892b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1790d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1791437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1792437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
179315910d16Sjeremylt       }
1794d1d35e2fSjeremylt       if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) {
17952b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
179671352a93Sjeremylt       }
17972b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
17982b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
179971352a93Sjeremylt       }
18002b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
18012b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1802fe2413ffSjeremylt     }
18032b730f8bSJeremy L Thompson   }
18042b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1805d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1806437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1807d1d35e2fSjeremylt       if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) {
18082b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
180971352a93Sjeremylt       }
18102b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
18112b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
181271352a93Sjeremylt       }
18132b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
18142b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
18152b730f8bSJeremy L Thompson     }
1816fe2413ffSjeremylt   }
1817d1d35e2fSjeremylt   // Destroy sub_operators
18182b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1819d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
18202b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
182152d6035fSJeremy L Thompson     }
18222b730f8bSJeremy L Thompson   }
18232b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
18242b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
18252b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
18263668ca4bSJeremy L Thompson   // Destroy any composite labels
18275ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
18283668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
18292b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
18302b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
18313668ca4bSJeremy L Thompson     }
18325ac9af79SJeremy L Thompson   }
18332b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1834fe2413ffSjeremylt 
18355107b09fSJeremy L Thompson   // Destroy fallback
18362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
18375107b09fSJeremy L Thompson 
1838ed9e99e6SJeremy L Thompson   // Destroy assembly data
18392b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
18402b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
184170a7ffb3SJeremy L Thompson 
18422b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
18432b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
18442b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
18452b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
18462b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1847e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1848d7b241e6Sjeremylt }
1849d7b241e6Sjeremylt 
1850d7b241e6Sjeremylt /// @}
1851