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