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