1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 #include <ceed-impl.h> 9 #include <ceed.h> 10 #include <ceed/backend.h> 11 #include <stdbool.h> 12 #include <stdio.h> 13 #include <string.h> 14 15 /// @file 16 /// Implementation of CeedOperator interfaces 17 18 /// ---------------------------------------------------------------------------- 19 /// CeedOperator Library Internal Functions 20 /// ---------------------------------------------------------------------------- 21 /// @addtogroup CeedOperatorDeveloper 22 /// @{ 23 24 /** 25 @brief Check if a `CeedOperator` Field matches the `CeedQFunction` Field 26 27 @param[in] ceed `Ceed` object for error handling 28 @param[in] qf_field `CeedQFunction` Field matching `CeedOperator` Field 29 @param[in] rstr `CeedOperator` Field `CeedElemRestriction` 30 @param[in] basis `CeedOperator` Field `CeedBasis` 31 32 @return An error code: 0 - success, otherwise - failure 33 34 @ref Developer 35 **/ 36 static int CeedOperatorCheckField(Ceed ceed, CeedQFunctionField qf_field, CeedElemRestriction rstr, CeedBasis basis) { 37 CeedInt dim = 1, num_comp = 1, q_comp = 1, rstr_num_comp = 1, size = qf_field->size; 38 CeedEvalMode eval_mode = qf_field->eval_mode; 39 40 // Restriction 41 CeedCheck((rstr == CEED_ELEMRESTRICTION_NONE) == (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_INCOMPATIBLE, 42 "CEED_ELEMRESTRICTION_NONE and CEED_EVAL_WEIGHT must be used together."); 43 if (rstr != CEED_ELEMRESTRICTION_NONE) { 44 CeedCall(CeedElemRestrictionGetNumComponents(rstr, &rstr_num_comp)); 45 } 46 // Basis 47 CeedCheck((basis == CEED_BASIS_NONE) == (eval_mode == CEED_EVAL_NONE), ceed, CEED_ERROR_INCOMPATIBLE, 48 "CEED_BASIS_NONE and CEED_EVAL_NONE must be used together."); 49 if (basis != CEED_BASIS_NONE) { 50 CeedCall(CeedBasisGetDimension(basis, &dim)); 51 CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); 52 CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 53 CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || rstr_num_comp == num_comp, ceed, CEED_ERROR_DIMENSION, 54 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT 55 " components, but CeedBasis has %" CeedInt_FMT " components", 56 qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp, num_comp); 57 } 58 // Field size 59 switch (eval_mode) { 60 case CEED_EVAL_NONE: 61 CeedCheck(size == rstr_num_comp, ceed, CEED_ERROR_DIMENSION, 62 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction has %" CeedInt_FMT " components", qf_field->field_name, 63 qf_field->size, CeedEvalModes[qf_field->eval_mode], rstr_num_comp); 64 break; 65 case CEED_EVAL_INTERP: 66 case CEED_EVAL_GRAD: 67 case CEED_EVAL_DIV: 68 case CEED_EVAL_CURL: 69 CeedCheck(size == num_comp * q_comp, ceed, CEED_ERROR_DIMENSION, 70 "Field '%s' of size %" CeedInt_FMT " and EvalMode %s: CeedElemRestriction/Basis has %" CeedInt_FMT " components", 71 qf_field->field_name, qf_field->size, CeedEvalModes[qf_field->eval_mode], num_comp * q_comp); 72 break; 73 case CEED_EVAL_WEIGHT: 74 // No additional checks required 75 break; 76 } 77 return CEED_ERROR_SUCCESS; 78 } 79 80 /** 81 @brief View a field of a `CeedOperator` 82 83 @param[in] field `CeedOperator` Field to view 84 @param[in] qf_field `CeedQFunction` Field (carries field name) 85 @param[in] field_number Number of field being viewed 86 @param[in] sub true indicates sub-operator, which increases indentation; false for top-level operator 87 @param[in] input true for an input field; false for output field 88 @param[in] stream Stream to view to, e.g., `stdout` 89 90 @return An error code: 0 - success, otherwise - failure 91 92 @ref Utility 93 **/ 94 static int CeedOperatorFieldView(CeedOperatorField field, CeedQFunctionField qf_field, CeedInt field_number, bool sub, bool input, FILE *stream) { 95 const char *pre = sub ? " " : ""; 96 const char *in_out = input ? "Input" : "Output"; 97 98 fprintf(stream, 99 "%s %s field %" CeedInt_FMT 100 ":\n" 101 "%s Name: \"%s\"\n", 102 pre, in_out, field_number, pre, qf_field->field_name); 103 fprintf(stream, "%s Size: %" CeedInt_FMT "\n", pre, qf_field->size); 104 fprintf(stream, "%s EvalMode: %s\n", pre, CeedEvalModes[qf_field->eval_mode]); 105 if (field->basis == CEED_BASIS_NONE) fprintf(stream, "%s No basis\n", pre); 106 if (field->vec == CEED_VECTOR_ACTIVE) fprintf(stream, "%s Active vector\n", pre); 107 else if (field->vec == CEED_VECTOR_NONE) fprintf(stream, "%s No vector\n", pre); 108 return CEED_ERROR_SUCCESS; 109 } 110 111 /** 112 @brief View a single `CeedOperator` 113 114 @param[in] op `CeedOperator` to view 115 @param[in] sub Boolean flag for sub-operator 116 @param[in] stream Stream to write; typically `stdout` or a file 117 118 @return Error code: 0 - success, otherwise - failure 119 120 @ref Utility 121 **/ 122 int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) { 123 const char *pre = sub ? " " : ""; 124 CeedInt num_elem, num_qpts, total_fields = 0; 125 126 CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 127 CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 128 CeedCall(CeedOperatorGetNumArgs(op, &total_fields)); 129 130 fprintf(stream, "%s %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts); 131 fprintf(stream, "%s %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : ""); 132 fprintf(stream, "%s %" CeedInt_FMT " input field%s:\n", pre, op->qf->num_input_fields, op->qf->num_input_fields > 1 ? "s" : ""); 133 for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 134 CeedCall(CeedOperatorFieldView(op->input_fields[i], op->qf->input_fields[i], i, sub, 1, stream)); 135 } 136 fprintf(stream, "%s %" CeedInt_FMT " output field%s:\n", pre, op->qf->num_output_fields, op->qf->num_output_fields > 1 ? "s" : ""); 137 for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 138 CeedCall(CeedOperatorFieldView(op->output_fields[i], op->qf->output_fields[i], i, sub, 0, stream)); 139 } 140 return CEED_ERROR_SUCCESS; 141 } 142 143 /** 144 @brief Find the active input vector `CeedBasis` for a non-composite `CeedOperator` 145 146 @param[in] op `CeedOperator` to find active `CeedBasis` for 147 @param[out] active_basis `CeedBasis` for active input vector or `NULL` for composite operator 148 149 @return An error code: 0 - success, otherwise - failure 150 151 @ref Developer 152 **/ 153 int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) { 154 CeedCall(CeedOperatorGetActiveBases(op, active_basis, NULL)); 155 return CEED_ERROR_SUCCESS; 156 } 157 158 /** 159 @brief Find the active input and output vector `CeedBasis` for a non-composite `CeedOperator` 160 161 @param[in] op `CeedOperator` to find active `CeedBasis` for 162 @param[out] active_input_basis `CeedBasis` for active input vector or `NULL` for composite operator 163 @param[out] active_output_basis `CeedBasis` for active output vector or `NULL` for composite operator 164 165 @return An error code: 0 - success, otherwise - failure 166 167 @ref Developer 168 **/ 169 int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) { 170 Ceed ceed; 171 172 CeedCall(CeedOperatorGetCeed(op, &ceed)); 173 if (active_input_basis) { 174 *active_input_basis = NULL; 175 if (!op->is_composite) { 176 for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 177 if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 178 CeedCheck(!*active_input_basis || *active_input_basis == op->input_fields[i]->basis, ceed, CEED_ERROR_MINOR, 179 "Multiple active input CeedBases found"); 180 *active_input_basis = op->input_fields[i]->basis; 181 } 182 } 183 CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found"); 184 } 185 } 186 if (active_output_basis) { 187 *active_output_basis = NULL; 188 if (!op->is_composite) { 189 for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 190 if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 191 CeedCheck(!*active_output_basis || *active_output_basis == op->output_fields[i]->basis, ceed, CEED_ERROR_MINOR, 192 "Multiple active output CeedBases found"); 193 *active_output_basis = op->output_fields[i]->basis; 194 } 195 } 196 CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found"); 197 } 198 } 199 return CEED_ERROR_SUCCESS; 200 } 201 202 /** 203 @brief Find the active vector `CeedElemRestriction` for a non-composite `CeedOperator` 204 205 @param[in] op `CeedOperator` to find active `CeedElemRestriction` for 206 @param[out] active_rstr `CeedElemRestriction` for active input vector or NULL for composite operator 207 208 @return An error code: 0 - success, otherwise - failure 209 210 @ref Utility 211 **/ 212 int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *active_rstr) { 213 CeedCall(CeedOperatorGetActiveElemRestrictions(op, active_rstr, NULL)); 214 return CEED_ERROR_SUCCESS; 215 } 216 217 /** 218 @brief Find the active input and output vector `CeedElemRestriction` for a non-composite `CeedOperator` 219 220 @param[in] op `CeedOperator` to find active `CeedElemRestriction` for 221 @param[out] active_input_rstr `CeedElemRestriction` for active input vector or NULL for composite operator 222 @param[out] active_output_rstr `CeedElemRestriction` for active output vector or NULL for composite operator 223 224 @return An error code: 0 - success, otherwise - failure 225 226 @ref Utility 227 **/ 228 int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) { 229 Ceed ceed; 230 231 CeedCall(CeedOperatorGetCeed(op, &ceed)); 232 if (active_input_rstr) { 233 *active_input_rstr = NULL; 234 if (!op->is_composite) { 235 for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 236 if (op->input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 237 CeedCheck(!*active_input_rstr || *active_input_rstr == op->input_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR, 238 "Multiple active input CeedElemRestrictions found"); 239 *active_input_rstr = op->input_fields[i]->elem_rstr; 240 } 241 } 242 CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found"); 243 } 244 } 245 if (active_output_rstr) { 246 *active_output_rstr = NULL; 247 if (!op->is_composite) { 248 for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 249 if (op->output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 250 CeedCheck(!*active_output_rstr || *active_output_rstr == op->output_fields[i]->elem_rstr, ceed, CEED_ERROR_MINOR, 251 "Multiple active output CeedElemRestrictions found"); 252 *active_output_rstr = op->output_fields[i]->elem_rstr; 253 } 254 } 255 CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found"); 256 } 257 } 258 return CEED_ERROR_SUCCESS; 259 } 260 261 /** 262 @brief Set `CeedQFunctionContext` field values of the specified type. 263 264 For composite operators, the value is set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 265 A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type. 266 267 @param[in,out] op `CeedOperator` 268 @param[in] field_label Label of field to set 269 @param[in] field_type Type of field to set 270 @param[in] values Values to set 271 272 @return An error code: 0 - success, otherwise - failure 273 274 @ref User 275 **/ 276 static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 277 bool is_composite = false; 278 279 CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 280 281 // Check if field_label and op correspond 282 if (field_label->from_op) { 283 CeedInt index = -1; 284 285 for (CeedInt i = 0; i < op->num_context_labels; i++) { 286 if (op->context_labels[i] == field_label) index = i; 287 } 288 CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 289 } 290 291 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 292 if (is_composite) { 293 CeedInt num_sub; 294 CeedOperator *sub_operators; 295 296 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 297 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 298 CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED, 299 "Composite operator modified after ContextFieldLabel created"); 300 301 for (CeedInt i = 0; i < num_sub; i++) { 302 // Try every sub-operator, ok if some sub-operators do not have field 303 if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 304 CeedCall(CeedQFunctionContextSetGeneric(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values)); 305 } 306 } 307 } else { 308 CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 309 CeedCall(CeedQFunctionContextSetGeneric(op->qf->ctx, field_label, field_type, values)); 310 } 311 CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true)); 312 return CEED_ERROR_SUCCESS; 313 } 314 315 /** 316 @brief Get `CeedQFunctionContext` field values of the specified type, read-only. 317 318 For composite operators, the values retrieved are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`. 319 A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type. 320 321 @param[in,out] op `CeedOperator` 322 @param[in] field_label Label of field to set 323 @param[in] field_type Type of field to set 324 @param[out] num_values Number of values of type `field_type` in array `values` 325 @param[out] values Values in the label 326 327 @return An error code: 0 - success, otherwise - failure 328 329 @ref User 330 **/ 331 static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values, 332 void *values) { 333 bool is_composite = false; 334 335 CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 336 337 *(void **)values = NULL; 338 *num_values = 0; 339 340 // Check if field_label and op correspond 341 if (field_label->from_op) { 342 CeedInt index = -1; 343 344 for (CeedInt i = 0; i < op->num_context_labels; i++) { 345 if (op->context_labels[i] == field_label) index = i; 346 } 347 CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 348 } 349 350 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 351 if (is_composite) { 352 CeedInt num_sub; 353 CeedOperator *sub_operators; 354 355 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 356 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 357 CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED, 358 "Composite operator modified after ContextFieldLabel created"); 359 360 for (CeedInt i = 0; i < num_sub; i++) { 361 // Try every sub-operator, ok if some sub-operators do not have field 362 if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 363 CeedCall(CeedQFunctionContextGetGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, num_values, values)); 364 return CEED_ERROR_SUCCESS; 365 } 366 } 367 } else { 368 CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 369 CeedCall(CeedQFunctionContextGetGenericRead(op->qf->ctx, field_label, field_type, num_values, values)); 370 } 371 return CEED_ERROR_SUCCESS; 372 } 373 374 /** 375 @brief Restore `CeedQFunctionContext` field values of the specified type, read-only. 376 377 For composite operators, the values restored are for the first sub-operator `CeedQFunctionContext` that have a matching `field_name`. 378 A non-zero error code is returned for single operators that do not have a matching field of the same type or composite operators that do not have any field of a matching type. 379 380 @param[in,out] op `CeedOperator` 381 @param[in] field_label Label of field to set 382 @param[in] field_type Type of field to set 383 @param[in] values Values array to restore 384 385 @return An error code: 0 - success, otherwise - failure 386 387 @ref User 388 **/ 389 static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 390 bool is_composite = false; 391 392 CeedCheck(field_label, op->ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); 393 394 // Check if field_label and op correspond 395 if (field_label->from_op) { 396 CeedInt index = -1; 397 398 for (CeedInt i = 0; i < op->num_context_labels; i++) { 399 if (op->context_labels[i] == field_label) index = i; 400 } 401 CeedCheck(index != -1, op->ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); 402 } 403 404 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 405 if (is_composite) { 406 CeedInt num_sub; 407 CeedOperator *sub_operators; 408 409 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 410 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 411 CeedCheck(num_sub == field_label->num_sub_labels, op->ceed, CEED_ERROR_UNSUPPORTED, 412 "Composite operator modified after ContextFieldLabel created"); 413 414 for (CeedInt i = 0; i < num_sub; i++) { 415 // Try every sub-operator, ok if some sub-operators do not have field 416 if (field_label->sub_labels[i] && sub_operators[i]->qf->ctx) { 417 CeedCall(CeedQFunctionContextRestoreGenericRead(sub_operators[i]->qf->ctx, field_label->sub_labels[i], field_type, values)); 418 return CEED_ERROR_SUCCESS; 419 } 420 } 421 } else { 422 CeedCheck(op->qf->ctx, op->ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); 423 CeedCall(CeedQFunctionContextRestoreGenericRead(op->qf->ctx, field_label, field_type, values)); 424 } 425 return CEED_ERROR_SUCCESS; 426 } 427 428 /// @} 429 430 /// ---------------------------------------------------------------------------- 431 /// CeedOperator Backend API 432 /// ---------------------------------------------------------------------------- 433 /// @addtogroup CeedOperatorBackend 434 /// @{ 435 436 /** 437 @brief Get the number of arguments associated with a `CeedOperator` 438 439 @param[in] op `CeedOperator` 440 @param[out] num_args Variable to store vector number of arguments 441 442 @return An error code: 0 - success, otherwise - failure 443 444 @ref Backend 445 **/ 446 int CeedOperatorGetNumArgs(CeedOperator op, CeedInt *num_args) { 447 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operators"); 448 *num_args = op->num_fields; 449 return CEED_ERROR_SUCCESS; 450 } 451 452 /** 453 @brief Get the tensor product status of all bases for a `CeedOperator`. 454 455 `has_tensor_bases` is only set to `true` if every field uses a tensor-product basis. 456 457 @param[in] op `CeedOperator` 458 @param[out] has_tensor_bases Variable to store tensor bases status 459 460 @return An error code: 0 - success, otherwise - failure 461 462 @ref Backend 463 **/ 464 int CeedOperatorHasTensorBases(CeedOperator op, bool *has_tensor_bases) { 465 CeedInt num_inputs, num_outputs; 466 CeedOperatorField *input_fields, *output_fields; 467 468 CeedCall(CeedOperatorGetFields(op, &num_inputs, &input_fields, &num_outputs, &output_fields)); 469 *has_tensor_bases = true; 470 for (CeedInt i = 0; i < num_inputs; i++) { 471 bool is_tensor; 472 CeedBasis basis; 473 474 CeedCall(CeedOperatorFieldGetBasis(input_fields[i], &basis)); 475 if (basis != CEED_BASIS_NONE) { 476 CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 477 *has_tensor_bases &= is_tensor; 478 } 479 } 480 for (CeedInt i = 0; i < num_outputs; i++) { 481 bool is_tensor; 482 CeedBasis basis; 483 484 CeedCall(CeedOperatorFieldGetBasis(output_fields[i], &basis)); 485 if (basis != CEED_BASIS_NONE) { 486 CeedCall(CeedBasisIsTensor(basis, &is_tensor)); 487 *has_tensor_bases &= is_tensor; 488 } 489 } 490 return CEED_ERROR_SUCCESS; 491 } 492 493 /** 494 @brief Get the setup status of a `CeedOperator` 495 496 @param[in] op `CeedOperator` 497 @param[out] is_setup_done Variable to store setup status 498 499 @return An error code: 0 - success, otherwise - failure 500 501 @ref Backend 502 **/ 503 int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) { 504 *is_setup_done = op->is_backend_setup; 505 return CEED_ERROR_SUCCESS; 506 } 507 508 /** 509 @brief Get the `CeedQFunction` associated with a `CeedOperator` 510 511 @param[in] op `CeedOperator` 512 @param[out] qf Variable to store `CeedQFunction` 513 514 @return An error code: 0 - success, otherwise - failure 515 516 @ref Backend 517 **/ 518 int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) { 519 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 520 *qf = op->qf; 521 return CEED_ERROR_SUCCESS; 522 } 523 524 /** 525 @brief Get a boolean value indicating if the `CeedOperator` is composite 526 527 @param[in] op `CeedOperator` 528 @param[out] is_composite Variable to store composite status 529 530 @return An error code: 0 - success, otherwise - failure 531 532 @ref Backend 533 **/ 534 int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) { 535 *is_composite = op->is_composite; 536 return CEED_ERROR_SUCCESS; 537 } 538 539 /** 540 @brief Get the backend data of a `CeedOperator` 541 542 @param[in] op `CeedOperator` 543 @param[out] data Variable to store data 544 545 @return An error code: 0 - success, otherwise - failure 546 547 @ref Backend 548 **/ 549 int CeedOperatorGetData(CeedOperator op, void *data) { 550 *(void **)data = op->data; 551 return CEED_ERROR_SUCCESS; 552 } 553 554 /** 555 @brief Set the backend data of a `CeedOperator` 556 557 @param[in,out] op `CeedOperator` 558 @param[in] data Data to set 559 560 @return An error code: 0 - success, otherwise - failure 561 562 @ref Backend 563 **/ 564 int CeedOperatorSetData(CeedOperator op, void *data) { 565 op->data = data; 566 return CEED_ERROR_SUCCESS; 567 } 568 569 /** 570 @brief Increment the reference counter for a `CeedOperator` 571 572 @param[in,out] op `CeedOperator` to increment the reference counter 573 574 @return An error code: 0 - success, otherwise - failure 575 576 @ref Backend 577 **/ 578 int CeedOperatorReference(CeedOperator op) { 579 op->ref_count++; 580 return CEED_ERROR_SUCCESS; 581 } 582 583 /** 584 @brief Set the setup flag of a `CeedOperator` to `true` 585 586 @param[in,out] op `CeedOperator` 587 588 @return An error code: 0 - success, otherwise - failure 589 590 @ref Backend 591 **/ 592 int CeedOperatorSetSetupDone(CeedOperator op) { 593 op->is_backend_setup = true; 594 return CEED_ERROR_SUCCESS; 595 } 596 597 /// @} 598 599 /// ---------------------------------------------------------------------------- 600 /// CeedOperator Public API 601 /// ---------------------------------------------------------------------------- 602 /// @addtogroup CeedOperatorUser 603 /// @{ 604 605 /** 606 @brief Create a `CeedOperator` and associate a `CeedQFunction`. 607 608 A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField(). 609 610 @param[in] ceed `Ceed` object used to create the `CeedOperator` 611 @param[in] qf `CeedQFunction` defining the action of the operator at quadrature points 612 @param[in] dqf `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE) 613 @param[in] dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE) 614 @param[out] op Address of the variable where the newly created `CeedOperator` will be stored 615 616 @return An error code: 0 - success, otherwise - failure 617 618 @ref User 619 */ 620 int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 621 if (!ceed->OperatorCreate) { 622 Ceed delegate; 623 624 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 625 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreate"); 626 CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op)); 627 return CEED_ERROR_SUCCESS; 628 } 629 630 CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction."); 631 632 CeedCall(CeedCalloc(1, op)); 633 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 634 (*op)->ref_count = 1; 635 (*op)->input_size = -1; 636 (*op)->output_size = -1; 637 CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf)); 638 if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf)); 639 if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT)); 640 CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 641 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 642 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 643 CeedCall(ceed->OperatorCreate(*op)); 644 return CEED_ERROR_SUCCESS; 645 } 646 647 /** 648 @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element. 649 650 A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField. 651 The locations of each point are set with @ref CeedOperatorAtPointsSetPoints(). 652 653 @param[in] ceed `Ceed` object used to create the `CeedOperator` 654 @param[in] qf `CeedQFunction` defining the action of the operator at quadrature points 655 @param[in] dqf `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 656 @param[in] dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 657 @param[out] op Address of the variable where the newly created CeedOperator will be stored 658 659 @return An error code: 0 - success, otherwise - failure 660 661 @ref User 662 */ 663 int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 664 if (!ceed->OperatorCreateAtPoints) { 665 Ceed delegate; 666 667 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 668 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreateAtPoints"); 669 CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op)); 670 return CEED_ERROR_SUCCESS; 671 } 672 673 CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction."); 674 675 CeedCall(CeedCalloc(1, op)); 676 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 677 (*op)->ref_count = 1; 678 (*op)->is_at_points = true; 679 (*op)->input_size = -1; 680 (*op)->output_size = -1; 681 CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf)); 682 if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf)); 683 if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT)); 684 CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 685 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 686 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 687 CeedCall(ceed->OperatorCreateAtPoints(*op)); 688 return CEED_ERROR_SUCCESS; 689 } 690 691 /** 692 @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator` 693 694 @param[in] ceed `Ceed` object used to create the `CeedOperator` 695 @param[out] op Address of the variable where the newly created composite `CeedOperator` will be stored 696 697 @return An error code: 0 - success, otherwise - failure 698 699 @ref User 700 */ 701 int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) { 702 if (!ceed->CompositeOperatorCreate) { 703 Ceed delegate; 704 705 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 706 if (delegate) { 707 CeedCall(CeedCompositeOperatorCreate(delegate, op)); 708 return CEED_ERROR_SUCCESS; 709 } 710 } 711 712 CeedCall(CeedCalloc(1, op)); 713 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 714 (*op)->ref_count = 1; 715 (*op)->is_composite = true; 716 CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators)); 717 (*op)->input_size = -1; 718 (*op)->output_size = -1; 719 720 if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op)); 721 return CEED_ERROR_SUCCESS; 722 } 723 724 /** 725 @brief Copy the pointer to a `CeedOperator`. 726 727 Both pointers should be destroyed with @ref CeedOperatorDestroy(). 728 729 Note: If the value of `*op_copy` passed to this function is non-`NULL`, then it is assumed that `*op_copy` is a pointer to a `CeedOperator`. 730 This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`. 731 732 @param[in] op `CeedOperator` to copy reference to 733 @param[in,out] op_copy Variable to store copied reference 734 735 @return An error code: 0 - success, otherwise - failure 736 737 @ref User 738 **/ 739 int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) { 740 CeedCall(CeedOperatorReference(op)); 741 CeedCall(CeedOperatorDestroy(op_copy)); 742 *op_copy = op; 743 return CEED_ERROR_SUCCESS; 744 } 745 746 /** 747 @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`. 748 749 This function is used to specify both active and passive fields to a `CeedOperator`. 750 For passive fields, a `CeedVector` `vec` must be provided. 751 Passive fields can inputs or outputs (updated in-place when operator is applied). 752 753 Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply(). 754 There can be at most one active input `CeedVector` and at most one active output@ref CeedVector passed to @ref CeedOperatorApply(). 755 756 The number of quadrature points must agree across all points. 757 When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `rstr`. 758 759 @param[in,out] op `CeedOperator` on which to provide the field 760 @param[in] field_name Name of the field (to be matched with the name used by `CeedQFunction`) 761 @param[in] rstr `CeedElemRestriction` 762 @param[in] basis `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points 763 @param[in] vec `CeedVector` to be used by CeedOperator or @ref CEED_VECTOR_ACTIVE if field is active or @ref CEED_VECTOR_NONE if using @ref CEED_EVAL_WEIGHT in the `CeedQFunction` 764 765 @return An error code: 0 - success, otherwise - failure 766 767 @ref User 768 **/ 769 int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) { 770 bool is_input = true; 771 CeedInt num_elem = 0, num_qpts = 0; 772 CeedQFunctionField qf_field; 773 CeedOperatorField *op_field; 774 775 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); 776 CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 777 CeedCheck(rstr, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name); 778 CeedCheck(basis, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name); 779 CeedCheck(vec, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name); 780 781 CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); 782 CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION, 783 "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem); 784 { 785 CeedRestrictionType rstr_type; 786 787 CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type)); 788 if (rstr_type == CEED_RESTRICTION_POINTS) { 789 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields"); 790 CeedCheck(basis == CEED_BASIS_NONE, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE"); 791 if (!op->first_points_rstr) { 792 CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr)); 793 } else { 794 bool are_compatible; 795 796 CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible)); 797 CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE, 798 "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction"); 799 } 800 } 801 } 802 803 if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts)); 804 else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 805 CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION, 806 "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT 807 " quadrature points but expected %" CeedInt_FMT " quadrature points.", 808 basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts); 809 for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 810 if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) { 811 qf_field = op->qf->input_fields[i]; 812 op_field = &op->input_fields[i]; 813 goto found; 814 } 815 } 816 is_input = false; 817 for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 818 if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) { 819 qf_field = op->qf->output_fields[i]; 820 op_field = &op->output_fields[i]; 821 goto found; 822 } 823 } 824 // LCOV_EXCL_START 825 return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name); 826 // LCOV_EXCL_STOP 827 found: 828 CeedCall(CeedOperatorCheckField(op->ceed, qf_field, rstr, basis)); 829 CeedCall(CeedCalloc(1, op_field)); 830 831 if (vec == CEED_VECTOR_ACTIVE) { 832 CeedSize l_size; 833 834 CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); 835 if (is_input) { 836 if (op->input_size == -1) op->input_size = l_size; 837 CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, 838 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size); 839 } else { 840 if (op->output_size == -1) op->output_size = l_size; 841 CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, 842 "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size); 843 } 844 } 845 846 CeedCall(CeedVectorReferenceCopy(vec, &(*op_field)->vec)); 847 CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*op_field)->elem_rstr)); 848 if (rstr != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) { 849 op->num_elem = num_elem; 850 op->has_restriction = true; // Restriction set, but num_elem may be 0 851 } 852 CeedCall(CeedBasisReferenceCopy(basis, &(*op_field)->basis)); 853 if (op->num_qpts == 0 && !op->is_at_points) op->num_qpts = num_qpts; // no consistent number of qpts for OperatorAtPoints 854 op->num_fields += 1; 855 CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name)); 856 return CEED_ERROR_SUCCESS; 857 } 858 859 /** 860 @brief Get the `CeedOperator` Field of a `CeedOperator`. 861 862 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 863 864 @param[in] op `CeedOperator` 865 @param[out] num_input_fields Variable to store number of input fields 866 @param[out] input_fields Variable to store input fields 867 @param[out] num_output_fields Variable to store number of output fields 868 @param[out] output_fields Variable to store output fields 869 870 @return An error code: 0 - success, otherwise - failure 871 872 @ref Advanced 873 **/ 874 int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields, 875 CeedOperatorField **output_fields) { 876 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 877 CeedCall(CeedOperatorCheckReady(op)); 878 879 if (num_input_fields) *num_input_fields = op->qf->num_input_fields; 880 if (input_fields) *input_fields = op->input_fields; 881 if (num_output_fields) *num_output_fields = op->qf->num_output_fields; 882 if (output_fields) *output_fields = op->output_fields; 883 return CEED_ERROR_SUCCESS; 884 } 885 886 /** 887 @brief Set the arbitrary points in each element for a `CeedOperator` at points. 888 889 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 890 891 @param[in,out] op `CeedOperator` at points 892 @param[in] rstr_points `CeedElemRestriction` for the coordinates of each point by element 893 @param[in] point_coords `CeedVector` holding coordinates of each point 894 895 @return An error code: 0 - success, otherwise - failure 896 897 @ref Advanced 898 **/ 899 int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) { 900 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points"); 901 CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 902 903 if (!op->first_points_rstr) { 904 CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr)); 905 } else { 906 bool are_compatible; 907 908 CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible)); 909 CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE, 910 "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction"); 911 } 912 913 CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points)); 914 CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords)); 915 return CEED_ERROR_SUCCESS; 916 } 917 918 /** 919 @brief Get the arbitrary points in each element for a `CeedOperator` at points. 920 921 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 922 923 @param[in] op `CeedOperator` at points 924 @param[out] rstr_points Variable to hold `CeedElemRestriction` for the coordinates of each point by element 925 @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point 926 927 @return An error code: 0 - success, otherwise - failure 928 929 @ref Advanced 930 **/ 931 int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) { 932 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points"); 933 CeedCall(CeedOperatorCheckReady(op)); 934 935 if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points)); 936 if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords)); 937 return CEED_ERROR_SUCCESS; 938 } 939 940 /** 941 @brief Get a `CeedOperator` Field of a `CeedOperator` from its name. 942 943 `op_field` is set to `NULL` if the field is not found. 944 945 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 946 947 @param[in] op `CeedOperator` 948 @param[in] field_name Name of desired `CeedOperator` Field 949 @param[out] op_field `CeedOperator` Field corresponding to the name 950 951 @return An error code: 0 - success, otherwise - failure 952 953 @ref Advanced 954 **/ 955 int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) { 956 char *name; 957 CeedInt num_input_fields, num_output_fields; 958 CeedOperatorField *input_fields, *output_fields; 959 960 *op_field = NULL; 961 CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 962 for (CeedInt i = 0; i < num_input_fields; i++) { 963 CeedCall(CeedOperatorFieldGetName(input_fields[i], &name)); 964 if (!strcmp(name, field_name)) { 965 *op_field = input_fields[i]; 966 return CEED_ERROR_SUCCESS; 967 } 968 } 969 for (CeedInt i = 0; i < num_output_fields; i++) { 970 CeedCall(CeedOperatorFieldGetName(output_fields[i], &name)); 971 if (!strcmp(name, field_name)) { 972 *op_field = output_fields[i]; 973 return CEED_ERROR_SUCCESS; 974 } 975 } 976 return CEED_ERROR_SUCCESS; 977 } 978 979 /** 980 @brief Get the name of a `CeedOperator` Field 981 982 @param[in] op_field `CeedOperator` Field 983 @param[out] field_name Variable to store the field name 984 985 @return An error code: 0 - success, otherwise - failure 986 987 @ref Advanced 988 **/ 989 int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) { 990 *field_name = (char *)op_field->field_name; 991 return CEED_ERROR_SUCCESS; 992 } 993 994 /** 995 @brief Get the `CeedElemRestriction` of a `CeedOperator` Field 996 997 @param[in] op_field `CeedOperator` Field 998 @param[out] rstr Variable to store `CeedElemRestriction` 999 1000 @return An error code: 0 - success, otherwise - failure 1001 1002 @ref Advanced 1003 **/ 1004 int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) { 1005 *rstr = op_field->elem_rstr; 1006 return CEED_ERROR_SUCCESS; 1007 } 1008 1009 /** 1010 @brief Get the `CeedBasis` of a `CeedOperator` Field 1011 1012 @param[in] op_field `CeedOperator` Field 1013 @param[out] basis Variable to store `CeedBasis` 1014 1015 @return An error code: 0 - success, otherwise - failure 1016 1017 @ref Advanced 1018 **/ 1019 int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) { 1020 *basis = op_field->basis; 1021 return CEED_ERROR_SUCCESS; 1022 } 1023 1024 /** 1025 @brief Get the `CeedVector` of a `CeedOperator` Field 1026 1027 @param[in] op_field `CeedOperator` Field 1028 @param[out] vec Variable to store `CeedVector` 1029 1030 @return An error code: 0 - success, otherwise - failure 1031 1032 @ref Advanced 1033 **/ 1034 int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) { 1035 *vec = op_field->vec; 1036 return CEED_ERROR_SUCCESS; 1037 } 1038 1039 /** 1040 @brief Add a sub-operator to a composite `CeedOperator` 1041 1042 @param[in,out] composite_op Composite `CeedOperator` 1043 @param[in] sub_op Sub-operator `CeedOperator` 1044 1045 @return An error code: 0 - success, otherwise - failure 1046 1047 @ref User 1048 */ 1049 int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) { 1050 CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); 1051 CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators"); 1052 CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 1053 1054 { 1055 CeedSize input_size, output_size; 1056 1057 CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size)); 1058 if (composite_op->input_size == -1) composite_op->input_size = input_size; 1059 if (composite_op->output_size == -1) composite_op->output_size = output_size; 1060 // Note, a size of -1 means no active vector restriction set, so no incompatibility 1061 CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), 1062 composite_op->ceed, CEED_ERROR_MAJOR, 1063 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT 1064 ") not compatible with sub-operator of " 1065 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")", 1066 composite_op->input_size, composite_op->output_size, input_size, output_size); 1067 } 1068 1069 composite_op->sub_operators[composite_op->num_suboperators] = sub_op; 1070 CeedCall(CeedOperatorReference(sub_op)); 1071 composite_op->num_suboperators++; 1072 return CEED_ERROR_SUCCESS; 1073 } 1074 1075 /** 1076 @brief Get the number of sub-operators associated with a `CeedOperator` 1077 1078 @param[in] op `CeedOperator` 1079 @param[out] num_suboperators Variable to store number of sub-operators 1080 1081 @return An error code: 0 - success, otherwise - failure 1082 1083 @ref Backend 1084 **/ 1085 int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) { 1086 CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 1087 *num_suboperators = op->num_suboperators; 1088 return CEED_ERROR_SUCCESS; 1089 } 1090 1091 /** 1092 @brief Get the list of sub-operators associated with a `CeedOperator` 1093 1094 @param[in] op `CeedOperator` 1095 @param[out] sub_operators Variable to store list of sub-operators 1096 1097 @return An error code: 0 - success, otherwise - failure 1098 1099 @ref Backend 1100 **/ 1101 int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) { 1102 CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 1103 *sub_operators = op->sub_operators; 1104 return CEED_ERROR_SUCCESS; 1105 } 1106 1107 /** 1108 @brief Check if a `CeedOperator` is ready to be used. 1109 1110 @param[in] op `CeedOperator` to check 1111 1112 @return An error code: 0 - success, otherwise - failure 1113 1114 @ref User 1115 **/ 1116 int CeedOperatorCheckReady(CeedOperator op) { 1117 Ceed ceed; 1118 1119 CeedCall(CeedOperatorGetCeed(op, &ceed)); 1120 1121 if (op->is_interface_setup) return CEED_ERROR_SUCCESS; 1122 1123 CeedQFunction qf = op->qf; 1124 if (op->is_composite) { 1125 if (!op->num_suboperators) { 1126 // Empty operator setup 1127 op->input_size = 0; 1128 op->output_size = 0; 1129 } else { 1130 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1131 CeedCall(CeedOperatorCheckReady(op->sub_operators[i])); 1132 } 1133 // Sub-operators could be modified after adding to composite operator 1134 // Need to verify no lvec incompatibility from any changes 1135 CeedSize input_size, output_size; 1136 CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 1137 } 1138 } else { 1139 CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set"); 1140 CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set"); 1141 CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required"); 1142 CeedCheck(op->num_qpts > 0 || op->is_at_points, ceed, CEED_ERROR_INCOMPLETE, 1143 "At least one non-collocated CeedBasis is required or the number of quadrature points must be set"); 1144 } 1145 1146 // Flag as immutable and ready 1147 op->is_interface_setup = true; 1148 if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true; 1149 if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true; 1150 if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true; 1151 return CEED_ERROR_SUCCESS; 1152 } 1153 1154 /** 1155 @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`. 1156 1157 Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output. 1158 1159 @param[in] op `CeedOperator` 1160 @param[out] input_size Variable to store active input vector length, or `NULL` 1161 @param[out] output_size Variable to store active output vector length, or `NULL` 1162 1163 @return An error code: 0 - success, otherwise - failure 1164 1165 @ref User 1166 **/ 1167 int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) { 1168 bool is_composite; 1169 1170 if (input_size) *input_size = op->input_size; 1171 if (output_size) *output_size = op->output_size; 1172 1173 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1174 if (is_composite && (op->input_size == -1 || op->output_size == -1)) { 1175 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1176 CeedSize sub_input_size, sub_output_size; 1177 1178 CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size)); 1179 if (op->input_size == -1) op->input_size = sub_input_size; 1180 if (op->output_size == -1) op->output_size = sub_output_size; 1181 // Note, a size of -1 means no active vector restriction set, so no incompatibility 1182 CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed, 1183 CEED_ERROR_MAJOR, 1184 "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT 1185 ") not compatible with sub-operator of " 1186 "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")", 1187 op->input_size, op->output_size, input_size, output_size); 1188 } 1189 } 1190 return CEED_ERROR_SUCCESS; 1191 } 1192 1193 /** 1194 @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions. 1195 1196 When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called. 1197 When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(). 1198 1199 @param[in] op `CeedOperator` 1200 @param[in] reuse_assembly_data Boolean flag setting assembly data reuse 1201 1202 @return An error code: 0 - success, otherwise - failure 1203 1204 @ref Advanced 1205 **/ 1206 int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) { 1207 bool is_composite; 1208 1209 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1210 if (is_composite) { 1211 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1212 CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data)); 1213 } 1214 } else { 1215 CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data)); 1216 } 1217 return CEED_ERROR_SUCCESS; 1218 } 1219 1220 /** 1221 @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly. 1222 1223 @param[in] op `CeedOperator` 1224 @param[in] needs_data_update Boolean flag setting assembly data reuse 1225 1226 @return An error code: 0 - success, otherwise - failure 1227 1228 @ref Advanced 1229 **/ 1230 int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) { 1231 bool is_composite; 1232 1233 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1234 if (is_composite) { 1235 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1236 CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update)); 1237 } 1238 } else { 1239 CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update)); 1240 } 1241 return CEED_ERROR_SUCCESS; 1242 } 1243 1244 /** 1245 @brief Set name of `CeedOperator` for @ref CeedOperatorView() output 1246 1247 @param[in,out] op `CeedOperator` 1248 @param[in] name Name to set, or NULL to remove previously set name 1249 1250 @return An error code: 0 - success, otherwise - failure 1251 1252 @ref User 1253 **/ 1254 int CeedOperatorSetName(CeedOperator op, const char *name) { 1255 char *name_copy; 1256 size_t name_len = name ? strlen(name) : 0; 1257 1258 CeedCall(CeedFree(&op->name)); 1259 if (name_len > 0) { 1260 CeedCall(CeedCalloc(name_len + 1, &name_copy)); 1261 memcpy(name_copy, name, name_len); 1262 op->name = name_copy; 1263 } 1264 return CEED_ERROR_SUCCESS; 1265 } 1266 1267 /** 1268 @brief View a `CeedOperator` 1269 1270 @param[in] op `CeedOperator` to view 1271 @param[in] stream Stream to write; typically `stdout` or a file 1272 1273 @return Error code: 0 - success, otherwise - failure 1274 1275 @ref User 1276 **/ 1277 int CeedOperatorView(CeedOperator op, FILE *stream) { 1278 bool has_name = op->name; 1279 1280 if (op->is_composite) { 1281 fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 1282 1283 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1284 has_name = op->sub_operators[i]->name; 1285 fprintf(stream, " SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : ""); 1286 CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream)); 1287 } 1288 } else { 1289 fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 1290 CeedCall(CeedOperatorSingleView(op, 0, stream)); 1291 } 1292 return CEED_ERROR_SUCCESS; 1293 } 1294 1295 /** 1296 @brief Get the `Ceed` associated with a `CeedOperator` 1297 1298 @param[in] op `CeedOperator` 1299 @param[out] ceed Variable to store `Ceed` 1300 1301 @return An error code: 0 - success, otherwise - failure 1302 1303 @ref Advanced 1304 **/ 1305 int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) { 1306 *ceed = op->ceed; 1307 return CEED_ERROR_SUCCESS; 1308 } 1309 1310 /** 1311 @brief Get the number of elements associated with a `CeedOperator` 1312 1313 @param[in] op `CeedOperator` 1314 @param[out] num_elem Variable to store number of elements 1315 1316 @return An error code: 0 - success, otherwise - failure 1317 1318 @ref Advanced 1319 **/ 1320 int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) { 1321 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1322 *num_elem = op->num_elem; 1323 return CEED_ERROR_SUCCESS; 1324 } 1325 1326 /** 1327 @brief Get the number of quadrature points associated with a `CeedOperator` 1328 1329 @param[in] op `CeedOperator` 1330 @param[out] num_qpts Variable to store vector number of quadrature points 1331 1332 @return An error code: 0 - success, otherwise - failure 1333 1334 @ref Advanced 1335 **/ 1336 int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) { 1337 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1338 *num_qpts = op->num_qpts; 1339 return CEED_ERROR_SUCCESS; 1340 } 1341 1342 /** 1343 @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector` 1344 1345 @param[in] op `CeedOperator` to estimate FLOPs for 1346 @param[out] flops Address of variable to hold FLOPs estimate 1347 1348 @ref Backend 1349 **/ 1350 int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { 1351 bool is_composite; 1352 1353 CeedCall(CeedOperatorCheckReady(op)); 1354 1355 *flops = 0; 1356 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1357 if (is_composite) { 1358 CeedInt num_suboperators; 1359 1360 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1361 CeedOperator *sub_operators; 1362 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1363 1364 // FLOPs for each suboperator 1365 for (CeedInt i = 0; i < num_suboperators; i++) { 1366 CeedSize suboperator_flops; 1367 1368 CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops)); 1369 *flops += suboperator_flops; 1370 } 1371 } else { 1372 CeedInt num_input_fields, num_output_fields, num_elem = 0; 1373 CeedOperatorField *input_fields, *output_fields; 1374 1375 CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1376 CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1377 1378 // Input FLOPs 1379 for (CeedInt i = 0; i < num_input_fields; i++) { 1380 if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1381 CeedSize rstr_flops, basis_flops; 1382 1383 CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops)); 1384 *flops += rstr_flops; 1385 CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops)); 1386 *flops += basis_flops * num_elem; 1387 } 1388 } 1389 // QF FLOPs 1390 { 1391 CeedInt num_qpts; 1392 CeedSize qf_flops; 1393 1394 CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 1395 CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops)); 1396 CeedCheck(qf_flops > -1, op->ceed, CEED_ERROR_INCOMPLETE, "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate"); 1397 *flops += num_elem * num_qpts * qf_flops; 1398 } 1399 1400 // Output FLOPs 1401 for (CeedInt i = 0; i < num_output_fields; i++) { 1402 if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1403 CeedSize rstr_flops, basis_flops; 1404 1405 CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops)); 1406 *flops += rstr_flops; 1407 CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops)); 1408 *flops += basis_flops * num_elem; 1409 } 1410 } 1411 } 1412 return CEED_ERROR_SUCCESS; 1413 } 1414 1415 /** 1416 @brief Get `CeedQFunction` global context for a `CeedOperator`. 1417 1418 The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy(). 1419 1420 Note: If the value of `ctx` passed into this function is non-`NULL`, then it is assumed that `ctx` is a pointer to a `CeedQFunctionContext`. 1421 This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`. 1422 1423 @param[in] op `CeedOperator` 1424 @param[out] ctx Variable to store `CeedQFunctionContext` 1425 1426 @return An error code: 0 - success, otherwise - failure 1427 1428 @ref Advanced 1429 **/ 1430 int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) { 1431 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator"); 1432 if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx)); 1433 else *ctx = NULL; 1434 return CEED_ERROR_SUCCESS; 1435 } 1436 1437 /** 1438 @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`. 1439 1440 Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()). 1441 1442 @param[in] op `CeedOperator` 1443 @param[in] field_name Name of field to retrieve label 1444 @param[out] field_label Variable to field label 1445 1446 @return An error code: 0 - success, otherwise - failure 1447 1448 @ref User 1449 **/ 1450 int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) { 1451 bool is_composite, field_found = false; 1452 1453 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1454 1455 if (is_composite) { 1456 // Composite operator 1457 // -- Check if composite label already created 1458 for (CeedInt i = 0; i < op->num_context_labels; i++) { 1459 if (!strcmp(op->context_labels[i]->name, field_name)) { 1460 *field_label = op->context_labels[i]; 1461 return CEED_ERROR_SUCCESS; 1462 } 1463 } 1464 1465 // -- Create composite label if needed 1466 CeedInt num_sub; 1467 CeedOperator *sub_operators; 1468 CeedContextFieldLabel new_field_label; 1469 1470 CeedCall(CeedCalloc(1, &new_field_label)); 1471 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 1472 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1473 CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels)); 1474 new_field_label->num_sub_labels = num_sub; 1475 1476 for (CeedInt i = 0; i < num_sub; i++) { 1477 if (sub_operators[i]->qf->ctx) { 1478 CeedContextFieldLabel new_field_label_i; 1479 1480 CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i)); 1481 if (new_field_label_i) { 1482 field_found = true; 1483 new_field_label->sub_labels[i] = new_field_label_i; 1484 new_field_label->name = new_field_label_i->name; 1485 new_field_label->description = new_field_label_i->description; 1486 if (new_field_label->type && new_field_label->type != new_field_label_i->type) { 1487 // LCOV_EXCL_START 1488 CeedCall(CeedFree(&new_field_label)); 1489 return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s", 1490 CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]); 1491 // LCOV_EXCL_STOP 1492 } else { 1493 new_field_label->type = new_field_label_i->type; 1494 } 1495 if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) { 1496 // LCOV_EXCL_START 1497 CeedCall(CeedFree(&new_field_label)); 1498 return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %zu != %zu", 1499 new_field_label->num_values, new_field_label_i->num_values); 1500 // LCOV_EXCL_STOP 1501 } else { 1502 new_field_label->num_values = new_field_label_i->num_values; 1503 } 1504 } 1505 } 1506 } 1507 // -- Cleanup if field was found 1508 if (field_found) { 1509 *field_label = new_field_label; 1510 } else { 1511 // LCOV_EXCL_START 1512 CeedCall(CeedFree(&new_field_label->sub_labels)); 1513 CeedCall(CeedFree(&new_field_label)); 1514 *field_label = NULL; 1515 // LCOV_EXCL_STOP 1516 } 1517 } else { 1518 // Single, non-composite operator 1519 if (op->qf->ctx) { 1520 CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label)); 1521 } else { 1522 *field_label = NULL; 1523 } 1524 } 1525 1526 // Set label in operator 1527 if (*field_label) { 1528 (*field_label)->from_op = true; 1529 1530 // Move new composite label to operator 1531 if (op->num_context_labels == 0) { 1532 CeedCall(CeedCalloc(1, &op->context_labels)); 1533 op->max_context_labels = 1; 1534 } else if (op->num_context_labels == op->max_context_labels) { 1535 CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels)); 1536 op->max_context_labels *= 2; 1537 } 1538 op->context_labels[op->num_context_labels] = *field_label; 1539 op->num_context_labels++; 1540 } 1541 return CEED_ERROR_SUCCESS; 1542 } 1543 1544 /** 1545 @brief Set `CeedQFunctionContext` field holding double precision values. 1546 1547 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1548 1549 @param[in,out] op `CeedOperator` 1550 @param[in] field_label Label of field to set 1551 @param[in] values Values to set 1552 1553 @return An error code: 0 - success, otherwise - failure 1554 1555 @ref User 1556 **/ 1557 int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) { 1558 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1559 } 1560 1561 /** 1562 @brief Get `CeedQFunctionContext` field holding double precision values, read-only. 1563 1564 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1565 1566 @param[in] op `CeedOperator` 1567 @param[in] field_label Label of field to get 1568 @param[out] num_values Number of values in the field label 1569 @param[out] values Pointer to context values 1570 1571 @return An error code: 0 - success, otherwise - failure 1572 1573 @ref User 1574 **/ 1575 int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) { 1576 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values); 1577 } 1578 1579 /** 1580 @brief Restore `CeedQFunctionContext` field holding double precision values, read-only. 1581 1582 @param[in] op `CeedOperator` 1583 @param[in] field_label Label of field to restore 1584 @param[out] values Pointer to context values 1585 1586 @return An error code: 0 - success, otherwise - failure 1587 1588 @ref User 1589 **/ 1590 int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) { 1591 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1592 } 1593 1594 /** 1595 @brief Set `CeedQFunctionContext` field holding `int32` values. 1596 1597 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1598 1599 @param[in,out] op `CeedOperator` 1600 @param[in] field_label Label of field to set 1601 @param[in] values Values to set 1602 1603 @return An error code: 0 - success, otherwise - failure 1604 1605 @ref User 1606 **/ 1607 int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) { 1608 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1609 } 1610 1611 /** 1612 @brief Get `CeedQFunctionContext` field holding `int32` values, read-only. 1613 1614 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1615 1616 @param[in] op `CeedOperator` 1617 @param[in] field_label Label of field to get 1618 @param[out] num_values Number of `int32` values in `values` 1619 @param[out] values Pointer to context values 1620 1621 @return An error code: 0 - success, otherwise - failure 1622 1623 @ref User 1624 **/ 1625 int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) { 1626 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values); 1627 } 1628 1629 /** 1630 @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only. 1631 1632 @param[in] op `CeedOperator` 1633 @param[in] field_label Label of field to get 1634 @param[out] values Pointer to context values 1635 1636 @return An error code: 0 - success, otherwise - failure 1637 1638 @ref User 1639 **/ 1640 int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) { 1641 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1642 } 1643 1644 /** 1645 @brief Set `CeedQFunctionContext` field holding boolean values. 1646 1647 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1648 1649 @param[in,out] op `CeedOperator` 1650 @param[in] field_label Label of field to set 1651 @param[in] values Values to set 1652 1653 @return An error code: 0 - success, otherwise - failure 1654 1655 @ref User 1656 **/ 1657 int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) { 1658 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values); 1659 } 1660 1661 /** 1662 @brief Get `CeedQFunctionContext` field holding boolean values, read-only. 1663 1664 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1665 1666 @param[in] op `CeedOperator` 1667 @param[in] field_label Label of field to get 1668 @param[out] num_values Number of boolean values in `values` 1669 @param[out] values Pointer to context values 1670 1671 @return An error code: 0 - success, otherwise - failure 1672 1673 @ref User 1674 **/ 1675 int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) { 1676 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values); 1677 } 1678 1679 /** 1680 @brief Restore `CeedQFunctionContext` field holding boolean values, read-only. 1681 1682 @param[in] op `CeedOperator` 1683 @param[in] field_label Label of field to get 1684 @param[out] values Pointer to context values 1685 1686 @return An error code: 0 - success, otherwise - failure 1687 1688 @ref User 1689 **/ 1690 int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) { 1691 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values); 1692 } 1693 1694 /** 1695 @brief Apply `CeedOperator` to a `CeedVector`. 1696 1697 This computes the action of the operator on the specified (active) input, yielding its (active) output. 1698 All inputs and outputs must be specified using @ref CeedOperatorSetField(). 1699 1700 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1701 1702 @param[in] op `CeedOperator` to apply 1703 @param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 1704 @param[out] out `CeedVector` to store result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs 1705 @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1706 1707 @return An error code: 0 - success, otherwise - failure 1708 1709 @ref User 1710 **/ 1711 int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 1712 CeedCall(CeedOperatorCheckReady(op)); 1713 1714 if (op->is_composite) { 1715 // Composite Operator 1716 if (op->ApplyComposite) { 1717 CeedCall(op->ApplyComposite(op, in, out, request)); 1718 } else { 1719 CeedInt num_suboperators; 1720 CeedOperator *sub_operators; 1721 1722 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1723 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1724 1725 // Zero all output vectors 1726 if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0)); 1727 for (CeedInt i = 0; i < num_suboperators; i++) { 1728 for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) { 1729 CeedVector vec = sub_operators[i]->output_fields[j]->vec; 1730 1731 if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) { 1732 CeedCall(CeedVectorSetValue(vec, 0.0)); 1733 } 1734 } 1735 } 1736 // Apply 1737 for (CeedInt i = 0; i < num_suboperators; i++) { 1738 CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 1739 } 1740 } 1741 } else { 1742 // Standard Operator 1743 if (op->Apply) { 1744 CeedCall(op->Apply(op, in, out, request)); 1745 } else { 1746 // Zero all output vectors 1747 CeedQFunction qf = op->qf; 1748 1749 for (CeedInt i = 0; i < qf->num_output_fields; i++) { 1750 CeedVector vec = op->output_fields[i]->vec; 1751 1752 if (vec == CEED_VECTOR_ACTIVE) vec = out; 1753 if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0)); 1754 } 1755 // Apply 1756 if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request)); 1757 } 1758 } 1759 return CEED_ERROR_SUCCESS; 1760 } 1761 1762 /** 1763 @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`. 1764 1765 This computes the action of the operator on the specified (active) input, yielding its (active) output. 1766 All inputs and outputs must be specified using @ref CeedOperatorSetField(). 1767 1768 @param[in] op `CeedOperator` to apply 1769 @param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 1770 @param[out] out `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs 1771 @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1772 1773 @return An error code: 0 - success, otherwise - failure 1774 1775 @ref User 1776 **/ 1777 int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 1778 CeedCall(CeedOperatorCheckReady(op)); 1779 1780 if (op->is_composite) { 1781 // Composite Operator 1782 if (op->ApplyAddComposite) { 1783 CeedCall(op->ApplyAddComposite(op, in, out, request)); 1784 } else { 1785 CeedInt num_suboperators; 1786 CeedOperator *sub_operators; 1787 1788 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1789 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1790 for (CeedInt i = 0; i < num_suboperators; i++) { 1791 CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 1792 } 1793 } 1794 } else if (op->num_elem) { 1795 // Standard Operator 1796 CeedCall(op->ApplyAdd(op, in, out, request)); 1797 } 1798 return CEED_ERROR_SUCCESS; 1799 } 1800 1801 /** 1802 @brief Destroy a `CeedOperator` 1803 1804 @param[in,out] op `CeedOperator` to destroy 1805 1806 @return An error code: 0 - success, otherwise - failure 1807 1808 @ref User 1809 **/ 1810 int CeedOperatorDestroy(CeedOperator *op) { 1811 if (!*op || --(*op)->ref_count > 0) { 1812 *op = NULL; 1813 return CEED_ERROR_SUCCESS; 1814 } 1815 if ((*op)->Destroy) CeedCall((*op)->Destroy(*op)); 1816 CeedCall(CeedDestroy(&(*op)->ceed)); 1817 // Free fields 1818 for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1819 if ((*op)->input_fields[i]) { 1820 if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) { 1821 CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr)); 1822 } 1823 if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) { 1824 CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis)); 1825 } 1826 if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) { 1827 CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec)); 1828 } 1829 CeedCall(CeedFree(&(*op)->input_fields[i]->field_name)); 1830 CeedCall(CeedFree(&(*op)->input_fields[i])); 1831 } 1832 } 1833 for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1834 if ((*op)->output_fields[i]) { 1835 CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr)); 1836 if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) { 1837 CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis)); 1838 } 1839 if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) { 1840 CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec)); 1841 } 1842 CeedCall(CeedFree(&(*op)->output_fields[i]->field_name)); 1843 CeedCall(CeedFree(&(*op)->output_fields[i])); 1844 } 1845 } 1846 // AtPoints data 1847 CeedCall(CeedVectorDestroy(&(*op)->point_coords)); 1848 CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points)); 1849 CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr)); 1850 // Destroy sub_operators 1851 for (CeedInt i = 0; i < (*op)->num_suboperators; i++) { 1852 if ((*op)->sub_operators[i]) { 1853 CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i])); 1854 } 1855 } 1856 CeedCall(CeedQFunctionDestroy(&(*op)->qf)); 1857 CeedCall(CeedQFunctionDestroy(&(*op)->dqf)); 1858 CeedCall(CeedQFunctionDestroy(&(*op)->dqfT)); 1859 // Destroy any composite labels 1860 if ((*op)->is_composite) { 1861 for (CeedInt i = 0; i < (*op)->num_context_labels; i++) { 1862 CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels)); 1863 CeedCall(CeedFree(&(*op)->context_labels[i])); 1864 } 1865 } 1866 CeedCall(CeedFree(&(*op)->context_labels)); 1867 1868 // Destroy fallback 1869 CeedCall(CeedOperatorDestroy(&(*op)->op_fallback)); 1870 1871 // Destroy assembly data 1872 CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled)); 1873 CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled)); 1874 1875 CeedCall(CeedFree(&(*op)->input_fields)); 1876 CeedCall(CeedFree(&(*op)->output_fields)); 1877 CeedCall(CeedFree(&(*op)->sub_operators)); 1878 CeedCall(CeedFree(&(*op)->name)); 1879 CeedCall(CeedFree(op)); 1880 return CEED_ERROR_SUCCESS; 1881 } 1882 1883 /// @} 1884