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