xref: /libCEED/tests/t213-elemrestriction.c (revision 0126412d32e96658dcd899fe3d8b989fdf201e32)
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          x_array[num_comp * (num_elem + 1)];
20   CeedInt             layout[3];
21   CeedElemRestriction elem_restriction;
22 
23   CeedInit(argv[1], &ceed);
24 
25   CeedVectorCreate(ceed, num_comp * (num_elem + 1), &x);
26   for (CeedInt i = 0; i < num_elem + 1; i++) {
27     x_array[i + 0 * (num_elem + 1)] = 10 + i;
28     x_array[i + 1 * (num_elem + 1)] = 20 + i;
29     x_array[i + 2 * (num_elem + 1)] = 30 + i;
30   }
31   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
32   CeedVectorCreate(ceed, num_comp * num_blk * blk_size * elem_size, &y);
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, &elem_restriction);
41 
42   // NoTranspose
43   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
44   {
45     const CeedScalar *y_array;
46 
47     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
48     CeedElemRestrictionGetELayout(elem_restriction, &layout);
49     for (CeedInt i = 0; i < elem_size; i++) {     // Node
50       for (CeedInt j = 0; j < num_comp; j++) {    // Component
51         for (CeedInt k = 0; k < num_elem; k++) {  // Element
52           CeedInt block = k / blk_size;
53           CeedInt elem  = k % blk_size;
54           CeedInt index = (i * blk_size + elem) * layout[0] + j * layout[1] * blk_size + block * layout[2] * blk_size;
55           if (y_array[index] != x_array[ind[k * elem_size + i] + j * (num_elem + 1)])
56             // LCOV_EXCL_START
57             printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", i, j, k, (double)y_array[index]);
58           // LCOV_EXCL_STOP
59         }
60       }
61     }
62     CeedVectorRestoreArrayRead(y, &y_array);
63   }
64 
65   // Transpose
66   CeedVectorSetValue(x, 0);
67   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
68   {
69     const CeedScalar *x_array;
70 
71     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array);
72     for (CeedInt i = 0; i < num_elem + 1; i++) {
73       for (CeedInt j = 0; j < num_comp; j++) {
74         if (x_array[i + j * (num_elem + 1)] != ((j + 1) * 10 + i) * (i > 0 && i < num_elem ? 2.0 : 1.0)) {
75           // LCOV_EXCL_START
76           printf("Error in restricted array x[%" CeedInt_FMT "][%" CeedInt_FMT "] = %f\n", j, i, (double)x_array[i + j * (num_elem + 1)]);
77           // LCOV_EXCL_STOP
78         }
79       }
80     }
81     CeedVectorRestoreArrayRead(x, &x_array);
82   }
83 
84   CeedVectorDestroy(&x);
85   CeedVectorDestroy(&y);
86   CeedElemRestrictionDestroy(&elem_restriction);
87   CeedDestroy(&ceed);
88   return 0;
89 }
90