xref: /libCEED/interface/ceed-elemrestriction.c (revision 4c789ea28eed8450eb29ac731b0b6c80a3f128bb)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d7b241e6Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5d7b241e6Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7d7b241e6Sjeremylt 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stdio.h>
13c17ec2beSJeremy L Thompson #include <string.h>
14d7b241e6Sjeremylt 
157a982d89SJeremy L. Thompson /// @file
167a982d89SJeremy L. Thompson /// Implementation of CeedElemRestriction interfaces
177a982d89SJeremy L. Thompson 
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// CeedElemRestriction Library Internal Functions
207a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
217a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionDeveloper
227a982d89SJeremy L. Thompson /// @{
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /**
25ca94c3ddSJeremy L Thompson   @brief Permute and pad offsets for a blocked `CeedElemRestriction`
267a982d89SJeremy L. Thompson 
27ca94c3ddSJeremy L Thompson   @param[in]  offsets       Array of shape `[num_elem, elem_size]`
28ca94c3ddSJeremy L Thompson   @param[out] block_offsets Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
29e7f679fcSJeremy L Thompson   @param[in]  num_block     Number of blocks
30ea61e9acSJeremy L Thompson   @param[in]  num_elem      Number of elements
31e7f679fcSJeremy L Thompson   @param[in]  block_size    Number of elements in a block
32ea61e9acSJeremy L Thompson   @param[in]  elem_size     Size of each element
337a982d89SJeremy L. Thompson 
347a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
357a982d89SJeremy L. Thompson 
367a982d89SJeremy L. Thompson   @ref Utility
377a982d89SJeremy L. Thompson **/
38e7f679fcSJeremy L Thompson int CeedPermutePadOffsets(const CeedInt *offsets, CeedInt *block_offsets, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
39e7f679fcSJeremy L Thompson                           CeedInt elem_size) {
40e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
41e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
422b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < elem_size; k++) {
43e7f679fcSJeremy L Thompson         block_offsets[e * elem_size + k * block_size + j] = offsets[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
442b730f8bSJeremy L Thompson       }
452b730f8bSJeremy L Thompson     }
462b730f8bSJeremy L Thompson   }
47e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
487a982d89SJeremy L. Thompson }
497a982d89SJeremy L. Thompson 
5077d1c127SSebastian Grimberg /**
51ca94c3ddSJeremy L Thompson   @brief Permute and pad orientations for a blocked `CeedElemRestriction`
5277d1c127SSebastian Grimberg 
53ca94c3ddSJeremy L Thompson   @param[in]  orients       Array of shape `[num_elem, elem_size]`
54ca94c3ddSJeremy L Thompson   @param[out] block_orients Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
55e7f679fcSJeremy L Thompson   @param[in]  num_block     Number of blocks
5677d1c127SSebastian Grimberg   @param[in]  num_elem      Number of elements
57e7f679fcSJeremy L Thompson   @param[in]  block_size    Number of elements in a block
5877d1c127SSebastian Grimberg   @param[in]  elem_size     Size of each element
5977d1c127SSebastian Grimberg 
6077d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
6177d1c127SSebastian Grimberg 
6277d1c127SSebastian Grimberg   @ref Utility
6377d1c127SSebastian Grimberg **/
64e7f679fcSJeremy L Thompson int CeedPermutePadOrients(const bool *orients, bool *block_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size, CeedInt elem_size) {
65e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
66e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
6777d1c127SSebastian Grimberg       for (CeedInt k = 0; k < elem_size; k++) {
68e7f679fcSJeremy L Thompson         block_orients[e * elem_size + k * block_size + j] = orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
6977d1c127SSebastian Grimberg       }
7077d1c127SSebastian Grimberg     }
7177d1c127SSebastian Grimberg   }
7277d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
7377d1c127SSebastian Grimberg }
7477d1c127SSebastian Grimberg 
750c73c039SSebastian Grimberg /**
76ca94c3ddSJeremy L Thompson   @brief Permute and pad curl-conforming orientations for a blocked `CeedElemRestriction`
770c73c039SSebastian Grimberg 
785c7e0f51SSebastian Grimberg   @param[in]  curl_orients       Array of shape `[num_elem, elem_size]`
79ca94c3ddSJeremy L Thompson   @param[out] block_curl_orients Array of permuted and padded array values of shape `[num_block, elem_size, block_size]`
80e7f679fcSJeremy L Thompson   @param[in]  num_block          Number of blocks
810c73c039SSebastian Grimberg   @param[in]  num_elem           Number of elements
82e7f679fcSJeremy L Thompson   @param[in]  block_size         Number of elements in a block
830c73c039SSebastian Grimberg   @param[in]  elem_size          Size of each element
840c73c039SSebastian Grimberg 
850c73c039SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
860c73c039SSebastian Grimberg 
870c73c039SSebastian Grimberg   @ref Utility
880c73c039SSebastian Grimberg **/
89e7f679fcSJeremy L Thompson int CeedPermutePadCurlOrients(const CeedInt8 *curl_orients, CeedInt8 *block_curl_orients, CeedInt num_block, CeedInt num_elem, CeedInt block_size,
900c73c039SSebastian Grimberg                               CeedInt elem_size) {
91e7f679fcSJeremy L Thompson   for (CeedInt e = 0; e < num_block * block_size; e += block_size) {
92e7f679fcSJeremy L Thompson     for (CeedInt j = 0; j < block_size; j++) {
930c73c039SSebastian Grimberg       for (CeedInt k = 0; k < elem_size; k++) {
94e7f679fcSJeremy L Thompson         block_curl_orients[e * elem_size + k * block_size + j] = curl_orients[CeedIntMin(e + j, num_elem - 1) * elem_size + k];
950c73c039SSebastian Grimberg       }
960c73c039SSebastian Grimberg     }
970c73c039SSebastian Grimberg   }
980c73c039SSebastian Grimberg   return CEED_ERROR_SUCCESS;
990c73c039SSebastian Grimberg }
1000c73c039SSebastian Grimberg 
1017a982d89SJeremy L. Thompson /// @}
1027a982d89SJeremy L. Thompson 
1037a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1047a982d89SJeremy L. Thompson /// CeedElemRestriction Backend API
1057a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
1067a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionBackend
1077a982d89SJeremy L. Thompson /// @{
1087a982d89SJeremy L. Thompson 
1097a982d89SJeremy L. Thompson /**
110ca94c3ddSJeremy L Thompson   @brief Get the type of a `CeedElemRestriction`
111a681ae63Sjeremylt 
112ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
113fcbe8c06SSebastian Grimberg   @param[out] rstr_type Variable to store restriction type
114fcbe8c06SSebastian Grimberg 
115fcbe8c06SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
116fcbe8c06SSebastian Grimberg 
117fcbe8c06SSebastian Grimberg   @ref Backend
118fcbe8c06SSebastian Grimberg **/
119fcbe8c06SSebastian Grimberg int CeedElemRestrictionGetType(CeedElemRestriction rstr, CeedRestrictionType *rstr_type) {
120fcbe8c06SSebastian Grimberg   *rstr_type = rstr->rstr_type;
121fcbe8c06SSebastian Grimberg   return CEED_ERROR_SUCCESS;
122fcbe8c06SSebastian Grimberg }
123fcbe8c06SSebastian Grimberg 
124fcbe8c06SSebastian Grimberg /**
125*4c789ea2SJeremy L Thompson   @brief Get the number of tabs to indent for @ref CeedElemRestrictionView() output
126*4c789ea2SJeremy L Thompson 
127*4c789ea2SJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction` to get the number of view tabs
128*4c789ea2SJeremy L Thompson   @param[out] num_tabs Number of view tabs
129*4c789ea2SJeremy L Thompson 
130*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
131*4c789ea2SJeremy L Thompson 
132*4c789ea2SJeremy L Thompson   @ref Backend
133*4c789ea2SJeremy L Thompson **/
134*4c789ea2SJeremy L Thompson int CeedElemRestrictionGetNumViewTabs(CeedElemRestriction rstr, CeedInt *num_tabs) {
135*4c789ea2SJeremy L Thompson   *num_tabs = rstr->num_tabs;
136*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
137*4c789ea2SJeremy L Thompson }
138*4c789ea2SJeremy L Thompson 
139*4c789ea2SJeremy L Thompson /**
140ca94c3ddSJeremy L Thompson   @brief Get the strided status of a `CeedElemRestriction`
141fcbe8c06SSebastian Grimberg 
142ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
143ca94c3ddSJeremy L Thompson   @param[out] is_strided Variable to store strided status
144ca94c3ddSJeremy L Thompson 
145ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
146ca94c3ddSJeremy L Thompson 
147ca94c3ddSJeremy L Thompson   @ref Backend
148fcbe8c06SSebastian Grimberg **/
149fcbe8c06SSebastian Grimberg int CeedElemRestrictionIsStrided(CeedElemRestriction rstr, bool *is_strided) {
150fcbe8c06SSebastian Grimberg   *is_strided = (rstr->rstr_type == CEED_RESTRICTION_STRIDED);
151fcbe8c06SSebastian Grimberg   return CEED_ERROR_SUCCESS;
152fcbe8c06SSebastian Grimberg }
153fcbe8c06SSebastian Grimberg 
154fcbe8c06SSebastian Grimberg /**
155ca94c3ddSJeremy L Thompson   @brief Get the points status of a `CeedElemRestriction`
1563ac8f562SJeremy L Thompson 
157ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
158ca94c3ddSJeremy L Thompson   @param[out] is_points Variable to store points status
159ca94c3ddSJeremy L Thompson 
160ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
161ca94c3ddSJeremy L Thompson 
162ca94c3ddSJeremy L Thompson   @ref Backend
1633ac8f562SJeremy L Thompson **/
164637baffdSJeremy L Thompson int CeedElemRestrictionIsAtPoints(CeedElemRestriction rstr, bool *is_points) {
1653ac8f562SJeremy L Thompson   *is_points = (rstr->rstr_type == CEED_RESTRICTION_POINTS);
1663ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1673ac8f562SJeremy L Thompson }
1683ac8f562SJeremy L Thompson 
1693ac8f562SJeremy L Thompson /**
170ca94c3ddSJeremy L Thompson   @brief Check if two `CeedElemRestriction` created with @ref CeedElemRestrictionCreateAtPoints() and use the same points per element
17148acf710SJeremy L Thompson 
172ca94c3ddSJeremy L Thompson   @param[in]  rstr_a         First `CeedElemRestriction`
173ca94c3ddSJeremy L Thompson   @param[in]  rstr_b         Second `CeedElemRestriction`
17448acf710SJeremy L Thompson   @param[out] are_compatible Variable to store compatibility status
175ca94c3ddSJeremy L Thompson 
176ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
177ca94c3ddSJeremy L Thompson 
178ca94c3ddSJeremy L Thompson   @ref Backend
17948acf710SJeremy L Thompson **/
18048acf710SJeremy L Thompson int CeedElemRestrictionAtPointsAreCompatible(CeedElemRestriction rstr_a, CeedElemRestriction rstr_b, bool *are_compatible) {
18148acf710SJeremy L Thompson   CeedInt num_elem_a, num_elem_b, num_points_a, num_points_b;
18248acf710SJeremy L Thompson 
18348acf710SJeremy L Thompson   // Cannot compare non-points restrictions
1849bc66399SJeremy L Thompson   CeedCheck(rstr_a->rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr_a), CEED_ERROR_UNSUPPORTED,
1859bc66399SJeremy L Thompson             "First CeedElemRestriction must be AtPoints");
1869bc66399SJeremy L Thompson   CeedCheck(rstr_b->rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr_a), CEED_ERROR_UNSUPPORTED,
1879bc66399SJeremy L Thompson             "Second CeedElemRestriction must be AtPoints");
18848acf710SJeremy L Thompson 
18948acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_a, &num_elem_a));
19048acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_b, &num_elem_b));
19148acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumPoints(rstr_a, &num_points_a));
19248acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumPoints(rstr_b, &num_points_b));
19348acf710SJeremy L Thompson 
19448acf710SJeremy L Thompson   // Check size and contents of offsets arrays
19548acf710SJeremy L Thompson   *are_compatible = true;
19648acf710SJeremy L Thompson   if (num_elem_a != num_elem_b) *are_compatible = false;
19748acf710SJeremy L Thompson   if (num_points_a != num_points_b) *are_compatible = false;
19848acf710SJeremy L Thompson   if (*are_compatible) {
19948acf710SJeremy L Thompson     const CeedInt *offsets_a, *offsets_b;
20048acf710SJeremy L Thompson 
20148acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionGetOffsets(rstr_a, CEED_MEM_HOST, &offsets_a));
20248acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionGetOffsets(rstr_b, CEED_MEM_HOST, &offsets_b));
20348acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_elem_a + 1 + num_points_a; i++) *are_compatible &= offsets_a[i] == offsets_b[i];
20448acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_a, &offsets_a));
20548acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_b, &offsets_b));
20648acf710SJeremy L Thompson   }
20748acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20848acf710SJeremy L Thompson }
20948acf710SJeremy L Thompson 
21048acf710SJeremy L Thompson /**
211ca94c3ddSJeremy L Thompson   @brief Get the strides of a strided `CeedElemRestriction`
2127a982d89SJeremy L. Thompson 
213ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
214a681ae63Sjeremylt   @param[out] strides Variable to store strides array
2157a982d89SJeremy L. Thompson 
2167a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2177a982d89SJeremy L. Thompson 
2187a982d89SJeremy L. Thompson   @ref Backend
2197a982d89SJeremy L. Thompson **/
22056c48462SJeremy L Thompson int CeedElemRestrictionGetStrides(CeedElemRestriction rstr, CeedInt strides[3]) {
2216e536b99SJeremy L Thompson   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
22256c48462SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) strides[i] = rstr->strides[i];
223e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2247a982d89SJeremy L. Thompson }
2257a982d89SJeremy L. Thompson 
2267a982d89SJeremy L. Thompson /**
227ca94c3ddSJeremy L Thompson   @brief Get the backend stride status of a `CeedElemRestriction`
22877d1c127SSebastian Grimberg 
229ca94c3ddSJeremy L Thompson   @param[in]  rstr                 `CeedElemRestriction`
23077d1c127SSebastian Grimberg   @param[out] has_backend_strides  Variable to store stride status
23177d1c127SSebastian Grimberg 
23277d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
23377d1c127SSebastian Grimberg 
23477d1c127SSebastian Grimberg   @ref Backend
23577d1c127SSebastian Grimberg **/
23677d1c127SSebastian Grimberg int CeedElemRestrictionHasBackendStrides(CeedElemRestriction rstr, bool *has_backend_strides) {
2376e536b99SJeremy L Thompson   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
23877d1c127SSebastian Grimberg   *has_backend_strides = ((rstr->strides[0] == CEED_STRIDES_BACKEND[0]) && (rstr->strides[1] == CEED_STRIDES_BACKEND[1]) &&
23977d1c127SSebastian Grimberg                           (rstr->strides[2] == CEED_STRIDES_BACKEND[2]));
24077d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
24177d1c127SSebastian Grimberg }
24277d1c127SSebastian Grimberg 
24377d1c127SSebastian Grimberg /**
244ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` offsets array by @ref CeedMemType
245bd33150aSjeremylt 
246ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction` to retrieve offsets
247fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
248fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
249ca94c3ddSJeremy L Thompson   @param[out] offsets  Array on memory type `mem_type`
250bd33150aSjeremylt 
251bd33150aSjeremylt   @return An error code: 0 - success, otherwise - failure
252bd33150aSjeremylt 
253bd33150aSjeremylt   @ref User
254bd33150aSjeremylt **/
2552b730f8bSJeremy L Thompson int CeedElemRestrictionGetOffsets(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
2567c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
2577c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(rstr->rstr_base, mem_type, offsets));
258c17ec2beSJeremy L Thompson   } else {
2596e536b99SJeremy L Thompson     CeedCheck(rstr->GetOffsets, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
2601ef3a2a9SJeremy L Thompson               "Backend does not implement CeedElemRestrictionGetOffsets");
2612b730f8bSJeremy L Thompson     CeedCall(rstr->GetOffsets(rstr, mem_type, offsets));
262d1d35e2fSjeremylt     rstr->num_readers++;
263c17ec2beSJeremy L Thompson   }
264e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
265430758c8SJeremy L Thompson }
266430758c8SJeremy L Thompson 
267430758c8SJeremy L Thompson /**
268ca94c3ddSJeremy L Thompson   @brief Restore an offsets array obtained using @ref CeedElemRestrictionGetOffsets()
269430758c8SJeremy L Thompson 
270ca94c3ddSJeremy L Thompson   @param[in] rstr    `CeedElemRestriction` to restore
271ea61e9acSJeremy L Thompson   @param[in] offsets Array of offset data
272430758c8SJeremy L Thompson 
273430758c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
274430758c8SJeremy L Thompson 
275430758c8SJeremy L Thompson   @ref User
276430758c8SJeremy L Thompson **/
2772b730f8bSJeremy L Thompson int CeedElemRestrictionRestoreOffsets(CeedElemRestriction rstr, const CeedInt **offsets) {
2787c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
2797c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(rstr->rstr_base, offsets));
280c17ec2beSJeremy L Thompson   } else {
281430758c8SJeremy L Thompson     *offsets = NULL;
282d1d35e2fSjeremylt     rstr->num_readers--;
283c17ec2beSJeremy L Thompson   }
284e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
285bd33150aSjeremylt }
286bd33150aSjeremylt 
287bd33150aSjeremylt /**
288ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` orientations array by @ref CeedMemType
2893ac43b2cSJeremy L Thompson 
290ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction` to retrieve orientations
291fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
292fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
293ca94c3ddSJeremy L Thompson   @param[out] orients  Array on memory type `mem_type`
2943ac43b2cSJeremy L Thompson 
2953ac43b2cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2963ac43b2cSJeremy L Thompson 
29777d1c127SSebastian Grimberg   @ref User
2983ac43b2cSJeremy L Thompson **/
29977d1c127SSebastian Grimberg int CeedElemRestrictionGetOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
3006e536b99SJeremy L Thompson   CeedCheck(rstr->GetOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
3011ef3a2a9SJeremy L Thompson             "Backend does not implement CeedElemRestrictionGetOrientations");
30277d1c127SSebastian Grimberg   CeedCall(rstr->GetOrientations(rstr, mem_type, orients));
30377d1c127SSebastian Grimberg   rstr->num_readers++;
304e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3053ac43b2cSJeremy L Thompson }
3063ac43b2cSJeremy L Thompson 
3073ac43b2cSJeremy L Thompson /**
308ca94c3ddSJeremy L Thompson   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetOrientations()
309b435c5a6Srezgarshakeri 
310ca94c3ddSJeremy L Thompson   @param[in] rstr    `CeedElemRestriction` to restore
31177d1c127SSebastian Grimberg   @param[in] orients Array of orientation data
312b435c5a6Srezgarshakeri 
313b435c5a6Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
314b435c5a6Srezgarshakeri 
31577d1c127SSebastian Grimberg   @ref User
316b435c5a6Srezgarshakeri **/
31777d1c127SSebastian Grimberg int CeedElemRestrictionRestoreOrientations(CeedElemRestriction rstr, const bool **orients) {
31877d1c127SSebastian Grimberg   *orients = NULL;
31977d1c127SSebastian Grimberg   rstr->num_readers--;
320b435c5a6Srezgarshakeri   return CEED_ERROR_SUCCESS;
321b435c5a6Srezgarshakeri }
322b435c5a6Srezgarshakeri 
323b435c5a6Srezgarshakeri /**
324ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` curl-conforming orientations array by @ref CeedMemType
3257a982d89SJeremy L. Thompson 
326ca94c3ddSJeremy L Thompson   @param[in]  rstr         `CeedElemRestriction` to retrieve curl-conforming orientations
327fcbe8c06SSebastian Grimberg   @param[in]  mem_type     Memory type on which to access the array.
328fcbe8c06SSebastian Grimberg                              If the backend uses a different memory type, this will perform a copy (possibly cached).
329ca94c3ddSJeremy L Thompson   @param[out] curl_orients Array on memory type `mem_type`
3307a982d89SJeremy L. Thompson 
3317a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3327a982d89SJeremy L. Thompson 
33377d1c127SSebastian Grimberg   @ref User
3347a982d89SJeremy L. Thompson **/
3350c73c039SSebastian Grimberg int CeedElemRestrictionGetCurlOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
3366e536b99SJeremy L Thompson   CeedCheck(rstr->GetCurlOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
3371ef3a2a9SJeremy L Thompson             "Backend does not implement CeedElemRestrictionGetCurlOrientations");
33877d1c127SSebastian Grimberg   CeedCall(rstr->GetCurlOrientations(rstr, mem_type, curl_orients));
33977d1c127SSebastian Grimberg   rstr->num_readers++;
34077d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
34177d1c127SSebastian Grimberg }
34277d1c127SSebastian Grimberg 
34377d1c127SSebastian Grimberg /**
344ca94c3ddSJeremy L Thompson   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetCurlOrientations()
34577d1c127SSebastian Grimberg 
346ca94c3ddSJeremy L Thompson   @param[in] rstr         `CeedElemRestriction` to restore
34777d1c127SSebastian Grimberg   @param[in] curl_orients Array of orientation data
34877d1c127SSebastian Grimberg 
34977d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
35077d1c127SSebastian Grimberg 
35177d1c127SSebastian Grimberg   @ref User
35277d1c127SSebastian Grimberg **/
3530c73c039SSebastian Grimberg int CeedElemRestrictionRestoreCurlOrientations(CeedElemRestriction rstr, const CeedInt8 **curl_orients) {
35477d1c127SSebastian Grimberg   *curl_orients = NULL;
35577d1c127SSebastian Grimberg   rstr->num_readers--;
356e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3577a982d89SJeremy L. Thompson }
3587a982d89SJeremy L. Thompson 
3597a982d89SJeremy L. Thompson /**
36049fd234cSJeremy L Thompson 
36122eb1385SJeremy L Thompson   @brief Get the L-vector layout of a strided `CeedElemRestriction`
36222eb1385SJeremy L Thompson 
36322eb1385SJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
36422eb1385SJeremy L Thompson   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
36522eb1385SJeremy L Thompson                         The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
36622eb1385SJeremy L Thompson 
36722eb1385SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
36822eb1385SJeremy L Thompson 
36922eb1385SJeremy L Thompson   @ref Backend
37022eb1385SJeremy L Thompson **/
37156c48462SJeremy L Thompson int CeedElemRestrictionGetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
37222eb1385SJeremy L Thompson   bool                has_backend_strides;
37322eb1385SJeremy L Thompson   CeedRestrictionType rstr_type;
37422eb1385SJeremy L Thompson 
37522eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
3769bc66399SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR,
3779bc66399SJeremy L Thompson             "Only strided CeedElemRestriction have strided L-vector layout");
37822eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
37922eb1385SJeremy L Thompson   if (has_backend_strides) {
3809bc66399SJeremy L Thompson     CeedCheck(rstr->l_layout[0], CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no L-vector layout data");
38156c48462SJeremy L Thompson     for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->l_layout[i];
38222eb1385SJeremy L Thompson   } else {
38322eb1385SJeremy L Thompson     CeedCall(CeedElemRestrictionGetStrides(rstr, layout));
38422eb1385SJeremy L Thompson   }
38522eb1385SJeremy L Thompson   return CEED_ERROR_SUCCESS;
38622eb1385SJeremy L Thompson }
38722eb1385SJeremy L Thompson 
38822eb1385SJeremy L Thompson /**
38922eb1385SJeremy L Thompson 
39022eb1385SJeremy L Thompson   @brief Set the L-vector layout of a strided `CeedElemRestriction`
39122eb1385SJeremy L Thompson 
39222eb1385SJeremy L Thompson   @param[in] rstr   `CeedElemRestriction`
39322eb1385SJeremy L Thompson   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
39422eb1385SJeremy L Thompson                       The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
39522eb1385SJeremy L Thompson 
39622eb1385SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
39722eb1385SJeremy L Thompson 
39822eb1385SJeremy L Thompson   @ref Backend
39922eb1385SJeremy L Thompson **/
40022eb1385SJeremy L Thompson int CeedElemRestrictionSetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
40122eb1385SJeremy L Thompson   CeedRestrictionType rstr_type;
40222eb1385SJeremy L Thompson 
40322eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
4046e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR,
4056e536b99SJeremy L Thompson             "Only strided CeedElemRestriction have strided L-vector layout");
40622eb1385SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) rstr->l_layout[i] = layout[i];
40722eb1385SJeremy L Thompson   return CEED_ERROR_SUCCESS;
40822eb1385SJeremy L Thompson }
40922eb1385SJeremy L Thompson 
41022eb1385SJeremy L Thompson /**
41122eb1385SJeremy L Thompson 
412ca94c3ddSJeremy L Thompson   @brief Get the E-vector layout of a `CeedElemRestriction`
41349fd234cSJeremy L Thompson 
414ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
415ca94c3ddSJeremy L Thompson   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
416ca94c3ddSJeremy L Thompson                         The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
41749fd234cSJeremy L Thompson 
41849fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
41949fd234cSJeremy L Thompson 
42049fd234cSJeremy L Thompson   @ref Backend
42149fd234cSJeremy L Thompson **/
42256c48462SJeremy L Thompson int CeedElemRestrictionGetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
4236e536b99SJeremy L Thompson   CeedCheck(rstr->e_layout[0], CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no E-vector layout data");
42456c48462SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->e_layout[i];
425e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
42649fd234cSJeremy L Thompson }
42749fd234cSJeremy L Thompson 
42849fd234cSJeremy L Thompson /**
42949fd234cSJeremy L Thompson 
430ca94c3ddSJeremy L Thompson   @brief Set the E-vector layout of a `CeedElemRestriction`
43149fd234cSJeremy L Thompson 
432ca94c3ddSJeremy L Thompson   @param[in] rstr   `CeedElemRestriction`
433ca94c3ddSJeremy L Thompson   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
434ca94c3ddSJeremy L Thompson                       The data for node `i`, component `j`, element `k` in the E-vector is given by `i*layout[0] + j*layout[1] + k*layout[2]`.
43549fd234cSJeremy L Thompson 
43649fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
43749fd234cSJeremy L Thompson 
43849fd234cSJeremy L Thompson   @ref Backend
43949fd234cSJeremy L Thompson **/
4402b730f8bSJeremy L Thompson int CeedElemRestrictionSetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
44122eb1385SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) rstr->e_layout[i] = layout[i];
442e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
44349fd234cSJeremy L Thompson }
44449fd234cSJeremy L Thompson 
44549fd234cSJeremy L Thompson /**
44619605835SJeremy L Thompson 
44719605835SJeremy L Thompson   @brief Get the E-vector element offset of a `CeedElemRestriction` at points
44819605835SJeremy L Thompson 
44919605835SJeremy L Thompson   @param[in]  rstr        `CeedElemRestriction`
45019605835SJeremy L Thompson   @param[in]  elem        Element number index into E-vector for
45119605835SJeremy L Thompson   @param[out] elem_offset Offset for element `elem` in the E-vector.
45219605835SJeremy L Thompson                             The data for point `i`, component `j`, element `elem` in the E-vector is given by `i*e_layout[0] + j*e_layout[1] + elem_offset`.
45319605835SJeremy L Thompson 
45419605835SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
45519605835SJeremy L Thompson 
45619605835SJeremy L Thompson   @ref Backend
45719605835SJeremy L Thompson **/
45819605835SJeremy L Thompson int CeedElemRestrictionGetAtPointsElementOffset(CeedElemRestriction rstr, CeedInt elem, CeedSize *elem_offset) {
45919605835SJeremy L Thompson   CeedInt             num_comp;
46019605835SJeremy L Thompson   CeedRestrictionType rstr_type;
46119605835SJeremy L Thompson 
46219605835SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
46319605835SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
46419605835SJeremy L Thompson             "Can only compute offset for a points CeedElemRestriction");
46519605835SJeremy L Thompson 
46619605835SJeremy L Thompson   // Backend method
46719605835SJeremy L Thompson   if (rstr->GetAtPointsElementOffset) {
46819605835SJeremy L Thompson     CeedCall(rstr->GetAtPointsElementOffset(rstr, elem, elem_offset));
46919605835SJeremy L Thompson     return CEED_ERROR_SUCCESS;
47019605835SJeremy L Thompson   }
47119605835SJeremy L Thompson 
47219605835SJeremy L Thompson   // Default layout (CPU)
47319605835SJeremy L Thompson   *elem_offset = 0;
47419605835SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
47519605835SJeremy L Thompson   for (CeedInt i = 0; i < elem; i++) {
47619605835SJeremy L Thompson     CeedInt num_points;
47719605835SJeremy L Thompson 
47819605835SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, i, &num_points));
47919605835SJeremy L Thompson     *elem_offset += num_points * num_comp;
48019605835SJeremy L Thompson   }
48119605835SJeremy L Thompson   return CEED_ERROR_SUCCESS;
48219605835SJeremy L Thompson }
48319605835SJeremy L Thompson 
48419605835SJeremy L Thompson /**
485ff1bc20eSJeremy L Thompson 
486ff1bc20eSJeremy L Thompson   @brief Set the E-vector size of a `CeedElemRestriction` at points
487ff1bc20eSJeremy L Thompson 
488ff1bc20eSJeremy L Thompson   @param[in,out]  rstr   `CeedElemRestriction`
489ff1bc20eSJeremy L Thompson   @param[in]      e_size New E-vector size; must be longer than the current E-vector size
490ff1bc20eSJeremy L Thompson 
491ff1bc20eSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
492ff1bc20eSJeremy L Thompson 
493ff1bc20eSJeremy L Thompson   @ref Backend
494ff1bc20eSJeremy L Thompson **/
495ff1bc20eSJeremy L Thompson int CeedElemRestrictionSetAtPointsEVectorSize(CeedElemRestriction rstr, CeedSize e_size) {
496ff1bc20eSJeremy L Thompson   CeedRestrictionType rstr_type;
497ff1bc20eSJeremy L Thompson 
498ff1bc20eSJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
4999bc66399SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
5009bc66399SJeremy L Thompson             "Can only compute offset for a points CeedElemRestriction");
5019bc66399SJeremy L Thompson   CeedCheck(e_size >= rstr->e_size, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
5023f08121cSJeremy L Thompson             "Can only increase the size of the E-vector for the CeedElemRestriction."
5033f08121cSJeremy L Thompson             " Current size: %" CeedSize_FMT " New size: %" CeedSize_FMT,
5043f08121cSJeremy L Thompson             rstr->e_size, e_size);
505ff1bc20eSJeremy L Thompson   rstr->e_size = e_size;
506ff1bc20eSJeremy L Thompson   return CEED_ERROR_SUCCESS;
507ff1bc20eSJeremy L Thompson }
508ff1bc20eSJeremy L Thompson 
509ff1bc20eSJeremy L Thompson /**
510ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedElemRestriction`
5117a982d89SJeremy L. Thompson 
512ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
5137a982d89SJeremy L. Thompson   @param[out] data Variable to store data
5147a982d89SJeremy L. Thompson 
5157a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5167a982d89SJeremy L. Thompson 
5177a982d89SJeremy L. Thompson   @ref Backend
5187a982d89SJeremy L. Thompson **/
519777ff853SJeremy L Thompson int CeedElemRestrictionGetData(CeedElemRestriction rstr, void *data) {
520777ff853SJeremy L Thompson   *(void **)data = rstr->data;
521e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5227a982d89SJeremy L. Thompson }
5237a982d89SJeremy L. Thompson 
5247a982d89SJeremy L. Thompson /**
525ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedElemRestriction`
5267a982d89SJeremy L. Thompson 
527ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction`
528ea61e9acSJeremy L Thompson   @param[in]     data Data to set
5297a982d89SJeremy L. Thompson 
5307a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
5317a982d89SJeremy L. Thompson 
5327a982d89SJeremy L. Thompson   @ref Backend
5337a982d89SJeremy L. Thompson **/
534777ff853SJeremy L Thompson int CeedElemRestrictionSetData(CeedElemRestriction rstr, void *data) {
535777ff853SJeremy L Thompson   rstr->data = data;
536e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5377a982d89SJeremy L. Thompson }
5387a982d89SJeremy L. Thompson 
53934359f16Sjeremylt /**
540ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedElemRestriction`
54134359f16Sjeremylt 
542ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction` to increment the reference counter
54334359f16Sjeremylt 
54434359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
54534359f16Sjeremylt 
54634359f16Sjeremylt   @ref Backend
54734359f16Sjeremylt **/
5489560d06aSjeremylt int CeedElemRestrictionReference(CeedElemRestriction rstr) {
54934359f16Sjeremylt   rstr->ref_count++;
55034359f16Sjeremylt   return CEED_ERROR_SUCCESS;
55134359f16Sjeremylt }
55234359f16Sjeremylt 
5536e15d496SJeremy L Thompson /**
554ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedElemRestriction` in `t_mode`
5556e15d496SJeremy L Thompson 
556ca94c3ddSJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction` to estimate FLOPs for
557ea61e9acSJeremy L Thompson   @param[in]  t_mode Apply restriction or transpose
558ea61e9acSJeremy L Thompson   @param[out] flops  Address of variable to hold FLOPs estimate
5596e15d496SJeremy L Thompson 
5606e15d496SJeremy L Thompson   @ref Backend
5616e15d496SJeremy L Thompson **/
5622b730f8bSJeremy L Thompson int CeedElemRestrictionGetFlopsEstimate(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedSize *flops) {
5631203703bSJeremy L Thompson   CeedSize            e_size, scale = 0;
56489edb9e3SSebastian Grimberg   CeedRestrictionType rstr_type;
5651c66c397SJeremy L Thompson 
5661203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
56789edb9e3SSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
56877d1c127SSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
56989edb9e3SSebastian Grimberg     switch (rstr_type) {
5703ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
5713ac8f562SJeremy L Thompson         scale = 0;
5723ac8f562SJeremy L Thompson         break;
57389edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
57489edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
57577d1c127SSebastian Grimberg         scale = 1;
57689edb9e3SSebastian Grimberg         break;
57789edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
57877d1c127SSebastian Grimberg         scale = 2;
57989edb9e3SSebastian Grimberg         break;
58089edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
58177d1c127SSebastian Grimberg         scale = 6;
58289edb9e3SSebastian Grimberg         break;
5836e15d496SJeremy L Thompson     }
58477d1c127SSebastian Grimberg   } else {
58589edb9e3SSebastian Grimberg     switch (rstr_type) {
58689edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
58789edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
5883ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
58977d1c127SSebastian Grimberg         scale = 0;
59089edb9e3SSebastian Grimberg         break;
59189edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
59277d1c127SSebastian Grimberg         scale = 1;
59389edb9e3SSebastian Grimberg         break;
59489edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
59577d1c127SSebastian Grimberg         scale = 5;
59689edb9e3SSebastian Grimberg         break;
59777d1c127SSebastian Grimberg     }
59877d1c127SSebastian Grimberg   }
5996e15d496SJeremy L Thompson   *flops = e_size * scale;
6006e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6016e15d496SJeremy L Thompson }
6026e15d496SJeremy L Thompson 
6037a982d89SJeremy L. Thompson /// @}
6047a982d89SJeremy L. Thompson 
60515910d16Sjeremylt /// @cond DOXYGEN_SKIP
60615910d16Sjeremylt static struct CeedElemRestriction_private ceed_elemrestriction_none;
60715910d16Sjeremylt /// @endcond
60815910d16Sjeremylt 
6097a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6107a982d89SJeremy L. Thompson /// CeedElemRestriction Public API
6117a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
6127a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionUser
613d7b241e6Sjeremylt /// @{
614d7b241e6Sjeremylt 
6157a982d89SJeremy L. Thompson /// Indicate that the stride is determined by the backend
61645f1e315Sjeremylt const CeedInt CEED_STRIDES_BACKEND[3] = {0};
6177a982d89SJeremy L. Thompson 
618ca94c3ddSJeremy L Thompson /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedElemRestriction`
6192b730f8bSJeremy L Thompson const CeedElemRestriction CEED_ELEMRESTRICTION_NONE = &ceed_elemrestriction_none;
6207a982d89SJeremy L. Thompson 
621d7b241e6Sjeremylt /**
622ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction`
623d7b241e6Sjeremylt 
624ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
625ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
626ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
627ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
628fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
629ca94c3ddSJeremy L Thompson                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
630fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
631fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
632ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
633ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
634ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
635ca94c3ddSJeremy L Thompson                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where 0 <= i < @a num_elem.
636ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
637ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
638d7b241e6Sjeremylt 
639b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
640dfdf5a53Sjeremylt 
6417a982d89SJeremy L. Thompson   @ref User
642b11c1e72Sjeremylt **/
6432b730f8bSJeremy L Thompson int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
6442b730f8bSJeremy L Thompson                               CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
6455fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
6465fe0d4faSjeremylt     Ceed delegate;
6476574a04fSJeremy L Thompson 
6482b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
649ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreate");
6502b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreate(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr));
6519bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
652e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
6535fe0d4faSjeremylt   }
6545fe0d4faSjeremylt 
655e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
6566574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
657ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
658ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
659e022e1f8SJeremy L Thompson 
6602b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
661db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
662d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
663d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
664d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
665d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
666d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
667d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
6681203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
669e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
670e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
67161a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
672fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
673e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
674d7b241e6Sjeremylt }
675d7b241e6Sjeremylt 
676d7b241e6Sjeremylt /**
677ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction` with orientation signs
678fc0567d9Srezgarshakeri 
679ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
680ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
681ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
682ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
683fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
684ca94c3ddSJeremy L Thompson                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
685fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
686fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
687ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
688ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
689ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
690ca94c3ddSJeremy L Thompson                             Row i holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
691ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
692ca94c3ddSJeremy L Thompson   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation
693ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
694fc0567d9Srezgarshakeri 
695fc0567d9Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
696fc0567d9Srezgarshakeri 
697fc0567d9Srezgarshakeri   @ref User
698fc0567d9Srezgarshakeri **/
6992b730f8bSJeremy L Thompson int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
70077d1c127SSebastian Grimberg                                       CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
701fc0567d9Srezgarshakeri                                       CeedElemRestriction *rstr) {
702fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
703fc0567d9Srezgarshakeri     Ceed delegate;
7046574a04fSJeremy L Thompson 
7052b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
706ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateOriented");
7071a8516d0SJames Wright     CeedCall(CeedElemRestrictionCreateOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients,
7081a8516d0SJames Wright                                                rstr));
7099bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
710fc0567d9Srezgarshakeri     return CEED_ERROR_SUCCESS;
711fc0567d9Srezgarshakeri   }
712fc0567d9Srezgarshakeri 
713e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
7146574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
715ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
716ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
717e022e1f8SJeremy L Thompson 
7182b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
719db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
720fc0567d9Srezgarshakeri   (*rstr)->ref_count   = 1;
721fc0567d9Srezgarshakeri   (*rstr)->num_elem    = num_elem;
722fc0567d9Srezgarshakeri   (*rstr)->elem_size   = elem_size;
723fc0567d9Srezgarshakeri   (*rstr)->num_comp    = num_comp;
724fc0567d9Srezgarshakeri   (*rstr)->comp_stride = comp_stride;
725fc0567d9Srezgarshakeri   (*rstr)->l_size      = l_size;
7261203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
727e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
728e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
729fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
730fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, orients, NULL, *rstr));
73177d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
73277d1c127SSebastian Grimberg }
73377d1c127SSebastian Grimberg 
73477d1c127SSebastian Grimberg /**
735ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction` with a general tridiagonal transformation matrix for curl-conforming elements
73677d1c127SSebastian Grimberg 
737ca94c3ddSJeremy L Thompson   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
738ca94c3ddSJeremy L Thompson   @param[in]  num_elem     Number of elements described in the `offsets` array
73977d1c127SSebastian Grimberg   @param[in]  elem_size    Size (number of "nodes") per element
74077d1c127SSebastian Grimberg   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
741fcbe8c06SSebastian Grimberg   @param[in]  comp_stride  Stride between components for the same L-vector "node".
742ca94c3ddSJeremy L Thompson                              Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
743fcbe8c06SSebastian Grimberg   @param[in]  l_size       The size of the L-vector.
744fcbe8c06SSebastian Grimberg                              This vector may be larger than the elements and fields given by this restriction.
745ca94c3ddSJeremy L Thompson   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
746ca94c3ddSJeremy L Thompson   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
747ca94c3ddSJeremy L Thompson   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
748ca94c3ddSJeremy L Thompson                              Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
749ca94c3ddSJeremy L Thompson                              All offsets must be in the range `[0, l_size - 1]`.
750ca94c3ddSJeremy L Thompson   @param[in]  curl_orients Array of shape `[num_elem, 3 * elem_size]` representing a row-major tridiagonal matrix (`curl_orients[i * 3 * elem_size] = curl_orients[(i + 1) * 3 * elem_size - 1] = 0`, where `0 <= i < num_elem`) which is applied to the element unknowns upon restriction.
751ca94c3ddSJeremy L Thompson                              This orientation matrix allows for pairs of face degrees of freedom on elements for \f$H(\mathrm{curl})\f$ spaces to be coupled in the element restriction operation, which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456).
752ca94c3ddSJeremy L Thompson   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
75377d1c127SSebastian Grimberg 
75477d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
75577d1c127SSebastian Grimberg 
75677d1c127SSebastian Grimberg   @ref User
75777d1c127SSebastian Grimberg **/
75877d1c127SSebastian Grimberg int CeedElemRestrictionCreateCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
7590c73c039SSebastian Grimberg                                           CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const CeedInt8 *curl_orients,
76077d1c127SSebastian Grimberg                                           CeedElemRestriction *rstr) {
761fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
76277d1c127SSebastian Grimberg     Ceed delegate;
76377d1c127SSebastian Grimberg 
76477d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
765ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateCurlOriented");
76677d1c127SSebastian Grimberg     CeedCall(CeedElemRestrictionCreateCurlOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
76777d1c127SSebastian Grimberg                                                    curl_orients, rstr));
7689bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
76977d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
77077d1c127SSebastian Grimberg   }
77177d1c127SSebastian Grimberg 
772e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
77377d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
774ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
775ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
77677d1c127SSebastian Grimberg 
77777d1c127SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
778fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
77977d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
78077d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
78177d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
78277d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
78377d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
78477d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
7851203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
786e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
787e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
788fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
789fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, curl_orients, *rstr));
790fc0567d9Srezgarshakeri   return CEED_ERROR_SUCCESS;
791fc0567d9Srezgarshakeri }
792fc0567d9Srezgarshakeri 
793fc0567d9Srezgarshakeri /**
794ca94c3ddSJeremy L Thompson   @brief Create a strided `CeedElemRestriction`
795d7b241e6Sjeremylt 
796ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` context used to create the `CeedElemRestriction`
797ea61e9acSJeremy L Thompson   @param[in]  num_elem  Number of elements described by the restriction
798ea61e9acSJeremy L Thompson   @param[in]  elem_size Size (number of "nodes") per element
799ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components per interpolation "node" (1 for scalar fields)
800fcbe8c06SSebastian Grimberg   @param[in]  l_size    The size of the L-vector.
801fcbe8c06SSebastian Grimberg                           This vector may be larger than the elements and fields given by this restriction.
802ca94c3ddSJeremy L Thompson   @param[in]  strides   Array for strides between `[nodes, components, elements]`.
803ca94c3ddSJeremy L Thompson                           Data for node `i`, component `j`, element `k` can be found in the L-vector at index `i*strides[0] + j*strides[1] + k*strides[2]`.
804ca94c3ddSJeremy L Thompson                           @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
805972f909dSJeremy L Thompson                           `CEED_STRIDES_BACKEND` should only be used pass data between `CeedOperator` created with the same `Ceed` backend.
806972f909dSJeremy L Thompson                           The L-vector layout will, in general, be different between `Ceed` backends.
807ca94c3ddSJeremy L Thompson   @param[out] rstr      Address of the variable where the newly created `CeedElemRestriction` will be stored
808d7b241e6Sjeremylt 
809b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
810dfdf5a53Sjeremylt 
8117a982d89SJeremy L. Thompson   @ref User
812b11c1e72Sjeremylt **/
8132b730f8bSJeremy L Thompson int CeedElemRestrictionCreateStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedSize l_size, const CeedInt strides[3],
814f90c8643Sjeremylt                                      CeedElemRestriction *rstr) {
8155fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
8165fe0d4faSjeremylt     Ceed delegate;
817b04eb3d9SSebastian Grimberg 
8182b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
819ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateStrided");
8202b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(delegate, num_elem, elem_size, num_comp, l_size, strides, rstr));
8219bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
822e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
8235fe0d4faSjeremylt   }
8245fe0d4faSjeremylt 
825e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
8266574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
827ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
828aa72de07SJeremy L Thompson   CeedCheck(l_size >= (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp, ceed, CEED_ERROR_DIMENSION,
829870ea2d9SJeremy L Thompson             "L-vector size must be at least num_elem * elem_size * num_comp. Expected: > %" CeedSize_FMT " Found: %" CeedSize_FMT,
830870ea2d9SJeremy L Thompson             (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp, l_size);
831e022e1f8SJeremy L Thompson 
8322b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
833db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
834d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
835d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
836d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
837d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
838d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
8391203703bSJeremy L Thompson   (*rstr)->e_size     = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
840e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_elem;
841e7f679fcSJeremy L Thompson   (*rstr)->block_size = 1;
842fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
8432b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
8442b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
845fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
846e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
847d7b241e6Sjeremylt }
848d7b241e6Sjeremylt 
849d7b241e6Sjeremylt /**
850ca94c3ddSJeremy L Thompson   @brief Create a points `CeedElemRestriction`, for restricting for restricting from a all local points to the current element in which they are located.
8513ac8f562SJeremy L Thompson 
8523ac8f562SJeremy L Thompson   The offsets array is arranged as
8533ac8f562SJeremy L Thompson 
8543ac8f562SJeremy L Thompson   element_0_start_index
8553ac8f562SJeremy L Thompson   element_1_start_index
8563ac8f562SJeremy L Thompson   ...
8573ac8f562SJeremy L Thompson   element_n_start_index
8583ac8f562SJeremy L Thompson   element_n_stop_index
8593ac8f562SJeremy L Thompson   element_0_point_0
8603ac8f562SJeremy L Thompson   element_0_point_1
8613ac8f562SJeremy L Thompson   ...
8623ac8f562SJeremy L Thompson 
863ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` context used to create the `CeedElemRestriction`
864ca94c3ddSJeremy L Thompson   @param[in]  num_elem   Number of elements described in the `offsets` array
865ca94c3ddSJeremy L Thompson   @param[in]  num_points Number of points described in the `offsets` array
8663ac8f562SJeremy L Thompson   @param[in]  num_comp   Number of field components per interpolation node (1 for scalar fields).
8673ac8f562SJeremy L Thompson                            Components are assumed to be contiguous by point.
8683ac8f562SJeremy L Thompson   @param[in]  l_size     The size of the L-vector.
8693ac8f562SJeremy L Thompson                            This vector may be larger than the elements and fields given by this restriction.
870ca94c3ddSJeremy L Thompson   @param[in]  mem_type   Memory type of the `offsets` array, see @ref CeedMemType
871ca94c3ddSJeremy L Thompson   @param[in]  copy_mode  Copy mode for the `offsets` array, see @ref CeedCopyMode
872ca94c3ddSJeremy L Thompson   @param[in]  offsets    Array of size `num_elem + 1 + num_points`.
8733ac8f562SJeremy L Thompson                            The first portion of the offsets array holds the ranges of indices corresponding to each element.
8743ac8f562SJeremy L Thompson                            The second portion holds the indices for each element.
875ca94c3ddSJeremy L Thompson   @param[out] rstr       Address of the variable where the newly created `CeedElemRestriction` will be stored
8763ac8f562SJeremy L Thompson 
8773ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
8783ac8f562SJeremy L Thompson 
8793ac8f562SJeremy L Thompson   @ref Backend
8803ac8f562SJeremy L Thompson  **/
8813ac8f562SJeremy L Thompson int CeedElemRestrictionCreateAtPoints(Ceed ceed, CeedInt num_elem, CeedInt num_points, CeedInt num_comp, CeedSize l_size, CeedMemType mem_type,
8823ac8f562SJeremy L Thompson                                       CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
8833ac8f562SJeremy L Thompson   if (!ceed->ElemRestrictionCreateAtPoints) {
8843ac8f562SJeremy L Thompson     Ceed delegate;
8853ac8f562SJeremy L Thompson 
8863ac8f562SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
887ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateAtPoints");
8883ac8f562SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateAtPoints(delegate, num_elem, num_points, num_comp, l_size, mem_type, copy_mode, offsets, rstr));
8899bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
8903ac8f562SJeremy L Thompson     return CEED_ERROR_SUCCESS;
8913ac8f562SJeremy L Thompson   }
8923ac8f562SJeremy L Thompson 
8933ac8f562SJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
8943ac8f562SJeremy L Thompson   CeedCheck(num_points >= 0, ceed, CEED_ERROR_DIMENSION, "Number of points must be non-negative");
895ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
896870ea2d9SJeremy L Thompson   CeedCheck(l_size >= (CeedSize)num_points * num_comp, ceed, CEED_ERROR_DIMENSION,
897870ea2d9SJeremy L Thompson             "L-vector must be at least num_points * num_comp. Expected: > %" CeedSize_FMT " Found: %" CeedSize_FMT, (CeedSize)num_points * num_comp,
898870ea2d9SJeremy L Thompson             l_size);
8993ac8f562SJeremy L Thompson 
9003ac8f562SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
9013ac8f562SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
9023ac8f562SJeremy L Thompson   (*rstr)->ref_count   = 1;
9033ac8f562SJeremy L Thompson   (*rstr)->num_elem    = num_elem;
9043ac8f562SJeremy L Thompson   (*rstr)->num_points  = num_points;
9053ac8f562SJeremy L Thompson   (*rstr)->num_comp    = num_comp;
9062c2c926cSJeremy L Thompson   (*rstr)->comp_stride = 1;
9073ac8f562SJeremy L Thompson   (*rstr)->l_size      = l_size;
9081203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_points * (CeedSize)num_comp;
90905fa913cSJeremy L Thompson   (*rstr)->num_block   = num_elem;
9103ac8f562SJeremy L Thompson   (*rstr)->block_size  = 1;
9113ac8f562SJeremy L Thompson   (*rstr)->rstr_type   = CEED_RESTRICTION_POINTS;
9123ac8f562SJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateAtPoints(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
9133ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9143ac8f562SJeremy L Thompson }
9153ac8f562SJeremy L Thompson 
9163ac8f562SJeremy L Thompson /**
917ca94c3ddSJeremy L Thompson   @brief Create a blocked `CeedElemRestriction`, typically only used by backends
918d7b241e6Sjeremylt 
919ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
920ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
921ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of unknowns) per element
922e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
923ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
924fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
925ca94c3ddSJeremy L Thompson                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
926fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
927fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
928ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
929ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
930ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
931ca94c3ddSJeremy L Thompson                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
932ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
933ca94c3ddSJeremy L Thompson                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
934ca94c3ddSJeremy L Thompson                             The default reordering is to interlace elements.
935ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
936d7b241e6Sjeremylt 
937b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
938dfdf5a53Sjeremylt 
9397a982d89SJeremy L. Thompson   @ref Backend
940b11c1e72Sjeremylt  **/
941e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedInt comp_stride,
9422b730f8bSJeremy L Thompson                                      CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
9434ce2993fSjeremylt                                      CeedElemRestriction *rstr) {
9441c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
945d7b241e6Sjeremylt 
9465fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
9475fe0d4faSjeremylt     Ceed delegate;
9486574a04fSJeremy L Thompson 
9492b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
950ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlocked");
951e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlocked(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
952e7f679fcSJeremy L Thompson                                               rstr));
9539bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
954e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
9555fe0d4faSjeremylt   }
956d7b241e6Sjeremylt 
957e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
9586574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
959e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
960ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
961ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
962e022e1f8SJeremy L Thompson 
963e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
964e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
965d7b241e6Sjeremylt 
966db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
967db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
968d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
969d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
970d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
971d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
972d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
973d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
9741203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
975e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
976e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
97761a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
978e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL, NULL, *rstr));
9791c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
980e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
981d7b241e6Sjeremylt }
982d7b241e6Sjeremylt 
983b11c1e72Sjeremylt /**
984ca94c3ddSJeremy L Thompson   @brief Create a blocked oriented `CeedElemRestriction`, typically only used by backends
98577d1c127SSebastian Grimberg 
986ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
987ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array.
98877d1c127SSebastian Grimberg   @param[in]  elem_size   Size (number of unknowns) per element
989e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
99077d1c127SSebastian Grimberg   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
991fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
992ca94c3ddSJeremy L Thompson                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
993fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
994fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
995ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
996ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
997ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
998ca94c3ddSJeremy L Thompson                             Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
999ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
1000ca94c3ddSJeremy L Thompson                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
1001ca94c3ddSJeremy L Thompson                             The default reordering is to interlace elements.
1002ca94c3ddSJeremy L Thompson   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation.
1003ca94c3ddSJeremy L Thompson                             Will also be permuted and padded similarly to `offsets`.
1004ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
100577d1c127SSebastian Grimberg 
100677d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
100777d1c127SSebastian Grimberg 
100877d1c127SSebastian Grimberg   @ref Backend
100977d1c127SSebastian Grimberg  **/
1010e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
1011e7f679fcSJeremy L Thompson                                              CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
1012e7f679fcSJeremy L Thompson                                              const CeedInt *offsets, const bool *orients, CeedElemRestriction *rstr) {
1013e7f679fcSJeremy L Thompson   bool    *block_orients;
10141c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
101577d1c127SSebastian Grimberg 
1016fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
101777d1c127SSebastian Grimberg     Ceed delegate;
101877d1c127SSebastian Grimberg 
101977d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1020ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedOriented");
1021e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode,
102277d1c127SSebastian Grimberg                                                       offsets, orients, rstr));
10239bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
102477d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
102577d1c127SSebastian Grimberg   }
102677d1c127SSebastian Grimberg 
102777d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1028e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1029ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1030ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
103177d1c127SSebastian Grimberg 
1032e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
1033e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_orients));
1034e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
1035e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOrients(orients, block_orients, num_block, num_elem, block_size, elem_size));
103677d1c127SSebastian Grimberg 
1037fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
1038fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
103977d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
104077d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
104177d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
104277d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
104377d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
104477d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
10451203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1046e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
1047e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
1048fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
10491a8516d0SJames Wright   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, (const bool *)block_orients, NULL,
10501a8516d0SJames Wright                                               *rstr));
10511c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
105277d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
105377d1c127SSebastian Grimberg }
105477d1c127SSebastian Grimberg 
105577d1c127SSebastian Grimberg /**
1056ca94c3ddSJeremy L Thompson   @brief Create a blocked curl-oriented `CeedElemRestriction`, typically only used by backends
105777d1c127SSebastian Grimberg 
1058ca94c3ddSJeremy L Thompson   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
1059ca94c3ddSJeremy L Thompson   @param[in]  num_elem     Number of elements described in the `offsets` array.
106077d1c127SSebastian Grimberg   @param[in]  elem_size    Size (number of unknowns) per element
1061e7f679fcSJeremy L Thompson   @param[in]  block_size   Number of elements in a block
106277d1c127SSebastian Grimberg   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
1063fcbe8c06SSebastian Grimberg   @param[in]  comp_stride  Stride between components for the same L-vector "node".
1064ca94c3ddSJeremy L Thompson                              Data for node `i`, component `j`, element `k` can be found in the L-vector at index `offsets[i + k*elem_size] + j*comp_stride`.
1065fcbe8c06SSebastian Grimberg   @param[in]  l_size       The size of the L-vector.
1066fcbe8c06SSebastian Grimberg                              This vector may be larger than the elements and fields given by this restriction.
1067ca94c3ddSJeremy L Thompson   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
1068ca94c3ddSJeremy L Thompson   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
1069ca94c3ddSJeremy L Thompson   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
1070ca94c3ddSJeremy L Thompson                              Row `i` holds the ordered list of the offsets (into the input `CeedVector`) for the unknowns corresponding to element `i`, where `0 <= i < num_elem`.
1071ca94c3ddSJeremy L Thompson                              All offsets must be in the range `[0, l_size - 1]`.
1072ca94c3ddSJeremy L Thompson                              The backend will permute and pad this array to the desired  ordering for the blocksize, which is typically given by the backend.
1073ca94c3ddSJeremy L Thompson                              The default reordering is to interlace elements.
1074ca94c3ddSJeremy L Thompson   @param[in]  curl_orients Array of shape `[num_elem, 3 * elem_size]` representing a row-major tridiagonal matrix (`curl_orients[i * 3 * elem_size] = curl_orients[(i + 1) * 3 * elem_size - 1] = 0`, where `0 <= i < num_elem`) which is applied to the element unknowns upon restriction.
1075ca94c3ddSJeremy L Thompson                              This orientation matrix allows for pairs of face degrees of freedom on elements for \f$H(\mathrm{curl})\f$ spaces to be coupled in the element restriction  operation, which is a way to resolve face orientation issues for 3D meshes (https://dl.acm.org/doi/pdf/10.1145/3524456).
1076ca94c3ddSJeremy L Thompson                              Will also be permuted and padded similarly to offsets.
1077ca94c3ddSJeremy L Thompson   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
107877d1c127SSebastian Grimberg 
107977d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
108077d1c127SSebastian Grimberg 
108177d1c127SSebastian Grimberg   @ref Backend
108277d1c127SSebastian Grimberg  **/
1083e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
108477d1c127SSebastian Grimberg                                                  CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
10850c73c039SSebastian Grimberg                                                  const CeedInt *offsets, const CeedInt8 *curl_orients, CeedElemRestriction *rstr) {
1086e7f679fcSJeremy L Thompson   CeedInt8 *block_curl_orients;
10871c66c397SJeremy L Thompson   CeedInt  *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
108877d1c127SSebastian Grimberg 
1089fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
109077d1c127SSebastian Grimberg     Ceed delegate;
109177d1c127SSebastian Grimberg 
109277d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1093ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedCurlOriented");
1094e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedCurlOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type,
1095e7f679fcSJeremy L Thompson                                                           copy_mode, offsets, curl_orients, rstr));
10969bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
109777d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
109877d1c127SSebastian Grimberg   }
109977d1c127SSebastian Grimberg 
1100e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
110177d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1102e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1103ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1104ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
110577d1c127SSebastian Grimberg 
1106e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
1107e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * 3 * elem_size, &block_curl_orients));
1108e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
1109e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadCurlOrients(curl_orients, block_curl_orients, num_block, num_elem, block_size, 3 * elem_size));
111077d1c127SSebastian Grimberg 
1111fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
1112fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
111377d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
111477d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
111577d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
111677d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
111777d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
111877d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
11191203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1120e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
1121e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
1122fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
1123e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL,
1124e7f679fcSJeremy L Thompson                                               (const CeedInt8 *)block_curl_orients, *rstr));
11251c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
112677d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
112777d1c127SSebastian Grimberg }
112877d1c127SSebastian Grimberg 
112977d1c127SSebastian Grimberg /**
1130ca94c3ddSJeremy L Thompson   @brief Create a blocked strided `CeedElemRestriction`, typically only used by backends
11317509a596Sjeremylt 
1132ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
1133ea61e9acSJeremy L Thompson   @param[in]  num_elem    Number of elements described by the restriction
1134ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
1135e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
1136ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
1137fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
1138fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
1139ca94c3ddSJeremy L Thompson   @param[in]  strides     Array for strides between `[nodes, components, elements]`.
1140ca94c3ddSJeremy L Thompson                             Data for node `i`, component `j`, element `k` can be found in the L-vector at index `i*strides[0] + j*strides[1] +k*strides[2]`.
1141ca94c3ddSJeremy L Thompson                             @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
1142ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
11437509a596Sjeremylt 
11447509a596Sjeremylt   @return An error code: 0 - success, otherwise - failure
11457509a596Sjeremylt 
11467a982d89SJeremy L. Thompson   @ref User
11477509a596Sjeremylt **/
1148e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedSize l_size,
11498621c6c6SJeremy L Thompson                                             const CeedInt strides[3], CeedElemRestriction *rstr) {
1150e7f679fcSJeremy L Thompson   CeedInt num_block = (num_elem / block_size) + !!(num_elem % block_size);
11517509a596Sjeremylt 
11527509a596Sjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
11537509a596Sjeremylt     Ceed delegate;
11546574a04fSJeremy L Thompson 
11552b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1156ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedStrided");
1157e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedStrided(delegate, num_elem, elem_size, block_size, num_comp, l_size, strides, rstr));
11589bc66399SJeremy L Thompson     CeedCall(CeedDestroy(&delegate));
1159e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
11607509a596Sjeremylt   }
11617509a596Sjeremylt 
1162e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
11636574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1164e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1165ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1166aa72de07SJeremy L Thompson   CeedCheck(l_size >= (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp, ceed, CEED_ERROR_DIMENSION,
1167870ea2d9SJeremy L Thompson             "L-vector size must be at least num_elem * elem_size * num_comp. Expected: > %" CeedSize_FMT " Found: %" CeedSize_FMT,
1168870ea2d9SJeremy L Thompson             (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp, l_size);
1169e022e1f8SJeremy L Thompson 
11702b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
1171db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
1172d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
1173d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
1174d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
1175d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
1176d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
11771203703bSJeremy L Thompson   (*rstr)->e_size     = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1178e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_block;
1179e7f679fcSJeremy L Thompson   (*rstr)->block_size = block_size;
1180fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
11812b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
11822b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
1183fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
1184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11857509a596Sjeremylt }
11867509a596Sjeremylt 
11877509a596Sjeremylt /**
1188ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unsigned version.
1189c17ec2beSJeremy L Thompson 
1190ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
1191c17ec2beSJeremy L Thompson 
1192ca94c3ddSJeremy L Thompson   @param[in]     rstr          `CeedElemRestriction` to create unsigned reference to
1193ca94c3ddSJeremy L Thompson   @param[in,out] rstr_unsigned Variable to store unsigned `CeedElemRestriction`
1194c17ec2beSJeremy L Thompson 
1195c17ec2beSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1196c17ec2beSJeremy L Thompson 
1197c17ec2beSJeremy L Thompson   @ref User
1198c17ec2beSJeremy L Thompson **/
1199c17ec2beSJeremy L Thompson int CeedElemRestrictionCreateUnsignedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unsigned) {
1200c17ec2beSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr_unsigned));
1201c17ec2beSJeremy L Thompson 
1202c17ec2beSJeremy L Thompson   // Copy old rstr
1203c17ec2beSJeremy L Thompson   memcpy(*rstr_unsigned, rstr, sizeof(struct CeedElemRestriction_private));
1204c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ceed = NULL;
1205c17ec2beSJeremy L Thompson   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unsigned)->ceed));
1206c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ref_count = 1;
1207c17ec2beSJeremy L Thompson   (*rstr_unsigned)->strides   = NULL;
1208c17ec2beSJeremy L Thompson   if (rstr->strides) {
1209c17ec2beSJeremy L Thompson     CeedCall(CeedMalloc(3, &(*rstr_unsigned)->strides));
1210c17ec2beSJeremy L Thompson     for (CeedInt i = 0; i < 3; i++) (*rstr_unsigned)->strides[i] = rstr->strides[i];
1211c17ec2beSJeremy L Thompson   }
12127c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unsigned)->rstr_base));
1213c17ec2beSJeremy L Thompson 
1214c17ec2beSJeremy L Thompson   // Override Apply
1215c17ec2beSJeremy L Thompson   (*rstr_unsigned)->Apply = rstr->ApplyUnsigned;
1216c17ec2beSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1217c17ec2beSJeremy L Thompson }
1218c17ec2beSJeremy L Thompson 
1219c17ec2beSJeremy L Thompson /**
1220ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unoriented version.
12217c1dbaffSSebastian Grimberg 
1222ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
12237c1dbaffSSebastian Grimberg 
1224ca94c3ddSJeremy L Thompson   @param[in]     rstr            `CeedElemRestriction` to create unoriented reference to
1225ca94c3ddSJeremy L Thompson   @param[in,out] rstr_unoriented Variable to store unoriented `CeedElemRestriction`
12267c1dbaffSSebastian Grimberg 
12277c1dbaffSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
12287c1dbaffSSebastian Grimberg 
12297c1dbaffSSebastian Grimberg   @ref User
12307c1dbaffSSebastian Grimberg **/
12317c1dbaffSSebastian Grimberg int CeedElemRestrictionCreateUnorientedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unoriented) {
12327c1dbaffSSebastian Grimberg   CeedCall(CeedCalloc(1, rstr_unoriented));
12337c1dbaffSSebastian Grimberg 
12347c1dbaffSSebastian Grimberg   // Copy old rstr
12357c1dbaffSSebastian Grimberg   memcpy(*rstr_unoriented, rstr, sizeof(struct CeedElemRestriction_private));
12367c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ceed = NULL;
12377c1dbaffSSebastian Grimberg   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unoriented)->ceed));
12387c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ref_count = 1;
12397c1dbaffSSebastian Grimberg   (*rstr_unoriented)->strides   = NULL;
12407c1dbaffSSebastian Grimberg   if (rstr->strides) {
12417c1dbaffSSebastian Grimberg     CeedCall(CeedMalloc(3, &(*rstr_unoriented)->strides));
12427c1dbaffSSebastian Grimberg     for (CeedInt i = 0; i < 3; i++) (*rstr_unoriented)->strides[i] = rstr->strides[i];
12437c1dbaffSSebastian Grimberg   }
12447c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unoriented)->rstr_base));
12457c1dbaffSSebastian Grimberg 
12467c1dbaffSSebastian Grimberg   // Override Apply
12477c1dbaffSSebastian Grimberg   (*rstr_unoriented)->Apply = rstr->ApplyUnoriented;
12487c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
12497c1dbaffSSebastian Grimberg }
12507c1dbaffSSebastian Grimberg 
12517c1dbaffSSebastian Grimberg /**
1252ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction`.
12539fd66db6SSebastian Grimberg 
1254ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
12559560d06aSjeremylt 
1256ca94c3ddSJeremy L Thompson   Note: If the value of `*rstr_copy` passed into this function is non-`NULL`, then it is assumed that `*rstr_copy` is a pointer to a `CeedElemRestriction`.
1257ca94c3ddSJeremy L Thompson         This `CeedElemRestriction` will be destroyed if `*rstr_copy` is the only reference to this `CeedElemRestriction`.
1258ea61e9acSJeremy L Thompson 
1259ca94c3ddSJeremy L Thompson   @param[in]     rstr      `CeedElemRestriction` to copy reference to
1260ea61e9acSJeremy L Thompson   @param[in,out] rstr_copy Variable to store copied reference
12619560d06aSjeremylt 
12629560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
12639560d06aSjeremylt 
12649560d06aSjeremylt   @ref User
12659560d06aSjeremylt **/
12662b730f8bSJeremy L Thompson int CeedElemRestrictionReferenceCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_copy) {
1267393ac2cdSJeremy L Thompson   if (rstr != CEED_ELEMRESTRICTION_NONE) CeedCall(CeedElemRestrictionReference(rstr));
12682b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(rstr_copy));
12699560d06aSjeremylt   *rstr_copy = rstr;
12709560d06aSjeremylt   return CEED_ERROR_SUCCESS;
12719560d06aSjeremylt }
12729560d06aSjeremylt 
12739560d06aSjeremylt /**
1274ca94c3ddSJeremy L Thompson   @brief Create `CeedVector` associated with a `CeedElemRestriction`
1275b11c1e72Sjeremylt 
1276ca94c3ddSJeremy L Thompson   @param[in]  rstr  `CeedElemRestriction`
1277ca94c3ddSJeremy L Thompson   @param[out] l_vec The address of the L-vector to be created, or `NULL`
1278ca94c3ddSJeremy L Thompson   @param[out] e_vec The address of the E-vector to be created, or `NULL`
1279b11c1e72Sjeremylt 
1280b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1281dfdf5a53Sjeremylt 
12827a982d89SJeremy L. Thompson   @ref User
1283b11c1e72Sjeremylt **/
12842b730f8bSJeremy L Thompson int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, CeedVector *e_vec) {
1285d2643443SJeremy L Thompson   CeedSize e_size, l_size;
12861203703bSJeremy L Thompson   Ceed     ceed;
128748acf710SJeremy L Thompson 
12881203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
12891203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
12901203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
12911203703bSJeremy L Thompson   if (l_vec) CeedCall(CeedVectorCreate(ceed, l_size, l_vec));
12921203703bSJeremy L Thompson   if (e_vec) CeedCall(CeedVectorCreate(ceed, e_size, e_vec));
12939bc66399SJeremy L Thompson   CeedCall(CeedDestroy(&ceed));
1294e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1295d7b241e6Sjeremylt }
1296d7b241e6Sjeremylt 
1297d7b241e6Sjeremylt /**
1298d9e1f99aSValeria Barra   @brief Restrict an L-vector to an E-vector or apply its transpose
1299d7b241e6Sjeremylt 
1300ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1301ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1302ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1303ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape `[num_elem * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1304fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1305ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1306b11c1e72Sjeremylt 
1307b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1308dfdf5a53Sjeremylt 
13097a982d89SJeremy L. Thompson   @ref User
1310b11c1e72Sjeremylt **/
13112b730f8bSJeremy L Thompson int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request) {
1312701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1313701e7d35SJeremy L Thompson   CeedInt  num_elem;
1314d7b241e6Sjeremylt 
1315d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1316701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_ru_len));
1317701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1318d7b241e6Sjeremylt   } else {
1319701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_u_len));
1320701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1321d7b241e6Sjeremylt   }
1322701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
13239bc66399SJeremy L Thompson   CeedCheck(min_u_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1324701e7d35SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1325701e7d35SJeremy L Thompson             min_u_len);
1326701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
13279bc66399SJeremy L Thompson   CeedCheck(min_ru_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1328701e7d35SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1329701e7d35SJeremy L Thompson             min_ru_len);
1330701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1331701e7d35SJeremy L Thompson   if (num_elem > 0) CeedCall(rstr->Apply(rstr, t_mode, u, ru, request));
1332e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1333d7b241e6Sjeremylt }
1334d7b241e6Sjeremylt 
1335d7b241e6Sjeremylt /**
13363ac8f562SJeremy L Thompson   @brief Restrict an L-vector of points to a single element or apply its transpose
13373ac8f562SJeremy L Thompson 
1338ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1339ca94c3ddSJeremy L Thompson   @param[in]  elem    Element number in range `[0, num_elem)`
13403ac8f562SJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1341ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1342ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape [`num_points * num_comp]` when `t_mode` = @ref CEED_NOTRANSPOSE).
13433ac8f562SJeremy L Thompson                         Ordering of the e-vector is decided by the backend.
13443ac8f562SJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
13453ac8f562SJeremy L Thompson 
13463ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
13473ac8f562SJeremy L Thompson 
13483ac8f562SJeremy L Thompson   @ref User
13493ac8f562SJeremy L Thompson **/
135005fa913cSJeremy L Thompson int CeedElemRestrictionApplyAtPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
13513ac8f562SJeremy L Thompson                                               CeedRequest *request) {
1352701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1353701e7d35SJeremy L Thompson   CeedInt  num_elem;
13543ac8f562SJeremy L Thompson 
13559bc66399SJeremy L Thompson   CeedCheck(rstr->ApplyAtPointsInElement, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
13569bc66399SJeremy L Thompson             "Backend does not implement CeedElemRestrictionApplyAtPointsInElement");
13571ef3a2a9SJeremy L Thompson 
13583ac8f562SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
1359701e7d35SJeremy L Thompson     CeedInt num_points, num_comp;
1360701e7d35SJeremy L Thompson 
1361701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1362701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1363701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1364701e7d35SJeremy L Thompson     min_ru_len = (CeedSize)num_points * (CeedSize)num_comp;
13653ac8f562SJeremy L Thompson   } else {
1366701e7d35SJeremy L Thompson     CeedInt num_points, num_comp;
1367701e7d35SJeremy L Thompson 
1368701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1369701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1370701e7d35SJeremy L Thompson     min_u_len = (CeedSize)num_points * (CeedSize)num_comp;
1371701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
13723ac8f562SJeremy L Thompson   }
1373701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
13749bc66399SJeremy L Thompson   CeedCheck(min_u_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
13753ac8f562SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
13763ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
1377701e7d35SJeremy L Thompson             len, min_ru_len, min_u_len, elem);
1378701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
13799bc66399SJeremy L Thompson   CeedCheck(min_ru_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
13803ac8f562SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
13813ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
1382701e7d35SJeremy L Thompson             len, min_ru_len, min_u_len, elem);
1383701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
13849bc66399SJeremy L Thompson   CeedCheck(elem < num_elem, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1385701e7d35SJeremy L Thompson             "Cannot retrieve element %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", elem, elem, num_elem);
1386701e7d35SJeremy L Thompson   if (num_elem > 0) CeedCall(rstr->ApplyAtPointsInElement(rstr, elem, t_mode, u, ru, request));
13873ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13883ac8f562SJeremy L Thompson }
13893ac8f562SJeremy L Thompson 
13903ac8f562SJeremy L Thompson /**
1391d9e1f99aSValeria Barra   @brief Restrict an L-vector to a block of an E-vector or apply its transpose
1392be9261b7Sjeremylt 
1393ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1394ca94c3ddSJeremy L Thompson   @param[in]  block   Block number to restrict to/from, i.e. `block = 0` will handle elements `[0 : block_size]` and `block = 3` will handle elements `[3*block_size : 4*block_size]`
1395ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1396ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1397ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape `[block_size * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1398fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1399ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1400be9261b7Sjeremylt 
1401be9261b7Sjeremylt   @return An error code: 0 - success, otherwise - failure
1402be9261b7Sjeremylt 
14037a982d89SJeremy L. Thompson   @ref Backend
1404be9261b7Sjeremylt **/
14052b730f8bSJeremy L Thompson int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
14062b730f8bSJeremy L Thompson                                   CeedRequest *request) {
1407701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1408701e7d35SJeremy L Thompson   CeedInt  block_size, num_elem;
1409be9261b7Sjeremylt 
14109bc66399SJeremy L Thompson   CeedCheck(rstr->ApplyBlock, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
14119bc66399SJeremy L Thompson             "Backend does not implement CeedElemRestrictionApplyBlock");
14126402da51SJeremy L Thompson 
1413701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetBlockSize(rstr, &block_size));
1414d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1415701e7d35SJeremy L Thompson     CeedInt elem_size, num_comp;
1416701e7d35SJeremy L Thompson 
1417701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1418701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1419701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1420701e7d35SJeremy L Thompson     min_ru_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1421be9261b7Sjeremylt   } else {
1422701e7d35SJeremy L Thompson     CeedInt elem_size, num_comp;
1423701e7d35SJeremy L Thompson 
1424701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1425701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1426701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1427701e7d35SJeremy L Thompson     min_u_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1428be9261b7Sjeremylt   }
1429701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
14309bc66399SJeremy L Thompson   CeedCheck(min_u_len == len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1431701e7d35SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1432701e7d35SJeremy L Thompson             min_ru_len);
1433701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
14349bc66399SJeremy L Thompson   CeedCheck(min_ru_len == len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1435701e7d35SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1436701e7d35SJeremy L Thompson             min_u_len);
1437701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
14389bc66399SJeremy L Thompson   CeedCheck(block_size * block <= num_elem, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION,
1439701e7d35SJeremy L Thompson             "Cannot retrieve block %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", block, block_size * block,
1440701e7d35SJeremy L Thompson             num_elem);
14412b730f8bSJeremy L Thompson   CeedCall(rstr->ApplyBlock(rstr, block, t_mode, u, ru, request));
1442e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1443be9261b7Sjeremylt }
1444be9261b7Sjeremylt 
1445be9261b7Sjeremylt /**
1446ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedElemRestriction`
1447b7c9bbdaSJeremy L Thompson 
1448ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
1449ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1450b7c9bbdaSJeremy L Thompson 
1451b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1452b7c9bbdaSJeremy L Thompson 
1453b7c9bbdaSJeremy L Thompson   @ref Advanced
1454b7c9bbdaSJeremy L Thompson **/
1455b7c9bbdaSJeremy L Thompson int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) {
14569bc66399SJeremy L Thompson   *ceed = NULL;
14579bc66399SJeremy L Thompson   CeedCall(CeedReferenceCopy(CeedElemRestrictionReturnCeed(rstr), ceed));
1458b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1459b7c9bbdaSJeremy L Thompson }
1460b7c9bbdaSJeremy L Thompson 
1461b7c9bbdaSJeremy L Thompson /**
14626e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedElemRestriction`
14636e536b99SJeremy L Thompson 
14646e536b99SJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
14656e536b99SJeremy L Thompson 
14666e536b99SJeremy L Thompson   @return `Ceed` associated with the `rstr`
14676e536b99SJeremy L Thompson 
14686e536b99SJeremy L Thompson   @ref Advanced
14696e536b99SJeremy L Thompson **/
14706e536b99SJeremy L Thompson Ceed CeedElemRestrictionReturnCeed(CeedElemRestriction rstr) { return rstr->ceed; }
14716e536b99SJeremy L Thompson 
14726e536b99SJeremy L Thompson /**
1473d979a051Sjeremylt   @brief Get the L-vector component stride
1474a681ae63Sjeremylt 
1475ca94c3ddSJeremy L Thompson   @param[in]  rstr        `CeedElemRestriction`
1476d1d35e2fSjeremylt   @param[out] comp_stride Variable to store component stride
1477a681ae63Sjeremylt 
1478a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1479a681ae63Sjeremylt 
1480b7c9bbdaSJeremy L Thompson   @ref Advanced
1481a681ae63Sjeremylt **/
14822b730f8bSJeremy L Thompson int CeedElemRestrictionGetCompStride(CeedElemRestriction rstr, CeedInt *comp_stride) {
1483d1d35e2fSjeremylt   *comp_stride = rstr->comp_stride;
1484e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1485a681ae63Sjeremylt }
1486a681ae63Sjeremylt 
1487a681ae63Sjeremylt /**
1488ca94c3ddSJeremy L Thompson   @brief Get the total number of elements in the range of a `CeedElemRestriction`
1489a681ae63Sjeremylt 
1490ca94c3ddSJeremy L Thompson   @param[in] rstr      `CeedElemRestriction`
1491d1d35e2fSjeremylt   @param[out] num_elem Variable to store number of elements
1492a681ae63Sjeremylt 
1493a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1494a681ae63Sjeremylt 
1495b7c9bbdaSJeremy L Thompson   @ref Advanced
1496a681ae63Sjeremylt **/
14972b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr, CeedInt *num_elem) {
1498d1d35e2fSjeremylt   *num_elem = rstr->num_elem;
1499e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1500a681ae63Sjeremylt }
1501a681ae63Sjeremylt 
1502a681ae63Sjeremylt /**
1503ca94c3ddSJeremy L Thompson   @brief Get the size of elements in the `CeedElemRestriction`
1504a681ae63Sjeremylt 
1505ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
1506d1d35e2fSjeremylt   @param[out] elem_size Variable to store size of elements
1507a681ae63Sjeremylt 
1508a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1509a681ae63Sjeremylt 
1510b7c9bbdaSJeremy L Thompson   @ref Advanced
1511a681ae63Sjeremylt **/
15122b730f8bSJeremy L Thompson int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr, CeedInt *elem_size) {
1513d1d35e2fSjeremylt   *elem_size = rstr->elem_size;
15142c7e7413SJeremy L Thompson   return CEED_ERROR_SUCCESS;
15152c7e7413SJeremy L Thompson }
15162c7e7413SJeremy L Thompson 
15172c7e7413SJeremy L Thompson /**
151807d5dec1SJeremy L Thompson 
1519725a297dSZach Atkins   @brief Get the number of points in the offsets array for a points `CeedElemRestriction`
152007d5dec1SJeremy L Thompson 
1521ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
1522725a297dSZach Atkins   @param[out] num_points The number of points in the offsets array
152307d5dec1SJeremy L Thompson 
152407d5dec1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
152507d5dec1SJeremy L Thompson 
1526c63574e3SJeremy L Thompson   @ref User
152707d5dec1SJeremy L Thompson **/
152807d5dec1SJeremy L Thompson int CeedElemRestrictionGetNumPoints(CeedElemRestriction rstr, CeedInt *num_points) {
15291203703bSJeremy L Thompson   CeedRestrictionType rstr_type;
153007d5dec1SJeremy L Thompson 
15311203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
15326e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
153307d5dec1SJeremy L Thompson             "Can only retrieve the number of points for a points CeedElemRestriction");
153407d5dec1SJeremy L Thompson 
153507d5dec1SJeremy L Thompson   *num_points = rstr->num_points;
153607d5dec1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
153707d5dec1SJeremy L Thompson }
153807d5dec1SJeremy L Thompson 
153907d5dec1SJeremy L Thompson /**
154007d5dec1SJeremy L Thompson 
1541ca94c3ddSJeremy L Thompson   @brief Get the number of points in an element of a `CeedElemRestriction` at points
154207d5dec1SJeremy L Thompson 
1543ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
154407d5dec1SJeremy L Thompson   @param[in]  elem       Index number of element to retrieve the number of points for
154507d5dec1SJeremy L Thompson   @param[out] num_points The number of points in the element at index elem
154607d5dec1SJeremy L Thompson 
154707d5dec1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
154807d5dec1SJeremy L Thompson 
1549c63574e3SJeremy L Thompson   @ref User
155007d5dec1SJeremy L Thompson **/
155107d5dec1SJeremy L Thompson int CeedElemRestrictionGetNumPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedInt *num_points) {
15521203703bSJeremy L Thompson   CeedRestrictionType rstr_type;
155307d5dec1SJeremy L Thompson   const CeedInt      *offsets;
155407d5dec1SJeremy L Thompson 
15551203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
15566e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
155707d5dec1SJeremy L Thompson             "Can only retrieve the number of points for a points CeedElemRestriction");
155807d5dec1SJeremy L Thompson 
155907d5dec1SJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
156007d5dec1SJeremy L Thompson   *num_points = offsets[elem + 1] - offsets[elem];
156107d5dec1SJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
156207d5dec1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
156307d5dec1SJeremy L Thompson }
156407d5dec1SJeremy L Thompson 
156507d5dec1SJeremy L Thompson /**
1566a4065bfbSZach Atkins   @brief Get the minimum and/or maximum number of points in an element for a `CeedElemRestriction` at points
15672c7e7413SJeremy L Thompson 
1568ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
1569a4065bfbSZach Atkins   @param[out] min_points Variable to minimum number of points in an element, or `NULL`
1570a4065bfbSZach Atkins   @param[out] max_points Variable to maximum number of points in an element, or `NULL`
15712c7e7413SJeremy L Thompson 
15722c7e7413SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
15732c7e7413SJeremy L Thompson 
15742c7e7413SJeremy L Thompson   @ref Advanced
15752c7e7413SJeremy L Thompson **/
1576a4065bfbSZach Atkins int CeedElemRestrictionGetMinMaxPointsInElement(CeedElemRestriction rstr, CeedInt *min_points, CeedInt *max_points) {
1577a4065bfbSZach Atkins   CeedInt             num_elem, num_points;
15782c7e7413SJeremy L Thompson   CeedRestrictionType rstr_type;
15792c7e7413SJeremy L Thompson 
15802c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
15816e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
1582a4065bfbSZach Atkins             "Cannot compute min/max points for a CeedElemRestriction that does not use points");
15832c7e7413SJeremy L Thompson 
15842c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
15852c7e7413SJeremy L Thompson 
1586a4065bfbSZach Atkins   // Exit early if there are no elements
1587a4065bfbSZach Atkins   if (num_elem == 0) {
1588a4065bfbSZach Atkins     if (min_points) *min_points = 0;
1589a4065bfbSZach Atkins     if (max_points) *max_points = 0;
1590a4065bfbSZach Atkins     return CEED_ERROR_SUCCESS;
1591a4065bfbSZach Atkins   }
1592a4065bfbSZach Atkins 
1593a4065bfbSZach Atkins   // Initialize to the number of points in the first element
1594a4065bfbSZach Atkins   CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, 0, &num_points));
1595a4065bfbSZach Atkins   if (min_points) *min_points = num_points;
1596a4065bfbSZach Atkins   if (max_points) *max_points = num_points;
1597a4065bfbSZach Atkins   for (CeedInt e = 1; e < num_elem; e++) {
15982c7e7413SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, e, &num_points));
1599a4065bfbSZach Atkins     if (min_points) *min_points = CeedIntMin(num_points, *min_points);
1600a4065bfbSZach Atkins     if (max_points) *max_points = CeedIntMax(num_points, *max_points);
16012c7e7413SJeremy L Thompson   }
1602e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1603a681ae63Sjeremylt }
1604a681ae63Sjeremylt 
1605a681ae63Sjeremylt /**
1606a4065bfbSZach Atkins   @brief Get the maximum number of points in an element for a `CeedElemRestriction` at points
1607a4065bfbSZach Atkins 
1608a4065bfbSZach Atkins   @param[in]  rstr       `CeedElemRestriction`
1609a4065bfbSZach Atkins   @param[out] max_points Variable to store maximum number of points in an element
1610a4065bfbSZach Atkins 
1611a4065bfbSZach Atkins   @return An error code: 0 - success, otherwise - failure
1612a4065bfbSZach Atkins 
1613a4065bfbSZach Atkins   @ref User
1614a4065bfbSZach Atkins 
1615a4065bfbSZach Atkins   @see CeedElemRestrictionGetMinMaxPointsInElement()
1616a4065bfbSZach Atkins **/
1617a4065bfbSZach Atkins int CeedElemRestrictionGetMaxPointsInElement(CeedElemRestriction rstr, CeedInt *max_points) {
1618a4065bfbSZach Atkins   return CeedElemRestrictionGetMinMaxPointsInElement(rstr, NULL, max_points);
1619a4065bfbSZach Atkins }
1620a4065bfbSZach Atkins 
1621a4065bfbSZach Atkins /**
1622a4065bfbSZach Atkins   @brief Get the minimum number of points in an element for a `CeedElemRestriction` at points
1623a4065bfbSZach Atkins 
1624a4065bfbSZach Atkins   @param[in]  rstr       `CeedElemRestriction`
1625a4065bfbSZach Atkins   @param[out] min_points Variable to store minimum number of points in an element
1626a4065bfbSZach Atkins 
1627a4065bfbSZach Atkins   @return An error code: 0 - success, otherwise - failure
1628a4065bfbSZach Atkins 
1629a4065bfbSZach Atkins   @ref User
1630a4065bfbSZach Atkins 
1631a4065bfbSZach Atkins   @see CeedElemRestrictionGetMinMaxPointsInElement()
1632a4065bfbSZach Atkins **/
1633a4065bfbSZach Atkins int CeedElemRestrictionGetMinPointsInElement(CeedElemRestriction rstr, CeedInt *min_points) {
1634a4065bfbSZach Atkins   return CeedElemRestrictionGetMinMaxPointsInElement(rstr, min_points, NULL);
1635a4065bfbSZach Atkins }
1636a4065bfbSZach Atkins 
1637a4065bfbSZach Atkins /**
1638ca94c3ddSJeremy L Thompson   @brief Get the size of the l-vector for a `CeedElemRestriction`
1639a681ae63Sjeremylt 
1640ca94c3ddSJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction`
1641701e7d35SJeremy L Thompson   @param[out] l_size Variable to store l-vector size
1642a681ae63Sjeremylt 
1643a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1644a681ae63Sjeremylt 
1645b7c9bbdaSJeremy L Thompson   @ref Advanced
1646a681ae63Sjeremylt **/
16472b730f8bSJeremy L Thompson int CeedElemRestrictionGetLVectorSize(CeedElemRestriction rstr, CeedSize *l_size) {
1648d1d35e2fSjeremylt   *l_size = rstr->l_size;
1649e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1650a681ae63Sjeremylt }
1651a681ae63Sjeremylt 
1652a681ae63Sjeremylt /**
1653701e7d35SJeremy L Thompson   @brief Get the size of the e-vector for a `CeedElemRestriction`
1654701e7d35SJeremy L Thompson 
1655701e7d35SJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction`
1656701e7d35SJeremy L Thompson   @param[out] e_size Variable to store e-vector size
1657701e7d35SJeremy L Thompson 
1658701e7d35SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1659701e7d35SJeremy L Thompson 
1660701e7d35SJeremy L Thompson   @ref Advanced
1661701e7d35SJeremy L Thompson **/
1662701e7d35SJeremy L Thompson int CeedElemRestrictionGetEVectorSize(CeedElemRestriction rstr, CeedSize *e_size) {
1663701e7d35SJeremy L Thompson   *e_size = rstr->e_size;
1664701e7d35SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1665701e7d35SJeremy L Thompson }
1666701e7d35SJeremy L Thompson 
1667701e7d35SJeremy L Thompson /**
1668ca94c3ddSJeremy L Thompson   @brief Get the number of components in the elements of a `CeedElemRestriction`
1669a681ae63Sjeremylt 
1670ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction`
1671d1d35e2fSjeremylt   @param[out] num_comp Variable to store number of components
1672a681ae63Sjeremylt 
1673a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1674a681ae63Sjeremylt 
1675b7c9bbdaSJeremy L Thompson   @ref Advanced
1676a681ae63Sjeremylt **/
16772b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr, CeedInt *num_comp) {
1678d1d35e2fSjeremylt   *num_comp = rstr->num_comp;
1679e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1680a681ae63Sjeremylt }
1681a681ae63Sjeremylt 
1682a681ae63Sjeremylt /**
1683ca94c3ddSJeremy L Thompson   @brief Get the number of blocks in a `CeedElemRestriction`
1684a681ae63Sjeremylt 
1685ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
1686d1d35e2fSjeremylt   @param[out] num_block Variable to store number of blocks
1687a681ae63Sjeremylt 
1688a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1689a681ae63Sjeremylt 
1690b7c9bbdaSJeremy L Thompson   @ref Advanced
1691a681ae63Sjeremylt **/
16922b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr, CeedInt *num_block) {
1693e7f679fcSJeremy L Thompson   *num_block = rstr->num_block;
1694e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1695a681ae63Sjeremylt }
1696a681ae63Sjeremylt 
1697a681ae63Sjeremylt /**
1698ca94c3ddSJeremy L Thompson   @brief Get the size of blocks in the `CeedElemRestriction`
1699a681ae63Sjeremylt 
1700ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
1701e7f679fcSJeremy L Thompson   @param[out] block_size Variable to store size of blocks
1702a681ae63Sjeremylt 
1703a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1704a681ae63Sjeremylt 
1705b7c9bbdaSJeremy L Thompson   @ref Advanced
1706a681ae63Sjeremylt **/
1707e7f679fcSJeremy L Thompson int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr, CeedInt *block_size) {
1708e7f679fcSJeremy L Thompson   *block_size = rstr->block_size;
1709e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1710a681ae63Sjeremylt }
1711a681ae63Sjeremylt 
1712a681ae63Sjeremylt /**
1713ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes in a `CeedElemRestriction`
17141469ee4dSjeremylt 
1715ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
1716ca94c3ddSJeremy L Thompson   @param[out] mult Vector to store multiplicity (of size `l_size`)
17171469ee4dSjeremylt 
17181469ee4dSjeremylt   @return An error code: 0 - success, otherwise - failure
17191469ee4dSjeremylt 
17207a982d89SJeremy L. Thompson   @ref User
17211469ee4dSjeremylt **/
17222b730f8bSJeremy L Thompson int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr, CeedVector mult) {
1723d1d35e2fSjeremylt   CeedVector e_vec;
17241469ee4dSjeremylt 
172525509ebbSRezgar Shakeri   // Create e_vec to hold intermediate computation in E^T (E 1)
17262b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(rstr, NULL, &e_vec));
17271469ee4dSjeremylt 
172825509ebbSRezgar Shakeri   // Compute e_vec = E * 1
17292b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 1.0));
17302b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, mult, e_vec, CEED_REQUEST_IMMEDIATE));
173125509ebbSRezgar Shakeri   // Compute multiplicity, mult = E^T * e_vec = E^T (E 1)
17322b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
17332b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, e_vec, mult, CEED_REQUEST_IMMEDIATE));
17341469ee4dSjeremylt   // Cleanup
17352b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&e_vec));
1736e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17371469ee4dSjeremylt }
17381469ee4dSjeremylt 
17391469ee4dSjeremylt /**
1740*4c789ea2SJeremy L Thompson   @brief Set the number of tabs to indent for @ref CeedElemRestrictionView() output
1741*4c789ea2SJeremy L Thompson 
1742*4c789ea2SJeremy L Thompson   @param[in] rstr     `CeedElemRestriction` to set the number of view tabs
1743*4c789ea2SJeremy L Thompson   @param[in] num_tabs Number of view tabs to set
1744*4c789ea2SJeremy L Thompson 
1745*4c789ea2SJeremy L Thompson   @return Error code: 0 - success, otherwise - failure
1746*4c789ea2SJeremy L Thompson 
1747*4c789ea2SJeremy L Thompson   @ref User
1748*4c789ea2SJeremy L Thompson **/
1749*4c789ea2SJeremy L Thompson int CeedElemRestrictionSetNumViewTabs(CeedElemRestriction rstr, CeedInt num_tabs) {
1750*4c789ea2SJeremy L Thompson   CeedCheck(num_tabs >= 0, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "Number of view tabs must be non-negative");
1751*4c789ea2SJeremy L Thompson   rstr->num_tabs = num_tabs;
1752*4c789ea2SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1753*4c789ea2SJeremy L Thompson }
1754*4c789ea2SJeremy L Thompson 
1755*4c789ea2SJeremy L Thompson /**
1756ca94c3ddSJeremy L Thompson   @brief View a `CeedElemRestriction`
1757f02ca4a2SJed Brown 
1758ca94c3ddSJeremy L Thompson   @param[in] rstr   `CeedElemRestriction` to view
1759ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1760f02ca4a2SJed Brown 
1761f02ca4a2SJed Brown   @return Error code: 0 - success, otherwise - failure
1762f02ca4a2SJed Brown 
17637a982d89SJeremy L. Thompson   @ref User
1764f02ca4a2SJed Brown **/
1765f02ca4a2SJed Brown int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream) {
1766*4c789ea2SJeremy L Thompson   char               *tabs = NULL;
17670930e4e7SJeremy L Thompson   CeedRestrictionType rstr_type;
17680930e4e7SJeremy L Thompson 
1769*4c789ea2SJeremy L Thompson   {
1770*4c789ea2SJeremy L Thompson     CeedInt num_tabs = 0;
1771*4c789ea2SJeremy L Thompson 
1772*4c789ea2SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumViewTabs(rstr, &num_tabs));
1773*4c789ea2SJeremy L Thompson     CeedCall(CeedCalloc(CEED_TAB_WIDTH * num_tabs + 1, &tabs));
1774*4c789ea2SJeremy L Thompson     for (CeedInt i = 0; i < CEED_TAB_WIDTH * num_tabs; i++) tabs[i] = ' ';
1775*4c789ea2SJeremy L Thompson   }
1776*4c789ea2SJeremy L Thompson 
17770930e4e7SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
17780930e4e7SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
17790930e4e7SJeremy L Thompson     CeedInt max_points;
17800930e4e7SJeremy L Thompson 
17810930e4e7SJeremy L Thompson     CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr, &max_points));
17820930e4e7SJeremy L Thompson     fprintf(stream,
1783*4c789ea2SJeremy L Thompson             "%sCeedElemRestriction at points from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with a maximum of %" CeedInt_FMT
17840930e4e7SJeremy L Thompson             " points on an element\n",
1785*4c789ea2SJeremy L Thompson             tabs, rstr->l_size, rstr->num_comp, rstr->num_elem, max_points);
17860930e4e7SJeremy L Thompson   } else {
1787249f8407SJeremy L Thompson     char strides_str[500];
17881c66c397SJeremy L Thompson 
17892b730f8bSJeremy L Thompson     if (rstr->strides) {
1790249f8407SJeremy L Thompson       sprintf(strides_str, "[%" CeedInt_FMT ", %" CeedInt_FMT ", %" CeedInt_FMT "]", rstr->strides[0], rstr->strides[1], rstr->strides[2]);
17912b730f8bSJeremy L Thompson     } else {
1792249f8407SJeremy L Thompson       sprintf(strides_str, "%" CeedInt_FMT, rstr->comp_stride);
17932b730f8bSJeremy L Thompson     }
1794249f8407SJeremy L Thompson     fprintf(stream,
1795*4c789ea2SJeremy L Thompson             "%s%sCeedElemRestriction from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with %" CeedInt_FMT
1796249f8407SJeremy L Thompson             " nodes each and %s %s\n",
1797*4c789ea2SJeremy L Thompson             tabs, rstr->block_size > 1 ? "Blocked " : "", rstr->l_size, rstr->num_comp, rstr->num_elem, rstr->elem_size,
1798249f8407SJeremy L Thompson             rstr->strides ? "strides" : "component stride", strides_str);
17990930e4e7SJeremy L Thompson   }
1800*4c789ea2SJeremy L Thompson   CeedCall(CeedFree(&tabs));
1801e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1802f02ca4a2SJed Brown }
1803f02ca4a2SJed Brown 
1804f02ca4a2SJed Brown /**
1805ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedElemRestriction`
1806b11c1e72Sjeremylt 
1807ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction` to destroy
1808b11c1e72Sjeremylt 
1809b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1810dfdf5a53Sjeremylt 
18117a982d89SJeremy L. Thompson   @ref User
1812b11c1e72Sjeremylt **/
18134ce2993fSjeremylt int CeedElemRestrictionDestroy(CeedElemRestriction *rstr) {
1814393ac2cdSJeremy L Thompson   if (!*rstr || *rstr == CEED_ELEMRESTRICTION_NONE || --(*rstr)->ref_count > 0) {
1815ad6481ceSJeremy L Thompson     *rstr = NULL;
1816ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1817ad6481ceSJeremy L Thompson   }
18186574a04fSJeremy L Thompson   CeedCheck((*rstr)->num_readers == 0, (*rstr)->ceed, CEED_ERROR_ACCESS,
18196574a04fSJeremy L Thompson             "Cannot destroy CeedElemRestriction, a process has read access to the offset data");
1820c17ec2beSJeremy L Thompson 
1821c17ec2beSJeremy L Thompson   // Only destroy backend data once between rstr and unsigned copy
18227c1dbaffSSebastian Grimberg   if ((*rstr)->rstr_base) CeedCall(CeedElemRestrictionDestroy(&(*rstr)->rstr_base));
1823c17ec2beSJeremy L Thompson   else if ((*rstr)->Destroy) CeedCall((*rstr)->Destroy(*rstr));
1824c17ec2beSJeremy L Thompson 
18252b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*rstr)->strides));
18262b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*rstr)->ceed));
18272b730f8bSJeremy L Thompson   CeedCall(CeedFree(rstr));
1828e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1829d7b241e6Sjeremylt }
1830d7b241e6Sjeremylt 
1831d7b241e6Sjeremylt /// @}
1832