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