xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision bafebce16a7da2c6afe5a069f690fc31b8ccb4ef)
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 /**
25ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` Field matches the `CeedQFunction` Field
26e15f9bd0SJeremy L Thompson 
27ca94c3ddSJeremy L Thompson   @param[in] ceed     `Ceed` object for error handling
28ca94c3ddSJeremy L Thompson   @param[in] qf_field `CeedQFunction` Field matching `CeedOperator` Field
29*bafebce1SSebastian Grimberg   @param[in] rstr     `CeedOperator` Field `CeedElemRestriction`
30*bafebce1SSebastian Grimberg   @param[in] basis    `CeedOperator` Field `CeedBasis`
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 **/
36*bafebce1SSebastian Grimberg static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction rstr, CeedBasis basis) {
37edb2538eSJeremy L Thompson   CeedInt      dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size = qf_field->size;
381c66c397SJeremy L Thompson   CeedEvalMode eval_mode = qf_field->eval_mode;
392b730f8bSJeremy L Thompson 
40e15f9bd0SJeremy L Thompson   // Restriction
41*bafebce1SSebastian Grimberg   CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE,
426574a04fSJeremy L Thompson             "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together.");
43*bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE) {
44*bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
47*bafebce1SSebastian Grimberg   CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE,
48356036faSJeremy L Thompson             "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together.");
49*bafebce1SSebastian Grimberg   if (basis != CEED_BASIS_NONE) {
50*bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetDimension(basis, &dim));
51*bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumComponents(basis, &num_comp));
52*bafebce1SSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
53*bafebce1SSebastian Grimberg     CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
54ca94c3ddSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT
55ca94c3ddSJeremy L Thompson               " components, but CeedBasis has %" CeedInt_FMT " components",
56edb2538eSJeremy L Thompson               qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp, num_comp);
57e15f9bd0SJeremy L Thompson   }
58e15f9bd0SJeremy L Thompson   // Field size
59d1d35e2fSjeremylt   switch (eval_mode) {
60e15f9bd0SJeremy L Thompson     case CEED_EVAL_NONE:
61edb2538eSJeremy L Thompson       CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION,
62ca94c3ddSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", qf_field->field_name,
63edb2538eSJeremy L Thompson                 qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp);
64e15f9bd0SJeremy L Thompson       break;
65e15f9bd0SJeremy L Thompson     case CEED_EVAL_INTERP:
66c4e3f59bSSebastian Grimberg     case CEED_EVAL_GRAD:
67c4e3f59bSSebastian Grimberg     case CEED_EVAL_DIV:
68c4e3f59bSSebastian Grimberg     case CEED_EVAL_CURL:
696574a04fSJeremy L Thompson       CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION,
70ca94c3ddSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components",
71ca94c3ddSJeremy L Thompson                 qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp);
72e15f9bd0SJeremy L Thompson       break;
73e15f9bd0SJeremy L Thompson     case CEED_EVAL_WEIGHT:
74d1d35e2fSjeremylt       // No additional checks required
75e15f9bd0SJeremy L Thompson       break;
76e15f9bd0SJeremy L Thompson   }
77e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787a982d89SJeremy L. Thompson }
797a982d89SJeremy L. Thompson 
807a982d89SJeremy L. Thompson /**
81ca94c3ddSJeremy L Thompson   @brief View a field of a `CeedOperator`
827a982d89SJeremy L. Thompson 
83ca94c3ddSJeremy L Thompson   @param[in] field        `CeedOperator` Field to view
84ca94c3ddSJeremy L Thompson   @param[in] qf_field     `CeedQFunction` Field (carries field name)
85d1d35e2fSjeremylt   @param[in] field_number Number of field being viewed
864c4400c7SValeria Barra   @param[in] sub          true indicates sub-operator, which increases indentation; false for top-level operator
87d1d35e2fSjeremylt   @param[in] input        true for an input field; false for output field
88ca94c3ddSJeremy L Thompson   @param[in] stream       Stream to view to, e.g., `stdout`
897a982d89SJeremy L. Thompson 
907a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
917a982d89SJeremy L. Thompson 
927a982d89SJeremy L. Thompson   @ref Utility
937a982d89SJeremy L. Thompson **/
942b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) {
957a982d89SJeremy L. Thompson   const char *pre    = sub ? "  " : "";
96d1d35e2fSjeremylt   const char *in_out = input ? "Input" : "Output";
977a982d89SJeremy L. Thompson 
982b730f8bSJeremy L Thompson   fprintf(stream,
992b730f8bSJeremy L Thompson           "%s    %s field %" CeedInt_FMT
1002b730f8bSJeremy L Thompson           ":\n"
1017a982d89SJeremy L. Thompson           "%s      Name: \"%s\"\n",
102d1d35e2fSjeremylt           pre, in_out, field_number, pre, qf_field->field_name);
103990fdeb6SJeremy L Thompson   fprintf(stream, "%s      Size: %" CeedInt_FMT "\n", pre, qf_field->size);
1042b730f8bSJeremy L Thompson   fprintf(stream, "%s      EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]);
105356036faSJeremy L Thompson   if (field->basis == CEED_BASIS_NONE) fprintf(stream, "%s      No basis\n", pre);
1062b730f8bSJeremy L Thompson   if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s      Active vector\n", pre);
1072b730f8bSJeremy L Thompson   else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s      No vector\n", pre);
108e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1097a982d89SJeremy L. Thompson }
1107a982d89SJeremy L. Thompson 
1117a982d89SJeremy L. Thompson /**
112ca94c3ddSJeremy L Thompson   @brief View a single `CeedOperator`
1137a982d89SJeremy L. Thompson 
114ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1157a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
116ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1177a982d89SJeremy L. Thompson 
1187a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
1197a982d89SJeremy L. Thompson 
1207a982d89SJeremy L. Thompson   @ref Utility
1217a982d89SJeremy L. Thompson **/
1227a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) {
1237a982d89SJeremy L. Thompson   const char *pre = sub ? "  " : "";
1241c66c397SJeremy L Thompson   CeedInt     num_elem, num_qpts, total_fields = 0;
1257a982d89SJeremy L. Thompson 
1262b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumElements(op, &num_elem));
1272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
1282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetNumArgs(op, &total_fields));
1291c66c397SJeremy L Thompson 
1302b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts);
1312b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : "");
1322b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : "");
133d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
1342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream));
1357a982d89SJeremy L. Thompson   }
1362b730f8bSJeremy L Thompson   fprintf(stream, "%s  %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : "");
137d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
1382b730f8bSJeremy L Thompson     CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream));
1397a982d89SJeremy L. Thompson   }
140e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1417a982d89SJeremy L. Thompson }
1427a982d89SJeremy L. Thompson 
143d99fa3c5SJeremy L Thompson /**
144ca94c3ddSJeremy L Thompson   @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator`
145eaf62fffSJeremy L Thompson 
146ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` to find active `CeedBasis` for
147ca94c3ddSJeremy L Thompson   @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator
148eaf62fffSJeremy L Thompson 
149eaf62fffSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150eaf62fffSJeremy L Thompson 
151eaf62fffSJeremy L Thompson   @ref Developer
152eaf62fffSJeremy L Thompson **/
153eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) {
154506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
155506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
156506b1a0cSSebastian Grimberg }
157506b1a0cSSebastian Grimberg 
158506b1a0cSSebastian Grimberg /**
159ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator`
160506b1a0cSSebastian Grimberg 
161ca94c3ddSJeremy L Thompson   @param[in]  op                  `CeedOperator` to find active `CeedBasis` for
162ca94c3ddSJeremy L Thompson   @param[out] active_input_basis  `CeedBasis` for active input vector or `NULL` for composite operator
163ca94c3ddSJeremy L Thompson   @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator
164506b1a0cSSebastian Grimberg 
165506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
166506b1a0cSSebastian Grimberg 
167506b1a0cSSebastian Grimberg   @ref Developer
168506b1a0cSSebastian Grimberg **/
169506b1a0cSSebastian Grimberg int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) {
1706aaed0edSJeremy L Thompson   Ceed ceed;
1711c66c397SJeremy L Thompson 
1726aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
173506b1a0cSSebastian Grimberg   if (active_input_basis) {
174506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
175506b1a0cSSebastian Grimberg     if (!op->is_composite) {
1762b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
177eaf62fffSJeremy L Thompson         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
178506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_basis || *active_input_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR,
179506b1a0cSSebastian Grimberg                     "Multiple active input CeedBases found");
180506b1a0cSSebastian Grimberg           *active_input_basis = op->input_fields[i]->basis;
181eaf62fffSJeremy L Thompson         }
1822b730f8bSJeremy L Thompson       }
183506b1a0cSSebastian Grimberg       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
184506b1a0cSSebastian Grimberg     }
185506b1a0cSSebastian Grimberg   }
186506b1a0cSSebastian Grimberg   if (active_output_basis) {
187506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
188506b1a0cSSebastian Grimberg     if (!op->is_composite) {
189506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
190506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
191506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_basis || *active_output_basis == op->output_fields[i]->basis, ceed, CEED_ERROR_MINOR,
192506b1a0cSSebastian Grimberg                     "Multiple active output CeedBases found");
193506b1a0cSSebastian Grimberg           *active_output_basis = op->output_fields[i]->basis;
194506b1a0cSSebastian Grimberg         }
195506b1a0cSSebastian Grimberg       }
196506b1a0cSSebastian Grimberg       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
197506b1a0cSSebastian Grimberg     }
198506b1a0cSSebastian Grimberg   }
199eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
200eaf62fffSJeremy L Thompson }
201eaf62fffSJeremy L Thompson 
202eaf62fffSJeremy L Thompson /**
203ca94c3ddSJeremy L Thompson   @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator`
204e2f04181SAndrew T. Barker 
205ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator` to find active `CeedElemRestriction` for
206ca94c3ddSJeremy L Thompson   @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator
207e2f04181SAndrew T. Barker 
208e2f04181SAndrew T. Barker   @return An error code: 0 - success, otherwise - failure
209e2f04181SAndrew T. Barker 
210e2f04181SAndrew T. Barker   @ref Utility
211e2f04181SAndrew T. Barker **/
2122b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) {
213506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
214506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
215506b1a0cSSebastian Grimberg }
216506b1a0cSSebastian Grimberg 
217506b1a0cSSebastian Grimberg /**
218ca94c3ddSJeremy L Thompson   @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator`
219506b1a0cSSebastian Grimberg 
220ca94c3ddSJeremy L Thompson   @param[in]  op                 `CeedOperator` to find active `CeedElemRestriction` for
221ca94c3ddSJeremy L Thompson   @param[out] active_input_rstr  `CeedElemRestriction` for active input vector or NULL for composite operator
222ca94c3ddSJeremy L Thompson   @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator
223506b1a0cSSebastian Grimberg 
224506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
225506b1a0cSSebastian Grimberg 
226506b1a0cSSebastian Grimberg   @ref Utility
227506b1a0cSSebastian Grimberg **/
228506b1a0cSSebastian Grimberg int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) {
2296aaed0edSJeremy L Thompson   Ceed ceed;
2301c66c397SJeremy L Thompson 
2316aaed0edSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
232506b1a0cSSebastian Grimberg   if (active_input_rstr) {
233506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
234506b1a0cSSebastian Grimberg     if (!op->is_composite) {
2352b730f8bSJeremy L Thompson       for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
236d1d35e2fSjeremylt         if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
237506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_rstr || *active_input_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
238506b1a0cSSebastian Grimberg                     "Multiple active input CeedElemRestrictions found");
239506b1a0cSSebastian Grimberg           *active_input_rstr = op->input_fields[i]->elem_rstr;
240e2f04181SAndrew T. Barker         }
2412b730f8bSJeremy L Thompson       }
242506b1a0cSSebastian Grimberg       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
243506b1a0cSSebastian Grimberg     }
244506b1a0cSSebastian Grimberg   }
245506b1a0cSSebastian Grimberg   if (active_output_rstr) {
246506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
247506b1a0cSSebastian Grimberg     if (!op->is_composite) {
248506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
249506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
250506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_rstr || *active_output_rstr == op->output_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
251506b1a0cSSebastian Grimberg                     "Multiple active output CeedElemRestrictions found");
252506b1a0cSSebastian Grimberg           *active_output_rstr = op->output_fields[i]->elem_rstr;
253506b1a0cSSebastian Grimberg         }
254506b1a0cSSebastian Grimberg       }
255506b1a0cSSebastian Grimberg       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
256506b1a0cSSebastian Grimberg     }
257506b1a0cSSebastian Grimberg   }
258e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
259e2f04181SAndrew T. Barker }
260e2f04181SAndrew T. Barker 
261d8dd9a91SJeremy L Thompson /**
262ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field values of the specified type.
2634385fb7fSSebastian Grimberg 
264ca94c3ddSJeremy L Thompson   For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
265ca94c3ddSJeremy 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 not have any field of a matching type.
266d8dd9a91SJeremy L Thompson 
267ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
268ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
269ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2702788fa27SJeremy L Thompson   @param[in]     values      Values to set
271d8dd9a91SJeremy L Thompson 
272d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
273d8dd9a91SJeremy L Thompson 
274d8dd9a91SJeremy L Thompson   @ref User
275d8dd9a91SJeremy L Thompson **/
2762788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2771c66c397SJeremy L Thompson   bool is_composite = false;
2781c66c397SJeremy L Thompson 
2796574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2803668ca4bSJeremy L Thompson 
2815ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2825ac9af79SJeremy L Thompson   if (field_label->from_op) {
2835ac9af79SJeremy L Thompson     CeedInt index = -1;
2845ac9af79SJeremy L Thompson 
2855ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2865ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2875ac9af79SJeremy L Thompson     }
2886574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2895ac9af79SJeremy L Thompson   }
2905ac9af79SJeremy L Thompson 
2912b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
292d8dd9a91SJeremy L Thompson   if (is_composite) {
293d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
294d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
295d8dd9a91SJeremy L Thompson 
296c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
297c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2986574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
2996574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
300d8dd9a91SJeremy L Thompson 
301d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
302d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3033668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3042788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
305d8dd9a91SJeremy L Thompson       }
306d8dd9a91SJeremy L Thompson     }
307d8dd9a91SJeremy L Thompson   } else {
3086574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3092788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
3102788fa27SJeremy L Thompson   }
3116d95ab46SJeremy L Thompson   CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true));
3122788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3132788fa27SJeremy L Thompson }
3142788fa27SJeremy L Thompson 
3152788fa27SJeremy L Thompson /**
316ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field values of the specified type, read-only.
3174385fb7fSSebastian Grimberg 
318ca94c3ddSJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
319ca94c3ddSJeremy 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 not have any field of a matching type.
3202788fa27SJeremy L Thompson 
321ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3222788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3232788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
324c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
325c5d0f995SJed Brown   @param[out]    values      Values in the label
3262788fa27SJeremy L Thompson 
3272788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3282788fa27SJeremy L Thompson 
3292788fa27SJeremy L Thompson   @ref User
3302788fa27SJeremy L Thompson **/
3312788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3322788fa27SJeremy L Thompson                                              void *values) {
3331c66c397SJeremy L Thompson   bool is_composite = false;
3341c66c397SJeremy L Thompson 
3356574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3362788fa27SJeremy L Thompson 
3372788fa27SJeremy L Thompson   *(void **)values = NULL;
3382788fa27SJeremy L Thompson   *num_values      = 0;
3392788fa27SJeremy L Thompson 
3405ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3415ac9af79SJeremy L Thompson   if (field_label->from_op) {
3425ac9af79SJeremy L Thompson     CeedInt index = -1;
3435ac9af79SJeremy L Thompson 
3445ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3455ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3465ac9af79SJeremy L Thompson     }
3476574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3485ac9af79SJeremy L Thompson   }
3495ac9af79SJeremy L Thompson 
3502788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3512788fa27SJeremy L Thompson   if (is_composite) {
3522788fa27SJeremy L Thompson     CeedInt       num_sub;
3532788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3542788fa27SJeremy L Thompson 
3552788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3562788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3576574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3586574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3592788fa27SJeremy L Thompson 
3602788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3612788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3622788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3632788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3642788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3652788fa27SJeremy L Thompson       }
3662788fa27SJeremy L Thompson     }
3672788fa27SJeremy L Thompson   } else {
3686574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3692788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3702788fa27SJeremy L Thompson   }
3712788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3722788fa27SJeremy L Thompson }
3732788fa27SJeremy L Thompson 
3742788fa27SJeremy L Thompson /**
375ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field values of the specified type, read-only.
3764385fb7fSSebastian Grimberg 
377ca94c3ddSJeremy L Thompson   For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`.
378ca94c3ddSJeremy 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 not have any field of a matching type.
3792788fa27SJeremy L Thompson 
380ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
3812788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3822788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
383c5d0f995SJed Brown   @param[in]     values      Values array to restore
3842788fa27SJeremy L Thompson 
3852788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3862788fa27SJeremy L Thompson 
3872788fa27SJeremy L Thompson   @ref User
3882788fa27SJeremy L Thompson **/
3892788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3901c66c397SJeremy L Thompson   bool is_composite = false;
3911c66c397SJeremy L Thompson 
3926574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3932788fa27SJeremy L Thompson 
3945ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3955ac9af79SJeremy L Thompson   if (field_label->from_op) {
3965ac9af79SJeremy L Thompson     CeedInt index = -1;
3975ac9af79SJeremy L Thompson 
3985ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3995ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4005ac9af79SJeremy L Thompson     }
4016574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4025ac9af79SJeremy L Thompson   }
4035ac9af79SJeremy L Thompson 
4042788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4052788fa27SJeremy L Thompson   if (is_composite) {
4062788fa27SJeremy L Thompson     CeedInt       num_sub;
4072788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4082788fa27SJeremy L Thompson 
4092788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4102788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4116574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
4126574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4132788fa27SJeremy L Thompson 
4142788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4152788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4162788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
4172788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
4182788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4192788fa27SJeremy L Thompson       }
4202788fa27SJeremy L Thompson     }
4212788fa27SJeremy L Thompson   } else {
4226574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4232788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
424d8dd9a91SJeremy L Thompson   }
425d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
426d8dd9a91SJeremy L Thompson }
427d8dd9a91SJeremy L Thompson 
4287a982d89SJeremy L. Thompson /// @}
4297a982d89SJeremy L. Thompson 
4307a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4317a982d89SJeremy L. Thompson /// CeedOperator Backend API
4327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4337a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
4347a982d89SJeremy L. Thompson /// @{
4357a982d89SJeremy L. Thompson 
4367a982d89SJeremy L. Thompson /**
437ca94c3ddSJeremy L Thompson   @brief Get the number of arguments associated with a `CeedOperator`
4387a982d89SJeremy L. Thompson 
439ca94c3ddSJeremy L Thompson   @param[in]  op        `CeedOperator`
440d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
4417a982d89SJeremy L. Thompson 
4427a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @ref Backend
4457a982d89SJeremy L. Thompson **/
446d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
4476574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
448d1d35e2fSjeremylt   *num_args = op->num_fields;
449e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4507a982d89SJeremy L. Thompson }
4517a982d89SJeremy L. Thompson 
4527a982d89SJeremy L. Thompson /**
453ca94c3ddSJeremy L Thompson   @brief Get the setup status of a `CeedOperator`
4547a982d89SJeremy L. Thompson 
455ca94c3ddSJeremy L Thompson   @param[in]  op            `CeedOperator`
456d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4577a982d89SJeremy L. Thompson 
4587a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   @ref Backend
4617a982d89SJeremy L. Thompson **/
462d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
463f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
464e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4657a982d89SJeremy L. Thompson }
4667a982d89SJeremy L. Thompson 
4677a982d89SJeremy L. Thompson /**
468ca94c3ddSJeremy L Thompson   @brief Get the `CeedQFunction` associated with a `CeedOperator`
4697a982d89SJeremy L. Thompson 
470ca94c3ddSJeremy L Thompson   @param[in]  op `CeedOperator`
471ca94c3ddSJeremy L Thompson   @param[out] qf Variable to store `CeedQFunction`
4727a982d89SJeremy L. Thompson 
4737a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4747a982d89SJeremy L. Thompson 
4757a982d89SJeremy L. Thompson   @ref Backend
4767a982d89SJeremy L. Thompson **/
4777a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
4786574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
4797a982d89SJeremy L. Thompson   *qf = op->qf;
480e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4817a982d89SJeremy L. Thompson }
4827a982d89SJeremy L. Thompson 
4837a982d89SJeremy L. Thompson /**
484ca94c3ddSJeremy L Thompson   @brief Get a boolean value indicating if the `CeedOperator` is composite
485c04a41a7SJeremy L Thompson 
486ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator`
487d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
488c04a41a7SJeremy L Thompson 
489c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
490c04a41a7SJeremy L Thompson 
491c04a41a7SJeremy L Thompson   @ref Backend
492c04a41a7SJeremy L Thompson **/
493d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
494f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
495e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
496c04a41a7SJeremy L Thompson }
497c04a41a7SJeremy L Thompson 
498c04a41a7SJeremy L Thompson /**
499ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedOperator`
5007a982d89SJeremy L. Thompson 
501ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
5027a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5037a982d89SJeremy L. Thompson 
5047a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5057a982d89SJeremy L. Thompson 
5067a982d89SJeremy L. Thompson   @ref Backend
5077a982d89SJeremy L. Thompson **/
508777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
509777ff853SJeremy L Thompson   *(void **)data = op->data;
510e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5117a982d89SJeremy L. Thompson }
5127a982d89SJeremy L. Thompson 
5137a982d89SJeremy L. Thompson /**
514ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedOperator`
5157a982d89SJeremy L. Thompson 
516ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
517ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5187a982d89SJeremy L. Thompson 
5197a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5207a982d89SJeremy L. Thompson 
5217a982d89SJeremy L. Thompson   @ref Backend
5227a982d89SJeremy L. Thompson **/
523777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
524777ff853SJeremy L Thompson   op->data = data;
525e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5267a982d89SJeremy L. Thompson }
5277a982d89SJeremy L. Thompson 
5287a982d89SJeremy L. Thompson /**
529ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedOperator`
53034359f16Sjeremylt 
531ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to increment the reference counter
53234359f16Sjeremylt 
53334359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
53434359f16Sjeremylt 
53534359f16Sjeremylt   @ref Backend
53634359f16Sjeremylt **/
5379560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
53834359f16Sjeremylt   op->ref_count++;
53934359f16Sjeremylt   return CEED_ERROR_SUCCESS;
54034359f16Sjeremylt }
54134359f16Sjeremylt 
54234359f16Sjeremylt /**
543ca94c3ddSJeremy L Thompson   @brief Set the setup flag of a `CeedOperator` to `true`
5447a982d89SJeremy L. Thompson 
545ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator`
5467a982d89SJeremy L. Thompson 
5477a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5487a982d89SJeremy L. Thompson 
5497a982d89SJeremy L. Thompson   @ref Backend
5507a982d89SJeremy L. Thompson **/
5517a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
552f04ea552SJeremy L Thompson   op->is_backend_setup = true;
553e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5547a982d89SJeremy L. Thompson }
5557a982d89SJeremy L. Thompson 
5567a982d89SJeremy L. Thompson /// @}
5577a982d89SJeremy L. Thompson 
5587a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5597a982d89SJeremy L. Thompson /// CeedOperator Public API
5607a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5617a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
562dfdf5a53Sjeremylt /// @{
563d7b241e6Sjeremylt 
564d7b241e6Sjeremylt /**
565ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` and associate a `CeedQFunction`.
5664385fb7fSSebastian Grimberg 
567ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField().
568d7b241e6Sjeremylt 
569ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
570ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
571ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
572ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE)
573ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created `CeedOperator` will be stored
574b11c1e72Sjeremylt 
575b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
576dfdf5a53Sjeremylt 
5777a982d89SJeremy L. Thompson   @ref User
578d7b241e6Sjeremylt  */
5792b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5805fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5815fe0d4faSjeremylt     Ceed delegate;
5826574a04fSJeremy L Thompson 
5832b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
584ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreate");
5852b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
586e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5875fe0d4faSjeremylt   }
5885fe0d4faSjeremylt 
589ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
590db002c03SJeremy L Thompson 
5912b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
592db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
593d1d35e2fSjeremylt   (*op)->ref_count   = 1;
5942b104005SJeremy L Thompson   (*op)->input_size  = -1;
5952b104005SJeremy L Thompson   (*op)->output_size = -1;
596db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
597db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
598db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
5992b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
6002b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
6012b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
6022b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
603e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
604d7b241e6Sjeremylt }
605d7b241e6Sjeremylt 
606d7b241e6Sjeremylt /**
607ca94c3ddSJeremy L Thompson   @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element.
60848acf710SJeremy L Thompson 
609ca94c3ddSJeremy L Thompson   A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField.
610ca94c3ddSJeremy L Thompson   The locations of each point are set with @ref CeedOperatorAtPointsSetPoints().
61148acf710SJeremy L Thompson 
612ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
613ca94c3ddSJeremy L Thompson   @param[in]  qf   `CeedQFunction` defining the action of the operator at quadrature points
614ca94c3ddSJeremy L Thompson   @param[in]  dqf  `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
615ca94c3ddSJeremy L Thompson   @param[in]  dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
61648acf710SJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
61748acf710SJeremy L Thompson 
61848acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
61948acf710SJeremy L Thompson 
62048acf710SJeremy L Thompson   @ref User
62148acf710SJeremy L Thompson  */
62248acf710SJeremy L Thompson int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
62348acf710SJeremy L Thompson   if (!ceed->OperatorCreateAtPoints) {
62448acf710SJeremy L Thompson     Ceed delegate;
62548acf710SJeremy L Thompson 
62648acf710SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
627ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreateAtPoints");
62848acf710SJeremy L Thompson     CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op));
62948acf710SJeremy L Thompson     return CEED_ERROR_SUCCESS;
63048acf710SJeremy L Thompson   }
63148acf710SJeremy L Thompson 
632ca94c3ddSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction.");
63348acf710SJeremy L Thompson 
63448acf710SJeremy L Thompson   CeedCall(CeedCalloc(1, op));
63548acf710SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
63648acf710SJeremy L Thompson   (*op)->ref_count    = 1;
63748acf710SJeremy L Thompson   (*op)->is_at_points = true;
63848acf710SJeremy L Thompson   (*op)->input_size   = -1;
63948acf710SJeremy L Thompson   (*op)->output_size  = -1;
64048acf710SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
64148acf710SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
64248acf710SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
64348acf710SJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
64448acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
64548acf710SJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
64648acf710SJeremy L Thompson   CeedCall(ceed->OperatorCreateAtPoints(*op));
64748acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
64848acf710SJeremy L Thompson }
64948acf710SJeremy L Thompson 
65048acf710SJeremy L Thompson /**
651ca94c3ddSJeremy L Thompson   @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator`
65252d6035fSJeremy L Thompson 
653ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedOperator`
654ca94c3ddSJeremy L Thompson   @param[out] op   Address of the variable where the newly created composite `CeedOperator` will be stored
65552d6035fSJeremy L Thompson 
65652d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
65752d6035fSJeremy L Thompson 
6587a982d89SJeremy L. Thompson   @ref User
65952d6035fSJeremy L Thompson  */
66052d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
66152d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
66252d6035fSJeremy L Thompson     Ceed delegate;
66352d6035fSJeremy L Thompson 
6641c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
665250756a7Sjeremylt     if (delegate) {
6662b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
667e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
66852d6035fSJeremy L Thompson     }
669250756a7Sjeremylt   }
67052d6035fSJeremy L Thompson 
6712b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
672db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
673996d9ab5SJed Brown   (*op)->ref_count    = 1;
674f04ea552SJeremy L Thompson   (*op)->is_composite = true;
6752b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
6762b104005SJeremy L Thompson   (*op)->input_size  = -1;
6772b104005SJeremy L Thompson   (*op)->output_size = -1;
678250756a7Sjeremylt 
679db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
680e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
68152d6035fSJeremy L Thompson }
68252d6035fSJeremy L Thompson 
68352d6035fSJeremy L Thompson /**
684ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedOperator`.
6854385fb7fSSebastian Grimberg 
686ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedOperatorDestroy().
687512bb800SJeremy L Thompson 
688ca94c3ddSJeremy 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`.
689ca94c3ddSJeremy L Thompson         This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`.
6909560d06aSjeremylt 
691ca94c3ddSJeremy L Thompson   @param[in]     op      `CeedOperator` to copy reference to
692ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
6939560d06aSjeremylt 
6949560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6959560d06aSjeremylt 
6969560d06aSjeremylt   @ref User
6979560d06aSjeremylt **/
6989560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
6992b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
7002b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
7019560d06aSjeremylt   *op_copy = op;
7029560d06aSjeremylt   return CEED_ERROR_SUCCESS;
7039560d06aSjeremylt }
7049560d06aSjeremylt 
7059560d06aSjeremylt /**
706ca94c3ddSJeremy L Thompson   @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`.
707d7b241e6Sjeremylt 
708ca94c3ddSJeremy L Thompson   This function is used to specify both active and passive fields to a `CeedOperator`.
709*bafebce1SSebastian Grimberg   For passive fields, a `CeedVector` `vec` must be provided.
710ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
711d7b241e6Sjeremylt 
712ca94c3ddSJeremy L Thompson   Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply().
713ca94c3ddSJeremy L Thompson   There can be at most one active input `CeedVector` and at most one active output@ref  CeedVector passed to @ref CeedOperatorApply().
714d7b241e6Sjeremylt 
715528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
716*bafebce1SSebastian Grimberg   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`.
717528a22edSJeremy L Thompson 
718ca94c3ddSJeremy L Thompson   @param[in,out] op         `CeedOperator` on which to provide the field
719ca94c3ddSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by `CeedQFunction`)
720*bafebce1SSebastian Grimberg   @param[in]     rstr       `CeedElemRestriction`
721*bafebce1SSebastian Grimberg   @param[in]     basis      `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
722*bafebce1SSebastian Grimberg   @param[in]     vec        `CeedVector` to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE if using @ref CEED_EVAL_WEIGHT in the `CeedQFunction`
723b11c1e72Sjeremylt 
724b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
725dfdf5a53Sjeremylt 
7267a982d89SJeremy L. Thompson   @ref User
727b11c1e72Sjeremylt **/
728*bafebce1SSebastian Grimberg int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) {
7291c66c397SJeremy L Thompson   bool               is_input = true;
7301c66c397SJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0;
7311c66c397SJeremy L Thompson   CeedQFunctionField qf_field;
7321c66c397SJeremy L Thompson   CeedOperatorField *op_field;
7331c66c397SJeremy L Thompson 
7346574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
7356574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
736*bafebce1SSebastian Grimberg   CeedCheck(rstr, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name);
737*bafebce1SSebastian Grimberg   CeedCheck(basis, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name);
738*bafebce1SSebastian Grimberg   CeedCheck(vec, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name);
73952d6035fSJeremy L Thompson 
740*bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
741*bafebce1SSebastian Grimberg   CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
742ca94c3ddSJeremy L Thompson             "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
7432c7e7413SJeremy L Thompson   {
7442c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
7452c7e7413SJeremy L Thompson 
746*bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
74748acf710SJeremy L Thompson     if (rstr_type == CEED_RESTRICTION_POINTS) {
748ca94c3ddSJeremy L Thompson       CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields");
749*bafebce1SSebastian Grimberg       CeedCheck(basis == CEED_BASIS_NONE, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE");
75048acf710SJeremy L Thompson       if (!op->first_points_rstr) {
751*bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr));
75248acf710SJeremy L Thompson       } else {
75348acf710SJeremy L Thompson         bool are_compatible;
75448acf710SJeremy L Thompson 
755*bafebce1SSebastian Grimberg         CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible));
75648acf710SJeremy L Thompson         CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
757ca94c3ddSJeremy L Thompson                   "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction");
75848acf710SJeremy L Thompson       }
75948acf710SJeremy L Thompson     }
7602c7e7413SJeremy L Thompson   }
761d7b241e6Sjeremylt 
762*bafebce1SSebastian Grimberg   if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts));
763*bafebce1SSebastian Grimberg   else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
764528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
765ca94c3ddSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT
766528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
767*bafebce1SSebastian Grimberg             basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts);
768d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
769d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
770d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
771d1d35e2fSjeremylt       op_field = &op->input_fields[i];
772d7b241e6Sjeremylt       goto found;
773d7b241e6Sjeremylt     }
774d7b241e6Sjeremylt   }
7752b104005SJeremy L Thompson   is_input = false;
776d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
777d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
778d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
779d1d35e2fSjeremylt       op_field = &op->output_fields[i];
780d7b241e6Sjeremylt       goto found;
781d7b241e6Sjeremylt     }
782d7b241e6Sjeremylt   }
783c042f62fSJeremy L Thompson   // LCOV_EXCL_START
784ca94c3ddSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name);
785c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
786d7b241e6Sjeremylt found:
787*bafebce1SSebastian Grimberg   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, rstr, basis));
7882b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
789e15f9bd0SJeremy L Thompson 
790*bafebce1SSebastian Grimberg   if (vec == CEED_VECTOR_ACTIVE) {
7912b104005SJeremy L Thompson     CeedSize l_size;
7921c66c397SJeremy L Thompson 
793*bafebce1SSebastian Grimberg     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
7942b104005SJeremy L Thompson     if (is_input) {
7952b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
7966574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
7976574a04fSJeremy L Thompson                 op->input_size);
7982b104005SJeremy L Thompson     } else {
7992b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
8006574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
8016574a04fSJeremy L Thompson                 op->output_size);
8022b104005SJeremy L Thompson     }
8032b730f8bSJeremy L Thompson   }
8042b104005SJeremy L Thompson 
805*bafebce1SSebastian Grimberg   CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec));
806*bafebce1SSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr));
807*bafebce1SSebastian Grimberg   if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) {
808d1d35e2fSjeremylt     op->num_elem        = num_elem;
809d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
810e15f9bd0SJeremy L Thompson   }
811*bafebce1SSebastian Grimberg   CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis));
81248acf710SJeremy L Thompson   if (op->num_qpts == 0 && !op->is_at_points) op->num_qpts = num_qpts;  // no consistent number of qpts for OperatorAtPoints
813d1d35e2fSjeremylt   op->num_fields += 1;
8142b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
815e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
816d7b241e6Sjeremylt }
817d7b241e6Sjeremylt 
818d7b241e6Sjeremylt /**
819ca94c3ddSJeremy L Thompson   @brief Get the `CeedOperator` Field of a `CeedOperator`.
82043bbe138SJeremy L Thompson 
821ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
822f04ea552SJeremy L Thompson 
823ca94c3ddSJeremy L Thompson   @param[in]  op                `CeedOperator`
824f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
825ca94c3ddSJeremy L Thompson   @param[out] input_fields      Variable to store input fields
826f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
827ca94c3ddSJeremy L Thompson   @param[out] output_fields     Variable to store output fields
82843bbe138SJeremy L Thompson 
82943bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83043bbe138SJeremy L Thompson 
831e9b533fbSJeremy L Thompson   @ref Advanced
83243bbe138SJeremy L Thompson **/
8332b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
83443bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
8356574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
8362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
83743bbe138SJeremy L Thompson 
83843bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
83943bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
84043bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
84143bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
84243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
84343bbe138SJeremy L Thompson }
84443bbe138SJeremy L Thompson 
84543bbe138SJeremy L Thompson /**
846ca94c3ddSJeremy L Thompson   @brief Set the arbitrary points in each element for a `CeedOperator` at points.
84748acf710SJeremy L Thompson 
848ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
84948acf710SJeremy L Thompson 
850ca94c3ddSJeremy L Thompson   @param[in,out] op           `CeedOperator` at points
851ca94c3ddSJeremy L Thompson   @param[in]     rstr_points  `CeedElemRestriction` for the coordinates of each point by element
852ca94c3ddSJeremy L Thompson   @param[in]     point_coords `CeedVector` holding coordinates of each point
85348acf710SJeremy L Thompson 
85448acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85548acf710SJeremy L Thompson 
85648acf710SJeremy L Thompson   @ref Advanced
85748acf710SJeremy L Thompson **/
85848acf710SJeremy L Thompson int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) {
85948acf710SJeremy L Thompson   CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
86048acf710SJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
86148acf710SJeremy L Thompson 
86248acf710SJeremy L Thompson   if (!op->first_points_rstr) {
86348acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr));
86448acf710SJeremy L Thompson   } else {
86548acf710SJeremy L Thompson     bool are_compatible;
86648acf710SJeremy L Thompson 
86748acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible));
86848acf710SJeremy L Thompson     CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE,
869ca94c3ddSJeremy L Thompson               "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction");
87048acf710SJeremy L Thompson   }
87148acf710SJeremy L Thompson 
87248acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points));
87348acf710SJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords));
87448acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87548acf710SJeremy L Thompson }
87648acf710SJeremy L Thompson 
87748acf710SJeremy L Thompson /**
878ca94c3ddSJeremy L Thompson   @brief Get the arbitrary points in each element for a `CeedOperator` at points.
87948acf710SJeremy L Thompson 
880ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
88148acf710SJeremy L Thompson 
882ca94c3ddSJeremy L Thompson   @param[in]  op           `CeedOperator` at points
883ca94c3ddSJeremy L Thompson   @param[out] rstr_points  Variable to hold `CeedElemRestriction` for the coordinates of each point by element
884ca94c3ddSJeremy L Thompson   @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point
88548acf710SJeremy L Thompson 
88648acf710SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88748acf710SJeremy L Thompson 
88848acf710SJeremy L Thompson   @ref Advanced
88948acf710SJeremy L Thompson **/
89048acf710SJeremy L Thompson int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) {
89148acf710SJeremy L Thompson   CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points");
89248acf710SJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
89348acf710SJeremy L Thompson 
89448acf710SJeremy L Thompson   if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points));
89548acf710SJeremy L Thompson   if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords));
89648acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89748acf710SJeremy L Thompson }
89848acf710SJeremy L Thompson 
89948acf710SJeremy L Thompson /**
900ca94c3ddSJeremy L Thompson   @brief Get a `CeedOperator` Field of a `CeedOperator` from its name
901de5900adSJames Wright 
902ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
903de5900adSJames Wright 
904ca94c3ddSJeremy L Thompson   @param[in]  op         `CeedOperator`
905ca94c3ddSJeremy L Thompson   @param[in]  field_name Name of desired `CeedOperator` Field
906ca94c3ddSJeremy L Thompson   @param[out] op_field   `CeedOperator` Field corresponding to the name
907de5900adSJames Wright 
908de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
909de5900adSJames Wright 
910de5900adSJames Wright   @ref Advanced
911de5900adSJames Wright **/
912de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
9131c66c397SJeremy L Thompson   char              *name;
914de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
915de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
916de5900adSJames Wright 
9171c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
918de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
919de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
920de5900adSJames Wright     if (!strcmp(name, field_name)) {
921de5900adSJames Wright       *op_field = input_fields[i];
922de5900adSJames Wright       return CEED_ERROR_SUCCESS;
923de5900adSJames Wright     }
924de5900adSJames Wright   }
925de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
926de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
927de5900adSJames Wright     if (!strcmp(name, field_name)) {
928de5900adSJames Wright       *op_field = output_fields[i];
929de5900adSJames Wright       return CEED_ERROR_SUCCESS;
930de5900adSJames Wright     }
931de5900adSJames Wright   }
932de5900adSJames Wright   // LCOV_EXCL_START
9331c66c397SJeremy L Thompson   bool has_name = op->name;
9341c66c397SJeremy L Thompson 
935de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
936de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
937de5900adSJames Wright   // LCOV_EXCL_STOP
938de5900adSJames Wright }
939de5900adSJames Wright 
940de5900adSJames Wright /**
941ca94c3ddSJeremy L Thompson   @brief Get the name of a `CeedOperator` Field
94228567f8fSJeremy L Thompson 
943ca94c3ddSJeremy L Thompson   @param[in]  op_field    `CeedOperator` Field
94428567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
94528567f8fSJeremy L Thompson 
94628567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
94728567f8fSJeremy L Thompson 
948e9b533fbSJeremy L Thompson   @ref Advanced
94928567f8fSJeremy L Thompson **/
95028567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
95128567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
95228567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
95328567f8fSJeremy L Thompson }
95428567f8fSJeremy L Thompson 
95528567f8fSJeremy L Thompson /**
956ca94c3ddSJeremy L Thompson   @brief Get the `CeedElemRestriction` of a `CeedOperator` Field
95743bbe138SJeremy L Thompson 
958ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
959ca94c3ddSJeremy L Thompson   @param[out] rstr     Variable to store `CeedElemRestriction`
96043bbe138SJeremy L Thompson 
96143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
96243bbe138SJeremy L Thompson 
963e9b533fbSJeremy L Thompson   @ref Advanced
96443bbe138SJeremy L Thompson **/
9652b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
966437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
96743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96843bbe138SJeremy L Thompson }
96943bbe138SJeremy L Thompson 
97043bbe138SJeremy L Thompson /**
971ca94c3ddSJeremy L Thompson   @brief Get the `CeedBasis` of a `CeedOperator` Field
97243bbe138SJeremy L Thompson 
973ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
974ca94c3ddSJeremy L Thompson   @param[out] basis    Variable to store `CeedBasis`
97543bbe138SJeremy L Thompson 
97643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
97743bbe138SJeremy L Thompson 
978e9b533fbSJeremy L Thompson   @ref Advanced
97943bbe138SJeremy L Thompson **/
98043bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
98143bbe138SJeremy L Thompson   *basis = op_field->basis;
98243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
98343bbe138SJeremy L Thompson }
98443bbe138SJeremy L Thompson 
98543bbe138SJeremy L Thompson /**
986ca94c3ddSJeremy L Thompson   @brief Get the `CeedVector` of a `CeedOperator` Field
98743bbe138SJeremy L Thompson 
988ca94c3ddSJeremy L Thompson   @param[in]  op_field `CeedOperator` Field
989ca94c3ddSJeremy L Thompson   @param[out] vec      Variable to store `CeedVector`
99043bbe138SJeremy L Thompson 
99143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
99243bbe138SJeremy L Thompson 
993e9b533fbSJeremy L Thompson   @ref Advanced
99443bbe138SJeremy L Thompson **/
99543bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
99643bbe138SJeremy L Thompson   *vec = op_field->vec;
99743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
99843bbe138SJeremy L Thompson }
99943bbe138SJeremy L Thompson 
100043bbe138SJeremy L Thompson /**
1001ca94c3ddSJeremy L Thompson   @brief Add a sub-operator to a composite `CeedOperator`
1002288c0443SJeremy L Thompson 
1003ca94c3ddSJeremy L Thompson   @param[in,out] composite_op Composite `CeedOperator`
1004ca94c3ddSJeremy L Thompson   @param[in]     sub_op       Sub-operator `CeedOperator`
100552d6035fSJeremy L Thompson 
100652d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
100752d6035fSJeremy L Thompson 
10087a982d89SJeremy L. Thompson   @ref User
100952d6035fSJeremy L Thompson  */
10102b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
10116574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
10126574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
10136574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
10142b730f8bSJeremy L Thompson 
10152b730f8bSJeremy L Thompson   {
10162b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
10171c66c397SJeremy L Thompson 
10182b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
10192b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
10202b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
10212b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
10226574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
10236574a04fSJeremy L Thompson               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   }
10282b730f8bSJeremy L Thompson 
1029d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
10302b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
1031d1d35e2fSjeremylt   composite_op->num_suboperators++;
1032e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
103352d6035fSJeremy L Thompson }
103452d6035fSJeremy L Thompson 
103552d6035fSJeremy L Thompson /**
1036ca94c3ddSJeremy L Thompson   @brief Get the number of sub-operators associated with a `CeedOperator`
103775f0d5a4SJeremy L Thompson 
1038ca94c3ddSJeremy L Thompson   @param[in]  op               `CeedOperator`
1039ca94c3ddSJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub-operators
104075f0d5a4SJeremy L Thompson 
104175f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
104275f0d5a4SJeremy L Thompson 
104375f0d5a4SJeremy L Thompson   @ref Backend
104475f0d5a4SJeremy L Thompson **/
1045c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
10466574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
104775f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
104875f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
104975f0d5a4SJeremy L Thompson }
105075f0d5a4SJeremy L Thompson 
105175f0d5a4SJeremy L Thompson /**
1052ca94c3ddSJeremy L Thompson   @brief Get the list of sub-operators associated with a `CeedOperator`
105375f0d5a4SJeremy L Thompson 
1054ca94c3ddSJeremy L Thompson   @param[in]  op             `CeedOperator`
1055ca94c3ddSJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub-operators
105675f0d5a4SJeremy L Thompson 
105775f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105875f0d5a4SJeremy L Thompson 
105975f0d5a4SJeremy L Thompson   @ref Backend
106075f0d5a4SJeremy L Thompson **/
1061c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
10626574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
106375f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
106475f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
106575f0d5a4SJeremy L Thompson }
106675f0d5a4SJeremy L Thompson 
106775f0d5a4SJeremy L Thompson /**
1068ca94c3ddSJeremy L Thompson   @brief Check if a `CeedOperator` is ready to be used.
10694db537f9SJeremy L Thompson 
1070ca94c3ddSJeremy L Thompson   @param[in] op `CeedOperator` to check
10714db537f9SJeremy L Thompson 
10724db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10734db537f9SJeremy L Thompson 
10744db537f9SJeremy L Thompson   @ref User
10754db537f9SJeremy L Thompson **/
10764db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
10774db537f9SJeremy L Thompson   Ceed ceed;
10781c66c397SJeremy L Thompson 
10792b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
10804db537f9SJeremy L Thompson 
10812b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
10824db537f9SJeremy L Thompson 
10834db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
10844db537f9SJeremy L Thompson   if (op->is_composite) {
108543622462SJeremy L Thompson     if (!op->num_suboperators) {
108643622462SJeremy L Thompson       // Empty operator setup
108743622462SJeremy L Thompson       op->input_size  = 0;
108843622462SJeremy L Thompson       op->output_size = 0;
108943622462SJeremy L Thompson     } else {
10904db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
10912b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
10924db537f9SJeremy L Thompson       }
10932b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
10942b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
10952b104005SJeremy L Thompson       CeedSize input_size, output_size;
10962b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
109743622462SJeremy L Thompson     }
10984db537f9SJeremy L Thompson   } else {
10996574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
11006574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
11016574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
110248acf710SJeremy L Thompson     CeedCheck(op->num_qpts > 0 || op->is_at_points, ceed, CEED_ERROR_INCOMPLETE,
1103ca94c3ddSJeremy L Thompson               "At least one non-collocated CeedBasis is required or the number of quadrature points must be set");
11042b730f8bSJeremy L Thompson   }
11054db537f9SJeremy L Thompson 
11064db537f9SJeremy L Thompson   // Flag as immutable and ready
11074db537f9SJeremy L Thompson   op->is_interface_setup = true;
11082b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
11092b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
11102b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
11114db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11124db537f9SJeremy L Thompson }
11134db537f9SJeremy L Thompson 
11144db537f9SJeremy L Thompson /**
1115ca94c3ddSJeremy L Thompson   @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`.
11164385fb7fSSebastian Grimberg 
1117ca94c3ddSJeremy L Thompson   Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output.
1118c9366a6bSJeremy L Thompson 
1119ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
1120ca94c3ddSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or `NULL`
1121ca94c3ddSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or `NULL`
1122c9366a6bSJeremy L Thompson 
1123c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1124c9366a6bSJeremy L Thompson 
1125c9366a6bSJeremy L Thompson   @ref User
1126c9366a6bSJeremy L Thompson **/
11272b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1128c9366a6bSJeremy L Thompson   bool is_composite;
1129c9366a6bSJeremy L Thompson 
11302b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
11312b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1132c9366a6bSJeremy L Thompson 
11332b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11342b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1135c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1136c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
11371c66c397SJeremy L Thompson 
11382b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
11392b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
11402b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
11412b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
11426574a04fSJeremy L Thompson       CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed,
11436574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
11442b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
11452b730f8bSJeremy L Thompson                 "shape (%td, %td)",
11462b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1147c9366a6bSJeremy L Thompson     }
11482b730f8bSJeremy L Thompson   }
1149c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1150c9366a6bSJeremy L Thompson }
1151c9366a6bSJeremy L Thompson 
1152c9366a6bSJeremy L Thompson /**
1153ca94c3ddSJeremy L Thompson   @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions.
11544385fb7fSSebastian Grimberg 
1155ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called.
1156ca94c3ddSJeremy L Thompson   When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded().
11578b919e6bSJeremy L Thompson 
1158ca94c3ddSJeremy L Thompson   @param[in] op                  `CeedOperator`
1159beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
11608b919e6bSJeremy L Thompson 
11618b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
11628b919e6bSJeremy L Thompson 
11638b919e6bSJeremy L Thompson   @ref Advanced
11648b919e6bSJeremy L Thompson **/
11652b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
11668b919e6bSJeremy L Thompson   bool is_composite;
11678b919e6bSJeremy L Thompson 
11682b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
11698b919e6bSJeremy L Thompson   if (is_composite) {
11708b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
11712b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
11728b919e6bSJeremy L Thompson     }
11738b919e6bSJeremy L Thompson   } else {
11742b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1175beecbf24SJeremy L Thompson   }
1176beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1177beecbf24SJeremy L Thompson }
1178beecbf24SJeremy L Thompson 
1179beecbf24SJeremy L Thompson /**
1180ca94c3ddSJeremy L Thompson   @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly.
1181beecbf24SJeremy L Thompson 
1182ca94c3ddSJeremy L Thompson   @param[in] op                `CeedOperator`
11836e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1184beecbf24SJeremy L Thompson 
1185beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1186beecbf24SJeremy L Thompson 
1187beecbf24SJeremy L Thompson   @ref Advanced
1188beecbf24SJeremy L Thompson **/
11892b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1190beecbf24SJeremy L Thompson   bool is_composite;
1191beecbf24SJeremy L Thompson 
11922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1193beecbf24SJeremy L Thompson   if (is_composite) {
1194beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
11952b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1196beecbf24SJeremy L Thompson     }
1197beecbf24SJeremy L Thompson   } else {
11982b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
11998b919e6bSJeremy L Thompson   }
12008b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
12018b919e6bSJeremy L Thompson }
12028b919e6bSJeremy L Thompson 
12038b919e6bSJeremy L Thompson /**
1204ca94c3ddSJeremy L Thompson   @brief Set name of `CeedOperator` for @ref CeedOperatorView() output
1205ea6b5821SJeremy L Thompson 
1206ca94c3ddSJeremy L Thompson   @param[in,out] op   `CeedOperator`
1207ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1208ea6b5821SJeremy L Thompson 
1209ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1210ea6b5821SJeremy L Thompson 
1211ea6b5821SJeremy L Thompson   @ref User
1212ea6b5821SJeremy L Thompson **/
1213ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1214ea6b5821SJeremy L Thompson   char  *name_copy;
1215ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1216ea6b5821SJeremy L Thompson 
12172b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1218ea6b5821SJeremy L Thompson   if (name_len > 0) {
12192b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1220ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1221ea6b5821SJeremy L Thompson     op->name = name_copy;
1222ea6b5821SJeremy L Thompson   }
1223ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1224ea6b5821SJeremy L Thompson }
1225ea6b5821SJeremy L Thompson 
1226ea6b5821SJeremy L Thompson /**
1227ca94c3ddSJeremy L Thompson   @brief View a `CeedOperator`
12287a982d89SJeremy L. Thompson 
1229ca94c3ddSJeremy L Thompson   @param[in] op     `CeedOperator` to view
1230ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
12317a982d89SJeremy L. Thompson 
12327a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
12337a982d89SJeremy L. Thompson 
12347a982d89SJeremy L. Thompson   @ref User
12357a982d89SJeremy L. Thompson **/
12367a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1237ea6b5821SJeremy L Thompson   bool has_name = op->name;
12387a982d89SJeremy L. Thompson 
1239f04ea552SJeremy L Thompson   if (op->is_composite) {
12402b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
12417a982d89SJeremy L. Thompson 
1242d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1243ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
12442b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
12452b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
12467a982d89SJeremy L. Thompson     }
12477a982d89SJeremy L. Thompson   } else {
12482b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
12492b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
12507a982d89SJeremy L. Thompson   }
1251e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12527a982d89SJeremy L. Thompson }
12533bd813ffSjeremylt 
12543bd813ffSjeremylt /**
1255ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedOperator`
1256b7c9bbdaSJeremy L Thompson 
1257ca94c3ddSJeremy L Thompson   @param[in]  op   `CeedOperator`
1258ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1259b7c9bbdaSJeremy L Thompson 
1260b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1261b7c9bbdaSJeremy L Thompson 
1262b7c9bbdaSJeremy L Thompson   @ref Advanced
1263b7c9bbdaSJeremy L Thompson **/
1264b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1265b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1266b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1267b7c9bbdaSJeremy L Thompson }
1268b7c9bbdaSJeremy L Thompson 
1269b7c9bbdaSJeremy L Thompson /**
1270ca94c3ddSJeremy L Thompson   @brief Get the number of elements associated with a `CeedOperator`
1271b7c9bbdaSJeremy L Thompson 
1272ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1273b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1274b7c9bbdaSJeremy L Thompson 
1275b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1276b7c9bbdaSJeremy L Thompson 
1277b7c9bbdaSJeremy L Thompson   @ref Advanced
1278b7c9bbdaSJeremy L Thompson **/
1279b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
12806574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1281b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1282b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1283b7c9bbdaSJeremy L Thompson }
1284b7c9bbdaSJeremy L Thompson 
1285b7c9bbdaSJeremy L Thompson /**
1286ca94c3ddSJeremy L Thompson   @brief Get the number of quadrature points associated with a `CeedOperator`
1287b7c9bbdaSJeremy L Thompson 
1288ca94c3ddSJeremy L Thompson   @param[in]  op       `CeedOperator`
1289b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1290b7c9bbdaSJeremy L Thompson 
1291b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1292b7c9bbdaSJeremy L Thompson 
1293b7c9bbdaSJeremy L Thompson   @ref Advanced
1294b7c9bbdaSJeremy L Thompson **/
1295b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
12966574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1297b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1298b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1299b7c9bbdaSJeremy L Thompson }
1300b7c9bbdaSJeremy L Thompson 
1301b7c9bbdaSJeremy L Thompson /**
1302ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector`
13036e15d496SJeremy L Thompson 
1304ca94c3ddSJeremy L Thompson   @param[in]  op    `CeedOperator` to estimate FLOPs for
1305ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
13066e15d496SJeremy L Thompson 
13076e15d496SJeremy L Thompson   @ref Backend
13086e15d496SJeremy L Thompson **/
13099d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
13106e15d496SJeremy L Thompson   bool is_composite;
13111c66c397SJeremy L Thompson 
13122b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
13136e15d496SJeremy L Thompson 
13146e15d496SJeremy L Thompson   *flops = 0;
13152b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13166e15d496SJeremy L Thompson   if (is_composite) {
13176e15d496SJeremy L Thompson     CeedInt num_suboperators;
13181c66c397SJeremy L Thompson 
1319c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
13206e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1321c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13226e15d496SJeremy L Thompson 
13236e15d496SJeremy L Thompson     // FLOPs for each suboperator
13246e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
13259d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
13261c66c397SJeremy L Thompson 
13272b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
13286e15d496SJeremy L Thompson       *flops += suboperator_flops;
13296e15d496SJeremy L Thompson     }
13306e15d496SJeremy L Thompson   } else {
13311c66c397SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
13326e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
13331c66c397SJeremy L Thompson 
13342b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
13352b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
13364385fb7fSSebastian Grimberg 
13376e15d496SJeremy L Thompson     // Input FLOPs
13386e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
13396e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1340edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
13416e15d496SJeremy L Thompson 
1342edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1343edb2538eSJeremy L Thompson         *flops += rstr_flops;
13442b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
13456e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
13466e15d496SJeremy L Thompson       }
13476e15d496SJeremy L Thompson     }
13486e15d496SJeremy L Thompson     // QF FLOPs
13491c66c397SJeremy L Thompson     {
13509d36ca50SJeremy L Thompson       CeedInt  num_qpts;
13519d36ca50SJeremy L Thompson       CeedSize qf_flops;
13521c66c397SJeremy L Thompson 
13532b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
13542b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
13556e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
13561c66c397SJeremy L Thompson     }
13571c66c397SJeremy L Thompson 
13586e15d496SJeremy L Thompson     // Output FLOPs
13596e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
13606e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1361edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
13626e15d496SJeremy L Thompson 
1363edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1364edb2538eSJeremy L Thompson         *flops += rstr_flops;
13652b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
13666e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
13676e15d496SJeremy L Thompson       }
13686e15d496SJeremy L Thompson     }
13696e15d496SJeremy L Thompson   }
13706e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13716e15d496SJeremy L Thompson }
13726e15d496SJeremy L Thompson 
13736e15d496SJeremy L Thompson /**
1374ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunction` global context for a `CeedOperator`.
13759fd66db6SSebastian Grimberg 
1376ca94c3ddSJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy().
1377512bb800SJeremy L Thompson 
1378ca94c3ddSJeremy 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 `CeedQFunctionContext`.
1379ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`.
13800126412dSJeremy L Thompson 
1381ca94c3ddSJeremy L Thompson   @param[in]  op  `CeedOperator`
1382ca94c3ddSJeremy L Thompson   @param[out] ctx Variable to store `CeedQFunctionContext`
13830126412dSJeremy L Thompson 
13840126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13850126412dSJeremy L Thompson 
1386859c15bbSJames Wright   @ref Advanced
13870126412dSJeremy L Thompson **/
13880126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
1389ca94c3ddSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator");
13900126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
13910126412dSJeremy L Thompson   else *ctx = NULL;
13920126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13930126412dSJeremy L Thompson }
13940126412dSJeremy L Thompson 
13950126412dSJeremy L Thompson /**
1396ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`.
13973668ca4bSJeremy L Thompson 
1398ca94c3ddSJeremy L Thompson   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()).
1399859c15bbSJames Wright 
1400ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
14013668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
14023668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
14033668ca4bSJeremy L Thompson 
14043668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14053668ca4bSJeremy L Thompson 
14063668ca4bSJeremy L Thompson   @ref User
14073668ca4bSJeremy L Thompson **/
140817b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
14091c66c397SJeremy L Thompson   bool is_composite, field_found = false;
14101c66c397SJeremy L Thompson 
14112b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
14122b730f8bSJeremy L Thompson 
14133668ca4bSJeremy L Thompson   if (is_composite) {
1414a98a090bSJeremy L Thompson     // Composite operator
1415a98a090bSJeremy L Thompson     // -- Check if composite label already created
14163668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
14173668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
14183668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
14193668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
14203668ca4bSJeremy L Thompson       }
14213668ca4bSJeremy L Thompson     }
14223668ca4bSJeremy L Thompson 
1423a98a090bSJeremy L Thompson     // -- Create composite label if needed
14243668ca4bSJeremy L Thompson     CeedInt               num_sub;
14253668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
14263668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
14273668ca4bSJeremy L Thompson 
14282b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1429c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1430c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
14312b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
14323668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
14333668ca4bSJeremy L Thompson 
14343668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
14353668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
14363668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
14371c66c397SJeremy L Thompson 
14382b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
14393668ca4bSJeremy L Thompson         if (new_field_label_i) {
1440a98a090bSJeremy L Thompson           field_found                    = true;
14413668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
14423668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
14433668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
14442b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
14457bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
14462b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
14472b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
14482b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
14497bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
14507bfe0f0eSJeremy L Thompson           } else {
14517bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
14527bfe0f0eSJeremy L Thompson           }
14532b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
14547bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
14552b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
14562b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
14577bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
14587bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
14597bfe0f0eSJeremy L Thompson           } else {
14607bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
14617bfe0f0eSJeremy L Thompson           }
14623668ca4bSJeremy L Thompson         }
14633668ca4bSJeremy L Thompson       }
14643668ca4bSJeremy L Thompson     }
1465a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1466a98a090bSJeremy L Thompson     if (field_found) {
1467a98a090bSJeremy L Thompson       *field_label = new_field_label;
1468a98a090bSJeremy L Thompson     } else {
14693668ca4bSJeremy L Thompson       // LCOV_EXCL_START
14702b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
14712b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
14723668ca4bSJeremy L Thompson       *field_label = NULL;
14733668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
14745ac9af79SJeremy L Thompson     }
14755ac9af79SJeremy L Thompson   } else {
1476a98a090bSJeremy L Thompson     // Single, non-composite operator
1477a98a090bSJeremy L Thompson     if (op->qf->ctx) {
14785ac9af79SJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1479a98a090bSJeremy L Thompson     } else {
1480a98a090bSJeremy L Thompson       *field_label = NULL;
1481a98a090bSJeremy L Thompson     }
14825ac9af79SJeremy L Thompson   }
14835ac9af79SJeremy L Thompson 
14845ac9af79SJeremy L Thompson   // Set label in operator
14855ac9af79SJeremy L Thompson   if (*field_label) {
14865ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
14875ac9af79SJeremy L Thompson 
14883668ca4bSJeremy L Thompson     // Move new composite label to operator
14893668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
14902b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
14913668ca4bSJeremy L Thompson       op->max_context_labels = 1;
14923668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
14932b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
14943668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
14953668ca4bSJeremy L Thompson     }
14965ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
14973668ca4bSJeremy L Thompson     op->num_context_labels++;
14983668ca4bSJeremy L Thompson   }
14993668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
15003668ca4bSJeremy L Thompson }
15013668ca4bSJeremy L Thompson 
15023668ca4bSJeremy L Thompson /**
1503ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values.
15044385fb7fSSebastian Grimberg 
1505ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1506d8dd9a91SJeremy L Thompson 
1507ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
15082788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1509ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1510d8dd9a91SJeremy L Thompson 
1511d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1512d8dd9a91SJeremy L Thompson 
1513d8dd9a91SJeremy L Thompson   @ref User
1514d8dd9a91SJeremy L Thompson **/
151517b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
15162b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1517d8dd9a91SJeremy L Thompson }
1518d8dd9a91SJeremy L Thompson 
1519d8dd9a91SJeremy L Thompson /**
1520ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only.
15214385fb7fSSebastian Grimberg 
1522ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
15232788fa27SJeremy L Thompson 
1524ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
15252788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
15262788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
15272788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15282788fa27SJeremy L Thompson 
15292788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15302788fa27SJeremy L Thompson 
15312788fa27SJeremy L Thompson   @ref User
15322788fa27SJeremy L Thompson **/
153317b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
15342788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
15352788fa27SJeremy L Thompson }
15362788fa27SJeremy L Thompson 
15372788fa27SJeremy L Thompson /**
1538ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only.
15392788fa27SJeremy L Thompson 
1540ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
15412788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
15422788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15432788fa27SJeremy L Thompson 
15442788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15452788fa27SJeremy L Thompson 
15462788fa27SJeremy L Thompson   @ref User
15472788fa27SJeremy L Thompson **/
154817b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
15492788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
15502788fa27SJeremy L Thompson }
15512788fa27SJeremy L Thompson 
15522788fa27SJeremy L Thompson /**
1553ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding `int32` values.
15544385fb7fSSebastian Grimberg 
1555ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
1556d8dd9a91SJeremy L Thompson 
1557ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
1558ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1559ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1560d8dd9a91SJeremy L Thompson 
1561d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1562d8dd9a91SJeremy L Thompson 
1563d8dd9a91SJeremy L Thompson   @ref User
1564d8dd9a91SJeremy L Thompson **/
156523dbfd29SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) {
15662b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1567d8dd9a91SJeremy L Thompson }
1568d8dd9a91SJeremy L Thompson 
1569d8dd9a91SJeremy L Thompson /**
1570ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only.
15714385fb7fSSebastian Grimberg 
1572ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
15732788fa27SJeremy L Thompson 
1574ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
15752788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1576ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of `int32` values in `values`
15772788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15782788fa27SJeremy L Thompson 
15792788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15802788fa27SJeremy L Thompson 
15812788fa27SJeremy L Thompson   @ref User
15822788fa27SJeremy L Thompson **/
158323dbfd29SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
15842788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
15852788fa27SJeremy L Thompson }
15862788fa27SJeremy L Thompson 
15872788fa27SJeremy L Thompson /**
1588ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only.
15892788fa27SJeremy L Thompson 
1590ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
15912788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
15922788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15932788fa27SJeremy L Thompson 
15942788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15952788fa27SJeremy L Thompson 
15962788fa27SJeremy L Thompson   @ref User
15972788fa27SJeremy L Thompson **/
159823dbfd29SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) {
15992788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
16002788fa27SJeremy L Thompson }
16012788fa27SJeremy L Thompson 
16022788fa27SJeremy L Thompson /**
1603ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values.
16045b6ec284SJeremy L Thompson 
1605ca94c3ddSJeremy L Thompson   For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`.
16065b6ec284SJeremy L Thompson 
1607ca94c3ddSJeremy L Thompson   @param[in,out] op          `CeedOperator`
16085b6ec284SJeremy L Thompson   @param[in]     field_label Label of field to set
16095b6ec284SJeremy L Thompson   @param[in]     values      Values to set
16105b6ec284SJeremy L Thompson 
16115b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16125b6ec284SJeremy L Thompson 
16135b6ec284SJeremy L Thompson   @ref User
16145b6ec284SJeremy L Thompson **/
16155b6ec284SJeremy L Thompson int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) {
16165b6ec284SJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
16175b6ec284SJeremy L Thompson }
16185b6ec284SJeremy L Thompson 
16195b6ec284SJeremy L Thompson /**
1620ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only.
16215b6ec284SJeremy L Thompson 
1622ca94c3ddSJeremy L Thompson   For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`.
16235b6ec284SJeremy L Thompson 
1624ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16255b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
1626ca94c3ddSJeremy L Thompson   @param[out] num_values  Number of boolean values in `values`
16275b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
16285b6ec284SJeremy L Thompson 
16295b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16305b6ec284SJeremy L Thompson 
16315b6ec284SJeremy L Thompson   @ref User
16325b6ec284SJeremy L Thompson **/
16335b6ec284SJeremy L Thompson int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
16345b6ec284SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values);
16355b6ec284SJeremy L Thompson }
16365b6ec284SJeremy L Thompson 
16375b6ec284SJeremy L Thompson /**
1638ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only.
16395b6ec284SJeremy L Thompson 
1640ca94c3ddSJeremy L Thompson   @param[in]  op          `CeedOperator`
16415b6ec284SJeremy L Thompson   @param[in]  field_label Label of field to get
16425b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
16435b6ec284SJeremy L Thompson 
16445b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
16455b6ec284SJeremy L Thompson 
16465b6ec284SJeremy L Thompson   @ref User
16475b6ec284SJeremy L Thompson **/
16485b6ec284SJeremy L Thompson int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) {
16495b6ec284SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values);
16505b6ec284SJeremy L Thompson }
16515b6ec284SJeremy L Thompson 
16525b6ec284SJeremy L Thompson /**
1653ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector`.
1654d7b241e6Sjeremylt 
1655ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1656ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1657d7b241e6Sjeremylt 
1658ca94c3ddSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
1659f04ea552SJeremy L Thompson 
1660ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1661ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1662ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to store result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
1663ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1664b11c1e72Sjeremylt 
1665b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1666dfdf5a53Sjeremylt 
16677a982d89SJeremy L. Thompson   @ref User
1668b11c1e72Sjeremylt **/
16692b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
16702b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1671d7b241e6Sjeremylt 
16723ca2b39bSJeremy L Thompson   if (op->is_composite) {
1673250756a7Sjeremylt     // Composite Operator
1674250756a7Sjeremylt     if (op->ApplyComposite) {
16752b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1676250756a7Sjeremylt     } else {
1677d1d35e2fSjeremylt       CeedInt       num_suboperators;
1678d1d35e2fSjeremylt       CeedOperator *sub_operators;
16791c66c397SJeremy L Thompson 
16801c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1681c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1682250756a7Sjeremylt 
1683250756a7Sjeremylt       // Zero all output vectors
16843ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1685d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1686d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1687d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
16881c66c397SJeremy L Thompson 
1689250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
16902b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1691250756a7Sjeremylt           }
1692250756a7Sjeremylt         }
1693250756a7Sjeremylt       }
1694250756a7Sjeremylt       // Apply
1695f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
1696f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1697cae8b89aSjeremylt       }
1698cae8b89aSjeremylt     }
16993ca2b39bSJeremy L Thompson   } else {
17003ca2b39bSJeremy L Thompson     // Standard Operator
17013ca2b39bSJeremy L Thompson     if (op->Apply) {
17023ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
17033ca2b39bSJeremy L Thompson     } else {
17043ca2b39bSJeremy L Thompson       // Zero all output vectors
17053ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
17061c66c397SJeremy L Thompson 
17073ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
17083ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
17091c66c397SJeremy L Thompson 
17103ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
17113ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
17123ca2b39bSJeremy L Thompson       }
17133ca2b39bSJeremy L Thompson       // Apply
17143ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
17153ca2b39bSJeremy L Thompson     }
1716250756a7Sjeremylt   }
1717e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1718cae8b89aSjeremylt }
1719cae8b89aSjeremylt 
1720cae8b89aSjeremylt /**
1721ca94c3ddSJeremy L Thompson   @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`.
1722cae8b89aSjeremylt 
1723ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1724ca94c3ddSJeremy L Thompson   All inputs and outputs must be specified using @ref CeedOperatorSetField().
1725cae8b89aSjeremylt 
1726ca94c3ddSJeremy L Thompson   @param[in]  op      `CeedOperator` to apply
1727ca94c3ddSJeremy L Thompson   @param[in]  in      `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
1728ca94c3ddSJeremy L Thompson   @param[out] out     `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
1729ca94c3ddSJeremy L Thompson   @param[in]  request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1730cae8b89aSjeremylt 
1731cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1732cae8b89aSjeremylt 
17337a982d89SJeremy L. Thompson   @ref User
1734cae8b89aSjeremylt **/
17352b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
17362b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1737cae8b89aSjeremylt 
17383ca2b39bSJeremy L Thompson   if (op->is_composite) {
1739250756a7Sjeremylt     // Composite Operator
1740250756a7Sjeremylt     if (op->ApplyAddComposite) {
17412b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1742cae8b89aSjeremylt     } else {
1743d1d35e2fSjeremylt       CeedInt       num_suboperators;
1744d1d35e2fSjeremylt       CeedOperator *sub_operators;
1745250756a7Sjeremylt 
17461c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
17471c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1748d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
17492b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
17501d7d2407SJeremy L Thompson       }
1751250756a7Sjeremylt     }
17523ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
17533ca2b39bSJeremy L Thompson     // Standard Operator
17543ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1755250756a7Sjeremylt   }
1756e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1757d7b241e6Sjeremylt }
1758d7b241e6Sjeremylt 
1759d7b241e6Sjeremylt /**
1760ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedOperator`
1761d7b241e6Sjeremylt 
1762ca94c3ddSJeremy L Thompson   @param[in,out] op `CeedOperator` to destroy
1763b11c1e72Sjeremylt 
1764b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1765dfdf5a53Sjeremylt 
17667a982d89SJeremy L. Thompson   @ref User
1767b11c1e72Sjeremylt **/
1768d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1769ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1770ad6481ceSJeremy L Thompson     *op = NULL;
1771ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1772ad6481ceSJeremy L Thompson   }
17732b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
17742b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1775fe2413ffSjeremylt   // Free fields
17762b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1777d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1778437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1779437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
178015910d16Sjeremylt       }
1781356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
17822b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
178371352a93Sjeremylt       }
17842b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
17852b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
178671352a93Sjeremylt       }
17872b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
17882b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1789fe2413ffSjeremylt     }
17902b730f8bSJeremy L Thompson   }
17912b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1792d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1793437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1794356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
17952b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
179671352a93Sjeremylt       }
17972b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
17982b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
179971352a93Sjeremylt       }
18002b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
18012b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
18022b730f8bSJeremy L Thompson     }
1803fe2413ffSjeremylt   }
180448acf710SJeremy L Thompson   // AtPoints data
180548acf710SJeremy L Thompson   CeedCall(CeedVectorDestroy(&(*op)->point_coords));
180648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points));
180748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr));
1808d1d35e2fSjeremylt   // Destroy sub_operators
18092b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1810d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
18112b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
181252d6035fSJeremy L Thompson     }
18132b730f8bSJeremy L Thompson   }
18142b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
18152b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
18162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
18173668ca4bSJeremy L Thompson   // Destroy any composite labels
18185ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
18193668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
18202b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
18212b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
18223668ca4bSJeremy L Thompson     }
18235ac9af79SJeremy L Thompson   }
18242b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1825fe2413ffSjeremylt 
18265107b09fSJeremy L Thompson   // Destroy fallback
18272b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
18285107b09fSJeremy L Thompson 
1829ed9e99e6SJeremy L Thompson   // Destroy assembly data
18302b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
18312b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
183270a7ffb3SJeremy L Thompson 
18332b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
18342b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
18352b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
18362b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
18372b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1838e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1839d7b241e6Sjeremylt }
1840d7b241e6Sjeremylt 
1841d7b241e6Sjeremylt /// @}
1842