xref: /libCEED/tests/t231-elemrestriction.c (revision ed094490f53e580908aa80e9fe815a6fd76d7526)
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 <ceed/backend.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_USE_POINTER, ind, &elem_restriction);
34 
35   CeedElemRestrictionCreateVector(elem_restriction, &x, &y);
36   CeedVectorSetValue(y, 0.0);
37   {
38     CeedInt    point_index = num_elem;
39     CeedScalar array[num_points];
40 
41     for (CeedInt i = 0; i < num_elem; i++) {
42       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
43 
44       for (CeedInt j = 0; j < num_points_in_elem; j++) {
45         array[point_index] = i;
46         point_index        = (point_index + 1) % num_points;
47       }
48     }
49     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
50   }
51 
52   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
53   {
54     CeedInt           e_layout[3];
55     const CeedScalar *read_array;
56 
57     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
58     CeedElemRestrictionGetELayout(elem_restriction, e_layout);
59 
60     for (CeedInt i = 0; i < num_elem; i++) {
61       CeedSize      elem_offset        = 0;
62       const CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
63 
64       CeedElemRestrictionGetAtPointsElementOffset(elem_restriction, i, &elem_offset);
65       for (CeedInt j = 0; j < num_points_in_elem; j++) {
66         if (i != read_array[elem_offset + j * e_layout[0]]) {
67           // LCOV_EXCL_START
68           printf("Error in restricted array y[%" CeedInt_FMT "] = %f != %f\n", (CeedInt)elem_offset + j * e_layout[0],
69                  (CeedScalar)read_array[elem_offset + j * e_layout[0]], (CeedScalar)i);
70           // LCOV_EXCL_STOP
71         }
72       }
73     }
74     CeedVectorRestoreArrayRead(y, &read_array);
75   }
76 
77   CeedVectorDestroy(&x);
78   CeedVectorDestroy(&y);
79   CeedElemRestrictionDestroy(&elem_restriction);
80   CeedDestroy(&ceed);
81   return 0;
82 }
83