xref: /libCEED/tests/t201-elemrestriction.c (revision ca567da4bd07bc8e9c3c34f09a40e154feb42909)
1 /// @file
2 /// Test creation, use, and destruction of a strided element restriction
3 /// \test Test creation, use, and destruction of a strided element restriction
4 #include <ceed.h>
5 #include <ceed/backend.h>
6 
7 int main(int argc, char **argv) {
8   Ceed                ceed;
9   CeedVector          x, y;
10   CeedInt             num_elem   = 3;
11   CeedInt             strides[3] = {1, 2, 2};
12   CeedInt             layout[3];
13   CeedScalar          x_array[num_elem * 2];
14   CeedElemRestriction elem_restriction;
15 
16   CeedInit(argv[1], &ceed);
17 
18   CeedVectorCreate(ceed, num_elem * 2, &x);
19   for (CeedInt i = 0; i < num_elem * 2; i++) x_array[i] = 10 + i;
20   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
21   CeedVectorCreate(ceed, num_elem * 2, &y);
22 
23   CeedElemRestrictionCreateStrided(ceed, num_elem, 2, 1, num_elem * 2, strides, &elem_restriction);
24   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
25 
26   {
27     const CeedScalar *y_array;
28 
29     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
30     CeedElemRestrictionGetELayout(elem_restriction, &layout);
31     for (CeedInt i = 0; i < 2; i++) {             // Node
32       for (CeedInt j = 0; j < 1; j++) {           // Component
33         for (CeedInt k = 0; k < num_elem; k++) {  // Element
34           if (y_array[i * layout[0] + j * layout[1] + k * layout[2]] != x_array[i * strides[0] + j * strides[1] + k * strides[2]]) {
35             // LCOV_EXCL_START
36             printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k,
37                    (CeedScalar)y_array[i * strides[0] + j * strides[1] + j * strides[2]]);
38             // LCOV_EXCL_STOP
39           }
40         }
41       }
42     }
43     CeedVectorRestoreArrayRead(y, &y_array);
44   }
45 
46   CeedVectorDestroy(&x);
47   CeedVectorDestroy(&y);
48   CeedElemRestrictionDestroy(&elem_restriction);
49   CeedDestroy(&ceed);
50   return 0;
51 }
52