xref: /libCEED/interface/ceed-qfunctioncontext.c (revision 3e1e85abf6c24fac2b39e0494a6c97716f813df0)
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 /**
225e6a0ab89SJeremy L Thompson   @brief Get label for a registered QFunctionContext field, or `NULL` if no
226e6a0ab89SJeremy L Thompson            field has been registered with this `field_name`
227e6a0ab89SJeremy L Thompson 
228e6a0ab89SJeremy L Thompson   @param[in] ctx           CeedQFunctionContext
229e6a0ab89SJeremy L Thompson   @param[in] field_name    Name of field to retrieve label
230e6a0ab89SJeremy L Thompson   @param[out] field_label  Variable to field label
231e6a0ab89SJeremy L Thompson 
232e6a0ab89SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
233e6a0ab89SJeremy L Thompson 
234*3e1e85abSJeremy L Thompson   @ref Backend
235e6a0ab89SJeremy L Thompson **/
236e6a0ab89SJeremy L Thompson int CeedQFunctionContextGetFieldLabel(CeedQFunctionContext ctx,
237e6a0ab89SJeremy L Thompson                                       const char *field_name,
238e6a0ab89SJeremy L Thompson                                       CeedContextFieldLabel *field_label) {
239e6a0ab89SJeremy L Thompson   int ierr;
240e6a0ab89SJeremy L Thompson 
241e6a0ab89SJeremy L Thompson   CeedInt field_index;
242e6a0ab89SJeremy L Thompson   ierr = CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index);
243e6a0ab89SJeremy L Thompson   CeedChk(ierr);
244e6a0ab89SJeremy L Thompson 
245e6a0ab89SJeremy L Thompson   if (field_index != -1) {
246e6a0ab89SJeremy L Thompson     *field_label = ctx->field_labels[field_index];
247e6a0ab89SJeremy L Thompson   } else {
248e6a0ab89SJeremy L Thompson     *field_label = NULL;
249e6a0ab89SJeremy L Thompson   }
250e6a0ab89SJeremy L Thompson 
251e6a0ab89SJeremy L Thompson   return CEED_ERROR_SUCCESS;
252e6a0ab89SJeremy L Thompson }
253e6a0ab89SJeremy L Thompson 
254e6a0ab89SJeremy 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 
264*3e1e85abSJeremy L Thompson   @ref Backend
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 /**
291bfacc300SJeremy L Thompson   @brief Set QFunctionContext field holding a double precision value
292bfacc300SJeremy L Thompson 
293bfacc300SJeremy L Thompson   @param ctx         CeedQFunctionContext
294bfacc300SJeremy L Thompson   @param field_label Label for field to register
295bfacc300SJeremy L Thompson   @param values      Values to set
296bfacc300SJeremy L Thompson 
297bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
298bfacc300SJeremy L Thompson 
299*3e1e85abSJeremy L Thompson   @ref Backend
300bfacc300SJeremy L Thompson **/
301bfacc300SJeremy L Thompson int CeedQFunctionContextSetDouble(CeedQFunctionContext ctx,
302bfacc300SJeremy L Thompson                                   CeedContextFieldLabel field_label, double *values) {
303bfacc300SJeremy L Thompson   int ierr;
304bfacc300SJeremy L Thompson 
305bfacc300SJeremy L Thompson   if (!field_label)
306bfacc300SJeremy L Thompson     // LCOV_EXCL_START
307bfacc300SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
308bfacc300SJeremy L Thompson                      "Invalid field label");
309bfacc300SJeremy L Thompson   // LCOV_EXCL_STOP
310bfacc300SJeremy L Thompson 
311bfacc300SJeremy L Thompson   ierr = CeedQFunctionContextSetGeneric(ctx, field_label,
312bfacc300SJeremy L Thompson                                         CEED_CONTEXT_FIELD_DOUBLE,
313bfacc300SJeremy L Thompson                                         values); CeedChk(ierr);
314bfacc300SJeremy L Thompson 
315bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
316bfacc300SJeremy L Thompson }
317bfacc300SJeremy L Thompson 
318bfacc300SJeremy L Thompson /**
319bfacc300SJeremy L Thompson   @brief Set QFunctionContext field holding an int32 value
320bfacc300SJeremy L Thompson 
321bfacc300SJeremy L Thompson   @param ctx         CeedQFunctionContext
322bfacc300SJeremy L Thompson   @param field_label Label for field to register
323bfacc300SJeremy L Thompson   @param values      Values to set
324bfacc300SJeremy L Thompson 
325bfacc300SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
326bfacc300SJeremy L Thompson 
327*3e1e85abSJeremy L Thompson   @ref Backend
328bfacc300SJeremy L Thompson **/
329bfacc300SJeremy L Thompson int CeedQFunctionContextSetInt32(CeedQFunctionContext ctx,
330bfacc300SJeremy L Thompson                                  CeedContextFieldLabel field_label, int *values) {
331bfacc300SJeremy L Thompson   int ierr;
332bfacc300SJeremy L Thompson 
333bfacc300SJeremy L Thompson   if (!field_label)
334bfacc300SJeremy L Thompson     // LCOV_EXCL_START
335bfacc300SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
336bfacc300SJeremy L Thompson                      "Invalid field label");
337bfacc300SJeremy L Thompson   // LCOV_EXCL_STOP
338bfacc300SJeremy L Thompson 
339bfacc300SJeremy L Thompson   ierr = CeedQFunctionContextSetGeneric(ctx, field_label,
340bfacc300SJeremy L Thompson                                         CEED_CONTEXT_FIELD_INT32,
341bfacc300SJeremy L Thompson                                         values); CeedChk(ierr);
342bfacc300SJeremy L Thompson 
343bfacc300SJeremy L Thompson   return CEED_ERROR_SUCCESS;
344bfacc300SJeremy L Thompson }
345bfacc300SJeremy L Thompson 
346bfacc300SJeremy L Thompson /**
34734359f16Sjeremylt   @brief Increment the reference counter for a CeedQFunctionContext
34834359f16Sjeremylt 
34934359f16Sjeremylt   @param ctx  CeedQFunctionContext to increment the reference counter
35034359f16Sjeremylt 
35134359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
35234359f16Sjeremylt 
35334359f16Sjeremylt   @ref Backend
35434359f16Sjeremylt **/
3559560d06aSjeremylt int CeedQFunctionContextReference(CeedQFunctionContext ctx) {
35634359f16Sjeremylt   ctx->ref_count++;
35734359f16Sjeremylt   return CEED_ERROR_SUCCESS;
35834359f16Sjeremylt }
35934359f16Sjeremylt 
360777ff853SJeremy L Thompson /// @}
361777ff853SJeremy L Thompson 
362777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
363777ff853SJeremy L Thompson /// CeedQFunctionContext Public API
364777ff853SJeremy L Thompson /// ----------------------------------------------------------------------------
365777ff853SJeremy L Thompson /// @addtogroup CeedQFunctionUser
366777ff853SJeremy L Thompson /// @{
367777ff853SJeremy L Thompson 
368777ff853SJeremy L Thompson /**
369777ff853SJeremy L Thompson   @brief Create a CeedQFunctionContext for storing CeedQFunction user context data
370777ff853SJeremy L Thompson 
371777ff853SJeremy L Thompson   @param ceed      A Ceed object where the CeedQFunctionContext will be created
372777ff853SJeremy L Thompson   @param[out] ctx  Address of the variable where the newly created
373777ff853SJeremy L Thompson                      CeedQFunctionContext will be stored
374777ff853SJeremy L Thompson 
375777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
376777ff853SJeremy L Thompson 
377777ff853SJeremy L Thompson   @ref User
378777ff853SJeremy L Thompson **/
379777ff853SJeremy L Thompson int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) {
380777ff853SJeremy L Thompson   int ierr;
381777ff853SJeremy L Thompson 
382777ff853SJeremy L Thompson   if (!ceed->QFunctionContextCreate) {
383777ff853SJeremy L Thompson     Ceed delegate;
384777ff853SJeremy L Thompson     ierr = CeedGetObjectDelegate(ceed, &delegate, "Context"); CeedChk(ierr);
385777ff853SJeremy L Thompson 
386777ff853SJeremy L Thompson     if (!delegate)
387777ff853SJeremy L Thompson       // LCOV_EXCL_START
388e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
389e15f9bd0SJeremy L Thompson                        "Backend does not support ContextCreate");
390777ff853SJeremy L Thompson     // LCOV_EXCL_STOP
391777ff853SJeremy L Thompson 
392777ff853SJeremy L Thompson     ierr = CeedQFunctionContextCreate(delegate, ctx); CeedChk(ierr);
393e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
394777ff853SJeremy L Thompson   }
395777ff853SJeremy L Thompson 
396777ff853SJeremy L Thompson   ierr = CeedCalloc(1, ctx); CeedChk(ierr);
397777ff853SJeremy L Thompson   (*ctx)->ceed = ceed;
3989560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
399d1d35e2fSjeremylt   (*ctx)->ref_count = 1;
400777ff853SJeremy L Thompson   ierr = ceed->QFunctionContextCreate(*ctx); CeedChk(ierr);
401e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
402777ff853SJeremy L Thompson }
403777ff853SJeremy L Thompson 
404777ff853SJeremy L Thompson /**
4059560d06aSjeremylt   @brief Copy the pointer to a CeedQFunctionContext. Both pointers should
4069560d06aSjeremylt            be destroyed with `CeedQFunctionContextDestroy()`;
4079560d06aSjeremylt            Note: If `*ctx_copy` is non-NULL, then it is assumed that
4089560d06aSjeremylt            `*ctx_copy` is a pointer to a CeedQFunctionContext. This
4099560d06aSjeremylt            CeedQFunctionContext will be destroyed if `*ctx_copy` is the
4109560d06aSjeremylt            only reference to this CeedQFunctionContext.
4119560d06aSjeremylt 
4129560d06aSjeremylt   @param ctx            CeedQFunctionContext to copy reference to
4139560d06aSjeremylt   @param[out] ctx_copy  Variable to store copied reference
4149560d06aSjeremylt 
4159560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
4169560d06aSjeremylt 
4179560d06aSjeremylt   @ref User
4189560d06aSjeremylt **/
4199560d06aSjeremylt int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx,
4209560d06aSjeremylt                                       CeedQFunctionContext *ctx_copy) {
4219560d06aSjeremylt   int ierr;
4229560d06aSjeremylt 
4239560d06aSjeremylt   ierr = CeedQFunctionContextReference(ctx); CeedChk(ierr);
4249560d06aSjeremylt   ierr = CeedQFunctionContextDestroy(ctx_copy); CeedChk(ierr);
4259560d06aSjeremylt   *ctx_copy = ctx;
4269560d06aSjeremylt   return CEED_ERROR_SUCCESS;
4279560d06aSjeremylt }
4289560d06aSjeremylt 
4299560d06aSjeremylt /**
430777ff853SJeremy L Thompson   @brief Set the data used by a CeedQFunctionContext, freeing any previously allocated
431777ff853SJeremy L Thompson            data if applicable. The backend may copy values to a different
432777ff853SJeremy L Thompson            memtype, such as during @ref CeedQFunctionApply().
433777ff853SJeremy L Thompson            See also @ref CeedQFunctionContextTakeData().
434777ff853SJeremy L Thompson 
435777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext
436d1d35e2fSjeremylt   @param mem_type   Memory type of the data being passed
437d1d35e2fSjeremylt   @param copy_mode  Copy mode for the data
438891038deSjeremylt   @param size       Size of data, in bytes
439777ff853SJeremy L Thompson   @param data       Data to be used
440777ff853SJeremy L Thompson 
441777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
442777ff853SJeremy L Thompson 
443777ff853SJeremy L Thompson   @ref User
444777ff853SJeremy L Thompson **/
445d1d35e2fSjeremylt int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type,
446d1d35e2fSjeremylt                                 CeedCopyMode copy_mode,
447777ff853SJeremy L Thompson                                 size_t size, void *data) {
448777ff853SJeremy L Thompson   int ierr;
449777ff853SJeremy L Thompson 
450777ff853SJeremy L Thompson   if (!ctx->SetData)
451777ff853SJeremy L Thompson     // LCOV_EXCL_START
452e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
453e15f9bd0SJeremy L Thompson                      "Backend does not support ContextSetData");
454777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
455777ff853SJeremy L Thompson 
456777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
457777ff853SJeremy L Thompson     // LCOV_EXCL_START
458777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
459777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
460777ff853SJeremy L Thompson                      "access lock is already in use");
461777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
462777ff853SJeremy L Thompson 
463d1d35e2fSjeremylt   ctx->ctx_size = size;
464d1d35e2fSjeremylt   ierr = ctx->SetData(ctx, mem_type, copy_mode, data); CeedChk(ierr);
465777ff853SJeremy L Thompson   ctx->state += 2;
466e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
467777ff853SJeremy L Thompson }
468777ff853SJeremy L Thompson 
469777ff853SJeremy L Thompson /**
470891038deSjeremylt   @brief Take ownership of the data in a CeedQFunctionContext via the specified memory type.
471891038deSjeremylt            The caller is responsible for managing and freeing the memory.
472891038deSjeremylt 
473891038deSjeremylt   @param ctx        CeedQFunctionContext to access
474891038deSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
475891038deSjeremylt                       uses a different memory type, this will perform a copy.
476891038deSjeremylt   @param[out] data  Data on memory type mem_type
477891038deSjeremylt 
478891038deSjeremylt   @return An error code: 0 - success, otherwise - failure
479891038deSjeremylt 
480891038deSjeremylt   @ref User
481891038deSjeremylt **/
482891038deSjeremylt int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type,
483891038deSjeremylt                                  void *data) {
484891038deSjeremylt   int ierr;
485891038deSjeremylt 
4869c774eddSJeremy L Thompson   bool has_valid_data = true;
4879c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
4889c774eddSJeremy L Thompson   if (!has_valid_data)
4899c774eddSJeremy L Thompson     // LCOV_EXCL_START
4909c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
4919c774eddSJeremy L Thompson                      "CeedQFunctionContext has no valid data to take, must set data");
4929c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
4939c774eddSJeremy L Thompson 
494891038deSjeremylt   if (!ctx->TakeData)
495891038deSjeremylt     // LCOV_EXCL_START
496891038deSjeremylt     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
497891038deSjeremylt                      "Backend does not support TakeData");
498891038deSjeremylt   // LCOV_EXCL_STOP
499891038deSjeremylt 
500891038deSjeremylt   if (ctx->state % 2 == 1)
501891038deSjeremylt     // LCOV_EXCL_START
502891038deSjeremylt     return CeedError(ctx->ceed, 1,
503891038deSjeremylt                      "Cannot grant CeedQFunctionContext data access, the "
504891038deSjeremylt                      "access lock is already in use");
505891038deSjeremylt   // LCOV_EXCL_STOP
506891038deSjeremylt 
5079c774eddSJeremy L Thompson   bool has_borrowed_data_of_type = true;
5089c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type,
5099c774eddSJeremy L Thompson          &has_borrowed_data_of_type); CeedChk(ierr);
5109c774eddSJeremy L Thompson   if (!has_borrowed_data_of_type)
5119c774eddSJeremy L Thompson     // LCOV_EXCL_START
5129c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
5139c774eddSJeremy L Thompson                      "CeedQFunctionContext has no borowed %s data, "
5149c774eddSJeremy L Thompson                      "must set data with CeedQFunctionContextSetData",
5159c774eddSJeremy L Thompson                      CeedMemTypes[mem_type]);
5169c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5179c774eddSJeremy L Thompson 
518891038deSjeremylt   void *temp_data = NULL;
519891038deSjeremylt   ierr = ctx->TakeData(ctx, mem_type, &temp_data); CeedChk(ierr);
520891038deSjeremylt   if (data) (*(void **)data) = temp_data;
521891038deSjeremylt   return CEED_ERROR_SUCCESS;
522891038deSjeremylt }
523891038deSjeremylt 
524891038deSjeremylt /**
525777ff853SJeremy L Thompson   @brief Get read/write access to a CeedQFunctionContext via the specified memory type.
526777ff853SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
527777ff853SJeremy L Thompson 
528777ff853SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
529d1d35e2fSjeremylt   @param mem_type   Memory type on which to access the data. If the backend
530777ff853SJeremy L Thompson                       uses a different memory type, this will perform a copy.
531d1d35e2fSjeremylt   @param[out] data  Data on memory type mem_type
532777ff853SJeremy L Thompson 
533777ff853SJeremy L Thompson   @note The CeedQFunctionContextGetData() and @ref CeedQFunctionContextRestoreData() functions
534777ff853SJeremy L Thompson     provide access to array pointers in the desired memory space. Pairing
535777ff853SJeremy L Thompson     get/restore allows the Context to track access.
536777ff853SJeremy L Thompson 
537777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
538777ff853SJeremy L Thompson 
539777ff853SJeremy L Thompson   @ref User
540777ff853SJeremy L Thompson **/
541d1d35e2fSjeremylt int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type,
542777ff853SJeremy L Thompson                                 void *data) {
543777ff853SJeremy L Thompson   int ierr;
544777ff853SJeremy L Thompson 
545777ff853SJeremy L Thompson   if (!ctx->GetData)
546777ff853SJeremy L Thompson     // LCOV_EXCL_START
547e15f9bd0SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
548e15f9bd0SJeremy L Thompson                      "Backend does not support GetData");
549777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
550777ff853SJeremy L Thompson 
551777ff853SJeremy L Thompson   if (ctx->state % 2 == 1)
552777ff853SJeremy L Thompson     // LCOV_EXCL_START
553777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
554777ff853SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
555777ff853SJeremy L Thompson                      "access lock is already in use");
556777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
557777ff853SJeremy L Thompson 
55828bfd0b7SJeremy L Thompson   if (ctx->num_readers > 0)
55928bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
56028bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
56128bfd0b7SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, a "
56228bfd0b7SJeremy L Thompson                      "process has read access");
56328bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
56428bfd0b7SJeremy L Thompson 
5659c774eddSJeremy L Thompson   bool has_valid_data = true;
5669c774eddSJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
5679c774eddSJeremy L Thompson   if (!has_valid_data)
5689c774eddSJeremy L Thompson     // LCOV_EXCL_START
5699c774eddSJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
5709c774eddSJeremy L Thompson                      "CeedQFunctionContext has no valid data to get, must set data");
5719c774eddSJeremy L Thompson   // LCOV_EXCL_STOP
5729c774eddSJeremy L Thompson 
573d1d35e2fSjeremylt   ierr = ctx->GetData(ctx, mem_type, data); CeedChk(ierr);
57428bfd0b7SJeremy L Thompson   ctx->state++;
57528bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
57628bfd0b7SJeremy L Thompson }
57728bfd0b7SJeremy L Thompson 
57828bfd0b7SJeremy L Thompson /**
57928bfd0b7SJeremy L Thompson   @brief Get read only access to a CeedQFunctionContext via the specified memory type.
58028bfd0b7SJeremy L Thompson            Restore access with @ref CeedQFunctionContextRestoreData().
58128bfd0b7SJeremy L Thompson 
58228bfd0b7SJeremy L Thompson   @param ctx        CeedQFunctionContext to access
58328bfd0b7SJeremy L Thompson   @param mem_type   Memory type on which to access the data. If the backend
58428bfd0b7SJeremy L Thompson                       uses a different memory type, this will perform a copy.
58528bfd0b7SJeremy L Thompson   @param[out] data  Data on memory type mem_type
58628bfd0b7SJeremy L Thompson 
58728bfd0b7SJeremy L Thompson   @note The CeedQFunctionContextGetDataRead() and @ref CeedQFunctionContextRestoreDataRead()
58828bfd0b7SJeremy L Thompson     functions provide access to array pointers in the desired memory space. Pairing
58928bfd0b7SJeremy L Thompson     get/restore allows the Context to track access.
59028bfd0b7SJeremy L Thompson 
59128bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
59228bfd0b7SJeremy L Thompson 
59328bfd0b7SJeremy L Thompson   @ref User
59428bfd0b7SJeremy L Thompson **/
59528bfd0b7SJeremy L Thompson int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx,
59628bfd0b7SJeremy L Thompson                                     CeedMemType mem_type,
59728bfd0b7SJeremy L Thompson                                     void *data) {
59828bfd0b7SJeremy L Thompson   int ierr;
59928bfd0b7SJeremy L Thompson 
60028bfd0b7SJeremy L Thompson   if (!ctx->GetDataRead)
60128bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
60228bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_UNSUPPORTED,
60328bfd0b7SJeremy L Thompson                      "Backend does not support GetDataRead");
60428bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
60528bfd0b7SJeremy L Thompson 
60628bfd0b7SJeremy L Thompson   if (ctx->state % 2 == 1)
60728bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
60828bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
60928bfd0b7SJeremy L Thompson                      "Cannot grant CeedQFunctionContext data access, the "
61028bfd0b7SJeremy L Thompson                      "access lock is already in use");
61128bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
61228bfd0b7SJeremy L Thompson 
61328bfd0b7SJeremy L Thompson   bool has_valid_data = true;
61428bfd0b7SJeremy L Thompson   ierr = CeedQFunctionContextHasValidData(ctx, &has_valid_data); CeedChk(ierr);
61528bfd0b7SJeremy L Thompson   if (!has_valid_data)
61628bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
61728bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, CEED_ERROR_BACKEND,
61828bfd0b7SJeremy L Thompson                      "CeedQFunctionContext has no valid data to get, must set data");
61928bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
62028bfd0b7SJeremy L Thompson 
62128bfd0b7SJeremy L Thompson   ierr = ctx->GetDataRead(ctx, mem_type, data); CeedChk(ierr);
62228bfd0b7SJeremy L Thompson   ctx->num_readers++;
623e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
624777ff853SJeremy L Thompson }
625777ff853SJeremy L Thompson 
626777ff853SJeremy L Thompson /**
627777ff853SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetData()
628777ff853SJeremy L Thompson 
629777ff853SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
630777ff853SJeremy L Thompson   @param data  Data to restore
631777ff853SJeremy L Thompson 
632777ff853SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
633777ff853SJeremy L Thompson 
634777ff853SJeremy L Thompson   @ref User
635777ff853SJeremy L Thompson **/
636777ff853SJeremy L Thompson int CeedQFunctionContextRestoreData(CeedQFunctionContext ctx, void *data) {
637777ff853SJeremy L Thompson   int ierr;
638777ff853SJeremy L Thompson 
639777ff853SJeremy L Thompson   if (ctx->state % 2 != 1)
640777ff853SJeremy L Thompson     // LCOV_EXCL_START
641777ff853SJeremy L Thompson     return CeedError(ctx->ceed, 1,
642777ff853SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
643777ff853SJeremy L Thompson                      "access was not granted");
644777ff853SJeremy L Thompson   // LCOV_EXCL_STOP
645777ff853SJeremy L Thompson 
646706efda3SJeremy L Thompson   if (ctx->RestoreData) {
647777ff853SJeremy L Thompson     ierr = ctx->RestoreData(ctx); CeedChk(ierr);
648706efda3SJeremy L Thompson   }
649777ff853SJeremy L Thompson   *(void **)data = NULL;
65028bfd0b7SJeremy L Thompson   ctx->state++;
65128bfd0b7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
65228bfd0b7SJeremy L Thompson }
65328bfd0b7SJeremy L Thompson 
65428bfd0b7SJeremy L Thompson /**
65528bfd0b7SJeremy L Thompson   @brief Restore data obtained using @ref CeedQFunctionContextGetDataRead()
65628bfd0b7SJeremy L Thompson 
65728bfd0b7SJeremy L Thompson   @param ctx   CeedQFunctionContext to restore
65828bfd0b7SJeremy L Thompson   @param data  Data to restore
65928bfd0b7SJeremy L Thompson 
66028bfd0b7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
66128bfd0b7SJeremy L Thompson 
66228bfd0b7SJeremy L Thompson   @ref User
66328bfd0b7SJeremy L Thompson **/
66428bfd0b7SJeremy L Thompson int CeedQFunctionContextRestoreDataRead(CeedQFunctionContext ctx, void *data) {
66528bfd0b7SJeremy L Thompson   int ierr;
66628bfd0b7SJeremy L Thompson 
66728bfd0b7SJeremy L Thompson   if (ctx->num_readers == 0)
66828bfd0b7SJeremy L Thompson     // LCOV_EXCL_START
66928bfd0b7SJeremy L Thompson     return CeedError(ctx->ceed, 1,
67028bfd0b7SJeremy L Thompson                      "Cannot restore CeedQFunctionContext array access, "
67128bfd0b7SJeremy L Thompson                      "access was not granted");
67228bfd0b7SJeremy L Thompson   // LCOV_EXCL_STOP
67328bfd0b7SJeremy L Thompson 
67428bfd0b7SJeremy L Thompson   if (ctx->RestoreDataRead) {
67528bfd0b7SJeremy L Thompson     ierr = ctx->RestoreData(ctx); CeedChk(ierr);
67628bfd0b7SJeremy L Thompson   }
67728bfd0b7SJeremy L Thompson   *(void **)data = NULL;
67828bfd0b7SJeremy L Thompson   ctx->num_readers--;
679e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
680777ff853SJeremy L Thompson }
681777ff853SJeremy L Thompson 
682777ff853SJeremy L Thompson /**
683cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a double precision value
684cdf32b93SJeremy L Thompson 
685cdf32b93SJeremy L Thompson   @param ctx               CeedQFunctionContext
686cdf32b93SJeremy L Thompson   @param field_name        Name of field to register
687cdf32b93SJeremy L Thompson   @param field_offset      Offset of field to register
6887bfe0f0eSJeremy L Thompson   @param num_values        Number of values to register, must be contiguous in memory
689cdf32b93SJeremy L Thompson   @param field_description Description of field, or NULL for none
690cdf32b93SJeremy L Thompson 
691cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
692cdf32b93SJeremy L Thompson 
693cdf32b93SJeremy L Thompson   @ref User
694cdf32b93SJeremy L Thompson **/
695cdf32b93SJeremy L Thompson int CeedQFunctionContextRegisterDouble(CeedQFunctionContext ctx,
696cdf32b93SJeremy L Thompson                                        const char *field_name, size_t field_offset,
6977bfe0f0eSJeremy L Thompson                                        size_t num_values,
698cdf32b93SJeremy L Thompson                                        const char *field_description) {
699cdf32b93SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset,
7007bfe0f0eSJeremy L Thompson          field_description, CEED_CONTEXT_FIELD_DOUBLE, sizeof(double), num_values);
701cdf32b93SJeremy L Thompson }
702cdf32b93SJeremy L Thompson 
703cdf32b93SJeremy L Thompson /**
704cdf32b93SJeremy L Thompson   @brief Register QFunctionContext a field holding a int32 value
705cdf32b93SJeremy L Thompson 
706cdf32b93SJeremy L Thompson   @param ctx               CeedQFunctionContext
707cdf32b93SJeremy L Thompson   @param field_name        Name of field to register
708cdf32b93SJeremy L Thompson   @param field_offset      Offset of field to register
7097bfe0f0eSJeremy L Thompson   @param num_values        Number of values to register, must be contiguous in memory
710cdf32b93SJeremy L Thompson   @param field_description Description of field, or NULL for none
711cdf32b93SJeremy L Thompson 
712cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
713cdf32b93SJeremy L Thompson 
714cdf32b93SJeremy L Thompson   @ref User
715cdf32b93SJeremy L Thompson **/
716cdf32b93SJeremy L Thompson int CeedQFunctionContextRegisterInt32(CeedQFunctionContext ctx,
717cdf32b93SJeremy L Thompson                                       const char *field_name, size_t field_offset,
7187bfe0f0eSJeremy L Thompson                                       size_t num_values,
719cdf32b93SJeremy L Thompson                                       const char *field_description) {
720cdf32b93SJeremy L Thompson   return CeedQFunctionContextRegisterGeneric(ctx, field_name, field_offset,
7217bfe0f0eSJeremy L Thompson          field_description, CEED_CONTEXT_FIELD_INT32, sizeof(int), num_values);
722cdf32b93SJeremy L Thompson }
723cdf32b93SJeremy L Thompson 
724cdf32b93SJeremy L Thompson /**
7253668ca4bSJeremy L Thompson   @brief Get labels for all registered QFunctionContext fields
726cdf32b93SJeremy L Thompson 
727cdf32b93SJeremy L Thompson   @param ctx                CeedQFunctionContext
7283668ca4bSJeremy L Thompson   @param[out] field_labels  Variable to hold array of field labels
729cdf32b93SJeremy L Thompson   @param[out] num_fields    Length of field descriptions array
730cdf32b93SJeremy L Thompson 
731cdf32b93SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
732cdf32b93SJeremy L Thompson 
733cdf32b93SJeremy L Thompson   @ref User
734cdf32b93SJeremy L Thompson **/
7353668ca4bSJeremy L Thompson int CeedQFunctionContextGetAllFieldLabels(CeedQFunctionContext ctx,
7363668ca4bSJeremy L Thompson     const CeedContextFieldLabel **field_labels, CeedInt *num_fields) {
7373668ca4bSJeremy L Thompson   *field_labels = ctx->field_labels;
738cdf32b93SJeremy L Thompson   *num_fields = ctx->num_fields;
739cdf32b93SJeremy L Thompson   return CEED_ERROR_SUCCESS;
740cdf32b93SJeremy L Thompson }
741cdf32b93SJeremy L Thompson 
742cdf32b93SJeremy L Thompson /**
7430f86cbe7SJeremy L Thompson   @brief Get the descriptive information about a CeedContextFieldLabel
7440f86cbe7SJeremy L Thompson 
7450f86cbe7SJeremy L Thompson   @param[in] label              CeedContextFieldLabel
7460f86cbe7SJeremy L Thompson   @param[out] field_name        Name of labeled field
7470f86cbe7SJeremy L Thompson   @param[out] field_description Description of field, or NULL for none
7487bfe0f0eSJeremy L Thompson   @param[out] num_values        Number of values registered
7490f86cbe7SJeremy L Thompson   @param[out] field_type        CeedContextFieldType
7500f86cbe7SJeremy L Thompson 
7510f86cbe7SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7520f86cbe7SJeremy L Thompson 
7530f86cbe7SJeremy L Thompson   @ref User
7540f86cbe7SJeremy L Thompson **/
7550f86cbe7SJeremy L Thompson int CeedContextFieldLabelGetDescription(CeedContextFieldLabel label,
7560f86cbe7SJeremy L Thompson                                         const char **field_name,
7570f86cbe7SJeremy L Thompson                                         const char **field_description,
7587bfe0f0eSJeremy L Thompson                                         size_t *num_values,
7590f86cbe7SJeremy L Thompson                                         CeedContextFieldType *field_type) {
7600f86cbe7SJeremy L Thompson   if (field_name) *field_name = label->name;
7610f86cbe7SJeremy L Thompson   if (field_description) *field_description = label->description;
7627bfe0f0eSJeremy L Thompson   if (num_values) *num_values = label->num_values;
7630f86cbe7SJeremy L Thompson   if (field_type) *field_type = label->type;
7640f86cbe7SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7650f86cbe7SJeremy L Thompson }
7660f86cbe7SJeremy L Thompson 
7670f86cbe7SJeremy 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