xref: /libCEED/tests/t203-elemrestriction.c (revision 7578c8217078a59840b2b4a3f9726cf4a6bc1827)
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 a[num_comp*(num_elem + 1)];
17   const CeedScalar *xx, *yy;
18   CeedInt layout[3];
19   CeedElemRestriction r;
20 
21   CeedInit(argv[1], &ceed);
22 
23   CeedVectorCreate(ceed, num_comp*(num_elem+1), &x);
24   for (CeedInt i=0; i<num_elem+1; i++) {
25     a[i+0*(num_elem+1)] = 10 + i;
26     a[i+1*(num_elem+1)] = 20 + i;
27     a[i+2*(num_elem+1)] = 30 + i;
28   }
29   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
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,
36                                    num_elem+1, num_comp*(num_elem+1), CEED_MEM_HOST,
37                                    CEED_USE_POINTER, ind, &r);
38   CeedVectorCreate(ceed, num_comp*num_blk*blk_size*elem_size, &y);
39 
40   // NoTranspose
41   CeedElemRestrictionApply(r, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
42   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
43   CeedElemRestrictionGetELayout(r, &layout);
44   for (CeedInt i=0; i<elem_size; i++)      // Node
45     for (CeedInt j=0; j<num_comp; j++)     // Component
46       for (CeedInt k=0; k<num_elem; k++) { // Element
47         CeedInt block = k / blk_size;
48         CeedInt elem = k % blk_size;
49         CeedInt index = (i*blk_size+elem)*layout[0] + j*layout[1]*blk_size +
50                         block*layout[2]*blk_size;
51         if (yy[index] != a[ind[k*elem_size + i]+j*(num_elem+1)])
52           // LCOV_EXCL_START
53           printf("Error in restricted array y[%d][%d][%d] = %f\n",
54                  i, j, k, (double)yy[index]);
55         // LCOV_EXCL_STOP
56       }
57   CeedVectorRestoreArrayRead(y, &yy);
58 
59   // Transpose
60   CeedVectorSetValue(x, 0);
61   CeedElemRestrictionApply(r, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
62   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &xx);
63   for (CeedInt i=0; i<num_elem+1; i++) {
64     for (CeedInt j=0; j<num_comp; j++) {
65       if (xx[i+j*(num_elem+1)] != ((j+1)*10+i)*(i > 0 && i < num_elem ? 2.0 : 1.0))
66         // LCOV_EXCL_START
67         printf("Error in restricted array x[%d][%d] = %f\n",
68                j, i, (double)xx[i+j*(num_elem+1)]);
69       // LCOV_EXCL_STOP
70     }
71   }
72   CeedVectorRestoreArrayRead(x, &xx);
73 
74   CeedVectorDestroy(&x);
75   CeedVectorDestroy(&y);
76   CeedElemRestrictionDestroy(&r);
77   CeedDestroy(&ceed);
78   return 0;
79 }
80