xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-elemrestriction.c (revision 6e536b992ff6bc401c55631f1bc4464446496b52)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
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 /**
125ca94c3ddSJeremy L Thompson   @brief Get the strided status of a `CeedElemRestriction`
126fcbe8c06SSebastian Grimberg 
127ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
128ca94c3ddSJeremy L Thompson   @param[out] is_strided Variable to store strided status
129ca94c3ddSJeremy L Thompson 
130ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
131ca94c3ddSJeremy L Thompson 
132ca94c3ddSJeremy L Thompson   @ref Backend
133fcbe8c06SSebastian Grimberg **/
134fcbe8c06SSebastian Grimberg int CeedElemRestrictionIsStrided(CeedElemRestriction rstr, bool *is_strided) {
135fcbe8c06SSebastian Grimberg   *is_strided = (rstr->rstr_type == CEED_RESTRICTION_STRIDED);
136fcbe8c06SSebastian Grimberg   return CEED_ERROR_SUCCESS;
137fcbe8c06SSebastian Grimberg }
138fcbe8c06SSebastian Grimberg 
139fcbe8c06SSebastian Grimberg /**
140ca94c3ddSJeremy L Thompson   @brief Get the points status of a `CeedElemRestriction`
1413ac8f562SJeremy L Thompson 
142ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
143ca94c3ddSJeremy L Thompson   @param[out] is_points Variable to store points status
144ca94c3ddSJeremy L Thompson 
145ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
146ca94c3ddSJeremy L Thompson 
147ca94c3ddSJeremy L Thompson   @ref Backend
1483ac8f562SJeremy L Thompson **/
1493ac8f562SJeremy L Thompson int CeedElemRestrictionIsPoints(CeedElemRestriction rstr, bool *is_points) {
1503ac8f562SJeremy L Thompson   *is_points = (rstr->rstr_type == CEED_RESTRICTION_POINTS);
1513ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1523ac8f562SJeremy L Thompson }
1533ac8f562SJeremy L Thompson 
1543ac8f562SJeremy L Thompson /**
155ca94c3ddSJeremy L Thompson   @brief Check if two `CeedElemRestriction` created with @ref CeedElemRestrictionCreateAtPoints() and use the same points per element
15648acf710SJeremy L Thompson 
157ca94c3ddSJeremy L Thompson   @param[in]  rstr_a         First `CeedElemRestriction`
158ca94c3ddSJeremy L Thompson   @param[in]  rstr_b         Second `CeedElemRestriction`
15948acf710SJeremy L Thompson   @param[out] are_compatible Variable to store compatibility status
160ca94c3ddSJeremy L Thompson 
161ca94c3ddSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
162ca94c3ddSJeremy L Thompson 
163ca94c3ddSJeremy L Thompson   @ref Backend
16448acf710SJeremy L Thompson **/
16548acf710SJeremy L Thompson int CeedElemRestrictionAtPointsAreCompatible(CeedElemRestriction rstr_a, CeedElemRestriction rstr_b, bool *are_compatible) {
16648acf710SJeremy L Thompson   CeedInt num_elem_a, num_elem_b, num_points_a, num_points_b;
1671203703bSJeremy L Thompson   Ceed    ceed;
1681203703bSJeremy L Thompson 
1691203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr_a, &ceed));
17048acf710SJeremy L Thompson 
17148acf710SJeremy L Thompson   // Cannot compare non-points restrictions
1721203703bSJeremy L Thompson   CeedCheck(rstr_a->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "First CeedElemRestriction must be AtPoints");
1731203703bSJeremy L Thompson   CeedCheck(rstr_b->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "Second CeedElemRestriction must be AtPoints");
17448acf710SJeremy L Thompson 
17548acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_a, &num_elem_a));
17648acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr_b, &num_elem_b));
17748acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumPoints(rstr_a, &num_points_a));
17848acf710SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumPoints(rstr_b, &num_points_b));
17948acf710SJeremy L Thompson 
18048acf710SJeremy L Thompson   // Check size and contents of offsets arrays
18148acf710SJeremy L Thompson   *are_compatible = true;
18248acf710SJeremy L Thompson   if (num_elem_a != num_elem_b) *are_compatible = false;
18348acf710SJeremy L Thompson   if (num_points_a != num_points_b) *are_compatible = false;
18448acf710SJeremy L Thompson   if (*are_compatible) {
18548acf710SJeremy L Thompson     const CeedInt *offsets_a, *offsets_b;
18648acf710SJeremy L Thompson 
18748acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionGetOffsets(rstr_a, CEED_MEM_HOST, &offsets_a));
18848acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionGetOffsets(rstr_b, CEED_MEM_HOST, &offsets_b));
18948acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_elem_a + 1 + num_points_a; i++) *are_compatible &= offsets_a[i] == offsets_b[i];
19048acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_a, &offsets_a));
19148acf710SJeremy L Thompson     CeedCall(CeedElemRestrictionRestoreOffsets(rstr_b, &offsets_b));
19248acf710SJeremy L Thompson   }
19348acf710SJeremy L Thompson   return CEED_ERROR_SUCCESS;
19448acf710SJeremy L Thompson }
19548acf710SJeremy L Thompson 
19648acf710SJeremy L Thompson /**
197ca94c3ddSJeremy L Thompson   @brief Get the strides of a strided `CeedElemRestriction`
1987a982d89SJeremy L. Thompson 
199ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
200a681ae63Sjeremylt   @param[out] strides Variable to store strides array
2017a982d89SJeremy L. Thompson 
2027a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
2037a982d89SJeremy L. Thompson 
2047a982d89SJeremy L. Thompson   @ref Backend
2057a982d89SJeremy L. Thompson **/
20656c48462SJeremy L Thompson int CeedElemRestrictionGetStrides(CeedElemRestriction rstr, CeedInt strides[3]) {
207*6e536b99SJeremy L Thompson   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
20856c48462SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) strides[i] = rstr->strides[i];
209e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2107a982d89SJeremy L. Thompson }
2117a982d89SJeremy L. Thompson 
2127a982d89SJeremy L. Thompson /**
213ca94c3ddSJeremy L Thompson   @brief Get the backend stride status of a `CeedElemRestriction`
21477d1c127SSebastian Grimberg 
215ca94c3ddSJeremy L Thompson   @param[in]  rstr                 `CeedElemRestriction`
21677d1c127SSebastian Grimberg   @param[out] has_backend_strides  Variable to store stride status
21777d1c127SSebastian Grimberg 
21877d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
21977d1c127SSebastian Grimberg 
22077d1c127SSebastian Grimberg   @ref Backend
22177d1c127SSebastian Grimberg **/
22277d1c127SSebastian Grimberg int CeedElemRestrictionHasBackendStrides(CeedElemRestriction rstr, bool *has_backend_strides) {
223*6e536b99SJeremy L Thompson   CeedCheck(rstr->strides, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no stride data");
22477d1c127SSebastian Grimberg   *has_backend_strides = ((rstr->strides[0] == CEED_STRIDES_BACKEND[0]) && (rstr->strides[1] == CEED_STRIDES_BACKEND[1]) &&
22577d1c127SSebastian Grimberg                           (rstr->strides[2] == CEED_STRIDES_BACKEND[2]));
22677d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
22777d1c127SSebastian Grimberg }
22877d1c127SSebastian Grimberg 
22977d1c127SSebastian Grimberg /**
230ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` offsets array by @ref CeedMemType
231bd33150aSjeremylt 
232ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction` to retrieve offsets
233fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
234fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
235ca94c3ddSJeremy L Thompson   @param[out] offsets  Array on memory type `mem_type`
236bd33150aSjeremylt 
237bd33150aSjeremylt   @return An error code: 0 - success, otherwise - failure
238bd33150aSjeremylt 
239bd33150aSjeremylt   @ref User
240bd33150aSjeremylt **/
2412b730f8bSJeremy L Thompson int CeedElemRestrictionGetOffsets(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt **offsets) {
2427c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
2437c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionGetOffsets(rstr->rstr_base, mem_type, offsets));
244c17ec2beSJeremy L Thompson   } else {
245*6e536b99SJeremy L Thompson     CeedCheck(rstr->GetOffsets, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
246*6e536b99SJeremy L Thompson               "Backend does not support CeedElemRestrictionGetOffsets");
2472b730f8bSJeremy L Thompson     CeedCall(rstr->GetOffsets(rstr, mem_type, offsets));
248d1d35e2fSjeremylt     rstr->num_readers++;
249c17ec2beSJeremy L Thompson   }
250e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
251430758c8SJeremy L Thompson }
252430758c8SJeremy L Thompson 
253430758c8SJeremy L Thompson /**
254ca94c3ddSJeremy L Thompson   @brief Restore an offsets array obtained using @ref CeedElemRestrictionGetOffsets()
255430758c8SJeremy L Thompson 
256ca94c3ddSJeremy L Thompson   @param[in] rstr    `CeedElemRestriction` to restore
257ea61e9acSJeremy L Thompson   @param[in] offsets Array of offset data
258430758c8SJeremy L Thompson 
259430758c8SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
260430758c8SJeremy L Thompson 
261430758c8SJeremy L Thompson   @ref User
262430758c8SJeremy L Thompson **/
2632b730f8bSJeremy L Thompson int CeedElemRestrictionRestoreOffsets(CeedElemRestriction rstr, const CeedInt **offsets) {
2647c1dbaffSSebastian Grimberg   if (rstr->rstr_base) {
2657c1dbaffSSebastian Grimberg     CeedCall(CeedElemRestrictionRestoreOffsets(rstr->rstr_base, offsets));
266c17ec2beSJeremy L Thompson   } else {
267430758c8SJeremy L Thompson     *offsets = NULL;
268d1d35e2fSjeremylt     rstr->num_readers--;
269c17ec2beSJeremy L Thompson   }
270e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
271bd33150aSjeremylt }
272bd33150aSjeremylt 
273bd33150aSjeremylt /**
274ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` orientations array by @ref CeedMemType
2753ac43b2cSJeremy L Thompson 
276ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction` to retrieve orientations
277fcbe8c06SSebastian Grimberg   @param[in]  mem_type Memory type on which to access the array.
278fcbe8c06SSebastian Grimberg                          If the backend uses a different memory type, this will perform a copy (possibly cached).
279ca94c3ddSJeremy L Thompson   @param[out] orients  Array on memory type `mem_type`
2803ac43b2cSJeremy L Thompson 
2813ac43b2cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
2823ac43b2cSJeremy L Thompson 
28377d1c127SSebastian Grimberg   @ref User
2843ac43b2cSJeremy L Thompson **/
28577d1c127SSebastian Grimberg int CeedElemRestrictionGetOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const bool **orients) {
286*6e536b99SJeremy L Thompson   CeedCheck(rstr->GetOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
287*6e536b99SJeremy L Thompson             "Backend does not support CeedElemRestrictionGetOrientations");
28877d1c127SSebastian Grimberg   CeedCall(rstr->GetOrientations(rstr, mem_type, orients));
28977d1c127SSebastian Grimberg   rstr->num_readers++;
290e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2913ac43b2cSJeremy L Thompson }
2923ac43b2cSJeremy L Thompson 
2933ac43b2cSJeremy L Thompson /**
294ca94c3ddSJeremy L Thompson   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetOrientations()
295b435c5a6Srezgarshakeri 
296ca94c3ddSJeremy L Thompson   @param[in] rstr    `CeedElemRestriction` to restore
29777d1c127SSebastian Grimberg   @param[in] orients Array of orientation data
298b435c5a6Srezgarshakeri 
299b435c5a6Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
300b435c5a6Srezgarshakeri 
30177d1c127SSebastian Grimberg   @ref User
302b435c5a6Srezgarshakeri **/
30377d1c127SSebastian Grimberg int CeedElemRestrictionRestoreOrientations(CeedElemRestriction rstr, const bool **orients) {
30477d1c127SSebastian Grimberg   *orients = NULL;
30577d1c127SSebastian Grimberg   rstr->num_readers--;
306b435c5a6Srezgarshakeri   return CEED_ERROR_SUCCESS;
307b435c5a6Srezgarshakeri }
308b435c5a6Srezgarshakeri 
309b435c5a6Srezgarshakeri /**
310ca94c3ddSJeremy L Thompson   @brief Get read-only access to a `CeedElemRestriction` curl-conforming orientations array by @ref CeedMemType
3117a982d89SJeremy L. Thompson 
312ca94c3ddSJeremy L Thompson   @param[in]  rstr         `CeedElemRestriction` to retrieve curl-conforming orientations
313fcbe8c06SSebastian Grimberg   @param[in]  mem_type     Memory type on which to access the array.
314fcbe8c06SSebastian Grimberg                              If the backend uses a different memory type, this will perform a copy (possibly cached).
315ca94c3ddSJeremy L Thompson   @param[out] curl_orients Array on memory type `mem_type`
3167a982d89SJeremy L. Thompson 
3177a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
3187a982d89SJeremy L. Thompson 
31977d1c127SSebastian Grimberg   @ref User
3207a982d89SJeremy L. Thompson **/
3210c73c039SSebastian Grimberg int CeedElemRestrictionGetCurlOrientations(CeedElemRestriction rstr, CeedMemType mem_type, const CeedInt8 **curl_orients) {
322*6e536b99SJeremy L Thompson   CeedCheck(rstr->GetCurlOrientations, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED,
323*6e536b99SJeremy L Thompson             "Backend does not support CeedElemRestrictionGetCurlOrientations");
32477d1c127SSebastian Grimberg   CeedCall(rstr->GetCurlOrientations(rstr, mem_type, curl_orients));
32577d1c127SSebastian Grimberg   rstr->num_readers++;
32677d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
32777d1c127SSebastian Grimberg }
32877d1c127SSebastian Grimberg 
32977d1c127SSebastian Grimberg /**
330ca94c3ddSJeremy L Thompson   @brief Restore an orientations array obtained using @ref CeedElemRestrictionGetCurlOrientations()
33177d1c127SSebastian Grimberg 
332ca94c3ddSJeremy L Thompson   @param[in] rstr         `CeedElemRestriction` to restore
33377d1c127SSebastian Grimberg   @param[in] curl_orients Array of orientation data
33477d1c127SSebastian Grimberg 
33577d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
33677d1c127SSebastian Grimberg 
33777d1c127SSebastian Grimberg   @ref User
33877d1c127SSebastian Grimberg **/
3390c73c039SSebastian Grimberg int CeedElemRestrictionRestoreCurlOrientations(CeedElemRestriction rstr, const CeedInt8 **curl_orients) {
34077d1c127SSebastian Grimberg   *curl_orients = NULL;
34177d1c127SSebastian Grimberg   rstr->num_readers--;
342e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3437a982d89SJeremy L. Thompson }
3447a982d89SJeremy L. Thompson 
3457a982d89SJeremy L. Thompson /**
34649fd234cSJeremy L Thompson 
34722eb1385SJeremy L Thompson   @brief Get the L-vector layout of a strided `CeedElemRestriction`
34822eb1385SJeremy L Thompson 
34922eb1385SJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
35022eb1385SJeremy L Thompson   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
35122eb1385SJeremy 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]`.
35222eb1385SJeremy L Thompson 
35322eb1385SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
35422eb1385SJeremy L Thompson 
35522eb1385SJeremy L Thompson   @ref Backend
35622eb1385SJeremy L Thompson **/
35756c48462SJeremy L Thompson int CeedElemRestrictionGetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
35822eb1385SJeremy L Thompson   bool                has_backend_strides;
35922eb1385SJeremy L Thompson   CeedRestrictionType rstr_type;
3601203703bSJeremy L Thompson   Ceed                ceed;
36122eb1385SJeremy L Thompson 
3621203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
36322eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
3641203703bSJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, ceed, CEED_ERROR_MINOR, "Only strided CeedElemRestriction have strided L-vector layout");
36522eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides));
36622eb1385SJeremy L Thompson   if (has_backend_strides) {
3671203703bSJeremy L Thompson     CeedCheck(rstr->l_layout[0], ceed, CEED_ERROR_MINOR, "CeedElemRestriction has no L-vector layout data");
36856c48462SJeremy L Thompson     for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->l_layout[i];
36922eb1385SJeremy L Thompson   } else {
37022eb1385SJeremy L Thompson     CeedCall(CeedElemRestrictionGetStrides(rstr, layout));
37122eb1385SJeremy L Thompson   }
37222eb1385SJeremy L Thompson   return CEED_ERROR_SUCCESS;
37322eb1385SJeremy L Thompson }
37422eb1385SJeremy L Thompson 
37522eb1385SJeremy L Thompson /**
37622eb1385SJeremy L Thompson 
37722eb1385SJeremy L Thompson   @brief Set the L-vector layout of a strided `CeedElemRestriction`
37822eb1385SJeremy L Thompson 
37922eb1385SJeremy L Thompson   @param[in] rstr   `CeedElemRestriction`
38022eb1385SJeremy L Thompson   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
38122eb1385SJeremy 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]`.
38222eb1385SJeremy L Thompson 
38322eb1385SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
38422eb1385SJeremy L Thompson 
38522eb1385SJeremy L Thompson   @ref Backend
38622eb1385SJeremy L Thompson **/
38722eb1385SJeremy L Thompson int CeedElemRestrictionSetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) {
38822eb1385SJeremy L Thompson   CeedRestrictionType rstr_type;
38922eb1385SJeremy L Thompson 
39022eb1385SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
391*6e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR,
392*6e536b99SJeremy L Thompson             "Only strided CeedElemRestriction have strided L-vector layout");
39322eb1385SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) rstr->l_layout[i] = layout[i];
39422eb1385SJeremy L Thompson   return CEED_ERROR_SUCCESS;
39522eb1385SJeremy L Thompson }
39622eb1385SJeremy L Thompson 
39722eb1385SJeremy L Thompson /**
39822eb1385SJeremy L Thompson 
399ca94c3ddSJeremy L Thompson   @brief Get the E-vector layout of a `CeedElemRestriction`
40049fd234cSJeremy L Thompson 
401ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
402ca94c3ddSJeremy L Thompson   @param[out] layout  Variable to store layout array, stored as `[nodes, components, elements]`.
403ca94c3ddSJeremy 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]`.
40449fd234cSJeremy L Thompson 
40549fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
40649fd234cSJeremy L Thompson 
40749fd234cSJeremy L Thompson   @ref Backend
40849fd234cSJeremy L Thompson **/
40956c48462SJeremy L Thompson int CeedElemRestrictionGetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
410*6e536b99SJeremy L Thompson   CeedCheck(rstr->e_layout[0], CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no E-vector layout data");
41156c48462SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->e_layout[i];
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
41349fd234cSJeremy L Thompson }
41449fd234cSJeremy L Thompson 
41549fd234cSJeremy L Thompson /**
41649fd234cSJeremy L Thompson 
417ca94c3ddSJeremy L Thompson   @brief Set the E-vector layout of a `CeedElemRestriction`
41849fd234cSJeremy L Thompson 
419ca94c3ddSJeremy L Thompson   @param[in] rstr   `CeedElemRestriction`
420ca94c3ddSJeremy L Thompson   @param[in] layout Variable to containing layout array, stored as `[nodes, components, elements]`.
421ca94c3ddSJeremy 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]`.
42249fd234cSJeremy L Thompson 
42349fd234cSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
42449fd234cSJeremy L Thompson 
42549fd234cSJeremy L Thompson   @ref Backend
42649fd234cSJeremy L Thompson **/
4272b730f8bSJeremy L Thompson int CeedElemRestrictionSetELayout(CeedElemRestriction rstr, CeedInt layout[3]) {
42822eb1385SJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) rstr->e_layout[i] = layout[i];
429e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
43049fd234cSJeremy L Thompson }
43149fd234cSJeremy L Thompson 
43249fd234cSJeremy L Thompson /**
433ca94c3ddSJeremy L Thompson   @brief Get the backend data of a `CeedElemRestriction`
4347a982d89SJeremy L. Thompson 
435ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
4367a982d89SJeremy L. Thompson   @param[out] data Variable to store data
4377a982d89SJeremy L. Thompson 
4387a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4397a982d89SJeremy L. Thompson 
4407a982d89SJeremy L. Thompson   @ref Backend
4417a982d89SJeremy L. Thompson **/
442777ff853SJeremy L Thompson int CeedElemRestrictionGetData(CeedElemRestriction rstr, void *data) {
443777ff853SJeremy L Thompson   *(void **)data = rstr->data;
444e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4457a982d89SJeremy L. Thompson }
4467a982d89SJeremy L. Thompson 
4477a982d89SJeremy L. Thompson /**
448ca94c3ddSJeremy L Thompson   @brief Set the backend data of a `CeedElemRestriction`
4497a982d89SJeremy L. Thompson 
450ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction`
451ea61e9acSJeremy L Thompson   @param[in]     data Data to set
4527a982d89SJeremy L. Thompson 
4537a982d89SJeremy L. Thompson   @return An error code: 0 - success, otherwise - failure
4547a982d89SJeremy L. Thompson 
4557a982d89SJeremy L. Thompson   @ref Backend
4567a982d89SJeremy L. Thompson **/
457777ff853SJeremy L Thompson int CeedElemRestrictionSetData(CeedElemRestriction rstr, void *data) {
458777ff853SJeremy L Thompson   rstr->data = data;
459e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4607a982d89SJeremy L. Thompson }
4617a982d89SJeremy L. Thompson 
46234359f16Sjeremylt /**
463ca94c3ddSJeremy L Thompson   @brief Increment the reference counter for a `CeedElemRestriction`
46434359f16Sjeremylt 
465ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction` to increment the reference counter
46634359f16Sjeremylt 
46734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
46834359f16Sjeremylt 
46934359f16Sjeremylt   @ref Backend
47034359f16Sjeremylt **/
4719560d06aSjeremylt int CeedElemRestrictionReference(CeedElemRestriction rstr) {
47234359f16Sjeremylt   rstr->ref_count++;
47334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
47434359f16Sjeremylt }
47534359f16Sjeremylt 
4766e15d496SJeremy L Thompson /**
477ca94c3ddSJeremy L Thompson   @brief Estimate number of FLOPs required to apply `CeedElemRestriction` in `t_mode`
4786e15d496SJeremy L Thompson 
479ca94c3ddSJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction` to estimate FLOPs for
480ea61e9acSJeremy L Thompson   @param[in]  t_mode Apply restriction or transpose
481ea61e9acSJeremy L Thompson   @param[out] flops  Address of variable to hold FLOPs estimate
4826e15d496SJeremy L Thompson 
4836e15d496SJeremy L Thompson   @ref Backend
4846e15d496SJeremy L Thompson **/
4852b730f8bSJeremy L Thompson int CeedElemRestrictionGetFlopsEstimate(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedSize *flops) {
4861203703bSJeremy L Thompson   CeedSize            e_size, scale = 0;
48789edb9e3SSebastian Grimberg   CeedRestrictionType rstr_type;
4881c66c397SJeremy L Thompson 
4891203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
49089edb9e3SSebastian Grimberg   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
49177d1c127SSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
49289edb9e3SSebastian Grimberg     switch (rstr_type) {
4933ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4943ac8f562SJeremy L Thompson         scale = 0;
4953ac8f562SJeremy L Thompson         break;
49689edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
49789edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
49877d1c127SSebastian Grimberg         scale = 1;
49989edb9e3SSebastian Grimberg         break;
50089edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
50177d1c127SSebastian Grimberg         scale = 2;
50289edb9e3SSebastian Grimberg         break;
50389edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
50477d1c127SSebastian Grimberg         scale = 6;
50589edb9e3SSebastian Grimberg         break;
5066e15d496SJeremy L Thompson     }
50777d1c127SSebastian Grimberg   } else {
50889edb9e3SSebastian Grimberg     switch (rstr_type) {
50989edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STRIDED:
51089edb9e3SSebastian Grimberg       case CEED_RESTRICTION_STANDARD:
5113ac8f562SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
51277d1c127SSebastian Grimberg         scale = 0;
51389edb9e3SSebastian Grimberg         break;
51489edb9e3SSebastian Grimberg       case CEED_RESTRICTION_ORIENTED:
51577d1c127SSebastian Grimberg         scale = 1;
51689edb9e3SSebastian Grimberg         break;
51789edb9e3SSebastian Grimberg       case CEED_RESTRICTION_CURL_ORIENTED:
51877d1c127SSebastian Grimberg         scale = 5;
51989edb9e3SSebastian Grimberg         break;
52077d1c127SSebastian Grimberg     }
52177d1c127SSebastian Grimberg   }
5226e15d496SJeremy L Thompson   *flops = e_size * scale;
5236e15d496SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5246e15d496SJeremy L Thompson }
5256e15d496SJeremy L Thompson 
5267a982d89SJeremy L. Thompson /// @}
5277a982d89SJeremy L. Thompson 
52815910d16Sjeremylt /// @cond DOXYGEN_SKIP
52915910d16Sjeremylt static struct CeedElemRestriction_private ceed_elemrestriction_none;
53015910d16Sjeremylt /// @endcond
53115910d16Sjeremylt 
5327a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5337a982d89SJeremy L. Thompson /// CeedElemRestriction Public API
5347a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
5357a982d89SJeremy L. Thompson /// @addtogroup CeedElemRestrictionUser
536d7b241e6Sjeremylt /// @{
537d7b241e6Sjeremylt 
5387a982d89SJeremy L. Thompson /// Indicate that the stride is determined by the backend
53945f1e315Sjeremylt const CeedInt CEED_STRIDES_BACKEND[3] = {0};
5407a982d89SJeremy L. Thompson 
541ca94c3ddSJeremy L Thompson /// Argument for @ref CeedOperatorSetField() indicating that the field does not require a `CeedElemRestriction`
5422b730f8bSJeremy L Thompson const CeedElemRestriction CEED_ELEMRESTRICTION_NONE = &ceed_elemrestriction_none;
5437a982d89SJeremy L. Thompson 
544d7b241e6Sjeremylt /**
545ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction`
546d7b241e6Sjeremylt 
547ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
548ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
549ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
550ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
551fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
552ca94c3ddSJeremy 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`.
553fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
554fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
555ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
556ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
557ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
558ca94c3ddSJeremy 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.
559ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
560ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
561d7b241e6Sjeremylt 
562b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
563dfdf5a53Sjeremylt 
5647a982d89SJeremy L. Thompson   @ref User
565b11c1e72Sjeremylt **/
5662b730f8bSJeremy L Thompson int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
5672b730f8bSJeremy L Thompson                               CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
5685fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
5695fe0d4faSjeremylt     Ceed delegate;
5706574a04fSJeremy L Thompson 
5712b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
572ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreate");
5732b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreate(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr));
574e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
5755fe0d4faSjeremylt   }
5765fe0d4faSjeremylt 
577e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
5786574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
579ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
580ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
581e022e1f8SJeremy L Thompson 
5822b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
583db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
584d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
585d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
586d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
587d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
588d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
589d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
5901203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
591e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
592e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
59361a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
594fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
595e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
596d7b241e6Sjeremylt }
597d7b241e6Sjeremylt 
598d7b241e6Sjeremylt /**
599ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction` with orientation signs
600fc0567d9Srezgarshakeri 
601ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
602ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
603ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
604ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
605fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
606ca94c3ddSJeremy 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`.
607fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
608fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
609ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
610ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
611ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
612ca94c3ddSJeremy 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`.
613ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
614ca94c3ddSJeremy L Thompson   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation
615ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
616fc0567d9Srezgarshakeri 
617fc0567d9Srezgarshakeri   @return An error code: 0 - success, otherwise - failure
618fc0567d9Srezgarshakeri 
619fc0567d9Srezgarshakeri   @ref User
620fc0567d9Srezgarshakeri **/
6212b730f8bSJeremy L Thompson int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
62277d1c127SSebastian Grimberg                                       CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
623fc0567d9Srezgarshakeri                                       CeedElemRestriction *rstr) {
624fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
625fc0567d9Srezgarshakeri     Ceed delegate;
6266574a04fSJeremy L Thompson 
6272b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
628ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateOriented");
6292b730f8bSJeremy L Thompson     CeedCall(
63077d1c127SSebastian Grimberg         CeedElemRestrictionCreateOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients, rstr));
631fc0567d9Srezgarshakeri     return CEED_ERROR_SUCCESS;
632fc0567d9Srezgarshakeri   }
633fc0567d9Srezgarshakeri 
634e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
6356574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
636ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
637ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
638e022e1f8SJeremy L Thompson 
6392b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
640db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
641fc0567d9Srezgarshakeri   (*rstr)->ref_count   = 1;
642fc0567d9Srezgarshakeri   (*rstr)->num_elem    = num_elem;
643fc0567d9Srezgarshakeri   (*rstr)->elem_size   = elem_size;
644fc0567d9Srezgarshakeri   (*rstr)->num_comp    = num_comp;
645fc0567d9Srezgarshakeri   (*rstr)->comp_stride = comp_stride;
646fc0567d9Srezgarshakeri   (*rstr)->l_size      = l_size;
6471203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
648e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
649e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
650fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
651fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, orients, NULL, *rstr));
65277d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
65377d1c127SSebastian Grimberg }
65477d1c127SSebastian Grimberg 
65577d1c127SSebastian Grimberg /**
656ca94c3ddSJeremy L Thompson   @brief Create a `CeedElemRestriction` with a general tridiagonal transformation matrix for curl-conforming elements
65777d1c127SSebastian Grimberg 
658ca94c3ddSJeremy L Thompson   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
659ca94c3ddSJeremy L Thompson   @param[in]  num_elem     Number of elements described in the `offsets` array
66077d1c127SSebastian Grimberg   @param[in]  elem_size    Size (number of "nodes") per element
66177d1c127SSebastian Grimberg   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
662fcbe8c06SSebastian Grimberg   @param[in]  comp_stride  Stride between components for the same L-vector "node".
663ca94c3ddSJeremy 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`.
664fcbe8c06SSebastian Grimberg   @param[in]  l_size       The size of the L-vector.
665fcbe8c06SSebastian Grimberg                              This vector may be larger than the elements and fields given by this restriction.
666ca94c3ddSJeremy L Thompson   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
667ca94c3ddSJeremy L Thompson   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
668ca94c3ddSJeremy L Thompson   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
669ca94c3ddSJeremy 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`.
670ca94c3ddSJeremy L Thompson                              All offsets must be in the range `[0, l_size - 1]`.
671ca94c3ddSJeremy 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.
672ca94c3ddSJeremy 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).
673ca94c3ddSJeremy L Thompson   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
67477d1c127SSebastian Grimberg 
67577d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
67677d1c127SSebastian Grimberg 
67777d1c127SSebastian Grimberg   @ref User
67877d1c127SSebastian Grimberg **/
67977d1c127SSebastian Grimberg int CeedElemRestrictionCreateCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedInt comp_stride, CeedSize l_size,
6800c73c039SSebastian Grimberg                                           CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const CeedInt8 *curl_orients,
68177d1c127SSebastian Grimberg                                           CeedElemRestriction *rstr) {
682fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreate) {
68377d1c127SSebastian Grimberg     Ceed delegate;
68477d1c127SSebastian Grimberg 
68577d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
686ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateCurlOriented");
68777d1c127SSebastian Grimberg     CeedCall(CeedElemRestrictionCreateCurlOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
68877d1c127SSebastian Grimberg                                                    curl_orients, rstr));
68977d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
69077d1c127SSebastian Grimberg   }
69177d1c127SSebastian Grimberg 
692e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
69377d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
694ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
695ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
69677d1c127SSebastian Grimberg 
69777d1c127SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
698fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
69977d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
70077d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
70177d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
70277d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
70377d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
70477d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
7051203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
706e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_elem;
707e7f679fcSJeremy L Thompson   (*rstr)->block_size  = 1;
708fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
709fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(mem_type, copy_mode, offsets, NULL, curl_orients, *rstr));
710fc0567d9Srezgarshakeri   return CEED_ERROR_SUCCESS;
711fc0567d9Srezgarshakeri }
712fc0567d9Srezgarshakeri 
713fc0567d9Srezgarshakeri /**
714ca94c3ddSJeremy L Thompson   @brief Create a strided `CeedElemRestriction`
715d7b241e6Sjeremylt 
716ca94c3ddSJeremy L Thompson   @param[in]  ceed      `Ceed` context used to create the `CeedElemRestriction`
717ea61e9acSJeremy L Thompson   @param[in]  num_elem  Number of elements described by the restriction
718ea61e9acSJeremy L Thompson   @param[in]  elem_size Size (number of "nodes") per element
719ea61e9acSJeremy L Thompson   @param[in]  num_comp  Number of field components per interpolation "node" (1 for scalar fields)
720fcbe8c06SSebastian Grimberg   @param[in]  l_size    The size of the L-vector.
721fcbe8c06SSebastian Grimberg                           This vector may be larger than the elements and fields given by this restriction.
722ca94c3ddSJeremy L Thompson   @param[in]  strides   Array for strides between `[nodes, components, elements]`.
723ca94c3ddSJeremy 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]`.
724ca94c3ddSJeremy L Thompson                           @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
725972f909dSJeremy L Thompson                           `CEED_STRIDES_BACKEND` should only be used pass data between `CeedOperator` created with the same `Ceed` backend.
726972f909dSJeremy L Thompson                           The L-vector layout will, in general, be different between `Ceed` backends.
727ca94c3ddSJeremy L Thompson   @param[out] rstr      Address of the variable where the newly created `CeedElemRestriction` will be stored
728d7b241e6Sjeremylt 
729b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
730dfdf5a53Sjeremylt 
7317a982d89SJeremy L. Thompson   @ref User
732b11c1e72Sjeremylt **/
7332b730f8bSJeremy L Thompson int CeedElemRestrictionCreateStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt num_comp, CeedSize l_size, const CeedInt strides[3],
734f90c8643Sjeremylt                                      CeedElemRestriction *rstr) {
7355fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreate) {
7365fe0d4faSjeremylt     Ceed delegate;
737b04eb3d9SSebastian Grimberg 
7382b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
739ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateStrided");
7402b730f8bSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateStrided(delegate, num_elem, elem_size, num_comp, l_size, strides, rstr));
741e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
7425fe0d4faSjeremylt   }
7435fe0d4faSjeremylt 
744e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
7456574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
746ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
747e7f679fcSJeremy L Thompson   CeedCheck(l_size >= num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector size must be at least num_elem * elem_size * num_comp");
748e022e1f8SJeremy L Thompson 
7492b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
750db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
751d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
752d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
753d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
754d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
755d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
7561203703bSJeremy L Thompson   (*rstr)->e_size     = (CeedSize)num_elem * (CeedSize)elem_size * (CeedSize)num_comp;
757e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_elem;
758e7f679fcSJeremy L Thompson   (*rstr)->block_size = 1;
759fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
7602b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
7612b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
762fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreate(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
763e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
764d7b241e6Sjeremylt }
765d7b241e6Sjeremylt 
766d7b241e6Sjeremylt /**
767ca94c3ddSJeremy 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.
7683ac8f562SJeremy L Thompson 
7693ac8f562SJeremy L Thompson   The offsets array is arranged as
7703ac8f562SJeremy L Thompson 
7713ac8f562SJeremy L Thompson   element_0_start_index
7723ac8f562SJeremy L Thompson   element_1_start_index
7733ac8f562SJeremy L Thompson   ...
7743ac8f562SJeremy L Thompson   element_n_start_index
7753ac8f562SJeremy L Thompson   element_n_stop_index
7763ac8f562SJeremy L Thompson   element_0_point_0
7773ac8f562SJeremy L Thompson   element_0_point_1
7783ac8f562SJeremy L Thompson   ...
7793ac8f562SJeremy L Thompson 
780ca94c3ddSJeremy L Thompson   @param[in]  ceed       `Ceed` context used to create the `CeedElemRestriction`
781ca94c3ddSJeremy L Thompson   @param[in]  num_elem   Number of elements described in the `offsets` array
782ca94c3ddSJeremy L Thompson   @param[in]  num_points Number of points described in the `offsets` array
7833ac8f562SJeremy L Thompson   @param[in]  num_comp   Number of field components per interpolation node (1 for scalar fields).
7843ac8f562SJeremy L Thompson                            Components are assumed to be contiguous by point.
7853ac8f562SJeremy L Thompson   @param[in]  l_size     The size of the L-vector.
7863ac8f562SJeremy L Thompson                            This vector may be larger than the elements and fields given by this restriction.
787ca94c3ddSJeremy L Thompson   @param[in]  mem_type   Memory type of the `offsets` array, see @ref CeedMemType
788ca94c3ddSJeremy L Thompson   @param[in]  copy_mode  Copy mode for the `offsets` array, see @ref CeedCopyMode
789ca94c3ddSJeremy L Thompson   @param[in]  offsets    Array of size `num_elem + 1 + num_points`.
7903ac8f562SJeremy L Thompson                            The first portion of the offsets array holds the ranges of indices corresponding to each element.
7913ac8f562SJeremy L Thompson                            The second portion holds the indices for each element.
792ca94c3ddSJeremy L Thompson   @param[out] rstr       Address of the variable where the newly created `CeedElemRestriction` will be stored
7933ac8f562SJeremy L Thompson 
7943ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
7953ac8f562SJeremy L Thompson 
7963ac8f562SJeremy L Thompson   @ref Backend
7973ac8f562SJeremy L Thompson  **/
7983ac8f562SJeremy L Thompson int CeedElemRestrictionCreateAtPoints(Ceed ceed, CeedInt num_elem, CeedInt num_points, CeedInt num_comp, CeedSize l_size, CeedMemType mem_type,
7993ac8f562SJeremy L Thompson                                       CeedCopyMode copy_mode, const CeedInt *offsets, CeedElemRestriction *rstr) {
8003ac8f562SJeremy L Thompson   if (!ceed->ElemRestrictionCreateAtPoints) {
8013ac8f562SJeremy L Thompson     Ceed delegate;
8023ac8f562SJeremy L Thompson 
8033ac8f562SJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
804ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateAtPoints");
8053ac8f562SJeremy L Thompson     CeedCall(CeedElemRestrictionCreateAtPoints(delegate, num_elem, num_points, num_comp, l_size, mem_type, copy_mode, offsets, rstr));
8063ac8f562SJeremy L Thompson     return CEED_ERROR_SUCCESS;
8073ac8f562SJeremy L Thompson   }
8083ac8f562SJeremy L Thompson 
8093ac8f562SJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
8103ac8f562SJeremy L Thompson   CeedCheck(num_points >= 0, ceed, CEED_ERROR_DIMENSION, "Number of points must be non-negative");
811ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
8123ac8f562SJeremy L Thompson   CeedCheck(l_size >= num_points * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector must be at least num_points * num_comp");
8133ac8f562SJeremy L Thompson 
8143ac8f562SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
8153ac8f562SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
8163ac8f562SJeremy L Thompson   (*rstr)->ref_count  = 1;
8173ac8f562SJeremy L Thompson   (*rstr)->num_elem   = num_elem;
8183ac8f562SJeremy L Thompson   (*rstr)->num_points = num_points;
8193ac8f562SJeremy L Thompson   (*rstr)->num_comp   = num_comp;
8203ac8f562SJeremy L Thompson   (*rstr)->l_size     = l_size;
8211203703bSJeremy L Thompson   (*rstr)->e_size     = (CeedSize)num_points * (CeedSize)num_comp;
82205fa913cSJeremy L Thompson   (*rstr)->num_block  = num_elem;
8233ac8f562SJeremy L Thompson   (*rstr)->block_size = 1;
8243ac8f562SJeremy L Thompson   (*rstr)->rstr_type  = CEED_RESTRICTION_POINTS;
8253ac8f562SJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateAtPoints(mem_type, copy_mode, offsets, NULL, NULL, *rstr));
8263ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8273ac8f562SJeremy L Thompson }
8283ac8f562SJeremy L Thompson 
8293ac8f562SJeremy L Thompson /**
830ca94c3ddSJeremy L Thompson   @brief Create a blocked `CeedElemRestriction`, typically only used by backends
831d7b241e6Sjeremylt 
832ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
833ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array
834ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of unknowns) per element
835e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
836ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
837fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
838ca94c3ddSJeremy 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`.
839fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
840fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
841ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
842ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
843ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
844ca94c3ddSJeremy 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`.
845ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
846ca94c3ddSJeremy L Thompson                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
847ca94c3ddSJeremy L Thompson                             The default reordering is to interlace elements.
848ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
849d7b241e6Sjeremylt 
850b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
851dfdf5a53Sjeremylt 
8527a982d89SJeremy L. Thompson   @ref Backend
853b11c1e72Sjeremylt  **/
854e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedInt comp_stride,
8552b730f8bSJeremy L Thompson                                      CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets,
8564ce2993fSjeremylt                                      CeedElemRestriction *rstr) {
8571c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
858d7b241e6Sjeremylt 
8595fe0d4faSjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
8605fe0d4faSjeremylt     Ceed delegate;
8616574a04fSJeremy L Thompson 
8622b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
863ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlocked");
864e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlocked(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets,
865e7f679fcSJeremy L Thompson                                               rstr));
866e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
8675fe0d4faSjeremylt   }
868d7b241e6Sjeremylt 
869e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
8706574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
871e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
872ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
873ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
874e022e1f8SJeremy L Thompson 
875e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
876e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
877d7b241e6Sjeremylt 
878db002c03SJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
879db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
880d1d35e2fSjeremylt   (*rstr)->ref_count   = 1;
881d1d35e2fSjeremylt   (*rstr)->num_elem    = num_elem;
882d1d35e2fSjeremylt   (*rstr)->elem_size   = elem_size;
883d1d35e2fSjeremylt   (*rstr)->num_comp    = num_comp;
884d1d35e2fSjeremylt   (*rstr)->comp_stride = comp_stride;
885d1d35e2fSjeremylt   (*rstr)->l_size      = l_size;
8861203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
887e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
888e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
88961a27d74SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_STANDARD;
890e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL, NULL, *rstr));
8911c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
892e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
893d7b241e6Sjeremylt }
894d7b241e6Sjeremylt 
895b11c1e72Sjeremylt /**
896ca94c3ddSJeremy L Thompson   @brief Create a blocked oriented `CeedElemRestriction`, typically only used by backends
89777d1c127SSebastian Grimberg 
898ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
899ca94c3ddSJeremy L Thompson   @param[in]  num_elem    Number of elements described in the `offsets` array.
90077d1c127SSebastian Grimberg   @param[in]  elem_size   Size (number of unknowns) per element
901e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
90277d1c127SSebastian Grimberg   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
903fcbe8c06SSebastian Grimberg   @param[in]  comp_stride Stride between components for the same L-vector "node".
904ca94c3ddSJeremy 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`.
905fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
906fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
907ca94c3ddSJeremy L Thompson   @param[in]  mem_type    Memory type of the `offsets` array, see @ref CeedMemType
908ca94c3ddSJeremy L Thompson   @param[in]  copy_mode   Copy mode for the `offsets` array, see @ref CeedCopyMode
909ca94c3ddSJeremy L Thompson   @param[in]  offsets     Array of shape `[num_elem, elem_size]`.
910ca94c3ddSJeremy 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`.
911ca94c3ddSJeremy L Thompson                             All offsets must be in the range `[0, l_size - 1]`.
912ca94c3ddSJeremy L Thompson                             The backend will permute and pad this array to the desired ordering for the blocksize, which is typically given by the backend.
913ca94c3ddSJeremy L Thompson                             The default reordering is to interlace elements.
914ca94c3ddSJeremy L Thompson   @param[in]  orients     Boolean array of shape `[num_elem, elem_size]` with `false` for positively oriented and `true` to flip the orientation.
915ca94c3ddSJeremy L Thompson                             Will also be permuted and padded similarly to `offsets`.
916ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
91777d1c127SSebastian Grimberg 
91877d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
91977d1c127SSebastian Grimberg 
92077d1c127SSebastian Grimberg   @ref Backend
92177d1c127SSebastian Grimberg  **/
922e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
923e7f679fcSJeremy L Thompson                                              CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
924e7f679fcSJeremy L Thompson                                              const CeedInt *offsets, const bool *orients, CeedElemRestriction *rstr) {
925e7f679fcSJeremy L Thompson   bool    *block_orients;
9261c66c397SJeremy L Thompson   CeedInt *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
92777d1c127SSebastian Grimberg 
928fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
92977d1c127SSebastian Grimberg     Ceed delegate;
93077d1c127SSebastian Grimberg 
93177d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
932ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedOriented");
933e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode,
93477d1c127SSebastian Grimberg                                                       offsets, orients, rstr));
93577d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
93677d1c127SSebastian Grimberg   }
93777d1c127SSebastian Grimberg 
93877d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
939e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
940ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
941ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
94277d1c127SSebastian Grimberg 
943e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
944e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_orients));
945e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
946e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOrients(orients, block_orients, num_block, num_elem, block_size, elem_size));
94777d1c127SSebastian Grimberg 
948fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
949fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
95077d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
95177d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
95277d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
95377d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
95477d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
95577d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
9561203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
957e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
958e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
959fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_ORIENTED;
960e7f679fcSJeremy L Thompson   CeedCall(
961e7f679fcSJeremy L Thompson       ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, (const bool *)block_orients, NULL, *rstr));
9621c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
96377d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
96477d1c127SSebastian Grimberg }
96577d1c127SSebastian Grimberg 
96677d1c127SSebastian Grimberg /**
967ca94c3ddSJeremy L Thompson   @brief Create a blocked curl-oriented `CeedElemRestriction`, typically only used by backends
96877d1c127SSebastian Grimberg 
969ca94c3ddSJeremy L Thompson   @param[in]  ceed         `Ceed` context used to create the `CeedElemRestriction`
970ca94c3ddSJeremy L Thompson   @param[in]  num_elem     Number of elements described in the `offsets` array.
97177d1c127SSebastian Grimberg   @param[in]  elem_size    Size (number of unknowns) per element
972e7f679fcSJeremy L Thompson   @param[in]  block_size   Number of elements in a block
97377d1c127SSebastian Grimberg   @param[in]  num_comp     Number of field components per interpolation node (1 for scalar fields)
974fcbe8c06SSebastian Grimberg   @param[in]  comp_stride  Stride between components for the same L-vector "node".
975ca94c3ddSJeremy 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`.
976fcbe8c06SSebastian Grimberg   @param[in]  l_size       The size of the L-vector.
977fcbe8c06SSebastian Grimberg                              This vector may be larger than the elements and fields given by this restriction.
978ca94c3ddSJeremy L Thompson   @param[in]  mem_type     Memory type of the `offsets` array, see @ref CeedMemType
979ca94c3ddSJeremy L Thompson   @param[in]  copy_mode    Copy mode for the `offsets` array, see @ref CeedCopyMode
980ca94c3ddSJeremy L Thompson   @param[in]  offsets      Array of shape `[num_elem, elem_size]`.
981ca94c3ddSJeremy 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`.
982ca94c3ddSJeremy L Thompson                              All offsets must be in the range `[0, l_size - 1]`.
983ca94c3ddSJeremy L Thompson                              The backend will permute and pad this array to the desired  ordering for the blocksize, which is typically given by the backend.
984ca94c3ddSJeremy L Thompson                              The default reordering is to interlace elements.
985ca94c3ddSJeremy 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.
986ca94c3ddSJeremy 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).
987ca94c3ddSJeremy L Thompson                              Will also be permuted and padded similarly to offsets.
988ca94c3ddSJeremy L Thompson   @param[out] rstr         Address of the variable where the newly created `CeedElemRestriction` will be stored
98977d1c127SSebastian Grimberg 
99077d1c127SSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
99177d1c127SSebastian Grimberg 
99277d1c127SSebastian Grimberg   @ref Backend
99377d1c127SSebastian Grimberg  **/
994e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp,
99577d1c127SSebastian Grimberg                                                  CeedInt comp_stride, CeedSize l_size, CeedMemType mem_type, CeedCopyMode copy_mode,
9960c73c039SSebastian Grimberg                                                  const CeedInt *offsets, const CeedInt8 *curl_orients, CeedElemRestriction *rstr) {
997e7f679fcSJeremy L Thompson   CeedInt8 *block_curl_orients;
9981c66c397SJeremy L Thompson   CeedInt  *block_offsets, num_block = (num_elem / block_size) + !!(num_elem % block_size);
99977d1c127SSebastian Grimberg 
1000fcbe8c06SSebastian Grimberg   if (!ceed->ElemRestrictionCreateBlocked) {
100177d1c127SSebastian Grimberg     Ceed delegate;
100277d1c127SSebastian Grimberg 
100377d1c127SSebastian Grimberg     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1004ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedCurlOriented");
1005e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedCurlOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type,
1006e7f679fcSJeremy L Thompson                                                           copy_mode, offsets, curl_orients, rstr));
100777d1c127SSebastian Grimberg     return CEED_ERROR_SUCCESS;
100877d1c127SSebastian Grimberg   }
100977d1c127SSebastian Grimberg 
1010e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
101177d1c127SSebastian Grimberg   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1012e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1013ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1014ca94c3ddSJeremy L Thompson   CeedCheck(num_comp == 1 || comp_stride > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction component stride must be at least 1");
101577d1c127SSebastian Grimberg 
1016e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * elem_size, &block_offsets));
1017e7f679fcSJeremy L Thompson   CeedCall(CeedCalloc(num_block * block_size * 3 * elem_size, &block_curl_orients));
1018e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadOffsets(offsets, block_offsets, num_block, num_elem, block_size, elem_size));
1019e7f679fcSJeremy L Thompson   CeedCall(CeedPermutePadCurlOrients(curl_orients, block_curl_orients, num_block, num_elem, block_size, 3 * elem_size));
102077d1c127SSebastian Grimberg 
1021fcbe8c06SSebastian Grimberg   CeedCall(CeedCalloc(1, rstr));
1022fcbe8c06SSebastian Grimberg   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
102377d1c127SSebastian Grimberg   (*rstr)->ref_count   = 1;
102477d1c127SSebastian Grimberg   (*rstr)->num_elem    = num_elem;
102577d1c127SSebastian Grimberg   (*rstr)->elem_size   = elem_size;
102677d1c127SSebastian Grimberg   (*rstr)->num_comp    = num_comp;
102777d1c127SSebastian Grimberg   (*rstr)->comp_stride = comp_stride;
102877d1c127SSebastian Grimberg   (*rstr)->l_size      = l_size;
10291203703bSJeremy L Thompson   (*rstr)->e_size      = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1030e7f679fcSJeremy L Thompson   (*rstr)->num_block   = num_block;
1031e7f679fcSJeremy L Thompson   (*rstr)->block_size  = block_size;
1032fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type   = CEED_RESTRICTION_CURL_ORIENTED;
1033e7f679fcSJeremy L Thompson   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, (const CeedInt *)block_offsets, NULL,
1034e7f679fcSJeremy L Thompson                                               (const CeedInt8 *)block_curl_orients, *rstr));
10351c66c397SJeremy L Thompson   if (copy_mode == CEED_OWN_POINTER) CeedCall(CeedFree(&offsets));
103677d1c127SSebastian Grimberg   return CEED_ERROR_SUCCESS;
103777d1c127SSebastian Grimberg }
103877d1c127SSebastian Grimberg 
103977d1c127SSebastian Grimberg /**
1040ca94c3ddSJeremy L Thompson   @brief Create a blocked strided `CeedElemRestriction`, typically only used by backends
10417509a596Sjeremylt 
1042ca94c3ddSJeremy L Thompson   @param[in]  ceed        `Ceed` context used to create the `CeedElemRestriction`
1043ea61e9acSJeremy L Thompson   @param[in]  num_elem    Number of elements described by the restriction
1044ea61e9acSJeremy L Thompson   @param[in]  elem_size   Size (number of "nodes") per element
1045e7f679fcSJeremy L Thompson   @param[in]  block_size  Number of elements in a block
1046ea61e9acSJeremy L Thompson   @param[in]  num_comp    Number of field components per interpolation node (1 for scalar fields)
1047fcbe8c06SSebastian Grimberg   @param[in]  l_size      The size of the L-vector.
1048fcbe8c06SSebastian Grimberg                             This vector may be larger than the elements and fields given by this restriction.
1049ca94c3ddSJeremy L Thompson   @param[in]  strides     Array for strides between `[nodes, components, elements]`.
1050ca94c3ddSJeremy 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]`.
1051ca94c3ddSJeremy L Thompson                             @ref CEED_STRIDES_BACKEND may be used for `CeedVector` ordered by the same `Ceed` backend.
1052ca94c3ddSJeremy L Thompson   @param[out] rstr        Address of the variable where the newly created `CeedElemRestriction` will be stored
10537509a596Sjeremylt 
10547509a596Sjeremylt   @return An error code: 0 - success, otherwise - failure
10557509a596Sjeremylt 
10567a982d89SJeremy L. Thompson   @ref User
10577509a596Sjeremylt **/
1058e7f679fcSJeremy L Thompson int CeedElemRestrictionCreateBlockedStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_size, CeedInt block_size, CeedInt num_comp, CeedSize l_size,
10598621c6c6SJeremy L Thompson                                             const CeedInt strides[3], CeedElemRestriction *rstr) {
1060e7f679fcSJeremy L Thompson   CeedInt num_block = (num_elem / block_size) + !!(num_elem % block_size);
10617509a596Sjeremylt 
10627509a596Sjeremylt   if (!ceed->ElemRestrictionCreateBlocked) {
10637509a596Sjeremylt     Ceed delegate;
10646574a04fSJeremy L Thompson 
10652b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction"));
1066ca94c3ddSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedStrided");
1067e7f679fcSJeremy L Thompson     CeedCall(CeedElemRestrictionCreateBlockedStrided(delegate, num_elem, elem_size, block_size, num_comp, l_size, strides, rstr));
1068e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
10697509a596Sjeremylt   }
10707509a596Sjeremylt 
1071e7f679fcSJeremy L Thompson   CeedCheck(num_elem >= 0, ceed, CEED_ERROR_DIMENSION, "Number of elements must be non-negative");
10726574a04fSJeremy L Thompson   CeedCheck(elem_size > 0, ceed, CEED_ERROR_DIMENSION, "Element size must be at least 1");
1073e7f679fcSJeremy L Thompson   CeedCheck(block_size > 0, ceed, CEED_ERROR_DIMENSION, "Block size must be at least 1");
1074ca94c3ddSJeremy L Thompson   CeedCheck(num_comp > 0, ceed, CEED_ERROR_DIMENSION, "CeedElemRestriction must have at least 1 component");
1075e7f679fcSJeremy L Thompson   CeedCheck(l_size >= num_elem * elem_size * num_comp, ceed, CEED_ERROR_DIMENSION, "L-vector size must be at least num_elem * elem_size * num_comp");
1076e022e1f8SJeremy L Thompson 
10772b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr));
1078db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*rstr)->ceed));
1079d1d35e2fSjeremylt   (*rstr)->ref_count  = 1;
1080d1d35e2fSjeremylt   (*rstr)->num_elem   = num_elem;
1081d1d35e2fSjeremylt   (*rstr)->elem_size  = elem_size;
1082d1d35e2fSjeremylt   (*rstr)->num_comp   = num_comp;
1083d1d35e2fSjeremylt   (*rstr)->l_size     = l_size;
10841203703bSJeremy L Thompson   (*rstr)->e_size     = (CeedSize)num_block * (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1085e7f679fcSJeremy L Thompson   (*rstr)->num_block  = num_block;
1086e7f679fcSJeremy L Thompson   (*rstr)->block_size = block_size;
1087fcbe8c06SSebastian Grimberg   (*rstr)->rstr_type  = CEED_RESTRICTION_STRIDED;
10882b730f8bSJeremy L Thompson   CeedCall(CeedMalloc(3, &(*rstr)->strides));
10892b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 3; i++) (*rstr)->strides[i] = strides[i];
1090fcbe8c06SSebastian Grimberg   CeedCall(ceed->ElemRestrictionCreateBlocked(CEED_MEM_HOST, CEED_OWN_POINTER, NULL, NULL, NULL, *rstr));
1091e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10927509a596Sjeremylt }
10937509a596Sjeremylt 
10947509a596Sjeremylt /**
1095ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unsigned version.
1096c17ec2beSJeremy L Thompson 
1097ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
1098c17ec2beSJeremy L Thompson 
1099ca94c3ddSJeremy L Thompson   @param[in]     rstr          `CeedElemRestriction` to create unsigned reference to
1100ca94c3ddSJeremy L Thompson   @param[in,out] rstr_unsigned Variable to store unsigned `CeedElemRestriction`
1101c17ec2beSJeremy L Thompson 
1102c17ec2beSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1103c17ec2beSJeremy L Thompson 
1104c17ec2beSJeremy L Thompson   @ref User
1105c17ec2beSJeremy L Thompson **/
1106c17ec2beSJeremy L Thompson int CeedElemRestrictionCreateUnsignedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unsigned) {
1107c17ec2beSJeremy L Thompson   CeedCall(CeedCalloc(1, rstr_unsigned));
1108c17ec2beSJeremy L Thompson 
1109c17ec2beSJeremy L Thompson   // Copy old rstr
1110c17ec2beSJeremy L Thompson   memcpy(*rstr_unsigned, rstr, sizeof(struct CeedElemRestriction_private));
1111c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ceed = NULL;
1112c17ec2beSJeremy L Thompson   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unsigned)->ceed));
1113c17ec2beSJeremy L Thompson   (*rstr_unsigned)->ref_count = 1;
1114c17ec2beSJeremy L Thompson   (*rstr_unsigned)->strides   = NULL;
1115c17ec2beSJeremy L Thompson   if (rstr->strides) {
1116c17ec2beSJeremy L Thompson     CeedCall(CeedMalloc(3, &(*rstr_unsigned)->strides));
1117c17ec2beSJeremy L Thompson     for (CeedInt i = 0; i < 3; i++) (*rstr_unsigned)->strides[i] = rstr->strides[i];
1118c17ec2beSJeremy L Thompson   }
11197c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unsigned)->rstr_base));
1120c17ec2beSJeremy L Thompson 
1121c17ec2beSJeremy L Thompson   // Override Apply
1122c17ec2beSJeremy L Thompson   (*rstr_unsigned)->Apply = rstr->ApplyUnsigned;
1123c17ec2beSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1124c17ec2beSJeremy L Thompson }
1125c17ec2beSJeremy L Thompson 
1126c17ec2beSJeremy L Thompson /**
1127ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction` and set @ref CeedElemRestrictionApply() implementation to use the unoriented version.
11287c1dbaffSSebastian Grimberg 
1129ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
11307c1dbaffSSebastian Grimberg 
1131ca94c3ddSJeremy L Thompson   @param[in]     rstr            `CeedElemRestriction` to create unoriented reference to
1132ca94c3ddSJeremy L Thompson   @param[in,out] rstr_unoriented Variable to store unoriented `CeedElemRestriction`
11337c1dbaffSSebastian Grimberg 
11347c1dbaffSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
11357c1dbaffSSebastian Grimberg 
11367c1dbaffSSebastian Grimberg   @ref User
11377c1dbaffSSebastian Grimberg **/
11387c1dbaffSSebastian Grimberg int CeedElemRestrictionCreateUnorientedCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_unoriented) {
11397c1dbaffSSebastian Grimberg   CeedCall(CeedCalloc(1, rstr_unoriented));
11407c1dbaffSSebastian Grimberg 
11417c1dbaffSSebastian Grimberg   // Copy old rstr
11427c1dbaffSSebastian Grimberg   memcpy(*rstr_unoriented, rstr, sizeof(struct CeedElemRestriction_private));
11437c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ceed = NULL;
11447c1dbaffSSebastian Grimberg   CeedCall(CeedReferenceCopy(rstr->ceed, &(*rstr_unoriented)->ceed));
11457c1dbaffSSebastian Grimberg   (*rstr_unoriented)->ref_count = 1;
11467c1dbaffSSebastian Grimberg   (*rstr_unoriented)->strides   = NULL;
11477c1dbaffSSebastian Grimberg   if (rstr->strides) {
11487c1dbaffSSebastian Grimberg     CeedCall(CeedMalloc(3, &(*rstr_unoriented)->strides));
11497c1dbaffSSebastian Grimberg     for (CeedInt i = 0; i < 3; i++) (*rstr_unoriented)->strides[i] = rstr->strides[i];
11507c1dbaffSSebastian Grimberg   }
11517c1dbaffSSebastian Grimberg   CeedCall(CeedElemRestrictionReferenceCopy(rstr, &(*rstr_unoriented)->rstr_base));
11527c1dbaffSSebastian Grimberg 
11537c1dbaffSSebastian Grimberg   // Override Apply
11547c1dbaffSSebastian Grimberg   (*rstr_unoriented)->Apply = rstr->ApplyUnoriented;
11557c1dbaffSSebastian Grimberg   return CEED_ERROR_SUCCESS;
11567c1dbaffSSebastian Grimberg }
11577c1dbaffSSebastian Grimberg 
11587c1dbaffSSebastian Grimberg /**
1159ca94c3ddSJeremy L Thompson   @brief Copy the pointer to a `CeedElemRestriction`.
11609fd66db6SSebastian Grimberg 
1161ca94c3ddSJeremy L Thompson   Both pointers should be destroyed with @ref CeedElemRestrictionDestroy().
11629560d06aSjeremylt 
1163ca94c3ddSJeremy 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`.
1164ca94c3ddSJeremy L Thompson         This `CeedElemRestriction` will be destroyed if `*rstr_copy` is the only reference to this `CeedElemRestriction`.
1165ea61e9acSJeremy L Thompson 
1166ca94c3ddSJeremy L Thompson   @param[in]     rstr      `CeedElemRestriction` to copy reference to
1167ea61e9acSJeremy L Thompson   @param[in,out] rstr_copy Variable to store copied reference
11689560d06aSjeremylt 
11699560d06aSjeremylt   @return An error code: 0 - success, otherwise - failure
11709560d06aSjeremylt 
11719560d06aSjeremylt   @ref User
11729560d06aSjeremylt **/
11732b730f8bSJeremy L Thompson int CeedElemRestrictionReferenceCopy(CeedElemRestriction rstr, CeedElemRestriction *rstr_copy) {
1174393ac2cdSJeremy L Thompson   if (rstr != CEED_ELEMRESTRICTION_NONE) CeedCall(CeedElemRestrictionReference(rstr));
11752b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionDestroy(rstr_copy));
11769560d06aSjeremylt   *rstr_copy = rstr;
11779560d06aSjeremylt   return CEED_ERROR_SUCCESS;
11789560d06aSjeremylt }
11799560d06aSjeremylt 
11809560d06aSjeremylt /**
1181ca94c3ddSJeremy L Thompson   @brief Create `CeedVector` associated with a `CeedElemRestriction`
1182b11c1e72Sjeremylt 
1183ca94c3ddSJeremy L Thompson   @param[in]  rstr  `CeedElemRestriction`
1184ca94c3ddSJeremy L Thompson   @param[out] l_vec The address of the L-vector to be created, or `NULL`
1185ca94c3ddSJeremy L Thompson   @param[out] e_vec The address of the E-vector to be created, or `NULL`
1186b11c1e72Sjeremylt 
1187b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1188dfdf5a53Sjeremylt 
11897a982d89SJeremy L. Thompson   @ref User
1190b11c1e72Sjeremylt **/
11912b730f8bSJeremy L Thompson int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, CeedVector *e_vec) {
1192d2643443SJeremy L Thompson   CeedSize e_size, l_size;
11931203703bSJeremy L Thompson   Ceed     ceed;
119448acf710SJeremy L Thompson 
11951203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
11961203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size));
11971203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size));
11981203703bSJeremy L Thompson   if (l_vec) CeedCall(CeedVectorCreate(ceed, l_size, l_vec));
11991203703bSJeremy L Thompson   if (e_vec) CeedCall(CeedVectorCreate(ceed, e_size, e_vec));
1200e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1201d7b241e6Sjeremylt }
1202d7b241e6Sjeremylt 
1203d7b241e6Sjeremylt /**
1204d9e1f99aSValeria Barra   @brief Restrict an L-vector to an E-vector or apply its transpose
1205d7b241e6Sjeremylt 
1206ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1207ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1208ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1209ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape `[num_elem * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1210fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1211ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1212b11c1e72Sjeremylt 
1213b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1214dfdf5a53Sjeremylt 
12157a982d89SJeremy L. Thompson   @ref User
1216b11c1e72Sjeremylt **/
12172b730f8bSJeremy L Thompson int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request) {
1218701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1219701e7d35SJeremy L Thompson   CeedInt  num_elem;
12201203703bSJeremy L Thompson   Ceed     ceed;
1221d7b241e6Sjeremylt 
12221203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
1223d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1224701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_ru_len));
1225701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1226d7b241e6Sjeremylt   } else {
1227701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_u_len));
1228701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1229d7b241e6Sjeremylt   }
1230701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
12311203703bSJeremy L Thompson   CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION,
1232701e7d35SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1233701e7d35SJeremy L Thompson             min_u_len);
1234701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
12351203703bSJeremy L Thompson   CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION,
1236701e7d35SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1237701e7d35SJeremy L Thompson             min_ru_len);
1238701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
1239701e7d35SJeremy L Thompson   if (num_elem > 0) CeedCall(rstr->Apply(rstr, t_mode, u, ru, request));
1240e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1241d7b241e6Sjeremylt }
1242d7b241e6Sjeremylt 
1243d7b241e6Sjeremylt /**
12443ac8f562SJeremy L Thompson   @brief Restrict an L-vector of points to a single element or apply its transpose
12453ac8f562SJeremy L Thompson 
1246ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1247ca94c3ddSJeremy L Thompson   @param[in]  elem    Element number in range `[0, num_elem)`
12483ac8f562SJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1249ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1250ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape [`num_points * num_comp]` when `t_mode` = @ref CEED_NOTRANSPOSE).
12513ac8f562SJeremy L Thompson                         Ordering of the e-vector is decided by the backend.
12523ac8f562SJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
12533ac8f562SJeremy L Thompson 
12543ac8f562SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
12553ac8f562SJeremy L Thompson 
12563ac8f562SJeremy L Thompson   @ref User
12573ac8f562SJeremy L Thompson **/
125805fa913cSJeremy L Thompson int CeedElemRestrictionApplyAtPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
12593ac8f562SJeremy L Thompson                                               CeedRequest *request) {
1260701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1261701e7d35SJeremy L Thompson   CeedInt  num_elem;
12621203703bSJeremy L Thompson   Ceed     ceed;
12633ac8f562SJeremy L Thompson 
12641203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
12653ac8f562SJeremy L Thompson   if (t_mode == CEED_NOTRANSPOSE) {
1266701e7d35SJeremy L Thompson     CeedInt num_points, num_comp;
1267701e7d35SJeremy L Thompson 
1268701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1269701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1270701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1271701e7d35SJeremy L Thompson     min_ru_len = (CeedSize)num_points * (CeedSize)num_comp;
12723ac8f562SJeremy L Thompson   } else {
1273701e7d35SJeremy L Thompson     CeedInt num_points, num_comp;
1274701e7d35SJeremy L Thompson 
1275701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, elem, &num_points));
1276701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1277701e7d35SJeremy L Thompson     min_u_len = (CeedSize)num_points * (CeedSize)num_comp;
1278701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
12793ac8f562SJeremy L Thompson   }
1280701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
12811203703bSJeremy L Thompson   CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION,
12823ac8f562SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
12833ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
1284701e7d35SJeremy L Thompson             len, min_ru_len, min_u_len, elem);
1285701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
12861203703bSJeremy L Thompson   CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION,
12873ac8f562SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT
12883ac8f562SJeremy L Thompson             ") for element %" CeedInt_FMT,
1289701e7d35SJeremy L Thompson             len, min_ru_len, min_u_len, elem);
1290701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
12911203703bSJeremy L Thompson   CeedCheck(elem < num_elem, ceed, CEED_ERROR_DIMENSION,
1292701e7d35SJeremy L Thompson             "Cannot retrieve element %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", elem, elem, num_elem);
1293701e7d35SJeremy L Thompson   if (num_elem > 0) CeedCall(rstr->ApplyAtPointsInElement(rstr, elem, t_mode, u, ru, request));
12943ac8f562SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12953ac8f562SJeremy L Thompson }
12963ac8f562SJeremy L Thompson 
12973ac8f562SJeremy L Thompson /**
1298d9e1f99aSValeria Barra   @brief Restrict an L-vector to a block of an E-vector or apply its transpose
1299be9261b7Sjeremylt 
1300ca94c3ddSJeremy L Thompson   @param[in]  rstr    `CeedElemRestriction`
1301ca94c3ddSJeremy 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]`
1302ea61e9acSJeremy L Thompson   @param[in]  t_mode  Apply restriction or transpose
1303ca94c3ddSJeremy L Thompson   @param[in]  u       Input vector (of size `l_size` when `t_mode` = @ref CEED_NOTRANSPOSE)
1304ca94c3ddSJeremy L Thompson   @param[out] ru      Output vector (of shape `[block_size * elem_size]` when `t_mode` = @ref CEED_NOTRANSPOSE).
1305fcbe8c06SSebastian Grimberg                         Ordering of the e-vector is decided by the backend.
1306ea61e9acSJeremy L Thompson   @param[in]  request Request or @ref CEED_REQUEST_IMMEDIATE
1307be9261b7Sjeremylt 
1308be9261b7Sjeremylt   @return An error code: 0 - success, otherwise - failure
1309be9261b7Sjeremylt 
13107a982d89SJeremy L. Thompson   @ref Backend
1311be9261b7Sjeremylt **/
13122b730f8bSJeremy L Thompson int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedTransposeMode t_mode, CeedVector u, CeedVector ru,
13132b730f8bSJeremy L Thompson                                   CeedRequest *request) {
1314701e7d35SJeremy L Thompson   CeedSize min_u_len, min_ru_len, len;
1315701e7d35SJeremy L Thompson   CeedInt  block_size, num_elem;
13161203703bSJeremy L Thompson   Ceed     ceed;
1317be9261b7Sjeremylt 
13181203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed));
13191203703bSJeremy L Thompson   CeedCheck(rstr->ApplyBlock, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionApplyBlock");
13206402da51SJeremy L Thompson 
1321701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetBlockSize(rstr, &block_size));
1322d1d35e2fSjeremylt   if (t_mode == CEED_NOTRANSPOSE) {
1323701e7d35SJeremy L Thompson     CeedInt elem_size, num_comp;
1324701e7d35SJeremy L Thompson 
1325701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1326701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1327701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len));
1328701e7d35SJeremy L Thompson     min_ru_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1329be9261b7Sjeremylt   } else {
1330701e7d35SJeremy L Thompson     CeedInt elem_size, num_comp;
1331701e7d35SJeremy L Thompson 
1332701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetElementSize(rstr, &elem_size));
1333701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumComponents(rstr, &num_comp));
1334701e7d35SJeremy L Thompson     CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len));
1335701e7d35SJeremy L Thompson     min_u_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp;
1336be9261b7Sjeremylt   }
1337701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(u, &len));
13381203703bSJeremy L Thompson   CeedCheck(min_u_len == len, ceed, CEED_ERROR_DIMENSION,
1339701e7d35SJeremy L Thompson             "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len,
1340701e7d35SJeremy L Thompson             min_ru_len);
1341701e7d35SJeremy L Thompson   CeedCall(CeedVectorGetLength(ru, &len));
13421203703bSJeremy L Thompson   CeedCheck(min_ru_len == len, ceed, CEED_ERROR_DIMENSION,
1343701e7d35SJeremy L Thompson             "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len,
1344701e7d35SJeremy L Thompson             min_u_len);
1345701e7d35SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
13461203703bSJeremy L Thompson   CeedCheck(block_size * block <= num_elem, ceed, CEED_ERROR_DIMENSION,
1347701e7d35SJeremy L Thompson             "Cannot retrieve block %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", block, block_size * block,
1348701e7d35SJeremy L Thompson             num_elem);
13492b730f8bSJeremy L Thompson   CeedCall(rstr->ApplyBlock(rstr, block, t_mode, u, ru, request));
1350e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1351be9261b7Sjeremylt }
1352be9261b7Sjeremylt 
1353be9261b7Sjeremylt /**
1354ca94c3ddSJeremy L Thompson   @brief Get the `Ceed` associated with a `CeedElemRestriction`
1355b7c9bbdaSJeremy L Thompson 
1356ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
1357ca94c3ddSJeremy L Thompson   @param[out] ceed Variable to store `Ceed`
1358b7c9bbdaSJeremy L Thompson 
1359b7c9bbdaSJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1360b7c9bbdaSJeremy L Thompson 
1361b7c9bbdaSJeremy L Thompson   @ref Advanced
1362b7c9bbdaSJeremy L Thompson **/
1363b7c9bbdaSJeremy L Thompson int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) {
1364*6e536b99SJeremy L Thompson   *ceed = CeedElemRestrictionReturnCeed(rstr);
1365b7c9bbdaSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1366b7c9bbdaSJeremy L Thompson }
1367b7c9bbdaSJeremy L Thompson 
1368b7c9bbdaSJeremy L Thompson /**
1369*6e536b99SJeremy L Thompson   @brief Return the `Ceed` associated with a `CeedElemRestriction`
1370*6e536b99SJeremy L Thompson 
1371*6e536b99SJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
1372*6e536b99SJeremy L Thompson 
1373*6e536b99SJeremy L Thompson   @return `Ceed` associated with the `rstr`
1374*6e536b99SJeremy L Thompson 
1375*6e536b99SJeremy L Thompson   @ref Advanced
1376*6e536b99SJeremy L Thompson **/
1377*6e536b99SJeremy L Thompson Ceed CeedElemRestrictionReturnCeed(CeedElemRestriction rstr) { return rstr->ceed; }
1378*6e536b99SJeremy L Thompson 
1379*6e536b99SJeremy L Thompson /**
1380d979a051Sjeremylt   @brief Get the L-vector component stride
1381a681ae63Sjeremylt 
1382ca94c3ddSJeremy L Thompson   @param[in]  rstr        `CeedElemRestriction`
1383d1d35e2fSjeremylt   @param[out] comp_stride Variable to store component stride
1384a681ae63Sjeremylt 
1385a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1386a681ae63Sjeremylt 
1387b7c9bbdaSJeremy L Thompson   @ref Advanced
1388a681ae63Sjeremylt **/
13892b730f8bSJeremy L Thompson int CeedElemRestrictionGetCompStride(CeedElemRestriction rstr, CeedInt *comp_stride) {
1390d1d35e2fSjeremylt   *comp_stride = rstr->comp_stride;
1391e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1392a681ae63Sjeremylt }
1393a681ae63Sjeremylt 
1394a681ae63Sjeremylt /**
1395ca94c3ddSJeremy L Thompson   @brief Get the total number of elements in the range of a `CeedElemRestriction`
1396a681ae63Sjeremylt 
1397ca94c3ddSJeremy L Thompson   @param[in] rstr      `CeedElemRestriction`
1398d1d35e2fSjeremylt   @param[out] num_elem Variable to store number of elements
1399a681ae63Sjeremylt 
1400a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1401a681ae63Sjeremylt 
1402b7c9bbdaSJeremy L Thompson   @ref Advanced
1403a681ae63Sjeremylt **/
14042b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumElements(CeedElemRestriction rstr, CeedInt *num_elem) {
1405d1d35e2fSjeremylt   *num_elem = rstr->num_elem;
1406e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1407a681ae63Sjeremylt }
1408a681ae63Sjeremylt 
1409a681ae63Sjeremylt /**
1410ca94c3ddSJeremy L Thompson   @brief Get the size of elements in the `CeedElemRestriction`
1411a681ae63Sjeremylt 
1412ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
1413d1d35e2fSjeremylt   @param[out] elem_size Variable to store size of elements
1414a681ae63Sjeremylt 
1415a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1416a681ae63Sjeremylt 
1417b7c9bbdaSJeremy L Thompson   @ref Advanced
1418a681ae63Sjeremylt **/
14192b730f8bSJeremy L Thompson int CeedElemRestrictionGetElementSize(CeedElemRestriction rstr, CeedInt *elem_size) {
1420d1d35e2fSjeremylt   *elem_size = rstr->elem_size;
14212c7e7413SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14222c7e7413SJeremy L Thompson }
14232c7e7413SJeremy L Thompson 
14242c7e7413SJeremy L Thompson /**
142507d5dec1SJeremy L Thompson 
1426ca94c3ddSJeremy L Thompson   @brief Get the number of points in the l-vector for a points `CeedElemRestriction`
142707d5dec1SJeremy L Thompson 
1428ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
142907d5dec1SJeremy L Thompson   @param[out] num_points The number of points in the l-vector
143007d5dec1SJeremy L Thompson 
143107d5dec1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
143207d5dec1SJeremy L Thompson 
1433c63574e3SJeremy L Thompson   @ref User
143407d5dec1SJeremy L Thompson **/
143507d5dec1SJeremy L Thompson int CeedElemRestrictionGetNumPoints(CeedElemRestriction rstr, CeedInt *num_points) {
14361203703bSJeremy L Thompson   CeedRestrictionType rstr_type;
143707d5dec1SJeremy L Thompson 
14381203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1439*6e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
144007d5dec1SJeremy L Thompson             "Can only retrieve the number of points for a points CeedElemRestriction");
144107d5dec1SJeremy L Thompson 
144207d5dec1SJeremy L Thompson   *num_points = rstr->num_points;
144307d5dec1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
144407d5dec1SJeremy L Thompson }
144507d5dec1SJeremy L Thompson 
144607d5dec1SJeremy L Thompson /**
144707d5dec1SJeremy L Thompson 
1448ca94c3ddSJeremy L Thompson   @brief Get the number of points in an element of a `CeedElemRestriction` at points
144907d5dec1SJeremy L Thompson 
1450ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
145107d5dec1SJeremy L Thompson   @param[in]  elem       Index number of element to retrieve the number of points for
145207d5dec1SJeremy L Thompson   @param[out] num_points The number of points in the element at index elem
145307d5dec1SJeremy L Thompson 
145407d5dec1SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
145507d5dec1SJeremy L Thompson 
1456c63574e3SJeremy L Thompson   @ref User
145707d5dec1SJeremy L Thompson **/
145807d5dec1SJeremy L Thompson int CeedElemRestrictionGetNumPointsInElement(CeedElemRestriction rstr, CeedInt elem, CeedInt *num_points) {
14591203703bSJeremy L Thompson   CeedRestrictionType rstr_type;
146007d5dec1SJeremy L Thompson   const CeedInt      *offsets;
146107d5dec1SJeremy L Thompson 
14621203703bSJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1463*6e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
146407d5dec1SJeremy L Thompson             "Can only retrieve the number of points for a points CeedElemRestriction");
146507d5dec1SJeremy L Thompson 
146607d5dec1SJeremy L Thompson   CeedCall(CeedElemRestrictionGetOffsets(rstr, CEED_MEM_HOST, &offsets));
146707d5dec1SJeremy L Thompson   *num_points = offsets[elem + 1] - offsets[elem];
146807d5dec1SJeremy L Thompson   CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets));
146907d5dec1SJeremy L Thompson   return CEED_ERROR_SUCCESS;
147007d5dec1SJeremy L Thompson }
147107d5dec1SJeremy L Thompson 
147207d5dec1SJeremy L Thompson /**
1473ca94c3ddSJeremy L Thompson   @brief Get the maximum number of points in an element for a `CeedElemRestriction` at points
14742c7e7413SJeremy L Thompson 
1475ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
14762c7e7413SJeremy L Thompson   @param[out] max_points Variable to store size of elements
14772c7e7413SJeremy L Thompson 
14782c7e7413SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
14792c7e7413SJeremy L Thompson 
14802c7e7413SJeremy L Thompson   @ref Advanced
14812c7e7413SJeremy L Thompson **/
14822c7e7413SJeremy L Thompson int CeedElemRestrictionGetMaxPointsInElement(CeedElemRestriction rstr, CeedInt *max_points) {
14832c7e7413SJeremy L Thompson   CeedInt             num_elem;
14842c7e7413SJeremy L Thompson   CeedRestrictionType rstr_type;
14852c7e7413SJeremy L Thompson 
14862c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
1487*6e536b99SJeremy L Thompson   CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE,
14882c7e7413SJeremy L Thompson             "Cannot compute max points for a CeedElemRestriction that does not use points");
14892c7e7413SJeremy L Thompson 
14902c7e7413SJeremy L Thompson   CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem));
14912c7e7413SJeremy L Thompson   *max_points = 0;
14922c7e7413SJeremy L Thompson   for (CeedInt e = 0; e < num_elem; e++) {
14932c7e7413SJeremy L Thompson     CeedInt num_points;
14942c7e7413SJeremy L Thompson 
14952c7e7413SJeremy L Thompson     CeedCall(CeedElemRestrictionGetNumPointsInElement(rstr, e, &num_points));
14962c7e7413SJeremy L Thompson     *max_points = CeedIntMax(num_points, *max_points);
14972c7e7413SJeremy L Thompson   }
1498e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1499a681ae63Sjeremylt }
1500a681ae63Sjeremylt 
1501a681ae63Sjeremylt /**
1502ca94c3ddSJeremy L Thompson   @brief Get the size of the l-vector for a `CeedElemRestriction`
1503a681ae63Sjeremylt 
1504ca94c3ddSJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction`
1505701e7d35SJeremy L Thompson   @param[out] l_size Variable to store l-vector size
1506a681ae63Sjeremylt 
1507a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1508a681ae63Sjeremylt 
1509b7c9bbdaSJeremy L Thompson   @ref Advanced
1510a681ae63Sjeremylt **/
15112b730f8bSJeremy L Thompson int CeedElemRestrictionGetLVectorSize(CeedElemRestriction rstr, CeedSize *l_size) {
1512d1d35e2fSjeremylt   *l_size = rstr->l_size;
1513e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1514a681ae63Sjeremylt }
1515a681ae63Sjeremylt 
1516a681ae63Sjeremylt /**
1517701e7d35SJeremy L Thompson   @brief Get the size of the e-vector for a `CeedElemRestriction`
1518701e7d35SJeremy L Thompson 
1519701e7d35SJeremy L Thompson   @param[in]  rstr   `CeedElemRestriction`
1520701e7d35SJeremy L Thompson   @param[out] e_size Variable to store e-vector size
1521701e7d35SJeremy L Thompson 
1522701e7d35SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1523701e7d35SJeremy L Thompson 
1524701e7d35SJeremy L Thompson   @ref Advanced
1525701e7d35SJeremy L Thompson **/
1526701e7d35SJeremy L Thompson int CeedElemRestrictionGetEVectorSize(CeedElemRestriction rstr, CeedSize *e_size) {
1527701e7d35SJeremy L Thompson   *e_size = rstr->e_size;
1528701e7d35SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1529701e7d35SJeremy L Thompson }
1530701e7d35SJeremy L Thompson 
1531701e7d35SJeremy L Thompson /**
1532ca94c3ddSJeremy L Thompson   @brief Get the number of components in the elements of a `CeedElemRestriction`
1533a681ae63Sjeremylt 
1534ca94c3ddSJeremy L Thompson   @param[in]  rstr     `CeedElemRestriction`
1535d1d35e2fSjeremylt   @param[out] num_comp Variable to store number of components
1536a681ae63Sjeremylt 
1537a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1538a681ae63Sjeremylt 
1539b7c9bbdaSJeremy L Thompson   @ref Advanced
1540a681ae63Sjeremylt **/
15412b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumComponents(CeedElemRestriction rstr, CeedInt *num_comp) {
1542d1d35e2fSjeremylt   *num_comp = rstr->num_comp;
1543e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1544a681ae63Sjeremylt }
1545a681ae63Sjeremylt 
1546a681ae63Sjeremylt /**
1547ca94c3ddSJeremy L Thompson   @brief Get the number of blocks in a `CeedElemRestriction`
1548a681ae63Sjeremylt 
1549ca94c3ddSJeremy L Thompson   @param[in]  rstr      `CeedElemRestriction`
1550d1d35e2fSjeremylt   @param[out] num_block Variable to store number of blocks
1551a681ae63Sjeremylt 
1552a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1553a681ae63Sjeremylt 
1554b7c9bbdaSJeremy L Thompson   @ref Advanced
1555a681ae63Sjeremylt **/
15562b730f8bSJeremy L Thompson int CeedElemRestrictionGetNumBlocks(CeedElemRestriction rstr, CeedInt *num_block) {
1557e7f679fcSJeremy L Thompson   *num_block = rstr->num_block;
1558e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1559a681ae63Sjeremylt }
1560a681ae63Sjeremylt 
1561a681ae63Sjeremylt /**
1562ca94c3ddSJeremy L Thompson   @brief Get the size of blocks in the `CeedElemRestriction`
1563a681ae63Sjeremylt 
1564ca94c3ddSJeremy L Thompson   @param[in]  rstr       `CeedElemRestriction`
1565e7f679fcSJeremy L Thompson   @param[out] block_size Variable to store size of blocks
1566a681ae63Sjeremylt 
1567a681ae63Sjeremylt   @return An error code: 0 - success, otherwise - failure
1568a681ae63Sjeremylt 
1569b7c9bbdaSJeremy L Thompson   @ref Advanced
1570a681ae63Sjeremylt **/
1571e7f679fcSJeremy L Thompson int CeedElemRestrictionGetBlockSize(CeedElemRestriction rstr, CeedInt *block_size) {
1572e7f679fcSJeremy L Thompson   *block_size = rstr->block_size;
1573e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1574a681ae63Sjeremylt }
1575a681ae63Sjeremylt 
1576a681ae63Sjeremylt /**
1577ca94c3ddSJeremy L Thompson   @brief Get the multiplicity of nodes in a `CeedElemRestriction`
15781469ee4dSjeremylt 
1579ca94c3ddSJeremy L Thompson   @param[in]  rstr `CeedElemRestriction`
1580ca94c3ddSJeremy L Thompson   @param[out] mult Vector to store multiplicity (of size `l_size`)
15811469ee4dSjeremylt 
15821469ee4dSjeremylt   @return An error code: 0 - success, otherwise - failure
15831469ee4dSjeremylt 
15847a982d89SJeremy L. Thompson   @ref User
15851469ee4dSjeremylt **/
15862b730f8bSJeremy L Thompson int CeedElemRestrictionGetMultiplicity(CeedElemRestriction rstr, CeedVector mult) {
1587d1d35e2fSjeremylt   CeedVector e_vec;
15881469ee4dSjeremylt 
158925509ebbSRezgar Shakeri   // Create e_vec to hold intermediate computation in E^T (E 1)
15902b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionCreateVector(rstr, NULL, &e_vec));
15911469ee4dSjeremylt 
159225509ebbSRezgar Shakeri   // Compute e_vec = E * 1
15932b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 1.0));
15942b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_NOTRANSPOSE, mult, e_vec, CEED_REQUEST_IMMEDIATE));
159525509ebbSRezgar Shakeri   // Compute multiplicity, mult = E^T * e_vec = E^T (E 1)
15962b730f8bSJeremy L Thompson   CeedCall(CeedVectorSetValue(mult, 0.0));
15972b730f8bSJeremy L Thompson   CeedCall(CeedElemRestrictionApply(rstr, CEED_TRANSPOSE, e_vec, mult, CEED_REQUEST_IMMEDIATE));
15981469ee4dSjeremylt   // Cleanup
15992b730f8bSJeremy L Thompson   CeedCall(CeedVectorDestroy(&e_vec));
1600e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16011469ee4dSjeremylt }
16021469ee4dSjeremylt 
16031469ee4dSjeremylt /**
1604ca94c3ddSJeremy L Thompson   @brief View a `CeedElemRestriction`
1605f02ca4a2SJed Brown 
1606ca94c3ddSJeremy L Thompson   @param[in] rstr   `CeedElemRestriction` to view
1607ca94c3ddSJeremy L Thompson   @param[in] stream Stream to write; typically `stdout` or a file
1608f02ca4a2SJed Brown 
1609f02ca4a2SJed Brown   @return Error code: 0 - success, otherwise - failure
1610f02ca4a2SJed Brown 
16117a982d89SJeremy L. Thompson   @ref User
1612f02ca4a2SJed Brown **/
1613f02ca4a2SJed Brown int CeedElemRestrictionView(CeedElemRestriction rstr, FILE *stream) {
16140930e4e7SJeremy L Thompson   CeedRestrictionType rstr_type;
16150930e4e7SJeremy L Thompson 
16160930e4e7SJeremy L Thompson   CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type));
16170930e4e7SJeremy L Thompson   if (rstr_type == CEED_RESTRICTION_POINTS) {
16180930e4e7SJeremy L Thompson     CeedInt max_points;
16190930e4e7SJeremy L Thompson 
16200930e4e7SJeremy L Thompson     CeedCall(CeedElemRestrictionGetMaxPointsInElement(rstr, &max_points));
16210930e4e7SJeremy L Thompson     fprintf(stream,
1622249f8407SJeremy L Thompson             "CeedElemRestriction at points from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with a maximum of %" CeedInt_FMT
16230930e4e7SJeremy L Thompson             " points on an element\n",
16240930e4e7SJeremy L Thompson             rstr->l_size, rstr->num_comp, rstr->num_elem, max_points);
16250930e4e7SJeremy L Thompson   } else {
1626249f8407SJeremy L Thompson     char strides_str[500];
16271c66c397SJeremy L Thompson 
16282b730f8bSJeremy L Thompson     if (rstr->strides) {
1629249f8407SJeremy L Thompson       sprintf(strides_str, "[%" CeedInt_FMT ", %" CeedInt_FMT ", %" CeedInt_FMT "]", rstr->strides[0], rstr->strides[1], rstr->strides[2]);
16302b730f8bSJeremy L Thompson     } else {
1631249f8407SJeremy L Thompson       sprintf(strides_str, "%" CeedInt_FMT, rstr->comp_stride);
16322b730f8bSJeremy L Thompson     }
1633249f8407SJeremy L Thompson     fprintf(stream,
1634249f8407SJeremy L Thompson             "%sCeedElemRestriction from (%" CeedSize_FMT ", %" CeedInt_FMT ") to %" CeedInt_FMT " elements with %" CeedInt_FMT
1635249f8407SJeremy L Thompson             " nodes each and %s %s\n",
1636e7f679fcSJeremy L Thompson             rstr->block_size > 1 ? "Blocked " : "", rstr->l_size, rstr->num_comp, rstr->num_elem, rstr->elem_size,
1637249f8407SJeremy L Thompson             rstr->strides ? "strides" : "component stride", strides_str);
16380930e4e7SJeremy L Thompson   }
1639e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1640f02ca4a2SJed Brown }
1641f02ca4a2SJed Brown 
1642f02ca4a2SJed Brown /**
1643ca94c3ddSJeremy L Thompson   @brief Destroy a `CeedElemRestriction`
1644b11c1e72Sjeremylt 
1645ca94c3ddSJeremy L Thompson   @param[in,out] rstr `CeedElemRestriction` to destroy
1646b11c1e72Sjeremylt 
1647b11c1e72Sjeremylt   @return An error code: 0 - success, otherwise - failure
1648dfdf5a53Sjeremylt 
16497a982d89SJeremy L. Thompson   @ref User
1650b11c1e72Sjeremylt **/
16514ce2993fSjeremylt int CeedElemRestrictionDestroy(CeedElemRestriction *rstr) {
1652393ac2cdSJeremy L Thompson   if (!*rstr || *rstr == CEED_ELEMRESTRICTION_NONE || --(*rstr)->ref_count > 0) {
1653ad6481ceSJeremy L Thompson     *rstr = NULL;
1654ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
1655ad6481ceSJeremy L Thompson   }
16566574a04fSJeremy L Thompson   CeedCheck((*rstr)->num_readers == 0, (*rstr)->ceed, CEED_ERROR_ACCESS,
16576574a04fSJeremy L Thompson             "Cannot destroy CeedElemRestriction, a process has read access to the offset data");
1658c17ec2beSJeremy L Thompson 
1659c17ec2beSJeremy L Thompson   // Only destroy backend data once between rstr and unsigned copy
16607c1dbaffSSebastian Grimberg   if ((*rstr)->rstr_base) CeedCall(CeedElemRestrictionDestroy(&(*rstr)->rstr_base));
1661c17ec2beSJeremy L Thompson   else if ((*rstr)->Destroy) CeedCall((*rstr)->Destroy(*rstr));
1662c17ec2beSJeremy L Thompson 
16632b730f8bSJeremy L Thompson   CeedCall(CeedFree(&(*rstr)->strides));
16642b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*rstr)->ceed));
16652b730f8bSJeremy L Thompson   CeedCall(CeedFree(rstr));
1666e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1667d7b241e6Sjeremylt }
1668d7b241e6Sjeremylt 
1669d7b241e6Sjeremylt /// @}
1670