xref: /libCEED/tests/t233-elemrestriction.c (revision 76e570b879b329e479addbab569cc13ecfa673ca)
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   CeedVectorCreate(ceed, num_points, &x);
18   CeedVectorSetValue(x, 0.0);
19 
20   {
21     CeedInt offset      = num_elem + 1;
22     CeedInt point_index = num_elem;
23 
24     for (CeedInt i = 0; i < num_elem; i++) {
25       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
26 
27       ind[i] = offset;
28       for (CeedInt j = 0; j < num_points_in_elem; j++) {
29         ind[offset + j] = point_index;
30         point_index     = (point_index + 1) % num_points;
31       }
32       offset += num_points_in_elem;
33     }
34     ind[num_elem] = offset;
35   }
36   CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind, &elem_restriction);
37 
38   {
39     CeedInt max_points;
40 
41     CeedElemRestrictionGetMaxPointsInElement(elem_restriction, &max_points);
42     CeedVectorCreate(ceed, max_points, &y);
43     CeedVectorSetValue(y, 1.0);
44   }
45 
46   {
47     for (CeedInt i = 0; i < num_elem; i++) {
48       CeedInt           point_index = num_elem;
49       const CeedScalar *read_array;
50 
51       CeedVectorSetValue(x, 0.0);
52       CeedElemRestrictionApplyAtPointsInElement(elem_restriction, i, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
53 
54       CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
55       for (CeedInt j = 0; j < num_elem; j++) {
56         CeedInt num_points_in_elem = (j + 1) % num_elem + 1;
57 
58         for (CeedInt k = 0; k < num_points_in_elem; k++) {
59           if (fabs(read_array[point_index] - (i == j ? 1.0 : 0.0)) > 10 * CEED_EPSILON) {
60             // LCOV_EXCL_START
61             printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", point_index, (CeedScalar)read_array[point_index]);
62             // LCOV_EXCL_STOP
63           }
64           point_index = (point_index + 1) % num_points;
65         }
66       }
67       CeedVectorRestoreArrayRead(x, &read_array);
68     }
69   }
70 
71   CeedVectorDestroy(&x);
72   CeedVectorDestroy(&y);
73   CeedElemRestrictionDestroy(&elem_restriction);
74   CeedDestroy(&ceed);
75   return 0;
76 }
77