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] r `CeedOperator` Field `CeedElemRestriction` 30 @param[in] b `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 r, CeedBasis b) { 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((r == 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 (r != CEED_ELEMRESTRICTION_NONE) { 44 CeedCall(CeedElemRestrictionGetNumComponents(r, &rstr_num_comp)); 45 } 46 // Basis 47 CeedCheck((b == 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 (b != CEED_BASIS_NONE) { 50 CeedCall(CeedBasisGetDimension(b, &dim)); 51 CeedCall(CeedBasisGetNumComponents(b, &num_comp)); 52 CeedCall(CeedBasisGetNumQuadratureComponents(b, eval_mode, &q_comp)); 53 CeedCheck(r == 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 setup status of a `CeedOperator` 454 455 @param[in] op `CeedOperator` 456 @param[out] is_setup_done Variable to store setup status 457 458 @return An error code: 0 - success, otherwise - failure 459 460 @ref Backend 461 **/ 462 int CeedOperatorIsSetupDone(CeedOperator op, bool *is_setup_done) { 463 *is_setup_done = op->is_backend_setup; 464 return CEED_ERROR_SUCCESS; 465 } 466 467 /** 468 @brief Get the `CeedQFunction` associated with a `CeedOperator` 469 470 @param[in] op `CeedOperator` 471 @param[out] qf Variable to store `CeedQFunction` 472 473 @return An error code: 0 - success, otherwise - failure 474 475 @ref Backend 476 **/ 477 int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) { 478 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 479 *qf = op->qf; 480 return CEED_ERROR_SUCCESS; 481 } 482 483 /** 484 @brief Get a boolean value indicating if the `CeedOperator` is composite 485 486 @param[in] op `CeedOperator` 487 @param[out] is_composite Variable to store composite status 488 489 @return An error code: 0 - success, otherwise - failure 490 491 @ref Backend 492 **/ 493 int CeedOperatorIsComposite(CeedOperator op, bool *is_composite) { 494 *is_composite = op->is_composite; 495 return CEED_ERROR_SUCCESS; 496 } 497 498 /** 499 @brief Get the backend data of a `CeedOperator` 500 501 @param[in] op `CeedOperator` 502 @param[out] data Variable to store data 503 504 @return An error code: 0 - success, otherwise - failure 505 506 @ref Backend 507 **/ 508 int CeedOperatorGetData(CeedOperator op, void *data) { 509 *(void **)data = op->data; 510 return CEED_ERROR_SUCCESS; 511 } 512 513 /** 514 @brief Set the backend data of a `CeedOperator` 515 516 @param[in,out] op `CeedOperator` 517 @param[in] data Data to set 518 519 @return An error code: 0 - success, otherwise - failure 520 521 @ref Backend 522 **/ 523 int CeedOperatorSetData(CeedOperator op, void *data) { 524 op->data = data; 525 return CEED_ERROR_SUCCESS; 526 } 527 528 /** 529 @brief Increment the reference counter for a `CeedOperator` 530 531 @param[in,out] op `CeedOperator` to increment the reference counter 532 533 @return An error code: 0 - success, otherwise - failure 534 535 @ref Backend 536 **/ 537 int CeedOperatorReference(CeedOperator op) { 538 op->ref_count++; 539 return CEED_ERROR_SUCCESS; 540 } 541 542 /** 543 @brief Set the setup flag of a `CeedOperator` to `true` 544 545 @param[in,out] op `CeedOperator` 546 547 @return An error code: 0 - success, otherwise - failure 548 549 @ref Backend 550 **/ 551 int CeedOperatorSetSetupDone(CeedOperator op) { 552 op->is_backend_setup = true; 553 return CEED_ERROR_SUCCESS; 554 } 555 556 /// @} 557 558 /// ---------------------------------------------------------------------------- 559 /// CeedOperator Public API 560 /// ---------------------------------------------------------------------------- 561 /// @addtogroup CeedOperatorUser 562 /// @{ 563 564 /** 565 @brief Create a `CeedOperator` and associate a `CeedQFunction`. 566 567 A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with @ref CeedOperatorSetField(). 568 569 @param[in] ceed `Ceed` object used to create the `CeedOperator` 570 @param[in] qf `CeedQFunction` defining the action of the operator at quadrature points 571 @param[in] dqf `CeedQFunction` defining the action of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE) 572 @param[in] dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of `qf` (or @ref CEED_QFUNCTION_NONE) 573 @param[out] op Address of the variable where the newly created `CeedOperator` will be stored 574 575 @return An error code: 0 - success, otherwise - failure 576 577 @ref User 578 */ 579 int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 580 if (!ceed->OperatorCreate) { 581 Ceed delegate; 582 583 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 584 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreate"); 585 CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op)); 586 return CEED_ERROR_SUCCESS; 587 } 588 589 CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction."); 590 591 CeedCall(CeedCalloc(1, op)); 592 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 593 (*op)->ref_count = 1; 594 (*op)->input_size = -1; 595 (*op)->output_size = -1; 596 CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf)); 597 if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf)); 598 if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT)); 599 CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 600 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 601 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 602 CeedCall(ceed->OperatorCreate(*op)); 603 return CEED_ERROR_SUCCESS; 604 } 605 606 /** 607 @brief Create a `CeedOperator` for evaluation at evaluation at arbitrary points in each element. 608 609 A `CeedBasis` and `CeedElemRestriction` can be associated with `CeedQFunction` fields with `CeedOperator` SetField. 610 The locations of each point are set with @ref CeedOperatorAtPointsSetPoints(). 611 612 @param[in] ceed `Ceed` object used to create the `CeedOperator` 613 @param[in] qf `CeedQFunction` defining the action of the operator at quadrature points 614 @param[in] dqf `CeedQFunction` defining the action of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 615 @param[in] dqfT `CeedQFunction` defining the action of the transpose of the Jacobian of @a qf (or @ref CEED_QFUNCTION_NONE) 616 @param[out] op Address of the variable where the newly created CeedOperator will be stored 617 618 @return An error code: 0 - success, otherwise - failure 619 620 @ref User 621 */ 622 int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunction dqfT, CeedOperator *op) { 623 if (!ceed->OperatorCreateAtPoints) { 624 Ceed delegate; 625 626 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 627 CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorCreateAtPoints"); 628 CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op)); 629 return CEED_ERROR_SUCCESS; 630 } 631 632 CeedCheck(qf && qf != CEED_QFUNCTION_NONE, ceed, CEED_ERROR_MINOR, "Operator must have a valid CeedQFunction."); 633 634 CeedCall(CeedCalloc(1, op)); 635 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 636 (*op)->ref_count = 1; 637 (*op)->is_at_points = true; 638 (*op)->input_size = -1; 639 (*op)->output_size = -1; 640 CeedCall(CeedQFunctionReferenceCopy(qf, &(*op)->qf)); 641 if (dqf && dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqf, &(*op)->dqf)); 642 if (dqfT && dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionReferenceCopy(dqfT, &(*op)->dqfT)); 643 CeedCall(CeedQFunctionAssemblyDataCreate(ceed, &(*op)->qf_assembled)); 644 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->input_fields)); 645 CeedCall(CeedCalloc(CEED_FIELD_MAX, &(*op)->output_fields)); 646 CeedCall(ceed->OperatorCreateAtPoints(*op)); 647 return CEED_ERROR_SUCCESS; 648 } 649 650 /** 651 @brief Create a composite `CeedOperator` that composes the action of several `CeedOperator` 652 653 @param[in] ceed `Ceed` object used to create the `CeedOperator` 654 @param[out] op Address of the variable where the newly created composite `CeedOperator` will be stored 655 656 @return An error code: 0 - success, otherwise - failure 657 658 @ref User 659 */ 660 int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) { 661 if (!ceed->CompositeOperatorCreate) { 662 Ceed delegate; 663 664 CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); 665 if (delegate) { 666 CeedCall(CeedCompositeOperatorCreate(delegate, op)); 667 return CEED_ERROR_SUCCESS; 668 } 669 } 670 671 CeedCall(CeedCalloc(1, op)); 672 CeedCall(CeedReferenceCopy(ceed, &(*op)->ceed)); 673 (*op)->ref_count = 1; 674 (*op)->is_composite = true; 675 CeedCall(CeedCalloc(CEED_COMPOSITE_MAX, &(*op)->sub_operators)); 676 (*op)->input_size = -1; 677 (*op)->output_size = -1; 678 679 if (ceed->CompositeOperatorCreate) CeedCall(ceed->CompositeOperatorCreate(*op)); 680 return CEED_ERROR_SUCCESS; 681 } 682 683 /** 684 @brief Copy the pointer to a `CeedOperator`. 685 686 Both pointers should be destroyed with @ref CeedOperatorDestroy(). 687 688 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`. 689 This `CeedOperator` will be destroyed if `*op_copy` is the only reference to this `CeedOperator`. 690 691 @param[in] op `CeedOperator` to copy reference to 692 @param[in,out] op_copy Variable to store copied reference 693 694 @return An error code: 0 - success, otherwise - failure 695 696 @ref User 697 **/ 698 int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) { 699 CeedCall(CeedOperatorReference(op)); 700 CeedCall(CeedOperatorDestroy(op_copy)); 701 *op_copy = op; 702 return CEED_ERROR_SUCCESS; 703 } 704 705 /** 706 @brief Provide a field to a `CeedOperator` for use by its `CeedQFunction`. 707 708 This function is used to specify both active and passive fields to a `CeedOperator`. 709 For passive fields, a `CeedVector` `v` must be provided. 710 Passive fields can inputs or outputs (updated in-place when operator is applied). 711 712 Active fields must be specified using this function, but their data (in a `CeedVector`) is passed in @ref CeedOperatorApply(). 713 There can be at most one active input `CeedVector` and at most one active output@ref CeedVector passed to @ref CeedOperatorApply(). 714 715 The number of quadrature points must agree across all points. 716 When using @ref CEED_BASIS_NONE, the number of quadrature points is determined by the element size of `r`. 717 718 @param[in,out] op `CeedOperator` on which to provide the field 719 @param[in] field_name Name of the field (to be matched with the name used by `CeedQFunction`) 720 @param[in] r `CeedElemRestriction` 721 @param[in] b `CeedBasis` in which the field resides or @ref CEED_BASIS_NONE if collocated with quadrature points 722 @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 CEED_EVAL_WEIGHT in the `CeedQFunction` 723 724 @return An error code: 0 - success, otherwise - failure 725 726 @ref User 727 **/ 728 int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction r, CeedBasis b, CeedVector v) { 729 bool is_input = true; 730 CeedInt num_elem = 0, num_qpts = 0; 731 CeedQFunctionField qf_field; 732 CeedOperatorField *op_field; 733 734 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); 735 CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 736 CeedCheck(r, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction r for field \"%s\" must be non-NULL.", field_name); 737 CeedCheck(b, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis b for field \"%s\" must be non-NULL.", field_name); 738 CeedCheck(v, op->ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector v for field \"%s\" must be non-NULL.", field_name); 739 740 CeedCall(CeedElemRestrictionGetNumElements(r, &num_elem)); 741 CeedCheck(r == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || op->num_elem == num_elem, op->ceed, CEED_ERROR_DIMENSION, 742 "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem); 743 { 744 CeedRestrictionType rstr_type; 745 746 CeedCall(CeedElemRestrictionGetType(r, &rstr_type)); 747 if (rstr_type == CEED_RESTRICTION_POINTS) { 748 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields"); 749 CeedCheck(b == CEED_BASIS_NONE, op->ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE"); 750 if (!op->first_points_rstr) { 751 CeedCall(CeedElemRestrictionReferenceCopy(r, &op->first_points_rstr)); 752 } else { 753 bool are_compatible; 754 755 CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, r, &are_compatible)); 756 CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE, 757 "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction"); 758 } 759 } 760 } 761 762 if (b == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(r, &num_qpts)); 763 else CeedCall(CeedBasisGetNumQuadraturePoints(b, &num_qpts)); 764 CeedCheck(op->num_qpts == 0 || op->num_qpts == num_qpts, op->ceed, CEED_ERROR_DIMENSION, 765 "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT 766 " quadrature points but expected %" CeedInt_FMT " quadrature points.", 767 b == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts); 768 for (CeedInt i = 0; i < op->qf->num_input_fields; i++) { 769 if (!strcmp(field_name, (*op->qf->input_fields[i]).field_name)) { 770 qf_field = op->qf->input_fields[i]; 771 op_field = &op->input_fields[i]; 772 goto found; 773 } 774 } 775 is_input = false; 776 for (CeedInt i = 0; i < op->qf->num_output_fields; i++) { 777 if (!strcmp(field_name, (*op->qf->output_fields[i]).field_name)) { 778 qf_field = op->qf->output_fields[i]; 779 op_field = &op->output_fields[i]; 780 goto found; 781 } 782 } 783 // LCOV_EXCL_START 784 return CeedError(op->ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name); 785 // LCOV_EXCL_STOP 786 found: 787 CeedCall(CeedOperatorCheckField(op->ceed, qf_field, r, b)); 788 CeedCall(CeedCalloc(1, op_field)); 789 790 if (v == CEED_VECTOR_ACTIVE) { 791 CeedSize l_size; 792 793 CeedCall(CeedElemRestrictionGetLVectorSize(r, &l_size)); 794 if (is_input) { 795 if (op->input_size == -1) op->input_size = l_size; 796 CeedCheck(l_size == op->input_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, 797 op->input_size); 798 } else { 799 if (op->output_size == -1) op->output_size = l_size; 800 CeedCheck(l_size == op->output_size, op->ceed, CEED_ERROR_INCOMPATIBLE, "LVector size %td does not match previous size %td", l_size, 801 op->output_size); 802 } 803 } 804 805 CeedCall(CeedVectorReferenceCopy(v, &(*op_field)->vec)); 806 CeedCall(CeedElemRestrictionReferenceCopy(r, &(*op_field)->elem_rstr)); 807 if (r != CEED_ELEMRESTRICTION_NONE && !op->has_restriction) { 808 op->num_elem = num_elem; 809 op->has_restriction = true; // Restriction set, but num_elem may be 0 810 } 811 CeedCall(CeedBasisReferenceCopy(b, &(*op_field)->basis)); 812 if (op->num_qpts == 0 && !op->is_at_points) op->num_qpts = num_qpts; // no consistent number of qpts for OperatorAtPoints 813 op->num_fields += 1; 814 CeedCall(CeedStringAllocCopy(field_name, (char **)&(*op_field)->field_name)); 815 return CEED_ERROR_SUCCESS; 816 } 817 818 /** 819 @brief Get the `CeedOperator` Field of a `CeedOperator`. 820 821 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 822 823 @param[in] op `CeedOperator` 824 @param[out] num_input_fields Variable to store number of input fields 825 @param[out] input_fields Variable to store input fields 826 @param[out] num_output_fields Variable to store number of output fields 827 @param[out] output_fields Variable to store output fields 828 829 @return An error code: 0 - success, otherwise - failure 830 831 @ref Advanced 832 **/ 833 int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperatorField **input_fields, CeedInt *num_output_fields, 834 CeedOperatorField **output_fields) { 835 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 836 CeedCall(CeedOperatorCheckReady(op)); 837 838 if (num_input_fields) *num_input_fields = op->qf->num_input_fields; 839 if (input_fields) *input_fields = op->input_fields; 840 if (num_output_fields) *num_output_fields = op->qf->num_output_fields; 841 if (output_fields) *output_fields = op->output_fields; 842 return CEED_ERROR_SUCCESS; 843 } 844 845 /** 846 @brief Set the arbitrary points in each element for a `CeedOperator` at points. 847 848 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 849 850 @param[in,out] op `CeedOperator` at points 851 @param[in] rstr_points `CeedElemRestriction` for the coordinates of each point by element 852 @param[in] point_coords `CeedVector` holding coordinates of each point 853 854 @return An error code: 0 - success, otherwise - failure 855 856 @ref Advanced 857 **/ 858 int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) { 859 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points"); 860 CeedCheck(!op->is_immutable, op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 861 862 if (!op->first_points_rstr) { 863 CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr)); 864 } else { 865 bool are_compatible; 866 867 CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible)); 868 CeedCheck(are_compatible, op->ceed, CEED_ERROR_INCOMPATIBLE, 869 "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction"); 870 } 871 872 CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->rstr_points)); 873 CeedCall(CeedVectorReferenceCopy(point_coords, &op->point_coords)); 874 return CEED_ERROR_SUCCESS; 875 } 876 877 /** 878 @brief Get the arbitrary points in each element for a `CeedOperator` at points. 879 880 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 881 882 @param[in] op `CeedOperator` at points 883 @param[out] rstr_points Variable to hold `CeedElemRestriction` for the coordinates of each point by element 884 @param[out] point_coords Variable to hold `CeedVector` holding coordinates of each point 885 886 @return An error code: 0 - success, otherwise - failure 887 888 @ref Advanced 889 **/ 890 int CeedOperatorAtPointsGetPoints(CeedOperator op, CeedElemRestriction *rstr_points, CeedVector *point_coords) { 891 CeedCheck(op->is_at_points, op->ceed, CEED_ERROR_MINOR, "Only defined for operator at points"); 892 CeedCall(CeedOperatorCheckReady(op)); 893 894 if (rstr_points) CeedCall(CeedElemRestrictionReferenceCopy(op->rstr_points, rstr_points)); 895 if (point_coords) CeedCall(CeedVectorReferenceCopy(op->point_coords, point_coords)); 896 return CEED_ERROR_SUCCESS; 897 } 898 899 /** 900 @brief Get a `CeedOperator` Field of a `CeedOperator` from its name 901 902 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 903 904 @param[in] op `CeedOperator` 905 @param[in] field_name Name of desired `CeedOperator` Field 906 @param[out] op_field `CeedOperator` Field corresponding to the name 907 908 @return An error code: 0 - success, otherwise - failure 909 910 @ref Advanced 911 **/ 912 int CeedOperatorGetFieldByName(CeedOperator op, const char *field_name, CeedOperatorField *op_field) { 913 char *name; 914 CeedInt num_input_fields, num_output_fields; 915 CeedOperatorField *input_fields, *output_fields; 916 917 CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 918 for (CeedInt i = 0; i < num_input_fields; i++) { 919 CeedCall(CeedOperatorFieldGetName(input_fields[i], &name)); 920 if (!strcmp(name, field_name)) { 921 *op_field = input_fields[i]; 922 return CEED_ERROR_SUCCESS; 923 } 924 } 925 for (CeedInt i = 0; i < num_output_fields; i++) { 926 CeedCall(CeedOperatorFieldGetName(output_fields[i], &name)); 927 if (!strcmp(name, field_name)) { 928 *op_field = output_fields[i]; 929 return CEED_ERROR_SUCCESS; 930 } 931 } 932 // LCOV_EXCL_START 933 bool has_name = op->name; 934 935 return CeedError(op->ceed, CEED_ERROR_MINOR, "The field \"%s\" not found in CeedOperator%s%s%s.\n", field_name, has_name ? " \"" : "", 936 has_name ? op->name : "", has_name ? "\"" : ""); 937 // LCOV_EXCL_STOP 938 } 939 940 /** 941 @brief Get the name of a `CeedOperator` Field 942 943 @param[in] op_field `CeedOperator` Field 944 @param[out] field_name Variable to store the field name 945 946 @return An error code: 0 - success, otherwise - failure 947 948 @ref Advanced 949 **/ 950 int CeedOperatorFieldGetName(CeedOperatorField op_field, char **field_name) { 951 *field_name = (char *)op_field->field_name; 952 return CEED_ERROR_SUCCESS; 953 } 954 955 /** 956 @brief Get the `CeedElemRestriction` of a `CeedOperator` Field 957 958 @param[in] op_field `CeedOperator` Field 959 @param[out] rstr Variable to store `CeedElemRestriction` 960 961 @return An error code: 0 - success, otherwise - failure 962 963 @ref Advanced 964 **/ 965 int CeedOperatorFieldGetElemRestriction(CeedOperatorField op_field, CeedElemRestriction *rstr) { 966 *rstr = op_field->elem_rstr; 967 return CEED_ERROR_SUCCESS; 968 } 969 970 /** 971 @brief Get the `CeedBasis` of a `CeedOperator` Field 972 973 @param[in] op_field `CeedOperator` Field 974 @param[out] basis Variable to store `CeedBasis` 975 976 @return An error code: 0 - success, otherwise - failure 977 978 @ref Advanced 979 **/ 980 int CeedOperatorFieldGetBasis(CeedOperatorField op_field, CeedBasis *basis) { 981 *basis = op_field->basis; 982 return CEED_ERROR_SUCCESS; 983 } 984 985 /** 986 @brief Get the `CeedVector` of a `CeedOperator` Field 987 988 @param[in] op_field `CeedOperator` Field 989 @param[out] vec Variable to store `CeedVector` 990 991 @return An error code: 0 - success, otherwise - failure 992 993 @ref Advanced 994 **/ 995 int CeedOperatorFieldGetVector(CeedOperatorField op_field, CeedVector *vec) { 996 *vec = op_field->vec; 997 return CEED_ERROR_SUCCESS; 998 } 999 1000 /** 1001 @brief Add a sub-operator to a composite `CeedOperator` 1002 1003 @param[in,out] composite_op Composite `CeedOperator` 1004 @param[in] sub_op Sub-operator `CeedOperator` 1005 1006 @return An error code: 0 - success, otherwise - failure 1007 1008 @ref User 1009 */ 1010 int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) { 1011 CeedCheck(composite_op->is_composite, composite_op->ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); 1012 CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, composite_op->ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators"); 1013 CeedCheck(!composite_op->is_immutable, composite_op->ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); 1014 1015 { 1016 CeedSize input_size, output_size; 1017 1018 CeedCall(CeedOperatorGetActiveVectorLengths(sub_op, &input_size, &output_size)); 1019 if (composite_op->input_size == -1) composite_op->input_size = input_size; 1020 if (composite_op->output_size == -1) composite_op->output_size = output_size; 1021 // Note, a size of -1 means no active vector restriction set, so no incompatibility 1022 CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), 1023 composite_op->ceed, CEED_ERROR_MAJOR, 1024 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 1025 "shape (%td, %td)", 1026 composite_op->input_size, composite_op->output_size, input_size, output_size); 1027 } 1028 1029 composite_op->sub_operators[composite_op->num_suboperators] = sub_op; 1030 CeedCall(CeedOperatorReference(sub_op)); 1031 composite_op->num_suboperators++; 1032 return CEED_ERROR_SUCCESS; 1033 } 1034 1035 /** 1036 @brief Get the number of sub-operators associated with a `CeedOperator` 1037 1038 @param[in] op `CeedOperator` 1039 @param[out] num_suboperators Variable to store number of sub-operators 1040 1041 @return An error code: 0 - success, otherwise - failure 1042 1043 @ref Backend 1044 **/ 1045 int CeedCompositeOperatorGetNumSub(CeedOperator op, CeedInt *num_suboperators) { 1046 CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 1047 *num_suboperators = op->num_suboperators; 1048 return CEED_ERROR_SUCCESS; 1049 } 1050 1051 /** 1052 @brief Get the list of sub-operators associated with a `CeedOperator` 1053 1054 @param[in] op `CeedOperator` 1055 @param[out] sub_operators Variable to store list of sub-operators 1056 1057 @return An error code: 0 - success, otherwise - failure 1058 1059 @ref Backend 1060 **/ 1061 int CeedCompositeOperatorGetSubList(CeedOperator op, CeedOperator **sub_operators) { 1062 CeedCheck(op->is_composite, op->ceed, CEED_ERROR_MINOR, "Only defined for a composite operator"); 1063 *sub_operators = op->sub_operators; 1064 return CEED_ERROR_SUCCESS; 1065 } 1066 1067 /** 1068 @brief Check if a `CeedOperator` is ready to be used. 1069 1070 @param[in] op `CeedOperator` to check 1071 1072 @return An error code: 0 - success, otherwise - failure 1073 1074 @ref User 1075 **/ 1076 int CeedOperatorCheckReady(CeedOperator op) { 1077 Ceed ceed; 1078 1079 CeedCall(CeedOperatorGetCeed(op, &ceed)); 1080 1081 if (op->is_interface_setup) return CEED_ERROR_SUCCESS; 1082 1083 CeedQFunction qf = op->qf; 1084 if (op->is_composite) { 1085 if (!op->num_suboperators) { 1086 // Empty operator setup 1087 op->input_size = 0; 1088 op->output_size = 0; 1089 } else { 1090 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1091 CeedCall(CeedOperatorCheckReady(op->sub_operators[i])); 1092 } 1093 // Sub-operators could be modified after adding to composite operator 1094 // Need to verify no lvec incompatibility from any changes 1095 CeedSize input_size, output_size; 1096 CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); 1097 } 1098 } else { 1099 CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set"); 1100 CeedCheck(op->num_fields == qf->num_input_fields + qf->num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set"); 1101 CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required"); 1102 CeedCheck(op->num_qpts > 0 || op->is_at_points, ceed, CEED_ERROR_INCOMPLETE, 1103 "At least one non-collocated CeedBasis is required or the number of quadrature points must be set"); 1104 } 1105 1106 // Flag as immutable and ready 1107 op->is_interface_setup = true; 1108 if (op->qf && op->qf != CEED_QFUNCTION_NONE) op->qf->is_immutable = true; 1109 if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) op->dqf->is_immutable = true; 1110 if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) op->dqfT->is_immutable = true; 1111 return CEED_ERROR_SUCCESS; 1112 } 1113 1114 /** 1115 @brief Get vector lengths for the active input and/or output `CeedVector` of a `CeedOperator`. 1116 1117 Note: Lengths of `-1` indicate that the CeedOperator does not have an active input and/or output. 1118 1119 @param[in] op `CeedOperator` 1120 @param[out] input_size Variable to store active input vector length, or `NULL` 1121 @param[out] output_size Variable to store active output vector length, or `NULL` 1122 1123 @return An error code: 0 - success, otherwise - failure 1124 1125 @ref User 1126 **/ 1127 int CeedOperatorGetActiveVectorLengths(CeedOperator op, CeedSize *input_size, CeedSize *output_size) { 1128 bool is_composite; 1129 1130 if (input_size) *input_size = op->input_size; 1131 if (output_size) *output_size = op->output_size; 1132 1133 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1134 if (is_composite && (op->input_size == -1 || op->output_size == -1)) { 1135 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1136 CeedSize sub_input_size, sub_output_size; 1137 1138 CeedCall(CeedOperatorGetActiveVectorLengths(op->sub_operators[i], &sub_input_size, &sub_output_size)); 1139 if (op->input_size == -1) op->input_size = sub_input_size; 1140 if (op->output_size == -1) op->output_size = sub_output_size; 1141 // Note, a size of -1 means no active vector restriction set, so no incompatibility 1142 CeedCheck((sub_input_size == -1 || sub_input_size == op->input_size) && (sub_output_size == -1 || sub_output_size == op->output_size), op->ceed, 1143 CEED_ERROR_MAJOR, 1144 "Sub-operators must have compatible dimensions; composite operator of shape (%td, %td) not compatible with sub-operator of " 1145 "shape (%td, %td)", 1146 op->input_size, op->output_size, input_size, output_size); 1147 } 1148 } 1149 return CEED_ERROR_SUCCESS; 1150 } 1151 1152 /** 1153 @brief Set reuse of `CeedQFunction` data in `CeedOperatorLinearAssemble*()` functions. 1154 1155 When `reuse_assembly_data = false` (default), the `CeedQFunction` associated with this `CeedOperator` is re-assembled every time a `CeedOperatorLinearAssemble*()` function is called. 1156 When `reuse_assembly_data = true`, the `CeedQFunction` associated with this `CeedOperator` is reused between calls to @ref CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(). 1157 1158 @param[in] op `CeedOperator` 1159 @param[in] reuse_assembly_data Boolean flag setting assembly data reuse 1160 1161 @return An error code: 0 - success, otherwise - failure 1162 1163 @ref Advanced 1164 **/ 1165 int CeedOperatorSetQFunctionAssemblyReuse(CeedOperator op, bool reuse_assembly_data) { 1166 bool is_composite; 1167 1168 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1169 if (is_composite) { 1170 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1171 CeedCall(CeedOperatorSetQFunctionAssemblyReuse(op->sub_operators[i], reuse_assembly_data)); 1172 } 1173 } else { 1174 CeedCall(CeedQFunctionAssemblyDataSetReuse(op->qf_assembled, reuse_assembly_data)); 1175 } 1176 return CEED_ERROR_SUCCESS; 1177 } 1178 1179 /** 1180 @brief Mark `CeedQFunction` data as updated and the `CeedQFunction` as requiring re-assembly. 1181 1182 @param[in] op `CeedOperator` 1183 @param[in] needs_data_update Boolean flag setting assembly data reuse 1184 1185 @return An error code: 0 - success, otherwise - failure 1186 1187 @ref Advanced 1188 **/ 1189 int CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(CeedOperator op, bool needs_data_update) { 1190 bool is_composite; 1191 1192 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1193 if (is_composite) { 1194 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1195 CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op->sub_operators[i], needs_data_update)); 1196 } 1197 } else { 1198 CeedCall(CeedQFunctionAssemblyDataSetUpdateNeeded(op->qf_assembled, needs_data_update)); 1199 } 1200 return CEED_ERROR_SUCCESS; 1201 } 1202 1203 /** 1204 @brief Set name of `CeedOperator` for @ref CeedOperatorView() output 1205 1206 @param[in,out] op `CeedOperator` 1207 @param[in] name Name to set, or NULL to remove previously set name 1208 1209 @return An error code: 0 - success, otherwise - failure 1210 1211 @ref User 1212 **/ 1213 int CeedOperatorSetName(CeedOperator op, const char *name) { 1214 char *name_copy; 1215 size_t name_len = name ? strlen(name) : 0; 1216 1217 CeedCall(CeedFree(&op->name)); 1218 if (name_len > 0) { 1219 CeedCall(CeedCalloc(name_len + 1, &name_copy)); 1220 memcpy(name_copy, name, name_len); 1221 op->name = name_copy; 1222 } 1223 return CEED_ERROR_SUCCESS; 1224 } 1225 1226 /** 1227 @brief View a `CeedOperator` 1228 1229 @param[in] op `CeedOperator` to view 1230 @param[in] stream Stream to write; typically `stdout` or a file 1231 1232 @return Error code: 0 - success, otherwise - failure 1233 1234 @ref User 1235 **/ 1236 int CeedOperatorView(CeedOperator op, FILE *stream) { 1237 bool has_name = op->name; 1238 1239 if (op->is_composite) { 1240 fprintf(stream, "Composite CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 1241 1242 for (CeedInt i = 0; i < op->num_suboperators; i++) { 1243 has_name = op->sub_operators[i]->name; 1244 fprintf(stream, " SubOperator %" CeedInt_FMT "%s%s:\n", i, has_name ? " - " : "", has_name ? op->sub_operators[i]->name : ""); 1245 CeedCall(CeedOperatorSingleView(op->sub_operators[i], 1, stream)); 1246 } 1247 } else { 1248 fprintf(stream, "CeedOperator%s%s\n", has_name ? " - " : "", has_name ? op->name : ""); 1249 CeedCall(CeedOperatorSingleView(op, 0, stream)); 1250 } 1251 return CEED_ERROR_SUCCESS; 1252 } 1253 1254 /** 1255 @brief Get the `Ceed` associated with a `CeedOperator` 1256 1257 @param[in] op `CeedOperator` 1258 @param[out] ceed Variable to store `Ceed` 1259 1260 @return An error code: 0 - success, otherwise - failure 1261 1262 @ref Advanced 1263 **/ 1264 int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) { 1265 *ceed = op->ceed; 1266 return CEED_ERROR_SUCCESS; 1267 } 1268 1269 /** 1270 @brief Get the number of elements associated with a `CeedOperator` 1271 1272 @param[in] op `CeedOperator` 1273 @param[out] num_elem Variable to store number of elements 1274 1275 @return An error code: 0 - success, otherwise - failure 1276 1277 @ref Advanced 1278 **/ 1279 int CeedOperatorGetNumElements(CeedOperator op, CeedInt *num_elem) { 1280 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1281 *num_elem = op->num_elem; 1282 return CEED_ERROR_SUCCESS; 1283 } 1284 1285 /** 1286 @brief Get the number of quadrature points associated with a `CeedOperator` 1287 1288 @param[in] op `CeedOperator` 1289 @param[out] num_qpts Variable to store vector number of quadrature points 1290 1291 @return An error code: 0 - success, otherwise - failure 1292 1293 @ref Advanced 1294 **/ 1295 int CeedOperatorGetNumQuadraturePoints(CeedOperator op, CeedInt *num_qpts) { 1296 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_MINOR, "Not defined for composite operator"); 1297 *num_qpts = op->num_qpts; 1298 return CEED_ERROR_SUCCESS; 1299 } 1300 1301 /** 1302 @brief Estimate number of FLOPs required to apply `CeedOperator` on the active `CeedVector` 1303 1304 @param[in] op `CeedOperator` to estimate FLOPs for 1305 @param[out] flops Address of variable to hold FLOPs estimate 1306 1307 @ref Backend 1308 **/ 1309 int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { 1310 bool is_composite; 1311 1312 CeedCall(CeedOperatorCheckReady(op)); 1313 1314 *flops = 0; 1315 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1316 if (is_composite) { 1317 CeedInt num_suboperators; 1318 1319 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1320 CeedOperator *sub_operators; 1321 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1322 1323 // FLOPs for each suboperator 1324 for (CeedInt i = 0; i < num_suboperators; i++) { 1325 CeedSize suboperator_flops; 1326 1327 CeedCall(CeedOperatorGetFlopsEstimate(sub_operators[i], &suboperator_flops)); 1328 *flops += suboperator_flops; 1329 } 1330 } else { 1331 CeedInt num_input_fields, num_output_fields, num_elem = 0; 1332 CeedOperatorField *input_fields, *output_fields; 1333 1334 CeedCall(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); 1335 CeedCall(CeedOperatorGetNumElements(op, &num_elem)); 1336 1337 // Input FLOPs 1338 for (CeedInt i = 0; i < num_input_fields; i++) { 1339 if (input_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1340 CeedSize rstr_flops, basis_flops; 1341 1342 CeedCall(CeedElemRestrictionGetFlopsEstimate(input_fields[i]->elem_rstr, CEED_NOTRANSPOSE, &rstr_flops)); 1343 *flops += rstr_flops; 1344 CeedCall(CeedBasisGetFlopsEstimate(input_fields[i]->basis, CEED_NOTRANSPOSE, op->qf->input_fields[i]->eval_mode, &basis_flops)); 1345 *flops += basis_flops * num_elem; 1346 } 1347 } 1348 // QF FLOPs 1349 { 1350 CeedInt num_qpts; 1351 CeedSize qf_flops; 1352 1353 CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); 1354 CeedCall(CeedQFunctionGetFlopsEstimate(op->qf, &qf_flops)); 1355 *flops += num_elem * num_qpts * qf_flops; 1356 } 1357 1358 // Output FLOPs 1359 for (CeedInt i = 0; i < num_output_fields; i++) { 1360 if (output_fields[i]->vec == CEED_VECTOR_ACTIVE) { 1361 CeedSize rstr_flops, basis_flops; 1362 1363 CeedCall(CeedElemRestrictionGetFlopsEstimate(output_fields[i]->elem_rstr, CEED_TRANSPOSE, &rstr_flops)); 1364 *flops += rstr_flops; 1365 CeedCall(CeedBasisGetFlopsEstimate(output_fields[i]->basis, CEED_TRANSPOSE, op->qf->output_fields[i]->eval_mode, &basis_flops)); 1366 *flops += basis_flops * num_elem; 1367 } 1368 } 1369 } 1370 return CEED_ERROR_SUCCESS; 1371 } 1372 1373 /** 1374 @brief Get `CeedQFunction` global context for a `CeedOperator`. 1375 1376 The caller is responsible for destroying `ctx` returned from this function via @ref CeedQFunctionContextDestroy(). 1377 1378 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`. 1379 This `CeedQFunctionContext` will be destroyed if `ctx` is the only reference to this `CeedQFunctionContext`. 1380 1381 @param[in] op `CeedOperator` 1382 @param[out] ctx Variable to store `CeedQFunctionContext` 1383 1384 @return An error code: 0 - success, otherwise - failure 1385 1386 @ref Advanced 1387 **/ 1388 int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) { 1389 CeedCheck(!op->is_composite, op->ceed, CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator"); 1390 if (op->qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(op->qf->ctx, ctx)); 1391 else *ctx = NULL; 1392 return CEED_ERROR_SUCCESS; 1393 } 1394 1395 /** 1396 @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`. 1397 1398 Fields are registered via `CeedQFunctionContextRegister*()` functions (eg. @ref CeedQFunctionContextRegisterDouble()). 1399 1400 @param[in] op `CeedOperator` 1401 @param[in] field_name Name of field to retrieve label 1402 @param[out] field_label Variable to field label 1403 1404 @return An error code: 0 - success, otherwise - failure 1405 1406 @ref User 1407 **/ 1408 int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, CeedContextFieldLabel *field_label) { 1409 bool is_composite, field_found = false; 1410 1411 CeedCall(CeedOperatorIsComposite(op, &is_composite)); 1412 1413 if (is_composite) { 1414 // Composite operator 1415 // -- Check if composite label already created 1416 for (CeedInt i = 0; i < op->num_context_labels; i++) { 1417 if (!strcmp(op->context_labels[i]->name, field_name)) { 1418 *field_label = op->context_labels[i]; 1419 return CEED_ERROR_SUCCESS; 1420 } 1421 } 1422 1423 // -- Create composite label if needed 1424 CeedInt num_sub; 1425 CeedOperator *sub_operators; 1426 CeedContextFieldLabel new_field_label; 1427 1428 CeedCall(CeedCalloc(1, &new_field_label)); 1429 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); 1430 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1431 CeedCall(CeedCalloc(num_sub, &new_field_label->sub_labels)); 1432 new_field_label->num_sub_labels = num_sub; 1433 1434 for (CeedInt i = 0; i < num_sub; i++) { 1435 if (sub_operators[i]->qf->ctx) { 1436 CeedContextFieldLabel new_field_label_i; 1437 1438 CeedCall(CeedQFunctionContextGetFieldLabel(sub_operators[i]->qf->ctx, field_name, &new_field_label_i)); 1439 if (new_field_label_i) { 1440 field_found = true; 1441 new_field_label->sub_labels[i] = new_field_label_i; 1442 new_field_label->name = new_field_label_i->name; 1443 new_field_label->description = new_field_label_i->description; 1444 if (new_field_label->type && new_field_label->type != new_field_label_i->type) { 1445 // LCOV_EXCL_START 1446 CeedCall(CeedFree(&new_field_label)); 1447 return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field types on sub-operator contexts. %s != %s", 1448 CeedContextFieldTypes[new_field_label->type], CeedContextFieldTypes[new_field_label_i->type]); 1449 // LCOV_EXCL_STOP 1450 } else { 1451 new_field_label->type = new_field_label_i->type; 1452 } 1453 if (new_field_label->num_values != 0 && new_field_label->num_values != new_field_label_i->num_values) { 1454 // LCOV_EXCL_START 1455 CeedCall(CeedFree(&new_field_label)); 1456 return CeedError(op->ceed, CEED_ERROR_INCOMPATIBLE, "Incompatible field number of values on sub-operator contexts. %ld != %ld", 1457 new_field_label->num_values, new_field_label_i->num_values); 1458 // LCOV_EXCL_STOP 1459 } else { 1460 new_field_label->num_values = new_field_label_i->num_values; 1461 } 1462 } 1463 } 1464 } 1465 // -- Cleanup if field was found 1466 if (field_found) { 1467 *field_label = new_field_label; 1468 } else { 1469 // LCOV_EXCL_START 1470 CeedCall(CeedFree(&new_field_label->sub_labels)); 1471 CeedCall(CeedFree(&new_field_label)); 1472 *field_label = NULL; 1473 // LCOV_EXCL_STOP 1474 } 1475 } else { 1476 // Single, non-composite operator 1477 if (op->qf->ctx) { 1478 CeedCall(CeedQFunctionContextGetFieldLabel(op->qf->ctx, field_name, field_label)); 1479 } else { 1480 *field_label = NULL; 1481 } 1482 } 1483 1484 // Set label in operator 1485 if (*field_label) { 1486 (*field_label)->from_op = true; 1487 1488 // Move new composite label to operator 1489 if (op->num_context_labels == 0) { 1490 CeedCall(CeedCalloc(1, &op->context_labels)); 1491 op->max_context_labels = 1; 1492 } else if (op->num_context_labels == op->max_context_labels) { 1493 CeedCall(CeedRealloc(2 * op->num_context_labels, &op->context_labels)); 1494 op->max_context_labels *= 2; 1495 } 1496 op->context_labels[op->num_context_labels] = *field_label; 1497 op->num_context_labels++; 1498 } 1499 return CEED_ERROR_SUCCESS; 1500 } 1501 1502 /** 1503 @brief Set `CeedQFunctionContext` field holding double precision values. 1504 1505 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1506 1507 @param[in,out] op `CeedOperator` 1508 @param[in] field_label Label of field to set 1509 @param[in] values Values to set 1510 1511 @return An error code: 0 - success, otherwise - failure 1512 1513 @ref User 1514 **/ 1515 int CeedOperatorSetContextDouble(CeedOperator op, CeedContextFieldLabel field_label, double *values) { 1516 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1517 } 1518 1519 /** 1520 @brief Get `CeedQFunctionContext` field holding double precision values, read-only. 1521 1522 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1523 1524 @param[in] op `CeedOperator` 1525 @param[in] field_label Label of field to get 1526 @param[out] num_values Number of values in the field label 1527 @param[out] values Pointer to context values 1528 1529 @return An error code: 0 - success, otherwise - failure 1530 1531 @ref User 1532 **/ 1533 int CeedOperatorGetContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const double **values) { 1534 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values); 1535 } 1536 1537 /** 1538 @brief Restore `CeedQFunctionContext` field holding double precision values, read-only. 1539 1540 @param[in] op `CeedOperator` 1541 @param[in] field_label Label of field to restore 1542 @param[out] values Pointer to context values 1543 1544 @return An error code: 0 - success, otherwise - failure 1545 1546 @ref User 1547 **/ 1548 int CeedOperatorRestoreContextDoubleRead(CeedOperator op, CeedContextFieldLabel field_label, const double **values) { 1549 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_DOUBLE, values); 1550 } 1551 1552 /** 1553 @brief Set `CeedQFunctionContext` field holding `int32` values. 1554 1555 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1556 1557 @param[in,out] op `CeedOperator` 1558 @param[in] field_label Label of field to set 1559 @param[in] values Values to set 1560 1561 @return An error code: 0 - success, otherwise - failure 1562 1563 @ref User 1564 **/ 1565 int CeedOperatorSetContextInt32(CeedOperator op, CeedContextFieldLabel field_label, int32_t *values) { 1566 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1567 } 1568 1569 /** 1570 @brief Get `CeedQFunctionContext` field holding `int32` values, read-only. 1571 1572 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1573 1574 @param[in] op `CeedOperator` 1575 @param[in] field_label Label of field to get 1576 @param[out] num_values Number of `int32` values in `values` 1577 @param[out] values Pointer to context values 1578 1579 @return An error code: 0 - success, otherwise - failure 1580 1581 @ref User 1582 **/ 1583 int CeedOperatorGetContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) { 1584 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values); 1585 } 1586 1587 /** 1588 @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only. 1589 1590 @param[in] op `CeedOperator` 1591 @param[in] field_label Label of field to get 1592 @param[out] values Pointer to context values 1593 1594 @return An error code: 0 - success, otherwise - failure 1595 1596 @ref User 1597 **/ 1598 int CeedOperatorRestoreContextInt32Read(CeedOperator op, CeedContextFieldLabel field_label, const int32_t **values) { 1599 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_INT32, values); 1600 } 1601 1602 /** 1603 @brief Set `CeedQFunctionContext` field holding boolean values. 1604 1605 For composite operators, the values are set in all sub-operator `CeedQFunctionContext` that have a matching `field_name`. 1606 1607 @param[in,out] op `CeedOperator` 1608 @param[in] field_label Label of field to set 1609 @param[in] values Values to set 1610 1611 @return An error code: 0 - success, otherwise - failure 1612 1613 @ref User 1614 **/ 1615 int CeedOperatorSetContextBoolean(CeedOperator op, CeedContextFieldLabel field_label, bool *values) { 1616 return CeedOperatorContextSetGeneric(op, field_label, CEED_CONTEXT_FIELD_BOOL, values); 1617 } 1618 1619 /** 1620 @brief Get `CeedQFunctionContext` field holding boolean values, read-only. 1621 1622 For composite operators, the values correspond to the first sub-operator `CeedQFunctionContext` that has a matching `field_name`. 1623 1624 @param[in] op `CeedOperator` 1625 @param[in] field_label Label of field to get 1626 @param[out] num_values Number of boolean values in `values` 1627 @param[out] values Pointer to context values 1628 1629 @return An error code: 0 - success, otherwise - failure 1630 1631 @ref User 1632 **/ 1633 int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) { 1634 return CeedOperatorContextGetGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values); 1635 } 1636 1637 /** 1638 @brief Restore `CeedQFunctionContext` field holding boolean values, read-only. 1639 1640 @param[in] op `CeedOperator` 1641 @param[in] field_label Label of field to get 1642 @param[out] values Pointer to context values 1643 1644 @return An error code: 0 - success, otherwise - failure 1645 1646 @ref User 1647 **/ 1648 int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values) { 1649 return CeedOperatorContextRestoreGenericRead(op, field_label, CEED_CONTEXT_FIELD_BOOL, values); 1650 } 1651 1652 /** 1653 @brief Apply `CeedOperator` to a `CeedVector`. 1654 1655 This computes the action of the operator on the specified (active) input, yielding its (active) output. 1656 All inputs and outputs must be specified using @ref CeedOperatorSetField(). 1657 1658 Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable. 1659 1660 @param[in] op `CeedOperator` to apply 1661 @param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 1662 @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 1663 @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1664 1665 @return An error code: 0 - success, otherwise - failure 1666 1667 @ref User 1668 **/ 1669 int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 1670 CeedCall(CeedOperatorCheckReady(op)); 1671 1672 if (op->is_composite) { 1673 // Composite Operator 1674 if (op->ApplyComposite) { 1675 CeedCall(op->ApplyComposite(op, in, out, request)); 1676 } else { 1677 CeedInt num_suboperators; 1678 CeedOperator *sub_operators; 1679 1680 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1681 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1682 1683 // Zero all output vectors 1684 if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0)); 1685 for (CeedInt i = 0; i < num_suboperators; i++) { 1686 for (CeedInt j = 0; j < sub_operators[i]->qf->num_output_fields; j++) { 1687 CeedVector vec = sub_operators[i]->output_fields[j]->vec; 1688 1689 if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) { 1690 CeedCall(CeedVectorSetValue(vec, 0.0)); 1691 } 1692 } 1693 } 1694 // Apply 1695 for (CeedInt i = 0; i < num_suboperators; i++) { 1696 CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 1697 } 1698 } 1699 } else { 1700 // Standard Operator 1701 if (op->Apply) { 1702 CeedCall(op->Apply(op, in, out, request)); 1703 } else { 1704 // Zero all output vectors 1705 CeedQFunction qf = op->qf; 1706 1707 for (CeedInt i = 0; i < qf->num_output_fields; i++) { 1708 CeedVector vec = op->output_fields[i]->vec; 1709 1710 if (vec == CEED_VECTOR_ACTIVE) vec = out; 1711 if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0)); 1712 } 1713 // Apply 1714 if (op->num_elem) CeedCall(op->ApplyAdd(op, in, out, request)); 1715 } 1716 } 1717 return CEED_ERROR_SUCCESS; 1718 } 1719 1720 /** 1721 @brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`. 1722 1723 This computes the action of the operator on the specified (active) input, yielding its (active) output. 1724 All inputs and outputs must be specified using @ref CeedOperatorSetField(). 1725 1726 @param[in] op `CeedOperator` to apply 1727 @param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs 1728 @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 1729 @param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE 1730 1731 @return An error code: 0 - success, otherwise - failure 1732 1733 @ref User 1734 **/ 1735 int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) { 1736 CeedCall(CeedOperatorCheckReady(op)); 1737 1738 if (op->is_composite) { 1739 // Composite Operator 1740 if (op->ApplyAddComposite) { 1741 CeedCall(op->ApplyAddComposite(op, in, out, request)); 1742 } else { 1743 CeedInt num_suboperators; 1744 CeedOperator *sub_operators; 1745 1746 CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); 1747 CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); 1748 for (CeedInt i = 0; i < num_suboperators; i++) { 1749 CeedCall(CeedOperatorApplyAdd(sub_operators[i], in, out, request)); 1750 } 1751 } 1752 } else if (op->num_elem) { 1753 // Standard Operator 1754 CeedCall(op->ApplyAdd(op, in, out, request)); 1755 } 1756 return CEED_ERROR_SUCCESS; 1757 } 1758 1759 /** 1760 @brief Destroy a `CeedOperator` 1761 1762 @param[in,out] op `CeedOperator` to destroy 1763 1764 @return An error code: 0 - success, otherwise - failure 1765 1766 @ref User 1767 **/ 1768 int CeedOperatorDestroy(CeedOperator *op) { 1769 if (!*op || --(*op)->ref_count > 0) { 1770 *op = NULL; 1771 return CEED_ERROR_SUCCESS; 1772 } 1773 if ((*op)->Destroy) CeedCall((*op)->Destroy(*op)); 1774 CeedCall(CeedDestroy(&(*op)->ceed)); 1775 // Free fields 1776 for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1777 if ((*op)->input_fields[i]) { 1778 if ((*op)->input_fields[i]->elem_rstr != CEED_ELEMRESTRICTION_NONE) { 1779 CeedCall(CeedElemRestrictionDestroy(&(*op)->input_fields[i]->elem_rstr)); 1780 } 1781 if ((*op)->input_fields[i]->basis != CEED_BASIS_NONE) { 1782 CeedCall(CeedBasisDestroy(&(*op)->input_fields[i]->basis)); 1783 } 1784 if ((*op)->input_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->input_fields[i]->vec != CEED_VECTOR_NONE) { 1785 CeedCall(CeedVectorDestroy(&(*op)->input_fields[i]->vec)); 1786 } 1787 CeedCall(CeedFree(&(*op)->input_fields[i]->field_name)); 1788 CeedCall(CeedFree(&(*op)->input_fields[i])); 1789 } 1790 } 1791 for (CeedInt i = 0; i < (*op)->num_fields; i++) { 1792 if ((*op)->output_fields[i]) { 1793 CeedCall(CeedElemRestrictionDestroy(&(*op)->output_fields[i]->elem_rstr)); 1794 if ((*op)->output_fields[i]->basis != CEED_BASIS_NONE) { 1795 CeedCall(CeedBasisDestroy(&(*op)->output_fields[i]->basis)); 1796 } 1797 if ((*op)->output_fields[i]->vec != CEED_VECTOR_ACTIVE && (*op)->output_fields[i]->vec != CEED_VECTOR_NONE) { 1798 CeedCall(CeedVectorDestroy(&(*op)->output_fields[i]->vec)); 1799 } 1800 CeedCall(CeedFree(&(*op)->output_fields[i]->field_name)); 1801 CeedCall(CeedFree(&(*op)->output_fields[i])); 1802 } 1803 } 1804 // AtPoints data 1805 CeedCall(CeedVectorDestroy(&(*op)->point_coords)); 1806 CeedCall(CeedElemRestrictionDestroy(&(*op)->rstr_points)); 1807 CeedCall(CeedElemRestrictionDestroy(&(*op)->first_points_rstr)); 1808 // Destroy sub_operators 1809 for (CeedInt i = 0; i < (*op)->num_suboperators; i++) { 1810 if ((*op)->sub_operators[i]) { 1811 CeedCall(CeedOperatorDestroy(&(*op)->sub_operators[i])); 1812 } 1813 } 1814 CeedCall(CeedQFunctionDestroy(&(*op)->qf)); 1815 CeedCall(CeedQFunctionDestroy(&(*op)->dqf)); 1816 CeedCall(CeedQFunctionDestroy(&(*op)->dqfT)); 1817 // Destroy any composite labels 1818 if ((*op)->is_composite) { 1819 for (CeedInt i = 0; i < (*op)->num_context_labels; i++) { 1820 CeedCall(CeedFree(&(*op)->context_labels[i]->sub_labels)); 1821 CeedCall(CeedFree(&(*op)->context_labels[i])); 1822 } 1823 } 1824 CeedCall(CeedFree(&(*op)->context_labels)); 1825 1826 // Destroy fallback 1827 CeedCall(CeedOperatorDestroy(&(*op)->op_fallback)); 1828 1829 // Destroy assembly data 1830 CeedCall(CeedQFunctionAssemblyDataDestroy(&(*op)->qf_assembled)); 1831 CeedCall(CeedOperatorAssemblyDataDestroy(&(*op)->op_assembled)); 1832 1833 CeedCall(CeedFree(&(*op)->input_fields)); 1834 CeedCall(CeedFree(&(*op)->output_fields)); 1835 CeedCall(CeedFree(&(*op)->sub_operators)); 1836 CeedCall(CeedFree(&(*op)->name)); 1837 CeedCall(CeedFree(op)); 1838 return CEED_ERROR_SUCCESS; 1839 } 1840 1841 /// @} 1842