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> 92b730f8bSJeremy L Thompson #include <ceed/backend.h> 102b730f8bSJeremy L Thompson #include <ceed/ceed.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; 382b730f8bSJeremy L Thompson CeedInt dim = 1, num_comp = 1, Q_comp = 1, restr_num_comp = 1, size = qf_field->size; 392b730f8bSJeremy L Thompson 40e15f9bd0SJeremy L Thompson // Restriction 41e15f9bd0SJeremy L Thompson if (r != CEED_ELEMRESTRICTION_NONE) { 42d1d35e2fSjeremylt if (eval_mode == CEED_EVAL_WEIGHT) { 43e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 442b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE should be used for a field with eval mode CEED_EVAL_WEIGHT"); 45e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 46e15f9bd0SJeremy L Thompson } 472b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumComponents(r, &restr_num_comp)); 48e1e9e29dSJed Brown } 49d1d35e2fSjeremylt if ((r == CEED_ELEMRESTRICTION_NONE) != (eval_mode == CEED_EVAL_WEIGHT)) { 50e1e9e29dSJed Brown // LCOV_EXCL_START 512b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together."); 52e1e9e29dSJed Brown // LCOV_EXCL_STOP 53e1e9e29dSJed Brown } 54e15f9bd0SJeremy L Thompson // Basis 55e15f9bd0SJeremy L Thompson if (b != CEED_BASIS_COLLOCATED) { 562b730f8bSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) { 578229195eSjeremylt // LCOV_EXCL_START 582b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with CEED_EVAL_NONE must be used with CEED_BASIS_COLLOCATED", 59d1d35e2fSjeremylt qf_field->field_name); 6041bdf1c9SJeremy L Thompson // LCOV_EXCL_STOP 612b730f8bSJeremy L Thompson } 622b730f8bSJeremy L Thompson CeedCall(CeedBasisGetDimension(b, &dim)); 632b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumComponents(b, &num_comp)); 642b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadratureComponents(b, &Q_comp)); 65d1d35e2fSjeremylt if (r != CEED_ELEMRESTRICTION_NONE && restr_num_comp != num_comp) { 66e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 67e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 682b730f8bSJeremy L Thompson "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has %" CeedInt_FMT 692b730f8bSJeremy L Thompson " components, but Basis has %" CeedInt_FMT " components", 702b730f8bSJeremy L Thompson qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp, num_comp); 71e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 72e15f9bd0SJeremy L Thompson } 7341bdf1c9SJeremy L Thompson } else if (eval_mode != CEED_EVAL_NONE) { 7441bdf1c9SJeremy L Thompson // LCOV_EXCL_START 752b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPATIBLE, "Field '%s' configured with %s cannot be used with CEED_BASIS_COLLOCATED", qf_field->field_name, 762b730f8bSJeremy L Thompson CeedEvalModes[eval_mode]); 7741bdf1c9SJeremy L Thompson // LCOV_EXCL_STOP 78e15f9bd0SJeremy L Thompson } 79e15f9bd0SJeremy L Thompson // Field size 80d1d35e2fSjeremylt switch (eval_mode) { 81e15f9bd0SJeremy L Thompson case CEED_EVAL_NONE: 822b730f8bSJeremy L Thompson if (size != restr_num_comp) { 83e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 84e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 852b730f8bSJeremy L Thompson "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction has " CeedInt_FMT " components", qf_field->field_name, 862b730f8bSJeremy L Thompson qf_field->size, CeedEvalModes[qf_field->eval_mode], restr_num_comp); 87e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 882b730f8bSJeremy L Thompson } 89e15f9bd0SJeremy L Thompson break; 90e15f9bd0SJeremy L Thompson case CEED_EVAL_INTERP: 912b730f8bSJeremy L Thompson if (size != num_comp * Q_comp) { 92e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 93e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 942b730f8bSJeremy L Thompson "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has " CeedInt_FMT " components", 952b730f8bSJeremy L Thompson qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * Q_comp); 96e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 972b730f8bSJeremy L Thompson } 98e15f9bd0SJeremy L Thompson break; 99e15f9bd0SJeremy L Thompson case CEED_EVAL_GRAD: 1002b730f8bSJeremy L Thompson if (size != num_comp * dim) { 101e15f9bd0SJeremy L Thompson // LCOV_EXCL_START 102e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_DIMENSION, 1032b730f8bSJeremy L Thompson "Field '%s' of size %" CeedInt_FMT " and EvalMode %s in %" CeedInt_FMT " dimensions: ElemRestriction/Basis has %" CeedInt_FMT 1042b730f8bSJeremy L Thompson " components", 1052b730f8bSJeremy L Thompson qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], dim, num_comp); 106e15f9bd0SJeremy L Thompson // LCOV_EXCL_STOP 1072b730f8bSJeremy L Thompson } 108e15f9bd0SJeremy L Thompson break; 109e15f9bd0SJeremy L Thompson case CEED_EVAL_WEIGHT: 110d1d35e2fSjeremylt // No additional checks required 111e15f9bd0SJeremy L Thompson break; 112e15f9bd0SJeremy L Thompson case CEED_EVAL_DIV: 1132b730f8bSJeremy L Thompson if (size != num_comp) { 114a0c16c2cSrezgarshakeri // LCOV_EXCL_START 115a0c16c2cSrezgarshakeri return CeedError(ceed, CEED_ERROR_DIMENSION, 1162b730f8bSJeremy L Thompson "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: ElemRestriction/Basis has " CeedInt_FMT " components", 1172b730f8bSJeremy L Thompson qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp); 118a0c16c2cSrezgarshakeri // LCOV_EXCL_STOP 1192b730f8bSJeremy L Thompson } 120e15f9bd0SJeremy L Thompson break; 121e15f9bd0SJeremy L Thompson case CEED_EVAL_CURL: 122e15f9bd0SJeremy L Thompson // Not implemented 123e15f9bd0SJeremy L Thompson break; 124e15f9bd0SJeremy L Thompson } 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1267a982d89SJeremy L. Thompson } 1277a982d89SJeremy L. Thompson 1287a982d89SJeremy L. Thompson /** 1297a982d89SJeremy L. Thompson @brief View a field of a CeedOperator 1307a982d89SJeremy L. Thompson 1317a982d89SJeremy L. Thompson @param[in] field Operator field to view 132d1d35e2fSjeremylt @param[in] qf_field QFunction field (carries field name) 133d1d35e2fSjeremylt @param[in] field_number Number of field being viewed 1344c4400c7SValeria Barra @param[in] sub true indicates sub-operator, which increases indentation; false for top-level operator 135d1d35e2fSjeremylt @param[in] input true for an input field; false for output field 1367a982d89SJeremy L. Thompson @param[in] stream Stream to view to, e.g., stdout 1377a982d89SJeremy L. Thompson 1387a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 1397a982d89SJeremy L. Thompson 1407a982d89SJeremy L. Thompson @ref Utility 1417a982d89SJeremy L. Thompson **/ 1422b730f8bSJeremy L Thompson static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) { 1437a982d89SJeremy L. Thompson const char *pre = sub ? " " : ""; 144d1d35e2fSjeremylt const char *in_out = input ? "Input" : "Output"; 1457a982d89SJeremy L. Thompson 1462b730f8bSJeremy L Thompson fprintf(stream, 1472b730f8bSJeremy L Thompson "%s %s field %" CeedInt_FMT 1482b730f8bSJeremy L Thompson ":\n" 1497a982d89SJeremy L. Thompson "%s Name: \"%s\"\n", 150d1d35e2fSjeremylt pre, in_out, field_number, pre, qf_field->field_name); 1517a982d89SJeremy L. Thompson 152990fdeb6SJeremy L Thompson fprintf(stream, "%s Size: %" CeedInt_FMT "\n", pre, qf_field->size); 153f5ebfb90SJeremy L Thompson 1542b730f8bSJeremy L Thompson fprintf(stream, "%s EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]); 155f5ebfb90SJeremy L Thompson 1562b730f8bSJeremy L Thompson if (field->basis == CEED_BASIS_COLLOCATED) fprintf(stream, "%s Collocated basis\n", pre); 1577a982d89SJeremy L. Thompson 1582b730f8bSJeremy L Thompson if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s Active vector\n", pre); 1592b730f8bSJeremy L Thompson else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s No vector\n", pre); 160f5ebfb90SJeremy L Thompson 161e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1627a982d89SJeremy L. Thompson } 1637a982d89SJeremy L. Thompson 1647a982d89SJeremy L. Thompson /** 1657a982d89SJeremy L. Thompson @brief View a single CeedOperator 1667a982d89SJeremy L. Thompson 1677a982d89SJeremy L. Thompson @param[in] op CeedOperator to view 1687a982d89SJeremy L. Thompson @param[in] sub Boolean flag for sub-operator 1697a982d89SJeremy L. Thompson @param[in] stream Stream to write; typically stdout/stderr or a file 1707a982d89SJeremy L. Thompson 1717a982d89SJeremy L. Thompson @return Error code: 0 - success, otherwise - failure 1727a982d89SJeremy L. Thompson 1737a982d89SJeremy L. Thompson @ref Utility 1747a982d89SJeremy L. Thompson **/ 1757a982d89SJeremy L. Thompson int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) { 1767a982d89SJeremy L. Thompson const char *pre = sub ? " " : ""; 1777a982d89SJeremy L. Thompson 178381e6593SJeremy L Thompson CeedInt num_elem, num_qpts; 1792b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1802b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 181381e6593SJeremy L Thompson 18278464608Sjeremylt CeedInt total_fields = 0; 1832b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumArgs(op, &total_fields)); 1842b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts); 1857a982d89SJeremy L. Thompson 1862b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : ""); 1877a982d89SJeremy L. Thompson 1882b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : ""); 189d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 1902b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream)); 1917a982d89SJeremy L. Thompson } 1927a982d89SJeremy L. Thompson 1932b730f8bSJeremy L Thompson fprintf(stream, "%s %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : ""); 194d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 1952b730f8bSJeremy L Thompson CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream)); 1967a982d89SJeremy L. Thompson } 197e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1987a982d89SJeremy L. Thompson } 1997a982d89SJeremy L. Thompson 200d99fa3c5SJeremy L Thompson /** 2010f60e0a8SJeremy L Thompson @brief Find the active vector basis for a non-composite CeedOperator 202eaf62fffSJeremy L Thompson 203eaf62fffSJeremy L Thompson @param[in] op CeedOperator to find active basis for 2040f60e0a8SJeremy L Thompson @param[out] active_basis Basis for active input vector or NULL for composite operator 205eaf62fffSJeremy L Thompson 206eaf62fffSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 207eaf62fffSJeremy L Thompson 208eaf62fffSJeremy L Thompson @ ref Developer 209eaf62fffSJeremy L Thompson **/ 210eaf62fffSJeremy L Thompson int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) { 211eaf62fffSJeremy L Thompson *active_basis = NULL; 2120f60e0a8SJeremy L Thompson if (op->is_composite) return CEED_ERROR_SUCCESS; 2132b730f8bSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 214eaf62fffSJeremy L Thompson if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 215eaf62fffSJeremy L Thompson *active_basis = op->input_fields[i]->basis; 216eaf62fffSJeremy L Thompson break; 217eaf62fffSJeremy L Thompson } 2182b730f8bSJeremy L Thompson } 219eaf62fffSJeremy L Thompson 220eaf62fffSJeremy L Thompson if (!*active_basis) { 221eaf62fffSJeremy L Thompson // LCOV_EXCL_START 222eaf62fffSJeremy L Thompson Ceed ceed; 2232b730f8bSJeremy L Thompson 2242b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2252b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MINOR, "No active CeedBasis found"); 226eaf62fffSJeremy L Thompson // LCOV_EXCL_STOP 227eaf62fffSJeremy L Thompson } 228eaf62fffSJeremy L Thompson return CEED_ERROR_SUCCESS; 229eaf62fffSJeremy L Thompson } 230eaf62fffSJeremy L Thompson 231eaf62fffSJeremy L Thompson /** 2320f60e0a8SJeremy L Thompson @brief Find the active vector ElemRestriction for a non-composite CeedOperator 233e2f04181SAndrew T. Barker 234e2f04181SAndrew T. Barker @param[in] op CeedOperator to find active basis for 2350f60e0a8SJeremy L Thompson @param[out] active_rstr ElemRestriction for active input vector or NULL for composite operator 236e2f04181SAndrew T. Barker 237e2f04181SAndrew T. Barker @return An error code: 0 - success, otherwise - failure 238e2f04181SAndrew T. Barker 239e2f04181SAndrew T. Barker @ref Utility 240e2f04181SAndrew T. Barker **/ 2412b730f8bSJeremy L Thompson int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) { 242d1d35e2fSjeremylt *active_rstr = NULL; 2430f60e0a8SJeremy L Thompson if (op->is_composite) return CEED_ERROR_SUCCESS; 2442b730f8bSJeremy L Thompson for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 245d1d35e2fSjeremylt if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 246d1d35e2fSjeremylt *active_rstr = op->input_fields[i]->elem_restr; 247e2f04181SAndrew T. Barker break; 248e2f04181SAndrew T. Barker } 2492b730f8bSJeremy L Thompson } 250e2f04181SAndrew T. Barker 251d1d35e2fSjeremylt if (!*active_rstr) { 252e2f04181SAndrew T. Barker // LCOV_EXCL_START 253e2f04181SAndrew T. Barker Ceed ceed; 2542b730f8bSJeremy L Thompson 2552b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 2562b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No active CeedElemRestriction found"); 257e2f04181SAndrew T. Barker // LCOV_EXCL_STOP 258e2f04181SAndrew T. Barker } 259e2f04181SAndrew T. Barker return CEED_ERROR_SUCCESS; 260e2f04181SAndrew T. Barker } 261e2f04181SAndrew T. Barker 262d8dd9a91SJeremy L Thompson /** 263d8dd9a91SJeremy L Thompson @brief Set QFunctionContext field value of the specified type. 264*ea61e9acSJeremy L Thompson For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`. 265*ea61e9acSJeremy L Thompson A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do 266*ea61e9acSJeremy L Thompson not have any field of a matching type. 267d8dd9a91SJeremy L Thompson 268*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 269*ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 270*ea61e9acSJeremy L Thompson @param[in] field_type Type of field to set 271*ea61e9acSJeremy L Thompson @param[in] value Value to set 272d8dd9a91SJeremy L Thompson 273d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 274d8dd9a91SJeremy L Thompson 275d8dd9a91SJeremy L Thompson @ref User 276d8dd9a91SJeremy L Thompson **/ 2772b730f8bSJeremy L Thompson static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *value) { 2782b730f8bSJeremy L Thompson if (!field_label) { 2793668ca4bSJeremy L Thompson // LCOV_EXCL_START 2802b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 2813668ca4bSJeremy L Thompson // LCOV_EXCL_STOP 2822b730f8bSJeremy L Thompson } 2833668ca4bSJeremy L Thompson 2843668ca4bSJeremy L Thompson bool is_composite = false; 2852b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 286d8dd9a91SJeremy L Thompson if (is_composite) { 287d8dd9a91SJeremy L Thompson CeedInt num_sub; 288d8dd9a91SJeremy L Thompson CeedOperator *sub_operators; 289d8dd9a91SJeremy L Thompson 2902b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumSub(op, &num_sub)); 2912b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetSubList(op, &sub_operators)); 2922b730f8bSJeremy L Thompson if (num_sub != field_label->num_sub_labels) { 2933668ca4bSJeremy L Thompson // LCOV_EXCL_START 2943668ca4bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, 2952b730f8bSJeremy L Thompson "ContextLabel does not correspond to composite operator. Use CeedOperatorGetContextFieldLabel()."); 2963668ca4bSJeremy L Thompson // LCOV_EXCL_STOP 2972b730f8bSJeremy L Thompson } 298d8dd9a91SJeremy L Thompson 299d8dd9a91SJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 300d8dd9a91SJeremy L Thompson // Try every sub-operator, ok if some sub-operators do not have field 3013668ca4bSJeremy L Thompson if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 3022b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, value)); 303d8dd9a91SJeremy L Thompson } 304d8dd9a91SJeremy L Thompson } 305d8dd9a91SJeremy L Thompson } else { 3062b730f8bSJeremy L Thompson if (!op->qf->ctx) { 307d8dd9a91SJeremy L Thompson // LCOV_EXCL_START 3082b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 309d8dd9a91SJeremy L Thompson // LCOV_EXCL_STOP 3102b730f8bSJeremy L Thompson } 311d8dd9a91SJeremy L Thompson 3122b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, value)); 313d8dd9a91SJeremy L Thompson } 314d8dd9a91SJeremy L Thompson 315d8dd9a91SJeremy L Thompson return CEED_ERROR_SUCCESS; 316d8dd9a91SJeremy L Thompson } 317d8dd9a91SJeremy L Thompson 3187a982d89SJeremy L. Thompson /// @} 3197a982d89SJeremy L. Thompson 3207a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3217a982d89SJeremy L. Thompson /// CeedOperator Backend API 3227a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 3237a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorBackend 3247a982d89SJeremy L. Thompson /// @{ 3257a982d89SJeremy L. Thompson 3267a982d89SJeremy L. Thompson /** 3277a982d89SJeremy L. Thompson @brief Get the number of arguments associated with a CeedOperator 3287a982d89SJeremy L. Thompson 329*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 330d1d35e2fSjeremylt @param[out] num_args Variable to store vector number of arguments 3317a982d89SJeremy L. Thompson 3327a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3337a982d89SJeremy L. Thompson 3347a982d89SJeremy L. Thompson @ref Backend 3357a982d89SJeremy L. Thompson **/ 3367a982d89SJeremy L. Thompson 337d1d35e2fSjeremylt int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) { 3382b730f8bSJeremy L Thompson if (op->is_composite) { 3397a982d89SJeremy L. Thompson // LCOV_EXCL_START 3402b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators"); 3417a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 3422b730f8bSJeremy L Thompson } 343d1d35e2fSjeremylt *num_args = op->num_fields; 344e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3457a982d89SJeremy L. Thompson } 3467a982d89SJeremy L. Thompson 3477a982d89SJeremy L. Thompson /** 3487a982d89SJeremy L. Thompson @brief Get the setup status of a CeedOperator 3497a982d89SJeremy L. Thompson 350*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 351d1d35e2fSjeremylt @param[out] is_setup_done Variable to store setup status 3527a982d89SJeremy L. Thompson 3537a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3547a982d89SJeremy L. Thompson 3557a982d89SJeremy L. Thompson @ref Backend 3567a982d89SJeremy L. Thompson **/ 3577a982d89SJeremy L. Thompson 358d1d35e2fSjeremylt int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) { 359f04ea552SJeremy L Thompson *is_setup_done = op->is_backend_setup; 360e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3617a982d89SJeremy L. Thompson } 3627a982d89SJeremy L. Thompson 3637a982d89SJeremy L. Thompson /** 3647a982d89SJeremy L. Thompson @brief Get the QFunction associated with a CeedOperator 3657a982d89SJeremy L. Thompson 366*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 3677a982d89SJeremy L. Thompson @param[out] qf Variable to store QFunction 3687a982d89SJeremy L. Thompson 3697a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 3707a982d89SJeremy L. Thompson 3717a982d89SJeremy L. Thompson @ref Backend 3727a982d89SJeremy L. Thompson **/ 3737a982d89SJeremy L. Thompson 3747a982d89SJeremy L. Thompson int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) { 3752b730f8bSJeremy L Thompson if (op->is_composite) { 3767a982d89SJeremy L. Thompson // LCOV_EXCL_START 3772b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 3787a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 3792b730f8bSJeremy L Thompson } 3807a982d89SJeremy L. Thompson *qf = op->qf; 381e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3827a982d89SJeremy L. Thompson } 3837a982d89SJeremy L. Thompson 3847a982d89SJeremy L. Thompson /** 385c04a41a7SJeremy L Thompson @brief Get a boolean value indicating if the CeedOperator is composite 386c04a41a7SJeremy L Thompson 387*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 388d1d35e2fSjeremylt @param[out] is_composite Variable to store composite status 389c04a41a7SJeremy L Thompson 390c04a41a7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 391c04a41a7SJeremy L Thompson 392c04a41a7SJeremy L Thompson @ref Backend 393c04a41a7SJeremy L Thompson **/ 394c04a41a7SJeremy L Thompson 395d1d35e2fSjeremylt int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) { 396f04ea552SJeremy L Thompson *is_composite = op->is_composite; 397e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 398c04a41a7SJeremy L Thompson } 399c04a41a7SJeremy L Thompson 400c04a41a7SJeremy L Thompson /** 401d1d35e2fSjeremylt @brief Get the number of sub_operators associated with a CeedOperator 4027a982d89SJeremy L. Thompson 403*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 404d1d35e2fSjeremylt @param[out] num_suboperators Variable to store number of sub_operators 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 **/ 4107a982d89SJeremy L. Thompson 411d1d35e2fSjeremylt int CeedOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) { 4122b730f8bSJeremy L Thompson if (!op->is_composite) { 4137a982d89SJeremy L. Thompson // LCOV_EXCL_START 414e15f9bd0SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator"); 4157a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 4162b730f8bSJeremy L Thompson } 417d1d35e2fSjeremylt *num_suboperators = op->num_suboperators; 418e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4197a982d89SJeremy L. Thompson } 4207a982d89SJeremy L. Thompson 4217a982d89SJeremy L. Thompson /** 422d1d35e2fSjeremylt @brief Get the list of sub_operators associated with a CeedOperator 4237a982d89SJeremy L. Thompson 424*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 425d1d35e2fSjeremylt @param[out] sub_operators Variable to store list of sub_operators 4267a982d89SJeremy L. Thompson 4277a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4287a982d89SJeremy L. Thompson 4297a982d89SJeremy L. Thompson @ref Backend 4307a982d89SJeremy L. Thompson **/ 4317a982d89SJeremy L. Thompson 432d1d35e2fSjeremylt int CeedOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) { 4332b730f8bSJeremy L Thompson if (!op->is_composite) { 4347a982d89SJeremy L. Thompson // LCOV_EXCL_START 435e15f9bd0SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not a composite operator"); 4367a982d89SJeremy L. Thompson // LCOV_EXCL_STOP 4372b730f8bSJeremy L Thompson } 438d1d35e2fSjeremylt *sub_operators = op->sub_operators; 439e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4407a982d89SJeremy L. Thompson } 4417a982d89SJeremy L. Thompson 4427a982d89SJeremy L. Thompson /** 4437a982d89SJeremy L. Thompson @brief Get the backend data of a CeedOperator 4447a982d89SJeremy L. Thompson 445*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 4467a982d89SJeremy L. Thompson @param[out] data Variable to store data 4477a982d89SJeremy L. Thompson 4487a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4497a982d89SJeremy L. Thompson 4507a982d89SJeremy L. Thompson @ref Backend 4517a982d89SJeremy L. Thompson **/ 4527a982d89SJeremy L. Thompson 453777ff853SJeremy L Thompson int CeedOperatorGetData(CeedOperator op, void *data) { 454777ff853SJeremy L Thompson *(void **)data = op->data; 455e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4567a982d89SJeremy L. Thompson } 4577a982d89SJeremy L. Thompson 4587a982d89SJeremy L. Thompson /** 4597a982d89SJeremy L. Thompson @brief Set the backend data of a CeedOperator 4607a982d89SJeremy L. Thompson 461*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 462*ea61e9acSJeremy L Thompson @param[in] data Data to set 4637a982d89SJeremy L. Thompson 4647a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4657a982d89SJeremy L. Thompson 4667a982d89SJeremy L. Thompson @ref Backend 4677a982d89SJeremy L. Thompson **/ 4687a982d89SJeremy L. Thompson 469777ff853SJeremy L Thompson int CeedOperatorSetData(CeedOperator op, void *data) { 470777ff853SJeremy L Thompson op->data = data; 471e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4727a982d89SJeremy L. Thompson } 4737a982d89SJeremy L. Thompson 4747a982d89SJeremy L. Thompson /** 47534359f16Sjeremylt @brief Increment the reference counter for a CeedOperator 47634359f16Sjeremylt 477*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator to increment the reference counter 47834359f16Sjeremylt 47934359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 48034359f16Sjeremylt 48134359f16Sjeremylt @ref Backend 48234359f16Sjeremylt **/ 4839560d06aSjeremylt int CeedOperatorReference(CeedOperator op) { 48434359f16Sjeremylt op->ref_count++; 48534359f16Sjeremylt return CEED_ERROR_SUCCESS; 48634359f16Sjeremylt } 48734359f16Sjeremylt 48834359f16Sjeremylt /** 4897a982d89SJeremy L. Thompson @brief Set the setup flag of a CeedOperator to True 4907a982d89SJeremy L. Thompson 491*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 4927a982d89SJeremy L. Thompson 4937a982d89SJeremy L. Thompson @return An error code: 0 - success, otherwise - failure 4947a982d89SJeremy L. Thompson 4957a982d89SJeremy L. Thompson @ref Backend 4967a982d89SJeremy L. Thompson **/ 4977a982d89SJeremy L. Thompson 4987a982d89SJeremy L. Thompson int CeedOperatorSetSetupDone(CeedOperator op) { 499f04ea552SJeremy L Thompson op->is_backend_setup = true; 500e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5017a982d89SJeremy L. Thompson } 5027a982d89SJeremy L. Thompson 5037a982d89SJeremy L. Thompson /// @} 5047a982d89SJeremy L. Thompson 5057a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5067a982d89SJeremy L. Thompson /// CeedOperator Public API 5077a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 5087a982d89SJeremy L. Thompson /// @addtogroup CeedOperatorUser 509dfdf5a53Sjeremylt /// @{ 510d7b241e6Sjeremylt 511d7b241e6Sjeremylt /** 512*ea61e9acSJeremy L Thompson @brief Create a CeedOperator and associate a CeedQFunction. 513*ea61e9acSJeremy L Thompson A CeedBasis and CeedElemRestriction can be associated with CeedQFunction fields with \ref CeedOperatorSetField. 514d7b241e6Sjeremylt 515*ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedOperator will be created 516*ea61e9acSJeremy L Thompson @param[in] qf QFunction defining the action of the operator at quadrature points 517*ea61e9acSJeremy L Thompson @param[in] dqf QFunction defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 518*ea61e9acSJeremy L Thompson @param[in] dqfT QFunction defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 519*ea61e9acSJeremy L Thompson @param[out] op Address of the variable where the newly created CeedOperator will be stored 520b11c1e72Sjeremylt 521b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 522dfdf5a53Sjeremylt 5237a982d89SJeremy L. Thompson @ref User 524d7b241e6Sjeremylt */ 5252b730f8bSJeremy L Thompson int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 5265fe0d4faSjeremylt if (!ceed->OperatorCreate) { 5275fe0d4faSjeremylt Ceed delegate; 5282b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 5295fe0d4faSjeremylt 5302b730f8bSJeremy L Thompson if (!delegate) { 531c042f62fSJeremy L Thompson // LCOV_EXCL_START 5322b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support OperatorCreate"); 533c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 5342b730f8bSJeremy L Thompson } 5355fe0d4faSjeremylt 5362b730f8bSJeremy L Thompson CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op)); 537e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5385fe0d4faSjeremylt } 5395fe0d4faSjeremylt 5402b730f8bSJeremy L Thompson if (!qf || qf == CEED_QFUNCTION_NONE) { 541b3b7035fSJeremy L Thompson // LCOV_EXCL_START 5422b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_MINOR, "Operator must have a valid QFunction."); 543b3b7035fSJeremy L Thompson // LCOV_EXCL_STOP 5442b730f8bSJeremy L Thompson } 5452b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op)); 546d7b241e6Sjeremylt (*op)->ceed = ceed; 5472b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 548d1d35e2fSjeremylt (*op)->ref_count = 1; 549d7b241e6Sjeremylt (*op)->qf = qf; 5502b104005SJeremy L Thompson (*op)->input_size = -1; 5512b104005SJeremy L Thompson (*op)->output_size = -1; 5522b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(qf)); 553442e7f0bSjeremylt if (dqf && dqf != CEED_QFUNCTION_NONE) { 554d7b241e6Sjeremylt (*op)->dqf = dqf; 5552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(dqf)); 556442e7f0bSjeremylt } 557442e7f0bSjeremylt if (dqfT && dqfT != CEED_QFUNCTION_NONE) { 558d7b241e6Sjeremylt (*op)->dqfT = dqfT; 5592b730f8bSJeremy L Thompson CeedCall(CeedQFunctionReference(dqfT)); 560442e7f0bSjeremylt } 5612b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 5622b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 5632b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 5642b730f8bSJeremy L Thompson CeedCall(ceed->OperatorCreate(*op)); 565e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 566d7b241e6Sjeremylt } 567d7b241e6Sjeremylt 568d7b241e6Sjeremylt /** 56952d6035fSJeremy L Thompson @brief Create an operator that composes the action of several operators 57052d6035fSJeremy L Thompson 571*ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedOperator will be created 572*ea61e9acSJeremy L Thompson @param[out] op Address of the variable where the newly created Composite CeedOperator will be stored 57352d6035fSJeremy L Thompson 57452d6035fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 57552d6035fSJeremy L Thompson 5767a982d89SJeremy L. Thompson @ref User 57752d6035fSJeremy L Thompson */ 57852d6035fSJeremy L Thompson int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) { 57952d6035fSJeremy L Thompson if (!ceed->CompositeOperatorCreate) { 58052d6035fSJeremy L Thompson Ceed delegate; 5812b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 58252d6035fSJeremy L Thompson 583250756a7Sjeremylt if (delegate) { 5842b730f8bSJeremy L Thompson CeedCall(CeedCompositeOperatorCreate(delegate, op)); 585e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 58652d6035fSJeremy L Thompson } 587250756a7Sjeremylt } 58852d6035fSJeremy L Thompson 5892b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op)); 59052d6035fSJeremy L Thompson (*op)->ceed = ceed; 5912b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 592996d9ab5SJed Brown (*op)->ref_count = 1; 593f04ea552SJeremy L Thompson (*op)->is_composite = true; 5942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators)); 5952b104005SJeremy L Thompson (*op)->input_size = -1; 5962b104005SJeremy L Thompson (*op)->output_size = -1; 597250756a7Sjeremylt 598250756a7Sjeremylt if (ceed->CompositeOperatorCreate) { 5992b730f8bSJeremy L Thompson CeedCall(ceed->CompositeOperatorCreate(*op)); 600250756a7Sjeremylt } 601e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 60252d6035fSJeremy L Thompson } 60352d6035fSJeremy L Thompson 60452d6035fSJeremy L Thompson /** 605*ea61e9acSJeremy L Thompson @brief Copy the pointer to a CeedOperator. 606*ea61e9acSJeremy L Thompson Both pointers should be destroyed with `CeedOperatorDestroy()`. 607*ea61e9acSJeremy L Thompson Note: If `*op_copy` is non-NULL, then it is assumed that `*op_copy` is a pointer to a CeedOperator. 608*ea61e9acSJeremy L Thompson This CeedOperator will be destroyed if `*op_copy` is the only reference to this CeedOperator. 6099560d06aSjeremylt 610*ea61e9acSJeremy L Thompson @param[in] op CeedOperator to copy reference to 611*ea61e9acSJeremy L Thompson @param[in,out] op_copy Variable to store copied reference 6129560d06aSjeremylt 6139560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 6149560d06aSjeremylt 6159560d06aSjeremylt @ref User 6169560d06aSjeremylt **/ 6179560d06aSjeremylt int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) { 6182b730f8bSJeremy L Thompson CeedCall(CeedOperatorReference(op)); 6192b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(op_copy)); 6209560d06aSjeremylt *op_copy = op; 6219560d06aSjeremylt return CEED_ERROR_SUCCESS; 6229560d06aSjeremylt } 6239560d06aSjeremylt 6249560d06aSjeremylt /** 625*ea61e9acSJeremy L Thompson @brief Provide a field to a CeedOperator for use by its CeedQFunction. 626d7b241e6Sjeremylt 627*ea61e9acSJeremy L Thompson This function is used to specify both active and passive fields to a CeedOperator. 628*ea61e9acSJeremy L Thompson For passive fields, a vector @arg v must be provided. 629*ea61e9acSJeremy L Thompson Passive fields can inputs or outputs (updated in-place when operator is applied). 630d7b241e6Sjeremylt 631*ea61e9acSJeremy L Thompson Active fields must be specified using this function, but their data (in a CeedVector) is passed in CeedOperatorApply(). 632*ea61e9acSJeremy L Thompson There can be at most one active input CeedVector and at most one active output CeedVector passed to CeedOperatorApply(). 633d7b241e6Sjeremylt 634*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator on which to provide the field 635*ea61e9acSJeremy L Thompson @param[in] field_name Name of the field (to be matched with the name used by CeedQFunction) 636*ea61e9acSJeremy L Thompson @param[in] r CeedElemRestriction 637*ea61e9acSJeremy L Thompson @param[in] b CeedBasis in which the field resides or @ref CEED_BASIS_COLLOCATED if collocated with quadrature points 638*ea61e9acSJeremy 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 if using @ref 639*ea61e9acSJeremy L Thompson CEED_EVAL_WEIGHT in the QFunction 640b11c1e72Sjeremylt 641b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 642dfdf5a53Sjeremylt 6437a982d89SJeremy L. Thompson @ref User 644b11c1e72Sjeremylt **/ 6452b730f8bSJeremy L Thompson int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) { 6462b730f8bSJeremy L Thompson if (op->is_composite) { 647c042f62fSJeremy L Thompson // LCOV_EXCL_START 6482b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); 649c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6502b730f8bSJeremy L Thompson } 6512b730f8bSJeremy L Thompson if (op->is_immutable) { 652f04ea552SJeremy L Thompson // LCOV_EXCL_START 6532b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 654f04ea552SJeremy L Thompson // LCOV_EXCL_STOP 6552b730f8bSJeremy L Thompson } 6562b730f8bSJeremy L Thompson if (!r) { 657c042f62fSJeremy L Thompson // LCOV_EXCL_START 6582b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "ElemRestriction r for field \"%s\" must be non-NULL.", field_name); 659c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6602b730f8bSJeremy L Thompson } 6612b730f8bSJeremy L Thompson if (!b) { 662c042f62fSJeremy L Thompson // LCOV_EXCL_START 6632b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Basis b for field \"%s\" must be non-NULL.", field_name); 664c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6652b730f8bSJeremy L Thompson } 6662b730f8bSJeremy L Thompson if (!v) { 667c042f62fSJeremy L Thompson // LCOV_EXCL_START 6682b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Vector v for field \"%s\" must be non-NULL.", field_name); 669c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6702b730f8bSJeremy L Thompson } 67152d6035fSJeremy L Thompson 672d1d35e2fSjeremylt CeedInt num_elem; 6732b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem)); 6742b730f8bSJeremy L Thompson if (r != CEED_ELEMRESTRICTION_NONE && op->has_restriction && op->num_elem != num_elem) { 675c042f62fSJeremy L Thompson // LCOV_EXCL_START 676e15f9bd0SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, 6772b730f8bSJeremy L Thompson "ElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem); 678c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 6792b730f8bSJeremy L Thompson } 680d7b241e6Sjeremylt 68178464608Sjeremylt CeedInt num_qpts = 0; 682e15f9bd0SJeremy L Thompson if (b != CEED_BASIS_COLLOCATED) { 6832b730f8bSJeremy L Thompson CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts)); 6842b730f8bSJeremy L Thompson if (op->num_qpts && op->num_qpts != num_qpts) { 685c042f62fSJeremy L Thompson // LCOV_EXCL_START 686e15f9bd0SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_DIMENSION, 6872b730f8bSJeremy L Thompson "Basis with %" CeedInt_FMT " quadrature points incompatible with prior %" CeedInt_FMT " points", num_qpts, op->num_qpts); 688c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 689d7b241e6Sjeremylt } 6902b730f8bSJeremy L Thompson } 691d1d35e2fSjeremylt CeedQFunctionField qf_field; 692d1d35e2fSjeremylt CeedOperatorField *op_field; 6932b104005SJeremy L Thompson bool is_input = true; 694d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 695d1d35e2fSjeremylt if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) { 696d1d35e2fSjeremylt qf_field = op->qf->input_fields[i]; 697d1d35e2fSjeremylt op_field = &op->input_fields[i]; 698d7b241e6Sjeremylt goto found; 699d7b241e6Sjeremylt } 700d7b241e6Sjeremylt } 7012b104005SJeremy L Thompson is_input = false; 702d1d35e2fSjeremylt for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 703d1d35e2fSjeremylt if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) { 704d1d35e2fSjeremylt qf_field = op->qf->output_fields[i]; 705d1d35e2fSjeremylt op_field = &op->output_fields[i]; 706d7b241e6Sjeremylt goto found; 707d7b241e6Sjeremylt } 708d7b241e6Sjeremylt } 709c042f62fSJeremy L Thompson // LCOV_EXCL_START 7102b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "QFunction has no knowledge of field '%s'", field_name); 711c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 712d7b241e6Sjeremylt found: 7132b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b)); 7142b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, op_field)); 715e15f9bd0SJeremy L Thompson 7162b104005SJeremy L Thompson if (v == CEED_VECTOR_ACTIVE) { 7172b104005SJeremy L Thompson CeedSize l_size; 7182b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size)); 7192b104005SJeremy L Thompson if (is_input) { 7202b104005SJeremy L Thompson if (op->input_size == -1) op->input_size = l_size; 7212b730f8bSJeremy L Thompson if (l_size != op->input_size) { 7222b104005SJeremy L Thompson // LCOV_EXCL_START 7232b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->input_size); 7242b104005SJeremy L Thompson // LCOV_EXCL_STOP 7252b730f8bSJeremy L Thompson } 7262b104005SJeremy L Thompson } else { 7272b104005SJeremy L Thompson if (op->output_size == -1) op->output_size = l_size; 7282b730f8bSJeremy L Thompson if (l_size != op->output_size) { 7292b104005SJeremy L Thompson // LCOV_EXCL_START 7302b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, op->output_size); 7312b104005SJeremy L Thompson // LCOV_EXCL_STOP 7322b104005SJeremy L Thompson } 7332b104005SJeremy L Thompson } 7342b730f8bSJeremy L Thompson } 7352b104005SJeremy L Thompson 736d1d35e2fSjeremylt (*op_field)->vec = v; 737e15f9bd0SJeremy L Thompson if (v != CEED_VECTOR_ACTIVE && v != CEED_VECTOR_NONE) { 7382b730f8bSJeremy L Thompson CeedCall(CeedVectorReference(v)); 739e15f9bd0SJeremy L Thompson } 740e15f9bd0SJeremy L Thompson 741d1d35e2fSjeremylt (*op_field)->elem_restr = r; 7422b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionReference(r)); 743e15f9bd0SJeremy L Thompson if (r != CEED_ELEMRESTRICTION_NONE) { 744d1d35e2fSjeremylt op->num_elem = num_elem; 745d1d35e2fSjeremylt op->has_restriction = true; // Restriction set, but num_elem may be 0 746e15f9bd0SJeremy L Thompson } 747d99fa3c5SJeremy L Thompson 748d1d35e2fSjeremylt (*op_field)->basis = b; 749e15f9bd0SJeremy L Thompson if (b != CEED_BASIS_COLLOCATED) { 750cd4dfc48Sjeremylt if (!op->num_qpts) { 7512b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetNumQuadraturePoints(op, num_qpts)); 752cd4dfc48Sjeremylt } 7532b730f8bSJeremy L Thompson CeedCall(CeedBasisReference(b)); 754e15f9bd0SJeremy L Thompson } 755e15f9bd0SJeremy L Thompson 756d1d35e2fSjeremylt op->num_fields += 1; 7572b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name)); 758e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 759d7b241e6Sjeremylt } 760d7b241e6Sjeremylt 761d7b241e6Sjeremylt /** 76243bbe138SJeremy L Thompson @brief Get the CeedOperatorFields of a CeedOperator 76343bbe138SJeremy L Thompson 764*ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 765f04ea552SJeremy L Thompson 766*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 767f74ec584SJeremy L Thompson @param[out] num_input_fields Variable to store number of input fields 76843bbe138SJeremy L Thompson @param[out] input_fields Variable to store input_fields 769f74ec584SJeremy L Thompson @param[out] num_output_fields Variable to store number of output fields 77043bbe138SJeremy L Thompson @param[out] output_fields Variable to store output_fields 77143bbe138SJeremy L Thompson 77243bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 77343bbe138SJeremy L Thompson 774e9b533fbSJeremy L Thompson @ref Advanced 77543bbe138SJeremy L Thompson **/ 7762b730f8bSJeremy L Thompson int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields, 77743bbe138SJeremy L Thompson CeedOperatorField **output_fields) { 7782b730f8bSJeremy L Thompson if (op->is_composite) { 77943bbe138SJeremy L Thompson // LCOV_EXCL_START 7802b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 78143bbe138SJeremy L Thompson // LCOV_EXCL_STOP 7822b730f8bSJeremy L Thompson } 7832b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 78443bbe138SJeremy L Thompson 78543bbe138SJeremy L Thompson if (num_input_fields) *num_input_fields = op->qf->num_input_fields; 78643bbe138SJeremy L Thompson if (input_fields) *input_fields = op->input_fields; 78743bbe138SJeremy L Thompson if (num_output_fields) *num_output_fields = op->qf->num_output_fields; 78843bbe138SJeremy L Thompson if (output_fields) *output_fields = op->output_fields; 78943bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 79043bbe138SJeremy L Thompson } 79143bbe138SJeremy L Thompson 79243bbe138SJeremy L Thompson /** 79328567f8fSJeremy L Thompson @brief Get the name of a CeedOperatorField 79428567f8fSJeremy L Thompson 795*ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 79628567f8fSJeremy L Thompson @param[out] field_name Variable to store the field name 79728567f8fSJeremy L Thompson 79828567f8fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 79928567f8fSJeremy L Thompson 800e9b533fbSJeremy L Thompson @ref Advanced 80128567f8fSJeremy L Thompson **/ 80228567f8fSJeremy L Thompson int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) { 80328567f8fSJeremy L Thompson *field_name = (char *)op_field->field_name; 80428567f8fSJeremy L Thompson return CEED_ERROR_SUCCESS; 80528567f8fSJeremy L Thompson } 80628567f8fSJeremy L Thompson 80728567f8fSJeremy L Thompson /** 80843bbe138SJeremy L Thompson @brief Get the CeedElemRestriction of a CeedOperatorField 80943bbe138SJeremy L Thompson 810*ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 81143bbe138SJeremy L Thompson @param[out] rstr Variable to store CeedElemRestriction 81243bbe138SJeremy L Thompson 81343bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 81443bbe138SJeremy L Thompson 815e9b533fbSJeremy L Thompson @ref Advanced 81643bbe138SJeremy L Thompson **/ 8172b730f8bSJeremy L Thompson int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) { 81843bbe138SJeremy L Thompson *rstr = op_field->elem_restr; 81943bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 82043bbe138SJeremy L Thompson } 82143bbe138SJeremy L Thompson 82243bbe138SJeremy L Thompson /** 82343bbe138SJeremy L Thompson @brief Get the CeedBasis of a CeedOperatorField 82443bbe138SJeremy L Thompson 825*ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 82643bbe138SJeremy L Thompson @param[out] basis Variable to store CeedBasis 82743bbe138SJeremy L Thompson 82843bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 82943bbe138SJeremy L Thompson 830e9b533fbSJeremy L Thompson @ref Advanced 83143bbe138SJeremy L Thompson **/ 83243bbe138SJeremy L Thompson int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) { 83343bbe138SJeremy L Thompson *basis = op_field->basis; 83443bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 83543bbe138SJeremy L Thompson } 83643bbe138SJeremy L Thompson 83743bbe138SJeremy L Thompson /** 83843bbe138SJeremy L Thompson @brief Get the CeedVector of a CeedOperatorField 83943bbe138SJeremy L Thompson 840*ea61e9acSJeremy L Thompson @param[in] op_field CeedOperatorField 84143bbe138SJeremy L Thompson @param[out] vec Variable to store CeedVector 84243bbe138SJeremy L Thompson 84343bbe138SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 84443bbe138SJeremy L Thompson 845e9b533fbSJeremy L Thompson @ref Advanced 84643bbe138SJeremy L Thompson **/ 84743bbe138SJeremy L Thompson int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) { 84843bbe138SJeremy L Thompson *vec = op_field->vec; 84943bbe138SJeremy L Thompson return CEED_ERROR_SUCCESS; 85043bbe138SJeremy L Thompson } 85143bbe138SJeremy L Thompson 85243bbe138SJeremy L Thompson /** 85352d6035fSJeremy L Thompson @brief Add a sub-operator to a composite CeedOperator 854288c0443SJeremy L Thompson 855*ea61e9acSJeremy L Thompson @param[in,out] composite_op Composite CeedOperator 856*ea61e9acSJeremy L Thompson @param[in] sub_op Sub-operator CeedOperator 85752d6035fSJeremy L Thompson 85852d6035fSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 85952d6035fSJeremy L Thompson 8607a982d89SJeremy L. Thompson @ref User 86152d6035fSJeremy L Thompson */ 8622b730f8bSJeremy L Thompson int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) { 8632b730f8bSJeremy L Thompson if (!composite_op->is_composite) { 864c042f62fSJeremy L Thompson // LCOV_EXCL_START 8652b730f8bSJeremy L Thompson return CeedError(composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); 8662b104005SJeremy L Thompson // LCOV_EXCL_STOP 8672b104005SJeremy L Thompson } 8682b104005SJeremy L Thompson 8692b730f8bSJeremy L Thompson if (composite_op->num_suboperators == CEED_COMPOSITE_MAX) { 8702b730f8bSJeremy L Thompson // LCOV_EXCL_START 8712b730f8bSJeremy L Thompson return CeedError(composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators"); 8722b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 8732b730f8bSJeremy L Thompson } 8742b730f8bSJeremy L Thompson if (composite_op->is_immutable) { 8752b730f8bSJeremy L Thompson // LCOV_EXCL_START 8762b730f8bSJeremy L Thompson return CeedError(composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 8772b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 8782b730f8bSJeremy L Thompson } 8792b730f8bSJeremy L Thompson 8802b730f8bSJeremy L Thompson { 8812b730f8bSJeremy L Thompson CeedSize input_size, output_size; 8822b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size)); 8832b730f8bSJeremy L Thompson if (composite_op->input_size == -1) composite_op->input_size = input_size; 8842b730f8bSJeremy L Thompson if (composite_op->output_size == -1) composite_op->output_size = output_size; 8852b730f8bSJeremy L Thompson // Note, a size of -1 means no active vector restriction set, so no incompatibility 8862b730f8bSJeremy L Thompson if ((input_size != -1 && input_size != composite_op->input_size) || (output_size != -1 && output_size != composite_op->output_size)) { 8872b730f8bSJeremy L Thompson // LCOV_EXCL_START 8882b730f8bSJeremy L Thompson return CeedError(composite_op->ceed, CEED_ERROR_MAJOR, 8892b730f8bSJeremy L Thompson "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 8902b730f8bSJeremy L Thompson "shape (%td, %td)", 8912b730f8bSJeremy L Thompson composite_op->input_size, composite_op->output_size, input_size, output_size); 8922b730f8bSJeremy L Thompson // LCOV_EXCL_STOP 8932b730f8bSJeremy L Thompson } 8942b730f8bSJeremy L Thompson } 8952b730f8bSJeremy L Thompson 896d1d35e2fSjeremylt composite_op->sub_operators[composite_op->num_suboperators] = sub_op; 8972b730f8bSJeremy L Thompson CeedCall(CeedOperatorReference(sub_op)); 898d1d35e2fSjeremylt composite_op->num_suboperators++; 899e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 90052d6035fSJeremy L Thompson } 90152d6035fSJeremy L Thompson 90252d6035fSJeremy L Thompson /** 9034db537f9SJeremy L Thompson @brief Check if a CeedOperator is ready to be used. 9044db537f9SJeremy L Thompson 9054db537f9SJeremy L Thompson @param[in] op CeedOperator to check 9064db537f9SJeremy L Thompson 9074db537f9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9084db537f9SJeremy L Thompson 9094db537f9SJeremy L Thompson @ref User 9104db537f9SJeremy L Thompson **/ 9114db537f9SJeremy L Thompson int CeedOperatorCheckReady(CeedOperator op) { 9124db537f9SJeremy L Thompson Ceed ceed; 9132b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetCeed(op, &ceed)); 9144db537f9SJeremy L Thompson 9152b730f8bSJeremy L Thompson if (op->is_interface_setup) return CEED_ERROR_SUCCESS; 9164db537f9SJeremy L Thompson 9174db537f9SJeremy L Thompson CeedQFunction qf = op->qf; 9184db537f9SJeremy L Thompson if (op->is_composite) { 91943622462SJeremy L Thompson if (!op->num_suboperators) { 92043622462SJeremy L Thompson // Empty operator setup 92143622462SJeremy L Thompson op->input_size = 0; 92243622462SJeremy L Thompson op->output_size = 0; 92343622462SJeremy L Thompson } else { 9244db537f9SJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 9252b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op->sub_operators[i])); 9264db537f9SJeremy L Thompson } 9272b104005SJeremy L Thompson // Sub-operators could be modified after adding to composite operator 9282b104005SJeremy L Thompson // Need to verify no lvec incompatibility from any changes 9292b104005SJeremy L Thompson CeedSize input_size, output_size; 9302b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 93143622462SJeremy L Thompson } 9324db537f9SJeremy L Thompson } else { 9332b730f8bSJeremy L Thompson if (op->num_fields == 0) { 9344db537f9SJeremy L Thompson // LCOV_EXCL_START 9354db537f9SJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, "No operator fields set"); 9364db537f9SJeremy L Thompson // LCOV_EXCL_STOP 9372b730f8bSJeremy L Thompson } 9382b730f8bSJeremy L Thompson if (op->num_fields < qf->num_input_fields + qf->num_output_fields) { 9394db537f9SJeremy L Thompson // LCOV_EXCL_START 9404db537f9SJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set"); 9414db537f9SJeremy L Thompson // LCOV_EXCL_STOP 9422b730f8bSJeremy L Thompson } 9432b730f8bSJeremy L Thompson if (!op->has_restriction) { 9444db537f9SJeremy L Thompson // LCOV_EXCL_START 9452b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required"); 9464db537f9SJeremy L Thompson // LCOV_EXCL_STOP 9472b730f8bSJeremy L Thompson } 9482b730f8bSJeremy L Thompson if (op->num_qpts == 0) { 9494db537f9SJeremy L Thompson // LCOV_EXCL_START 9502b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_INCOMPLETE, "At least one non-collocated basis is required or the number of quadrature points must be set"); 9514db537f9SJeremy L Thompson // LCOV_EXCL_STOP 9524db537f9SJeremy L Thompson } 9532b730f8bSJeremy L Thompson } 9544db537f9SJeremy L Thompson 9554db537f9SJeremy L Thompson // Flag as immutable and ready 9564db537f9SJeremy L Thompson op->is_interface_setup = true; 9572b730f8bSJeremy L Thompson if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true; 9582b730f8bSJeremy L Thompson if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true; 9592b730f8bSJeremy L Thompson if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true; 9604db537f9SJeremy L Thompson return CEED_ERROR_SUCCESS; 9614db537f9SJeremy L Thompson } 9624db537f9SJeremy L Thompson 9634db537f9SJeremy L Thompson /** 964c9366a6bSJeremy L Thompson @brief Get vector lengths for the active input and/or output vectors of a CeedOperator. 965*ea61e9acSJeremy L Thompson Note: Lengths of -1 indicate that the CeedOperator does not have an active input and/or output. 966c9366a6bSJeremy L Thompson 967c9366a6bSJeremy L Thompson @param[in] op CeedOperator 968c9366a6bSJeremy L Thompson @param[out] input_size Variable to store active input vector length, or NULL 969c9366a6bSJeremy L Thompson @param[out] output_size Variable to store active output vector length, or NULL 970c9366a6bSJeremy L Thompson 971c9366a6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 972c9366a6bSJeremy L Thompson 973c9366a6bSJeremy L Thompson @ref User 974c9366a6bSJeremy L Thompson **/ 9752b730f8bSJeremy L Thompson int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) { 976c9366a6bSJeremy L Thompson bool is_composite; 977c9366a6bSJeremy L Thompson 9782b104005SJeremy L Thompson if (input_size) *input_size = op->input_size; 9792b104005SJeremy L Thompson if (output_size) *output_size = op->output_size; 980c9366a6bSJeremy L Thompson 9812b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 9822b104005SJeremy L Thompson if (is_composite && (op->input_size == -1 || op->output_size == -1)) { 983c9366a6bSJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 984c9366a6bSJeremy L Thompson CeedSize sub_input_size, sub_output_size; 9852b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size)); 9862b104005SJeremy L Thompson if (op->input_size == -1) op->input_size = sub_input_size; 9872b104005SJeremy L Thompson if (op->output_size == -1) op->output_size = sub_output_size; 9882b104005SJeremy L Thompson // Note, a size of -1 means no active vector restriction set, so no incompatibility 9892b730f8bSJeremy L Thompson if ((sub_input_size != -1 && sub_input_size != op->input_size) || (sub_output_size != -1 && sub_output_size != op->output_size)) { 9902b104005SJeremy L Thompson // LCOV_EXCL_START 9912b104005SJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MAJOR, 9922b730f8bSJeremy L Thompson "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 9932b730f8bSJeremy L Thompson "shape (%td, %td)", 9942b104005SJeremy L Thompson op->input_size, op->output_size, input_size, output_size); 9952b104005SJeremy L Thompson // LCOV_EXCL_STOP 996c9366a6bSJeremy L Thompson } 997c9366a6bSJeremy L Thompson } 9982b730f8bSJeremy L Thompson } 999c9366a6bSJeremy L Thompson 1000c9366a6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 1001c9366a6bSJeremy L Thompson } 1002c9366a6bSJeremy L Thompson 1003c9366a6bSJeremy L Thompson /** 1004beecbf24SJeremy L Thompson @brief Set reuse of CeedQFunction data in CeedOperatorLinearAssemble* functions. 1005*ea61e9acSJeremy L Thompson When `reuse_assembly_data = false` (default), the CeedQFunction associated with this CeedOperator is re-assembled every time a 1006*ea61e9acSJeremy L Thompson `CeedOperatorLinearAssemble*` function is called. When `reuse_assembly_data = true`, the CeedQFunction associated with this CeedOperator is reused 1007*ea61e9acSJeremy L Thompson between calls to `CeedOperatorSetQFunctionAssemblyDataUpdated`. 10088b919e6bSJeremy L Thompson 1009beecbf24SJeremy L Thompson @param[in] op CeedOperator 1010beecbf24SJeremy L Thompson @param[in] reuse_assembly_data Boolean flag setting assembly data reuse 10118b919e6bSJeremy L Thompson 10128b919e6bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 10138b919e6bSJeremy L Thompson 10148b919e6bSJeremy L Thompson @ref Advanced 10158b919e6bSJeremy L Thompson **/ 10162b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) { 10178b919e6bSJeremy L Thompson bool is_composite; 10188b919e6bSJeremy L Thompson 10192b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 10208b919e6bSJeremy L Thompson if (is_composite) { 10218b919e6bSJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 10222b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data)); 10238b919e6bSJeremy L Thompson } 10248b919e6bSJeremy L Thompson } else { 10252b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data)); 1026beecbf24SJeremy L Thompson } 1027beecbf24SJeremy L Thompson 1028beecbf24SJeremy L Thompson return CEED_ERROR_SUCCESS; 1029beecbf24SJeremy L Thompson } 1030beecbf24SJeremy L Thompson 1031beecbf24SJeremy L Thompson /** 1032beecbf24SJeremy L Thompson @brief Mark CeedQFunction data as updated and the CeedQFunction as requiring re-assembly. 1033beecbf24SJeremy L Thompson 1034beecbf24SJeremy L Thompson @param[in] op CeedOperator 10356e15d496SJeremy L Thompson @param[in] needs_data_update Boolean flag setting assembly data reuse 1036beecbf24SJeremy L Thompson 1037beecbf24SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1038beecbf24SJeremy L Thompson 1039beecbf24SJeremy L Thompson @ref Advanced 1040beecbf24SJeremy L Thompson **/ 10412b730f8bSJeremy L Thompson int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) { 1042beecbf24SJeremy L Thompson bool is_composite; 1043beecbf24SJeremy L Thompson 10442b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1045beecbf24SJeremy L Thompson if (is_composite) { 1046beecbf24SJeremy L Thompson for (CeedInt i = 0; i < op->num_suboperators; i++) { 10472b730f8bSJeremy L Thompson CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update)); 1048beecbf24SJeremy L Thompson } 1049beecbf24SJeremy L Thompson } else { 10502b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update)); 10518b919e6bSJeremy L Thompson } 10528b919e6bSJeremy L Thompson 10538b919e6bSJeremy L Thompson return CEED_ERROR_SUCCESS; 10548b919e6bSJeremy L Thompson } 10558b919e6bSJeremy L Thompson 10568b919e6bSJeremy L Thompson /** 1057cd4dfc48Sjeremylt @brief Set the number of quadrature points associated with a CeedOperator. 1058*ea61e9acSJeremy L Thompson This should be used when creating a CeedOperator where every field has a collocated basis. 1059*ea61e9acSJeremy L Thompson This function cannot be used for composite CeedOperators. 1060cd4dfc48Sjeremylt 1061*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1062*ea61e9acSJeremy L Thompson @param[in] num_qpts Number of quadrature points to set 1063cd4dfc48Sjeremylt 1064cd4dfc48Sjeremylt @return An error code: 0 - success, otherwise - failure 1065cd4dfc48Sjeremylt 1066e9b533fbSJeremy L Thompson @ref Advanced 1067cd4dfc48Sjeremylt **/ 1068cd4dfc48Sjeremylt int CeedOperatorSetNumQuadraturePoints(CeedOperator op, CeedInt num_qpts) { 10692b730f8bSJeremy L Thompson if (op->is_composite) { 1070cd4dfc48Sjeremylt // LCOV_EXCL_START 10712b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1072cd4dfc48Sjeremylt // LCOV_EXCL_STOP 10732b730f8bSJeremy L Thompson } 10742b730f8bSJeremy L Thompson if (op->num_qpts) { 1075cd4dfc48Sjeremylt // LCOV_EXCL_START 10762b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Number of quadrature points already defined"); 1077cd4dfc48Sjeremylt // LCOV_EXCL_STOP 10782b730f8bSJeremy L Thompson } 10792b730f8bSJeremy L Thompson if (op->is_immutable) { 1080f04ea552SJeremy L Thompson // LCOV_EXCL_START 10812b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 1082f04ea552SJeremy L Thompson // LCOV_EXCL_STOP 10832b730f8bSJeremy L Thompson } 1084cd4dfc48Sjeremylt op->num_qpts = num_qpts; 1085cd4dfc48Sjeremylt return CEED_ERROR_SUCCESS; 1086cd4dfc48Sjeremylt } 1087cd4dfc48Sjeremylt 1088cd4dfc48Sjeremylt /** 1089ea6b5821SJeremy L Thompson @brief Set name of CeedOperator for CeedOperatorView output 1090ea6b5821SJeremy L Thompson 1091*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1092*ea61e9acSJeremy L Thompson @param[in] name Name to set, or NULL to remove previously set name 1093ea6b5821SJeremy L Thompson 1094ea6b5821SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1095ea6b5821SJeremy L Thompson 1096ea6b5821SJeremy L Thompson @ref User 1097ea6b5821SJeremy L Thompson **/ 1098ea6b5821SJeremy L Thompson int CeedOperatorSetName(CeedOperator op, const char *name) { 1099ea6b5821SJeremy L Thompson char *name_copy; 1100ea6b5821SJeremy L Thompson size_t name_len = name ? strlen(name) : 0; 1101ea6b5821SJeremy L Thompson 11022b730f8bSJeremy L Thompson CeedCall(CeedFree(&op->name)); 1103ea6b5821SJeremy L Thompson if (name_len > 0) { 11042b730f8bSJeremy L Thompson CeedCall(CeedCalloc(name_len + 1, &name_copy)); 1105ea6b5821SJeremy L Thompson memcpy(name_copy, name, name_len); 1106ea6b5821SJeremy L Thompson op->name = name_copy; 1107ea6b5821SJeremy L Thompson } 1108ea6b5821SJeremy L Thompson 1109ea6b5821SJeremy L Thompson return CEED_ERROR_SUCCESS; 1110ea6b5821SJeremy L Thompson } 1111ea6b5821SJeremy L Thompson 1112ea6b5821SJeremy L Thompson /** 11137a982d89SJeremy L. Thompson @brief View a CeedOperator 11147a982d89SJeremy L. Thompson 11157a982d89SJeremy L. Thompson @param[in] op CeedOperator to view 11167a982d89SJeremy L. Thompson @param[in] stream Stream to write; typically stdout/stderr or a file 11177a982d89SJeremy L. Thompson 11187a982d89SJeremy L. Thompson @return Error code: 0 - success, otherwise - failure 11197a982d89SJeremy L. Thompson 11207a982d89SJeremy L. Thompson @ref User 11217a982d89SJeremy L. Thompson **/ 11227a982d89SJeremy L. Thompson int CeedOperatorView(CeedOperator op, FILE *stream) { 1123ea6b5821SJeremy L Thompson bool has_name = op->name; 11247a982d89SJeremy L. Thompson 1125f04ea552SJeremy L Thompson if (op->is_composite) { 11262b730f8bSJeremy L Thompson fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 11277a982d89SJeremy L. Thompson 1128d1d35e2fSjeremylt for (CeedInt i = 0; i < op->num_suboperators; i++) { 1129ea6b5821SJeremy L Thompson has_name = op->sub_operators[i]->name; 11302b730f8bSJeremy L Thompson fprintf(stream, " SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : ""); 11312b730f8bSJeremy L Thompson CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream)); 11327a982d89SJeremy L. Thompson } 11337a982d89SJeremy L. Thompson } else { 11342b730f8bSJeremy L Thompson fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 11352b730f8bSJeremy L Thompson CeedCall(CeedOperatorSingleView(op, 0, stream)); 11367a982d89SJeremy L. Thompson } 1137e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 11387a982d89SJeremy L. Thompson } 11393bd813ffSjeremylt 11403bd813ffSjeremylt /** 1141b7c9bbdaSJeremy L Thompson @brief Get the Ceed associated with a CeedOperator 1142b7c9bbdaSJeremy L Thompson 1143*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1144b7c9bbdaSJeremy L Thompson @param[out] ceed Variable to store Ceed 1145b7c9bbdaSJeremy L Thompson 1146b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1147b7c9bbdaSJeremy L Thompson 1148b7c9bbdaSJeremy L Thompson @ref Advanced 1149b7c9bbdaSJeremy L Thompson **/ 1150b7c9bbdaSJeremy L Thompson int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) { 1151b7c9bbdaSJeremy L Thompson *ceed = op->ceed; 1152b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1153b7c9bbdaSJeremy L Thompson } 1154b7c9bbdaSJeremy L Thompson 1155b7c9bbdaSJeremy L Thompson /** 1156b7c9bbdaSJeremy L Thompson @brief Get the number of elements associated with a CeedOperator 1157b7c9bbdaSJeremy L Thompson 1158*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1159b7c9bbdaSJeremy L Thompson @param[out] num_elem Variable to store number of elements 1160b7c9bbdaSJeremy L Thompson 1161b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1162b7c9bbdaSJeremy L Thompson 1163b7c9bbdaSJeremy L Thompson @ref Advanced 1164b7c9bbdaSJeremy L Thompson **/ 1165b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) { 11662b730f8bSJeremy L Thompson if (op->is_composite) { 1167b7c9bbdaSJeremy L Thompson // LCOV_EXCL_START 11682b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1169b7c9bbdaSJeremy L Thompson // LCOV_EXCL_STOP 11702b730f8bSJeremy L Thompson } 1171b7c9bbdaSJeremy L Thompson *num_elem = op->num_elem; 1172b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1173b7c9bbdaSJeremy L Thompson } 1174b7c9bbdaSJeremy L Thompson 1175b7c9bbdaSJeremy L Thompson /** 1176b7c9bbdaSJeremy L Thompson @brief Get the number of quadrature points associated with a CeedOperator 1177b7c9bbdaSJeremy L Thompson 1178*ea61e9acSJeremy L Thompson @param[in] op CeedOperator 1179b7c9bbdaSJeremy L Thompson @param[out] num_qpts Variable to store vector number of quadrature points 1180b7c9bbdaSJeremy L Thompson 1181b7c9bbdaSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1182b7c9bbdaSJeremy L Thompson 1183b7c9bbdaSJeremy L Thompson @ref Advanced 1184b7c9bbdaSJeremy L Thompson **/ 1185b7c9bbdaSJeremy L Thompson int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) { 11862b730f8bSJeremy L Thompson if (op->is_composite) { 1187b7c9bbdaSJeremy L Thompson // LCOV_EXCL_START 11882b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1189b7c9bbdaSJeremy L Thompson // LCOV_EXCL_STOP 11902b730f8bSJeremy L Thompson } 1191b7c9bbdaSJeremy L Thompson *num_qpts = op->num_qpts; 1192b7c9bbdaSJeremy L Thompson return CEED_ERROR_SUCCESS; 1193b7c9bbdaSJeremy L Thompson } 1194b7c9bbdaSJeremy L Thompson 1195b7c9bbdaSJeremy L Thompson /** 11966e15d496SJeremy L Thompson @brief Estimate number of FLOPs required to apply CeedOperator on the active vector 11976e15d496SJeremy L Thompson 1198*ea61e9acSJeremy L Thompson @param[in] op CeedOperator to estimate FLOPs for 1199*ea61e9acSJeremy L Thompson @param[out] flops Address of variable to hold FLOPs estimate 12006e15d496SJeremy L Thompson 12016e15d496SJeremy L Thompson @ref Backend 12026e15d496SJeremy L Thompson **/ 12039d36ca50SJeremy L Thompson int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { 12046e15d496SJeremy L Thompson bool is_composite; 12052b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 12066e15d496SJeremy L Thompson 12076e15d496SJeremy L Thompson *flops = 0; 12082b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 12096e15d496SJeremy L Thompson if (is_composite) { 12106e15d496SJeremy L Thompson CeedInt num_suboperators; 12112b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumSub(op, &num_suboperators)); 12126e15d496SJeremy L Thompson CeedOperator *sub_operators; 12132b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetSubList(op, &sub_operators)); 12146e15d496SJeremy L Thompson 12156e15d496SJeremy L Thompson // FLOPs for each suboperator 12166e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_suboperators; i++) { 12179d36ca50SJeremy L Thompson CeedSize suboperator_flops; 12182b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops)); 12196e15d496SJeremy L Thompson *flops += suboperator_flops; 12206e15d496SJeremy L Thompson } 12216e15d496SJeremy L Thompson } else { 12226e15d496SJeremy L Thompson CeedInt num_input_fields, num_output_fields; 12236e15d496SJeremy L Thompson CeedOperatorField *input_fields, *output_fields; 12246e15d496SJeremy L Thompson 12252b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 12266e15d496SJeremy L Thompson 12276e15d496SJeremy L Thompson CeedInt num_elem = 0; 12282b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 12296e15d496SJeremy L Thompson // Input FLOPs 12306e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 12316e15d496SJeremy L Thompson if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 12329d36ca50SJeremy L Thompson CeedSize restr_flops, basis_flops; 12336e15d496SJeremy L Thompson 12342b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_restr, CEED_NOTRANSPOSE, &restr_flops)); 12356e15d496SJeremy L Thompson *flops += restr_flops; 12362b730f8bSJeremy L Thompson CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops)); 12376e15d496SJeremy L Thompson *flops += basis_flops * num_elem; 12386e15d496SJeremy L Thompson } 12396e15d496SJeremy L Thompson } 12406e15d496SJeremy L Thompson // QF FLOPs 12419d36ca50SJeremy L Thompson CeedInt num_qpts; 12429d36ca50SJeremy L Thompson CeedSize qf_flops; 12432b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 12442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops)); 12456e15d496SJeremy L Thompson *flops += num_elem * num_qpts * qf_flops; 12466e15d496SJeremy L Thompson // Output FLOPs 12476e15d496SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 12486e15d496SJeremy L Thompson if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 12499d36ca50SJeremy L Thompson CeedSize restr_flops, basis_flops; 12506e15d496SJeremy L Thompson 12512b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_restr, CEED_TRANSPOSE, &restr_flops)); 12526e15d496SJeremy L Thompson *flops += restr_flops; 12532b730f8bSJeremy L Thompson CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops)); 12546e15d496SJeremy L Thompson *flops += basis_flops * num_elem; 12556e15d496SJeremy L Thompson } 12566e15d496SJeremy L Thompson } 12576e15d496SJeremy L Thompson } 12586e15d496SJeremy L Thompson 12596e15d496SJeremy L Thompson return CEED_ERROR_SUCCESS; 12606e15d496SJeremy L Thompson } 12616e15d496SJeremy L Thompson 12626e15d496SJeremy L Thompson /** 1263*ea61e9acSJeremy L Thompson @brief Get label for a registered QFunctionContext field, or `NULL` if no field has been registered with this `field_name`. 12643668ca4bSJeremy L Thompson 12653668ca4bSJeremy L Thompson @param[in] op CeedOperator 12663668ca4bSJeremy L Thompson @param[in] field_name Name of field to retrieve label 12673668ca4bSJeremy L Thompson @param[out] field_label Variable to field label 12683668ca4bSJeremy L Thompson 12693668ca4bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 12703668ca4bSJeremy L Thompson 12713668ca4bSJeremy L Thompson @ref User 12723668ca4bSJeremy L Thompson **/ 12732b730f8bSJeremy L Thompson int CeedOperatorContextGetFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) { 12743668ca4bSJeremy L Thompson bool is_composite; 12752b730f8bSJeremy L Thompson CeedCall(CeedOperatorIsComposite(op, &is_composite)); 12762b730f8bSJeremy L Thompson 12773668ca4bSJeremy L Thompson if (is_composite) { 12783668ca4bSJeremy L Thompson // Check if composite label already created 12793668ca4bSJeremy L Thompson for (CeedInt i = 0; i < op->num_context_labels; i++) { 12803668ca4bSJeremy L Thompson if (!strcmp(op->context_labels[i]->name, field_name)) { 12813668ca4bSJeremy L Thompson *field_label = op->context_labels[i]; 12823668ca4bSJeremy L Thompson return CEED_ERROR_SUCCESS; 12833668ca4bSJeremy L Thompson } 12843668ca4bSJeremy L Thompson } 12853668ca4bSJeremy L Thompson 12863668ca4bSJeremy L Thompson // Create composite label if needed 12873668ca4bSJeremy L Thompson CeedInt num_sub; 12883668ca4bSJeremy L Thompson CeedOperator *sub_operators; 12893668ca4bSJeremy L Thompson CeedContextFieldLabel new_field_label; 12903668ca4bSJeremy L Thompson 12912b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &new_field_label)); 12922b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumSub(op, &num_sub)); 12932b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetSubList(op, &sub_operators)); 12942b730f8bSJeremy L Thompson CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels)); 12953668ca4bSJeremy L Thompson new_field_label->num_sub_labels = num_sub; 12963668ca4bSJeremy L Thompson 12973668ca4bSJeremy L Thompson bool label_found = false; 12983668ca4bSJeremy L Thompson for (CeedInt i = 0; i < num_sub; i++) { 12993668ca4bSJeremy L Thompson if (sub_operators[i]->qf->ctx) { 13003668ca4bSJeremy L Thompson CeedContextFieldLabel new_field_label_i; 13012b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i)); 13023668ca4bSJeremy L Thompson if (new_field_label_i) { 13033668ca4bSJeremy L Thompson label_found = true; 13043668ca4bSJeremy L Thompson new_field_label->sub_labels[i] = new_field_label_i; 13053668ca4bSJeremy L Thompson new_field_label->name = new_field_label_i->name; 13063668ca4bSJeremy L Thompson new_field_label->description = new_field_label_i->description; 13072b730f8bSJeremy L Thompson if (new_field_label->type && new_field_label->type != new_field_label_i->type) { 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 types on sub-operator contexts. %s != %s", 13112b730f8bSJeremy L Thompson CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]); 13127bfe0f0eSJeremy L Thompson // LCOV_EXCL_STOP 13137bfe0f0eSJeremy L Thompson } else { 13147bfe0f0eSJeremy L Thompson new_field_label->type = new_field_label_i->type; 13157bfe0f0eSJeremy L Thompson } 13162b730f8bSJeremy L Thompson if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) { 13177bfe0f0eSJeremy L Thompson // LCOV_EXCL_START 13182b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label)); 13192b730f8bSJeremy L Thompson return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld", 13207bfe0f0eSJeremy L Thompson new_field_label->num_values, new_field_label_i->num_values); 13217bfe0f0eSJeremy L Thompson // LCOV_EXCL_STOP 13227bfe0f0eSJeremy L Thompson } else { 13237bfe0f0eSJeremy L Thompson new_field_label->num_values = new_field_label_i->num_values; 13247bfe0f0eSJeremy L Thompson } 13253668ca4bSJeremy L Thompson } 13263668ca4bSJeremy L Thompson } 13273668ca4bSJeremy L Thompson } 13283668ca4bSJeremy L Thompson if (!label_found) { 13293668ca4bSJeremy L Thompson // LCOV_EXCL_START 13302b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label->sub_labels)); 13312b730f8bSJeremy L Thompson CeedCall(CeedFree(&new_field_label)); 13323668ca4bSJeremy L Thompson *field_label = NULL; 13333668ca4bSJeremy L Thompson // LCOV_EXCL_STOP 13343668ca4bSJeremy L Thompson } else { 13353668ca4bSJeremy L Thompson // Move new composite label to operator 13363668ca4bSJeremy L Thompson if (op->num_context_labels == 0) { 13372b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &op->context_labels)); 13383668ca4bSJeremy L Thompson op->max_context_labels = 1; 13393668ca4bSJeremy L Thompson } else if (op->num_context_labels == op->max_context_labels) { 13402b730f8bSJeremy L Thompson CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels)); 13413668ca4bSJeremy L Thompson op->max_context_labels *= 2; 13423668ca4bSJeremy L Thompson } 13433668ca4bSJeremy L Thompson op->context_labels[op->num_context_labels] = new_field_label; 13443668ca4bSJeremy L Thompson *field_label = new_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 } else { 13503668ca4bSJeremy L Thompson return CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label); 13513668ca4bSJeremy L Thompson } 13523668ca4bSJeremy L Thompson } 13533668ca4bSJeremy L Thompson 13543668ca4bSJeremy L Thompson /** 1355d8dd9a91SJeremy L Thompson @brief Set QFunctionContext field holding a double precision value. 1356*ea61e9acSJeremy L Thompson For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`. 1357d8dd9a91SJeremy L Thompson 1358*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1359*ea61e9acSJeremy L Thompson @param[in] field_label Label of field to register 1360*ea61e9acSJeremy L Thompson @param[in] values Values to set 1361d8dd9a91SJeremy L Thompson 1362d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1363d8dd9a91SJeremy L Thompson 1364d8dd9a91SJeremy L Thompson @ref User 1365d8dd9a91SJeremy L Thompson **/ 13662b730f8bSJeremy L Thompson int CeedOperatorContextSetDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) { 13672b730f8bSJeremy L Thompson return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1368d8dd9a91SJeremy L Thompson } 1369d8dd9a91SJeremy L Thompson 1370d8dd9a91SJeremy L Thompson /** 1371d8dd9a91SJeremy L Thompson @brief Set QFunctionContext field holding an int32 value. 1372*ea61e9acSJeremy L Thompson For composite operators, the value is set in all sub-operator QFunctionContexts that have a matching `field_name`. 1373d8dd9a91SJeremy L Thompson 1374*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator 1375*ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 1376*ea61e9acSJeremy L Thompson @param[in] values Values to set 1377d8dd9a91SJeremy L Thompson 1378d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1379d8dd9a91SJeremy L Thompson 1380d8dd9a91SJeremy L Thompson @ref User 1381d8dd9a91SJeremy L Thompson **/ 13822b730f8bSJeremy L Thompson int CeedOperatorContextSetInt32(CeedOperator op, CeedContextFieldLabel field_label, int *values) { 13832b730f8bSJeremy L Thompson return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1384d8dd9a91SJeremy L Thompson } 1385d8dd9a91SJeremy L Thompson 1386d8dd9a91SJeremy L Thompson /** 13873bd813ffSjeremylt @brief Apply CeedOperator to a vector 1388d7b241e6Sjeremylt 1389*ea61e9acSJeremy L Thompson This computes the action of the operator on the specified (active) input, yielding its (active) output. 1390*ea61e9acSJeremy L Thompson All inputs and outputs must be specified using CeedOperatorSetField(). 1391d7b241e6Sjeremylt 1392*ea61e9acSJeremy L Thompson Note: Calling this function asserts that setup is complete and sets the CeedOperator as immutable. 1393f04ea552SJeremy L Thompson 1394*ea61e9acSJeremy L Thompson @param[in] op CeedOperator to apply 1395*ea61e9acSJeremy L Thompson @param[in] in CeedVector containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 1396*ea61e9acSJeremy 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 active 1397*ea61e9acSJeremy L Thompson outputs 1398*ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1399b11c1e72Sjeremylt 1400b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1401dfdf5a53Sjeremylt 14027a982d89SJeremy L. Thompson @ref User 1403b11c1e72Sjeremylt **/ 14042b730f8bSJeremy L Thompson int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 14052b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1406d7b241e6Sjeremylt 1407d1d35e2fSjeremylt if (op->num_elem) { 1408250756a7Sjeremylt // Standard Operator 1409cae8b89aSjeremylt if (op->Apply) { 14102b730f8bSJeremy L Thompson CeedCall(op->Apply(op, in, out, request)); 1411cae8b89aSjeremylt } else { 1412cae8b89aSjeremylt // Zero all output vectors 1413250756a7Sjeremylt CeedQFunction qf = op->qf; 1414d1d35e2fSjeremylt for (CeedInt i = 0; i < qf->num_output_fields; i++) { 1415d1d35e2fSjeremylt CeedVector vec = op->output_fields[i]->vec; 14162b730f8bSJeremy L Thompson if (vec == CEED_VECTOR_ACTIVE) vec = out; 1417cae8b89aSjeremylt if (vec != CEED_VECTOR_NONE) { 14182b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(vec, 0.0)); 1419cae8b89aSjeremylt } 1420cae8b89aSjeremylt } 1421250756a7Sjeremylt // Apply 14222b730f8bSJeremy L Thompson CeedCall(op->ApplyAdd(op, in, out, request)); 1423250756a7Sjeremylt } 1424f04ea552SJeremy L Thompson } else if (op->is_composite) { 1425250756a7Sjeremylt // Composite Operator 1426250756a7Sjeremylt if (op->ApplyComposite) { 14272b730f8bSJeremy L Thompson CeedCall(op->ApplyComposite(op, in, out, request)); 1428250756a7Sjeremylt } else { 1429d1d35e2fSjeremylt CeedInt num_suboperators; 14302b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumSub(op, &num_suboperators)); 1431d1d35e2fSjeremylt CeedOperator *sub_operators; 14322b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetSubList(op, &sub_operators)); 1433250756a7Sjeremylt 1434250756a7Sjeremylt // Zero all output vectors 1435250756a7Sjeremylt if (out != CEED_VECTOR_NONE) { 14362b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(out, 0.0)); 1437cae8b89aSjeremylt } 1438d1d35e2fSjeremylt for (CeedInt i = 0; i < num_suboperators; i++) { 1439d1d35e2fSjeremylt for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) { 1440d1d35e2fSjeremylt CeedVector vec = sub_operators[i]->output_fields[j]->vec; 1441250756a7Sjeremylt if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) { 14422b730f8bSJeremy L Thompson CeedCall(CeedVectorSetValue(vec, 0.0)); 1443250756a7Sjeremylt } 1444250756a7Sjeremylt } 1445250756a7Sjeremylt } 1446250756a7Sjeremylt // Apply 1447d1d35e2fSjeremylt for (CeedInt i = 0; i < op->num_suboperators; i++) { 14482b730f8bSJeremy L Thompson CeedCall(CeedOperatorApplyAdd(op->sub_operators[i], in, out, request)); 1449cae8b89aSjeremylt } 1450cae8b89aSjeremylt } 1451250756a7Sjeremylt } 1452e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1453cae8b89aSjeremylt } 1454cae8b89aSjeremylt 1455cae8b89aSjeremylt /** 1456cae8b89aSjeremylt @brief Apply CeedOperator to a vector and add result to output vector 1457cae8b89aSjeremylt 1458*ea61e9acSJeremy L Thompson This computes the action of the operator on the specified (active) input, yielding its (active) output. 1459*ea61e9acSJeremy L Thompson All inputs and outputs must be specified using CeedOperatorSetField(). 1460cae8b89aSjeremylt 1461*ea61e9acSJeremy L Thompson @param[in] op CeedOperator to apply 1462*ea61e9acSJeremy L Thompson @param[in] in CeedVector containing input state or NULL if there are no active inputs 1463*ea61e9acSJeremy 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 1464*ea61e9acSJeremy L Thompson @param[in] request Address of CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1465cae8b89aSjeremylt 1466cae8b89aSjeremylt @return An error code: 0 - success, otherwise - failure 1467cae8b89aSjeremylt 14687a982d89SJeremy L. Thompson @ref User 1469cae8b89aSjeremylt **/ 14702b730f8bSJeremy L Thompson int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 14712b730f8bSJeremy L Thompson CeedCall(CeedOperatorCheckReady(op)); 1472cae8b89aSjeremylt 1473d1d35e2fSjeremylt if (op->num_elem) { 1474250756a7Sjeremylt // Standard Operator 14752b730f8bSJeremy L Thompson CeedCall(op->ApplyAdd(op, in, out, request)); 1476f04ea552SJeremy L Thompson } else if (op->is_composite) { 1477250756a7Sjeremylt // Composite Operator 1478250756a7Sjeremylt if (op->ApplyAddComposite) { 14792b730f8bSJeremy L Thompson CeedCall(op->ApplyAddComposite(op, in, out, request)); 1480cae8b89aSjeremylt } else { 1481d1d35e2fSjeremylt CeedInt num_suboperators; 14822b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetNumSub(op, &num_suboperators)); 1483d1d35e2fSjeremylt CeedOperator *sub_operators; 14842b730f8bSJeremy L Thompson CeedCall(CeedOperatorGetSubList(op, &sub_operators)); 1485250756a7Sjeremylt 1486d1d35e2fSjeremylt for (CeedInt i = 0; i < num_suboperators; i++) { 14872b730f8bSJeremy L Thompson CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 14881d7d2407SJeremy L Thompson } 1489250756a7Sjeremylt } 1490250756a7Sjeremylt } 1491e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1492d7b241e6Sjeremylt } 1493d7b241e6Sjeremylt 1494d7b241e6Sjeremylt /** 1495b11c1e72Sjeremylt @brief Destroy a CeedOperator 1496d7b241e6Sjeremylt 1497*ea61e9acSJeremy L Thompson @param[in,out] op CeedOperator to destroy 1498b11c1e72Sjeremylt 1499b11c1e72Sjeremylt @return An error code: 0 - success, otherwise - failure 1500dfdf5a53Sjeremylt 15017a982d89SJeremy L. Thompson @ref User 1502b11c1e72Sjeremylt **/ 1503d7b241e6Sjeremylt int CeedOperatorDestroy(CeedOperator *op) { 1504d1d35e2fSjeremylt if (!*op || --(*op)->ref_count > 0) return CEED_ERROR_SUCCESS; 15052b730f8bSJeremy L Thompson if ((*op)->Destroy) CeedCall((*op)->Destroy(*op)); 15062b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*op)->ceed)); 1507fe2413ffSjeremylt // Free fields 15082b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1509d1d35e2fSjeremylt if ((*op)->input_fields[i]) { 1510d1d35e2fSjeremylt if ((*op)->input_fields[i]->elem_restr != CEED_ELEMRESTRICTION_NONE) { 15112b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_restr)); 151215910d16Sjeremylt } 1513d1d35e2fSjeremylt if ((*op)->input_fields[i]->basis != CEED_BASIS_COLLOCATED) { 15142b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis)); 151571352a93Sjeremylt } 15162b730f8bSJeremy L Thompson if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) { 15172b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec)); 151871352a93Sjeremylt } 15192b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields[i]->field_name)); 15202b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields[i])); 1521fe2413ffSjeremylt } 15222b730f8bSJeremy L Thompson } 15232b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1524d1d35e2fSjeremylt if ((*op)->output_fields[i]) { 15252b730f8bSJeremy L Thompson CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_restr)); 1526d1d35e2fSjeremylt if ((*op)->output_fields[i]->basis != CEED_BASIS_COLLOCATED) { 15272b730f8bSJeremy L Thompson CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis)); 152871352a93Sjeremylt } 15292b730f8bSJeremy L Thompson if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) { 15302b730f8bSJeremy L Thompson CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec)); 153171352a93Sjeremylt } 15322b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields[i]->field_name)); 15332b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields[i])); 15342b730f8bSJeremy L Thompson } 1535fe2413ffSjeremylt } 1536d1d35e2fSjeremylt // Destroy sub_operators 15372b730f8bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_suboperators; i++) { 1538d1d35e2fSjeremylt if ((*op)->sub_operators[i]) { 15392b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i])); 154052d6035fSJeremy L Thompson } 15412b730f8bSJeremy L Thompson } 15422b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->qf)); 15432b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->dqf)); 15442b730f8bSJeremy L Thompson CeedCall(CeedQFunctionDestroy(&(*op)->dqfT)); 15453668ca4bSJeremy L Thompson // Destroy any composite labels 15463668ca4bSJeremy L Thompson for (CeedInt i = 0; i < (*op)->num_context_labels; i++) { 15472b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels)); 15482b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels[i])); 15493668ca4bSJeremy L Thompson } 15502b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->context_labels)); 1551fe2413ffSjeremylt 15525107b09fSJeremy L Thompson // Destroy fallback 15532b730f8bSJeremy L Thompson CeedCall(CeedOperatorDestroy(&(*op)->op_fallback)); 15545107b09fSJeremy L Thompson 1555ed9e99e6SJeremy L Thompson // Destroy assembly data 15562b730f8bSJeremy L Thompson CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled)); 15572b730f8bSJeremy L Thompson CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled)); 155870a7ffb3SJeremy L Thompson 15592b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->input_fields)); 15602b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->output_fields)); 15612b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->sub_operators)); 15622b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*op)->name)); 15632b730f8bSJeremy L Thompson CeedCall(CeedFree(op)); 1564e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1565d7b241e6Sjeremylt } 1566d7b241e6Sjeremylt 1567d7b241e6Sjeremylt /// @} 1568