xref: /libCEED/tests/t203-elemrestriction.c (revision 5fb68f377259d3910de46d787b7c5d1587fd01e1)
1 /// @file
2 /// Test creation, use, and destruction of a blocked element restriction with multiple components in the lvector
3 /// \test Test creation, use, and destruction of a blocked element restriction with multiple components in the lvector
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  = 8;
11   CeedInt             elem_size = 2;
12   CeedInt             num_blk   = 2;
13   CeedInt             blk_size  = 5;
14   CeedInt             num_comp  = 3;
15   CeedInt             ind[elem_size * num_elem];
16   CeedScalar          x_array[num_comp * (num_elem + 1)];
17   CeedInt             layout[3];
18   CeedElemRestriction elem_restriction;
19 
20   CeedInit(argv[1], &ceed);
21 
22   CeedVectorCreate(ceed, num_comp * (num_elem + 1), &x);
23   for (CeedInt i = 0; i < num_elem + 1; i++) {
24     x_array[i + 0 * (num_elem + 1)] = 10 + i;
25     x_array[i + 1 * (num_elem + 1)] = 20 + i;
26     x_array[i + 2 * (num_elem + 1)] = 30 + i;
27   }
28   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
29   CeedVectorCreate(ceed, num_comp * num_blk * blk_size * elem_size, &y);
30 
31   for (CeedInt i = 0; i < num_elem; i++) {
32     ind[2 * i + 0] = i;
33     ind[2 * i + 1] = i + 1;
34   }
35   CeedElemRestrictionCreateBlocked(ceed, num_elem, elem_size, blk_size, num_comp, num_elem + 1, num_comp * (num_elem + 1), CEED_MEM_HOST,
36                                    CEED_USE_POINTER, ind, &elem_restriction);
37 
38   // NoTranspose
39   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
40   {
41     const CeedScalar *y_array;
42 
43     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
44     CeedElemRestrictionGetELayout(elem_restriction, &layout);
45     for (CeedInt i = 0; i < elem_size; i++) {     // Node
46       for (CeedInt j = 0; j < num_comp; j++) {    // Component
47         for (CeedInt k = 0; k < num_elem; k++) {  // Element
48           CeedInt block = k / blk_size;
49           CeedInt elem  = k % blk_size;
50           CeedInt index = (i * blk_size + elem) * layout[0] + j * layout[1] * blk_size + block * layout[2] * blk_size;
51           if (y_array[index] != x_array[ind[k * elem_size + i] + j * (num_elem + 1)]) {
52             // LCOV_EXCL_START
53             printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k, (double)y_array[index]);
54             // LCOV_EXCL_STOP
55           }
56         }
57       }
58     }
59     CeedVectorRestoreArrayRead(y, &y_array);
60   }
61 
62   // Transpose
63   CeedVectorSetValue(x, 0);
64   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
65   {
66     const CeedScalar *x_array;
67 
68     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array);
69     for (CeedInt i = 0; i < num_elem + 1; i++) {
70       for (CeedInt j = 0; j < num_comp; j++) {
71         if (x_array[i + j * (num_elem + 1)] != ((j + 1) * 10 + i) * (i > 0 && i < num_elem ? 2.0 : 1.0)) {
72           // LCOV_EXCL_START
73           printf("Error in restricted array x[%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", j, i, (double)x_array[i + j * (num_elem + 1)]);
74           // LCOV_EXCL_STOP
75         }
76       }
77     }
78     CeedVectorRestoreArrayRead(x, &x_array);
79   }
80 
81   CeedVectorDestroy(&x);
82   CeedVectorDestroy(&y);
83   CeedElemRestrictionDestroy(&elem_restriction);
84   CeedDestroy(&ceed);
85   return 0;
86 }
87