xref: /libCEED/tests/t208-elemrestriction.c (revision 151157ab1cfc57bfbb9fa07ee95eb9b023c32737)
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 blk_size = 5;
13   CeedInt ind[elem_size*num_elem];
14   CeedScalar a[num_elem + 1];
15   const CeedScalar *xx, *yy;
16   CeedInt layout[3];
17   CeedElemRestriction r;
18 
19   CeedInit(argv[1], &ceed);
20 
21   CeedVectorCreate(ceed, num_elem+1, &x);
22   for (CeedInt i=0; i<num_elem+1; i++)
23     a[i] = 10 + i;
24   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
25 
26   for (CeedInt i=0; i<num_elem; i++) {
27     ind[2*i+0] = i;
28     ind[2*i+1] = i+1;
29   }
30   CeedElemRestrictionCreateBlocked(ceed, num_elem, elem_size, blk_size, 1, 1,
31                                    num_elem+1, CEED_MEM_HOST, CEED_USE_POINTER,
32                                    ind, &r);
33   CeedVectorCreate(ceed, blk_size*elem_size, &y);
34 
35   // NoTranspose
36   CeedElemRestrictionApplyBlock(r, 1, CEED_NOTRANSPOSE, x, y,
37                                 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=blk_size; 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 - blk_size*elem_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   CeedElemRestrictionApplyBlock(r, 1, CEED_TRANSPOSE, y, x,
59                                 CEED_REQUEST_IMMEDIATE);
60   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &xx);
61   for (CeedInt i=blk_size; i<num_elem+1; i++)
62     if (xx[i] != (10+i)*(i > blk_size && i < num_elem ? 2.0 : 1.0))
63       // LCOV_EXCL_START
64       printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n",
65              i, (double)xx[i]);
66   // LCOV_EXCL_STOP
67   CeedVectorRestoreArrayRead(x, &xx);
68 
69   CeedVectorDestroy(&x);
70   CeedVectorDestroy(&y);
71   CeedElemRestrictionDestroy(&r);
72   CeedDestroy(&ceed);
73   return 0;
74 }
75