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