xref: /libCEED/tests/t203-elemrestriction.c (revision 5571c6fd979b2d8a02ec737d0c535858266a543d)
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[%" CeedInt_FMT
54                  "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n",
55                  i, j, k, (double)yy[index]);
56         // LCOV_EXCL_STOP
57       }
58   CeedVectorRestoreArrayRead(y, &yy);
59 
60   // Transpose
61   CeedVectorSetValue(x, 0);
62   CeedElemRestrictionApply(r, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
63   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &xx);
64   for (CeedInt i=0; i<num_elem+1; i++) {
65     for (CeedInt j=0; j<num_comp; j++) {
66       if (xx[i+j*(num_elem+1)] != ((j+1)*10+i)*(i > 0 && i < num_elem ? 2.0 : 1.0))
67         // LCOV_EXCL_START
68         printf("Error in restricted array x[%" CeedInt_FMT
69                "][%" CeedInt_FMT "] = %f\n",
70                j, i, (double)xx[i+j*(num_elem+1)]);
71       // LCOV_EXCL_STOP
72     }
73   }
74   CeedVectorRestoreArrayRead(x, &xx);
75 
76   CeedVectorDestroy(&x);
77   CeedVectorDestroy(&y);
78   CeedElemRestrictionDestroy(&r);
79   CeedDestroy(&ceed);
80   return 0;
81 }
82