xref: /libCEED/tests/t217-elemrestriction.c (revision 01ecf5b4a3895d33c3d6fd6ee44ca505a2fd4980)
1bd403d52SSebastian Grimberg /// @file
2bd403d52SSebastian Grimberg /// Test creation, use, and destruction of an oriented element restriction
3bd403d52SSebastian Grimberg /// \test Test creation, use, and destruction of an oriented element restriction
4bd403d52SSebastian Grimberg #include <ceed.h>
5bd403d52SSebastian Grimberg #include <stdio.h>
6bd403d52SSebastian Grimberg 
main(int argc,char ** argv)7bd403d52SSebastian Grimberg int main(int argc, char **argv) {
8bd403d52SSebastian Grimberg   Ceed                ceed;
9bd403d52SSebastian Grimberg   CeedVector          x, y;
10bd403d52SSebastian Grimberg   CeedInt             num_elem = 6, elem_size = 2;
11bd403d52SSebastian Grimberg   CeedInt             ind[elem_size * num_elem];
12bd403d52SSebastian Grimberg   bool                orients[elem_size * num_elem];
13bd403d52SSebastian Grimberg   CeedScalar          x_array[num_elem + 1];
14bd403d52SSebastian Grimberg   CeedElemRestriction elem_restriction;
15bd403d52SSebastian Grimberg 
16bd403d52SSebastian Grimberg   CeedInit(argv[1], &ceed);
17bd403d52SSebastian Grimberg 
18bd403d52SSebastian Grimberg   CeedVectorCreate(ceed, num_elem + 1, &x);
19bd403d52SSebastian Grimberg   for (CeedInt i = 0; i < num_elem + 1; i++) x_array[i] = 10 + i;
20bd403d52SSebastian Grimberg   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
21bd403d52SSebastian Grimberg   CeedVectorCreate(ceed, num_elem * elem_size, &y);
22bd403d52SSebastian Grimberg 
23bd403d52SSebastian Grimberg   for (CeedInt i = 0; i < num_elem; i++) {
24bd403d52SSebastian Grimberg     ind[2 * i + 0]     = i;
25bd403d52SSebastian Grimberg     ind[2 * i + 1]     = i + 1;
26bd403d52SSebastian Grimberg     orients[2 * i + 0] = (i % 2) > 0;  // flip the dofs on element 1, 3, ...
27bd403d52SSebastian Grimberg     orients[2 * i + 1] = (i % 2) > 0;
28bd403d52SSebastian Grimberg   }
29bd403d52SSebastian Grimberg   CeedElemRestrictionCreateOriented(ceed, num_elem, elem_size, 1, 1, num_elem + 1, CEED_MEM_HOST, CEED_USE_POINTER, ind, orients, &elem_restriction);
30bd403d52SSebastian Grimberg 
31bd403d52SSebastian Grimberg   // NoTranspose
32bd403d52SSebastian Grimberg   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
33bd403d52SSebastian Grimberg   {
34bd403d52SSebastian Grimberg     const CeedScalar *y_array;
35bd403d52SSebastian Grimberg 
36bd403d52SSebastian Grimberg     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
37bd403d52SSebastian Grimberg     for (CeedInt i = 0; i < num_elem; i++) {
38bd403d52SSebastian Grimberg       for (CeedInt j = 0; j < elem_size; j++) {
39bd403d52SSebastian Grimberg         CeedInt k = j + elem_size * i;
40bd403d52SSebastian Grimberg         if (y_array[k] * CeedIntPow(-1, i % 2) != 10 + (k + 1) / 2) {
41bd403d52SSebastian Grimberg           // LCOV_EXCL_START
42bd403d52SSebastian Grimberg           printf("Error in restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_array[k]);
43bd403d52SSebastian Grimberg           // LCOV_EXCL_STOP
44bd403d52SSebastian Grimberg         }
45bd403d52SSebastian Grimberg       }
46bd403d52SSebastian Grimberg     }
47bd403d52SSebastian Grimberg     CeedVectorRestoreArrayRead(y, &y_array);
48bd403d52SSebastian Grimberg   }
49bd403d52SSebastian Grimberg 
50bd403d52SSebastian Grimberg   // Transpose
51bd403d52SSebastian Grimberg   CeedVectorSetValue(x, 0);
52bd403d52SSebastian Grimberg   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
53bd403d52SSebastian Grimberg   {
54bd403d52SSebastian Grimberg     const CeedScalar *x_array;
55bd403d52SSebastian Grimberg 
56bd403d52SSebastian Grimberg     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array);
57bd403d52SSebastian Grimberg     for (CeedInt i = 0; i < num_elem + 1; i++) {
58*6c10af5dSJeremy L Thompson       if (x_array[i] != (10 + i) * (i > 0 && i < num_elem ? 2.0 : 1.0)) {
592b62239cSJeremy L Thompson         // LCOV_EXCL_START
60bd403d52SSebastian Grimberg         printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)x_array[i]);
612b62239cSJeremy L Thompson         // LCOV_EXCL_STOP
62bd403d52SSebastian Grimberg       }
63*6c10af5dSJeremy L Thompson     }
64bd403d52SSebastian Grimberg     CeedVectorRestoreArrayRead(x, &x_array);
65bd403d52SSebastian Grimberg   }
66bd403d52SSebastian Grimberg 
67bd403d52SSebastian Grimberg   CeedVectorDestroy(&x);
68bd403d52SSebastian Grimberg   CeedVectorDestroy(&y);
69bd403d52SSebastian Grimberg   CeedElemRestrictionDestroy(&elem_restriction);
70bd403d52SSebastian Grimberg   CeedDestroy(&ceed);
71bd403d52SSebastian Grimberg   return 0;
72bd403d52SSebastian Grimberg }
73