xref: /libCEED/interface/ceed-qfunctioncontext.c (revision e6a0ab89c38f1283091e68ca521745ca21d629af)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
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 
8ec3da8bcSJed Brown #include <ceed/ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <ceed-impl.h>
113d576824SJeremy L Thompson #include <stdint.h>
123d576824SJeremy L Thompson #include <stdio.h>
13cdf32b93SJeremy L Thompson #include <string.h>
14777ff853SJeremy L Thompson 
15777ff853SJeremy L Thompson /// @file
16777ff853SJeremy L Thompson /// Implementation of public CeedQFunctionContext interfaces
17777ff853SJeremy L Thompson 
18777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
19cdf32b93SJeremy L Thompson /// CeedQFunctionContext Library Internal Functions
20cdf32b93SJeremy L Thompson /// ----------------------------------------------------------------------------
21cdf32b93SJeremy L Thompson /// @addtogroup CeedQFunctionDeveloper
22cdf32b93SJeremy L Thompson /// @{
23cdf32b93SJeremy L Thompson 
24cdf32b93SJeremy L Thompson /**
25cdf32b93SJeremy L Thompson   @brief Get index for QFunctionContext field
26cdf32b93SJeremy L Thompson 
27cdf32b93SJeremy L Thompson   @param ctx         CeedQFunctionContext
28cdf32b93SJeremy L Thompson   @param field_name  Name of field
29cdf32b93SJeremy L Thompson   @param field_index Index of field, or -1 if field is not registered
30cdf32b93SJeremy L Thompson 
31cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
32cdf32b93SJeremy L Thompson 
33cdf32b93SJeremy L Thompson   @ref Developer
34cdf32b93SJeremy L Thompson **/
35cdf32b93SJeremy L Thompson int CeedQFunctionContextGetFieldIndex(CeedQFunctionContext ctx,
36cdf32b93SJeremy L Thompson                                       const char *field_name, CeedInt *field_index) {
37cdf32b93SJeremy L Thompson   *field_index = -1;
38cdf32b93SJeremy L Thompson   for (CeedInt i=0; i<ctx->num_fields; i++)
393668ca4bSJeremy L Thompson     if (!strcmp(ctx->field_labels[i]->name, field_name))
40cdf32b93SJeremy L Thompson       *field_index = i;
41cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
42cdf32b93SJeremy L Thompson }
43cdf32b93SJeremy L Thompson 
44cdf32b93SJeremy L Thompson /**
45cdf32b93SJeremy L Thompson   @brief Common function for registering QFunctionContext fields
46cdf32b93SJeremy L Thompson 
47cdf32b93SJeremy L Thompson   @param ctx               CeedQFunctionContext
48cdf32b93SJeremy L Thompson   @param field_name        Name of field to register
49cdf32b93SJeremy L Thompson   @param field_offset      Offset of field to register
50cdf32b93SJeremy L Thompson   @param field_description Description of field, or NULL for none
51cdf32b93SJeremy L Thompson   @param field_type        Field data type, such as double or int32
52cdf32b93SJeremy L Thompson   @param field_size        Size of field, in bytes
537bfe0f0eSJeremy L Thompson   @param num_values        Number of values to register, must be contiguous in memory
54cdf32b93SJeremy L Thompson 
55cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
56cdf32b93SJeremy L Thompson 
57cdf32b93SJeremy L Thompson   @ref Developer
58cdf32b93SJeremy L Thompson **/
59cdf32b93SJeremy L Thompson int CeedQFunctionContextRegisterGeneric(CeedQFunctionContext ctx,
60cdf32b93SJeremy L Thompson                                         const char *field_name, size_t field_offset,
61cdf32b93SJeremy L Thompson                                         const char *field_description,
62cdf32b93SJeremy L Thompson                                         CeedContextFieldType field_type,
637bfe0f0eSJeremy L Thompson                                         size_t field_size, size_t num_values) {
64cdf32b93SJeremy L Thompson   int ierr;
65cdf32b93SJeremy L Thompson 
66cdf32b93SJeremy L Thompson   // Check for duplicate
67cdf32b93SJeremy L Thompson   CeedInt field_index = -1;
68cdf32b93SJeremy L Thompson   ierr = CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index);
69cdf32b93SJeremy L Thompson   CeedChk(ierr);
70cdf32b93SJeremy L Thompson   if (field_index != -1)
71cdf32b93SJeremy L Thompson     // LCOV_EXCL_START
72cdf32b93SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
73cdf32b93SJeremy L Thompson                      "QFunctionContext field with name \"%s\" already registered",
74cdf32b93SJeremy L Thompson                      field_name);
75cdf32b93SJeremy L Thompson   // LCOV_EXCL_STOP
76cdf32b93SJeremy L Thompson 
77cdf32b93SJeremy L Thompson   // Allocate space for field data
78cdf32b93SJeremy L Thompson   if (ctx->num_fields == 0) {
793668ca4bSJeremy L Thompson     ierr = CeedCalloc(1, &ctx->field_labels); CeedChk(ierr);
80cdf32b93SJeremy L Thompson     ctx->max_fields = 1;
81cdf32b93SJeremy L Thompson   } else if (ctx->num_fields == ctx->max_fields) {
823668ca4bSJeremy L Thompson     ierr = CeedRealloc(2*ctx->max_fields, &ctx->field_labels);
83cdf32b93SJeremy L Thompson     CeedChk(ierr);
84cdf32b93SJeremy L Thompson     ctx->max_fields *= 2;
85cdf32b93SJeremy L Thompson   }
863668ca4bSJeremy L Thompson   ierr = CeedCalloc(1, &ctx->field_labels[ctx->num_fields]); CeedChk(ierr);
87cdf32b93SJeremy L Thompson 
88cdf32b93SJeremy L Thompson   // Copy field data
89f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_name,
903668ca4bSJeremy L Thompson                              (char **)&ctx->field_labels[ctx->num_fields]->name);
91f7e22acaSJeremy L Thompson   CeedChk(ierr);
92f7e22acaSJeremy L Thompson   ierr = CeedStringAllocCopy(field_description,
933668ca4bSJeremy L Thompson                              (char **)&ctx->field_labels[ctx->num_fields]->description);
94f7e22acaSJeremy L Thompson   CeedChk(ierr);
953668ca4bSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->type = field_type;
963668ca4bSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->offset = field_offset;
977bfe0f0eSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->size = field_size * num_values;
987bfe0f0eSJeremy L Thompson   ctx->field_labels[ctx->num_fields]->num_values = num_values;
99cdf32b93SJeremy L Thompson   ctx->num_fields++;
100cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
101cdf32b93SJeremy L Thompson }
102cdf32b93SJeremy L Thompson 
103cdf32b93SJeremy L Thompson /// @}
104cdf32b93SJeremy L Thompson 
105cdf32b93SJeremy L Thompson /// ----------------------------------------------------------------------------
106777ff853SJeremy L Thompson /// CeedQFunctionContext Backend API
107777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
108777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionBackend
109777ff853SJeremy L Thompson /// @{
110777ff853SJeremy L Thompson 
111777ff853SJeremy L Thompson /**
112777ff853SJeremy L Thompson   @brief Get the Ceed associated with a CeedQFunctionContext
113777ff853SJeremy L Thompson 
114777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
115777ff853SJeremy L Thompson   @param[out] ceed  Variable to store Ceed
116777ff853SJeremy L Thompson 
117777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
118777ff853SJeremy L Thompson 
119777ff853SJeremy L Thompson   @ref Backend
120777ff853SJeremy L Thompson **/
121777ff853SJeremy L Thompson int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) {
122777ff853SJeremy L Thompson   *ceed = ctx->ceed;
123e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
124777ff853SJeremy L Thompson }
125777ff853SJeremy L Thompson 
126777ff853SJeremy L Thompson /**
1279c774eddSJeremy L Thompson   @brief Check for valid data in a CeedQFunctionContext
1289c774eddSJeremy L Thompson 
1299c774eddSJeremy L Thompson   @param ctx                  CeedQFunctionContext to check validity
1309c774eddSJeremy L Thompson   @param[out] has_valid_data  Variable to store validity
1319c774eddSJeremy L Thompson 
1329c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1339c774eddSJeremy L Thompson 
1349c774eddSJeremy L Thompson   @ref Backend
1359c774eddSJeremy L Thompson **/
1369c774eddSJeremy L Thompson int CeedQFunctionContextHasValidData(CeedQFunctionContext ctx,
1379c774eddSJeremy L Thompson                                      bool *has_valid_data) {
1389c774eddSJeremy L Thompson   int ierr;
1399c774eddSJeremy L Thompson 
1409c774eddSJeremy L Thompson   if (!ctx->HasValidData)
1419c774eddSJeremy L Thompson     // LCOV_EXCL_START
1429c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
1439c774eddSJeremy L Thompson                      "Backend does not support HasValidData");
1449c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
1459c774eddSJeremy L Thompson 
1469c774eddSJeremy L Thompson   ierr = ctx->HasValidData(ctx, has_valid_data); CeedChk(ierr);
1479c774eddSJeremy L Thompson 
1489c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1499c774eddSJeremy L Thompson }
1509c774eddSJeremy L Thompson 
1519c774eddSJeremy L Thompson /**
1529c774eddSJeremy L Thompson   @brief Check for borrowed data of a specific CeedMemType in a
1539c774eddSJeremy L Thompson            CeedQFunctionContext
1549c774eddSJeremy L Thompson 
1559c774eddSJeremy L Thompson   @param ctx                             CeedQFunctionContext to check
1569c774eddSJeremy L Thompson   @param mem_type                        Memory type to check
1579c774eddSJeremy L Thompson   @param[out] has_borrowed_data_of_type  Variable to store result
1589c774eddSJeremy L Thompson 
1599c774eddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1609c774eddSJeremy L Thompson 
1619c774eddSJeremy L Thompson   @ref Backend
1629c774eddSJeremy L Thompson **/
1639c774eddSJeremy L Thompson int CeedQFunctionContextHasBorrowedDataOfType(CeedQFunctionContext ctx,
1649c774eddSJeremy L Thompson     CeedMemType mem_type, bool *has_borrowed_data_of_type) {
1659c774eddSJeremy L Thompson   int ierr;
1669c774eddSJeremy L Thompson 
1679c774eddSJeremy L Thompson   if (!ctx->HasBorrowedDataOfType)
1689c774eddSJeremy L Thompson     // LCOV_EXCL_START
1699c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
1709c774eddSJeremy L Thompson                      "Backend does not support HasBorrowedDataOfType");
1719c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
1729c774eddSJeremy L Thompson 
1739c774eddSJeremy L Thompson   ierr = ctx->HasBorrowedDataOfType(ctx, mem_type, has_borrowed_data_of_type);
1749c774eddSJeremy L Thompson   CeedChk(ierr);
1759c774eddSJeremy L Thompson 
1769c774eddSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1779c774eddSJeremy L Thompson }
1789c774eddSJeremy L Thompson 
1799c774eddSJeremy L Thompson /**
180777ff853SJeremy L Thompson   @brief Get the state of a CeedQFunctionContext
181777ff853SJeremy L Thompson 
182777ff853SJeremy L Thompson   @param ctx         CeedQFunctionContext to retrieve state
183777ff853SJeremy L Thompson   @param[out] state  Variable to store state
184777ff853SJeremy L Thompson 
185777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
186777ff853SJeremy L Thompson 
187777ff853SJeremy L Thompson   @ref Backend
188777ff853SJeremy L Thompson **/
189777ff853SJeremy L Thompson int CeedQFunctionContextGetState(CeedQFunctionContext ctx, uint64_t *state) {
190777ff853SJeremy L Thompson   *state = ctx->state;
191e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
192777ff853SJeremy L Thompson }
193777ff853SJeremy L Thompson 
194777ff853SJeremy L Thompson /**
195777ff853SJeremy L Thompson   @brief Get backend data of a CeedQFunctionContext
196777ff853SJeremy L Thompson 
197777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
198777ff853SJeremy L Thompson   @param[out] data  Variable to store data
199777ff853SJeremy L Thompson 
200777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
201777ff853SJeremy L Thompson 
202777ff853SJeremy L Thompson   @ref Backend
203777ff853SJeremy L Thompson **/
204777ff853SJeremy L Thompson int CeedQFunctionContextGetBackendData(CeedQFunctionContext ctx, void *data) {
205777ff853SJeremy L Thompson   *(void **)data = ctx->data;
206e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
207777ff853SJeremy L Thompson }
208777ff853SJeremy L Thompson 
209777ff853SJeremy L Thompson /**
210777ff853SJeremy L Thompson   @brief Set backend data of a CeedQFunctionContext
211777ff853SJeremy L Thompson 
212777ff853SJeremy L Thompson   @param[out] ctx  CeedQFunctionContext
213777ff853SJeremy L Thompson   @param data      Data to set
214777ff853SJeremy L Thompson 
215777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
216777ff853SJeremy L Thompson 
217777ff853SJeremy L Thompson   @ref Backend
218777ff853SJeremy L Thompson **/
219777ff853SJeremy L Thompson int CeedQFunctionContextSetBackendData(CeedQFunctionContext ctx, void *data) {
220777ff853SJeremy L Thompson   ctx->data = data;
221e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
222777ff853SJeremy L Thompson }
223777ff853SJeremy L Thompson 
22434359f16Sjeremylt /**
225*e6a0ab89SJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no
226*e6a0ab89SJeremy L Thompson            field has been registered with this `field_name`
227*e6a0ab89SJeremy L Thompson 
228*e6a0ab89SJeremy L Thompson   @param[in] ctx           CeedQFunctionContext
229*e6a0ab89SJeremy L Thompson   @param[in] field_name    Name of field to retrieve label
230*e6a0ab89SJeremy L Thompson   @param[out] field_label  Variable to field label
231*e6a0ab89SJeremy L Thompson 
232*e6a0ab89SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
233*e6a0ab89SJeremy L Thompson 
234*e6a0ab89SJeremy L Thompson   @ref User
235*e6a0ab89SJeremy L Thompson **/
236*e6a0ab89SJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx,
237*e6a0ab89SJeremy L Thompson                                       const char *field_name,
238*e6a0ab89SJeremy L Thompson                                       CeedContextFieldLabel *field_label) {
239*e6a0ab89SJeremy L Thompson   int ierr;
240*e6a0ab89SJeremy L Thompson 
241*e6a0ab89SJeremy L Thompson   CeedInt field_index;
242*e6a0ab89SJeremy L Thompson   ierr = CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index);
243*e6a0ab89SJeremy L Thompson   CeedChk(ierr);
244*e6a0ab89SJeremy L Thompson 
245*e6a0ab89SJeremy L Thompson   if (field_index != -1) {
246*e6a0ab89SJeremy L Thompson     *field_label = ctx->field_labels[field_index];
247*e6a0ab89SJeremy L Thompson   } else {
248*e6a0ab89SJeremy L Thompson     *field_label = NULL;
249*e6a0ab89SJeremy L Thompson   }
250*e6a0ab89SJeremy L Thompson 
251*e6a0ab89SJeremy L Thompson   return CEED_ERROR_SUCCESS;
252*e6a0ab89SJeremy L Thompson }
253*e6a0ab89SJeremy L Thompson 
254*e6a0ab89SJeremy L Thompson /**
255d8dd9a91SJeremy L Thompson   @brief Set QFunctionContext field
256d8dd9a91SJeremy L Thompson 
257d8dd9a91SJeremy L Thompson   @param ctx         CeedQFunctionContext
2583668ca4bSJeremy L Thompson   @param field_label Label of field to set
259d8dd9a91SJeremy L Thompson   @param field_type  Type of field to set
260d8dd9a91SJeremy L Thompson   @param value       Value to set
261d8dd9a91SJeremy L Thompson 
262d8dd9a91SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
263d8dd9a91SJeremy L Thompson 
264d8dd9a91SJeremy L Thompson   @ref User
265d8dd9a91SJeremy L Thompson **/
266d8dd9a91SJeremy L Thompson int CeedQFunctionContextSetGeneric(CeedQFunctionContext ctx,
2673668ca4bSJeremy L Thompson                                    CeedContextFieldLabel field_label,
268d8dd9a91SJeremy L Thompson                                    CeedContextFieldType field_type,
2693668ca4bSJeremy L Thompson                                    void *value) {
270d8dd9a91SJeremy L Thompson   int ierr;
271d8dd9a91SJeremy L Thompson 
2723668ca4bSJeremy L Thompson   // Check field type
2733668ca4bSJeremy L Thompson   if (field_label->type != field_type)
274d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
275d8dd9a91SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
276d8dd9a91SJeremy L Thompson                      "QFunctionContext field with name \"%s\" registered as %s, "
2773668ca4bSJeremy L Thompson                      "not registered as %s", field_label->name,
2783668ca4bSJeremy L Thompson                      CeedContextFieldTypes[field_label->type],
279d8dd9a91SJeremy L Thompson                      CeedContextFieldTypes[field_type]);
280d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
281d8dd9a91SJeremy L Thompson 
282d8dd9a91SJeremy L Thompson   char *data;
283d8dd9a91SJeremy L Thompson   ierr = CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &data); CeedChk(ierr);
2843668ca4bSJeremy L Thompson   memcpy(&data[field_label->offset], value, field_label->size);
285d8dd9a91SJeremy L Thompson   ierr = CeedQFunctionContextRestoreData(ctx, &data); CeedChk(ierr);
286d8dd9a91SJeremy L Thompson 
287d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
288d8dd9a91SJeremy L Thompson }
289d8dd9a91SJeremy L Thompson 
290d8dd9a91SJeremy L Thompson /**
29134359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunctionContext
29234359f16Sjeremylt 
29334359f16Sjeremylt   @param ctx  CeedQFunctionContext to increment the reference counter
29434359f16Sjeremylt 
29534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
29634359f16Sjeremylt 
29734359f16Sjeremylt   @ref Backend
29834359f16Sjeremylt **/
2999560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
30034359f16Sjeremylt   ctx->ref_count++;
30134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
30234359f16Sjeremylt }
30334359f16Sjeremylt 
304777ff853SJeremy L Thompson /// @}
305777ff853SJeremy L Thompson 
306777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
307777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
308777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
309777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
310777ff853SJeremy L Thompson /// @{
311777ff853SJeremy L Thompson 
312777ff853SJeremy L Thompson /**
313777ff853SJeremy L Thompson   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
314777ff853SJeremy L Thompson 
315777ff853SJeremy L Thompson   @param ceed      A Ceed object where the CeedQFunctionContext will be created
316777ff853SJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created
317777ff853SJeremy L Thompson                      CeedQFunctionContext will be stored
318777ff853SJeremy L Thompson 
319777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
320777ff853SJeremy L Thompson 
321777ff853SJeremy L Thompson   @ref User
322777ff853SJeremy L Thompson **/
323777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
324777ff853SJeremy L Thompson   int ierr;
325777ff853SJeremy L Thompson 
326777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
327777ff853SJeremy L Thompson     Ceed delegate;
328777ff853SJeremy L Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "Context"); CeedChk(ierr);
329777ff853SJeremy L Thompson 
330777ff853SJeremy L Thompson     if (!delegate)
331777ff853SJeremy L Thompson       // LCOV_EXCL_START
332e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
333e15f9bd0SJeremy L Thompson                        "Backend does not support ContextCreate");
334777ff853SJeremy L Thompson     // LCOV_EXCL_STOP
335777ff853SJeremy L Thompson 
336777ff853SJeremy L Thompson     ierr = CeedQFunctionContextCreate(delegate, ctx); CeedChk(ierr);
337e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
338777ff853SJeremy L Thompson   }
339777ff853SJeremy L Thompson 
340777ff853SJeremy L Thompson   ierr = CeedCalloc(1, ctx); CeedChk(ierr);
341777ff853SJeremy L Thompson   (*ctx)->ceed = ceed;
3429560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
343d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
344777ff853SJeremy L Thompson   ierr = ceed->QFunctionContextCreate(*ctx); CeedChk(ierr);
345e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
346777ff853SJeremy L Thompson }
347777ff853SJeremy L Thompson 
348777ff853SJeremy L Thompson /**
3499560d06aSjeremylt   @brief Copy the pointer to a CeedQFunctionContext. Both pointers should
3509560d06aSjeremylt            be destroyed with `CeedQFunctionContextDestroy()`;
3519560d06aSjeremylt            Note: If `*ctx_copy` is non-NULL, then it is assumed that
3529560d06aSjeremylt            `*ctx_copy` is a pointer to a CeedQFunctionContext. This
3539560d06aSjeremylt            CeedQFunctionContext will be destroyed if `*ctx_copy` is the
3549560d06aSjeremylt            only reference to this CeedQFunctionContext.
3559560d06aSjeremylt 
3569560d06aSjeremylt   @param ctx            CeedQFunctionContext to copy reference to
3579560d06aSjeremylt   @param[out] ctx_copy  Variable to store copied reference
3589560d06aSjeremylt 
3599560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
3609560d06aSjeremylt 
3619560d06aSjeremylt   @ref User
3629560d06aSjeremylt **/
3639560d06aSjeremylt int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx,
3649560d06aSjeremylt                                       CeedQFunctionContext *ctx_copy) {
3659560d06aSjeremylt   int ierr;
3669560d06aSjeremylt 
3679560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
3689560d06aSjeremylt   ierr = CeedQFunctionContextDestroy(ctx_copy); CeedChk(ierr);
3699560d06aSjeremylt   *ctx_copy = ctx;
3709560d06aSjeremylt   return CEED_ERROR_SUCCESS;
3719560d06aSjeremylt }
3729560d06aSjeremylt 
3739560d06aSjeremylt /**
374777ff853SJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
375777ff853SJeremy L Thompson            data if applicable. The backend may copy values to a different
376777ff853SJeremy L Thompson            memtype, such as during @ref CeedQFunctionApply().
377777ff853SJeremy L Thompson            See also @ref CeedQFunctionContextTakeData().
378777ff853SJeremy L Thompson 
379777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
380d1d35e2fSjeremylt   @param mem_type   Memory type of the data being passed
381d1d35e2fSjeremylt   @param copy_mode  Copy mode for the data
382891038deSjeremylt   @param size       Size of data, in bytes
383777ff853SJeremy L Thompson   @param data       Data to be used
384777ff853SJeremy L Thompson 
385777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
386777ff853SJeremy L Thompson 
387777ff853SJeremy L Thompson   @ref User
388777ff853SJeremy L Thompson **/
389d1d35e2fSjeremylt int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
390d1d35e2fSjeremylt                                 CeedCopyMode copy_mode,
391777ff853SJeremy L Thompson                                 size_t size, void *data) {
392777ff853SJeremy L Thompson   int ierr;
393777ff853SJeremy L Thompson 
394777ff853SJeremy L Thompson   if (!ctx->SetData)
395777ff853SJeremy L Thompson     // LCOV_EXCL_START
396e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
397e15f9bd0SJeremy L Thompson                      "Backend does not support ContextSetData");
398777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
399777ff853SJeremy L Thompson 
400777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
401777ff853SJeremy L Thompson     // LCOV_EXCL_START
402777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
403777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
404777ff853SJeremy L Thompson                      "access lock is already in use");
405777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
406777ff853SJeremy L Thompson 
407d1d35e2fSjeremylt   ctx->ctx_size = size;
408d1d35e2fSjeremylt   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
409777ff853SJeremy L Thompson   ctx->state += 2;
410e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
411777ff853SJeremy L Thompson }
412777ff853SJeremy L Thompson 
413777ff853SJeremy L Thompson /**
414891038deSjeremylt   @brief Take ownership of the data in a CeedQFunctionContext via the specified memory type.
415891038deSjeremylt            The caller is responsible for managing and freeing the memory.
416891038deSjeremylt 
417891038deSjeremylt   @param ctx        CeedQFunctionContext to access
418891038deSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
419891038deSjeremylt                       uses a different memory type, this will perform a copy.
420891038deSjeremylt   @param[out] data  Data on memory type mem_type
421891038deSjeremylt 
422891038deSjeremylt   @return An error code: 0 - success, otherwise - failure
423891038deSjeremylt 
424891038deSjeremylt   @ref User
425891038deSjeremylt **/
426891038deSjeremylt int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type,
427891038deSjeremylt                                  void *data) {
428891038deSjeremylt   int ierr;
429891038deSjeremylt 
4309c774eddSJeremy L Thompson   bool has_valid_data = true;
4319c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
4329c774eddSJeremy L Thompson   if (!has_valid_data)
4339c774eddSJeremy L Thompson     // LCOV_EXCL_START
4349c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
4359c774eddSJeremy L Thompson                      "CeedQFunctionContext has no valid data to take, must set data");
4369c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
4379c774eddSJeremy L Thompson 
438891038deSjeremylt   if (!ctx->TakeData)
439891038deSjeremylt     // LCOV_EXCL_START
440891038deSjeremylt     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
441891038deSjeremylt                      "Backend does not support TakeData");
442891038deSjeremylt   // LCOV_EXCL_STOP
443891038deSjeremylt 
444891038deSjeremylt   if (ctx->state % 2 == 1)
445891038deSjeremylt     // LCOV_EXCL_START
446891038deSjeremylt     return CeedError(ctx->ceed, 1,
447891038deSjeremylt                      "Cannot grant CeedQFunctionContext data access, the "
448891038deSjeremylt                      "access lock is already in use");
449891038deSjeremylt   // LCOV_EXCL_STOP
450891038deSjeremylt 
4519c774eddSJeremy L Thompson   bool has_borrowed_data_of_type = true;
4529c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type,
4539c774eddSJeremy L Thompson          &has_borrowed_data_of_type); CeedChk(ierr);
4549c774eddSJeremy L Thompson   if (!has_borrowed_data_of_type)
4559c774eddSJeremy L Thompson     // LCOV_EXCL_START
4569c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
4579c774eddSJeremy L Thompson                      "CeedQFunctionContext has no borowed %s data, "
4589c774eddSJeremy L Thompson                      "must set data with CeedQFunctionContextSetData",
4599c774eddSJeremy L Thompson                      CeedMemTypes[mem_type]);
4609c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
4619c774eddSJeremy L Thompson 
462891038deSjeremylt   void *temp_data = NULL;
463891038deSjeremylt   ierr = ctx->TakeData(ctx, mem_type, &temp_data); CeedChk(ierr);
464891038deSjeremylt   if (data) (*(void **)data) = temp_data;
465891038deSjeremylt   return CEED_ERROR_SUCCESS;
466891038deSjeremylt }
467891038deSjeremylt 
468891038deSjeremylt /**
469777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
470777ff853SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
471777ff853SJeremy L Thompson 
472777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
473d1d35e2fSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
474777ff853SJeremy L Thompson                       uses a different memory type, this will perform a copy.
475d1d35e2fSjeremylt   @param[out] data  Data on memory type mem_type
476777ff853SJeremy L Thompson 
477777ff853SJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
478777ff853SJeremy L Thompson     provide access to array pointers in the desired memory space. Pairing
479777ff853SJeremy L Thompson     get/restore allows the Context to track access.
480777ff853SJeremy L Thompson 
481777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
482777ff853SJeremy L Thompson 
483777ff853SJeremy L Thompson   @ref User
484777ff853SJeremy L Thompson **/
485d1d35e2fSjeremylt int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
486777ff853SJeremy L Thompson                                 void *data) {
487777ff853SJeremy L Thompson   int ierr;
488777ff853SJeremy L Thompson 
489777ff853SJeremy L Thompson   if (!ctx->GetData)
490777ff853SJeremy L Thompson     // LCOV_EXCL_START
491e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
492e15f9bd0SJeremy L Thompson                      "Backend does not support GetData");
493777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
494777ff853SJeremy L Thompson 
495777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
496777ff853SJeremy L Thompson     // LCOV_EXCL_START
497777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
498777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
499777ff853SJeremy L Thompson                      "access lock is already in use");
500777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
501777ff853SJeremy L Thompson 
50228bfd0b7SJeremy L Thompson   if (ctx->num_readers > 0)
50328bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
50428bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
50528bfd0b7SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, a "
50628bfd0b7SJeremy L Thompson                      "process has read access");
50728bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
50828bfd0b7SJeremy L Thompson 
5099c774eddSJeremy L Thompson   bool has_valid_data = true;
5109c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
5119c774eddSJeremy L Thompson   if (!has_valid_data)
5129c774eddSJeremy L Thompson     // LCOV_EXCL_START
5139c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
5149c774eddSJeremy L Thompson                      "CeedQFunctionContext has no valid data to get, must set data");
5159c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5169c774eddSJeremy L Thompson 
517d1d35e2fSjeremylt   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
51828bfd0b7SJeremy L Thompson   ctx->state++;
51928bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
52028bfd0b7SJeremy L Thompson }
52128bfd0b7SJeremy L Thompson 
52228bfd0b7SJeremy L Thompson /**
52328bfd0b7SJeremy L Thompson   @brief Get read only access to a CeedQFunctionContext via the specified memory type.
52428bfd0b7SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
52528bfd0b7SJeremy L Thompson 
52628bfd0b7SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
52728bfd0b7SJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
52828bfd0b7SJeremy L Thompson                       uses a different memory type, this will perform a copy.
52928bfd0b7SJeremy L Thompson   @param[out] data  Data on memory type mem_type
53028bfd0b7SJeremy L Thompson 
53128bfd0b7SJeremy L Thompson   @note The CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead()
53228bfd0b7SJeremy L Thompson     functions provide access to array pointers in the desired memory space. Pairing
53328bfd0b7SJeremy L Thompson     get/restore allows the Context to track access.
53428bfd0b7SJeremy L Thompson 
53528bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
53628bfd0b7SJeremy L Thompson 
53728bfd0b7SJeremy L Thompson   @ref User
53828bfd0b7SJeremy L Thompson **/
53928bfd0b7SJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx,
54028bfd0b7SJeremy L Thompson                                     CeedMemType mem_type,
54128bfd0b7SJeremy L Thompson                                     void *data) {
54228bfd0b7SJeremy L Thompson   int ierr;
54328bfd0b7SJeremy L Thompson 
54428bfd0b7SJeremy L Thompson   if (!ctx->GetDataRead)
54528bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
54628bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
54728bfd0b7SJeremy L Thompson                      "Backend does not support GetDataRead");
54828bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
54928bfd0b7SJeremy L Thompson 
55028bfd0b7SJeremy L Thompson   if (ctx->state % 2 == 1)
55128bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
55228bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
55328bfd0b7SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
55428bfd0b7SJeremy L Thompson                      "access lock is already in use");
55528bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
55628bfd0b7SJeremy L Thompson 
55728bfd0b7SJeremy L Thompson   bool has_valid_data = true;
55828bfd0b7SJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
55928bfd0b7SJeremy L Thompson   if (!has_valid_data)
56028bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
56128bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
56228bfd0b7SJeremy L Thompson                      "CeedQFunctionContext has no valid data to get, must set data");
56328bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
56428bfd0b7SJeremy L Thompson 
56528bfd0b7SJeremy L Thompson   ierr = ctx->GetDataRead(ctx, mem_type, data); CeedChk(ierr);
56628bfd0b7SJeremy L Thompson   ctx->num_readers++;
567e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
568777ff853SJeremy L Thompson }
569777ff853SJeremy L Thompson 
570777ff853SJeremy L Thompson /**
571777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
572777ff853SJeremy L Thompson 
573777ff853SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
574777ff853SJeremy L Thompson   @param data  Data to restore
575777ff853SJeremy L Thompson 
576777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
577777ff853SJeremy L Thompson 
578777ff853SJeremy L Thompson   @ref User
579777ff853SJeremy L Thompson **/
580777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
581777ff853SJeremy L Thompson   int ierr;
582777ff853SJeremy L Thompson 
583777ff853SJeremy L Thompson   if (ctx->state % 2 != 1)
584777ff853SJeremy L Thompson     // LCOV_EXCL_START
585777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
586777ff853SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
587777ff853SJeremy L Thompson                      "access was not granted");
588777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
589777ff853SJeremy L Thompson 
590706efda3SJeremy L Thompson   if (ctx->RestoreData) {
591777ff853SJeremy L Thompson     ierr = ctx->RestoreData(ctx); CeedChk(ierr);
592706efda3SJeremy L Thompson   }
593777ff853SJeremy L Thompson   *(void **)data = NULL;
59428bfd0b7SJeremy L Thompson   ctx->state++;
59528bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
59628bfd0b7SJeremy L Thompson }
59728bfd0b7SJeremy L Thompson 
59828bfd0b7SJeremy L Thompson /**
59928bfd0b7SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead()
60028bfd0b7SJeremy L Thompson 
60128bfd0b7SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
60228bfd0b7SJeremy L Thompson   @param data  Data to restore
60328bfd0b7SJeremy L Thompson 
60428bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
60528bfd0b7SJeremy L Thompson 
60628bfd0b7SJeremy L Thompson   @ref User
60728bfd0b7SJeremy L Thompson **/
60828bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) {
60928bfd0b7SJeremy L Thompson   int ierr;
61028bfd0b7SJeremy L Thompson 
61128bfd0b7SJeremy L Thompson   if (ctx->num_readers == 0)
61228bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
61328bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
61428bfd0b7SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
61528bfd0b7SJeremy L Thompson                      "access was not granted");
61628bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
61728bfd0b7SJeremy L Thompson 
61828bfd0b7SJeremy L Thompson   if (ctx->RestoreDataRead) {
61928bfd0b7SJeremy L Thompson     ierr = ctx->RestoreData(ctx); CeedChk(ierr);
62028bfd0b7SJeremy L Thompson   }
62128bfd0b7SJeremy L Thompson   *(void **)data = NULL;
62228bfd0b7SJeremy L Thompson   ctx->num_readers--;
623e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
624777ff853SJeremy L Thompson }
625777ff853SJeremy L Thompson 
626777ff853SJeremy L Thompson /**
627cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a double precision value
628cdf32b93SJeremy L Thompson 
629cdf32b93SJeremy L Thompson   @param ctx               CeedQFunctionContext
630cdf32b93SJeremy L Thompson   @param field_name        Name of field to register
631cdf32b93SJeremy L Thompson   @param field_offset      Offset of field to register
6327bfe0f0eSJeremy L Thompson   @param num_values        Number of values to register, must be contiguous in memory
633cdf32b93SJeremy L Thompson   @param field_description Description of field, or NULL for none
634cdf32b93SJeremy L Thompson 
635cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
636cdf32b93SJeremy L Thompson 
637cdf32b93SJeremy L Thompson   @ref User
638cdf32b93SJeremy L Thompson **/
639cdf32b93SJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx,
640cdf32b93SJeremy L Thompson                                        const char *field_name, size_t field_offset,
6417bfe0f0eSJeremy L Thompson                                        size_t num_values,
642cdf32b93SJeremy L Thompson                                        const char *field_description) {
643cdf32b93SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset,
6447bfe0f0eSJeremy L Thompson          field_description, CEED_CONTEXT_FIELD_DOUBLE, sizeof(double), num_values);
645cdf32b93SJeremy L Thompson }
646cdf32b93SJeremy L Thompson 
647cdf32b93SJeremy L Thompson /**
648cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a int32 value
649cdf32b93SJeremy L Thompson 
650cdf32b93SJeremy L Thompson   @param ctx               CeedQFunctionContext
651cdf32b93SJeremy L Thompson   @param field_name        Name of field to register
652cdf32b93SJeremy L Thompson   @param field_offset      Offset of field to register
6537bfe0f0eSJeremy L Thompson   @param num_values        Number of values to register, must be contiguous in memory
654cdf32b93SJeremy L Thompson   @param field_description Description of field, or NULL for none
655cdf32b93SJeremy L Thompson 
656cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
657cdf32b93SJeremy L Thompson 
658cdf32b93SJeremy L Thompson   @ref User
659cdf32b93SJeremy L Thompson **/
660cdf32b93SJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx,
661cdf32b93SJeremy L Thompson                                       const char *field_name, size_t field_offset,
6627bfe0f0eSJeremy L Thompson                                       size_t num_values,
663cdf32b93SJeremy L Thompson                                       const char *field_description) {
664cdf32b93SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset,
6657bfe0f0eSJeremy L Thompson          field_description, CEED_CONTEXT_FIELD_INT32, sizeof(int), num_values);
666cdf32b93SJeremy L Thompson }
667cdf32b93SJeremy L Thompson 
668cdf32b93SJeremy L Thompson /**
6693668ca4bSJeremy L Thompson   @brief Get labels for all registered QFunctionContext fields
670cdf32b93SJeremy L Thompson 
671cdf32b93SJeremy L Thompson   @param ctx                CeedQFunctionContext
6723668ca4bSJeremy L Thompson   @param[out] field_labels  Variable to hold array of field labels
673cdf32b93SJeremy L Thompson   @param[out] num_fields    Length of field descriptions array
674cdf32b93SJeremy L Thompson 
675cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
676cdf32b93SJeremy L Thompson 
677cdf32b93SJeremy L Thompson   @ref User
678cdf32b93SJeremy L Thompson **/
6793668ca4bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx,
6803668ca4bSJeremy L Thompson     const CeedContextFieldLabel **field_labels, CeedInt *num_fields) {
6813668ca4bSJeremy L Thompson   *field_labels = ctx->field_labels;
682cdf32b93SJeremy L Thompson   *num_fields = ctx->num_fields;
683cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
684cdf32b93SJeremy L Thompson }
685cdf32b93SJeremy L Thompson 
686cdf32b93SJeremy L Thompson /**
6870f86cbe7SJeremy L Thompson   @brief Get the descriptive information about a CeedContextFieldLabel
6880f86cbe7SJeremy L Thompson 
6890f86cbe7SJeremy L Thompson   @param[in] label              CeedContextFieldLabel
6900f86cbe7SJeremy L Thompson   @param[out] field_name        Name of labeled field
6910f86cbe7SJeremy L Thompson   @param[out] field_description Description of field, or NULL for none
6927bfe0f0eSJeremy L Thompson   @param[out] num_values        Number of values registered
6930f86cbe7SJeremy L Thompson   @param[out] field_type        CeedContextFieldType
6940f86cbe7SJeremy L Thompson 
6950f86cbe7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
6960f86cbe7SJeremy L Thompson 
6970f86cbe7SJeremy L Thompson   @ref User
6980f86cbe7SJeremy L Thompson **/
6990f86cbe7SJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label,
7000f86cbe7SJeremy L Thompson                                         const char **field_name,
7010f86cbe7SJeremy L Thompson                                         const char **field_description,
7027bfe0f0eSJeremy L Thompson                                         size_t *num_values,
7030f86cbe7SJeremy L Thompson                                         CeedContextFieldType *field_type) {
7040f86cbe7SJeremy L Thompson   if (field_name) *field_name = label->name;
7050f86cbe7SJeremy L Thompson   if (field_description) *field_description = label->description;
7067bfe0f0eSJeremy L Thompson   if (num_values) *num_values = label->num_values;
7070f86cbe7SJeremy L Thompson   if (field_type) *field_type = label->type;
7080f86cbe7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7090f86cbe7SJeremy L Thompson }
7100f86cbe7SJeremy L Thompson 
7110f86cbe7SJeremy L Thompson /**
712cdf32b93SJeremy L Thompson   @brief Set QFunctionContext field holding a double precision value
713cdf32b93SJeremy L Thompson 
714cdf32b93SJeremy L Thompson   @param ctx         CeedQFunctionContext
7153668ca4bSJeremy L Thompson   @param field_label Label for field to register
7167bfe0f0eSJeremy L Thompson   @param values      Values to set
717cdf32b93SJeremy L Thompson 
718cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
719cdf32b93SJeremy L Thompson 
720cdf32b93SJeremy L Thompson   @ref User
721cdf32b93SJeremy L Thompson **/
722cdf32b93SJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx,
7237bfe0f0eSJeremy L Thompson                                   CeedContextFieldLabel field_label, double *values) {
724d8dd9a91SJeremy L Thompson   int ierr;
725d8dd9a91SJeremy L Thompson 
7263668ca4bSJeremy L Thompson   if (!field_label)
727d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
728d8dd9a91SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
7293668ca4bSJeremy L Thompson                      "Invalid field label");
730d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
731d8dd9a91SJeremy L Thompson 
7323668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextSetGeneric(ctx, field_label,
7333668ca4bSJeremy L Thompson                                         CEED_CONTEXT_FIELD_DOUBLE,
7347bfe0f0eSJeremy L Thompson                                         values); CeedChk(ierr);
7353668ca4bSJeremy L Thompson 
736d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
737cdf32b93SJeremy L Thompson }
738cdf32b93SJeremy L Thompson 
739cdf32b93SJeremy L Thompson /**
740d8dd9a91SJeremy L Thompson   @brief Set QFunctionContext field holding an int32 value
741cdf32b93SJeremy L Thompson 
742cdf32b93SJeremy L Thompson   @param ctx         CeedQFunctionContext
7433668ca4bSJeremy L Thompson   @param field_label Label for field to register
7447bfe0f0eSJeremy L Thompson   @param values      Values to set
745cdf32b93SJeremy L Thompson 
746cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
747cdf32b93SJeremy L Thompson 
748cdf32b93SJeremy L Thompson   @ref User
749cdf32b93SJeremy L Thompson **/
750cdf32b93SJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx,
7517bfe0f0eSJeremy L Thompson                                  CeedContextFieldLabel field_label, int *values) {
752d8dd9a91SJeremy L Thompson   int ierr;
753d8dd9a91SJeremy L Thompson 
7543668ca4bSJeremy L Thompson   if (!field_label)
755d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
756d8dd9a91SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
7573668ca4bSJeremy L Thompson                      "Invalid field label");
758d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
759d8dd9a91SJeremy L Thompson 
7603668ca4bSJeremy L Thompson   ierr = CeedQFunctionContextSetGeneric(ctx, field_label,
7613668ca4bSJeremy L Thompson                                         CEED_CONTEXT_FIELD_INT32,
7627bfe0f0eSJeremy L Thompson                                         values); CeedChk(ierr);
7633668ca4bSJeremy L Thompson 
764d8dd9a91SJeremy L Thompson   return CEED_ERROR_SUCCESS;
765cdf32b93SJeremy L Thompson }
766cdf32b93SJeremy L Thompson 
767cdf32b93SJeremy L Thompson /**
76880a9ef05SNatalie Beams   @brief Get data size for a Context
76980a9ef05SNatalie Beams 
77080a9ef05SNatalie Beams   @param ctx            CeedQFunctionContext
77180a9ef05SNatalie Beams   @param[out] ctx_size  Variable to store size of context data values
77280a9ef05SNatalie Beams 
77380a9ef05SNatalie Beams   @return An error code: 0 - success, otherwise - failure
77480a9ef05SNatalie Beams 
77580a9ef05SNatalie Beams   @ref User
77680a9ef05SNatalie Beams **/
77780a9ef05SNatalie Beams int CeedQFunctionContextGetContextSize(CeedQFunctionContext ctx,
77880a9ef05SNatalie Beams                                        size_t *ctx_size) {
77980a9ef05SNatalie Beams   *ctx_size = ctx->ctx_size;
78080a9ef05SNatalie Beams   return CEED_ERROR_SUCCESS;
78180a9ef05SNatalie Beams }
78280a9ef05SNatalie Beams 
78380a9ef05SNatalie Beams 
78480a9ef05SNatalie Beams /**
785777ff853SJeremy L Thompson   @brief View a CeedQFunctionContext
786777ff853SJeremy L Thompson 
787777ff853SJeremy L Thompson   @param[in] ctx     CeedQFunctionContext to view
788777ff853SJeremy L Thompson   @param[in] stream  Filestream to write to
789777ff853SJeremy L Thompson 
790777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
791777ff853SJeremy L Thompson 
792777ff853SJeremy L Thompson   @ref User
793777ff853SJeremy L Thompson **/
794777ff853SJeremy L Thompson int CeedQFunctionContextView(CeedQFunctionContext ctx, FILE *stream) {
795777ff853SJeremy L Thompson   fprintf(stream, "CeedQFunctionContext\n");
796d1d35e2fSjeremylt   fprintf(stream, "  Context Data Size: %ld\n", ctx->ctx_size);
7973668ca4bSJeremy L Thompson   for (CeedInt i = 0; i < ctx->num_fields; i++) {
7983668ca4bSJeremy L Thompson     // LCOV_EXCL_START
7993668ca4bSJeremy L Thompson     fprintf(stream, "  Labeled %s field: %s\n",
8003668ca4bSJeremy L Thompson             CeedContextFieldTypes[ctx->field_labels[i]->type],
8013668ca4bSJeremy L Thompson             ctx->field_labels[i]->name);
8023668ca4bSJeremy L Thompson     // LCOV_EXCL_STOP
8033668ca4bSJeremy L Thompson   }
804e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
805777ff853SJeremy L Thompson }
806777ff853SJeremy L Thompson 
807777ff853SJeremy L Thompson /**
808777ff853SJeremy L Thompson   @brief Destroy a CeedQFunctionContext
809777ff853SJeremy L Thompson 
810777ff853SJeremy L Thompson   @param ctx  CeedQFunctionContext to destroy
811777ff853SJeremy L Thompson 
812777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
813777ff853SJeremy L Thompson 
814777ff853SJeremy L Thompson   @ref User
815777ff853SJeremy L Thompson **/
816777ff853SJeremy L Thompson int CeedQFunctionContextDestroy(CeedQFunctionContext *ctx) {
817777ff853SJeremy L Thompson   int ierr;
818777ff853SJeremy L Thompson 
819d1d35e2fSjeremylt   if (!*ctx || --(*ctx)->ref_count > 0)
820e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
821777ff853SJeremy L Thompson 
822777ff853SJeremy L Thompson   if ((*ctx) && ((*ctx)->state % 2) == 1)
823777ff853SJeremy L Thompson     // LCOV_EXCL_START
824777ff853SJeremy L Thompson     return CeedError((*ctx)->ceed, 1,
825777ff853SJeremy L Thompson                      "Cannot destroy CeedQFunctionContext, the access "
826777ff853SJeremy L Thompson                      "lock is in use");
827777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
828777ff853SJeremy L Thompson 
829777ff853SJeremy L Thompson   if ((*ctx)->Destroy) {
830777ff853SJeremy L Thompson     ierr = (*ctx)->Destroy(*ctx); CeedChk(ierr);
831777ff853SJeremy L Thompson   }
832cdf32b93SJeremy L Thompson   for (CeedInt i=0; i<(*ctx)->num_fields; i++) {
8333668ca4bSJeremy L Thompson     ierr = CeedFree(&(*ctx)->field_labels[i]->name); CeedChk(ierr);
8343668ca4bSJeremy L Thompson     ierr = CeedFree(&(*ctx)->field_labels[i]->description); CeedChk(ierr);
8353668ca4bSJeremy L Thompson     ierr = CeedFree(&(*ctx)->field_labels[i]); CeedChk(ierr);
836cdf32b93SJeremy L Thompson   }
8373668ca4bSJeremy L Thompson   ierr = CeedFree(&(*ctx)->field_labels); CeedChk(ierr);
838777ff853SJeremy L Thompson   ierr = CeedDestroy(&(*ctx)->ceed); CeedChk(ierr);
839777ff853SJeremy L Thompson   ierr = CeedFree(ctx); CeedChk(ierr);
840cdf32b93SJeremy L Thompson 
841e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
842777ff853SJeremy L Thompson }
843777ff853SJeremy L Thompson 
844777ff853SJeremy L Thompson /// @}
845