xref: /libCEED/tests/t207-elemrestriction.c (revision 49fd234cd5a6b1faaf6ecfc267b22ac88f378a38)
173d26085Sjeremylt /// @file
273d26085Sjeremylt /// Test creation, transpose use, and destruction of an interlaced multicomponent element restriction
373d26085Sjeremylt /// \test Test creation, transpose use, and destruction of an interlaced multicomponent element restriction
473d26085Sjeremylt #include <ceed.h>
5*49fd234cSJeremy L Thompson #include <ceed-backend.h>
673d26085Sjeremylt 
773d26085Sjeremylt int main(int argc, char **argv) {
873d26085Sjeremylt   Ceed ceed;
973d26085Sjeremylt   CeedVector x, y;
10*49fd234cSJeremy L Thompson   CeedInt ne = 5;
1173d26085Sjeremylt   CeedInt ind[2*ne];
12*49fd234cSJeremy L Thompson   CeedInt layout[3];
13*49fd234cSJeremy L Thompson   CeedScalar mult;
1473d26085Sjeremylt   CeedScalar a[2*(ne*2)];
1573d26085Sjeremylt   const CeedScalar *yy;
1673d26085Sjeremylt   CeedElemRestriction r;
1773d26085Sjeremylt 
1873d26085Sjeremylt   CeedInit(argv[1], &ceed);
1973d26085Sjeremylt 
2073d26085Sjeremylt   // Setup
2173d26085Sjeremylt   CeedVectorCreate(ceed, 2*(ne*2), &x);
2273d26085Sjeremylt 
2373d26085Sjeremylt   for (CeedInt i=0; i<ne; i++) {
24d979a051Sjeremylt     ind[2*i+0] = 2*i;
25d979a051Sjeremylt     ind[2*i+1] = 2*(i+1);
2673d26085Sjeremylt   }
27d979a051Sjeremylt   CeedElemRestrictionCreate(ceed, ne, 2, 2, 1, 2*(ne+1), CEED_MEM_HOST,
28288c0443SJeremy L Thompson                             CEED_USE_POINTER, ind, &r);
2973d26085Sjeremylt   CeedVectorCreate(ceed, 2*(ne+1), &y);
3073d26085Sjeremylt   CeedVectorSetValue(y, 0); // Allocates array
3173d26085Sjeremylt 
32*49fd234cSJeremy L Thompson   // Set x data in backend E-layout
33*49fd234cSJeremy L Thompson   CeedElemRestrictionGetELayout(r, &layout);
34*49fd234cSJeremy L Thompson   for (CeedInt i=0; i<2; i++)      // Node
35*49fd234cSJeremy L Thompson     for (CeedInt j=0; j<2; j++)    // Component
36*49fd234cSJeremy L Thompson       for (CeedInt k=0; k<ne; k++) // Element
37*49fd234cSJeremy L Thompson         a[i*layout[0] + j*layout[1] + k*layout[2]] = 10*j+(2*k+i+1)/2;
38*49fd234cSJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, a);
39*49fd234cSJeremy L Thompson 
4073d26085Sjeremylt   // Restrict
41a8d32208Sjeremylt   CeedElemRestrictionApply(r, CEED_TRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
4273d26085Sjeremylt 
4373d26085Sjeremylt   // Check
4473d26085Sjeremylt   CeedVectorGetArrayRead(y, CEED_MEM_HOST, &yy);
4573d26085Sjeremylt   for (CeedInt i=0; i<ne+1; i++) {
4673d26085Sjeremylt     mult = i>0&&i<ne ? 2 : 1;
47*49fd234cSJeremy L Thompson     if (yy[2*i] != i*mult)
48a2546046Sjeremylt       // LCOV_EXCL_START
4973d26085Sjeremylt       printf("Error in restricted array y[%d] = %f != %f\n",
50*49fd234cSJeremy L Thompson              2*i, (double)yy[2*i], i*mult);
51de996c55Sjeremylt     // LCOV_EXCL_STOP
52*49fd234cSJeremy L Thompson     if (yy[2*i+1] != (10+i)*mult)
53a2546046Sjeremylt       // LCOV_EXCL_START
5473d26085Sjeremylt       printf("Error in restricted array y[%d] = %f != %f\n",
55*49fd234cSJeremy L Thompson              2*i+1, (double)yy[2*i+1], (10.+i)*mult);
56de996c55Sjeremylt     // LCOV_EXCL_STOP
5773d26085Sjeremylt   }
5873d26085Sjeremylt 
5973d26085Sjeremylt   CeedVectorRestoreArrayRead(y, &yy);
6073d26085Sjeremylt   CeedVectorDestroy(&x);
6173d26085Sjeremylt   CeedVectorDestroy(&y);
6273d26085Sjeremylt   CeedElemRestrictionDestroy(&r);
6373d26085Sjeremylt   CeedDestroy(&ceed);
6473d26085Sjeremylt   return 0;
6573d26085Sjeremylt }
66