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