xref: /libCEED/tests/t208-elemrestriction.c (revision d310b3d31eeeddd20725517a3a61881a36d919f0)
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++) a[i] = 10 + i;
23   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
24 
25   for (CeedInt i = 0; i < num_elem; i++) {
26     ind[2 * i + 0] = i;
27     ind[2 * i + 1] = i + 1;
28   }
29   CeedElemRestrictionCreateBlocked(ceed, num_elem, elem_size, blk_size, 1, 1, num_elem + 1, CEED_MEM_HOST, CEED_USE_POINTER, ind, &r);
30   CeedVectorCreate(ceed, blk_size * elem_size, &y);
31 
32   // NoTranspose
33   CeedElemRestrictionApplyBlock(r, 1, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
34   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
35   CeedElemRestrictionGetELayout(r, &layout);
36   for (CeedInt i = 0; i < elem_size; i++) {            // Node
37     for (CeedInt j = 0; j < 1; j++) {                  // Component
38       for (CeedInt k = blk_size; k < num_elem; k++) {  // Element
39         CeedInt block = k / blk_size;
40         CeedInt elem  = k % blk_size;
41         CeedInt index = (i * blk_size + elem) * layout[0] + j * layout[1] * blk_size + block * layout[2] * blk_size - blk_size * elem_size;
42         if (yy[index] != a[ind[k * elem_size + i]]) {
43           // LCOV_EXCL_START
44           printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k, (double)yy[index]);
45           // LCOV_EXCL_STOP
46         }
47       }
48     }
49   }
50   CeedVectorRestoreArrayRead(y, &yy);
51 
52   // Transpose
53   CeedVectorSetValue(x, 0);
54   CeedElemRestrictionApplyBlock(r, 1, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
55   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &xx);
56   for (CeedInt i = blk_size; i < num_elem + 1; i++) {
57     if (xx[i] != (10 + i) * (i > blk_size && i < num_elem ? 2.0 : 1.0)) {
58       // LCOV_EXCL_START
59       printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", i, (double)xx[i]);
60       // LCOV_EXCL_STOP
61     }
62   }
63   CeedVectorRestoreArrayRead(x, &xx);
64 
65   CeedVectorDestroy(&x);
66   CeedVectorDestroy(&y);
67   CeedElemRestrictionDestroy(&r);
68   CeedDestroy(&ceed);
69   return 0;
70 }
71