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