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