xref: /libCEED/tests/t233-elemrestriction.c (revision 99421279ebf997f2fb157e1e18f23b9dc112bbac)
1 /// @file
2 /// Test creation, use, and destruction of an element restriction at points for single elements
3 /// \test Test creation, use, and destruction of an element restriction at points for single elements
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   {
17     CeedInt offset      = num_elem + 1;
18     CeedInt point_index = num_elem;
19 
20     for (CeedInt i = 0; i < num_elem; i++) {
21       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
22 
23       ind[i] = offset;
24       for (CeedInt j = 0; j < num_points_in_elem; j++) {
25         ind[offset + j] = point_index;
26         point_index     = (point_index + 1) % num_points;
27       }
28       offset += num_points_in_elem;
29     }
30     ind[num_elem] = offset;
31   }
32   CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_USE_POINTER, ind, &elem_restriction);
33 
34   CeedElemRestrictionCreateVector(elem_restriction, &x, NULL);
35   {
36     CeedInt    point_index = num_elem;
37     CeedScalar array[num_points];
38 
39     for (CeedInt i = 0; i < num_elem; i++) {
40       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
41 
42       for (CeedInt j = 0; j < num_points_in_elem; j++) {
43         array[point_index] = i;
44         point_index        = (point_index + 1) % num_points;
45       }
46     }
47     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
48   }
49 
50   {
51     CeedInt min_points, max_points;
52 
53     CeedElemRestrictionGetMinPointsInElement(elem_restriction, &min_points);
54     CeedElemRestrictionGetMaxPointsInElement(elem_restriction, &max_points);
55     if (min_points != 1 || max_points != num_elem) {
56       // LCOV_EXCL_START
57       printf("Error in min/max points: min %" CeedInt_FMT " max %" CeedInt_FMT "\n", min_points, max_points);
58       // LCOV_EXCL_STOP
59     }
60     CeedVectorCreate(ceed, max_points, &y);
61   }
62 
63   {
64     for (CeedInt i = 0; i < num_elem; i++) {
65       CeedInt           num_points_in_elem = (i + 1) % num_elem + 1;
66       const CeedScalar *read_array;
67 
68       CeedElemRestrictionApplyAtPointsInElement(elem_restriction, i, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
69       CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
70 
71       for (CeedInt j = 0; j < num_points_in_elem; j++) {
72         if (i != read_array[j]) {
73           // LCOV_EXCL_START
74           printf("Error in restricted element array %" CeedInt_FMT " y[%" CeedInt_FMT "] = %f\n", i, j, (CeedScalar)read_array[j]);
75           // LCOV_EXCL_STOP
76         }
77       }
78       CeedVectorRestoreArrayRead(y, &read_array);
79     }
80   }
81 
82   CeedVectorDestroy(&x);
83   CeedVectorDestroy(&y);
84   CeedElemRestrictionDestroy(&elem_restriction);
85   CeedDestroy(&ceed);
86   return 0;
87 }
88