19ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3777ff853SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5777ff853SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7777ff853SJeremy L Thompson 83d576824SJeremy L Thompson #include <ceed-impl.h> 949aac155SJeremy L Thompson #include <ceed.h> 102b730f8bSJeremy L Thompson #include <ceed/backend.h> 1149aac155SJeremy L Thompson #include <stdbool.h> 123d576824SJeremy L Thompson #include <stdint.h> 133d576824SJeremy L Thompson #include <stdio.h> 14cdf32b93SJeremy L Thompson #include <string.h> 15777ff853SJeremy L Thompson 16777ff853SJeremy L Thompson /// @file 17777ff853SJeremy L Thompson /// Implementation of public CeedQFunctionContext interfaces 18777ff853SJeremy L Thompson 19777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 20cdf32b93SJeremy L Thompson /// CeedQFunctionContext Library Internal Functions 21cdf32b93SJeremy L Thompson /// ---------------------------------------------------------------------------- 22cdf32b93SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper 23cdf32b93SJeremy L Thompson /// @{ 24cdf32b93SJeremy L Thompson 25cdf32b93SJeremy L Thompson /** 26ca94c3ddSJeremy L Thompson @brief Get index for `CeedQFunctionContext` field 27cdf32b93SJeremy L Thompson 28ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 29ea61e9acSJeremy L Thompson @param[in] field_name Name of field 30ca94c3ddSJeremy L Thompson @param[out] field_index Index of field, or `-1` if field is not registered 31cdf32b93SJeremy L Thompson 32cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 33cdf32b93SJeremy L Thompson 34cdf32b93SJeremy L Thompson @ref Developer 35cdf32b93SJeremy L Thompson **/ 362b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldIndex(CeedQFunctionContext ctx, const char *field_name, CeedInt *field_index) { 37cdf32b93SJeremy L Thompson *field_index = -1; 382b730f8bSJeremy L Thompson for (CeedInt i = 0; i < ctx->num_fields; i++) { 392b730f8bSJeremy L Thompson if (!strcmp(ctx->field_labels[i]->name, field_name)) *field_index = i; 402b730f8bSJeremy L Thompson } 41cdf32b93SJeremy L Thompson return CEED_ERROR_SUCCESS; 42cdf32b93SJeremy L Thompson } 43cdf32b93SJeremy L Thompson 44cdf32b93SJeremy L Thompson /** 45ca94c3ddSJeremy L Thompson @brief Common function for registering `CeedQFunctionContext` fields 46cdf32b93SJeremy L Thompson 47ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 48ea61e9acSJeremy L Thompson @param[in] field_name Name of field to register 49ea61e9acSJeremy L Thompson @param[in] field_offset Offset of field to register 50ca94c3ddSJeremy L Thompson @param[in] field_description Description of field, or `NULL` for none 51ca94c3ddSJeremy L Thompson @param[in] field_type @ref CeedContextFieldType 52ea61e9acSJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 53cdf32b93SJeremy L Thompson 54cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 55cdf32b93SJeremy L Thompson 56cdf32b93SJeremy L Thompson @ref Developer 57cdf32b93SJeremy L Thompson **/ 582b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterGeneric(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, const char *field_description, 595b6ec284SJeremy L Thompson CeedContextFieldType field_type, size_t num_values) { 605b6ec284SJeremy L Thompson size_t field_size = 0; 61cdf32b93SJeremy L Thompson CeedInt field_index = -1; 621c66c397SJeremy L Thompson 631c66c397SJeremy L Thompson // Check for duplicate 642b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index)); 659bc66399SJeremy L Thompson CeedCheck(field_index == -1, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 669bc66399SJeremy L Thompson "QFunctionContext field with name \"%s\" already registered", field_name); 67cdf32b93SJeremy L Thompson 68cdf32b93SJeremy L Thompson // Allocate space for field data 69cdf32b93SJeremy L Thompson if (ctx->num_fields == 0) { 702b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ctx->field_labels)); 71cdf32b93SJeremy L Thompson ctx->max_fields = 1; 72cdf32b93SJeremy L Thompson } else if (ctx->num_fields == ctx->max_fields) { 732b730f8bSJeremy L Thompson CeedCall(CeedRealloc(2 * ctx->max_fields, &ctx->field_labels)); 74cdf32b93SJeremy L Thompson ctx->max_fields *= 2; 75cdf32b93SJeremy L Thompson } 762b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, &ctx->field_labels[ctx->num_fields])); 77cdf32b93SJeremy L Thompson 785b6ec284SJeremy L Thompson // Compute field size 795b6ec284SJeremy L Thompson switch (field_type) { 805b6ec284SJeremy L Thompson case CEED_CONTEXT_FIELD_DOUBLE: 815b6ec284SJeremy L Thompson field_size = sizeof(double); 825b6ec284SJeremy L Thompson break; 835b6ec284SJeremy L Thompson case CEED_CONTEXT_FIELD_INT32: 845b6ec284SJeremy L Thompson field_size = sizeof(int); 855b6ec284SJeremy L Thompson break; 865b6ec284SJeremy L Thompson case CEED_CONTEXT_FIELD_BOOL: 875b6ec284SJeremy L Thompson field_size = sizeof(bool); 885b6ec284SJeremy L Thompson break; 895b6ec284SJeremy L Thompson } 905b6ec284SJeremy L Thompson 91cdf32b93SJeremy L Thompson // Copy field data 922b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_name, (char **)&ctx->field_labels[ctx->num_fields]->name)); 932b730f8bSJeremy L Thompson CeedCall(CeedStringAllocCopy(field_description, (char **)&ctx->field_labels[ctx->num_fields]->description)); 943668ca4bSJeremy L Thompson ctx->field_labels[ctx->num_fields]->type = field_type; 953668ca4bSJeremy L Thompson ctx->field_labels[ctx->num_fields]->offset = field_offset; 967bfe0f0eSJeremy L Thompson ctx->field_labels[ctx->num_fields]->size = field_size * num_values; 977bfe0f0eSJeremy L Thompson ctx->field_labels[ctx->num_fields]->num_values = num_values; 98cdf32b93SJeremy L Thompson ctx->num_fields++; 99cdf32b93SJeremy L Thompson return CEED_ERROR_SUCCESS; 100cdf32b93SJeremy L Thompson } 101cdf32b93SJeremy L Thompson 1023b190ab8SJeremy L Thompson /** 103ca94c3ddSJeremy L Thompson @brief Destroy user data held by `CeedQFunctionContext`, using function set by @ref CeedQFunctionContextSetDataDestroy(), if applicable 1043b190ab8SJeremy L Thompson 105ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` to destroy user data 1063b190ab8SJeremy L Thompson 1073b190ab8SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1083b190ab8SJeremy L Thompson 1093b190ab8SJeremy L Thompson @ref Developer 1103b190ab8SJeremy L Thompson **/ 1113b190ab8SJeremy L Thompson static int CeedQFunctionContextDestroyData(CeedQFunctionContext ctx) { 1123b190ab8SJeremy L Thompson if (ctx->DataDestroy) { 1132b730f8bSJeremy L Thompson CeedCall(ctx->DataDestroy(ctx)); 1143b190ab8SJeremy L Thompson } else { 1153b190ab8SJeremy L Thompson CeedMemType data_destroy_mem_type; 1161c66c397SJeremy L Thompson CeedQFunctionContextDataDestroyUser data_destroy_function; 1173b190ab8SJeremy L Thompson 1182b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function)); 1193b190ab8SJeremy L Thompson if (data_destroy_function) { 1203b190ab8SJeremy L Thompson void *data; 1213b190ab8SJeremy L Thompson 1222b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetData(ctx, data_destroy_mem_type, &data)); 1232b730f8bSJeremy L Thompson CeedCall(data_destroy_function(data)); 1242b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextRestoreData(ctx, &data)); 1253b190ab8SJeremy L Thompson } 1263b190ab8SJeremy L Thompson } 1273b190ab8SJeremy L Thompson return CEED_ERROR_SUCCESS; 1283b190ab8SJeremy L Thompson } 1293b190ab8SJeremy L Thompson 130b0f67a9cSJeremy L Thompson /** 131b0f67a9cSJeremy L Thompson @brief View a `CeedQFunctionContext` passed as a `CeedObject` 132b0f67a9cSJeremy L Thompson 133b0f67a9cSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to view 134b0f67a9cSJeremy L Thompson @param[in] stream Filestream to write to 135b0f67a9cSJeremy L Thompson 136b0f67a9cSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 137b0f67a9cSJeremy L Thompson 138b0f67a9cSJeremy L Thompson @ref Developer 139b0f67a9cSJeremy L Thompson **/ 140b0f67a9cSJeremy L Thompson static int CeedQFunctionContextView_Object(CeedObject ctx, FILE *stream) { 141b0f67a9cSJeremy L Thompson CeedCall(CeedQFunctionContextView((CeedQFunctionContext)ctx, stream)); 142b0f67a9cSJeremy L Thompson return CEED_ERROR_SUCCESS; 143b0f67a9cSJeremy L Thompson } 144b0f67a9cSJeremy L Thompson 145*6c328a79SJeremy L Thompson /** 146*6c328a79SJeremy L Thompson @brief Destroy a `CeedQFunctionContext` passed as a `CeedObject` 147*6c328a79SJeremy L Thompson 148*6c328a79SJeremy L Thompson @param[in,out] ctx Address of `CeedQFunctionContext` to destroy 149*6c328a79SJeremy L Thompson 150*6c328a79SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 151*6c328a79SJeremy L Thompson 152*6c328a79SJeremy L Thompson @ref Developer 153*6c328a79SJeremy L Thompson **/ 154*6c328a79SJeremy L Thompson static int CeedQFunctionContextDestroy_Object(CeedObject *ctx) { 155*6c328a79SJeremy L Thompson CeedCall(CeedQFunctionContextDestroy((CeedQFunctionContext *)ctx)); 156*6c328a79SJeremy L Thompson return CEED_ERROR_SUCCESS; 157*6c328a79SJeremy L Thompson } 158*6c328a79SJeremy L Thompson 159cdf32b93SJeremy L Thompson /// @} 160cdf32b93SJeremy L Thompson 161cdf32b93SJeremy L Thompson /// ---------------------------------------------------------------------------- 162777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API 163777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 164777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend 165777ff853SJeremy L Thompson /// @{ 166777ff853SJeremy L Thompson 167777ff853SJeremy L Thompson /** 168ca94c3ddSJeremy L Thompson @brief Get the `Ceed` associated with a `CeedQFunctionContext` 169777ff853SJeremy L Thompson 170ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 171ca94c3ddSJeremy L Thompson @param[out] ceed Variable to store `Ceed` 172777ff853SJeremy L Thompson 173777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 174777ff853SJeremy L Thompson 175777ff853SJeremy L Thompson @ref Backend 176777ff853SJeremy L Thompson **/ 177777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) { 178b0f67a9cSJeremy L Thompson CeedCall(CeedObjectGetCeed((CeedObject)ctx, ceed)); 179e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 180777ff853SJeremy L Thompson } 181777ff853SJeremy L Thompson 182777ff853SJeremy L Thompson /** 1836e536b99SJeremy L Thompson @brief Return the `Ceed` associated with a `CeedQFunctionContext` 1846e536b99SJeremy L Thompson 1856e536b99SJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 1866e536b99SJeremy L Thompson 1876e536b99SJeremy L Thompson @return `Ceed` associated with `ctx` 1886e536b99SJeremy L Thompson 1896e536b99SJeremy L Thompson @ref Backend 1906e536b99SJeremy L Thompson **/ 191b0f67a9cSJeremy L Thompson Ceed CeedQFunctionContextReturnCeed(CeedQFunctionContext ctx) { return CeedObjectReturnCeed((CeedObject)ctx); } 1926e536b99SJeremy L Thompson 1936e536b99SJeremy L Thompson /** 194ca94c3ddSJeremy L Thompson @brief Check for valid data in a `CeedQFunctionContext` 1959c774eddSJeremy L Thompson 196ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to check validity 1979c774eddSJeremy L Thompson @param[out] has_valid_data Variable to store validity 1989c774eddSJeremy L Thompson 1999c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2009c774eddSJeremy L Thompson 2019c774eddSJeremy L Thompson @ref Backend 2029c774eddSJeremy L Thompson **/ 2032b730f8bSJeremy L Thompson int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx, bool *has_valid_data) { 2046e536b99SJeremy L Thompson CeedCheck(ctx->HasValidData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 2056e536b99SJeremy L Thompson "Backend does not support CeedQFunctionContextHasValidData"); 2062b730f8bSJeremy L Thompson CeedCall(ctx->HasValidData(ctx, has_valid_data)); 2079c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 2089c774eddSJeremy L Thompson } 2099c774eddSJeremy L Thompson 2109c774eddSJeremy L Thompson /** 211ca94c3ddSJeremy L Thompson @brief Check for borrowed data of a specific @ref CeedMemType in a `CeedQFunctionContext` 2129c774eddSJeremy L Thompson 213ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to check 214ea61e9acSJeremy L Thompson @param[in] mem_type Memory type to check 2159c774eddSJeremy L Thompson @param[out] has_borrowed_data_of_type Variable to store result 2169c774eddSJeremy L Thompson 2179c774eddSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 2189c774eddSJeremy L Thompson 2199c774eddSJeremy L Thompson @ref Backend 2209c774eddSJeremy L Thompson **/ 2212b730f8bSJeremy L Thompson int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) { 2226e536b99SJeremy L Thompson CeedCheck(ctx->HasBorrowedDataOfType, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 2236e536b99SJeremy L Thompson "Backend does not support CeedQFunctionContextHasBorrowedDataOfType"); 2242b730f8bSJeremy L Thompson CeedCall(ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type)); 2259c774eddSJeremy L Thompson return CEED_ERROR_SUCCESS; 2269c774eddSJeremy L Thompson } 2279c774eddSJeremy L Thompson 2289c774eddSJeremy L Thompson /** 229ca94c3ddSJeremy L Thompson @brief Get the state of a `CeedQFunctionContext` 230777ff853SJeremy L Thompson 231ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to retrieve state 232777ff853SJeremy L Thompson @param[out] state Variable to store state 233777ff853SJeremy L Thompson 234777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 235777ff853SJeremy L Thompson 236777ff853SJeremy L Thompson @ref Backend 237777ff853SJeremy L Thompson **/ 238777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) { 239777ff853SJeremy L Thompson *state = ctx->state; 240e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 241777ff853SJeremy L Thompson } 242777ff853SJeremy L Thompson 243777ff853SJeremy L Thompson /** 244ca94c3ddSJeremy L Thompson @brief Get backend data of a `CeedQFunctionContext` 245777ff853SJeremy L Thompson 246ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 247777ff853SJeremy L Thompson @param[out] data Variable to store data 248777ff853SJeremy L Thompson 249777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 250777ff853SJeremy L Thompson 251777ff853SJeremy L Thompson @ref Backend 252777ff853SJeremy L Thompson **/ 253777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) { 254777ff853SJeremy L Thompson *(void **)data = ctx->data; 255e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 256777ff853SJeremy L Thompson } 257777ff853SJeremy L Thompson 258777ff853SJeremy L Thompson /** 259ca94c3ddSJeremy L Thompson @brief Set backend data of a `CeedQFunctionContext` 260777ff853SJeremy L Thompson 261ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 262ea61e9acSJeremy L Thompson @param[in] data Data to set 263777ff853SJeremy L Thompson 264777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 265777ff853SJeremy L Thompson 266777ff853SJeremy L Thompson @ref Backend 267777ff853SJeremy L Thompson **/ 268777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) { 269777ff853SJeremy L Thompson ctx->data = data; 270e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 271777ff853SJeremy L Thompson } 272777ff853SJeremy L Thompson 27334359f16Sjeremylt /** 274ca94c3ddSJeremy L Thompson @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name` 275e6a0ab89SJeremy L Thompson 276ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 277e6a0ab89SJeremy L Thompson @param[in] field_name Name of field to retrieve label 278e6a0ab89SJeremy L Thompson @param[out] field_label Variable to field label 279e6a0ab89SJeremy L Thompson 280e6a0ab89SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 281e6a0ab89SJeremy L Thompson 2823e1e85abSJeremy L Thompson @ref Backend 283e6a0ab89SJeremy L Thompson **/ 2842b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx, const char *field_name, CeedContextFieldLabel *field_label) { 285e6a0ab89SJeremy L Thompson CeedInt field_index; 2861c66c397SJeremy L Thompson 2872b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index)); 288e6a0ab89SJeremy L Thompson 289e6a0ab89SJeremy L Thompson if (field_index != -1) { 290e6a0ab89SJeremy L Thompson *field_label = ctx->field_labels[field_index]; 291e6a0ab89SJeremy L Thompson } else { 292e6a0ab89SJeremy L Thompson *field_label = NULL; 293e6a0ab89SJeremy L Thompson } 294e6a0ab89SJeremy L Thompson return CEED_ERROR_SUCCESS; 295e6a0ab89SJeremy L Thompson } 296e6a0ab89SJeremy L Thompson 297e6a0ab89SJeremy L Thompson /** 298ca94c3ddSJeremy L Thompson @brief Set `CeedQFunctionContext` field 299d8dd9a91SJeremy L Thompson 300ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 301ea61e9acSJeremy L Thompson @param[in] field_label Label of field to set 302ea61e9acSJeremy L Thompson @param[in] field_type Type of field to set 3032788fa27SJeremy L Thompson @param[in] values Value to set 304d8dd9a91SJeremy L Thompson 305d8dd9a91SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 306d8dd9a91SJeremy L Thompson 3073e1e85abSJeremy L Thompson @ref Backend 308d8dd9a91SJeremy L Thompson **/ 3092788fa27SJeremy L Thompson int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { 3101a34d7dcSJeremy L Thompson bool is_different; 3111c66c397SJeremy L Thompson char *data; 3121c66c397SJeremy L Thompson 3133668ca4bSJeremy L Thompson // Check field type 3146e536b99SJeremy L Thompson CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 3156574a04fSJeremy L Thompson "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name, 3166574a04fSJeremy L Thompson CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 317d8dd9a91SJeremy L Thompson 3181a34d7dcSJeremy L Thompson CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data)); 3191a34d7dcSJeremy L Thompson is_different = memcmp(&data[field_label->offset], values, field_label->size); 3201a34d7dcSJeremy L Thompson CeedCall(CeedQFunctionContextRestoreDataRead(ctx, &data)); 3211a34d7dcSJeremy L Thompson if (is_different) { 3222b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data)); 3232788fa27SJeremy L Thompson memcpy(&data[field_label->offset], values, field_label->size); 3242b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextRestoreData(ctx, &data)); 3251a34d7dcSJeremy L Thompson } 326d8dd9a91SJeremy L Thompson return CEED_ERROR_SUCCESS; 327d8dd9a91SJeremy L Thompson } 328d8dd9a91SJeremy L Thompson 329d8dd9a91SJeremy L Thompson /** 330ca94c3ddSJeremy L Thompson @brief Get `CeedQFunctionContext` field data, read-only 3312788fa27SJeremy L Thompson 332ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 3332788fa27SJeremy L Thompson @param[in] field_label Label of field to read 3342788fa27SJeremy L Thompson @param[in] field_type Type of field to read 3352788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 3362788fa27SJeremy L Thompson @param[out] values Pointer to context values 3372788fa27SJeremy L Thompson 3382788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3392788fa27SJeremy L Thompson 3402788fa27SJeremy L Thompson @ref Backend 3412788fa27SJeremy L Thompson **/ 3422788fa27SJeremy L Thompson int CeedQFunctionContextGetGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, 3432788fa27SJeremy L Thompson size_t *num_values, void *values) { 3441c66c397SJeremy L Thompson char *data; 3451c66c397SJeremy L Thompson 3462788fa27SJeremy L Thompson // Check field type 3476e536b99SJeremy L Thompson CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 3486574a04fSJeremy L Thompson "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name, 3496574a04fSJeremy L Thompson CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 3502788fa27SJeremy L Thompson 3512788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data)); 3522788fa27SJeremy L Thompson *(void **)values = &data[field_label->offset]; 3532788fa27SJeremy L Thompson switch (field_type) { 3542788fa27SJeremy L Thompson case CEED_CONTEXT_FIELD_INT32: 3552788fa27SJeremy L Thompson *num_values = field_label->size / sizeof(int); 3562788fa27SJeremy L Thompson break; 3572788fa27SJeremy L Thompson case CEED_CONTEXT_FIELD_DOUBLE: 3582788fa27SJeremy L Thompson *num_values = field_label->size / sizeof(double); 3592788fa27SJeremy L Thompson break; 3605b6ec284SJeremy L Thompson case CEED_CONTEXT_FIELD_BOOL: 3615b6ec284SJeremy L Thompson *num_values = field_label->size / sizeof(bool); 3625b6ec284SJeremy L Thompson break; 3632788fa27SJeremy L Thompson } 3642788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3652788fa27SJeremy L Thompson } 3662788fa27SJeremy L Thompson 3672788fa27SJeremy L Thompson /** 368ca94c3ddSJeremy L Thompson @brief Restore `CeedQFunctionContext` field data, read-only 3692788fa27SJeremy L Thompson 370ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 3712788fa27SJeremy L Thompson @param[in] field_label Label of field to restore 3722788fa27SJeremy L Thompson @param[in] field_type Type of field to restore 3732788fa27SJeremy L Thompson @param[out] values Pointer to context values 3742788fa27SJeremy L Thompson 3752788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 3762788fa27SJeremy L Thompson 3772788fa27SJeremy L Thompson @ref Backend 3782788fa27SJeremy L Thompson **/ 3792788fa27SJeremy L Thompson int CeedQFunctionContextRestoreGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, 3802788fa27SJeremy L Thompson void *values) { 3812788fa27SJeremy L Thompson // Check field type 3826e536b99SJeremy L Thompson CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 3836574a04fSJeremy L Thompson "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name, 3846574a04fSJeremy L Thompson CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]); 3852788fa27SJeremy L Thompson 3862788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreDataRead(ctx, values)); 3872788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 3882788fa27SJeremy L Thompson } 3892788fa27SJeremy L Thompson 3902788fa27SJeremy L Thompson /** 391ca94c3ddSJeremy L Thompson @brief Set `CeedQFunctionContext` field holding double precision values 392bfacc300SJeremy L Thompson 393ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 3942788fa27SJeremy L Thompson @param[in] field_label Label for field to set 395ea61e9acSJeremy L Thompson @param[in] values Values to set 396bfacc300SJeremy L Thompson 397bfacc300SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 398bfacc300SJeremy L Thompson 3993e1e85abSJeremy L Thompson @ref Backend 400bfacc300SJeremy L Thompson **/ 4012b730f8bSJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, double *values) { 4026e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4032b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values)); 404bfacc300SJeremy L Thompson return CEED_ERROR_SUCCESS; 405bfacc300SJeremy L Thompson } 406bfacc300SJeremy L Thompson 407bfacc300SJeremy L Thompson /** 408ca94c3ddSJeremy L Thompson @brief Get `CeedQFunctionContext` field holding double precision values, read-only 4092788fa27SJeremy L Thompson 410ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 4112788fa27SJeremy L Thompson @param[in] field_label Label for field to get 4122788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 4132788fa27SJeremy L Thompson @param[out] values Pointer to context values 4142788fa27SJeremy L Thompson 4152788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4162788fa27SJeremy L Thompson 4172788fa27SJeremy L Thompson @ref Backend 4182788fa27SJeremy L Thompson **/ 4192788fa27SJeremy L Thompson int CeedQFunctionContextGetDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const double **values) { 4206e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4212788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values)); 4222788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4232788fa27SJeremy L Thompson } 4242788fa27SJeremy L Thompson 4252788fa27SJeremy L Thompson /** 426ca94c3ddSJeremy L Thompson @brief Restore `CeedQFunctionContext` field holding double precision values, read-only 4272788fa27SJeremy L Thompson 428ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 4292788fa27SJeremy L Thompson @param[in] field_label Label for field to restore 4302788fa27SJeremy L Thompson @param[out] values Pointer to context values 4312788fa27SJeremy L Thompson 4322788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4332788fa27SJeremy L Thompson 4342788fa27SJeremy L Thompson @ref Backend 4352788fa27SJeremy L Thompson **/ 4362788fa27SJeremy L Thompson int CeedQFunctionContextRestoreDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const double **values) { 4376e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4382788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values)); 4392788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4402788fa27SJeremy L Thompson } 4412788fa27SJeremy L Thompson 4422788fa27SJeremy L Thompson /** 443ca94c3ddSJeremy L Thompson @brief Set CeedQFunctionContext field holding `int32` values 444bfacc300SJeremy L Thompson 445ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 4462788fa27SJeremy L Thompson @param[in] field_label Label for field to set 447ea61e9acSJeremy L Thompson @param[in] values Values to set 448bfacc300SJeremy L Thompson 449bfacc300SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 450bfacc300SJeremy L Thompson 4513e1e85abSJeremy L Thompson @ref Backend 452bfacc300SJeremy L Thompson **/ 45323dbfd29SJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, int32_t *values) { 4546e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values)); 456bfacc300SJeremy L Thompson return CEED_ERROR_SUCCESS; 457bfacc300SJeremy L Thompson } 458bfacc300SJeremy L Thompson 459bfacc300SJeremy L Thompson /** 460ca94c3ddSJeremy L Thompson @brief Get `CeedQFunctionContext` field holding `int32` values, read-only 4612788fa27SJeremy L Thompson 462ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 4632788fa27SJeremy L Thompson @param[in] field_label Label for field to get 4642788fa27SJeremy L Thompson @param[out] num_values Number of values in the field label 4652788fa27SJeremy L Thompson @param[out] values Pointer to context values 4662788fa27SJeremy L Thompson 4672788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4682788fa27SJeremy L Thompson 4692788fa27SJeremy L Thompson @ref Backend 4702788fa27SJeremy L Thompson **/ 47123dbfd29SJeremy L Thompson int CeedQFunctionContextGetInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) { 4726e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4732788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values)); 4742788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4752788fa27SJeremy L Thompson } 4762788fa27SJeremy L Thompson 4772788fa27SJeremy L Thompson /** 478ca94c3ddSJeremy L Thompson @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only 4792788fa27SJeremy L Thompson 480ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 4812788fa27SJeremy L Thompson @param[in] field_label Label for field to restore 4822788fa27SJeremy L Thompson @param[out] values Pointer to context values 4832788fa27SJeremy L Thompson 4842788fa27SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 4852788fa27SJeremy L Thompson 4862788fa27SJeremy L Thompson @ref Backend 4872788fa27SJeremy L Thompson **/ 48823dbfd29SJeremy L Thompson int CeedQFunctionContextRestoreInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const int32_t **values) { 4896e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 4902788fa27SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values)); 4912788fa27SJeremy L Thompson return CEED_ERROR_SUCCESS; 4922788fa27SJeremy L Thompson } 4932788fa27SJeremy L Thompson 4942788fa27SJeremy L Thompson /** 495ca94c3ddSJeremy L Thompson @brief Set `CeedQFunctionContext` field holding boolean values 4965b6ec284SJeremy L Thompson 497ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 4985b6ec284SJeremy L Thompson @param[in] field_label Label for field to set 4995b6ec284SJeremy L Thompson @param[in] values Values to set 5005b6ec284SJeremy L Thompson 5015b6ec284SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5025b6ec284SJeremy L Thompson 5035b6ec284SJeremy L Thompson @ref Backend 5045b6ec284SJeremy L Thompson **/ 5055b6ec284SJeremy L Thompson int CeedQFunctionContextSetBoolean(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, bool *values) { 5066e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 5075b6ec284SJeremy L Thompson CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, values)); 5085b6ec284SJeremy L Thompson return CEED_ERROR_SUCCESS; 5095b6ec284SJeremy L Thompson } 5105b6ec284SJeremy L Thompson 5115b6ec284SJeremy L Thompson /** 512ca94c3ddSJeremy L Thompson @brief Get `CeedQFunctionContext` field holding boolean values, read-only 5135b6ec284SJeremy L Thompson 514ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 5155b6ec284SJeremy L Thompson @param[in] field_label Label for field to get 5165b6ec284SJeremy L Thompson @param[out] num_values Number of values in the field label 5175b6ec284SJeremy L Thompson @param[out] values Pointer to context values 5185b6ec284SJeremy L Thompson 5195b6ec284SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5205b6ec284SJeremy L Thompson 5215b6ec284SJeremy L Thompson @ref Backend 5225b6ec284SJeremy L Thompson **/ 5235b6ec284SJeremy L Thompson int CeedQFunctionContextGetBooleanRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) { 5246e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 5255b6ec284SJeremy L Thompson CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values)); 5265b6ec284SJeremy L Thompson return CEED_ERROR_SUCCESS; 5275b6ec284SJeremy L Thompson } 5285b6ec284SJeremy L Thompson 5295b6ec284SJeremy L Thompson /** 530ca94c3ddSJeremy L Thompson @brief Restore `CeedQFunctionContext` field holding boolean values, read-only 5315b6ec284SJeremy L Thompson 532ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 5335b6ec284SJeremy L Thompson @param[in] field_label Label for field to restore 5345b6ec284SJeremy L Thompson @param[out] values Pointer to context values 5355b6ec284SJeremy L Thompson 5365b6ec284SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5375b6ec284SJeremy L Thompson 5385b6ec284SJeremy L Thompson @ref Backend 5395b6ec284SJeremy L Thompson **/ 5405b6ec284SJeremy L Thompson int CeedQFunctionContextRestoreBooleanRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const bool **values) { 5416e536b99SJeremy L Thompson CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label"); 5425b6ec284SJeremy L Thompson CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, values)); 5435b6ec284SJeremy L Thompson return CEED_ERROR_SUCCESS; 5445b6ec284SJeremy L Thompson } 5455b6ec284SJeremy L Thompson 5465b6ec284SJeremy L Thompson /** 547ca94c3ddSJeremy L Thompson @brief Get additional destroy routine for `CeedQFunctionContext` user data 5482e64a2b9SJeremy L Thompson 549ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to get user destroy function 5502e64a2b9SJeremy L Thompson @param[out] f_mem_type Memory type to use when passing data into `f` 5512e64a2b9SJeremy L Thompson @param[out] f Additional routine to use to destroy user data 5522e64a2b9SJeremy L Thompson 5532e64a2b9SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 5542e64a2b9SJeremy L Thompson 5552e64a2b9SJeremy L Thompson @ref Backend 5562e64a2b9SJeremy L Thompson **/ 5572b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataDestroy(CeedQFunctionContext ctx, CeedMemType *f_mem_type, CeedQFunctionContextDataDestroyUser *f) { 5582e64a2b9SJeremy L Thompson if (f_mem_type) *f_mem_type = ctx->data_destroy_mem_type; 5592e64a2b9SJeremy L Thompson if (f) *f = ctx->data_destroy_function; 5602e64a2b9SJeremy L Thompson return CEED_ERROR_SUCCESS; 5612e64a2b9SJeremy L Thompson } 5622e64a2b9SJeremy L Thompson 5632e64a2b9SJeremy L Thompson /** 564ca94c3ddSJeremy L Thompson @brief Increment the reference counter for a `CeedQFunctionContext` 56534359f16Sjeremylt 566ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` to increment the reference counter 56734359f16Sjeremylt 56834359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 56934359f16Sjeremylt 57034359f16Sjeremylt @ref Backend 57134359f16Sjeremylt **/ 5729560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) { 573b0f67a9cSJeremy L Thompson CeedCall(CeedObjectReference((CeedObject)ctx)); 57434359f16Sjeremylt return CEED_ERROR_SUCCESS; 57534359f16Sjeremylt } 57634359f16Sjeremylt 577777ff853SJeremy L Thompson /// @} 578777ff853SJeremy L Thompson 579777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 580777ff853SJeremy L Thompson /// CeedQFunctionContext Public API 581777ff853SJeremy L Thompson /// ---------------------------------------------------------------------------- 582777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser 583777ff853SJeremy L Thompson /// @{ 584777ff853SJeremy L Thompson 585777ff853SJeremy L Thompson /** 586ca94c3ddSJeremy L Thompson @brief Create a `CeedQFunctionContext` for storing `CeedQFunctionContext` user context data 587777ff853SJeremy L Thompson 588ca94c3ddSJeremy L Thompson @param[in] ceed `Ceed` object used to create the `CeedQFunctionContext` 589ca94c3ddSJeremy L Thompson @param[out] ctx Address of the variable where the newly created `CeedQFunctionContext` will be stored 590777ff853SJeremy L Thompson 591777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 592777ff853SJeremy L Thompson 593777ff853SJeremy L Thompson @ref User 594777ff853SJeremy L Thompson **/ 595777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) { 596777ff853SJeremy L Thompson if (!ceed->QFunctionContextCreate) { 597777ff853SJeremy L Thompson Ceed delegate; 5986574a04fSJeremy L Thompson 5992b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context")); 6001ef3a2a9SJeremy L Thompson CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionContextCreate"); 6012b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextCreate(delegate, ctx)); 6029bc66399SJeremy L Thompson CeedCall(CeedDestroy(&delegate)); 603e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 604777ff853SJeremy L Thompson } 605777ff853SJeremy L Thompson 6062b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, ctx)); 607*6c328a79SJeremy L Thompson CeedCall(CeedObjectCreate(ceed, CeedQFunctionContextView_Object, CeedQFunctionContextDestroy_Object, &(*ctx)->obj)); 6082b730f8bSJeremy L Thompson CeedCall(ceed->QFunctionContextCreate(*ctx)); 609e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 610777ff853SJeremy L Thompson } 611777ff853SJeremy L Thompson 612777ff853SJeremy L Thompson /** 613ca94c3ddSJeremy L Thompson @brief Copy the pointer to a `CeedQFunctionContext`. 6144385fb7fSSebastian Grimberg 615ca94c3ddSJeremy L Thompson Both pointers should be destroyed with @ref CeedQFunctionContextDestroy(). 616512bb800SJeremy L Thompson 617ca94c3ddSJeremy L Thompson Note: If the value of `*ctx_copy` passed to this function is non-`NULL`, then it is assumed that `*ctx_copy` is a pointer to a `CeedQFunctionContext`. 618ca94c3ddSJeremy L Thompson This `CeedQFunctionContext` will be destroyed if `*ctx_copy` is the only reference to this `CeedQFunctionContext`. 6199560d06aSjeremylt 620ea61e9acSJeremy L Thompson @param[in] ctx CeedQFunctionContext to copy reference to 621ea61e9acSJeremy L Thompson @param[in,out] ctx_copy Variable to store copied reference 6229560d06aSjeremylt 6239560d06aSjeremylt @return An error code: 0 - success, otherwise - failure 6249560d06aSjeremylt 6259560d06aSjeremylt @ref User 6269560d06aSjeremylt **/ 6272b730f8bSJeremy L Thompson int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionContext *ctx_copy) { 6282b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextReference(ctx)); 6292b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroy(ctx_copy)); 6309560d06aSjeremylt *ctx_copy = ctx; 6319560d06aSjeremylt return CEED_ERROR_SUCCESS; 6329560d06aSjeremylt } 6339560d06aSjeremylt 6349560d06aSjeremylt /** 635ca94c3ddSJeremy L Thompson @brief Set the data used by a `CeedQFunctionContext`, freeing any previously allocated data if applicable. 6364385fb7fSSebastian Grimberg 637ca94c3ddSJeremy L Thompson The backend may copy values to a different @ref CeedMemType, such as during @ref CeedQFunctionApply(). 638777ff853SJeremy L Thompson See also @ref CeedQFunctionContextTakeData(). 639777ff853SJeremy L Thompson 640ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 641ea61e9acSJeremy L Thompson @param[in] mem_type Memory type of the data being passed 642ea61e9acSJeremy L Thompson @param[in] copy_mode Copy mode for the data 643ea61e9acSJeremy L Thompson @param[in] size Size of data, in bytes 644ea61e9acSJeremy L Thompson @param[in] data Data to be used 645777ff853SJeremy L Thompson 646777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 647777ff853SJeremy L Thompson 648777ff853SJeremy L Thompson @ref User 649777ff853SJeremy L Thompson **/ 6502b730f8bSJeremy L Thompson int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) { 6519bc66399SJeremy L Thompson CeedCheck(ctx->SetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextSetData"); 6529bc66399SJeremy L Thompson CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, 6539bc66399SJeremy L Thompson "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 654777ff853SJeremy L Thompson 6552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroyData(ctx)); 656d1d35e2fSjeremylt ctx->ctx_size = size; 6572b730f8bSJeremy L Thompson CeedCall(ctx->SetData(ctx, mem_type, copy_mode, data)); 658777ff853SJeremy L Thompson ctx->state += 2; 659e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 660777ff853SJeremy L Thompson } 661777ff853SJeremy L Thompson 662777ff853SJeremy L Thompson /** 663ca94c3ddSJeremy L Thompson @brief Take ownership of the data in a `CeedQFunctionContext` via the specified memory type. 6644385fb7fSSebastian Grimberg 665891038deSjeremylt The caller is responsible for managing and freeing the memory. 666891038deSjeremylt 667ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to access 668ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 669ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 670891038deSjeremylt @param[out] data Data on memory type mem_type 671891038deSjeremylt 672891038deSjeremylt @return An error code: 0 - success, otherwise - failure 673891038deSjeremylt 674891038deSjeremylt @ref User 675891038deSjeremylt **/ 6762b730f8bSJeremy L Thompson int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 6771c66c397SJeremy L Thompson void *temp_data = NULL; 6781c66c397SJeremy L Thompson bool has_valid_data = true, has_borrowed_data_of_type = true; 6791c66c397SJeremy L Thompson 6802b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 6819bc66399SJeremy L Thompson CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data"); 6829c774eddSJeremy L Thompson 6839bc66399SJeremy L Thompson CeedCheck(ctx->TakeData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextTakeData"); 6849bc66399SJeremy L Thompson CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, 6859bc66399SJeremy L Thompson "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 686891038deSjeremylt 6872b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type)); 6889bc66399SJeremy L Thompson CeedCheck(has_borrowed_data_of_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, 6896574a04fSJeremy L Thompson "CeedQFunctionContext has no borrowed %s data, must set data with CeedQFunctionContextSetData", CeedMemTypes[mem_type]); 6909c774eddSJeremy L Thompson 6912b730f8bSJeremy L Thompson CeedCall(ctx->TakeData(ctx, mem_type, &temp_data)); 692891038deSjeremylt if (data) (*(void **)data) = temp_data; 693891038deSjeremylt return CEED_ERROR_SUCCESS; 694891038deSjeremylt } 695891038deSjeremylt 696891038deSjeremylt /** 697ca94c3ddSJeremy L Thompson @brief Get read/write access to a `CeedQFunctionContext` via the specified memory type. 6984385fb7fSSebastian Grimberg 699777ff853SJeremy L Thompson Restore access with @ref CeedQFunctionContextRestoreData(). 700777ff853SJeremy L Thompson 701ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to access 702ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 703ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 704d1d35e2fSjeremylt @param[out] data Data on memory type mem_type 705777ff853SJeremy L Thompson 706ca94c3ddSJeremy L Thompson @note The @ref CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions provide access to array pointers in the desired memory space. 707ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedQFunctionContext` to track access. 708777ff853SJeremy L Thompson 709777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 710777ff853SJeremy L Thompson 711777ff853SJeremy L Thompson @ref User 712777ff853SJeremy L Thompson **/ 7132b730f8bSJeremy L Thompson int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 7141c66c397SJeremy L Thompson bool has_valid_data = true; 7151c66c397SJeremy L Thompson 7169bc66399SJeremy L Thompson CeedCheck(ctx->GetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextGetData"); 7179bc66399SJeremy L Thompson CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, 7189bc66399SJeremy L Thompson "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 7199bc66399SJeremy L Thompson CeedCheck(ctx->num_readers == 0, CeedQFunctionContextReturnCeed(ctx), 1, 7209bc66399SJeremy L Thompson "Cannot grant CeedQFunctionContext data access, a process has read access"); 72128bfd0b7SJeremy L Thompson 7222b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 7239bc66399SJeremy L Thompson CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); 7249c774eddSJeremy L Thompson 7252b730f8bSJeremy L Thompson CeedCall(ctx->GetData(ctx, mem_type, data)); 72628bfd0b7SJeremy L Thompson ctx->state++; 72728bfd0b7SJeremy L Thompson return CEED_ERROR_SUCCESS; 72828bfd0b7SJeremy L Thompson } 72928bfd0b7SJeremy L Thompson 73028bfd0b7SJeremy L Thompson /** 731ca94c3ddSJeremy L Thompson @brief Get read only access to a `CeedQFunctionContext` via the specified memory type. 7324385fb7fSSebastian Grimberg 73328bfd0b7SJeremy L Thompson Restore access with @ref CeedQFunctionContextRestoreData(). 73428bfd0b7SJeremy L Thompson 735ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to access 736ea61e9acSJeremy L Thompson @param[in] mem_type Memory type on which to access the data. 737ea61e9acSJeremy L Thompson If the backend uses a different memory type, this will perform a copy. 73828bfd0b7SJeremy L Thompson @param[out] data Data on memory type mem_type 73928bfd0b7SJeremy L Thompson 740ca94c3ddSJeremy L Thompson @note The @ref CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead() functions provide access to array pointers in the desired memory space. 741ca94c3ddSJeremy L Thompson Pairing get/restore allows the `CeedQFunctionContext` to track access. 74228bfd0b7SJeremy L Thompson 74328bfd0b7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 74428bfd0b7SJeremy L Thompson 74528bfd0b7SJeremy L Thompson @ref User 74628bfd0b7SJeremy L Thompson **/ 7472b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { 7481c66c397SJeremy L Thompson bool has_valid_data = true; 7491c66c397SJeremy L Thompson 7509bc66399SJeremy L Thompson CeedCheck(ctx->GetDataRead, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, 7519bc66399SJeremy L Thompson "Backend does not support CeedQFunctionContextGetDataRead"); 7529bc66399SJeremy L Thompson CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, 7539bc66399SJeremy L Thompson "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); 75428bfd0b7SJeremy L Thompson 7552b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); 7569bc66399SJeremy L Thompson CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); 75728bfd0b7SJeremy L Thompson 7582b730f8bSJeremy L Thompson CeedCall(ctx->GetDataRead(ctx, mem_type, data)); 75928bfd0b7SJeremy L Thompson ctx->num_readers++; 760e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 761777ff853SJeremy L Thompson } 762777ff853SJeremy L Thompson 763777ff853SJeremy L Thompson /** 764777ff853SJeremy L Thompson @brief Restore data obtained using @ref CeedQFunctionContextGetData() 765777ff853SJeremy L Thompson 766ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to restore 767ea61e9acSJeremy L Thompson @param[in,out] data Data to restore 768777ff853SJeremy L Thompson 769777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 770777ff853SJeremy L Thompson 771777ff853SJeremy L Thompson @ref User 772777ff853SJeremy L Thompson **/ 773777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) { 7746e536b99SJeremy L Thompson CeedCheck(ctx->state % 2 == 1, CeedQFunctionContextReturnCeed(ctx), 1, "Cannot restore CeedQFunctionContext array access, access was not granted"); 775777ff853SJeremy L Thompson 7766574a04fSJeremy L Thompson if (ctx->RestoreData) CeedCall(ctx->RestoreData(ctx)); 777777ff853SJeremy L Thompson *(void **)data = NULL; 77828bfd0b7SJeremy L Thompson ctx->state++; 77928bfd0b7SJeremy L Thompson return CEED_ERROR_SUCCESS; 78028bfd0b7SJeremy L Thompson } 78128bfd0b7SJeremy L Thompson 78228bfd0b7SJeremy L Thompson /** 78328bfd0b7SJeremy L Thompson @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead() 78428bfd0b7SJeremy L Thompson 785ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to restore 786ea61e9acSJeremy L Thompson @param[in,out] data Data to restore 78728bfd0b7SJeremy L Thompson 78828bfd0b7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 78928bfd0b7SJeremy L Thompson 79028bfd0b7SJeremy L Thompson @ref User 79128bfd0b7SJeremy L Thompson **/ 79228bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) { 7936e536b99SJeremy L Thompson CeedCheck(ctx->num_readers > 0, CeedQFunctionContextReturnCeed(ctx), 1, "Cannot restore CeedQFunctionContext array access, access was not granted"); 79428bfd0b7SJeremy L Thompson 79575a19770SJeremy L Thompson ctx->num_readers--; 7966574a04fSJeremy L Thompson if (ctx->num_readers == 0 && ctx->RestoreDataRead) CeedCall(ctx->RestoreDataRead(ctx)); 79728bfd0b7SJeremy L Thompson *(void **)data = NULL; 798e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 799777ff853SJeremy L Thompson } 800777ff853SJeremy L Thompson 801777ff853SJeremy L Thompson /** 802ca94c3ddSJeremy L Thompson @brief Register a `CeedQFunctionContext` field holding double precision values 803cdf32b93SJeremy L Thompson 804ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 805ea61e9acSJeremy L Thompson @param[in] field_name Name of field to register 806ea61e9acSJeremy L Thompson @param[in] field_offset Offset of field to register 807ea61e9acSJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 808ca94c3ddSJeremy L Thompson @param[in] field_description Description of field, or `NULL` for none 809cdf32b93SJeremy L Thompson 810cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 811cdf32b93SJeremy L Thompson 812cdf32b93SJeremy L Thompson @ref User 813cdf32b93SJeremy L Thompson **/ 8142b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values, 815cdf32b93SJeremy L Thompson const char *field_description) { 8165b6ec284SJeremy L Thompson return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_DOUBLE, num_values); 817cdf32b93SJeremy L Thompson } 818cdf32b93SJeremy L Thompson 819cdf32b93SJeremy L Thompson /** 820ca94c3ddSJeremy L Thompson @brief Register a `CeedQFunctionContext` field holding `int32` values 821cdf32b93SJeremy L Thompson 822ea61e9acSJeremy L Thompson @param[in,out] ctx CeedQFunctionContext 823ea61e9acSJeremy L Thompson @param[in] field_name Name of field to register 824ea61e9acSJeremy L Thompson @param[in] field_offset Offset of field to register 825ea61e9acSJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 826ca94c3ddSJeremy L Thompson @param[in] field_description Description of field, or `NULL` for none 827cdf32b93SJeremy L Thompson 828cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 829cdf32b93SJeremy L Thompson 830cdf32b93SJeremy L Thompson @ref User 831cdf32b93SJeremy L Thompson **/ 8322b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values, 833cdf32b93SJeremy L Thompson const char *field_description) { 8345b6ec284SJeremy L Thompson return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_INT32, num_values); 8355b6ec284SJeremy L Thompson } 8365b6ec284SJeremy L Thompson 8375b6ec284SJeremy L Thompson /** 838ca94c3ddSJeremy L Thompson @brief Register a `CeedQFunctionContext` field holding boolean values 8395b6ec284SJeremy L Thompson 840ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` 8415b6ec284SJeremy L Thompson @param[in] field_name Name of field to register 8425b6ec284SJeremy L Thompson @param[in] field_offset Offset of field to register 8435b6ec284SJeremy L Thompson @param[in] num_values Number of values to register, must be contiguous in memory 844ca94c3ddSJeremy L Thompson @param[in] field_description Description of field, or `NULL` for none 8455b6ec284SJeremy L Thompson 8465b6ec284SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 8475b6ec284SJeremy L Thompson 8485b6ec284SJeremy L Thompson @ref User 8495b6ec284SJeremy L Thompson **/ 8505b6ec284SJeremy L Thompson int CeedQFunctionContextRegisterBoolean(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values, 8515b6ec284SJeremy L Thompson const char *field_description) { 8525b6ec284SJeremy L Thompson return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_BOOL, num_values); 853cdf32b93SJeremy L Thompson } 854cdf32b93SJeremy L Thompson 855cdf32b93SJeremy L Thompson /** 856ca94c3ddSJeremy L Thompson @brief Get labels for all registered `CeedQFunctionContext` fields 857cdf32b93SJeremy L Thompson 858ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 8593668ca4bSJeremy L Thompson @param[out] field_labels Variable to hold array of field labels 860cdf32b93SJeremy L Thompson @param[out] num_fields Length of field descriptions array 861cdf32b93SJeremy L Thompson 862cdf32b93SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 863cdf32b93SJeremy L Thompson 864cdf32b93SJeremy L Thompson @ref User 865cdf32b93SJeremy L Thompson **/ 8662b730f8bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx, const CeedContextFieldLabel **field_labels, CeedInt *num_fields) { 8673668ca4bSJeremy L Thompson *field_labels = ctx->field_labels; 868cdf32b93SJeremy L Thompson *num_fields = ctx->num_fields; 869cdf32b93SJeremy L Thompson return CEED_ERROR_SUCCESS; 870cdf32b93SJeremy L Thompson } 871cdf32b93SJeremy L Thompson 872cdf32b93SJeremy L Thompson /** 873ca94c3ddSJeremy L Thompson @brief Get the descriptive information about a `CeedContextFieldLabel` 8740f86cbe7SJeremy L Thompson 875ca94c3ddSJeremy L Thompson @param[in] label @ref CeedContextFieldLabel 8760f86cbe7SJeremy L Thompson @param[out] field_name Name of labeled field 8771ff07f3dSJeremy L Thompson @param[out] field_offset Offset of field registered 8787bfe0f0eSJeremy L Thompson @param[out] num_values Number of values registered 8791ff07f3dSJeremy L Thompson @param[out] field_description Description of field, or NULL for none 880ca94c3ddSJeremy L Thompson @param[out] field_type @ref CeedContextFieldType 8810f86cbe7SJeremy L Thompson 8820f86cbe7SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 8830f86cbe7SJeremy L Thompson 8840f86cbe7SJeremy L Thompson @ref User 8850f86cbe7SJeremy L Thompson **/ 8861ff07f3dSJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label, const char **field_name, size_t *field_offset, size_t *num_values, 8871ff07f3dSJeremy L Thompson const char **field_description, CeedContextFieldType *field_type) { 8880f86cbe7SJeremy L Thompson if (field_name) *field_name = label->name; 8891ff07f3dSJeremy L Thompson if (field_offset) *field_offset = label->offset; 8907bfe0f0eSJeremy L Thompson if (num_values) *num_values = label->num_values; 8911ff07f3dSJeremy L Thompson if (field_description) *field_description = label->description; 8920f86cbe7SJeremy L Thompson if (field_type) *field_type = label->type; 8930f86cbe7SJeremy L Thompson return CEED_ERROR_SUCCESS; 8940f86cbe7SJeremy L Thompson } 8950f86cbe7SJeremy L Thompson 8960f86cbe7SJeremy L Thompson /** 89780a9ef05SNatalie Beams @brief Get data size for a Context 89880a9ef05SNatalie Beams 899ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` 90080a9ef05SNatalie Beams @param[out] ctx_size Variable to store size of context data values 90180a9ef05SNatalie Beams 90280a9ef05SNatalie Beams @return An error code: 0 - success, otherwise - failure 90380a9ef05SNatalie Beams 90480a9ef05SNatalie Beams @ref User 90580a9ef05SNatalie Beams **/ 9062b730f8bSJeremy L Thompson int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx, size_t *ctx_size) { 90780a9ef05SNatalie Beams *ctx_size = ctx->ctx_size; 90880a9ef05SNatalie Beams return CEED_ERROR_SUCCESS; 90980a9ef05SNatalie Beams } 91080a9ef05SNatalie Beams 91180a9ef05SNatalie Beams /** 9124c789ea2SJeremy L Thompson @brief Set the number of tabs to indent for @ref CeedQFunctionContextView() output 9134c789ea2SJeremy L Thompson 9144c789ea2SJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to set the number of view tabs 9154c789ea2SJeremy L Thompson @param[in] num_tabs Number of view tabs to set 9164c789ea2SJeremy L Thompson 9174c789ea2SJeremy L Thompson @return Error code: 0 - success, otherwise - failure 9184c789ea2SJeremy L Thompson 9194c789ea2SJeremy L Thompson @ref User 9204c789ea2SJeremy L Thompson **/ 9214c789ea2SJeremy L Thompson int CeedQFunctionContextSetNumViewTabs(CeedQFunctionContext ctx, CeedInt num_tabs) { 922a299a25bSJeremy L Thompson CeedCall(CeedObjectSetNumViewTabs((CeedObject)ctx, num_tabs)); 9234c789ea2SJeremy L Thompson return CEED_ERROR_SUCCESS; 9244c789ea2SJeremy L Thompson } 9254c789ea2SJeremy L Thompson 9264c789ea2SJeremy L Thompson /** 927690992b2SZach Atkins @brief Get the number of tabs to indent for @ref CeedQFunctionContextView() output 928690992b2SZach Atkins 929690992b2SZach Atkins @param[in] ctx `CeedQFunctionContext` to get the number of view tabs 930690992b2SZach Atkins @param[out] num_tabs Number of view tabs 931690992b2SZach Atkins 932690992b2SZach Atkins @return Error code: 0 - success, otherwise - failure 933690992b2SZach Atkins 934690992b2SZach Atkins @ref User 935690992b2SZach Atkins **/ 936690992b2SZach Atkins int CeedQFunctionContextGetNumViewTabs(CeedQFunctionContext ctx, CeedInt *num_tabs) { 937a299a25bSJeremy L Thompson CeedCall(CeedObjectGetNumViewTabs((CeedObject)ctx, num_tabs)); 938690992b2SZach Atkins return CEED_ERROR_SUCCESS; 939690992b2SZach Atkins } 940690992b2SZach Atkins 941690992b2SZach Atkins /** 942ca94c3ddSJeremy L Thompson @brief View a `CeedQFunctionContext` 943777ff853SJeremy L Thompson 944ca94c3ddSJeremy L Thompson @param[in] ctx `CeedQFunctionContext` to view 945777ff853SJeremy L Thompson @param[in] stream Filestream to write to 946777ff853SJeremy L Thompson 947777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 948777ff853SJeremy L Thompson 949777ff853SJeremy L Thompson @ref User 950777ff853SJeremy L Thompson **/ 951777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) { 9524c789ea2SJeremy L Thompson char *tabs = NULL; 9534c789ea2SJeremy L Thompson 9544c789ea2SJeremy L Thompson { 9554c789ea2SJeremy L Thompson CeedInt num_tabs = 0; 9564c789ea2SJeremy L Thompson 9574c789ea2SJeremy L Thompson CeedCall(CeedQFunctionContextGetNumViewTabs(ctx, &num_tabs)); 9584c789ea2SJeremy L Thompson CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs)); 9594c789ea2SJeremy L Thompson for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' '; 9603668ca4bSJeremy L Thompson } 9614c789ea2SJeremy L Thompson 9624c789ea2SJeremy L Thompson fprintf(stream, "%sCeedQFunctionContext\n", tabs); 9634c789ea2SJeremy L Thompson fprintf(stream, "%s Context Data Size: %zu\n", tabs, ctx->ctx_size); 9644c789ea2SJeremy L Thompson for (CeedInt i = 0; i < ctx->num_fields; i++) { 9654c789ea2SJeremy L Thompson fprintf(stream, "%s Labeled %s field: %s\n", tabs, CeedContextFieldTypes[ctx->field_labels[i]->type], ctx->field_labels[i]->name); 9664c789ea2SJeremy L Thompson } 9674c789ea2SJeremy L Thompson CeedCall(CeedFree(&tabs)); 968e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 969777ff853SJeremy L Thompson } 970777ff853SJeremy L Thompson 971777ff853SJeremy L Thompson /** 972ca94c3ddSJeremy L Thompson @brief Set additional destroy routine for `CeedQFunctionContext` user data 9732790b72bSJeremy L Thompson 974ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` to set user destroy function 9752e64a2b9SJeremy L Thompson @param[in] f_mem_type Memory type to use when passing data into `f` 9762e64a2b9SJeremy L Thompson @param[in] f Additional routine to use to destroy user data 9772790b72bSJeremy L Thompson 9782790b72bSJeremy L Thompson @return An error code: 0 - success, otherwise - failure 9792790b72bSJeremy L Thompson 9802790b72bSJeremy L Thompson @ref User 9812790b72bSJeremy L Thompson **/ 9822b730f8bSJeremy L Thompson int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx, CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f) { 9836e536b99SJeremy L Thompson CeedCheck(f, CeedQFunctionContextReturnCeed(ctx), 1, "Must provide valid callback function for destroying user data"); 9842790b72bSJeremy L Thompson ctx->data_destroy_mem_type = f_mem_type; 9852790b72bSJeremy L Thompson ctx->data_destroy_function = f; 9862790b72bSJeremy L Thompson return CEED_ERROR_SUCCESS; 9872790b72bSJeremy L Thompson } 9882790b72bSJeremy L Thompson 9892790b72bSJeremy L Thompson /** 990ca94c3ddSJeremy L Thompson @brief Destroy a `CeedQFunctionContext` 991777ff853SJeremy L Thompson 992ca94c3ddSJeremy L Thompson @param[in,out] ctx `CeedQFunctionContext` to destroy 993777ff853SJeremy L Thompson 994777ff853SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 995777ff853SJeremy L Thompson 996777ff853SJeremy L Thompson @ref User 997777ff853SJeremy L Thompson **/ 998777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) { 999b0f67a9cSJeremy L Thompson if (!*ctx || CeedObjectDereference((CeedObject)*ctx) > 0) { 1000ad6481ceSJeremy L Thompson *ctx = NULL; 1001ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 1002ad6481ceSJeremy L Thompson } 1003b0f67a9cSJeremy L Thompson CeedCheck(((*ctx)->state % 2) == 0, CeedQFunctionContextReturnCeed(*ctx), 1, "Cannot destroy CeedQFunctionContext, the access lock is in use"); 1004777ff853SJeremy L Thompson 10052b730f8bSJeremy L Thompson CeedCall(CeedQFunctionContextDestroyData(*ctx)); 10062b730f8bSJeremy L Thompson if ((*ctx)->Destroy) CeedCall((*ctx)->Destroy(*ctx)); 1007cdf32b93SJeremy L Thompson for (CeedInt i = 0; i < (*ctx)->num_fields; i++) { 10082b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i]->name)); 10092b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i]->description)); 10102b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels[i])); 1011cdf32b93SJeremy L Thompson } 10122b730f8bSJeremy L Thompson CeedCall(CeedFree(&(*ctx)->field_labels)); 1013*6c328a79SJeremy L Thompson CeedCall(CeedObjectDestroy_Private(&(*ctx)->obj)); 10142b730f8bSJeremy L Thompson CeedCall(CeedFree(ctx)); 1015e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1016777ff853SJeremy L Thompson } 1017777ff853SJeremy L Thompson 1018777ff853SJeremy L Thompson /// @} 1019