xref: /libCEED/tests/t207-elemrestriction.c (revision 859c15bb6fe60f9064a52dbb084dbd4855ef54f7)
1 /// @file
2 /// Test creation, transpose use, and destruction of an interlaced multi-component element restriction
3 /// \test Test creation, transpose use, and destruction of an interlaced multi-component 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 = 5;
11   CeedInt             ind[2 * num_elem];
12   CeedInt             layout[3];
13   CeedScalar          mult;
14   CeedElemRestriction elem_restriction;
15 
16   CeedInit(argv[1], &ceed);
17   CeedVectorCreate(ceed, 2 * (num_elem + 1), &y);
18   CeedVectorSetValue(y, 0.0);  // Allocates array
19 
20   // Setup
21   CeedVectorCreate(ceed, 2 * (num_elem * 2), &x);
22 
23   for (CeedInt i = 0; i < num_elem; i++) {
24     ind[2 * i + 0] = 2 * i;
25     ind[2 * i + 1] = 2 * (i + 1);
26   }
27   CeedElemRestrictionCreate(ceed, num_elem, 2, 2, 1, 2 * (num_elem + 1), CEED_MEM_HOST, CEED_USE_POINTER, ind, &elem_restriction);
28 
29   // Set x data in backend E-layout
30   CeedElemRestrictionGetELayout(elem_restriction, &layout);
31   {
32     CeedScalar x_array[2 * (num_elem * 2)];
33 
34     for (CeedInt i = 0; i < 2; i++) {             // Node
35       for (CeedInt j = 0; j < 2; j++) {           // Component
36         for (CeedInt k = 0; k < num_elem; k++) {  // Element
37           x_array[i * layout[0] + j * layout[1] + k * layout[2]] = 10 * j + (2 * k + i + 1) / 2;
38         }
39       }
40     }
41     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
42   }
43 
44   // Restrict
45   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
46 
47   // Check
48   {
49     const CeedScalar *y_array;
50 
51     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
52     for (CeedInt i = 0; i < num_elem + 1; i++) {
53       mult = i > 0 && i < num_elem ? 2 : 1;
54       if (y_array[2 * i] != i * mult) {
55         // LCOV_EXCL_START
56         printf("Error in restricted array y[%" CeedInt_FMT "] = %f != %f\n", 2 * i, (CeedScalar)y_array[2 * i], i * mult);
57         // LCOV_EXCL_STOP
58       }
59       if (y_array[2 * i + 1] != (10 + i) * mult) {
60         // LCOV_EXCL_START
61         printf("Error in restricted array y[%" CeedInt_FMT "] = %f != %f\n", 2 * i + 1, (CeedScalar)y_array[2 * i + 1], (10. + i) * mult);
62         // LCOV_EXCL_STOP
63       }
64     }
65     CeedVectorRestoreArrayRead(y, &y_array);
66   }
67 
68   CeedVectorDestroy(&x);
69   CeedVectorDestroy(&y);
70   CeedElemRestrictionDestroy(&elem_restriction);
71   CeedDestroy(&ceed);
72   return 0;
73 }
74