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