13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3d7b241e6Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5d7b241e6Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7d7b241e6Sjeremylt 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 113d576824SJeremy L Thompson #include <stdbool.h> 123d576824SJeremy L Thompson #include <stdio.h> 133d576824SJeremy L Thompson #include <string.h> 14d7b241e6Sjeremylt 15dfdf5a53Sjeremylt /// @file 167a982d89SJeremy L. Thompson /// Implementation of CeedOperator interfaces 177a982d89SJeremy L. Thompson 187a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 197a982d89SJeremy L. Thompson /// CeedOperator Library Internal Functions 207a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 217a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorDeveloper 227a982d89SJeremy L. Thompson /// @{ 237a982d89SJeremy L. Thompson 247a982d89SJeremy L. Thompson /** 25e15f9bd0SJeremy L Thompson @brief Check if a CeedOperator Field matches the QFunction Field 26e15f9bd0SJeremy L Thompson 27e15f9bd0SJeremy L Thompson @param[in] ceed Ceed object for error handling 28d1d35e2fSjeremylt @param[in] qf_field QFunction Field matching Operator Field 29e15f9bd0SJeremy L Thompson @param[in] r Operator Field ElemRestriction 30e15f9bd0SJeremy L Thompson @param[in] b Operator Field Basis 31e15f9bd0SJeremy L Thompson 32e15f9bd0SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 33e15f9bd0SJeremy L Thompson 34e15f9bd0SJeremy L Thompson @ref Developer 35e15f9bd0SJeremy L Thompson **/ 362b730f8bSJeremy L Thompson static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction r, CeedBasis b) { 37d1d35e2fSjeremylt CeedEvalMode eval_mode = qf_field->eval_mode; 38c4e3f59bSSebastian Grimberg CeedInt dim = 1, num_comp = 1, q_comp = 1, restr_num_comp = 1, size = qf_field->size; 392b730f8bSJeremy L Thompson 40e15f9bd0SJeremy L Thompson // Restriction 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) { 442b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp)); 45e1e9e29dSJed Brown } 46e15f9bd0SJeremy L Thompson // Basis 476574a04fSJeremy L Thompson CeedCheck((b == CEED_BASIS_COLLOCATED) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE, 486574a04fSJeremy L Thompson "CEED_BASIS_COLLOCATED and CEED_EVAL_NONE must be used together."); 49e15f9bd0SJeremy L Thompson if (b != CEED_BASIS_COLLOCATED) { 502b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(b, &dim)); 512b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(b, &num_comp)); 52c4e3f59bSSebastian Grimberg CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp)); 536574a04fSJeremy L Thompson CeedCheck(r == CEED_ELEMRESTRICTION_NONE || restr_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", 562b730f8bSJeremy L Thompson qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp); 57e15f9bd0SJeremy L Thompson } 58e15f9bd0SJeremy L Thompson // Field size 59d1d35e2fSjeremylt switch (eval_mode) { 60e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 616574a04fSJeremy L Thompson CeedCheck(size == restr_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, 632b730f8bSJeremy L Thompson qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_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); 1037a982d89SJeremy L. Thompson 104990fdeb6SJeremy L Thompson fprintf(stream, "%s Size: %" CeedInt_FMT "\n", pre, qf_field->size); 105f5ebfb90SJeremy L Thompson 1062b730f8bSJeremy L Thompson fprintf(stream, "%s EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]); 107f5ebfb90SJeremy L Thompson 1082b730f8bSJeremy L Thompson if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s Collocated basis\n", pre); 1097a982d89SJeremy L. Thompson 1102b730f8bSJeremy L Thompson if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s Active vector\n", pre); 1112b730f8bSJeremy L Thompson else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s No vector\n", pre); 112f5ebfb90SJeremy L Thompson 113e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1147a982d89SJeremy L. Thompson } 1157a982d89SJeremy L. Thompson 1167a982d89SJeremy L. Thompson /** 1177a982d89SJeremy L. Thompson @brief View a single CeedOperator 1187a982d89SJeremy L. Thompson 1197a982d89SJeremy L. Thompson @param[in] op CeedOperator to view 1207a982d89SJeremy L. Thompson @param[in] sub Boolean flag for sub-operator 1217a982d89SJeremy L. Thompson @param[in] stream Stream to write; typically stdout/stderr or a file 1227a982d89SJeremy L. Thompson 1237a982d89SJeremy L. Thompson @return Error code: 0 - success, otherwise - failure 1247a982d89SJeremy L. Thompson 1257a982d89SJeremy L. Thompson @ref Utility 1267a982d89SJeremy L. Thompson **/ 1277a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) { 1287a982d89SJeremy L. Thompson const char *pre = sub ? " " : ""; 1297a982d89SJeremy L. Thompson 130381e6593SJeremy L Thompson CeedInt num_elem, num_qpts; 1312b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1322b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 133381e6593SJeremy L Thompson 13478464608Sjeremylt CeedInt total_fields = 0; 1352b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumArgs(op, &total_fields)); 1362b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts); 1377a982d89SJeremy L. Thompson 1382b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : ""); 1397a982d89SJeremy L. Thompson 1402b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : ""); 141d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 1422b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream)); 1437a982d89SJeremy L. Thompson } 1447a982d89SJeremy L. Thompson 1452b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : ""); 146d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 1472b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream)); 1487a982d89SJeremy L. Thompson } 149e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1507a982d89SJeremy L. Thompson } 1517a982d89SJeremy L. Thompson 152d99fa3c5SJeremy L Thompson /** 1530f60e0a8SJeremy L Thompson @brief Find the active vector basis for a non-composite CeedOperator 154eaf62fffSJeremy L Thompson 155eaf62fffSJeremy L Thompson @param[in] op CeedOperator to find active basis for 1560f60e0a8SJeremy L Thompson @param[out] active_basis Basis for active input vector or NULL for composite operator 157eaf62fffSJeremy L Thompson 158eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 159eaf62fffSJeremy L Thompson 160eaf62fffSJeremy L Thompson @ref Developer 161eaf62fffSJeremy L Thompson **/ 162eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) { 1636aaed0edSJeremy L Thompson Ceed ceed; 1646aaed0edSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 1656aaed0edSJeremy L Thompson 166eaf62fffSJeremy L Thompson *active_basis = NULL; 1670f60e0a8SJeremy L Thompson if (op->is_composite) return CEED_ERROR_SUCCESS; 1682b730f8bSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 169eaf62fffSJeremy L Thompson if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1706574a04fSJeremy L Thompson CeedCheck(!*active_basis || *active_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR, "Multiple active CeedBases found"); 171eaf62fffSJeremy L Thompson *active_basis = op->input_fields[i]->basis; 172eaf62fffSJeremy L Thompson } 1732b730f8bSJeremy L Thompson } 174eaf62fffSJeremy L Thompson 1756574a04fSJeremy L Thompson CeedCheck(*active_basis, ceed, CEED_ERROR_INCOMPLETE, "No active CeedBasis found"); 176eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 177eaf62fffSJeremy L Thompson } 178eaf62fffSJeremy L Thompson 179eaf62fffSJeremy L Thompson /** 1800f60e0a8SJeremy L Thompson @brief Find the active vector ElemRestriction for a non-composite CeedOperator 181e2f04181SAndrew T. Barker 182e2f04181SAndrew T. Barker @param[in] op CeedOperator to find active basis for 1830f60e0a8SJeremy L Thompson @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator 184e2f04181SAndrew T. Barker 185e2f04181SAndrew T. Barker @return An error code: 0 - success, otherwise - failure 186e2f04181SAndrew T. Barker 187e2f04181SAndrew T. Barker @ref Utility 188e2f04181SAndrew T. Barker **/ 1892b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) { 1906aaed0edSJeremy L Thompson Ceed ceed; 1916aaed0edSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 1926aaed0edSJeremy L Thompson 193d1d35e2fSjeremylt *active_rstr = NULL; 1940f60e0a8SJeremy L Thompson if (op->is_composite) return CEED_ERROR_SUCCESS; 1952b730f8bSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 196d1d35e2fSjeremylt if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1976574a04fSJeremy L Thompson CeedCheck(!*active_rstr || *active_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR, 1986574a04fSJeremy L Thompson "Multiple active CeedElemRestrictions found"); 199437c7c90SJeremy L Thompson *active_rstr = op->input_fields[i]->elem_rstr; 200e2f04181SAndrew T. Barker } 2012b730f8bSJeremy L Thompson } 202e2f04181SAndrew T. Barker 2036574a04fSJeremy L Thompson CeedCheck(*active_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found"); 204e2f04181SAndrew T. Barker return CEED_ERROR_SUCCESS; 205e2f04181SAndrew T. Barker } 206e2f04181SAndrew T. Barker 207d8dd9a91SJeremy L Thompson /** 2082788fa27SJeremy L Thompson @brief Set QFunctionContext field values of the specified type. 2094385fb7fSSebastian Grimberg 210ea61e9acSJeremy L Thompson For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`. 2119fd66db6SSebastian 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 2129fd66db6SSebastian Grimberg any field of a matching type. 213d8dd9a91SJeremy L Thompson 214ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 215ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 216ea61e9acSJeremy L Thompson @param[in] field_type Type of field to set 2172788fa27SJeremy L Thompson @param[in] values Values to set 218d8dd9a91SJeremy L Thompson 219d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 220d8dd9a91SJeremy L Thompson 221d8dd9a91SJeremy L Thompson @ref User 222d8dd9a91SJeremy L Thompson **/ 2232788fa27SJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 2246574a04fSJeremy L Thompson CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 2253668ca4bSJeremy L Thompson 2265ac9af79SJeremy L Thompson // Check if field_label and op correspond 2275ac9af79SJeremy L Thompson if (field_label->from_op) { 2285ac9af79SJeremy L Thompson CeedInt index = -1; 2295ac9af79SJeremy L Thompson 2305ac9af79SJeremy L Thompson for (CeedInt i = 0; i < op->num_context_labels; i++) { 2315ac9af79SJeremy L Thompson if (op->context_labels[i] == field_label) index = i; 2325ac9af79SJeremy L Thompson } 2336574a04fSJeremy L Thompson CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 2345ac9af79SJeremy L Thompson } 2355ac9af79SJeremy L Thompson 2363668ca4bSJeremy L Thompson bool is_composite = false; 2372b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 238d8dd9a91SJeremy L Thompson if (is_composite) { 239d8dd9a91SJeremy L Thompson CeedInt num_sub; 240d8dd9a91SJeremy L Thompson CeedOperator *sub_operators; 241d8dd9a91SJeremy L Thompson 242c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 243c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 2446574a04fSJeremy L Thompson CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED, 2456574a04fSJeremy L Thompson "Composite operator modified after ContextFieldLabel created"); 246d8dd9a91SJeremy L Thompson 247d8dd9a91SJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 248d8dd9a91SJeremy L Thompson // Try every sub-operator, ok if some sub-operators do not have field 2493668ca4bSJeremy L Thompson if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 2502788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values)); 251d8dd9a91SJeremy L Thompson } 252d8dd9a91SJeremy L Thompson } 253d8dd9a91SJeremy L Thompson } else { 2546574a04fSJeremy L Thompson CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 2552788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values)); 2562788fa27SJeremy L Thompson } 2572788fa27SJeremy L Thompson 2582788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 2592788fa27SJeremy L Thompson } 2602788fa27SJeremy L Thompson 2612788fa27SJeremy L Thompson /** 2622788fa27SJeremy L Thompson @brief Get QFunctionContext field values of the specified type, read-only. 2634385fb7fSSebastian Grimberg 2642788fa27SJeremy L Thompson For composite operators, the values retrieved are for the first sub-operator QFunctionContext 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. 2672788fa27SJeremy L Thompson 2682788fa27SJeremy L Thompson @param[in,out] op CeedOperator 2692788fa27SJeremy L Thompson @param[in] field_label Label of field to set 2702788fa27SJeremy L Thompson @param[in] field_type Type of field to set 271c5d0f995SJed Brown @param[out] num_values Number of values of type `field_type` in array `values` 272c5d0f995SJed Brown @param[out] values Values in the label 2732788fa27SJeremy L Thompson 2742788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2752788fa27SJeremy L Thompson 2762788fa27SJeremy L Thompson @ref User 2772788fa27SJeremy L Thompson **/ 2782788fa27SJeremy L Thompson static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values, 2792788fa27SJeremy L Thompson void *values) { 2806574a04fSJeremy L Thompson CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 2812788fa27SJeremy L Thompson 2822788fa27SJeremy L Thompson *(void **)values = NULL; 2832788fa27SJeremy L Thompson *num_values = 0; 2842788fa27SJeremy L Thompson 2855ac9af79SJeremy L Thompson // Check if field_label and op correspond 2865ac9af79SJeremy L Thompson if (field_label->from_op) { 2875ac9af79SJeremy L Thompson CeedInt index = -1; 2885ac9af79SJeremy L Thompson 2895ac9af79SJeremy L Thompson for (CeedInt i = 0; i < op->num_context_labels; i++) { 2905ac9af79SJeremy L Thompson if (op->context_labels[i] == field_label) index = i; 2915ac9af79SJeremy L Thompson } 2926574a04fSJeremy L Thompson CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 2935ac9af79SJeremy L Thompson } 2945ac9af79SJeremy L Thompson 2952788fa27SJeremy L Thompson bool is_composite = false; 2962788fa27SJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 2972788fa27SJeremy L Thompson if (is_composite) { 2982788fa27SJeremy L Thompson CeedInt num_sub; 2992788fa27SJeremy L Thompson CeedOperator *sub_operators; 3002788fa27SJeremy L Thompson 3012788fa27SJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 3022788fa27SJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 3036574a04fSJeremy L Thompson CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED, 3046574a04fSJeremy L Thompson "Composite operator modified after ContextFieldLabel created"); 3052788fa27SJeremy L Thompson 3062788fa27SJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 3072788fa27SJeremy L Thompson // Try every sub-operator, ok if some sub-operators do not have field 3082788fa27SJeremy L Thompson if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 3092788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values)); 3102788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3112788fa27SJeremy L Thompson } 3122788fa27SJeremy L Thompson } 3132788fa27SJeremy L Thompson } else { 3146574a04fSJeremy L Thompson CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 3152788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values)); 3162788fa27SJeremy L Thompson } 3172788fa27SJeremy L Thompson 3182788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3192788fa27SJeremy L Thompson } 3202788fa27SJeremy L Thompson 3212788fa27SJeremy L Thompson /** 3222788fa27SJeremy L Thompson @brief Restore QFunctionContext field values of the specified type, read-only. 3234385fb7fSSebastian Grimberg 3242788fa27SJeremy L Thompson For composite operators, the values restored are for the first sub-operator QFunctionContext that have a matching `field_name`. 3259fd66db6SSebastian 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 3269fd66db6SSebastian Grimberg any field of a matching type. 3272788fa27SJeremy L Thompson 3282788fa27SJeremy L Thompson @param[in,out] op CeedOperator 3292788fa27SJeremy L Thompson @param[in] field_label Label of field to set 3302788fa27SJeremy L Thompson @param[in] field_type Type of field to set 331c5d0f995SJed Brown @param[in] values Values array to restore 3322788fa27SJeremy L Thompson 3332788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3342788fa27SJeremy L Thompson 3352788fa27SJeremy L Thompson @ref User 3362788fa27SJeremy L Thompson **/ 3372788fa27SJeremy L Thompson static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 3386574a04fSJeremy L Thompson CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 3392788fa27SJeremy L Thompson 3405ac9af79SJeremy L Thompson // Check if field_label and op correspond 3415ac9af79SJeremy L Thompson if (field_label->from_op) { 3425ac9af79SJeremy L Thompson CeedInt index = -1; 3435ac9af79SJeremy L Thompson 3445ac9af79SJeremy L Thompson for (CeedInt i = 0; i < op->num_context_labels; i++) { 3455ac9af79SJeremy L Thompson if (op->context_labels[i] == field_label) index = i; 3465ac9af79SJeremy L Thompson } 3476574a04fSJeremy L Thompson CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 3485ac9af79SJeremy L Thompson } 3495ac9af79SJeremy L Thompson 3502788fa27SJeremy L Thompson bool is_composite = false; 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(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, 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(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values)); 371d8dd9a91SJeremy L Thompson } 372d8dd9a91SJeremy L Thompson 373d8dd9a91SJeremy L Thompson return CEED_ERROR_SUCCESS; 374d8dd9a91SJeremy L Thompson } 375d8dd9a91SJeremy L Thompson 3767a982d89SJeremy L. Thompson /// @} 3777a982d89SJeremy L. Thompson 3787a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3797a982d89SJeremy L. Thompson /// CeedOperator Backend API 3807a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3817a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend 3827a982d89SJeremy L. Thompson /// @{ 3837a982d89SJeremy L. Thompson 3847a982d89SJeremy L. Thompson /** 3857a982d89SJeremy L. Thompson @brief Get the number of arguments associated with a CeedOperator 3867a982d89SJeremy L. Thompson 387ea61e9acSJeremy L Thompson @param[in] op CeedOperator 388d1d35e2fSjeremylt @param[out] num_args Variable to store vector number of arguments 3897a982d89SJeremy L. Thompson 3907a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3917a982d89SJeremy L. Thompson 3927a982d89SJeremy L. Thompson @ref Backend 3937a982d89SJeremy L. Thompson **/ 394d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) { 3956574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators"); 396d1d35e2fSjeremylt *num_args = op->num_fields; 397e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3987a982d89SJeremy L. Thompson } 3997a982d89SJeremy L. Thompson 4007a982d89SJeremy L. Thompson /** 4017a982d89SJeremy L. Thompson @brief Get the setup status of a CeedOperator 4027a982d89SJeremy L. Thompson 403ea61e9acSJeremy L Thompson @param[in] op CeedOperator 404d1d35e2fSjeremylt @param[out] is_setup_done Variable to store setup status 4057a982d89SJeremy L. Thompson 4067a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4077a982d89SJeremy L. Thompson 4087a982d89SJeremy L. Thompson @ref Backend 4097a982d89SJeremy L. Thompson **/ 410d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) { 411f04ea552SJeremy L Thompson *is_setup_done = op->is_backend_setup; 412e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4137a982d89SJeremy L. Thompson } 4147a982d89SJeremy L. Thompson 4157a982d89SJeremy L. Thompson /** 4167a982d89SJeremy L. Thompson @brief Get the QFunction associated with a CeedOperator 4177a982d89SJeremy L. Thompson 418ea61e9acSJeremy L Thompson @param[in] op CeedOperator 4197a982d89SJeremy L. Thompson @param[out] qf Variable to store QFunction 4207a982d89SJeremy L. Thompson 4217a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4227a982d89SJeremy L. Thompson 4237a982d89SJeremy L. Thompson @ref Backend 4247a982d89SJeremy L. Thompson **/ 4257a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) { 4266574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 4277a982d89SJeremy L. Thompson *qf = op->qf; 428e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4297a982d89SJeremy L. Thompson } 4307a982d89SJeremy L. Thompson 4317a982d89SJeremy L. Thompson /** 432c04a41a7SJeremy L Thompson @brief Get a boolean value indicating if the CeedOperator is composite 433c04a41a7SJeremy L Thompson 434ea61e9acSJeremy L Thompson @param[in] op CeedOperator 435d1d35e2fSjeremylt @param[out] is_composite Variable to store composite status 436c04a41a7SJeremy L Thompson 437c04a41a7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 438c04a41a7SJeremy L Thompson 439c04a41a7SJeremy L Thompson @ref Backend 440c04a41a7SJeremy L Thompson **/ 441d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) { 442f04ea552SJeremy L Thompson *is_composite = op->is_composite; 443e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 444c04a41a7SJeremy L Thompson } 445c04a41a7SJeremy L Thompson 446c04a41a7SJeremy L Thompson /** 4477a982d89SJeremy L. Thompson @brief Get the backend data of a CeedOperator 4487a982d89SJeremy L. Thompson 449ea61e9acSJeremy L Thompson @param[in] op CeedOperator 4507a982d89SJeremy L. Thompson @param[out] data Variable to store data 4517a982d89SJeremy L. Thompson 4527a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4537a982d89SJeremy L. Thompson 4547a982d89SJeremy L. Thompson @ref Backend 4557a982d89SJeremy L. Thompson **/ 456777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) { 457777ff853SJeremy L Thompson *(void **)data = op->data; 458e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4597a982d89SJeremy L. Thompson } 4607a982d89SJeremy L. Thompson 4617a982d89SJeremy L. Thompson /** 4627a982d89SJeremy L. Thompson @brief Set the backend data of a CeedOperator 4637a982d89SJeremy L. Thompson 464ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 465ea61e9acSJeremy L Thompson @param[in] data Data to set 4667a982d89SJeremy L. Thompson 4677a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4687a982d89SJeremy L. Thompson 4697a982d89SJeremy L. Thompson @ref Backend 4707a982d89SJeremy L. Thompson **/ 471777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) { 472777ff853SJeremy L Thompson op->data = data; 473e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4747a982d89SJeremy L. Thompson } 4757a982d89SJeremy L. Thompson 4767a982d89SJeremy L. Thompson /** 47734359f16Sjeremylt @brief Increment the reference counter for a CeedOperator 47834359f16Sjeremylt 479ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator to increment the reference counter 48034359f16Sjeremylt 48134359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 48234359f16Sjeremylt 48334359f16Sjeremylt @ref Backend 48434359f16Sjeremylt **/ 4859560d06aSjeremylt int CeedOperatorReference(CeedOperator op) { 48634359f16Sjeremylt op->ref_count++; 48734359f16Sjeremylt return CEED_ERROR_SUCCESS; 48834359f16Sjeremylt } 48934359f16Sjeremylt 49034359f16Sjeremylt /** 4917a982d89SJeremy L. Thompson @brief Set the setup flag of a CeedOperator to True 4927a982d89SJeremy L. Thompson 493ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 4947a982d89SJeremy L. Thompson 4957a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4967a982d89SJeremy L. Thompson 4977a982d89SJeremy L. Thompson @ref Backend 4987a982d89SJeremy L. Thompson **/ 4997a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) { 500f04ea552SJeremy L Thompson op->is_backend_setup = true; 501e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5027a982d89SJeremy L. Thompson } 5037a982d89SJeremy L. Thompson 5047a982d89SJeremy L. Thompson /// @} 5057a982d89SJeremy L. Thompson 5067a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5077a982d89SJeremy L. Thompson /// CeedOperator Public API 5087a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5097a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser 510dfdf5a53Sjeremylt /// @{ 511d7b241e6Sjeremylt 512d7b241e6Sjeremylt /** 513ea61e9acSJeremy L Thompson @brief Create a CeedOperator and associate a CeedQFunction. 5144385fb7fSSebastian Grimberg 515ea61e9acSJeremy L Thompson A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField. 516d7b241e6Sjeremylt 517ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedOperator will be created 518ea61e9acSJeremy L Thompson @param[in] qf QFunction defining the action of the operator at quadrature points 519ea61e9acSJeremy L Thompson @param[in] dqf QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 520ea61e9acSJeremy L Thompson @param[in] dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 521ea61e9acSJeremy L Thompson @param[out] op Address of the variable where the newly created CeedOperator will be stored 522b11c1e72Sjeremylt 523b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 524dfdf5a53Sjeremylt 5257a982d89SJeremy L. Thompson @ref User 526d7b241e6Sjeremylt */ 5272b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 5285fe0d4faSjeremylt if (!ceed->OperatorCreate) { 5295fe0d4faSjeremylt Ceed delegate; 5306574a04fSJeremy L Thompson 5312b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 5326574a04fSJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate"); 5332b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op)); 534e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5355fe0d4faSjeremylt } 5365fe0d4faSjeremylt 5376574a04fSJeremy L Thompson CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction."); 5382b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op)); 539d7b241e6Sjeremylt (*op)->ceed = ceed; 5402b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 541d1d35e2fSjeremylt (*op)->ref_count = 1; 542d7b241e6Sjeremylt (*op)->qf = qf; 5432b104005SJeremy L Thompson (*op)->input_size = -1; 5442b104005SJeremy L Thompson (*op)->output_size = -1; 5452b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(qf)); 546442e7f0bSjeremylt if (dqf && dqf != CEED_QFUNCTION_NONE) { 547d7b241e6Sjeremylt (*op)->dqf = dqf; 5482b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(dqf)); 549442e7f0bSjeremylt } 550442e7f0bSjeremylt if (dqfT && dqfT != CEED_QFUNCTION_NONE) { 551d7b241e6Sjeremylt (*op)->dqfT = dqfT; 5522b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(dqfT)); 553442e7f0bSjeremylt } 5542b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 5552b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 5562b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 5572b730f8bSJeremy L Thompson CeedCall(ceed->OperatorCreate(*op)); 558e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 559d7b241e6Sjeremylt } 560d7b241e6Sjeremylt 561d7b241e6Sjeremylt /** 56252d6035fSJeremy L Thompson @brief Create an operator that composes the action of several operators 56352d6035fSJeremy L Thompson 564ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedOperator will be created 565ea61e9acSJeremy L Thompson @param[out] op Address of the variable where the newly created Composite CeedOperator will be stored 56652d6035fSJeremy L Thompson 56752d6035fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 56852d6035fSJeremy L Thompson 5697a982d89SJeremy L. Thompson @ref User 57052d6035fSJeremy L Thompson */ 57152d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) { 57252d6035fSJeremy L Thompson if (!ceed->CompositeOperatorCreate) { 57352d6035fSJeremy L Thompson Ceed delegate; 5742b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 57552d6035fSJeremy L Thompson 576250756a7Sjeremylt if (delegate) { 5772b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(delegate, op)); 578e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 57952d6035fSJeremy L Thompson } 580250756a7Sjeremylt } 58152d6035fSJeremy L Thompson 5822b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op)); 58352d6035fSJeremy L Thompson (*op)->ceed = ceed; 5842b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 585996d9ab5SJed Brown (*op)->ref_count = 1; 586f04ea552SJeremy L Thompson (*op)->is_composite = true; 5872b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators)); 5882b104005SJeremy L Thompson (*op)->input_size = -1; 5892b104005SJeremy L Thompson (*op)->output_size = -1; 590250756a7Sjeremylt 591250756a7Sjeremylt if (ceed->CompositeOperatorCreate) { 5922b730f8bSJeremy L Thompson CeedCall(ceed->CompositeOperatorCreate(*op)); 593250756a7Sjeremylt } 594e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 59552d6035fSJeremy L Thompson } 59652d6035fSJeremy L Thompson 59752d6035fSJeremy L Thompson /** 598ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedOperator. 5994385fb7fSSebastian Grimberg 600ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedOperatorDestroy()`. 601512bb800SJeremy L Thompson 602512bb800SJeremy 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. 603512bb800SJeremy L Thompson This CeedOperator will be destroyed if `op_copy` is the only reference to this CeedOperator. 6049560d06aSjeremylt 605ea61e9acSJeremy L Thompson @param[in] op CeedOperator to copy reference to 606ea61e9acSJeremy L Thompson @param[in,out] op_copy Variable to store copied reference 6079560d06aSjeremylt 6089560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 6099560d06aSjeremylt 6109560d06aSjeremylt @ref User 6119560d06aSjeremylt **/ 6129560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) { 6132b730f8bSJeremy L Thompson CeedCall(CeedOperatorReference(op)); 6142b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(op_copy)); 6159560d06aSjeremylt *op_copy = op; 6169560d06aSjeremylt return CEED_ERROR_SUCCESS; 6179560d06aSjeremylt } 6189560d06aSjeremylt 6199560d06aSjeremylt /** 620ea61e9acSJeremy L Thompson @brief Provide a field to a CeedOperator for use by its CeedQFunction. 621d7b241e6Sjeremylt 622ea61e9acSJeremy L Thompson This function is used to specify both active and passive fields to a CeedOperator. 623ea61e9acSJeremy L Thompson For passive fields, a vector @arg v must be provided. 624ea61e9acSJeremy L Thompson Passive fields can inputs or outputs (updated in-place when operator is applied). 625d7b241e6Sjeremylt 626ea61e9acSJeremy L Thompson Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply(). 627ea61e9acSJeremy L Thompson There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply(). 628d7b241e6Sjeremylt 629ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator on which to provide the field 630ea61e9acSJeremy L Thompson @param[in] field_name Name of the field (to be matched with the name used by CeedQFunction) 631ea61e9acSJeremy L Thompson @param[in] r CeedElemRestriction 632ea61e9acSJeremy L Thompson @param[in] b CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points 6335ac9af79SJeremy 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 6345ac9af79SJeremy L Thompson if using @ref CEED_EVAL_WEIGHT in the QFunction 635b11c1e72Sjeremylt 636b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 637dfdf5a53Sjeremylt 6387a982d89SJeremy L. Thompson @ref User 639b11c1e72Sjeremylt **/ 6402b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) { 6416574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); 6426574a04fSJeremy L Thompson CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 6436574a04fSJeremy L Thompson CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name); 6446574a04fSJeremy L Thompson CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name); 6456574a04fSJeremy L Thompson CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name); 64652d6035fSJeremy L Thompson 6476574a04fSJeremy L Thompson CeedInt num_elem = 0; 6482b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem)); 6496574a04fSJeremy L Thompson CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION, 6502b730f8bSJeremy L Thompson "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem); 651d7b241e6Sjeremylt 65278464608Sjeremylt CeedInt num_qpts = 0; 6532b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts)); 6546574a04fSJeremy L Thompson CeedCheck(b == CEED_BASIS_COLLOCATED || !op->num_qpts || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION, 6552b730f8bSJeremy L Thompson "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts); 656d1d35e2fSjeremylt CeedQFunctionField qf_field; 657d1d35e2fSjeremylt CeedOperatorField *op_field; 6582b104005SJeremy L Thompson bool is_input = true; 659d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 660d1d35e2fSjeremylt if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) { 661d1d35e2fSjeremylt qf_field = op->qf->input_fields[i]; 662d1d35e2fSjeremylt op_field = &op->input_fields[i]; 663d7b241e6Sjeremylt goto found; 664d7b241e6Sjeremylt } 665d7b241e6Sjeremylt } 6662b104005SJeremy L Thompson is_input = false; 667d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 668d1d35e2fSjeremylt if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) { 669d1d35e2fSjeremylt qf_field = op->qf->output_fields[i]; 670d1d35e2fSjeremylt op_field = &op->output_fields[i]; 671d7b241e6Sjeremylt goto found; 672d7b241e6Sjeremylt } 673d7b241e6Sjeremylt } 674c042f62fSJeremy L Thompson // LCOV_EXCL_START 6752b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name); 676c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 677d7b241e6Sjeremylt found: 6782b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b)); 6792b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op_field)); 680e15f9bd0SJeremy L Thompson 6812b104005SJeremy L Thompson if (v == CEED_VECTOR_ACTIVE) { 6822b104005SJeremy L Thompson CeedSize l_size; 6832b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size)); 6842b104005SJeremy L Thompson if (is_input) { 6852b104005SJeremy L Thompson if (op->input_size == -1) op->input_size = l_size; 6866574a04fSJeremy L Thompson CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, 6876574a04fSJeremy L Thompson op->input_size); 6882b104005SJeremy L Thompson } else { 6892b104005SJeremy L Thompson if (op->output_size == -1) op->output_size = l_size; 6906574a04fSJeremy L Thompson CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, 6916574a04fSJeremy L Thompson op->output_size); 6922b104005SJeremy L Thompson } 6932b730f8bSJeremy L Thompson } 6942b104005SJeremy L Thompson 695393ac2cdSJeremy L Thompson CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec)); 696393ac2cdSJeremy L Thompson CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr)); 697e15f9bd0SJeremy L Thompson if (r != CEED_ELEMRESTRICTION_NONE) { 698d1d35e2fSjeremylt op->num_elem = num_elem; 699d1d35e2fSjeremylt op->has_restriction = true; // Restriction set, but num_elem may be 0 700e15f9bd0SJeremy L Thompson } 701393ac2cdSJeremy L Thompson CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis)); 702393ac2cdSJeremy L Thompson if (!op->num_qpts && b != CEED_BASIS_COLLOCATED) CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts)); 703e15f9bd0SJeremy L Thompson 704d1d35e2fSjeremylt op->num_fields += 1; 7052b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name)); 706e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 707d7b241e6Sjeremylt } 708d7b241e6Sjeremylt 709d7b241e6Sjeremylt /** 71043bbe138SJeremy L Thompson @brief Get the CeedOperatorFields of a CeedOperator 71143bbe138SJeremy L Thompson 712ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 713f04ea552SJeremy L Thompson 714ea61e9acSJeremy L Thompson @param[in] op CeedOperator 715f74ec584SJeremy L Thompson @param[out] num_input_fields Variable to store number of input fields 71643bbe138SJeremy L Thompson @param[out] input_fields Variable to store input_fields 717f74ec584SJeremy L Thompson @param[out] num_output_fields Variable to store number of output fields 71843bbe138SJeremy L Thompson @param[out] output_fields Variable to store output_fields 71943bbe138SJeremy L Thompson 72043bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 72143bbe138SJeremy L Thompson 722e9b533fbSJeremy L Thompson @ref Advanced 72343bbe138SJeremy L Thompson **/ 7242b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields, 72543bbe138SJeremy L Thompson CeedOperatorField **output_fields) { 7266574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 7272b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 72843bbe138SJeremy L Thompson 72943bbe138SJeremy L Thompson if (num_input_fields) *num_input_fields = op->qf->num_input_fields; 73043bbe138SJeremy L Thompson if (input_fields) *input_fields = op->input_fields; 73143bbe138SJeremy L Thompson if (num_output_fields) *num_output_fields = op->qf->num_output_fields; 73243bbe138SJeremy L Thompson if (output_fields) *output_fields = op->output_fields; 73343bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 73443bbe138SJeremy L Thompson } 73543bbe138SJeremy L Thompson 73643bbe138SJeremy L Thompson /** 737de5900adSJames Wright @brief Get a CeedOperatorField of an CeedOperator from its name 738de5900adSJames Wright 739de5900adSJames Wright Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 740de5900adSJames Wright 741de5900adSJames Wright @param[in] op CeedOperator 742de5900adSJames Wright @param[in] field_name Name of desired CeedOperatorField 743de5900adSJames Wright @param[out] op_field CeedOperatorField corresponding to the name 744de5900adSJames Wright 745de5900adSJames Wright @return An error code: 0 - success, otherwise - failure 746de5900adSJames Wright 747de5900adSJames Wright @ref Advanced 748de5900adSJames Wright **/ 749de5900adSJames Wright int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) { 750de5900adSJames Wright CeedInt num_input_fields, num_output_fields; 751de5900adSJames Wright CeedOperatorField *input_fields, *output_fields; 752de5900adSJames Wright char *name; 753de5900adSJames Wright CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 754de5900adSJames Wright 755de5900adSJames Wright for (CeedInt i = 0; i < num_input_fields; i++) { 756de5900adSJames Wright CeedCall(CeedOperatorFieldGetName(input_fields[i], &name)); 757de5900adSJames Wright if (!strcmp(name, field_name)) { 758de5900adSJames Wright *op_field = input_fields[i]; 759de5900adSJames Wright return CEED_ERROR_SUCCESS; 760de5900adSJames Wright } 761de5900adSJames Wright } 762de5900adSJames Wright 763de5900adSJames Wright for (CeedInt i = 0; i < num_output_fields; i++) { 764de5900adSJames Wright CeedCall(CeedOperatorFieldGetName(output_fields[i], &name)); 765de5900adSJames Wright if (!strcmp(name, field_name)) { 766de5900adSJames Wright *op_field = output_fields[i]; 767de5900adSJames Wright return CEED_ERROR_SUCCESS; 768de5900adSJames Wright } 769de5900adSJames Wright } 770de5900adSJames Wright 771de5900adSJames Wright bool has_name = op->name; 772de5900adSJames Wright // LCOV_EXCL_START 773de5900adSJames Wright return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "", 774de5900adSJames Wright has_name ? op->name : "", has_name ? "\"" : ""); 775de5900adSJames Wright // LCOV_EXCL_STOP 776de5900adSJames Wright } 777de5900adSJames Wright 778de5900adSJames Wright /** 77928567f8fSJeremy L Thompson @brief Get the name of a CeedOperatorField 78028567f8fSJeremy L Thompson 781ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 78228567f8fSJeremy L Thompson @param[out] field_name Variable to store the field name 78328567f8fSJeremy L Thompson 78428567f8fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 78528567f8fSJeremy L Thompson 786e9b533fbSJeremy L Thompson @ref Advanced 78728567f8fSJeremy L Thompson **/ 78828567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) { 78928567f8fSJeremy L Thompson *field_name = (char *)op_field->field_name; 79028567f8fSJeremy L Thompson return CEED_ERROR_SUCCESS; 79128567f8fSJeremy L Thompson } 79228567f8fSJeremy L Thompson 79328567f8fSJeremy L Thompson /** 79443bbe138SJeremy L Thompson @brief Get the CeedElemRestriction of a CeedOperatorField 79543bbe138SJeremy L Thompson 796ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 79743bbe138SJeremy L Thompson @param[out] rstr Variable to store CeedElemRestriction 79843bbe138SJeremy L Thompson 79943bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 80043bbe138SJeremy L Thompson 801e9b533fbSJeremy L Thompson @ref Advanced 80243bbe138SJeremy L Thompson **/ 8032b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) { 804437c7c90SJeremy L Thompson *rstr = op_field->elem_rstr; 80543bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 80643bbe138SJeremy L Thompson } 80743bbe138SJeremy L Thompson 80843bbe138SJeremy L Thompson /** 80943bbe138SJeremy L Thompson @brief Get the CeedBasis of a CeedOperatorField 81043bbe138SJeremy L Thompson 811ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 81243bbe138SJeremy L Thompson @param[out] basis Variable to store CeedBasis 81343bbe138SJeremy L Thompson 81443bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 81543bbe138SJeremy L Thompson 816e9b533fbSJeremy L Thompson @ref Advanced 81743bbe138SJeremy L Thompson **/ 81843bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) { 81943bbe138SJeremy L Thompson *basis = op_field->basis; 82043bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 82143bbe138SJeremy L Thompson } 82243bbe138SJeremy L Thompson 82343bbe138SJeremy L Thompson /** 82443bbe138SJeremy L Thompson @brief Get the CeedVector of a CeedOperatorField 82543bbe138SJeremy L Thompson 826ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 82743bbe138SJeremy L Thompson @param[out] vec Variable to store CeedVector 82843bbe138SJeremy L Thompson 82943bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 83043bbe138SJeremy L Thompson 831e9b533fbSJeremy L Thompson @ref Advanced 83243bbe138SJeremy L Thompson **/ 83343bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) { 83443bbe138SJeremy L Thompson *vec = op_field->vec; 83543bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 83643bbe138SJeremy L Thompson } 83743bbe138SJeremy L Thompson 83843bbe138SJeremy L Thompson /** 83952d6035fSJeremy L Thompson @brief Add a sub-operator to a composite CeedOperator 840288c0443SJeremy L Thompson 841ea61e9acSJeremy L Thompson @param[in,out] composite_op Composite CeedOperator 842ea61e9acSJeremy L Thompson @param[in] sub_op Sub-operator CeedOperator 84352d6035fSJeremy L Thompson 84452d6035fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 84552d6035fSJeremy L Thompson 8467a982d89SJeremy L. Thompson @ref User 84752d6035fSJeremy L Thompson */ 8482b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) { 8496574a04fSJeremy L Thompson CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); 8506574a04fSJeremy L Thompson CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators"); 8516574a04fSJeremy L Thompson CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 8522b730f8bSJeremy L Thompson 8532b730f8bSJeremy L Thompson { 8542b730f8bSJeremy L Thompson CeedSize input_size, output_size; 8552b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size)); 8562b730f8bSJeremy L Thompson if (composite_op->input_size == -1) composite_op->input_size = input_size; 8572b730f8bSJeremy L Thompson if (composite_op->output_size == -1) composite_op->output_size = output_size; 8582b730f8bSJeremy L Thompson // Note, a size of -1 means no active vector restriction set, so no incompatibility 8596574a04fSJeremy L Thompson CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), 8606574a04fSJeremy L Thompson composite_op->ceed, CEED_ERROR_MAJOR, 8612b730f8bSJeremy L Thompson "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 8622b730f8bSJeremy L Thompson "shape (%td, %td)", 8632b730f8bSJeremy L Thompson composite_op->input_size, composite_op->output_size, input_size, output_size); 8642b730f8bSJeremy L Thompson } 8652b730f8bSJeremy L Thompson 866d1d35e2fSjeremylt composite_op->sub_operators[composite_op->num_suboperators] = sub_op; 8672b730f8bSJeremy L Thompson CeedCall(CeedOperatorReference(sub_op)); 868d1d35e2fSjeremylt composite_op->num_suboperators++; 869e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 87052d6035fSJeremy L Thompson } 87152d6035fSJeremy L Thompson 87252d6035fSJeremy L Thompson /** 87375f0d5a4SJeremy L Thompson @brief Get the number of sub_operators associated with a CeedOperator 87475f0d5a4SJeremy L Thompson 87575f0d5a4SJeremy L Thompson @param[in] op CeedOperator 87675f0d5a4SJeremy L Thompson @param[out] num_suboperators Variable to store number of sub_operators 87775f0d5a4SJeremy L Thompson 87875f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 87975f0d5a4SJeremy L Thompson 88075f0d5a4SJeremy L Thompson @ref Backend 88175f0d5a4SJeremy L Thompson **/ 882c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) { 8836574a04fSJeremy L Thompson CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 88475f0d5a4SJeremy L Thompson *num_suboperators = op->num_suboperators; 88575f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 88675f0d5a4SJeremy L Thompson } 88775f0d5a4SJeremy L Thompson 88875f0d5a4SJeremy L Thompson /** 88975f0d5a4SJeremy L Thompson @brief Get the list of sub_operators associated with a CeedOperator 89075f0d5a4SJeremy L Thompson 89175f0d5a4SJeremy L Thompson @param op CeedOperator 89275f0d5a4SJeremy L Thompson @param[out] sub_operators Variable to store list of sub_operators 89375f0d5a4SJeremy L Thompson 89475f0d5a4SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 89575f0d5a4SJeremy L Thompson 89675f0d5a4SJeremy L Thompson @ref Backend 89775f0d5a4SJeremy L Thompson **/ 898c6ebc35dSJeremy L Thompson int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) { 8996574a04fSJeremy L Thompson CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 90075f0d5a4SJeremy L Thompson *sub_operators = op->sub_operators; 90175f0d5a4SJeremy L Thompson return CEED_ERROR_SUCCESS; 90275f0d5a4SJeremy L Thompson } 90375f0d5a4SJeremy L Thompson 90475f0d5a4SJeremy L Thompson /** 9054db537f9SJeremy L Thompson @brief Check if a CeedOperator is ready to be used. 9064db537f9SJeremy L Thompson 9074db537f9SJeremy L Thompson @param[in] op CeedOperator to check 9084db537f9SJeremy L Thompson 9094db537f9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9104db537f9SJeremy L Thompson 9114db537f9SJeremy L Thompson @ref User 9124db537f9SJeremy L Thompson **/ 9134db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) { 9144db537f9SJeremy L Thompson Ceed ceed; 9152b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 9164db537f9SJeremy L Thompson 9172b730f8bSJeremy L Thompson if (op->is_interface_setup) return CEED_ERROR_SUCCESS; 9184db537f9SJeremy L Thompson 9194db537f9SJeremy L Thompson CeedQFunction qf = op->qf; 9204db537f9SJeremy L Thompson if (op->is_composite) { 92143622462SJeremy L Thompson if (!op->num_suboperators) { 92243622462SJeremy L Thompson // Empty operator setup 92343622462SJeremy L Thompson op->input_size = 0; 92443622462SJeremy L Thompson op->output_size = 0; 92543622462SJeremy L Thompson } else { 9264db537f9SJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 9272b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op->sub_operators[i])); 9284db537f9SJeremy L Thompson } 9292b104005SJeremy L Thompson // Sub-operators could be modified after adding to composite operator 9302b104005SJeremy L Thompson // Need to verify no lvec incompatibility from any changes 9312b104005SJeremy L Thompson CeedSize input_size, output_size; 9322b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 93343622462SJeremy L Thompson } 9344db537f9SJeremy L Thompson } else { 9356574a04fSJeremy L Thompson CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set"); 9366574a04fSJeremy L Thompson CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set"); 9376574a04fSJeremy L Thompson CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required"); 9386574a04fSJeremy L Thompson CeedCheck(op->num_qpts > 0, ceed, CEED_ERROR_INCOMPLETE, 9396574a04fSJeremy L Thompson "At least one non-collocated basis is required or the number of quadrature points must be set"); 9402b730f8bSJeremy L Thompson } 9414db537f9SJeremy L Thompson 9424db537f9SJeremy L Thompson // Flag as immutable and ready 9434db537f9SJeremy L Thompson op->is_interface_setup = true; 9442b730f8bSJeremy L Thompson if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true; 9452b730f8bSJeremy L Thompson if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true; 9462b730f8bSJeremy L Thompson if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true; 9474db537f9SJeremy L Thompson return CEED_ERROR_SUCCESS; 9484db537f9SJeremy L Thompson } 9494db537f9SJeremy L Thompson 9504db537f9SJeremy L Thompson /** 951c9366a6bSJeremy L Thompson @brief Get vector lengths for the active input and/or output vectors of a CeedOperator. 9524385fb7fSSebastian Grimberg 953ea61e9acSJeremy L Thompson Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output. 954c9366a6bSJeremy L Thompson 955c9366a6bSJeremy L Thompson @param[in] op CeedOperator 956c9366a6bSJeremy L Thompson @param[out] input_size Variable to store active input vector length, or NULL 957c9366a6bSJeremy L Thompson @param[out] output_size Variable to store active output vector length, or NULL 958c9366a6bSJeremy L Thompson 959c9366a6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 960c9366a6bSJeremy L Thompson 961c9366a6bSJeremy L Thompson @ref User 962c9366a6bSJeremy L Thompson **/ 9632b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) { 964c9366a6bSJeremy L Thompson bool is_composite; 965c9366a6bSJeremy L Thompson 9662b104005SJeremy L Thompson if (input_size) *input_size = op->input_size; 9672b104005SJeremy L Thompson if (output_size) *output_size = op->output_size; 968c9366a6bSJeremy L Thompson 9692b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 9702b104005SJeremy L Thompson if (is_composite && (op->input_size == -1 || op->output_size == -1)) { 971c9366a6bSJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 972c9366a6bSJeremy L Thompson CeedSize sub_input_size, sub_output_size; 9732b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size)); 9742b104005SJeremy L Thompson if (op->input_size == -1) op->input_size = sub_input_size; 9752b104005SJeremy L Thompson if (op->output_size == -1) op->output_size = sub_output_size; 9762b104005SJeremy L Thompson // Note, a size of -1 means no active vector restriction set, so no incompatibility 9776574a04fSJeremy 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, 9786574a04fSJeremy L Thompson CEED_ERROR_MAJOR, 9792b730f8bSJeremy L Thompson "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 9802b730f8bSJeremy L Thompson "shape (%td, %td)", 9812b104005SJeremy L Thompson op->input_size, op->output_size, input_size, output_size); 982c9366a6bSJeremy L Thompson } 9832b730f8bSJeremy L Thompson } 984c9366a6bSJeremy L Thompson 985c9366a6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 986c9366a6bSJeremy L Thompson } 987c9366a6bSJeremy L Thompson 988c9366a6bSJeremy L Thompson /** 989beecbf24SJeremy L Thompson @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions. 9904385fb7fSSebastian Grimberg 991ea61e9acSJeremy L Thompson When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a 9929fd66db6SSebastian Grimberg `CeedOperatorLinearAssemble*` function is called. 9939fd66db6SSebastian Grimberg When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused between calls to 9949fd66db6SSebastian Grimberg `CeedOperatorSetQFunctionAssemblyDataUpdated`. 9958b919e6bSJeremy L Thompson 996beecbf24SJeremy L Thompson @param[in] op CeedOperator 997beecbf24SJeremy L Thompson @param[in] reuse_assembly_data Boolean flag setting assembly data reuse 9988b919e6bSJeremy L Thompson 9998b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 10008b919e6bSJeremy L Thompson 10018b919e6bSJeremy L Thompson @ref Advanced 10028b919e6bSJeremy L Thompson **/ 10032b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) { 10048b919e6bSJeremy L Thompson bool is_composite; 10058b919e6bSJeremy L Thompson 10062b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 10078b919e6bSJeremy L Thompson if (is_composite) { 10088b919e6bSJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 10092b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data)); 10108b919e6bSJeremy L Thompson } 10118b919e6bSJeremy L Thompson } else { 10122b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data)); 1013beecbf24SJeremy L Thompson } 1014beecbf24SJeremy L Thompson 1015beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1016beecbf24SJeremy L Thompson } 1017beecbf24SJeremy L Thompson 1018beecbf24SJeremy L Thompson /** 1019beecbf24SJeremy L Thompson @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly. 1020beecbf24SJeremy L Thompson 1021beecbf24SJeremy L Thompson @param[in] op CeedOperator 10226e15d496SJeremy L Thompson @param[in] needs_data_update Boolean flag setting assembly data reuse 1023beecbf24SJeremy L Thompson 1024beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1025beecbf24SJeremy L Thompson 1026beecbf24SJeremy L Thompson @ref Advanced 1027beecbf24SJeremy L Thompson **/ 10282b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) { 1029beecbf24SJeremy L Thompson bool is_composite; 1030beecbf24SJeremy L Thompson 10312b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1032beecbf24SJeremy L Thompson if (is_composite) { 1033beecbf24SJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 10342b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update)); 1035beecbf24SJeremy L Thompson } 1036beecbf24SJeremy L Thompson } else { 10372b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update)); 10388b919e6bSJeremy L Thompson } 10398b919e6bSJeremy L Thompson 10408b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 10418b919e6bSJeremy L Thompson } 10428b919e6bSJeremy L Thompson 10438b919e6bSJeremy L Thompson /** 1044cd4dfc48Sjeremylt @brief Set the number of quadrature points associated with a CeedOperator. 10454385fb7fSSebastian Grimberg 1046ea61e9acSJeremy L Thompson This should be used when creating a CeedOperator where every field has a collocated basis. 1047ea61e9acSJeremy L Thompson This function cannot be used for composite CeedOperators. 1048cd4dfc48Sjeremylt 1049ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1050ea61e9acSJeremy L Thompson @param[in] num_qpts Number of quadrature points to set 1051cd4dfc48Sjeremylt 1052cd4dfc48Sjeremylt @return An error code: 0 - success, otherwise - failure 1053cd4dfc48Sjeremylt 1054e9b533fbSJeremy L Thompson @ref Advanced 1055cd4dfc48Sjeremylt **/ 1056cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) { 10576574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 10586574a04fSJeremy L Thompson CeedCheck(op->num_qpts == 0, op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined"); 10596574a04fSJeremy L Thompson CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 1060cd4dfc48Sjeremylt op->num_qpts = num_qpts; 1061cd4dfc48Sjeremylt return CEED_ERROR_SUCCESS; 1062cd4dfc48Sjeremylt } 1063cd4dfc48Sjeremylt 1064cd4dfc48Sjeremylt /** 1065ea6b5821SJeremy L Thompson @brief Set name of CeedOperator for CeedOperatorView output 1066ea6b5821SJeremy L Thompson 1067ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1068ea61e9acSJeremy L Thompson @param[in] name Name to set, or NULL to remove previously set name 1069ea6b5821SJeremy L Thompson 1070ea6b5821SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1071ea6b5821SJeremy L Thompson 1072ea6b5821SJeremy L Thompson @ref User 1073ea6b5821SJeremy L Thompson **/ 1074ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) { 1075ea6b5821SJeremy L Thompson char *name_copy; 1076ea6b5821SJeremy L Thompson size_t name_len = name ? strlen(name) : 0; 1077ea6b5821SJeremy L Thompson 10782b730f8bSJeremy L Thompson CeedCall(CeedFree(&op->name)); 1079ea6b5821SJeremy L Thompson if (name_len > 0) { 10802b730f8bSJeremy L Thompson CeedCall(CeedCalloc(name_len + 1, &name_copy)); 1081ea6b5821SJeremy L Thompson memcpy(name_copy, name, name_len); 1082ea6b5821SJeremy L Thompson op->name = name_copy; 1083ea6b5821SJeremy L Thompson } 1084ea6b5821SJeremy L Thompson 1085ea6b5821SJeremy L Thompson return CEED_ERROR_SUCCESS; 1086ea6b5821SJeremy L Thompson } 1087ea6b5821SJeremy L Thompson 1088ea6b5821SJeremy L Thompson /** 10897a982d89SJeremy L. Thompson @brief View a CeedOperator 10907a982d89SJeremy L. Thompson 10917a982d89SJeremy L. Thompson @param[in] op CeedOperator to view 10927a982d89SJeremy L. Thompson @param[in] stream Stream to write; typically stdout/stderr or a file 10937a982d89SJeremy L. Thompson 10947a982d89SJeremy L. Thompson @return Error code: 0 - success, otherwise - failure 10957a982d89SJeremy L. Thompson 10967a982d89SJeremy L. Thompson @ref User 10977a982d89SJeremy L. Thompson **/ 10987a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) { 1099ea6b5821SJeremy L Thompson bool has_name = op->name; 11007a982d89SJeremy L. Thompson 1101f04ea552SJeremy L Thompson if (op->is_composite) { 11022b730f8bSJeremy L Thompson fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 11037a982d89SJeremy L. Thompson 1104d1d35e2fSjeremylt for (CeedInt i = 0; i < op->num_suboperators; i++) { 1105ea6b5821SJeremy L Thompson has_name = op->sub_operators[i]->name; 11062b730f8bSJeremy L Thompson fprintf(stream, " SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : ""); 11072b730f8bSJeremy L Thompson CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream)); 11087a982d89SJeremy L. Thompson } 11097a982d89SJeremy L. Thompson } else { 11102b730f8bSJeremy L Thompson fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 11112b730f8bSJeremy L Thompson CeedCall(CeedOperatorSingleView(op, 0, stream)); 11127a982d89SJeremy L. Thompson } 1113e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11147a982d89SJeremy L. Thompson } 11153bd813ffSjeremylt 11163bd813ffSjeremylt /** 1117b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedOperator 1118b7c9bbdaSJeremy L Thompson 1119ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1120b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1121b7c9bbdaSJeremy L Thompson 1122b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1123b7c9bbdaSJeremy L Thompson 1124b7c9bbdaSJeremy L Thompson @ref Advanced 1125b7c9bbdaSJeremy L Thompson **/ 1126b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) { 1127b7c9bbdaSJeremy L Thompson *ceed = op->ceed; 1128b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1129b7c9bbdaSJeremy L Thompson } 1130b7c9bbdaSJeremy L Thompson 1131b7c9bbdaSJeremy L Thompson /** 1132b7c9bbdaSJeremy L Thompson @brief Get the number of elements associated with a CeedOperator 1133b7c9bbdaSJeremy L Thompson 1134ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1135b7c9bbdaSJeremy L Thompson @param[out] num_elem Variable to store number of elements 1136b7c9bbdaSJeremy L Thompson 1137b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1138b7c9bbdaSJeremy L Thompson 1139b7c9bbdaSJeremy L Thompson @ref Advanced 1140b7c9bbdaSJeremy L Thompson **/ 1141b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) { 11426574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1143b7c9bbdaSJeremy L Thompson *num_elem = op->num_elem; 1144b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1145b7c9bbdaSJeremy L Thompson } 1146b7c9bbdaSJeremy L Thompson 1147b7c9bbdaSJeremy L Thompson /** 1148b7c9bbdaSJeremy L Thompson @brief Get the number of quadrature points associated with a CeedOperator 1149b7c9bbdaSJeremy L Thompson 1150ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1151b7c9bbdaSJeremy L Thompson @param[out] num_qpts Variable to store vector number of quadrature points 1152b7c9bbdaSJeremy L Thompson 1153b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1154b7c9bbdaSJeremy L Thompson 1155b7c9bbdaSJeremy L Thompson @ref Advanced 1156b7c9bbdaSJeremy L Thompson **/ 1157b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) { 11586574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1159b7c9bbdaSJeremy L Thompson *num_qpts = op->num_qpts; 1160b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1161b7c9bbdaSJeremy L Thompson } 1162b7c9bbdaSJeremy L Thompson 1163b7c9bbdaSJeremy L Thompson /** 11646e15d496SJeremy L Thompson @brief Estimate number of FLOPs required to apply CeedOperator on the active vector 11656e15d496SJeremy L Thompson 1166ea61e9acSJeremy L Thompson @param[in] op CeedOperator to estimate FLOPs for 1167ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 11686e15d496SJeremy L Thompson 11696e15d496SJeremy L Thompson @ref Backend 11706e15d496SJeremy L Thompson **/ 11719d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { 11726e15d496SJeremy L Thompson bool is_composite; 11732b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 11746e15d496SJeremy L Thompson 11756e15d496SJeremy L Thompson *flops = 0; 11762b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 11776e15d496SJeremy L Thompson if (is_composite) { 11786e15d496SJeremy L Thompson CeedInt num_suboperators; 1179c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 11806e15d496SJeremy L Thompson CeedOperator *sub_operators; 1181c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 11826e15d496SJeremy L Thompson 11836e15d496SJeremy L Thompson // FLOPs for each suboperator 11846e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 11859d36ca50SJeremy L Thompson CeedSize suboperator_flops; 11862b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops)); 11876e15d496SJeremy L Thompson *flops += suboperator_flops; 11886e15d496SJeremy L Thompson } 11896e15d496SJeremy L Thompson } else { 11906e15d496SJeremy L Thompson CeedInt num_input_fields, num_output_fields; 11916e15d496SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 11922b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 11936e15d496SJeremy L Thompson CeedInt num_elem = 0; 11942b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 11954385fb7fSSebastian Grimberg 11966e15d496SJeremy L Thompson // Input FLOPs 11976e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 11986e15d496SJeremy L Thompson if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 11999d36ca50SJeremy L Thompson CeedSize restr_flops, basis_flops; 12006e15d496SJeremy L Thompson 1201437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &restr_flops)); 12026e15d496SJeremy L Thompson *flops += restr_flops; 12032b730f8bSJeremy L Thompson CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops)); 12046e15d496SJeremy L Thompson *flops += basis_flops * num_elem; 12056e15d496SJeremy L Thompson } 12066e15d496SJeremy L Thompson } 12076e15d496SJeremy L Thompson // QF FLOPs 12089d36ca50SJeremy L Thompson CeedInt num_qpts; 12099d36ca50SJeremy L Thompson CeedSize qf_flops; 12102b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 12112b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops)); 12126e15d496SJeremy L Thompson *flops += num_elem * num_qpts * qf_flops; 12136e15d496SJeremy L Thompson // Output FLOPs 12146e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 12156e15d496SJeremy L Thompson if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 12169d36ca50SJeremy L Thompson CeedSize restr_flops, basis_flops; 12176e15d496SJeremy L Thompson 1218437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &restr_flops)); 12196e15d496SJeremy L Thompson *flops += restr_flops; 12202b730f8bSJeremy L Thompson CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops)); 12216e15d496SJeremy L Thompson *flops += basis_flops * num_elem; 12226e15d496SJeremy L Thompson } 12236e15d496SJeremy L Thompson } 12246e15d496SJeremy L Thompson } 12256e15d496SJeremy L Thompson 12266e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 12276e15d496SJeremy L Thompson } 12286e15d496SJeremy L Thompson 12296e15d496SJeremy L Thompson /** 12300126412dSJeremy L Thompson @brief Get CeedQFunction global context for a CeedOperator. 12319fd66db6SSebastian Grimberg 1232512bb800SJeremy L Thompson The caller is responsible for destroying `ctx` returned from this function via `CeedQFunctionContextDestroy()`. 1233512bb800SJeremy L Thompson 12349fd66db6SSebastian 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. 12359fd66db6SSebastian Grimberg This CeedQFunctionContext will be destroyed if `ctx` is the only reference to this CeedQFunctionContext. 12360126412dSJeremy L Thompson 1237859c15bbSJames Wright @param[in] op CeedOperator 12380126412dSJeremy L Thompson @param[out] ctx Variable to store CeedQFunctionContext 12390126412dSJeremy L Thompson 12400126412dSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 12410126412dSJeremy L Thompson 1242859c15bbSJames Wright @ref Advanced 12430126412dSJeremy L Thompson **/ 12440126412dSJeremy L Thompson int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) { 12456574a04fSJeremy L Thompson CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve QFunctionContext for composite operator"); 12460126412dSJeremy L Thompson if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx)); 12470126412dSJeremy L Thompson else *ctx = NULL; 12480126412dSJeremy L Thompson return CEED_ERROR_SUCCESS; 12490126412dSJeremy L Thompson } 12500126412dSJeremy L Thompson 12510126412dSJeremy L Thompson /** 1252ea61e9acSJeremy L Thompson @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`. 12533668ca4bSJeremy L Thompson 1254859c15bbSJames Wright Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. `CeedQFunctionContextRegisterDouble()`). 1255859c15bbSJames Wright 12563668ca4bSJeremy L Thompson @param[in] op CeedOperator 12573668ca4bSJeremy L Thompson @param[in] field_name Name of field to retrieve label 12583668ca4bSJeremy L Thompson @param[out] field_label Variable to field label 12593668ca4bSJeremy L Thompson 12603668ca4bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 12613668ca4bSJeremy L Thompson 12623668ca4bSJeremy L Thompson @ref User 12633668ca4bSJeremy L Thompson **/ 126417b0d5c6SJeremy L Thompson int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) { 12653668ca4bSJeremy L Thompson bool is_composite; 12662b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 12672b730f8bSJeremy L Thompson 12683668ca4bSJeremy L Thompson if (is_composite) { 12693668ca4bSJeremy L Thompson // Check if composite label already created 12703668ca4bSJeremy L Thompson for (CeedInt i = 0; i < op->num_context_labels; i++) { 12713668ca4bSJeremy L Thompson if (!strcmp(op->context_labels[i]->name, field_name)) { 12723668ca4bSJeremy L Thompson *field_label = op->context_labels[i]; 12733668ca4bSJeremy L Thompson return CEED_ERROR_SUCCESS; 12743668ca4bSJeremy L Thompson } 12753668ca4bSJeremy L Thompson } 12763668ca4bSJeremy L Thompson 12773668ca4bSJeremy L Thompson // Create composite label if needed 12783668ca4bSJeremy L Thompson CeedInt num_sub; 12793668ca4bSJeremy L Thompson CeedOperator *sub_operators; 12803668ca4bSJeremy L Thompson CeedContextFieldLabel new_field_label; 12813668ca4bSJeremy L Thompson 12822b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &new_field_label)); 1283c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 1284c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 12852b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels)); 12863668ca4bSJeremy L Thompson new_field_label->num_sub_labels = num_sub; 12873668ca4bSJeremy L Thompson 12883668ca4bSJeremy L Thompson bool label_found = false; 12893668ca4bSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 12903668ca4bSJeremy L Thompson if (sub_operators[i]->qf->ctx) { 12913668ca4bSJeremy L Thompson CeedContextFieldLabel new_field_label_i; 12922b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i)); 12933668ca4bSJeremy L Thompson if (new_field_label_i) { 12943668ca4bSJeremy L Thompson label_found = true; 12953668ca4bSJeremy L Thompson new_field_label->sub_labels[i] = new_field_label_i; 12963668ca4bSJeremy L Thompson new_field_label->name = new_field_label_i->name; 12973668ca4bSJeremy L Thompson new_field_label->description = new_field_label_i->description; 12982b730f8bSJeremy L Thompson if (new_field_label->type && new_field_label->type != new_field_label_i->type) { 12997bfe0f0eSJeremy L Thompson // LCOV_EXCL_START 13002b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label)); 13012b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s", 13022b730f8bSJeremy L Thompson CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]); 13037bfe0f0eSJeremy L Thompson // LCOV_EXCL_STOP 13047bfe0f0eSJeremy L Thompson } else { 13057bfe0f0eSJeremy L Thompson new_field_label->type = new_field_label_i->type; 13067bfe0f0eSJeremy L Thompson } 13072b730f8bSJeremy L Thompson if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) { 13087bfe0f0eSJeremy L Thompson // LCOV_EXCL_START 13092b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label)); 13102b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld", 13117bfe0f0eSJeremy L Thompson new_field_label->num_values, new_field_label_i->num_values); 13127bfe0f0eSJeremy L Thompson // LCOV_EXCL_STOP 13137bfe0f0eSJeremy L Thompson } else { 13147bfe0f0eSJeremy L Thompson new_field_label->num_values = new_field_label_i->num_values; 13157bfe0f0eSJeremy L Thompson } 13163668ca4bSJeremy L Thompson } 13173668ca4bSJeremy L Thompson } 13183668ca4bSJeremy L Thompson } 13193668ca4bSJeremy L Thompson if (!label_found) { 13203668ca4bSJeremy L Thompson // LCOV_EXCL_START 13212b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label->sub_labels)); 13222b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label)); 13233668ca4bSJeremy L Thompson *field_label = NULL; 13243668ca4bSJeremy L Thompson // LCOV_EXCL_STOP 13253668ca4bSJeremy L Thompson } else { 13265ac9af79SJeremy L Thompson *field_label = new_field_label; 13275ac9af79SJeremy L Thompson } 13285ac9af79SJeremy L Thompson } else { 13295ac9af79SJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label)); 13305ac9af79SJeremy L Thompson } 13315ac9af79SJeremy L Thompson 13325ac9af79SJeremy L Thompson // Set label in operator 13335ac9af79SJeremy L Thompson if (*field_label) { 13345ac9af79SJeremy L Thompson (*field_label)->from_op = true; 13355ac9af79SJeremy L Thompson 13363668ca4bSJeremy L Thompson // Move new composite label to operator 13373668ca4bSJeremy L Thompson if (op->num_context_labels == 0) { 13382b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &op->context_labels)); 13393668ca4bSJeremy L Thompson op->max_context_labels = 1; 13403668ca4bSJeremy L Thompson } else if (op->num_context_labels == op->max_context_labels) { 13412b730f8bSJeremy L Thompson CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels)); 13423668ca4bSJeremy L Thompson op->max_context_labels *= 2; 13433668ca4bSJeremy L Thompson } 13445ac9af79SJeremy L Thompson op->context_labels[op->num_context_labels] = *field_label; 13453668ca4bSJeremy L Thompson op->num_context_labels++; 13463668ca4bSJeremy L Thompson } 13473668ca4bSJeremy L Thompson 13483668ca4bSJeremy L Thompson return CEED_ERROR_SUCCESS; 13493668ca4bSJeremy L Thompson } 13503668ca4bSJeremy L Thompson 13513668ca4bSJeremy L Thompson /** 13522788fa27SJeremy L Thompson @brief Set QFunctionContext field holding double precision values. 13534385fb7fSSebastian Grimberg 13542788fa27SJeremy L Thompson For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`. 1355d8dd9a91SJeremy L Thompson 1356ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 13572788fa27SJeremy L Thompson @param[in] field_label Label of field to set 1358ea61e9acSJeremy L Thompson @param[in] values Values to set 1359d8dd9a91SJeremy L Thompson 1360d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1361d8dd9a91SJeremy L Thompson 1362d8dd9a91SJeremy L Thompson @ref User 1363d8dd9a91SJeremy L Thompson **/ 136417b0d5c6SJeremy L Thompson int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) { 13652b730f8bSJeremy L Thompson return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1366d8dd9a91SJeremy L Thompson } 1367d8dd9a91SJeremy L Thompson 1368d8dd9a91SJeremy L Thompson /** 13692788fa27SJeremy L Thompson @brief Get QFunctionContext field holding double precision values, read-only. 13704385fb7fSSebastian Grimberg 13712788fa27SJeremy L Thompson For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`. 13722788fa27SJeremy L Thompson 13732788fa27SJeremy L Thompson @param[in] op CeedOperator 13742788fa27SJeremy L Thompson @param[in] field_label Label of field to get 13752788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 13762788fa27SJeremy L Thompson @param[out] values Pointer to context values 13772788fa27SJeremy L Thompson 13782788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 13792788fa27SJeremy L Thompson 13802788fa27SJeremy L Thompson @ref User 13812788fa27SJeremy L Thompson **/ 138217b0d5c6SJeremy L Thompson int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) { 13832788fa27SJeremy L Thompson return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values); 13842788fa27SJeremy L Thompson } 13852788fa27SJeremy L Thompson 13862788fa27SJeremy L Thompson /** 13872788fa27SJeremy L Thompson @brief Restore QFunctionContext field holding double precision values, read-only. 13882788fa27SJeremy L Thompson 13892788fa27SJeremy L Thompson @param[in] op CeedOperator 13902788fa27SJeremy L Thompson @param[in] field_label Label of field to restore 13912788fa27SJeremy L Thompson @param[out] values Pointer to context values 13922788fa27SJeremy L Thompson 13932788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 13942788fa27SJeremy L Thompson 13952788fa27SJeremy L Thompson @ref User 13962788fa27SJeremy L Thompson **/ 139717b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) { 13982788fa27SJeremy L Thompson return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 13992788fa27SJeremy L Thompson } 14002788fa27SJeremy L Thompson 14012788fa27SJeremy L Thompson /** 14022788fa27SJeremy L Thompson @brief Set QFunctionContext field holding int32 values. 14034385fb7fSSebastian Grimberg 14042788fa27SJeremy L Thompson For composite operators, the values are set in all sub-operator QFunctionContexts that have a matching `field_name`. 1405d8dd9a91SJeremy L Thompson 1406ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1407ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 1408ea61e9acSJeremy L Thompson @param[in] values Values to set 1409d8dd9a91SJeremy L Thompson 1410d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1411d8dd9a91SJeremy L Thompson 1412d8dd9a91SJeremy L Thompson @ref User 1413d8dd9a91SJeremy L Thompson **/ 141417b0d5c6SJeremy L Thompson int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) { 14152b730f8bSJeremy L Thompson return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1416d8dd9a91SJeremy L Thompson } 1417d8dd9a91SJeremy L Thompson 1418d8dd9a91SJeremy L Thompson /** 14192788fa27SJeremy L Thompson @brief Get QFunctionContext field holding int32 values, read-only. 14204385fb7fSSebastian Grimberg 14212788fa27SJeremy L Thompson For composite operators, the values correspond to the first sub-operator QFunctionContexts that has a matching `field_name`. 14222788fa27SJeremy L Thompson 14232788fa27SJeremy L Thompson @param[in] op CeedOperator 14242788fa27SJeremy L Thompson @param[in] field_label Label of field to get 1425c5d0f995SJed Brown @param[out] num_values Number of int32 values in `values` 14262788fa27SJeremy L Thompson @param[out] values Pointer to context values 14272788fa27SJeremy L Thompson 14282788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 14292788fa27SJeremy L Thompson 14302788fa27SJeremy L Thompson @ref User 14312788fa27SJeremy L Thompson **/ 143217b0d5c6SJeremy L Thompson int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int **values) { 14332788fa27SJeremy L Thompson return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values); 14342788fa27SJeremy L Thompson } 14352788fa27SJeremy L Thompson 14362788fa27SJeremy L Thompson /** 14372788fa27SJeremy L Thompson @brief Restore QFunctionContext field holding int32 values, read-only. 14382788fa27SJeremy L Thompson 14392788fa27SJeremy L Thompson @param[in] op CeedOperator 14402788fa27SJeremy L Thompson @param[in] field_label Label of field to get 14412788fa27SJeremy L Thompson @param[out] values Pointer to context values 14422788fa27SJeremy L Thompson 14432788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 14442788fa27SJeremy L Thompson 14452788fa27SJeremy L Thompson @ref User 14462788fa27SJeremy L Thompson **/ 144717b0d5c6SJeremy L Thompson int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int **values) { 14482788fa27SJeremy L Thompson return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 14492788fa27SJeremy L Thompson } 14502788fa27SJeremy L Thompson 14512788fa27SJeremy L Thompson /** 14523bd813ffSjeremylt @brief Apply CeedOperator to a vector 1453d7b241e6Sjeremylt 1454ea61e9acSJeremy L Thompson This computes the action of the operator on the specified (active) input, yielding its (active) output. 1455ea61e9acSJeremy L Thompson All inputs and outputs must be specified using CeedOperatorSetField(). 1456d7b241e6Sjeremylt 1457ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1458f04ea552SJeremy L Thompson 1459ea61e9acSJeremy L Thompson @param[in] op CeedOperator to apply 1460ea61e9acSJeremy L Thompson @param[in] in CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 14615ac9af79SJeremy 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 14625ac9af79SJeremy L Thompson active outputs 1463ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1464b11c1e72Sjeremylt 1465b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1466dfdf5a53Sjeremylt 14677a982d89SJeremy L. Thompson @ref User 1468b11c1e72Sjeremylt **/ 14692b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 14702b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1471d7b241e6Sjeremylt 1472*3ca2b39bSJeremy L Thompson if (op->is_composite) { 1473250756a7Sjeremylt // Composite Operator 1474250756a7Sjeremylt if (op->ApplyComposite) { 14752b730f8bSJeremy L Thompson CeedCall(op->ApplyComposite(op, in, out, request)); 1476250756a7Sjeremylt } else { 1477d1d35e2fSjeremylt CeedInt num_suboperators; 1478c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1479d1d35e2fSjeremylt CeedOperator *sub_operators; 1480c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1481250756a7Sjeremylt 1482250756a7Sjeremylt // Zero all output vectors 1483*3ca2b39bSJeremy L Thompson if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0)); 1484d1d35e2fSjeremylt for (CeedInt i = 0; i < num_suboperators; i++) { 1485d1d35e2fSjeremylt for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) { 1486d1d35e2fSjeremylt CeedVector vec = sub_operators[i]->output_fields[j]->vec; 1487250756a7Sjeremylt if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) { 14882b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(vec, 0.0)); 1489250756a7Sjeremylt } 1490250756a7Sjeremylt } 1491250756a7Sjeremylt } 1492250756a7Sjeremylt // Apply 1493d1d35e2fSjeremylt for (CeedInt i = 0; i < op->num_suboperators; i++) { 14942b730f8bSJeremy L Thompson CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request)); 1495cae8b89aSjeremylt } 1496cae8b89aSjeremylt } 1497*3ca2b39bSJeremy L Thompson } else { 1498*3ca2b39bSJeremy L Thompson // Standard Operator 1499*3ca2b39bSJeremy L Thompson if (op->Apply) { 1500*3ca2b39bSJeremy L Thompson CeedCall(op->Apply(op, in, out, request)); 1501*3ca2b39bSJeremy L Thompson } else { 1502*3ca2b39bSJeremy L Thompson // Zero all output vectors 1503*3ca2b39bSJeremy L Thompson CeedQFunction qf = op->qf; 1504*3ca2b39bSJeremy L Thompson for (CeedInt i = 0; i < qf->num_output_fields; i++) { 1505*3ca2b39bSJeremy L Thompson CeedVector vec = op->output_fields[i]->vec; 1506*3ca2b39bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out; 1507*3ca2b39bSJeremy L Thompson if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0)); 1508*3ca2b39bSJeremy L Thompson } 1509*3ca2b39bSJeremy L Thompson // Apply 1510*3ca2b39bSJeremy L Thompson if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request)); 1511*3ca2b39bSJeremy L Thompson } 1512250756a7Sjeremylt } 1513e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1514cae8b89aSjeremylt } 1515cae8b89aSjeremylt 1516cae8b89aSjeremylt /** 1517cae8b89aSjeremylt @brief Apply CeedOperator to a vector and add result to output vector 1518cae8b89aSjeremylt 1519ea61e9acSJeremy L Thompson This computes the action of the operator on the specified (active) input, yielding its (active) output. 1520ea61e9acSJeremy L Thompson All inputs and outputs must be specified using CeedOperatorSetField(). 1521cae8b89aSjeremylt 1522ea61e9acSJeremy L Thompson @param[in] op CeedOperator to apply 1523ea61e9acSJeremy L Thompson @param[in] in CeedVector containing input state or NULL if there are no active inputs 1524ea61e9acSJeremy 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 1525ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1526cae8b89aSjeremylt 1527cae8b89aSjeremylt @return An error code: 0 - success, otherwise - failure 1528cae8b89aSjeremylt 15297a982d89SJeremy L. Thompson @ref User 1530cae8b89aSjeremylt **/ 15312b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 15322b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1533cae8b89aSjeremylt 1534*3ca2b39bSJeremy L Thompson if (op->is_composite) { 1535250756a7Sjeremylt // Composite Operator 1536250756a7Sjeremylt if (op->ApplyAddComposite) { 15372b730f8bSJeremy L Thompson CeedCall(op->ApplyAddComposite(op, in, out, request)); 1538cae8b89aSjeremylt } else { 1539d1d35e2fSjeremylt CeedInt num_suboperators; 1540c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1541d1d35e2fSjeremylt CeedOperator *sub_operators; 1542c6ebc35dSJeremy L Thompson CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1543250756a7Sjeremylt 1544d1d35e2fSjeremylt for (CeedInt i = 0; i < num_suboperators; i++) { 15452b730f8bSJeremy L Thompson CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 15461d7d2407SJeremy L Thompson } 1547250756a7Sjeremylt } 1548*3ca2b39bSJeremy L Thompson } else if (op->num_elem) { 1549*3ca2b39bSJeremy L Thompson // Standard Operator 1550*3ca2b39bSJeremy L Thompson CeedCall(op->ApplyAdd(op, in, out, request)); 1551250756a7Sjeremylt } 1552e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1553d7b241e6Sjeremylt } 1554d7b241e6Sjeremylt 1555d7b241e6Sjeremylt /** 1556b11c1e72Sjeremylt @brief Destroy a CeedOperator 1557d7b241e6Sjeremylt 1558ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator to destroy 1559b11c1e72Sjeremylt 1560b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1561dfdf5a53Sjeremylt 15627a982d89SJeremy L. Thompson @ref User 1563b11c1e72Sjeremylt **/ 1564d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) { 1565ad6481ceSJeremy L Thompson if (!*op || --(*op)->ref_count > 0) { 1566ad6481ceSJeremy L Thompson *op = NULL; 1567ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1568ad6481ceSJeremy L Thompson } 15692b730f8bSJeremy L Thompson if ((*op)->Destroy) CeedCall((*op)->Destroy(*op)); 15702b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*op)->ceed)); 1571fe2413ffSjeremylt // Free fields 15722b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1573d1d35e2fSjeremylt if ((*op)->input_fields[i]) { 1574437c7c90SJeremy L Thompson if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) { 1575437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr)); 157615910d16Sjeremylt } 1577d1d35e2fSjeremylt if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) { 15782b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis)); 157971352a93Sjeremylt } 15802b730f8bSJeremy L Thompson if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) { 15812b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec)); 158271352a93Sjeremylt } 15832b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields[i]->field_name)); 15842b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields[i])); 1585fe2413ffSjeremylt } 15862b730f8bSJeremy L Thompson } 15872b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1588d1d35e2fSjeremylt if ((*op)->output_fields[i]) { 1589437c7c90SJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr)); 1590d1d35e2fSjeremylt if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) { 15912b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis)); 159271352a93Sjeremylt } 15932b730f8bSJeremy L Thompson if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) { 15942b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec)); 159571352a93Sjeremylt } 15962b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields[i]->field_name)); 15972b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields[i])); 15982b730f8bSJeremy L Thompson } 1599fe2413ffSjeremylt } 1600d1d35e2fSjeremylt // Destroy sub_operators 16012b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_suboperators; i++) { 1602d1d35e2fSjeremylt if ((*op)->sub_operators[i]) { 16032b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i])); 160452d6035fSJeremy L Thompson } 16052b730f8bSJeremy L Thompson } 16062b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->qf)); 16072b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->dqf)); 16082b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->dqfT)); 16093668ca4bSJeremy L Thompson // Destroy any composite labels 16105ac9af79SJeremy L Thompson if ((*op)->is_composite) { 16113668ca4bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_context_labels; i++) { 16122b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels)); 16132b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels[i])); 16143668ca4bSJeremy L Thompson } 16155ac9af79SJeremy L Thompson } 16162b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels)); 1617fe2413ffSjeremylt 16185107b09fSJeremy L Thompson // Destroy fallback 16192b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(&(*op)->op_fallback)); 16205107b09fSJeremy L Thompson 1621ed9e99e6SJeremy L Thompson // Destroy assembly data 16222b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled)); 16232b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled)); 162470a7ffb3SJeremy L Thompson 16252b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields)); 16262b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields)); 16272b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->sub_operators)); 16282b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->name)); 16292b730f8bSJeremy L Thompson CeedCall(CeedFree(op)); 1630e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1631d7b241e6Sjeremylt } 1632d7b241e6Sjeremylt 1633d7b241e6Sjeremylt /// @} 1634