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