xref: /libCEED/tests/t234-elemrestriction.c (revision 4b3e95d5da7d4d1a3e8f0acb9d6f6cc36918381b) !
1 /// @file
2 /// Test creation, transpose use, and destruction of an element restriction at points for single elements
3 /// \test Test creation, transpose use, and destruction of an element restriction at points for single elements
4 #include <ceed.h>
5 #include <math.h>
6 #include <stdio.h>
7 
8 int main(int argc, char **argv) {
9   Ceed                ceed;
10   CeedInt             num_elem = 3, num_points = num_elem * 2;
11   CeedInt             ind[(num_elem + 1) + num_points];
12   CeedVector          x, y;
13   CeedElemRestriction elem_restriction;
14 
15   CeedInit(argv[1], &ceed);
16 
17   {
18     CeedInt offset      = num_elem + 1;
19     CeedInt point_index = num_elem;
20 
21     for (CeedInt i = 0; i < num_elem; i++) {
22       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
23 
24       ind[i] = offset;
25       for (CeedInt j = 0; j < num_points_in_elem; j++) {
26         ind[offset + j] = point_index;
27         point_index     = (point_index + 1) % num_points;
28       }
29       offset += num_points_in_elem;
30     }
31     ind[num_elem] = offset;
32   }
33   CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind, &elem_restriction);
34 
35   CeedElemRestrictionCreateVector(elem_restriction, &x, NULL);
36   CeedVectorSetValue(x, 0.0);
37   {
38     CeedInt max_points;
39 
40     CeedElemRestrictionGetMaxPointsInElement(elem_restriction, &max_points);
41     CeedVectorCreate(ceed, max_points, &y);
42     CeedVectorSetValue(y, 1.0);
43   }
44 
45   {
46     for (CeedInt i = 0; i < num_elem; i++) {
47       CeedInt           point_index = num_elem;
48       const CeedScalar *read_array;
49 
50       CeedVectorSetValue(x, 0.0);
51       CeedElemRestrictionApplyAtPointsInElement(elem_restriction, i, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
52 
53       CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
54       for (CeedInt j = 0; j < num_elem; j++) {
55         CeedInt num_points_in_elem = (j + 1) % num_elem + 1;
56 
57         for (CeedInt k = 0; k < num_points_in_elem; k++) {
58           if (fabs(read_array[point_index] - (i == j ? 1.0 : 0.0)) > 10 * CEED_EPSILON) {
59             // LCOV_EXCL_START
60             printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", point_index, (CeedScalar)read_array[point_index]);
61             // LCOV_EXCL_STOP
62           }
63           point_index = (point_index + 1) % num_points;
64         }
65       }
66       CeedVectorRestoreArrayRead(x, &read_array);
67     }
68   }
69 
70   CeedVectorDestroy(&x);
71   CeedVectorDestroy(&y);
72   CeedElemRestrictionDestroy(&elem_restriction);
73   CeedDestroy(&ceed);
74   return 0;
75 }
76