xref: /libCEED/tests/t205-elemrestriction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
173d26085Sjeremylt /// @file
2ceac335aSJeremy L Thompson /// Test creation, use, and destruction of an interlaced multi-component element restriction
3ceac335aSJeremy L Thompson /// \test Test creation, use, and destruction of an interlaced multi-component element restriction
473d26085Sjeremylt #include <ceed.h>
5ec3da8bcSJed Brown #include <ceed/backend.h>
6*49aac155SJeremy L Thompson #include <stdio.h>
773d26085Sjeremylt 
873d26085Sjeremylt int main(int argc, char **argv) {
973d26085Sjeremylt   Ceed                ceed;
1073d26085Sjeremylt   CeedVector          x, y;
11d1d35e2fSjeremylt   CeedInt             num_elem = 3;
12d1d35e2fSjeremylt   CeedInt             ind[2 * num_elem];
1349fd234cSJeremy L Thompson   CeedInt             layout[3];
144fee36f0SJeremy L Thompson   CeedScalar          x_array[2 * (num_elem + 1)];
154fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction;
1673d26085Sjeremylt 
1773d26085Sjeremylt   CeedInit(argv[1], &ceed);
1873d26085Sjeremylt 
1973d26085Sjeremylt   // Setup
20d1d35e2fSjeremylt   CeedVectorCreate(ceed, 2 * (num_elem + 1), &x);
21d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem + 1; i++) {
224fee36f0SJeremy L Thompson     x_array[2 * i]     = 10 + i;
234fee36f0SJeremy L Thompson     x_array[2 * i + 1] = 20 + i;
2473d26085Sjeremylt   }
254fee36f0SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
264fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, 2 * (num_elem * 2), &y);
2773d26085Sjeremylt 
28d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
29d979a051Sjeremylt     ind[2 * i + 0] = 2 * i;
30d979a051Sjeremylt     ind[2 * i + 1] = 2 * (i + 1);
3173d26085Sjeremylt   }
324fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 2, 1, 2 * (num_elem + 1), CEED_MEM_HOST, CEED_USE_POINTER, ind, &elem_restriction);
3373d26085Sjeremylt 
3473d26085Sjeremylt   // Restrict
354fee36f0SJeremy L Thompson   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
3673d26085Sjeremylt 
3773d26085Sjeremylt   // Check
384fee36f0SJeremy L Thompson   {
394fee36f0SJeremy L Thompson     const CeedScalar *y_array;
404fee36f0SJeremy L Thompson 
414fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
424fee36f0SJeremy L Thompson     CeedElemRestrictionGetELayout(elem_restriction, &layout);
432b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < 2; i++) {             // Node
442b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 2; j++) {           // Component
452b730f8bSJeremy L Thompson         for (CeedInt k = 0; k < num_elem; k++) {  // Element
464fee36f0SJeremy L Thompson           if (y_array[i * layout[0] + j * layout[1] + k * layout[2]] != x_array[ind[i + k * 2] + j]) {
47a2546046Sjeremylt             // LCOV_EXCL_START
482b730f8bSJeremy L Thompson             printf("Error in restricted array y[%" CeedInt_FMT "][%" CeedInt_FMT "][%" CeedInt_FMT "] = %f != %f\n", i, j, k,
494fee36f0SJeremy L Thompson                    (CeedScalar)y_array[i * layout[0] + j * layout[1] + k * layout[2]], x_array[ind[i + k * 2] + j * (num_elem + 1)]);
50de996c55Sjeremylt             // LCOV_EXCL_STOP
512b730f8bSJeremy L Thompson           }
522b730f8bSJeremy L Thompson         }
532b730f8bSJeremy L Thompson       }
542b730f8bSJeremy L Thompson     }
554fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(y, &y_array);
564fee36f0SJeremy L Thompson   }
5773d26085Sjeremylt 
5873d26085Sjeremylt   CeedVectorDestroy(&x);
5973d26085Sjeremylt   CeedVectorDestroy(&y);
604fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction);
6173d26085Sjeremylt   CeedDestroy(&ceed);
6273d26085Sjeremylt   return 0;
6373d26085Sjeremylt }
64