xref: /libCEED/interface/ceed-qfunctioncontext.c (revision 4c789ea28eed8450eb29ac731b0b6c80a3f128bb)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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 
130cdf32b93SJeremy L Thompson /// @}
131cdf32b93SJeremy L Thompson 
132cdf32b93SJeremy L Thompson /// ----------------------------------------------------------------------------
133777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API
134777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
135777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend
136777ff853SJeremy L Thompson /// @{
137777ff853SJeremy L Thompson 
138777ff853SJeremy L Thompson /**
139ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedQFunctionContext`
140777ff853SJeremy L Thompson 
141ca94c3ddSJeremy L Thompson   @param[in]  ctx  `CeedQFunctionContext`
142ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
143777ff853SJeremy L Thompson 
144777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
145777ff853SJeremy L Thompson 
146777ff853SJeremy L Thompson   @ref Backend
147777ff853SJeremy L Thompson **/
148777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
1499bc66399SJeremy L Thompson   *ceed = NULL;
1509bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedQFunctionContextReturnCeed(ctx), ceed));
151e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
152777ff853SJeremy L Thompson }
153777ff853SJeremy L Thompson 
154777ff853SJeremy L Thompson /**
1556e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedQFunctionContext`
1566e536b99SJeremy L Thompson 
1576e536b99SJeremy L Thompson   @param[in]  ctx  `CeedQFunctionContext`
1586e536b99SJeremy L Thompson 
1596e536b99SJeremy L Thompson   @return `Ceed` associated with `ctx`
1606e536b99SJeremy L Thompson 
1616e536b99SJeremy L Thompson   @ref Backend
1626e536b99SJeremy L Thompson **/
1636e536b99SJeremy L Thompson Ceed CeedQFunctionContextReturnCeed(CeedQFunctionContext ctx) { return ctx->ceed; }
1646e536b99SJeremy L Thompson 
1656e536b99SJeremy L Thompson /**
166*4c789ea2SJeremy L Thompson   @brief Get the number of tabs to indent for @ref CeedQFunctionContextView() output
167*4c789ea2SJeremy L Thompson 
168*4c789ea2SJeremy L Thompson   @param[in]  ctx      `CeedQFunctionContext` to get the number of view tabs
169*4c789ea2SJeremy L Thompson   @param[out] num_tabs Number of view tabs
170*4c789ea2SJeremy L Thompson 
171*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
172*4c789ea2SJeremy L Thompson 
173*4c789ea2SJeremy L Thompson   @ref Backend
174*4c789ea2SJeremy L Thompson **/
175*4c789ea2SJeremy L Thompson int CeedQFunctionContextGetNumViewTabs(CeedQFunctionContext ctx, CeedInt *num_tabs) {
176*4c789ea2SJeremy L Thompson   *num_tabs = ctx->num_tabs;
177*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
178*4c789ea2SJeremy L Thompson }
179*4c789ea2SJeremy L Thompson 
180*4c789ea2SJeremy L Thompson /**
181ca94c3ddSJeremy L Thompson   @brief Check for valid data in a `CeedQFunctionContext`
1829c774eddSJeremy L Thompson 
183ca94c3ddSJeremy L Thompson   @param[in]  ctx            `CeedQFunctionContext` to check validity
1849c774eddSJeremy L Thompson   @param[out] has_valid_data Variable to store validity
1859c774eddSJeremy L Thompson 
1869c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1879c774eddSJeremy L Thompson 
1889c774eddSJeremy L Thompson   @ref Backend
1899c774eddSJeremy L Thompson **/
1902b730f8bSJeremy L Thompson int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx, bool *has_valid_data) {
1916e536b99SJeremy L Thompson   CeedCheck(ctx->HasValidData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
1926e536b99SJeremy L Thompson             "Backend does not support CeedQFunctionContextHasValidData");
1932b730f8bSJeremy L Thompson   CeedCall(ctx->HasValidData(ctx, has_valid_data));
1949c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1959c774eddSJeremy L Thompson }
1969c774eddSJeremy L Thompson 
1979c774eddSJeremy L Thompson /**
198ca94c3ddSJeremy L Thompson   @brief Check for borrowed data of a specific @ref CeedMemType in a `CeedQFunctionContext`
1999c774eddSJeremy L Thompson 
200ca94c3ddSJeremy L Thompson   @param[in]  ctx                       `CeedQFunctionContext` to check
201ea61e9acSJeremy L Thompson   @param[in]  mem_type                  Memory type to check
2029c774eddSJeremy L Thompson   @param[out] has_borrowed_data_of_type Variable to store result
2039c774eddSJeremy L Thompson 
2049c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2059c774eddSJeremy L Thompson 
2069c774eddSJeremy L Thompson   @ref Backend
2079c774eddSJeremy L Thompson **/
2082b730f8bSJeremy L Thompson int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) {
2096e536b99SJeremy L Thompson   CeedCheck(ctx->HasBorrowedDataOfType, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
2106e536b99SJeremy L Thompson             "Backend does not support CeedQFunctionContextHasBorrowedDataOfType");
2112b730f8bSJeremy L Thompson   CeedCall(ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type));
2129c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
2139c774eddSJeremy L Thompson }
2149c774eddSJeremy L Thompson 
2159c774eddSJeremy L Thompson /**
216ca94c3ddSJeremy L Thompson   @brief Get the state of a `CeedQFunctionContext`
217777ff853SJeremy L Thompson 
218ca94c3ddSJeremy L Thompson   @param[in]  ctx   `CeedQFunctionContext` to retrieve state
219777ff853SJeremy L Thompson   @param[out] state Variable to store state
220777ff853SJeremy L Thompson 
221777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
222777ff853SJeremy L Thompson 
223777ff853SJeremy L Thompson   @ref Backend
224777ff853SJeremy L Thompson **/
225777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
226777ff853SJeremy L Thompson   *state = ctx->state;
227e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
228777ff853SJeremy L Thompson }
229777ff853SJeremy L Thompson 
230777ff853SJeremy L Thompson /**
231ca94c3ddSJeremy L Thompson   @brief Get backend data of a `CeedQFunctionContext`
232777ff853SJeremy L Thompson 
233ca94c3ddSJeremy L Thompson   @param[in]  ctx  `CeedQFunctionContext`
234777ff853SJeremy L Thompson   @param[out] data Variable to store data
235777ff853SJeremy L Thompson 
236777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
237777ff853SJeremy L Thompson 
238777ff853SJeremy L Thompson   @ref Backend
239777ff853SJeremy L Thompson **/
240777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
241777ff853SJeremy L Thompson   *(void **)data = ctx->data;
242e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
243777ff853SJeremy L Thompson }
244777ff853SJeremy L Thompson 
245777ff853SJeremy L Thompson /**
246ca94c3ddSJeremy L Thompson   @brief Set backend data of a `CeedQFunctionContext`
247777ff853SJeremy L Thompson 
248ca94c3ddSJeremy L Thompson   @param[in,out] ctx  `CeedQFunctionContext`
249ea61e9acSJeremy L Thompson   @param[in]     data Data to set
250777ff853SJeremy L Thompson 
251777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
252777ff853SJeremy L Thompson 
253777ff853SJeremy L Thompson   @ref Backend
254777ff853SJeremy L Thompson **/
255777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
256777ff853SJeremy L Thompson   ctx->data = data;
257e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
258777ff853SJeremy L Thompson }
259777ff853SJeremy L Thompson 
26034359f16Sjeremylt /**
261ca94c3ddSJeremy L Thompson   @brief Get label for a registered `CeedQFunctionContext` field, or `NULL` if no field has been registered with this `field_name`
262e6a0ab89SJeremy L Thompson 
263ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
264e6a0ab89SJeremy L Thompson   @param[in]  field_name  Name of field to retrieve label
265e6a0ab89SJeremy L Thompson   @param[out] field_label Variable to field label
266e6a0ab89SJeremy L Thompson 
267e6a0ab89SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
268e6a0ab89SJeremy L Thompson 
2693e1e85abSJeremy L Thompson   @ref Backend
270e6a0ab89SJeremy L Thompson **/
2712b730f8bSJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx, const char *field_name, CeedContextFieldLabel *field_label) {
272e6a0ab89SJeremy L Thompson   CeedInt field_index;
2731c66c397SJeremy L Thompson 
2742b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index));
275e6a0ab89SJeremy L Thompson 
276e6a0ab89SJeremy L Thompson   if (field_index != -1) {
277e6a0ab89SJeremy L Thompson     *field_label = ctx->field_labels[field_index];
278e6a0ab89SJeremy L Thompson   } else {
279e6a0ab89SJeremy L Thompson     *field_label = NULL;
280e6a0ab89SJeremy L Thompson   }
281e6a0ab89SJeremy L Thompson   return CEED_ERROR_SUCCESS;
282e6a0ab89SJeremy L Thompson }
283e6a0ab89SJeremy L Thompson 
284e6a0ab89SJeremy L Thompson /**
285ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field
286d8dd9a91SJeremy L Thompson 
287ca94c3ddSJeremy L Thompson   @param[in,out] ctx         `CeedQFunctionContext`
288ea61e9acSJeremy L Thompson   @param[in]     field_label Label of field to set
289ea61e9acSJeremy L Thompson   @param[in]     field_type  Type of field to set
2902788fa27SJeremy L Thompson   @param[in]     values      Value to set
291d8dd9a91SJeremy L Thompson 
292d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
293d8dd9a91SJeremy L Thompson 
2943e1e85abSJeremy L Thompson   @ref Backend
295d8dd9a91SJeremy L Thompson **/
2962788fa27SJeremy L Thompson int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) {
2971a34d7dcSJeremy L Thompson   bool  is_different;
2981c66c397SJeremy L Thompson   char *data;
2991c66c397SJeremy L Thompson 
3003668ca4bSJeremy L Thompson   // Check field type
3016e536b99SJeremy L Thompson   CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
3026574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
3036574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
304d8dd9a91SJeremy L Thompson 
3051a34d7dcSJeremy L Thompson   CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data));
3061a34d7dcSJeremy L Thompson   is_different = memcmp(&data[field_label->offset], values, field_label->size);
3071a34d7dcSJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreDataRead(ctx, &data));
3081a34d7dcSJeremy L Thompson   if (is_different) {
3092b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data));
3102788fa27SJeremy L Thompson     memcpy(&data[field_label->offset], values, field_label->size);
3112b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextRestoreData(ctx, &data));
3121a34d7dcSJeremy L Thompson   }
313d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
314d8dd9a91SJeremy L Thompson }
315d8dd9a91SJeremy L Thompson 
316d8dd9a91SJeremy L Thompson /**
317ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field data, read-only
3182788fa27SJeremy L Thompson 
319ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
3202788fa27SJeremy L Thompson   @param[in]  field_label Label of field to read
3212788fa27SJeremy L Thompson   @param[in]  field_type  Type of field to read
3222788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
3232788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
3242788fa27SJeremy L Thompson 
3252788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3262788fa27SJeremy L Thompson 
3272788fa27SJeremy L Thompson   @ref Backend
3282788fa27SJeremy L Thompson **/
3292788fa27SJeremy L Thompson int CeedQFunctionContextGetGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type,
3302788fa27SJeremy L Thompson                                        size_t *num_values, void *values) {
3311c66c397SJeremy L Thompson   char *data;
3321c66c397SJeremy L Thompson 
3332788fa27SJeremy L Thompson   // Check field type
3346e536b99SJeremy L Thompson   CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
3356574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
3366574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
3372788fa27SJeremy L Thompson 
3382788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &data));
3392788fa27SJeremy L Thompson   *(void **)values = &data[field_label->offset];
3402788fa27SJeremy L Thompson   switch (field_type) {
3412788fa27SJeremy L Thompson     case CEED_CONTEXT_FIELD_INT32:
3422788fa27SJeremy L Thompson       *num_values = field_label->size / sizeof(int);
3432788fa27SJeremy L Thompson       break;
3442788fa27SJeremy L Thompson     case CEED_CONTEXT_FIELD_DOUBLE:
3452788fa27SJeremy L Thompson       *num_values = field_label->size / sizeof(double);
3462788fa27SJeremy L Thompson       break;
3475b6ec284SJeremy L Thompson     case CEED_CONTEXT_FIELD_BOOL:
3485b6ec284SJeremy L Thompson       *num_values = field_label->size / sizeof(bool);
3495b6ec284SJeremy L Thompson       break;
3502788fa27SJeremy L Thompson   }
3512788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3522788fa27SJeremy L Thompson }
3532788fa27SJeremy L Thompson 
3542788fa27SJeremy L Thompson /**
355ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field data, read-only
3562788fa27SJeremy L Thompson 
357ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
3582788fa27SJeremy L Thompson   @param[in]  field_label Label of field to restore
3592788fa27SJeremy L Thompson   @param[in]  field_type  Type of field to restore
3602788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
3612788fa27SJeremy L Thompson 
3622788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
3632788fa27SJeremy L Thompson 
3642788fa27SJeremy L Thompson   @ref Backend
3652788fa27SJeremy L Thompson **/
3662788fa27SJeremy L Thompson int CeedQFunctionContextRestoreGenericRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, CeedContextFieldType field_type,
3672788fa27SJeremy L Thompson                                            void *values) {
3682788fa27SJeremy L Thompson   // Check field type
3696e536b99SJeremy L Thompson   CeedCheck(field_label->type == field_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
3706574a04fSJeremy L Thompson             "QFunctionContext field with name \"%s\" registered as %s, not registered as %s", field_label->name,
3716574a04fSJeremy L Thompson             CeedContextFieldTypes[field_label->type], CeedContextFieldTypes[field_type]);
3722788fa27SJeremy L Thompson 
3732788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreDataRead(ctx, values));
3742788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3752788fa27SJeremy L Thompson }
3762788fa27SJeremy L Thompson 
3772788fa27SJeremy L Thompson /**
378ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding double precision values
379bfacc300SJeremy L Thompson 
380ca94c3ddSJeremy L Thompson   @param[in,out] ctx         `CeedQFunctionContext`
3812788fa27SJeremy L Thompson   @param[in]     field_label Label for field to set
382ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
383bfacc300SJeremy L Thompson 
384bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
385bfacc300SJeremy L Thompson 
3863e1e85abSJeremy L Thompson   @ref Backend
387bfacc300SJeremy L Thompson **/
3882b730f8bSJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, double *values) {
3896e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
3902b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values));
391bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
392bfacc300SJeremy L Thompson }
393bfacc300SJeremy L Thompson 
394bfacc300SJeremy L Thompson /**
395ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding double precision values, read-only
3962788fa27SJeremy L Thompson 
397ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
3982788fa27SJeremy L Thompson   @param[in]  field_label Label for field to get
3992788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
4002788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4012788fa27SJeremy L Thompson 
4022788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4032788fa27SJeremy L Thompson 
4042788fa27SJeremy L Thompson   @ref Backend
4052788fa27SJeremy L Thompson **/
4062788fa27SJeremy L Thompson int CeedQFunctionContextGetDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const double **values) {
4076e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4082788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, num_values, values));
4092788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4102788fa27SJeremy L Thompson }
4112788fa27SJeremy L Thompson 
4122788fa27SJeremy L Thompson /**
413ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding double precision values, read-only
4142788fa27SJeremy L Thompson 
415ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
4162788fa27SJeremy L Thompson   @param[in]  field_label Label for field to restore
4172788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4182788fa27SJeremy L Thompson 
4192788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4202788fa27SJeremy L Thompson 
4212788fa27SJeremy L Thompson   @ref Backend
4222788fa27SJeremy L Thompson **/
4232788fa27SJeremy L Thompson int CeedQFunctionContextRestoreDoubleRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const double **values) {
4246e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4252788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_DOUBLE, values));
4262788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4272788fa27SJeremy L Thompson }
4282788fa27SJeremy L Thompson 
4292788fa27SJeremy L Thompson /**
430ca94c3ddSJeremy L Thompson   @brief Set CeedQFunctionContext field holding `int32` values
431bfacc300SJeremy L Thompson 
432ea61e9acSJeremy L Thompson   @param[in,out] ctx         CeedQFunctionContext
4332788fa27SJeremy L Thompson   @param[in]     field_label Label for field to set
434ea61e9acSJeremy L Thompson   @param[in]     values      Values to set
435bfacc300SJeremy L Thompson 
436bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
437bfacc300SJeremy L Thompson 
4383e1e85abSJeremy L Thompson   @ref Backend
439bfacc300SJeremy L Thompson **/
44023dbfd29SJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, int32_t *values) {
4416e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4422b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values));
443bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
444bfacc300SJeremy L Thompson }
445bfacc300SJeremy L Thompson 
446bfacc300SJeremy L Thompson /**
447ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding `int32` values, read-only
4482788fa27SJeremy L Thompson 
449ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
4502788fa27SJeremy L Thompson   @param[in]  field_label Label for field to get
4512788fa27SJeremy L Thompson   @param[out] num_values  Number of values in the field label
4522788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4532788fa27SJeremy L Thompson 
4542788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4552788fa27SJeremy L Thompson 
4562788fa27SJeremy L Thompson   @ref Backend
4572788fa27SJeremy L Thompson **/
45823dbfd29SJeremy L Thompson int CeedQFunctionContextGetInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const int32_t **values) {
4596e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4602788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, num_values, values));
4612788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4622788fa27SJeremy L Thompson }
4632788fa27SJeremy L Thompson 
4642788fa27SJeremy L Thompson /**
465ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding `int32` values, read-only
4662788fa27SJeremy L Thompson 
467ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
4682788fa27SJeremy L Thompson   @param[in]  field_label Label for field to restore
4692788fa27SJeremy L Thompson   @param[out] values      Pointer to context values
4702788fa27SJeremy L Thompson 
4712788fa27SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4722788fa27SJeremy L Thompson 
4732788fa27SJeremy L Thompson   @ref Backend
4742788fa27SJeremy L Thompson **/
47523dbfd29SJeremy L Thompson int CeedQFunctionContextRestoreInt32Read(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const int32_t **values) {
4766e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4772788fa27SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_INT32, values));
4782788fa27SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4792788fa27SJeremy L Thompson }
4802788fa27SJeremy L Thompson 
4812788fa27SJeremy L Thompson /**
482ca94c3ddSJeremy L Thompson   @brief Set `CeedQFunctionContext` field holding boolean values
4835b6ec284SJeremy L Thompson 
484ca94c3ddSJeremy L Thompson   @param[in,out] ctx         `CeedQFunctionContext`
4855b6ec284SJeremy L Thompson   @param[in]     field_label Label for field to set
4865b6ec284SJeremy L Thompson   @param[in]     values      Values to set
4875b6ec284SJeremy L Thompson 
4885b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
4895b6ec284SJeremy L Thompson 
4905b6ec284SJeremy L Thompson   @ref Backend
4915b6ec284SJeremy L Thompson **/
4925b6ec284SJeremy L Thompson int CeedQFunctionContextSetBoolean(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, bool *values) {
4936e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
4945b6ec284SJeremy L Thompson   CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, values));
4955b6ec284SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4965b6ec284SJeremy L Thompson }
4975b6ec284SJeremy L Thompson 
4985b6ec284SJeremy L Thompson /**
499ca94c3ddSJeremy L Thompson   @brief Get `CeedQFunctionContext` field holding boolean values, read-only
5005b6ec284SJeremy L Thompson 
501ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
5025b6ec284SJeremy L Thompson   @param[in]  field_label Label for field to get
5035b6ec284SJeremy L Thompson   @param[out] num_values  Number of values in the field label
5045b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
5055b6ec284SJeremy L Thompson 
5065b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5075b6ec284SJeremy L Thompson 
5085b6ec284SJeremy L Thompson   @ref Backend
5095b6ec284SJeremy L Thompson **/
5105b6ec284SJeremy L Thompson int CeedQFunctionContextGetBooleanRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, size_t *num_values, const bool **values) {
5116e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
5125b6ec284SJeremy L Thompson   CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, num_values, values));
5135b6ec284SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5145b6ec284SJeremy L Thompson }
5155b6ec284SJeremy L Thompson 
5165b6ec284SJeremy L Thompson /**
517ca94c3ddSJeremy L Thompson   @brief Restore `CeedQFunctionContext` field holding boolean values, read-only
5185b6ec284SJeremy L Thompson 
519ca94c3ddSJeremy L Thompson   @param[in]  ctx         `CeedQFunctionContext`
5205b6ec284SJeremy L Thompson   @param[in]  field_label Label for field to restore
5215b6ec284SJeremy L Thompson   @param[out] values      Pointer to context values
5225b6ec284SJeremy L Thompson 
5235b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5245b6ec284SJeremy L Thompson 
5255b6ec284SJeremy L Thompson   @ref Backend
5265b6ec284SJeremy L Thompson **/
5275b6ec284SJeremy L Thompson int CeedQFunctionContextRestoreBooleanRead(CeedQFunctionContext ctx, CeedContextFieldLabel field_label, const bool **values) {
5286e536b99SJeremy L Thompson   CeedCheck(field_label, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Invalid field label");
5295b6ec284SJeremy L Thompson   CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, CEED_CONTEXT_FIELD_BOOL, values));
5305b6ec284SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5315b6ec284SJeremy L Thompson }
5325b6ec284SJeremy L Thompson 
5335b6ec284SJeremy L Thompson /**
534ca94c3ddSJeremy L Thompson   @brief Get additional destroy routine for `CeedQFunctionContext` user data
5352e64a2b9SJeremy L Thompson 
536ca94c3ddSJeremy L Thompson   @param[in] ctx         `CeedQFunctionContext` to get user destroy function
5372e64a2b9SJeremy L Thompson   @param[out] f_mem_type Memory type to use when passing data into `f`
5382e64a2b9SJeremy L Thompson   @param[out] f          Additional routine to use to destroy user data
5392e64a2b9SJeremy L Thompson 
5402e64a2b9SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
5412e64a2b9SJeremy L Thompson 
5422e64a2b9SJeremy L Thompson   @ref Backend
5432e64a2b9SJeremy L Thompson **/
5442b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataDestroy(CeedQFunctionContext ctx, CeedMemType *f_mem_type, CeedQFunctionContextDataDestroyUser *f) {
5452e64a2b9SJeremy L Thompson   if (f_mem_type) *f_mem_type = ctx->data_destroy_mem_type;
5462e64a2b9SJeremy L Thompson   if (f) *f = ctx->data_destroy_function;
5472e64a2b9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5482e64a2b9SJeremy L Thompson }
5492e64a2b9SJeremy L Thompson 
5502e64a2b9SJeremy L Thompson /**
551ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedQFunctionContext`
55234359f16Sjeremylt 
553ca94c3ddSJeremy L Thompson   @param[in,out] ctx `CeedQFunctionContext` to increment the reference counter
55434359f16Sjeremylt 
55534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
55634359f16Sjeremylt 
55734359f16Sjeremylt   @ref Backend
55834359f16Sjeremylt **/
5599560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
56034359f16Sjeremylt   ctx->ref_count++;
56134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
56234359f16Sjeremylt }
56334359f16Sjeremylt 
564777ff853SJeremy L Thompson /// @}
565777ff853SJeremy L Thompson 
566777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
567777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
568777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
569777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
570777ff853SJeremy L Thompson /// @{
571777ff853SJeremy L Thompson 
572777ff853SJeremy L Thompson /**
573ca94c3ddSJeremy L Thompson   @brief Create a `CeedQFunctionContext` for storing `CeedQFunctionContext` user context data
574777ff853SJeremy L Thompson 
575ca94c3ddSJeremy L Thompson   @param[in]  ceed `Ceed` object used to create the `CeedQFunctionContext`
576ca94c3ddSJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created `CeedQFunctionContext` will be stored
577777ff853SJeremy L Thompson 
578777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
579777ff853SJeremy L Thompson 
580777ff853SJeremy L Thompson   @ref User
581777ff853SJeremy L Thompson **/
582777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
583777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
584777ff853SJeremy L Thompson     Ceed delegate;
5856574a04fSJeremy L Thompson 
5862b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context"));
5871ef3a2a9SJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionContextCreate");
5882b730f8bSJeremy L Thompson     CeedCall(CeedQFunctionContextCreate(delegate, ctx));
5899bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
590e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
591777ff853SJeremy L Thompson   }
592777ff853SJeremy L Thompson 
5932b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, ctx));
594db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*ctx)->ceed));
595d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
5962b730f8bSJeremy L Thompson   CeedCall(ceed->QFunctionContextCreate(*ctx));
597e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
598777ff853SJeremy L Thompson }
599777ff853SJeremy L Thompson 
600777ff853SJeremy L Thompson /**
601ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedQFunctionContext`.
6024385fb7fSSebastian Grimberg 
603ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedQFunctionContextDestroy().
604512bb800SJeremy L Thompson 
605ca94c3ddSJeremy 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`.
606ca94c3ddSJeremy L Thompson         This `CeedQFunctionContext` will be destroyed if `*ctx_copy` is the only reference to this `CeedQFunctionContext`.
6079560d06aSjeremylt 
608ea61e9acSJeremy L Thompson   @param[in]     ctx      CeedQFunctionContext to copy reference to
609ea61e9acSJeremy L Thompson   @param[in,out] ctx_copy Variable to store copied reference
6109560d06aSjeremylt 
6119560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
6129560d06aSjeremylt 
6139560d06aSjeremylt   @ref User
6149560d06aSjeremylt **/
6152b730f8bSJeremy L Thompson int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionContext *ctx_copy) {
6162b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextReference(ctx));
6172b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroy(ctx_copy));
6189560d06aSjeremylt   *ctx_copy = ctx;
6199560d06aSjeremylt   return CEED_ERROR_SUCCESS;
6209560d06aSjeremylt }
6219560d06aSjeremylt 
6229560d06aSjeremylt /**
623ca94c3ddSJeremy L Thompson   @brief Set the data used by a `CeedQFunctionContext`, freeing any previously allocated data if applicable.
6244385fb7fSSebastian Grimberg 
625ca94c3ddSJeremy L Thompson   The backend may copy values to a different @ref CeedMemType, such as during @ref CeedQFunctionApply().
626777ff853SJeremy L Thompson   See also @ref CeedQFunctionContextTakeData().
627777ff853SJeremy L Thompson 
628ca94c3ddSJeremy L Thompson   @param[in,out] ctx       `CeedQFunctionContext`
629ea61e9acSJeremy L Thompson   @param[in]     mem_type  Memory type of the data being passed
630ea61e9acSJeremy L Thompson   @param[in]     copy_mode Copy mode for the data
631ea61e9acSJeremy L Thompson   @param[in]     size      Size of data, in bytes
632ea61e9acSJeremy L Thompson   @param[in]     data      Data to be used
633777ff853SJeremy L Thompson 
634777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
635777ff853SJeremy L Thompson 
636777ff853SJeremy L Thompson   @ref User
637777ff853SJeremy L Thompson **/
6382b730f8bSJeremy L Thompson int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) {
6399bc66399SJeremy L Thompson   CeedCheck(ctx->SetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextSetData");
6409bc66399SJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1,
6419bc66399SJeremy L Thompson             "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
642777ff853SJeremy L Thompson 
6432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroyData(ctx));
644d1d35e2fSjeremylt   ctx->ctx_size = size;
6452b730f8bSJeremy L Thompson   CeedCall(ctx->SetData(ctx, mem_type, copy_mode, data));
646777ff853SJeremy L Thompson   ctx->state += 2;
647e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
648777ff853SJeremy L Thompson }
649777ff853SJeremy L Thompson 
650777ff853SJeremy L Thompson /**
651ca94c3ddSJeremy L Thompson   @brief Take ownership of the data in a `CeedQFunctionContext` via the specified memory type.
6524385fb7fSSebastian Grimberg 
653891038deSjeremylt   The caller is responsible for managing and freeing the memory.
654891038deSjeremylt 
655ca94c3ddSJeremy L Thompson   @param[in]  ctx      `CeedQFunctionContext` to access
656ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
657ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
658891038deSjeremylt   @param[out] data     Data on memory type mem_type
659891038deSjeremylt 
660891038deSjeremylt   @return An error code: 0 - success, otherwise - failure
661891038deSjeremylt 
662891038deSjeremylt   @ref User
663891038deSjeremylt **/
6642b730f8bSJeremy L Thompson int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
6651c66c397SJeremy L Thompson   void *temp_data      = NULL;
6661c66c397SJeremy L Thompson   bool  has_valid_data = true, has_borrowed_data_of_type = true;
6671c66c397SJeremy L Thompson 
6682b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
6699bc66399SJeremy L Thompson   CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data");
6709c774eddSJeremy L Thompson 
6719bc66399SJeremy L Thompson   CeedCheck(ctx->TakeData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextTakeData");
6729bc66399SJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1,
6739bc66399SJeremy L Thompson             "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
674891038deSjeremylt 
6752b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type));
6769bc66399SJeremy L Thompson   CeedCheck(has_borrowed_data_of_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND,
6776574a04fSJeremy L Thompson             "CeedQFunctionContext has no borrowed %s data, must set data with CeedQFunctionContextSetData", CeedMemTypes[mem_type]);
6789c774eddSJeremy L Thompson 
6792b730f8bSJeremy L Thompson   CeedCall(ctx->TakeData(ctx, mem_type, &temp_data));
680891038deSjeremylt   if (data) (*(void **)data) = temp_data;
681891038deSjeremylt   return CEED_ERROR_SUCCESS;
682891038deSjeremylt }
683891038deSjeremylt 
684891038deSjeremylt /**
685ca94c3ddSJeremy L Thompson   @brief Get read/write access to a `CeedQFunctionContext` via the specified memory type.
6864385fb7fSSebastian Grimberg 
687777ff853SJeremy L Thompson   Restore access with @ref CeedQFunctionContextRestoreData().
688777ff853SJeremy L Thompson 
689ca94c3ddSJeremy L Thompson   @param[in]  ctx      `CeedQFunctionContext` to access
690ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
691ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
692d1d35e2fSjeremylt   @param[out] data     Data on memory type mem_type
693777ff853SJeremy L Thompson 
694ca94c3ddSJeremy L Thompson   @note The @ref CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions provide access to array pointers in the desired memory space.
695ca94c3ddSJeremy L Thompson         Pairing get/restore allows the `CeedQFunctionContext` to track access.
696777ff853SJeremy L Thompson 
697777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
698777ff853SJeremy L Thompson 
699777ff853SJeremy L Thompson   @ref User
700777ff853SJeremy L Thompson **/
7012b730f8bSJeremy L Thompson int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
7021c66c397SJeremy L Thompson   bool has_valid_data = true;
7031c66c397SJeremy L Thompson 
7049bc66399SJeremy L Thompson   CeedCheck(ctx->GetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextGetData");
7059bc66399SJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1,
7069bc66399SJeremy L Thompson             "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
7079bc66399SJeremy L Thompson   CeedCheck(ctx->num_readers == 0, CeedQFunctionContextReturnCeed(ctx), 1,
7089bc66399SJeremy L Thompson             "Cannot grant CeedQFunctionContext data access, a process has read access");
70928bfd0b7SJeremy L Thompson 
7102b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
7119bc66399SJeremy L Thompson   CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
7129c774eddSJeremy L Thompson 
7132b730f8bSJeremy L Thompson   CeedCall(ctx->GetData(ctx, mem_type, data));
71428bfd0b7SJeremy L Thompson   ctx->state++;
71528bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
71628bfd0b7SJeremy L Thompson }
71728bfd0b7SJeremy L Thompson 
71828bfd0b7SJeremy L Thompson /**
719ca94c3ddSJeremy L Thompson   @brief Get read only access to a `CeedQFunctionContext` via the specified memory type.
7204385fb7fSSebastian Grimberg 
72128bfd0b7SJeremy L Thompson   Restore access with @ref CeedQFunctionContextRestoreData().
72228bfd0b7SJeremy L Thompson 
723ca94c3ddSJeremy L Thompson   @param[in]  ctx      `CeedQFunctionContext` to access
724ea61e9acSJeremy L Thompson   @param[in]  mem_type Memory type on which to access the data.
725ea61e9acSJeremy L Thompson                          If the backend uses a different memory type, this will perform a copy.
72628bfd0b7SJeremy L Thompson   @param[out] data     Data on memory type mem_type
72728bfd0b7SJeremy L Thompson 
728ca94c3ddSJeremy L Thompson   @note The @ref CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead() functions provide access to array pointers in the desired memory space.
729ca94c3ddSJeremy L Thompson         Pairing get/restore allows the `CeedQFunctionContext` to track access.
73028bfd0b7SJeremy L Thompson 
73128bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
73228bfd0b7SJeremy L Thompson 
73328bfd0b7SJeremy L Thompson   @ref User
73428bfd0b7SJeremy L Thompson **/
7352b730f8bSJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
7361c66c397SJeremy L Thompson   bool has_valid_data = true;
7371c66c397SJeremy L Thompson 
7389bc66399SJeremy L Thompson   CeedCheck(ctx->GetDataRead, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED,
7399bc66399SJeremy L Thompson             "Backend does not support CeedQFunctionContextGetDataRead");
7409bc66399SJeremy L Thompson   CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1,
7419bc66399SJeremy L Thompson             "Cannot grant CeedQFunctionContext data access, the access lock is already in use");
74228bfd0b7SJeremy L Thompson 
7432b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data));
7449bc66399SJeremy L Thompson   CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data");
74528bfd0b7SJeremy L Thompson 
7462b730f8bSJeremy L Thompson   CeedCall(ctx->GetDataRead(ctx, mem_type, data));
74728bfd0b7SJeremy L Thompson   ctx->num_readers++;
748e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
749777ff853SJeremy L Thompson }
750777ff853SJeremy L Thompson 
751777ff853SJeremy L Thompson /**
752777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
753777ff853SJeremy L Thompson 
754ca94c3ddSJeremy L Thompson   @param[in]     ctx  `CeedQFunctionContext` to restore
755ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
756777ff853SJeremy L Thompson 
757777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
758777ff853SJeremy L Thompson 
759777ff853SJeremy L Thompson   @ref User
760777ff853SJeremy L Thompson **/
761777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
7626e536b99SJeremy L Thompson   CeedCheck(ctx->state % 2 == 1, CeedQFunctionContextReturnCeed(ctx), 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
763777ff853SJeremy L Thompson 
7646574a04fSJeremy L Thompson   if (ctx->RestoreData) CeedCall(ctx->RestoreData(ctx));
765777ff853SJeremy L Thompson   *(void **)data = NULL;
76628bfd0b7SJeremy L Thompson   ctx->state++;
76728bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
76828bfd0b7SJeremy L Thompson }
76928bfd0b7SJeremy L Thompson 
77028bfd0b7SJeremy L Thompson /**
77128bfd0b7SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead()
77228bfd0b7SJeremy L Thompson 
773ca94c3ddSJeremy L Thompson   @param[in]     ctx  `CeedQFunctionContext` to restore
774ea61e9acSJeremy L Thompson   @param[in,out] data Data to restore
77528bfd0b7SJeremy L Thompson 
77628bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
77728bfd0b7SJeremy L Thompson 
77828bfd0b7SJeremy L Thompson   @ref User
77928bfd0b7SJeremy L Thompson **/
78028bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) {
7816e536b99SJeremy L Thompson   CeedCheck(ctx->num_readers > 0, CeedQFunctionContextReturnCeed(ctx), 1, "Cannot restore CeedQFunctionContext array access, access was not granted");
78228bfd0b7SJeremy L Thompson 
78375a19770SJeremy L Thompson   ctx->num_readers--;
7846574a04fSJeremy L Thompson   if (ctx->num_readers == 0 && ctx->RestoreDataRead) CeedCall(ctx->RestoreDataRead(ctx));
78528bfd0b7SJeremy L Thompson   *(void **)data = NULL;
786e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
787777ff853SJeremy L Thompson }
788777ff853SJeremy L Thompson 
789777ff853SJeremy L Thompson /**
790ca94c3ddSJeremy L Thompson   @brief Register a `CeedQFunctionContext` field holding double precision values
791cdf32b93SJeremy L Thompson 
792ca94c3ddSJeremy L Thompson   @param[in,out] ctx               `CeedQFunctionContext`
793ea61e9acSJeremy L Thompson   @param[in]     field_name        Name of field to register
794ea61e9acSJeremy L Thompson   @param[in]     field_offset      Offset of field to register
795ea61e9acSJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
796ca94c3ddSJeremy L Thompson   @param[in]     field_description Description of field, or `NULL` for none
797cdf32b93SJeremy L Thompson 
798cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
799cdf32b93SJeremy L Thompson 
800cdf32b93SJeremy L Thompson   @ref User
801cdf32b93SJeremy L Thompson **/
8022b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
803cdf32b93SJeremy L Thompson                                        const char *field_description) {
8045b6ec284SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_DOUBLE, num_values);
805cdf32b93SJeremy L Thompson }
806cdf32b93SJeremy L Thompson 
807cdf32b93SJeremy L Thompson /**
808ca94c3ddSJeremy L Thompson   @brief Register a `CeedQFunctionContext` field holding `int32` values
809cdf32b93SJeremy L Thompson 
810ea61e9acSJeremy L Thompson   @param[in,out] ctx               CeedQFunctionContext
811ea61e9acSJeremy L Thompson   @param[in]     field_name        Name of field to register
812ea61e9acSJeremy L Thompson   @param[in]     field_offset      Offset of field to register
813ea61e9acSJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
814ca94c3ddSJeremy L Thompson   @param[in]     field_description Description of field, or `NULL` for none
815cdf32b93SJeremy L Thompson 
816cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
817cdf32b93SJeremy L Thompson 
818cdf32b93SJeremy L Thompson   @ref User
819cdf32b93SJeremy L Thompson **/
8202b730f8bSJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
821cdf32b93SJeremy L Thompson                                       const char *field_description) {
8225b6ec284SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_INT32, num_values);
8235b6ec284SJeremy L Thompson }
8245b6ec284SJeremy L Thompson 
8255b6ec284SJeremy L Thompson /**
826ca94c3ddSJeremy L Thompson   @brief Register a `CeedQFunctionContext` field holding boolean values
8275b6ec284SJeremy L Thompson 
828ca94c3ddSJeremy L Thompson   @param[in,out] ctx               `CeedQFunctionContext`
8295b6ec284SJeremy L Thompson   @param[in]     field_name        Name of field to register
8305b6ec284SJeremy L Thompson   @param[in]     field_offset      Offset of field to register
8315b6ec284SJeremy L Thompson   @param[in]     num_values        Number of values to register, must be contiguous in memory
832ca94c3ddSJeremy L Thompson   @param[in]     field_description Description of field, or `NULL` for none
8335b6ec284SJeremy L Thompson 
8345b6ec284SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8355b6ec284SJeremy L Thompson 
8365b6ec284SJeremy L Thompson   @ref User
8375b6ec284SJeremy L Thompson **/
8385b6ec284SJeremy L Thompson int CeedQFunctionContextRegisterBoolean(CeedQFunctionContext ctx, const char *field_name, size_t field_offset, size_t num_values,
8395b6ec284SJeremy L Thompson                                         const char *field_description) {
8405b6ec284SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset, field_description, CEED_CONTEXT_FIELD_BOOL, num_values);
841cdf32b93SJeremy L Thompson }
842cdf32b93SJeremy L Thompson 
843cdf32b93SJeremy L Thompson /**
844ca94c3ddSJeremy L Thompson   @brief Get labels for all registered `CeedQFunctionContext` fields
845cdf32b93SJeremy L Thompson 
846ca94c3ddSJeremy L Thompson   @param[in]  ctx          `CeedQFunctionContext`
8473668ca4bSJeremy L Thompson   @param[out] field_labels Variable to hold array of field labels
848cdf32b93SJeremy L Thompson   @param[out] num_fields   Length of field descriptions array
849cdf32b93SJeremy L Thompson 
850cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
851cdf32b93SJeremy L Thompson 
852cdf32b93SJeremy L Thompson   @ref User
853cdf32b93SJeremy L Thompson **/
8542b730f8bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx, const CeedContextFieldLabel **field_labels, CeedInt *num_fields) {
8553668ca4bSJeremy L Thompson   *field_labels = ctx->field_labels;
856cdf32b93SJeremy L Thompson   *num_fields   = ctx->num_fields;
857cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
858cdf32b93SJeremy L Thompson }
859cdf32b93SJeremy L Thompson 
860cdf32b93SJeremy L Thompson /**
861ca94c3ddSJeremy L Thompson   @brief Get the descriptive information about a `CeedContextFieldLabel`
8620f86cbe7SJeremy L Thompson 
863ca94c3ddSJeremy L Thompson   @param[in]  label             @ref CeedContextFieldLabel
8640f86cbe7SJeremy L Thompson   @param[out] field_name        Name of labeled field
8651ff07f3dSJeremy L Thompson   @param[out] field_offset      Offset of field registered
8667bfe0f0eSJeremy L Thompson   @param[out] num_values        Number of values registered
8671ff07f3dSJeremy L Thompson   @param[out] field_description Description of field, or NULL for none
868ca94c3ddSJeremy L Thompson   @param[out] field_type        @ref CeedContextFieldType
8690f86cbe7SJeremy L Thompson 
8700f86cbe7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8710f86cbe7SJeremy L Thompson 
8720f86cbe7SJeremy L Thompson   @ref User
8730f86cbe7SJeremy L Thompson **/
8741ff07f3dSJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label, const char **field_name, size_t *field_offset, size_t *num_values,
8751ff07f3dSJeremy L Thompson                                         const char **field_description, CeedContextFieldType *field_type) {
8760f86cbe7SJeremy L Thompson   if (field_name) *field_name = label->name;
8771ff07f3dSJeremy L Thompson   if (field_offset) *field_offset = label->offset;
8787bfe0f0eSJeremy L Thompson   if (num_values) *num_values = label->num_values;
8791ff07f3dSJeremy L Thompson   if (field_description) *field_description = label->description;
8800f86cbe7SJeremy L Thompson   if (field_type) *field_type = label->type;
8810f86cbe7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8820f86cbe7SJeremy L Thompson }
8830f86cbe7SJeremy L Thompson 
8840f86cbe7SJeremy L Thompson /**
88580a9ef05SNatalie Beams   @brief Get data size for a Context
88680a9ef05SNatalie Beams 
887ca94c3ddSJeremy L Thompson   @param[in]  ctx      `CeedQFunctionContext`
88880a9ef05SNatalie Beams   @param[out] ctx_size Variable to store size of context data values
88980a9ef05SNatalie Beams 
89080a9ef05SNatalie Beams   @return An error code: 0 - success, otherwise - failure
89180a9ef05SNatalie Beams 
89280a9ef05SNatalie Beams   @ref User
89380a9ef05SNatalie Beams **/
8942b730f8bSJeremy L Thompson int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx, size_t *ctx_size) {
89580a9ef05SNatalie Beams   *ctx_size = ctx->ctx_size;
89680a9ef05SNatalie Beams   return CEED_ERROR_SUCCESS;
89780a9ef05SNatalie Beams }
89880a9ef05SNatalie Beams 
89980a9ef05SNatalie Beams /**
900*4c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedQFunctionContextView() output
901*4c789ea2SJeremy L Thompson 
902*4c789ea2SJeremy L Thompson   @param[in] ctx      `CeedQFunctionContext` to set the number of view tabs
903*4c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
904*4c789ea2SJeremy L Thompson 
905*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
906*4c789ea2SJeremy L Thompson 
907*4c789ea2SJeremy L Thompson   @ref User
908*4c789ea2SJeremy L Thompson **/
909*4c789ea2SJeremy L Thompson int CeedQFunctionContextSetNumViewTabs(CeedQFunctionContext ctx, CeedInt num_tabs) {
910*4c789ea2SJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
911*4c789ea2SJeremy L Thompson   ctx->num_tabs = num_tabs;
912*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
913*4c789ea2SJeremy L Thompson }
914*4c789ea2SJeremy L Thompson 
915*4c789ea2SJeremy L Thompson /**
916ca94c3ddSJeremy L Thompson   @brief View a `CeedQFunctionContext`
917777ff853SJeremy L Thompson 
918ca94c3ddSJeremy L Thompson   @param[in] ctx    `CeedQFunctionContext` to view
919777ff853SJeremy L Thompson   @param[in] stream Filestream to write to
920777ff853SJeremy L Thompson 
921777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
922777ff853SJeremy L Thompson 
923777ff853SJeremy L Thompson   @ref User
924777ff853SJeremy L Thompson **/
925777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
926*4c789ea2SJeremy L Thompson   char *tabs = NULL;
927*4c789ea2SJeremy L Thompson 
928*4c789ea2SJeremy L Thompson   {
929*4c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
930*4c789ea2SJeremy L Thompson 
931*4c789ea2SJeremy L Thompson     CeedCall(CeedQFunctionContextGetNumViewTabs(ctx, &num_tabs));
932*4c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
933*4c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
9343668ca4bSJeremy L Thompson   }
935*4c789ea2SJeremy L Thompson 
936*4c789ea2SJeremy L Thompson   fprintf(stream, "%sCeedQFunctionContext\n", tabs);
937*4c789ea2SJeremy L Thompson   fprintf(stream, "%s  Context Data Size: %zu\n", tabs, ctx->ctx_size);
938*4c789ea2SJeremy L Thompson   for (CeedInt i = 0; i < ctx->num_fields; i++) {
939*4c789ea2SJeremy L Thompson     fprintf(stream, "%s  Labeled %s field: %s\n", tabs, CeedContextFieldTypes[ctx->field_labels[i]->type], ctx->field_labels[i]->name);
940*4c789ea2SJeremy L Thompson   }
941*4c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
942e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
943777ff853SJeremy L Thompson }
944777ff853SJeremy L Thompson 
945777ff853SJeremy L Thompson /**
946ca94c3ddSJeremy L Thompson   @brief Set additional destroy routine for `CeedQFunctionContext` user data
9472790b72bSJeremy L Thompson 
948ca94c3ddSJeremy L Thompson   @param[in,out] ctx        `CeedQFunctionContext` to set user destroy function
9492e64a2b9SJeremy L Thompson   @param[in]     f_mem_type Memory type to use when passing data into `f`
9502e64a2b9SJeremy L Thompson   @param[in]     f          Additional routine to use to destroy user data
9512790b72bSJeremy L Thompson 
9522790b72bSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
9532790b72bSJeremy L Thompson 
9542790b72bSJeremy L Thompson   @ref User
9552790b72bSJeremy L Thompson **/
9562b730f8bSJeremy L Thompson int CeedQFunctionContextSetDataDestroy(CeedQFunctionContext ctx, CeedMemType f_mem_type, CeedQFunctionContextDataDestroyUser f) {
9576e536b99SJeremy L Thompson   CeedCheck(f, CeedQFunctionContextReturnCeed(ctx), 1, "Must provide valid callback function for destroying user data");
9582790b72bSJeremy L Thompson   ctx->data_destroy_mem_type = f_mem_type;
9592790b72bSJeremy L Thompson   ctx->data_destroy_function = f;
9602790b72bSJeremy L Thompson   return CEED_ERROR_SUCCESS;
9612790b72bSJeremy L Thompson }
9622790b72bSJeremy L Thompson 
9632790b72bSJeremy L Thompson /**
964ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedQFunctionContext`
965777ff853SJeremy L Thompson 
966ca94c3ddSJeremy L Thompson   @param[in,out] ctx `CeedQFunctionContext` to destroy
967777ff853SJeremy L Thompson 
968777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
969777ff853SJeremy L Thompson 
970777ff853SJeremy L Thompson   @ref User
971777ff853SJeremy L Thompson **/
972777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
973ad6481ceSJeremy L Thompson   if (!*ctx || --(*ctx)->ref_count > 0) {
974ad6481ceSJeremy L Thompson     *ctx = NULL;
975ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
976ad6481ceSJeremy L Thompson   }
9776574a04fSJeremy L Thompson   CeedCheck(((*ctx)->state % 2) == 0, (*ctx)->ceed, 1, "Cannot destroy CeedQFunctionContext, the access lock is in use");
978777ff853SJeremy L Thompson 
9792b730f8bSJeremy L Thompson   CeedCall(CeedQFunctionContextDestroyData(*ctx));
9802b730f8bSJeremy L Thompson   if ((*ctx)->Destroy) CeedCall((*ctx)->Destroy(*ctx));
981cdf32b93SJeremy L Thompson   for (CeedInt i = 0; i < (*ctx)->num_fields; i++) {
9822b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]->name));
9832b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]->description));
9842b730f8bSJeremy L Thompson     CeedCall(CeedFree(&(*ctx)->field_labels[i]));
985cdf32b93SJeremy L Thompson   }
9862b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*ctx)->field_labels));
9872b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*ctx)->ceed));
9882b730f8bSJeremy L Thompson   CeedCall(CeedFree(ctx));
989e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
990777ff853SJeremy L Thompson }
991777ff853SJeremy L Thompson 
992777ff853SJeremy L Thompson /// @}
993