xref: /libCEED/tests/t213-elemrestriction.c (revision 8ec64e9ae9d5df169dba8c8ee61d8ec8907b8f80)
1 /// @file
2 /// Test creation, use, and destruction of a blocked element restriction with multiple components in the lvector, and libCEED owns ind pointer
3 /// \test Test creation, use, and destruction of a blocked element restriction with multiple components in the lvector, and libCEED owns ind pointer
4 #include <ceed.h>
5 #include <ceed/backend.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 int main(int argc, char **argv) {
10   Ceed                ceed;
11   CeedVector          x, y;
12   CeedInt             num_elem  = 8;
13   CeedInt             elem_size = 2;
14   CeedInt             num_blk   = 2;
15   CeedInt             blk_size  = 5;
16   CeedInt             num_comp  = 3;
17   CeedInt             ind[elem_size * num_elem];
18   CeedInt            *ceed_ind = malloc(sizeof(CeedInt) * elem_size * num_elem);
19   CeedScalar          a[num_comp * (num_elem + 1)];
20   const CeedScalar   *xx, *yy;
21   CeedInt             layout[3];
22   CeedElemRestriction r;
23 
24   CeedInit(argv[1], &ceed);
25 
26   CeedVectorCreate(ceed, num_comp * (num_elem + 1), &x);
27   for (CeedInt i = 0; i < num_elem + 1; i++) {
28     a[i + 0 * (num_elem + 1)] = 10 + i;
29     a[i + 1 * (num_elem + 1)] = 20 + i;
30     a[i + 2 * (num_elem + 1)] = 30 + i;
31   }
32   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
33 
34   for (CeedInt i = 0; i < num_elem; i++) {
35     ind[2 * i + 0] = i;
36     ind[2 * i + 1] = i + 1;
37   }
38   memcpy(ceed_ind, ind, sizeof(CeedInt) * elem_size * num_elem);
39   CeedElemRestrictionCreateBlocked(ceed, num_elem, elem_size, blk_size, num_comp, num_elem + 1, num_comp * (num_elem + 1), CEED_MEM_HOST,
40                                    CEED_OWN_POINTER, ceed_ind, &r);
41   CeedVectorCreate(ceed, num_comp * num_blk * blk_size * elem_size, &y);
42 
43   // NoTranspose
44   CeedElemRestrictionApply(r, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
45   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
46   CeedElemRestrictionGetELayout(r, &layout);
47   for (CeedInt i = 0; i < elem_size; i++) {     // Node
48     for (CeedInt j = 0; j < num_comp; j++) {    // Component
49       for (CeedInt k = 0; k < num_elem; k++) {  // Element
50         CeedInt block = k / blk_size;
51         CeedInt elem  = k % blk_size;
52         CeedInt index = (i * blk_size + elem) * layout[0] + j * layout[1] * blk_size + block * layout[2] * blk_size;
53         if (yy[index] != a[ind[k * elem_size + i] + j * (num_elem + 1)])
54           // LCOV_EXCL_START
55           printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k, (double)yy[index]);
56         // LCOV_EXCL_STOP
57       }
58     }
59   }
60   CeedVectorRestoreArrayRead(y, &yy);
61 
62   // Transpose
63   CeedVectorSetValue(x, 0);
64   CeedElemRestrictionApply(r, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
65   CeedVectorGetArrayRead(x, CEED_MEM_HOST, &xx);
66   for (CeedInt i = 0; i < num_elem + 1; i++) {
67     for (CeedInt j = 0; j < num_comp; j++) {
68       if (xx[i + j * (num_elem + 1)] != ((j + 1) * 10 + i) * (i > 0 && i < num_elem ? 2.0 : 1.0)) {
69         // LCOV_EXCL_START
70         printf("Error in restricted array x[%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", j, i, (double)xx[i + j * (num_elem + 1)]);
71         // LCOV_EXCL_STOP
72       }
73     }
74   }
75   CeedVectorRestoreArrayRead(x, &xx);
76 
77   CeedVectorDestroy(&x);
78   CeedVectorDestroy(&y);
79   CeedElemRestrictionDestroy(&r);
80   CeedDestroy(&ceed);
81   return 0;
82 }
83