xref: /libCEED/tests/t205-elemrestriction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
1 /// @file
2 /// Test creation, use, and destruction of an interlaced multicomponent element restriction
3 /// \test Test creation, 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 = 3;
11   CeedInt             ind[2 * num_elem];
12   CeedInt             layout[3];
13   CeedScalar          a[2 * (num_elem + 1)];
14   const CeedScalar   *yy;
15   CeedElemRestriction r;
16 
17   CeedInit(argv[1], &ceed);
18 
19   // Setup
20   CeedVectorCreate(ceed, 2 * (num_elem + 1), &x);
21   for (CeedInt i = 0; i < num_elem + 1; i++) {
22     a[2 * i]     = 10 + i;
23     a[2 * i + 1] = 20 + i;
24   }
25   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
26 
27   for (CeedInt i = 0; i < num_elem; i++) {
28     ind[2 * i + 0] = 2 * i;
29     ind[2 * i + 1] = 2 * (i + 1);
30   }
31   CeedElemRestrictionCreate(ceed, num_elem, 2, 2, 1, 2 * (num_elem + 1), CEED_MEM_HOST, CEED_USE_POINTER, ind, &r);
32   CeedVectorCreate(ceed, 2 * (num_elem * 2), &y);
33 
34   // Restrict
35   CeedElemRestrictionApply(r, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
36 
37   // Check
38   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
39   CeedElemRestrictionGetELayout(r, &layout);
40   for (CeedInt i = 0; i < 2; i++) {             // Node
41     for (CeedInt j = 0; j < 2; j++) {           // Component
42       for (CeedInt k = 0; k < num_elem; k++) {  // Element
43         if (yy[i * layout[0] + j * layout[1] + k * layout[2]] != a[ind[i + k * 2] + j]) {
44           // LCOV_EXCL_START
45           printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f != %f\n", i, j, k,
46                  (CeedScalar)yy[i * layout[0] + j * layout[1] + k * layout[2]], a[ind[i + k * 2] + j * (num_elem + 1)]);
47           // LCOV_EXCL_STOP
48         }
49       }
50     }
51   }
52 
53   CeedVectorRestoreArrayRead(y, &yy);
54   CeedVectorDestroy(&x);
55   CeedVectorDestroy(&y);
56   CeedElemRestrictionDestroy(&r);
57   CeedDestroy(&ceed);
58   return 0;
59 }
60