xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-operator.c (revision 506b1a0c535297a01950817d4491440b9ddb5287)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
133d576824SJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
15dfdf5a53Sjeremylt /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25e15f9bd0SJeremy L Thompson   @brief Check if a CeedOperator Field matches the QFunction Field
26e15f9bd0SJeremy L Thompson 
27e15f9bd0SJeremy L Thompson   @param[in] ceed     Ceed object for error handling
28d1d35e2fSjeremylt   @param[in] qf_field QFunction Field matching Operator Field
29e15f9bd0SJeremy L Thompson   @param[in] r        Operator Field ElemRestriction
30e15f9bd0SJeremy L Thompson   @param[in] b        Operator Field Basis
31e15f9bd0SJeremy L Thompson 
32e15f9bd0SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
33e15f9bd0SJeremy L Thompson 
34e15f9bd0SJeremy L Thompson   @ref Developer
35e15f9bd0SJeremy L Thompson **/
362b730f8bSJeremy L Thompson static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction r, CeedBasis b) {
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
416574a04fSJeremy L Thompson   CeedCheck((r == 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.");
43e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
44edb2538eSJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(r, &rstr_num_comp));
45e1e9e29dSJed Brown   }
46e15f9bd0SJeremy L Thompson   // Basis
47356036faSJeremy L Thompson   CeedCheck((b == 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.");
49356036faSJeremy L Thompson   if (b != CEED_BASIS_NONE) {
502b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetDimension(b, &dim));
512b730f8bSJeremy L Thompson     CeedCall(CeedBasisGetNumComponents(b, &num_comp));
52c4e3f59bSSebastian Grimberg     CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp));
53edb2538eSJeremy L Thompson     CeedCheck(r == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION,
546574a04fSJeremy L Thompson               "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT " components, but Basis has %" CeedInt_FMT
556574a04fSJeremy L Thompson               " 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,
62953dad27SJames Wright                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction 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,
706574a04fSJeremy L Thompson                 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has %" CeedInt_FMT " components", qf_field->field_name,
716574a04fSJeremy L Thompson                 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 /**
817a982d89SJeremy L. Thompson   @brief View a field of a CeedOperator
827a982d89SJeremy L. Thompson 
837a982d89SJeremy L. Thompson   @param[in] field        Operator field to view
84d1d35e2fSjeremylt   @param[in] qf_field     QFunction 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
887a982d89SJeremy 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 /**
1127a982d89SJeremy L. Thompson   @brief View a single CeedOperator
1137a982d89SJeremy L. Thompson 
1147a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
1157a982d89SJeremy L. Thompson   @param[in] sub    Boolean flag for sub-operator
1167a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr 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 /**
1440f60e0a8SJeremy L Thompson   @brief Find the active vector basis for a non-composite CeedOperator
145eaf62fffSJeremy L Thompson 
146eaf62fffSJeremy L Thompson   @param[in] op            CeedOperator to find active basis for
1470f60e0a8SJeremy L Thompson   @param[out] active_basis Basis 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) {
154*506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL));
155*506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
156*506b1a0cSSebastian Grimberg }
157*506b1a0cSSebastian Grimberg 
158*506b1a0cSSebastian Grimberg /**
159*506b1a0cSSebastian Grimberg   @brief Find the active input and output vector bases for a non-composite CeedOperator
160*506b1a0cSSebastian Grimberg 
161*506b1a0cSSebastian Grimberg   @param[in] op                   CeedOperator to find active bases for
162*506b1a0cSSebastian Grimberg   @param[out] active_input_basis  Basis for active input vector or NULL for composite operator
163*506b1a0cSSebastian Grimberg   @param[out] active_output_basis Basis for active output vector or NULL for composite operator
164*506b1a0cSSebastian Grimberg 
165*506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
166*506b1a0cSSebastian Grimberg 
167*506b1a0cSSebastian Grimberg   @ref Developer
168*506b1a0cSSebastian Grimberg **/
169*506b1a0cSSebastian 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));
173*506b1a0cSSebastian Grimberg   if (active_input_basis) {
174*506b1a0cSSebastian Grimberg     *active_input_basis = NULL;
175*506b1a0cSSebastian 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) {
178*506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_basis || *active_input_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR,
179*506b1a0cSSebastian Grimberg                     "Multiple active input CeedBases found");
180*506b1a0cSSebastian Grimberg           *active_input_basis = op->input_fields[i]->basis;
181eaf62fffSJeremy L Thompson         }
1822b730f8bSJeremy L Thompson       }
183*506b1a0cSSebastian Grimberg       CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found");
184*506b1a0cSSebastian Grimberg     }
185*506b1a0cSSebastian Grimberg   }
186*506b1a0cSSebastian Grimberg   if (active_output_basis) {
187*506b1a0cSSebastian Grimberg     *active_output_basis = NULL;
188*506b1a0cSSebastian Grimberg     if (!op->is_composite) {
189*506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
190*506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
191*506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_basis || *active_output_basis == op->output_fields[i]->basis, ceed, CEED_ERROR_MINOR,
192*506b1a0cSSebastian Grimberg                     "Multiple active output CeedBases found");
193*506b1a0cSSebastian Grimberg           *active_output_basis = op->output_fields[i]->basis;
194*506b1a0cSSebastian Grimberg         }
195*506b1a0cSSebastian Grimberg       }
196*506b1a0cSSebastian Grimberg       CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found");
197*506b1a0cSSebastian Grimberg     }
198*506b1a0cSSebastian Grimberg   }
199eaf62fffSJeremy L Thompson   return CEED_ERROR_SUCCESS;
200eaf62fffSJeremy L Thompson }
201eaf62fffSJeremy L Thompson 
202eaf62fffSJeremy L Thompson /**
2030f60e0a8SJeremy L Thompson   @brief Find the active vector ElemRestriction for a non-composite CeedOperator
204e2f04181SAndrew T. Barker 
205*506b1a0cSSebastian Grimberg   @param[in] op           CeedOperator to find active ElemRestriction for
2060f60e0a8SJeremy L Thompson   @param[out] active_rstr ElemRestriction 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) {
213*506b1a0cSSebastian Grimberg   CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL));
214*506b1a0cSSebastian Grimberg   return CEED_ERROR_SUCCESS;
215*506b1a0cSSebastian Grimberg }
216*506b1a0cSSebastian Grimberg 
217*506b1a0cSSebastian Grimberg /**
218*506b1a0cSSebastian Grimberg   @brief Find the active input and output vector ElemRestrictions for a non-composite CeedOperator
219*506b1a0cSSebastian Grimberg 
220*506b1a0cSSebastian Grimberg   @param[in] op                  CeedOperator to find active ElemRestrictions for
221*506b1a0cSSebastian Grimberg   @param[out] active_input_rstr  ElemRestriction for active input vector or NULL for composite operator
222*506b1a0cSSebastian Grimberg   @param[out] active_output_rstr ElemRestriction for active output vector or NULL for composite operator
223*506b1a0cSSebastian Grimberg 
224*506b1a0cSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
225*506b1a0cSSebastian Grimberg 
226*506b1a0cSSebastian Grimberg   @ref Utility
227*506b1a0cSSebastian Grimberg **/
228*506b1a0cSSebastian 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));
232*506b1a0cSSebastian Grimberg   if (active_input_rstr) {
233*506b1a0cSSebastian Grimberg     *active_input_rstr = NULL;
234*506b1a0cSSebastian 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) {
237*506b1a0cSSebastian Grimberg           CeedCheck(!*active_input_rstr || *active_input_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
238*506b1a0cSSebastian Grimberg                     "Multiple active input CeedElemRestrictions found");
239*506b1a0cSSebastian Grimberg           *active_input_rstr = op->input_fields[i]->elem_rstr;
240e2f04181SAndrew T. Barker         }
2412b730f8bSJeremy L Thompson       }
242*506b1a0cSSebastian Grimberg       CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found");
243*506b1a0cSSebastian Grimberg     }
244*506b1a0cSSebastian Grimberg   }
245*506b1a0cSSebastian Grimberg   if (active_output_rstr) {
246*506b1a0cSSebastian Grimberg     *active_output_rstr = NULL;
247*506b1a0cSSebastian Grimberg     if (!op->is_composite) {
248*506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
249*506b1a0cSSebastian Grimberg         if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
250*506b1a0cSSebastian Grimberg           CeedCheck(!*active_output_rstr || *active_output_rstr == op->output_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR,
251*506b1a0cSSebastian Grimberg                     "Multiple active output CeedElemRestrictions found");
252*506b1a0cSSebastian Grimberg           *active_output_rstr = op->output_fields[i]->elem_rstr;
253*506b1a0cSSebastian Grimberg         }
254*506b1a0cSSebastian Grimberg       }
255*506b1a0cSSebastian Grimberg       CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found");
256*506b1a0cSSebastian Grimberg     }
257*506b1a0cSSebastian Grimberg   }
258e2f04181SAndrew T. Barker   return CEED_ERROR_SUCCESS;
259e2f04181SAndrew T. Barker }
260e2f04181SAndrew T. Barker 
261d8dd9a91SJeremy L Thompson /**
2622788fa27SJeremy L Thompson   @brief Set QFunctionContext field values of the specified type.
2634385fb7fSSebastian Grimberg 
264ea61e9acSJeremy L Thompson   For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`.
2659fd66db6SSebastian Grimberg   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
2669fd66db6SSebastian Grimberg any field of a matching type.
267d8dd9a91SJeremy L Thompson 
268ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
269ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
270ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2712788fa27SJeremy L Thompson   @param[in]     values      Values to set
272d8dd9a91SJeremy L Thompson 
273d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
274d8dd9a91SJeremy L Thompson 
275d8dd9a91SJeremy L Thompson   @ref User
276d8dd9a91SJeremy L Thompson **/
2772788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2781c66c397SJeremy L Thompson   bool is_composite = false;
2791c66c397SJeremy L Thompson 
2806574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
2813668ca4bSJeremy L Thompson 
2825ac9af79SJeremy L Thompson   // Check if field_label and op correspond
2835ac9af79SJeremy L Thompson   if (field_label->from_op) {
2845ac9af79SJeremy L Thompson     CeedInt index = -1;
2855ac9af79SJeremy L Thompson 
2865ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
2875ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
2885ac9af79SJeremy L Thompson     }
2896574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
2905ac9af79SJeremy L Thompson   }
2915ac9af79SJeremy L Thompson 
2922b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
293d8dd9a91SJeremy L Thompson   if (is_composite) {
294d8dd9a91SJeremy L Thompson     CeedInt       num_sub;
295d8dd9a91SJeremy L Thompson     CeedOperator *sub_operators;
296d8dd9a91SJeremy L Thompson 
297c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
298c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2996574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3006574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
301d8dd9a91SJeremy L Thompson 
302d8dd9a91SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
303d8dd9a91SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3043668ca4bSJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3052788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
306d8dd9a91SJeremy L Thompson       }
307d8dd9a91SJeremy L Thompson     }
308d8dd9a91SJeremy L Thompson   } else {
3096574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3102788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values));
3112788fa27SJeremy L Thompson   }
3122788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3132788fa27SJeremy L Thompson }
3142788fa27SJeremy L Thompson 
3152788fa27SJeremy L Thompson /**
3162788fa27SJeremy L Thompson   @brief Get QFunctionContext field values of the specified type, read-only.
3174385fb7fSSebastian Grimberg 
3182788fa27SJeremy L Thompson   For composite operators, the values retrieved are for the first sub-operator QFunctionContext that have a matching `field_name`.
3199fd66db6SSebastian Grimberg   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
3209fd66db6SSebastian Grimberg any field of a matching type.
3212788fa27SJeremy L Thompson 
3222788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3232788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3242788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
325c5d0f995SJed Brown   @param[out]    num_values  Number of values of type `field_type` in array `values`
326c5d0f995SJed Brown   @param[out]    values      Values in the label
3272788fa27SJeremy L Thompson 
3282788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3292788fa27SJeremy L Thompson 
3302788fa27SJeremy L Thompson   @ref User
3312788fa27SJeremy L Thompson **/
3322788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values,
3332788fa27SJeremy L Thompson                                              void *values) {
3341c66c397SJeremy L Thompson   bool is_composite = false;
3351c66c397SJeremy L Thompson 
3366574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3372788fa27SJeremy L Thompson 
3382788fa27SJeremy L Thompson   *(void **)values = NULL;
3392788fa27SJeremy L Thompson   *num_values      = 0;
3402788fa27SJeremy L Thompson 
3415ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3425ac9af79SJeremy L Thompson   if (field_label->from_op) {
3435ac9af79SJeremy L Thompson     CeedInt index = -1;
3445ac9af79SJeremy L Thompson 
3455ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
3465ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
3475ac9af79SJeremy L Thompson     }
3486574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
3495ac9af79SJeremy L Thompson   }
3505ac9af79SJeremy L Thompson 
3512788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
3522788fa27SJeremy L Thompson   if (is_composite) {
3532788fa27SJeremy L Thompson     CeedInt       num_sub;
3542788fa27SJeremy L Thompson     CeedOperator *sub_operators;
3552788fa27SJeremy L Thompson 
3562788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
3572788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
3586574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
3596574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
3602788fa27SJeremy L Thompson 
3612788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
3622788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
3632788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
3642788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values));
3652788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
3662788fa27SJeremy L Thompson       }
3672788fa27SJeremy L Thompson     }
3682788fa27SJeremy L Thompson   } else {
3696574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
3702788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values));
3712788fa27SJeremy L Thompson   }
3722788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3732788fa27SJeremy L Thompson }
3742788fa27SJeremy L Thompson 
3752788fa27SJeremy L Thompson /**
3762788fa27SJeremy L Thompson   @brief Restore QFunctionContext field values of the specified type, read-only.
3774385fb7fSSebastian Grimberg 
3782788fa27SJeremy L Thompson   For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`.
3799fd66db6SSebastian Grimberg   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
3809fd66db6SSebastian Grimberg any field of a matching type.
3812788fa27SJeremy L Thompson 
3822788fa27SJeremy L Thompson   @param[in,out] op          CeedOperator
3832788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
3842788fa27SJeremy L Thompson   @param[in]     field_type  Type of field to set
385c5d0f995SJed Brown   @param[in]     values      Values array to restore
3862788fa27SJeremy L Thompson 
3872788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3882788fa27SJeremy L Thompson 
3892788fa27SJeremy L Thompson   @ref User
3902788fa27SJeremy L Thompson **/
3912788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
3921c66c397SJeremy L Thompson   bool is_composite = false;
3931c66c397SJeremy L Thompson 
3946574a04fSJeremy L Thompson   CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label");
3952788fa27SJeremy L Thompson 
3965ac9af79SJeremy L Thompson   // Check if field_label and op correspond
3975ac9af79SJeremy L Thompson   if (field_label->from_op) {
3985ac9af79SJeremy L Thompson     CeedInt index = -1;
3995ac9af79SJeremy L Thompson 
4005ac9af79SJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
4015ac9af79SJeremy L Thompson       if (op->context_labels[i] == field_label) index = i;
4025ac9af79SJeremy L Thompson     }
4036574a04fSJeremy L Thompson     CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator");
4045ac9af79SJeremy L Thompson   }
4055ac9af79SJeremy L Thompson 
4062788fa27SJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
4072788fa27SJeremy L Thompson   if (is_composite) {
4082788fa27SJeremy L Thompson     CeedInt       num_sub;
4092788fa27SJeremy L Thompson     CeedOperator *sub_operators;
4102788fa27SJeremy L Thompson 
4112788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
4122788fa27SJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
4136574a04fSJeremy L Thompson     CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED,
4146574a04fSJeremy L Thompson               "Composite operator modified after ContextFieldLabel created");
4152788fa27SJeremy L Thompson 
4162788fa27SJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
4172788fa27SJeremy L Thompson       // Try every sub-operator, ok if some sub-operators do not have field
4182788fa27SJeremy L Thompson       if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) {
4192788fa27SJeremy L Thompson         CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values));
4202788fa27SJeremy L Thompson         return CEED_ERROR_SUCCESS;
4212788fa27SJeremy L Thompson       }
4222788fa27SJeremy L Thompson     }
4232788fa27SJeremy L Thompson   } else {
4246574a04fSJeremy L Thompson     CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data");
4252788fa27SJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values));
426d8dd9a91SJeremy L Thompson   }
427d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
428d8dd9a91SJeremy L Thompson }
429d8dd9a91SJeremy L Thompson 
4307a982d89SJeremy L. Thompson /// @}
4317a982d89SJeremy L. Thompson 
4327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4337a982d89SJeremy L. Thompson /// CeedOperator Backend API
4347a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
4357a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend
4367a982d89SJeremy L. Thompson /// @{
4377a982d89SJeremy L. Thompson 
4387a982d89SJeremy L. Thompson /**
4397a982d89SJeremy L. Thompson   @brief Get the number of arguments associated with a CeedOperator
4407a982d89SJeremy L. Thompson 
441ea61e9acSJeremy L Thompson   @param[in]  op        CeedOperator
442d1d35e2fSjeremylt   @param[out] num_args  Variable to store vector number of arguments
4437a982d89SJeremy L. Thompson 
4447a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4457a982d89SJeremy L. Thompson 
4467a982d89SJeremy L. Thompson   @ref Backend
4477a982d89SJeremy L. Thompson **/
448d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) {
4496574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators");
450d1d35e2fSjeremylt   *num_args = op->num_fields;
451e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4527a982d89SJeremy L. Thompson }
4537a982d89SJeremy L. Thompson 
4547a982d89SJeremy L. Thompson /**
4557a982d89SJeremy L. Thompson   @brief Get the setup status of a CeedOperator
4567a982d89SJeremy L. Thompson 
457ea61e9acSJeremy L Thompson   @param[in]  op            CeedOperator
458d1d35e2fSjeremylt   @param[out] is_setup_done Variable to store setup status
4597a982d89SJeremy L. Thompson 
4607a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4617a982d89SJeremy L. Thompson 
4627a982d89SJeremy L. Thompson   @ref Backend
4637a982d89SJeremy L. Thompson **/
464d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) {
465f04ea552SJeremy L Thompson   *is_setup_done = op->is_backend_setup;
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4677a982d89SJeremy L. Thompson }
4687a982d89SJeremy L. Thompson 
4697a982d89SJeremy L. Thompson /**
4707a982d89SJeremy L. Thompson   @brief Get the QFunction associated with a CeedOperator
4717a982d89SJeremy L. Thompson 
472ea61e9acSJeremy L Thompson   @param[in]  op CeedOperator
4737a982d89SJeremy L. Thompson   @param[out] qf Variable to store QFunction
4747a982d89SJeremy L. Thompson 
4757a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4767a982d89SJeremy L. Thompson 
4777a982d89SJeremy L. Thompson   @ref Backend
4787a982d89SJeremy L. Thompson **/
4797a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) {
4806574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
4817a982d89SJeremy L. Thompson   *qf = op->qf;
482e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4837a982d89SJeremy L. Thompson }
4847a982d89SJeremy L. Thompson 
4857a982d89SJeremy L. Thompson /**
486c04a41a7SJeremy L Thompson   @brief Get a boolean value indicating if the CeedOperator is composite
487c04a41a7SJeremy L Thompson 
488ea61e9acSJeremy L Thompson   @param[in]  op           CeedOperator
489d1d35e2fSjeremylt   @param[out] is_composite Variable to store composite status
490c04a41a7SJeremy L Thompson 
491c04a41a7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
492c04a41a7SJeremy L Thompson 
493c04a41a7SJeremy L Thompson   @ref Backend
494c04a41a7SJeremy L Thompson **/
495d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) {
496f04ea552SJeremy L Thompson   *is_composite = op->is_composite;
497e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
498c04a41a7SJeremy L Thompson }
499c04a41a7SJeremy L Thompson 
500c04a41a7SJeremy L Thompson /**
5017a982d89SJeremy L. Thompson   @brief Get the backend data of a CeedOperator
5027a982d89SJeremy L. Thompson 
503ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
5047a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5057a982d89SJeremy L. Thompson 
5067a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5077a982d89SJeremy L. Thompson 
5087a982d89SJeremy L. Thompson   @ref Backend
5097a982d89SJeremy L. Thompson **/
510777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) {
511777ff853SJeremy L Thompson   *(void **)data = op->data;
512e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5137a982d89SJeremy L. Thompson }
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson /**
5167a982d89SJeremy L. Thompson   @brief Set the backend data of a CeedOperator
5177a982d89SJeremy L. Thompson 
518ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
519ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5207a982d89SJeremy L. Thompson 
5217a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5227a982d89SJeremy L. Thompson 
5237a982d89SJeremy L. Thompson   @ref Backend
5247a982d89SJeremy L. Thompson **/
525777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) {
526777ff853SJeremy L Thompson   op->data = data;
527e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5287a982d89SJeremy L. Thompson }
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson /**
53134359f16Sjeremylt   @brief Increment the reference counter for a CeedOperator
53234359f16Sjeremylt 
533ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to increment the reference counter
53434359f16Sjeremylt 
53534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
53634359f16Sjeremylt 
53734359f16Sjeremylt   @ref Backend
53834359f16Sjeremylt **/
5399560d06aSjeremylt int CeedOperatorReference(CeedOperator op) {
54034359f16Sjeremylt   op->ref_count++;
54134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
54234359f16Sjeremylt }
54334359f16Sjeremylt 
54434359f16Sjeremylt /**
5457a982d89SJeremy L. Thompson   @brief Set the setup flag of a CeedOperator to True
5467a982d89SJeremy L. Thompson 
547ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator
5487a982d89SJeremy L. Thompson 
5497a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5507a982d89SJeremy L. Thompson 
5517a982d89SJeremy L. Thompson   @ref Backend
5527a982d89SJeremy L. Thompson **/
5537a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) {
554f04ea552SJeremy L Thompson   op->is_backend_setup = true;
555e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5567a982d89SJeremy L. Thompson }
5577a982d89SJeremy L. Thompson 
5587a982d89SJeremy L. Thompson /// @}
5597a982d89SJeremy L. Thompson 
5607a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5617a982d89SJeremy L. Thompson /// CeedOperator Public API
5627a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5637a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser
564dfdf5a53Sjeremylt /// @{
565d7b241e6Sjeremylt 
566d7b241e6Sjeremylt /**
567ea61e9acSJeremy L Thompson   @brief Create a CeedOperator and associate a CeedQFunction.
5684385fb7fSSebastian Grimberg 
569ea61e9acSJeremy L Thompson   A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField.
570d7b241e6Sjeremylt 
571ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
572ea61e9acSJeremy L Thompson   @param[in]  qf   QFunction defining the action of the operator at quadrature points
573ea61e9acSJeremy L Thompson   @param[in]  dqf  QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
574ea61e9acSJeremy L Thompson   @param[in]  dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE)
575ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created CeedOperator will be stored
576b11c1e72Sjeremylt 
577b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
578dfdf5a53Sjeremylt 
5797a982d89SJeremy L. Thompson   @ref User
580d7b241e6Sjeremylt  */
5812b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) {
5825fe0d4faSjeremylt   if (!ceed->OperatorCreate) {
5835fe0d4faSjeremylt     Ceed delegate;
5846574a04fSJeremy L Thompson 
5852b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
5866574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate");
5872b730f8bSJeremy L Thompson     CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op));
588e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5895fe0d4faSjeremylt   }
5905fe0d4faSjeremylt 
5916574a04fSJeremy L Thompson   CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction.");
592db002c03SJeremy L Thompson 
5932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
594db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
595d1d35e2fSjeremylt   (*op)->ref_count   = 1;
5962b104005SJeremy L Thompson   (*op)->input_size  = -1;
5972b104005SJeremy L Thompson   (*op)->output_size = -1;
598db002c03SJeremy L Thompson   CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf));
599db002c03SJeremy L Thompson   if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf));
600db002c03SJeremy L Thompson   if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT));
6012b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled));
6022b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields));
6032b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields));
6042b730f8bSJeremy L Thompson   CeedCall(ceed->OperatorCreate(*op));
605e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
606d7b241e6Sjeremylt }
607d7b241e6Sjeremylt 
608d7b241e6Sjeremylt /**
60952d6035fSJeremy L Thompson   @brief Create an operator that composes the action of several operators
61052d6035fSJeremy L Thompson 
611ea61e9acSJeremy L Thompson   @param[in]  ceed Ceed object where the CeedOperator will be created
612ea61e9acSJeremy L Thompson   @param[out] op   Address of the variable where the newly created Composite CeedOperator will be stored
61352d6035fSJeremy L Thompson 
61452d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
61552d6035fSJeremy L Thompson 
6167a982d89SJeremy L. Thompson   @ref User
61752d6035fSJeremy L Thompson  */
61852d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) {
61952d6035fSJeremy L Thompson   if (!ceed->CompositeOperatorCreate) {
62052d6035fSJeremy L Thompson     Ceed delegate;
62152d6035fSJeremy L Thompson 
6221c66c397SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator"));
623250756a7Sjeremylt     if (delegate) {
6242b730f8bSJeremy L Thompson       CeedCall(CeedCompositeOperatorCreate(delegate, op));
625e15f9bd0SJeremy L Thompson       return CEED_ERROR_SUCCESS;
62652d6035fSJeremy L Thompson     }
627250756a7Sjeremylt   }
62852d6035fSJeremy L Thompson 
6292b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op));
630db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed));
631996d9ab5SJed Brown   (*op)->ref_count    = 1;
632f04ea552SJeremy L Thompson   (*op)->is_composite = true;
6332b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators));
6342b104005SJeremy L Thompson   (*op)->input_size  = -1;
6352b104005SJeremy L Thompson   (*op)->output_size = -1;
636250756a7Sjeremylt 
637db002c03SJeremy L Thompson   if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op));
638e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
63952d6035fSJeremy L Thompson }
64052d6035fSJeremy L Thompson 
64152d6035fSJeremy L Thompson /**
642ea61e9acSJeremy L Thompson   @brief Copy the pointer to a CeedOperator.
6434385fb7fSSebastian Grimberg 
644ea61e9acSJeremy L Thompson   Both pointers should be destroyed with `CeedOperatorDestroy()`.
645512bb800SJeremy L Thompson 
646512bb800SJeremy 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.
647512bb800SJeremy L Thompson         This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator.
6489560d06aSjeremylt 
649ea61e9acSJeremy L Thompson   @param[in]  op         CeedOperator to copy reference to
650ea61e9acSJeremy L Thompson   @param[in,out] op_copy Variable to store copied reference
6519560d06aSjeremylt 
6529560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6539560d06aSjeremylt 
6549560d06aSjeremylt   @ref User
6559560d06aSjeremylt **/
6569560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) {
6572b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(op));
6582b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(op_copy));
6599560d06aSjeremylt   *op_copy = op;
6609560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6619560d06aSjeremylt }
6629560d06aSjeremylt 
6639560d06aSjeremylt /**
664ea61e9acSJeremy L Thompson   @brief Provide a field to a CeedOperator for use by its CeedQFunction.
665d7b241e6Sjeremylt 
666ea61e9acSJeremy L Thompson   This function is used to specify both active and passive fields to a CeedOperator.
667ea61e9acSJeremy L Thompson   For passive fields, a vector @arg v must be provided.
668ea61e9acSJeremy L Thompson   Passive fields can inputs or outputs (updated in-place when operator is applied).
669d7b241e6Sjeremylt 
670ea61e9acSJeremy L Thompson   Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply().
671ea61e9acSJeremy L Thompson   There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply().
672d7b241e6Sjeremylt 
673528a22edSJeremy L Thompson   The number of quadrature points must agree across all points.
674356036faSJeremy L Thompson   When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of r.
675528a22edSJeremy L Thompson 
676ea61e9acSJeremy L Thompson   @param[in,out] op         CeedOperator on which to provide the field
677ea61e9acSJeremy L Thompson   @param[in]     field_name Name of the field (to be matched with the name used by CeedQFunction)
678ea61e9acSJeremy L Thompson   @param[in]     r          CeedElemRestriction
679356036faSJeremy L Thompson   @param[in]     b          CeedBasis in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points
6805ac9af79SJeremy L Thompson   @param[in]     v          CeedVector to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE
6815ac9af79SJeremy L Thompson                               if using @ref CEED_EVAL_WEIGHT in the QFunction
682b11c1e72Sjeremylt 
683b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
684dfdf5a53Sjeremylt 
6857a982d89SJeremy L. Thompson   @ref User
686b11c1e72Sjeremylt **/
6872b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) {
6881c66c397SJeremy L Thompson   bool               is_input = true;
6891c66c397SJeremy L Thompson   CeedInt            num_elem = 0, num_qpts = 0;
6901c66c397SJeremy L Thompson   CeedQFunctionField qf_field;
6911c66c397SJeremy L Thompson   CeedOperatorField *op_field;
6921c66c397SJeremy L Thompson 
6936574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator.");
6946574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
6956574a04fSJeremy L Thompson   CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name);
6966574a04fSJeremy L Thompson   CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name);
6976574a04fSJeremy L Thompson   CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name);
69852d6035fSJeremy L Thompson 
6992b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem));
7006574a04fSJeremy L Thompson   CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION,
7012b730f8bSJeremy L Thompson             "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem);
7022c7e7413SJeremy L Thompson   {
7032c7e7413SJeremy L Thompson     CeedRestrictionType rstr_type;
7042c7e7413SJeremy L Thompson 
7052c7e7413SJeremy L Thompson     CeedCall(CeedElemRestrictionGetType(r, &rstr_type));
7062c7e7413SJeremy L Thompson     CeedCheck(rstr_type != CEED_RESTRICTION_POINTS, op->ceed, CEED_ERROR_UNSUPPORTED,
7072c7e7413SJeremy L Thompson               "CeedElemRestrictionAtPoints not supported for standard operator fields");
7082c7e7413SJeremy L Thompson   }
709d7b241e6Sjeremylt 
710356036faSJeremy L Thompson   if (b == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts));
7119a18a30cSJeremy L Thompson   else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts));
712528a22edSJeremy L Thompson   CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION,
713528a22edSJeremy L Thompson             "%s must correspond to the same number of quadrature points as previously added Bases. Found %" CeedInt_FMT
714528a22edSJeremy L Thompson             " quadrature points but expected %" CeedInt_FMT " quadrature points.",
715356036faSJeremy L Thompson             b == CEED_BASIS_NONE ? "ElemRestriction" : "Basis", num_qpts, op->num_qpts);
716d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_input_fields; i++) {
717d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) {
718d1d35e2fSjeremylt       qf_field = op->qf->input_fields[i];
719d1d35e2fSjeremylt       op_field = &op->input_fields[i];
720d7b241e6Sjeremylt       goto found;
721d7b241e6Sjeremylt     }
722d7b241e6Sjeremylt   }
7232b104005SJeremy L Thompson   is_input = false;
724d1d35e2fSjeremylt   for (CeedInt i = 0; i < op->qf->num_output_fields; i++) {
725d1d35e2fSjeremylt     if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) {
726d1d35e2fSjeremylt       qf_field = op->qf->output_fields[i];
727d1d35e2fSjeremylt       op_field = &op->output_fields[i];
728d7b241e6Sjeremylt       goto found;
729d7b241e6Sjeremylt     }
730d7b241e6Sjeremylt   }
731c042f62fSJeremy L Thompson   // LCOV_EXCL_START
7322b730f8bSJeremy L Thompson   return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name);
733c042f62fSJeremy L Thompson   // LCOV_EXCL_STOP
734d7b241e6Sjeremylt found:
7352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b));
7362b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, op_field));
737e15f9bd0SJeremy L Thompson 
7382b104005SJeremy L Thompson   if (v == CEED_VECTOR_ACTIVE) {
7392b104005SJeremy L Thompson     CeedSize l_size;
7401c66c397SJeremy L Thompson 
7412b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size));
7422b104005SJeremy L Thompson     if (is_input) {
7432b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = l_size;
7446574a04fSJeremy L Thompson       CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
7456574a04fSJeremy L Thompson                 op->input_size);
7462b104005SJeremy L Thompson     } else {
7472b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = l_size;
7486574a04fSJeremy L Thompson       CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size,
7496574a04fSJeremy L Thompson                 op->output_size);
7502b104005SJeremy L Thompson     }
7512b730f8bSJeremy L Thompson   }
7522b104005SJeremy L Thompson 
753393ac2cdSJeremy L Thompson   CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec));
754393ac2cdSJeremy L Thompson   CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr));
755e15f9bd0SJeremy L Thompson   if (r != CEED_ELEMRESTRICTION_NONE) {
756d1d35e2fSjeremylt     op->num_elem        = num_elem;
757d1d35e2fSjeremylt     op->has_restriction = true;  // Restriction set, but num_elem may be 0
758e15f9bd0SJeremy L Thompson   }
759393ac2cdSJeremy L Thompson   CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis));
7605a72c492SJeremy L Thompson   if (op->num_qpts == 0) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts));
761e15f9bd0SJeremy L Thompson 
762d1d35e2fSjeremylt   op->num_fields += 1;
7632b730f8bSJeremy L Thompson   CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name));
764e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
765d7b241e6Sjeremylt }
766d7b241e6Sjeremylt 
767d7b241e6Sjeremylt /**
76843bbe138SJeremy L Thompson   @brief Get the CeedOperatorFields of a CeedOperator
76943bbe138SJeremy L Thompson 
770ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
771f04ea552SJeremy L Thompson 
772ea61e9acSJeremy L Thompson   @param[in]  op                CeedOperator
773f74ec584SJeremy L Thompson   @param[out] num_input_fields  Variable to store number of input fields
77443bbe138SJeremy L Thompson   @param[out] input_fields      Variable to store input_fields
775f74ec584SJeremy L Thompson   @param[out] num_output_fields Variable to store number of output fields
77643bbe138SJeremy L Thompson   @param[out] output_fields     Variable to store output_fields
77743bbe138SJeremy L Thompson 
77843bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
77943bbe138SJeremy L Thompson 
780e9b533fbSJeremy L Thompson   @ref Advanced
78143bbe138SJeremy L Thompson **/
7822b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields,
78343bbe138SJeremy L Thompson                           CeedOperatorField **output_fields) {
7846574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
7852b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
78643bbe138SJeremy L Thompson 
78743bbe138SJeremy L Thompson   if (num_input_fields) *num_input_fields = op->qf->num_input_fields;
78843bbe138SJeremy L Thompson   if (input_fields) *input_fields = op->input_fields;
78943bbe138SJeremy L Thompson   if (num_output_fields) *num_output_fields = op->qf->num_output_fields;
79043bbe138SJeremy L Thompson   if (output_fields) *output_fields = op->output_fields;
79143bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79243bbe138SJeremy L Thompson }
79343bbe138SJeremy L Thompson 
79443bbe138SJeremy L Thompson /**
795de5900adSJames Wright   @brief Get a CeedOperatorField of an CeedOperator from its name
796de5900adSJames Wright 
797de5900adSJames Wright   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
798de5900adSJames Wright 
799de5900adSJames Wright   @param[in]  op         CeedOperator
800de5900adSJames Wright   @param[in]  field_name Name of desired CeedOperatorField
801de5900adSJames Wright   @param[out] op_field   CeedOperatorField corresponding to the name
802de5900adSJames Wright 
803de5900adSJames Wright   @return An error code: 0 - success, otherwise - failure
804de5900adSJames Wright 
805de5900adSJames Wright   @ref Advanced
806de5900adSJames Wright **/
807de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) {
8081c66c397SJeremy L Thompson   char              *name;
809de5900adSJames Wright   CeedInt            num_input_fields, num_output_fields;
810de5900adSJames Wright   CeedOperatorField *input_fields, *output_fields;
811de5900adSJames Wright 
8121c66c397SJeremy L Thompson   CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
813de5900adSJames Wright   for (CeedInt i = 0; i < num_input_fields; i++) {
814de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(input_fields[i], &name));
815de5900adSJames Wright     if (!strcmp(name, field_name)) {
816de5900adSJames Wright       *op_field = input_fields[i];
817de5900adSJames Wright       return CEED_ERROR_SUCCESS;
818de5900adSJames Wright     }
819de5900adSJames Wright   }
820de5900adSJames Wright   for (CeedInt i = 0; i < num_output_fields; i++) {
821de5900adSJames Wright     CeedCall(CeedOperatorFieldGetName(output_fields[i], &name));
822de5900adSJames Wright     if (!strcmp(name, field_name)) {
823de5900adSJames Wright       *op_field = output_fields[i];
824de5900adSJames Wright       return CEED_ERROR_SUCCESS;
825de5900adSJames Wright     }
826de5900adSJames Wright   }
827de5900adSJames Wright   // LCOV_EXCL_START
8281c66c397SJeremy L Thompson   bool has_name = op->name;
8291c66c397SJeremy L Thompson 
830de5900adSJames Wright   return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "",
831de5900adSJames Wright                    has_name ? op->name : "", has_name ? "\"" : "");
832de5900adSJames Wright   // LCOV_EXCL_STOP
833de5900adSJames Wright }
834de5900adSJames Wright 
835de5900adSJames Wright /**
83628567f8fSJeremy L Thompson   @brief Get the name of a CeedOperatorField
83728567f8fSJeremy L Thompson 
838ea61e9acSJeremy L Thompson   @param[in]  op_field    CeedOperatorField
83928567f8fSJeremy L Thompson   @param[out] field_name  Variable to store the field name
84028567f8fSJeremy L Thompson 
84128567f8fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
84228567f8fSJeremy L Thompson 
843e9b533fbSJeremy L Thompson   @ref Advanced
84428567f8fSJeremy L Thompson **/
84528567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) {
84628567f8fSJeremy L Thompson   *field_name = (char *)op_field->field_name;
84728567f8fSJeremy L Thompson   return CEED_ERROR_SUCCESS;
84828567f8fSJeremy L Thompson }
84928567f8fSJeremy L Thompson 
85028567f8fSJeremy L Thompson /**
85143bbe138SJeremy L Thompson   @brief Get the CeedElemRestriction of a CeedOperatorField
85243bbe138SJeremy L Thompson 
853ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
85443bbe138SJeremy L Thompson   @param[out] rstr     Variable to store CeedElemRestriction
85543bbe138SJeremy L Thompson 
85643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
85743bbe138SJeremy L Thompson 
858e9b533fbSJeremy L Thompson   @ref Advanced
85943bbe138SJeremy L Thompson **/
8602b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) {
861437c7c90SJeremy L Thompson   *rstr = op_field->elem_rstr;
86243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
86343bbe138SJeremy L Thompson }
86443bbe138SJeremy L Thompson 
86543bbe138SJeremy L Thompson /**
86643bbe138SJeremy L Thompson   @brief Get the CeedBasis of a CeedOperatorField
86743bbe138SJeremy L Thompson 
868ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
86943bbe138SJeremy L Thompson   @param[out] basis    Variable to store CeedBasis
87043bbe138SJeremy L Thompson 
87143bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
87243bbe138SJeremy L Thompson 
873e9b533fbSJeremy L Thompson   @ref Advanced
87443bbe138SJeremy L Thompson **/
87543bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) {
87643bbe138SJeremy L Thompson   *basis = op_field->basis;
87743bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
87843bbe138SJeremy L Thompson }
87943bbe138SJeremy L Thompson 
88043bbe138SJeremy L Thompson /**
88143bbe138SJeremy L Thompson   @brief Get the CeedVector of a CeedOperatorField
88243bbe138SJeremy L Thompson 
883ea61e9acSJeremy L Thompson   @param[in]  op_field CeedOperatorField
88443bbe138SJeremy L Thompson   @param[out] vec      Variable to store CeedVector
88543bbe138SJeremy L Thompson 
88643bbe138SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
88743bbe138SJeremy L Thompson 
888e9b533fbSJeremy L Thompson   @ref Advanced
88943bbe138SJeremy L Thompson **/
89043bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) {
89143bbe138SJeremy L Thompson   *vec = op_field->vec;
89243bbe138SJeremy L Thompson   return CEED_ERROR_SUCCESS;
89343bbe138SJeremy L Thompson }
89443bbe138SJeremy L Thompson 
89543bbe138SJeremy L Thompson /**
89652d6035fSJeremy L Thompson   @brief Add a sub-operator to a composite CeedOperator
897288c0443SJeremy L Thompson 
898ea61e9acSJeremy L Thompson   @param[in,out] composite_op Composite CeedOperator
899ea61e9acSJeremy L Thompson   @param[in]     sub_op       Sub-operator CeedOperator
90052d6035fSJeremy L Thompson 
90152d6035fSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
90252d6035fSJeremy L Thompson 
9037a982d89SJeremy L. Thompson   @ref User
90452d6035fSJeremy L Thompson  */
9052b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) {
9066574a04fSJeremy L Thompson   CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator");
9076574a04fSJeremy L Thompson   CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators");
9086574a04fSJeremy L Thompson   CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
9092b730f8bSJeremy L Thompson 
9102b730f8bSJeremy L Thompson   {
9112b730f8bSJeremy L Thompson     CeedSize input_size, output_size;
9121c66c397SJeremy L Thompson 
9132b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size));
9142b730f8bSJeremy L Thompson     if (composite_op->input_size == -1) composite_op->input_size = input_size;
9152b730f8bSJeremy L Thompson     if (composite_op->output_size == -1) composite_op->output_size = output_size;
9162b730f8bSJeremy L Thompson     // Note, a size of -1 means no active vector restriction set, so no incompatibility
9176574a04fSJeremy L Thompson     CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size),
9186574a04fSJeremy L Thompson               composite_op->ceed, CEED_ERROR_MAJOR,
9192b730f8bSJeremy L Thompson               "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
9202b730f8bSJeremy L Thompson               "shape (%td, %td)",
9212b730f8bSJeremy L Thompson               composite_op->input_size, composite_op->output_size, input_size, output_size);
9222b730f8bSJeremy L Thompson   }
9232b730f8bSJeremy L Thompson 
924d1d35e2fSjeremylt   composite_op->sub_operators[composite_op->num_suboperators] = sub_op;
9252b730f8bSJeremy L Thompson   CeedCall(CeedOperatorReference(sub_op));
926d1d35e2fSjeremylt   composite_op->num_suboperators++;
927e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
92852d6035fSJeremy L Thompson }
92952d6035fSJeremy L Thompson 
93052d6035fSJeremy L Thompson /**
93175f0d5a4SJeremy L Thompson   @brief Get the number of sub_operators associated with a CeedOperator
93275f0d5a4SJeremy L Thompson 
93375f0d5a4SJeremy L Thompson   @param[in]  op               CeedOperator
93475f0d5a4SJeremy L Thompson   @param[out] num_suboperators Variable to store number of sub_operators
93575f0d5a4SJeremy L Thompson 
93675f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
93775f0d5a4SJeremy L Thompson 
93875f0d5a4SJeremy L Thompson   @ref Backend
93975f0d5a4SJeremy L Thompson **/
940c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) {
9416574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
94275f0d5a4SJeremy L Thompson   *num_suboperators = op->num_suboperators;
94375f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
94475f0d5a4SJeremy L Thompson }
94575f0d5a4SJeremy L Thompson 
94675f0d5a4SJeremy L Thompson /**
94775f0d5a4SJeremy L Thompson   @brief Get the list of sub_operators associated with a CeedOperator
94875f0d5a4SJeremy L Thompson 
94975f0d5a4SJeremy L Thompson   @param op                  CeedOperator
95075f0d5a4SJeremy L Thompson   @param[out] sub_operators  Variable to store list of sub_operators
95175f0d5a4SJeremy L Thompson 
95275f0d5a4SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
95375f0d5a4SJeremy L Thompson 
95475f0d5a4SJeremy L Thompson   @ref Backend
95575f0d5a4SJeremy L Thompson **/
956c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) {
9576574a04fSJeremy L Thompson   CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator");
95875f0d5a4SJeremy L Thompson   *sub_operators = op->sub_operators;
95975f0d5a4SJeremy L Thompson   return CEED_ERROR_SUCCESS;
96075f0d5a4SJeremy L Thompson }
96175f0d5a4SJeremy L Thompson 
96275f0d5a4SJeremy L Thompson /**
9634db537f9SJeremy L Thompson   @brief Check if a CeedOperator is ready to be used.
9644db537f9SJeremy L Thompson 
9654db537f9SJeremy L Thompson   @param[in] op CeedOperator to check
9664db537f9SJeremy L Thompson 
9674db537f9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9684db537f9SJeremy L Thompson 
9694db537f9SJeremy L Thompson   @ref User
9704db537f9SJeremy L Thompson **/
9714db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) {
9724db537f9SJeremy L Thompson   Ceed ceed;
9731c66c397SJeremy L Thompson 
9742b730f8bSJeremy L Thompson   CeedCall(CeedOperatorGetCeed(op, &ceed));
9754db537f9SJeremy L Thompson 
9762b730f8bSJeremy L Thompson   if (op->is_interface_setup) return CEED_ERROR_SUCCESS;
9774db537f9SJeremy L Thompson 
9784db537f9SJeremy L Thompson   CeedQFunction qf = op->qf;
9794db537f9SJeremy L Thompson   if (op->is_composite) {
98043622462SJeremy L Thompson     if (!op->num_suboperators) {
98143622462SJeremy L Thompson       // Empty operator setup
98243622462SJeremy L Thompson       op->input_size  = 0;
98343622462SJeremy L Thompson       op->output_size = 0;
98443622462SJeremy L Thompson     } else {
9854db537f9SJeremy L Thompson       for (CeedInt i = 0; i < op->num_suboperators; i++) {
9862b730f8bSJeremy L Thompson         CeedCall(CeedOperatorCheckReady(op->sub_operators[i]));
9874db537f9SJeremy L Thompson       }
9882b104005SJeremy L Thompson       // Sub-operators could be modified after adding to composite operator
9892b104005SJeremy L Thompson       // Need to verify no lvec incompatibility from any changes
9902b104005SJeremy L Thompson       CeedSize input_size, output_size;
9912b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size));
99243622462SJeremy L Thompson     }
9934db537f9SJeremy L Thompson   } else {
9946574a04fSJeremy L Thompson     CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set");
9956574a04fSJeremy L Thompson     CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set");
9966574a04fSJeremy L Thompson     CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required");
9976574a04fSJeremy L Thompson     CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE,
9986574a04fSJeremy L Thompson               "At least one non-collocated basis is required or the number of quadrature points must be set");
9992b730f8bSJeremy L Thompson   }
10004db537f9SJeremy L Thompson 
10014db537f9SJeremy L Thompson   // Flag as immutable and ready
10024db537f9SJeremy L Thompson   op->is_interface_setup = true;
10032b730f8bSJeremy L Thompson   if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true;
10042b730f8bSJeremy L Thompson   if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true;
10052b730f8bSJeremy L Thompson   if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true;
10064db537f9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10074db537f9SJeremy L Thompson }
10084db537f9SJeremy L Thompson 
10094db537f9SJeremy L Thompson /**
1010c9366a6bSJeremy L Thompson   @brief Get vector lengths for the active input and/or output vectors of a CeedOperator.
10114385fb7fSSebastian Grimberg 
1012ea61e9acSJeremy L Thompson   Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output.
1013c9366a6bSJeremy L Thompson 
1014c9366a6bSJeremy L Thompson   @param[in]  op          CeedOperator
1015c9366a6bSJeremy L Thompson   @param[out] input_size  Variable to store active input vector length, or NULL
1016c9366a6bSJeremy L Thompson   @param[out] output_size Variable to store active output vector length, or NULL
1017c9366a6bSJeremy L Thompson 
1018c9366a6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1019c9366a6bSJeremy L Thompson 
1020c9366a6bSJeremy L Thompson   @ref User
1021c9366a6bSJeremy L Thompson **/
10222b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) {
1023c9366a6bSJeremy L Thompson   bool is_composite;
1024c9366a6bSJeremy L Thompson 
10252b104005SJeremy L Thompson   if (input_size) *input_size = op->input_size;
10262b104005SJeremy L Thompson   if (output_size) *output_size = op->output_size;
1027c9366a6bSJeremy L Thompson 
10282b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10292b104005SJeremy L Thompson   if (is_composite && (op->input_size == -1 || op->output_size == -1)) {
1030c9366a6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1031c9366a6bSJeremy L Thompson       CeedSize sub_input_size, sub_output_size;
10321c66c397SJeremy L Thompson 
10332b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size));
10342b104005SJeremy L Thompson       if (op->input_size == -1) op->input_size = sub_input_size;
10352b104005SJeremy L Thompson       if (op->output_size == -1) op->output_size = sub_output_size;
10362b104005SJeremy L Thompson       // Note, a size of -1 means no active vector restriction set, so no incompatibility
10376574a04fSJeremy 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,
10386574a04fSJeremy L Thompson                 CEED_ERROR_MAJOR,
10392b730f8bSJeremy L Thompson                 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of "
10402b730f8bSJeremy L Thompson                 "shape (%td, %td)",
10412b104005SJeremy L Thompson                 op->input_size, op->output_size, input_size, output_size);
1042c9366a6bSJeremy L Thompson     }
10432b730f8bSJeremy L Thompson   }
1044c9366a6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1045c9366a6bSJeremy L Thompson }
1046c9366a6bSJeremy L Thompson 
1047c9366a6bSJeremy L Thompson /**
1048beecbf24SJeremy L Thompson   @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions.
10494385fb7fSSebastian Grimberg 
1050ea61e9acSJeremy L Thompson   When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a
10519fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called.
10529fd66db6SSebastian Grimberg   When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to
10539fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`.
10548b919e6bSJeremy L Thompson 
1055beecbf24SJeremy L Thompson   @param[in] op                  CeedOperator
1056beecbf24SJeremy L Thompson   @param[in] reuse_assembly_data Boolean flag setting assembly data reuse
10578b919e6bSJeremy L Thompson 
10588b919e6bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
10598b919e6bSJeremy L Thompson 
10608b919e6bSJeremy L Thompson   @ref Advanced
10618b919e6bSJeremy L Thompson **/
10622b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) {
10638b919e6bSJeremy L Thompson   bool is_composite;
10648b919e6bSJeremy L Thompson 
10652b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
10668b919e6bSJeremy L Thompson   if (is_composite) {
10678b919e6bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10682b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data));
10698b919e6bSJeremy L Thompson     }
10708b919e6bSJeremy L Thompson   } else {
10712b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data));
1072beecbf24SJeremy L Thompson   }
1073beecbf24SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1074beecbf24SJeremy L Thompson }
1075beecbf24SJeremy L Thompson 
1076beecbf24SJeremy L Thompson /**
1077beecbf24SJeremy L Thompson   @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly.
1078beecbf24SJeremy L Thompson 
1079beecbf24SJeremy L Thompson   @param[in] op                CeedOperator
10806e15d496SJeremy L Thompson   @param[in] needs_data_update Boolean flag setting assembly data reuse
1081beecbf24SJeremy L Thompson 
1082beecbf24SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1083beecbf24SJeremy L Thompson 
1084beecbf24SJeremy L Thompson   @ref Advanced
1085beecbf24SJeremy L Thompson **/
10862b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) {
1087beecbf24SJeremy L Thompson   bool is_composite;
1088beecbf24SJeremy L Thompson 
10892b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
1090beecbf24SJeremy L Thompson   if (is_composite) {
1091beecbf24SJeremy L Thompson     for (CeedInt i = 0; i < op->num_suboperators; i++) {
10922b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update));
1093beecbf24SJeremy L Thompson     }
1094beecbf24SJeremy L Thompson   } else {
10952b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update));
10968b919e6bSJeremy L Thompson   }
10978b919e6bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
10988b919e6bSJeremy L Thompson }
10998b919e6bSJeremy L Thompson 
11008b919e6bSJeremy L Thompson /**
1101cd4dfc48Sjeremylt   @brief Set the number of quadrature points associated with a CeedOperator.
11024385fb7fSSebastian Grimberg 
1103ea61e9acSJeremy L Thompson   This should be used when creating a CeedOperator where every field has a collocated basis.
1104ea61e9acSJeremy L Thompson   This function cannot be used for composite CeedOperators.
1105cd4dfc48Sjeremylt 
1106ea61e9acSJeremy L Thompson   @param[in,out] op       CeedOperator
1107ea61e9acSJeremy L Thompson   @param[in]     num_qpts Number of quadrature points to set
1108cd4dfc48Sjeremylt 
1109cd4dfc48Sjeremylt   @return An error code: 0 - success, otherwise - failure
1110cd4dfc48Sjeremylt 
1111e9b533fbSJeremy L Thompson   @ref Advanced
1112cd4dfc48Sjeremylt **/
1113cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) {
1114aca496beSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite CeedOperator");
11156574a04fSJeremy L Thompson   CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable");
1116aca496beSJeremy L Thompson   if (op->num_qpts > 0) {
1117aca496beSJeremy L Thompson     CeedWarn(
1118aca496beSJeremy L Thompson         "CeedOperatorSetNumQuadraturePoints will be removed from the libCEED interface in the next release.\n"
1119ac5aa7bcSJeremy L Thompson         "This function is redundant and you can safely remove any calls to this function without replacing them.");
1120aca496beSJeremy L Thompson     CeedCheck(num_qpts == op->num_qpts, op->ceed, CEED_ERROR_DIMENSION, "Different number of quadrature points already defined for the CeedOperator");
1121aca496beSJeremy L Thompson   }
1122cd4dfc48Sjeremylt   op->num_qpts = num_qpts;
1123cd4dfc48Sjeremylt   return CEED_ERROR_SUCCESS;
1124cd4dfc48Sjeremylt }
1125cd4dfc48Sjeremylt 
1126cd4dfc48Sjeremylt /**
1127ea6b5821SJeremy L Thompson   @brief Set name of CeedOperator for CeedOperatorView output
1128ea6b5821SJeremy L Thompson 
1129ea61e9acSJeremy L Thompson   @param[in,out] op   CeedOperator
1130ea61e9acSJeremy L Thompson   @param[in]     name Name to set, or NULL to remove previously set name
1131ea6b5821SJeremy L Thompson 
1132ea6b5821SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1133ea6b5821SJeremy L Thompson 
1134ea6b5821SJeremy L Thompson   @ref User
1135ea6b5821SJeremy L Thompson **/
1136ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) {
1137ea6b5821SJeremy L Thompson   char  *name_copy;
1138ea6b5821SJeremy L Thompson   size_t name_len = name ? strlen(name) : 0;
1139ea6b5821SJeremy L Thompson 
11402b730f8bSJeremy L Thompson   CeedCall(CeedFree(&op->name));
1141ea6b5821SJeremy L Thompson   if (name_len > 0) {
11422b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(name_len + 1, &name_copy));
1143ea6b5821SJeremy L Thompson     memcpy(name_copy, name, name_len);
1144ea6b5821SJeremy L Thompson     op->name = name_copy;
1145ea6b5821SJeremy L Thompson   }
1146ea6b5821SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1147ea6b5821SJeremy L Thompson }
1148ea6b5821SJeremy L Thompson 
1149ea6b5821SJeremy L Thompson /**
11507a982d89SJeremy L. Thompson   @brief View a CeedOperator
11517a982d89SJeremy L. Thompson 
11527a982d89SJeremy L. Thompson   @param[in] op     CeedOperator to view
11537a982d89SJeremy L. Thompson   @param[in] stream Stream to write; typically stdout/stderr or a file
11547a982d89SJeremy L. Thompson 
11557a982d89SJeremy L. Thompson   @return Error code: 0 - success, otherwise - failure
11567a982d89SJeremy L. Thompson 
11577a982d89SJeremy L. Thompson   @ref User
11587a982d89SJeremy L. Thompson **/
11597a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) {
1160ea6b5821SJeremy L Thompson   bool has_name = op->name;
11617a982d89SJeremy L. Thompson 
1162f04ea552SJeremy L Thompson   if (op->is_composite) {
11632b730f8bSJeremy L Thompson     fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11647a982d89SJeremy L. Thompson 
1165d1d35e2fSjeremylt     for (CeedInt i = 0; i < op->num_suboperators; i++) {
1166ea6b5821SJeremy L Thompson       has_name = op->sub_operators[i]->name;
11672b730f8bSJeremy L Thompson       fprintf(stream, "  SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : "");
11682b730f8bSJeremy L Thompson       CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream));
11697a982d89SJeremy L. Thompson     }
11707a982d89SJeremy L. Thompson   } else {
11712b730f8bSJeremy L Thompson     fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : "");
11722b730f8bSJeremy L Thompson     CeedCall(CeedOperatorSingleView(op, 0, stream));
11737a982d89SJeremy L. Thompson   }
1174e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11757a982d89SJeremy L. Thompson }
11763bd813ffSjeremylt 
11773bd813ffSjeremylt /**
1178b7c9bbdaSJeremy L Thompson   @brief Get the Ceed associated with a CeedOperator
1179b7c9bbdaSJeremy L Thompson 
1180ea61e9acSJeremy L Thompson   @param[in]  op   CeedOperator
1181b7c9bbdaSJeremy L Thompson   @param[out] ceed Variable to store Ceed
1182b7c9bbdaSJeremy L Thompson 
1183b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1184b7c9bbdaSJeremy L Thompson 
1185b7c9bbdaSJeremy L Thompson   @ref Advanced
1186b7c9bbdaSJeremy L Thompson **/
1187b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) {
1188b7c9bbdaSJeremy L Thompson   *ceed = op->ceed;
1189b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1190b7c9bbdaSJeremy L Thompson }
1191b7c9bbdaSJeremy L Thompson 
1192b7c9bbdaSJeremy L Thompson /**
1193b7c9bbdaSJeremy L Thompson   @brief Get the number of elements associated with a CeedOperator
1194b7c9bbdaSJeremy L Thompson 
1195ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1196b7c9bbdaSJeremy L Thompson   @param[out] num_elem Variable to store number of elements
1197b7c9bbdaSJeremy L Thompson 
1198b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1199b7c9bbdaSJeremy L Thompson 
1200b7c9bbdaSJeremy L Thompson   @ref Advanced
1201b7c9bbdaSJeremy L Thompson **/
1202b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) {
12036574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1204b7c9bbdaSJeremy L Thompson   *num_elem = op->num_elem;
1205b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1206b7c9bbdaSJeremy L Thompson }
1207b7c9bbdaSJeremy L Thompson 
1208b7c9bbdaSJeremy L Thompson /**
1209b7c9bbdaSJeremy L Thompson   @brief Get the number of quadrature points associated with a CeedOperator
1210b7c9bbdaSJeremy L Thompson 
1211ea61e9acSJeremy L Thompson   @param[in]  op       CeedOperator
1212b7c9bbdaSJeremy L Thompson   @param[out] num_qpts Variable to store vector number of quadrature points
1213b7c9bbdaSJeremy L Thompson 
1214b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1215b7c9bbdaSJeremy L Thompson 
1216b7c9bbdaSJeremy L Thompson   @ref Advanced
1217b7c9bbdaSJeremy L Thompson **/
1218b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) {
12196574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator");
1220b7c9bbdaSJeremy L Thompson   *num_qpts = op->num_qpts;
1221b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1222b7c9bbdaSJeremy L Thompson }
1223b7c9bbdaSJeremy L Thompson 
1224b7c9bbdaSJeremy L Thompson /**
12256e15d496SJeremy L Thompson   @brief Estimate number of FLOPs required to apply CeedOperator on the active vector
12266e15d496SJeremy L Thompson 
1227ea61e9acSJeremy L Thompson   @param[in]  op    CeedOperator to estimate FLOPs for
1228ea61e9acSJeremy L Thompson   @param[out] flops Address of variable to hold FLOPs estimate
12296e15d496SJeremy L Thompson 
12306e15d496SJeremy L Thompson   @ref Backend
12316e15d496SJeremy L Thompson **/
12329d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) {
12336e15d496SJeremy L Thompson   bool is_composite;
12341c66c397SJeremy L Thompson 
12352b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
12366e15d496SJeremy L Thompson 
12376e15d496SJeremy L Thompson   *flops = 0;
12382b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
12396e15d496SJeremy L Thompson   if (is_composite) {
12406e15d496SJeremy L Thompson     CeedInt num_suboperators;
12411c66c397SJeremy L Thompson 
1242c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
12436e15d496SJeremy L Thompson     CeedOperator *sub_operators;
1244c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
12456e15d496SJeremy L Thompson 
12466e15d496SJeremy L Thompson     // FLOPs for each suboperator
12476e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_suboperators; i++) {
12489d36ca50SJeremy L Thompson       CeedSize suboperator_flops;
12491c66c397SJeremy L Thompson 
12502b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops));
12516e15d496SJeremy L Thompson       *flops += suboperator_flops;
12526e15d496SJeremy L Thompson     }
12536e15d496SJeremy L Thompson   } else {
12541c66c397SJeremy L Thompson     CeedInt            num_input_fields, num_output_fields, num_elem = 0;
12556e15d496SJeremy L Thompson     CeedOperatorField *input_fields, *output_fields;
12561c66c397SJeremy L Thompson 
12572b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
12582b730f8bSJeremy L Thompson     CeedCall(CeedOperatorGetNumElements(op, &num_elem));
12594385fb7fSSebastian Grimberg 
12606e15d496SJeremy L Thompson     // Input FLOPs
12616e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
12626e15d496SJeremy L Thompson       if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1263edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
12646e15d496SJeremy L Thompson 
1265edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops));
1266edb2538eSJeremy L Thompson         *flops += rstr_flops;
12672b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops));
12686e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12696e15d496SJeremy L Thompson       }
12706e15d496SJeremy L Thompson     }
12716e15d496SJeremy L Thompson     // QF FLOPs
12721c66c397SJeremy L Thompson     {
12739d36ca50SJeremy L Thompson       CeedInt  num_qpts;
12749d36ca50SJeremy L Thompson       CeedSize qf_flops;
12751c66c397SJeremy L Thompson 
12762b730f8bSJeremy L Thompson       CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts));
12772b730f8bSJeremy L Thompson       CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops));
12786e15d496SJeremy L Thompson       *flops += num_elem * num_qpts * qf_flops;
12791c66c397SJeremy L Thompson     }
12801c66c397SJeremy L Thompson 
12816e15d496SJeremy L Thompson     // Output FLOPs
12826e15d496SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
12836e15d496SJeremy L Thompson       if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) {
1284edb2538eSJeremy L Thompson         CeedSize rstr_flops, basis_flops;
12856e15d496SJeremy L Thompson 
1286edb2538eSJeremy L Thompson         CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops));
1287edb2538eSJeremy L Thompson         *flops += rstr_flops;
12882b730f8bSJeremy L Thompson         CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops));
12896e15d496SJeremy L Thompson         *flops += basis_flops * num_elem;
12906e15d496SJeremy L Thompson       }
12916e15d496SJeremy L Thompson     }
12926e15d496SJeremy L Thompson   }
12936e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12946e15d496SJeremy L Thompson }
12956e15d496SJeremy L Thompson 
12966e15d496SJeremy L Thompson /**
12970126412dSJeremy L Thompson   @brief Get CeedQFunction global context for a CeedOperator.
12989fd66db6SSebastian Grimberg 
1299512bb800SJeremy L Thompson   The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`.
1300512bb800SJeremy L Thompson 
13019fd66db6SSebastian Grimberg   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.
13029fd66db6SSebastian Grimberg         This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext.
13030126412dSJeremy L Thompson 
1304859c15bbSJames Wright   @param[in]  op  CeedOperator
13050126412dSJeremy L Thompson   @param[out] ctx Variable to store CeedQFunctionContext
13060126412dSJeremy L Thompson 
13070126412dSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13080126412dSJeremy L Thompson 
1309859c15bbSJames Wright   @ref Advanced
13100126412dSJeremy L Thompson **/
13110126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) {
13126574a04fSJeremy L Thompson   CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator");
13130126412dSJeremy L Thompson   if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx));
13140126412dSJeremy L Thompson   else *ctx = NULL;
13150126412dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13160126412dSJeremy L Thompson }
13170126412dSJeremy L Thompson 
13180126412dSJeremy L Thompson /**
1319ea61e9acSJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`.
13203668ca4bSJeremy L Thompson 
1321859c15bbSJames Wright   Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`).
1322859c15bbSJames Wright 
13233668ca4bSJeremy L Thompson   @param[in]  op          CeedOperator
13243668ca4bSJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
13253668ca4bSJeremy L Thompson   @param[out] field_label Variable to field label
13263668ca4bSJeremy L Thompson 
13273668ca4bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13283668ca4bSJeremy L Thompson 
13293668ca4bSJeremy L Thompson   @ref User
13303668ca4bSJeremy L Thompson **/
133117b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) {
13321c66c397SJeremy L Thompson   bool is_composite, field_found = false;
13331c66c397SJeremy L Thompson 
13342b730f8bSJeremy L Thompson   CeedCall(CeedOperatorIsComposite(op, &is_composite));
13352b730f8bSJeremy L Thompson 
13363668ca4bSJeremy L Thompson   if (is_composite) {
1337a98a090bSJeremy L Thompson     // Composite operator
1338a98a090bSJeremy L Thompson     // -- Check if composite label already created
13393668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < op->num_context_labels; i++) {
13403668ca4bSJeremy L Thompson       if (!strcmp(op->context_labels[i]->name, field_name)) {
13413668ca4bSJeremy L Thompson         *field_label = op->context_labels[i];
13423668ca4bSJeremy L Thompson         return CEED_ERROR_SUCCESS;
13433668ca4bSJeremy L Thompson       }
13443668ca4bSJeremy L Thompson     }
13453668ca4bSJeremy L Thompson 
1346a98a090bSJeremy L Thompson     // -- Create composite label if needed
13473668ca4bSJeremy L Thompson     CeedInt               num_sub;
13483668ca4bSJeremy L Thompson     CeedOperator         *sub_operators;
13493668ca4bSJeremy L Thompson     CeedContextFieldLabel new_field_label;
13503668ca4bSJeremy L Thompson 
13512b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(1, &new_field_label));
1352c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub));
1353c6ebc35dSJeremy L Thompson     CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
13542b730f8bSJeremy L Thompson     CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels));
13553668ca4bSJeremy L Thompson     new_field_label->num_sub_labels = num_sub;
13563668ca4bSJeremy L Thompson 
13573668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < num_sub; i++) {
13583668ca4bSJeremy L Thompson       if (sub_operators[i]->qf->ctx) {
13593668ca4bSJeremy L Thompson         CeedContextFieldLabel new_field_label_i;
13601c66c397SJeremy L Thompson 
13612b730f8bSJeremy L Thompson         CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i));
13623668ca4bSJeremy L Thompson         if (new_field_label_i) {
1363a98a090bSJeremy L Thompson           field_found                    = true;
13643668ca4bSJeremy L Thompson           new_field_label->sub_labels[i] = new_field_label_i;
13653668ca4bSJeremy L Thompson           new_field_label->name          = new_field_label_i->name;
13663668ca4bSJeremy L Thompson           new_field_label->description   = new_field_label_i->description;
13672b730f8bSJeremy L Thompson           if (new_field_label->type && new_field_label->type != new_field_label_i->type) {
13687bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13692b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13702b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s",
13712b730f8bSJeremy L Thompson                              CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]);
13727bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13737bfe0f0eSJeremy L Thompson           } else {
13747bfe0f0eSJeremy L Thompson             new_field_label->type = new_field_label_i->type;
13757bfe0f0eSJeremy L Thompson           }
13762b730f8bSJeremy L Thompson           if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) {
13777bfe0f0eSJeremy L Thompson             // LCOV_EXCL_START
13782b730f8bSJeremy L Thompson             CeedCall(CeedFree(&new_field_label));
13792b730f8bSJeremy L Thompson             return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld",
13807bfe0f0eSJeremy L Thompson                              new_field_label->num_values, new_field_label_i->num_values);
13817bfe0f0eSJeremy L Thompson             // LCOV_EXCL_STOP
13827bfe0f0eSJeremy L Thompson           } else {
13837bfe0f0eSJeremy L Thompson             new_field_label->num_values = new_field_label_i->num_values;
13847bfe0f0eSJeremy L Thompson           }
13853668ca4bSJeremy L Thompson         }
13863668ca4bSJeremy L Thompson       }
13873668ca4bSJeremy L Thompson     }
1388a98a090bSJeremy L Thompson     // -- Cleanup if field was found
1389a98a090bSJeremy L Thompson     if (field_found) {
1390a98a090bSJeremy L Thompson       *field_label = new_field_label;
1391a98a090bSJeremy L Thompson     } else {
13923668ca4bSJeremy L Thompson       // LCOV_EXCL_START
13932b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label->sub_labels));
13942b730f8bSJeremy L Thompson       CeedCall(CeedFree(&new_field_label));
13953668ca4bSJeremy L Thompson       *field_label = NULL;
13963668ca4bSJeremy L Thompson       // LCOV_EXCL_STOP
13975ac9af79SJeremy L Thompson     }
13985ac9af79SJeremy L Thompson   } else {
1399a98a090bSJeremy L Thompson     // Single, non-composite operator
1400a98a090bSJeremy L Thompson     if (op->qf->ctx) {
14015ac9af79SJeremy L Thompson       CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label));
1402a98a090bSJeremy L Thompson     } else {
1403a98a090bSJeremy L Thompson       *field_label = NULL;
1404a98a090bSJeremy L Thompson     }
14055ac9af79SJeremy L Thompson   }
14065ac9af79SJeremy L Thompson 
14075ac9af79SJeremy L Thompson   // Set label in operator
14085ac9af79SJeremy L Thompson   if (*field_label) {
14095ac9af79SJeremy L Thompson     (*field_label)->from_op = true;
14105ac9af79SJeremy L Thompson 
14113668ca4bSJeremy L Thompson     // Move new composite label to operator
14123668ca4bSJeremy L Thompson     if (op->num_context_labels == 0) {
14132b730f8bSJeremy L Thompson       CeedCall(CeedCalloc(1, &op->context_labels));
14143668ca4bSJeremy L Thompson       op->max_context_labels = 1;
14153668ca4bSJeremy L Thompson     } else if (op->num_context_labels == op->max_context_labels) {
14162b730f8bSJeremy L Thompson       CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels));
14173668ca4bSJeremy L Thompson       op->max_context_labels *= 2;
14183668ca4bSJeremy L Thompson     }
14195ac9af79SJeremy L Thompson     op->context_labels[op->num_context_labels] = *field_label;
14203668ca4bSJeremy L Thompson     op->num_context_labels++;
14213668ca4bSJeremy L Thompson   }
14223668ca4bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14233668ca4bSJeremy L Thompson }
14243668ca4bSJeremy L Thompson 
14253668ca4bSJeremy L Thompson /**
14262788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding double precision values.
14274385fb7fSSebastian Grimberg 
14282788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1429d8dd9a91SJeremy L Thompson 
1430ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
14312788fa27SJeremy L Thompson   @param[in]     field_label Label of field to set
1432ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1433d8dd9a91SJeremy L Thompson 
1434d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1435d8dd9a91SJeremy L Thompson 
1436d8dd9a91SJeremy L Thompson   @ref User
1437d8dd9a91SJeremy L Thompson **/
143817b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) {
14392b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
1440d8dd9a91SJeremy L Thompson }
1441d8dd9a91SJeremy L Thompson 
1442d8dd9a91SJeremy L Thompson /**
14432788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding double precision values, read-only.
14444385fb7fSSebastian Grimberg 
14452788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14462788fa27SJeremy L Thompson 
14472788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14482788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
14492788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
14502788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14512788fa27SJeremy L Thompson 
14522788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14532788fa27SJeremy L Thompson 
14542788fa27SJeremy L Thompson   @ref User
14552788fa27SJeremy L Thompson **/
145617b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
14572788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values);
14582788fa27SJeremy L Thompson }
14592788fa27SJeremy L Thompson 
14602788fa27SJeremy L Thompson /**
14612788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding double precision values, read-only.
14622788fa27SJeremy L Thompson 
14632788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14642788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
14652788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
14662788fa27SJeremy L Thompson 
14672788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14682788fa27SJeremy L Thompson 
14692788fa27SJeremy L Thompson   @ref User
14702788fa27SJeremy L Thompson **/
147117b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) {
14722788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values);
14732788fa27SJeremy L Thompson }
14742788fa27SJeremy L Thompson 
14752788fa27SJeremy L Thompson /**
14762788fa27SJeremy L Thompson   @brief Set QFunctionContext field holding int32 values.
14774385fb7fSSebastian Grimberg 
14782788fa27SJeremy L Thompson   For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`.
1479d8dd9a91SJeremy L Thompson 
1480ea61e9acSJeremy L Thompson   @param[in,out] op          CeedOperator
1481ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
1482ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
1483d8dd9a91SJeremy L Thompson 
1484d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1485d8dd9a91SJeremy L Thompson 
1486d8dd9a91SJeremy L Thompson   @ref User
1487d8dd9a91SJeremy L Thompson **/
148817b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) {
14892b730f8bSJeremy L Thompson   return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
1490d8dd9a91SJeremy L Thompson }
1491d8dd9a91SJeremy L Thompson 
1492d8dd9a91SJeremy L Thompson /**
14932788fa27SJeremy L Thompson   @brief Get QFunctionContext field holding int32 values, read-only.
14944385fb7fSSebastian Grimberg 
14952788fa27SJeremy L Thompson   For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`.
14962788fa27SJeremy L Thompson 
14972788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
14982788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
1499c5d0f995SJed Brown   @param[out] num_values  Number of int32 values in `values`
15002788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15012788fa27SJeremy L Thompson 
15022788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15032788fa27SJeremy L Thompson 
15042788fa27SJeremy L Thompson   @ref User
15052788fa27SJeremy L Thompson **/
150617b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) {
15072788fa27SJeremy L Thompson   return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values);
15082788fa27SJeremy L Thompson }
15092788fa27SJeremy L Thompson 
15102788fa27SJeremy L Thompson /**
15112788fa27SJeremy L Thompson   @brief Restore QFunctionContext field holding int32 values, read-only.
15122788fa27SJeremy L Thompson 
15132788fa27SJeremy L Thompson   @param[in]  op          CeedOperator
15142788fa27SJeremy L Thompson   @param[in]  field_label Label of field to get
15152788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
15162788fa27SJeremy L Thompson 
15172788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15182788fa27SJeremy L Thompson 
15192788fa27SJeremy L Thompson   @ref User
15202788fa27SJeremy L Thompson **/
152117b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) {
15222788fa27SJeremy L Thompson   return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values);
15232788fa27SJeremy L Thompson }
15242788fa27SJeremy L Thompson 
15252788fa27SJeremy L Thompson /**
15263bd813ffSjeremylt   @brief Apply CeedOperator to a vector
1527d7b241e6Sjeremylt 
1528ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1529ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1530d7b241e6Sjeremylt 
1531ea61e9acSJeremy L Thompson   Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable.
1532f04ea552SJeremy L Thompson 
1533ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1534ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
15355ac9af79SJeremy L Thompson   @param[out] out     CeedVector to store result of applying operator (must be distinct from @a in) or @ref CEED_VECTOR_NONE if there are no
15365ac9af79SJeremy L Thompson active outputs
1537ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1538b11c1e72Sjeremylt 
1539b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1540dfdf5a53Sjeremylt 
15417a982d89SJeremy L. Thompson   @ref User
1542b11c1e72Sjeremylt **/
15432b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
15442b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1545d7b241e6Sjeremylt 
15463ca2b39bSJeremy L Thompson   if (op->is_composite) {
1547250756a7Sjeremylt     // Composite Operator
1548250756a7Sjeremylt     if (op->ApplyComposite) {
15492b730f8bSJeremy L Thompson       CeedCall(op->ApplyComposite(op, in, out, request));
1550250756a7Sjeremylt     } else {
1551d1d35e2fSjeremylt       CeedInt       num_suboperators;
1552d1d35e2fSjeremylt       CeedOperator *sub_operators;
15531c66c397SJeremy L Thompson 
15541c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
1555c6ebc35dSJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1556250756a7Sjeremylt 
1557250756a7Sjeremylt       // Zero all output vectors
15583ca2b39bSJeremy L Thompson       if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
1559d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
1560d1d35e2fSjeremylt         for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) {
1561d1d35e2fSjeremylt           CeedVector vec = sub_operators[i]->output_fields[j]->vec;
15621c66c397SJeremy L Thompson 
1563250756a7Sjeremylt           if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
15642b730f8bSJeremy L Thompson             CeedCall(CeedVectorSetValue(vec, 0.0));
1565250756a7Sjeremylt           }
1566250756a7Sjeremylt         }
1567250756a7Sjeremylt       }
1568250756a7Sjeremylt       // Apply
1569f754c177SSebastian Grimberg       for (CeedInt i = 0; i < num_suboperators; i++) {
1570f754c177SSebastian Grimberg         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
1571cae8b89aSjeremylt       }
1572cae8b89aSjeremylt     }
15733ca2b39bSJeremy L Thompson   } else {
15743ca2b39bSJeremy L Thompson     // Standard Operator
15753ca2b39bSJeremy L Thompson     if (op->Apply) {
15763ca2b39bSJeremy L Thompson       CeedCall(op->Apply(op, in, out, request));
15773ca2b39bSJeremy L Thompson     } else {
15783ca2b39bSJeremy L Thompson       // Zero all output vectors
15793ca2b39bSJeremy L Thompson       CeedQFunction qf = op->qf;
15801c66c397SJeremy L Thompson 
15813ca2b39bSJeremy L Thompson       for (CeedInt i = 0; i < qf->num_output_fields; i++) {
15823ca2b39bSJeremy L Thompson         CeedVector vec = op->output_fields[i]->vec;
15831c66c397SJeremy L Thompson 
15843ca2b39bSJeremy L Thompson         if (vec == CEED_VECTOR_ACTIVE) vec = out;
15853ca2b39bSJeremy L Thompson         if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
15863ca2b39bSJeremy L Thompson       }
15873ca2b39bSJeremy L Thompson       // Apply
15883ca2b39bSJeremy L Thompson       if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request));
15893ca2b39bSJeremy L Thompson     }
1590250756a7Sjeremylt   }
1591e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1592cae8b89aSjeremylt }
1593cae8b89aSjeremylt 
1594cae8b89aSjeremylt /**
1595cae8b89aSjeremylt   @brief Apply CeedOperator to a vector and add result to output vector
1596cae8b89aSjeremylt 
1597ea61e9acSJeremy L Thompson   This computes the action of the operator on the specified (active) input, yielding its (active) output.
1598ea61e9acSJeremy L Thompson   All inputs and outputs must be specified using CeedOperatorSetField().
1599cae8b89aSjeremylt 
1600ea61e9acSJeremy L Thompson   @param[in]  op      CeedOperator to apply
1601ea61e9acSJeremy L Thompson   @param[in]  in      CeedVector containing input state or NULL if there are no active inputs
1602ea61e9acSJeremy L Thompson   @param[out] out     CeedVector to sum in result of applying operator (must be distinct from @a in) or NULL if there are no active outputs
1603ea61e9acSJeremy L Thompson   @param[in]  request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
1604cae8b89aSjeremylt 
1605cae8b89aSjeremylt   @return An error code: 0 - success, otherwise - failure
1606cae8b89aSjeremylt 
16077a982d89SJeremy L. Thompson   @ref User
1608cae8b89aSjeremylt **/
16092b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
16102b730f8bSJeremy L Thompson   CeedCall(CeedOperatorCheckReady(op));
1611cae8b89aSjeremylt 
16123ca2b39bSJeremy L Thompson   if (op->is_composite) {
1613250756a7Sjeremylt     // Composite Operator
1614250756a7Sjeremylt     if (op->ApplyAddComposite) {
16152b730f8bSJeremy L Thompson       CeedCall(op->ApplyAddComposite(op, in, out, request));
1616cae8b89aSjeremylt     } else {
1617d1d35e2fSjeremylt       CeedInt       num_suboperators;
1618d1d35e2fSjeremylt       CeedOperator *sub_operators;
1619250756a7Sjeremylt 
16201c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
16211c66c397SJeremy L Thompson       CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
1622d1d35e2fSjeremylt       for (CeedInt i = 0; i < num_suboperators; i++) {
16232b730f8bSJeremy L Thompson         CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request));
16241d7d2407SJeremy L Thompson       }
1625250756a7Sjeremylt     }
16263ca2b39bSJeremy L Thompson   } else if (op->num_elem) {
16273ca2b39bSJeremy L Thompson     // Standard Operator
16283ca2b39bSJeremy L Thompson     CeedCall(op->ApplyAdd(op, in, out, request));
1629250756a7Sjeremylt   }
1630e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1631d7b241e6Sjeremylt }
1632d7b241e6Sjeremylt 
1633d7b241e6Sjeremylt /**
1634b11c1e72Sjeremylt   @brief Destroy a CeedOperator
1635d7b241e6Sjeremylt 
1636ea61e9acSJeremy L Thompson   @param[in,out] op CeedOperator to destroy
1637b11c1e72Sjeremylt 
1638b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1639dfdf5a53Sjeremylt 
16407a982d89SJeremy L. Thompson   @ref User
1641b11c1e72Sjeremylt **/
1642d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) {
1643ad6481ceSJeremy L Thompson   if (!*op || --(*op)->ref_count > 0) {
1644ad6481ceSJeremy L Thompson     *op = NULL;
1645ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1646ad6481ceSJeremy L Thompson   }
16472b730f8bSJeremy L Thompson   if ((*op)->Destroy) CeedCall((*op)->Destroy(*op));
16482b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*op)->ceed));
1649fe2413ffSjeremylt   // Free fields
16502b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1651d1d35e2fSjeremylt     if ((*op)->input_fields[i]) {
1652437c7c90SJeremy L Thompson       if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1653437c7c90SJeremy L Thompson         CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr));
165415910d16Sjeremylt       }
1655356036faSJeremy L Thompson       if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) {
16562b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis));
165771352a93Sjeremylt       }
16582b730f8bSJeremy L Thompson       if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) {
16592b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec));
166071352a93Sjeremylt       }
16612b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]->field_name));
16622b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->input_fields[i]));
1663fe2413ffSjeremylt     }
16642b730f8bSJeremy L Thompson   }
16652b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_fields; i++) {
1666d1d35e2fSjeremylt     if ((*op)->output_fields[i]) {
1667437c7c90SJeremy L Thompson       CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr));
1668356036faSJeremy L Thompson       if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) {
16692b730f8bSJeremy L Thompson         CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis));
167071352a93Sjeremylt       }
16712b730f8bSJeremy L Thompson       if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) {
16722b730f8bSJeremy L Thompson         CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec));
167371352a93Sjeremylt       }
16742b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]->field_name));
16752b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->output_fields[i]));
16762b730f8bSJeremy L Thompson     }
1677fe2413ffSjeremylt   }
1678d1d35e2fSjeremylt   // Destroy sub_operators
16792b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (*op)->num_suboperators; i++) {
1680d1d35e2fSjeremylt     if ((*op)->sub_operators[i]) {
16812b730f8bSJeremy L Thompson       CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i]));
168252d6035fSJeremy L Thompson     }
16832b730f8bSJeremy L Thompson   }
16842b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->qf));
16852b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqf));
16862b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionDestroy(&(*op)->dqfT));
16873668ca4bSJeremy L Thompson   // Destroy any composite labels
16885ac9af79SJeremy L Thompson   if ((*op)->is_composite) {
16893668ca4bSJeremy L Thompson     for (CeedInt i = 0; i < (*op)->num_context_labels; i++) {
16902b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels));
16912b730f8bSJeremy L Thompson       CeedCall(CeedFree(&(*op)->context_labels[i]));
16923668ca4bSJeremy L Thompson     }
16935ac9af79SJeremy L Thompson   }
16942b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->context_labels));
1695fe2413ffSjeremylt 
16965107b09fSJeremy L Thompson   // Destroy fallback
16972b730f8bSJeremy L Thompson   CeedCall(CeedOperatorDestroy(&(*op)->op_fallback));
16985107b09fSJeremy L Thompson 
1699ed9e99e6SJeremy L Thompson   // Destroy assembly data
17002b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled));
17012b730f8bSJeremy L Thompson   CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled));
170270a7ffb3SJeremy L Thompson 
17032b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->input_fields));
17042b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->output_fields));
17052b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->sub_operators));
17062b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*op)->name));
17072b730f8bSJeremy L Thompson   CeedCall(CeedFree(op));
1708e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1709d7b241e6Sjeremylt }
1710d7b241e6Sjeremylt 
1711d7b241e6Sjeremylt /// @}
1712