xref: /libCEED/tests/t231-elemrestriction.c (revision 196058356ce16dc55f8605a42694186898beb49b)
10930e4e7SJeremy L Thompson /// @file
20930e4e7SJeremy L Thompson /// Test creation, use, and destruction of an element restriction at points
30930e4e7SJeremy L Thompson /// \test Test creation, use, and destruction of an element restriction at points
40930e4e7SJeremy L Thompson #include <ceed.h>
5*19605835SJeremy L Thompson #include <ceed/backend.h>
60930e4e7SJeremy L Thompson #include <stdio.h>
70930e4e7SJeremy L Thompson 
80930e4e7SJeremy L Thompson int main(int argc, char **argv) {
90930e4e7SJeremy L Thompson   Ceed                ceed;
100930e4e7SJeremy L Thompson   CeedInt             num_elem = 3, num_points = num_elem * 2;
110930e4e7SJeremy L Thompson   CeedInt             ind[(num_elem + 1) + num_points];
120930e4e7SJeremy L Thompson   CeedVector          x, y;
130930e4e7SJeremy L Thompson   CeedElemRestriction elem_restriction;
140930e4e7SJeremy L Thompson 
150930e4e7SJeremy L Thompson   CeedInit(argv[1], &ceed);
160930e4e7SJeremy L Thompson 
170930e4e7SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &x);
180930e4e7SJeremy L Thompson   {
190930e4e7SJeremy L Thompson     CeedInt    point_index = num_elem;
200930e4e7SJeremy L Thompson     CeedScalar array[num_points];
210930e4e7SJeremy L Thompson 
220930e4e7SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
230930e4e7SJeremy L Thompson       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
240930e4e7SJeremy L Thompson 
250930e4e7SJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
260930e4e7SJeremy L Thompson         array[point_index] = i;
270930e4e7SJeremy L Thompson         point_index        = (point_index + 1) % num_points;
280930e4e7SJeremy L Thompson       }
290930e4e7SJeremy L Thompson     }
300930e4e7SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
310930e4e7SJeremy L Thompson   }
320930e4e7SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &y);
330930e4e7SJeremy L Thompson 
340930e4e7SJeremy L Thompson   {
350930e4e7SJeremy L Thompson     CeedInt offset      = num_elem + 1;
360930e4e7SJeremy L Thompson     CeedInt point_index = num_elem;
370930e4e7SJeremy L Thompson 
380930e4e7SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
390930e4e7SJeremy L Thompson       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
400930e4e7SJeremy L Thompson 
410930e4e7SJeremy L Thompson       ind[i] = offset;
420930e4e7SJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
430930e4e7SJeremy L Thompson         ind[offset + j] = point_index;
440930e4e7SJeremy L Thompson         point_index     = (point_index + 1) % num_points;
450930e4e7SJeremy L Thompson       }
460930e4e7SJeremy L Thompson       offset += num_points_in_elem;
470930e4e7SJeremy L Thompson     }
480930e4e7SJeremy L Thompson     ind[num_elem] = offset;
490930e4e7SJeremy L Thompson   }
500930e4e7SJeremy L Thompson   CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_USE_POINTER, ind, &elem_restriction);
510930e4e7SJeremy L Thompson   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
520930e4e7SJeremy L Thompson   {
53*19605835SJeremy L Thompson     CeedInt           e_layout[3];
540930e4e7SJeremy L Thompson     const CeedScalar *read_array;
550930e4e7SJeremy L Thompson 
560930e4e7SJeremy L Thompson     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &read_array);
57*19605835SJeremy L Thompson     CeedElemRestrictionGetELayout(elem_restriction, e_layout);
580930e4e7SJeremy L Thompson 
590930e4e7SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
60*19605835SJeremy L Thompson       CeedSize      elem_offset        = 0;
61*19605835SJeremy L Thompson       const CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
620930e4e7SJeremy L Thompson 
63*19605835SJeremy L Thompson       CeedElemRestrictionGetAtPointsElementOffset(elem_restriction, i, &elem_offset);
640930e4e7SJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
65*19605835SJeremy L Thompson         if (i != read_array[elem_offset + j * e_layout[0]]) {
662b62239cSJeremy L Thompson           // LCOV_EXCL_START
67*19605835SJeremy L Thompson           printf("Error in restricted array y[%" CeedInt_FMT "] = %f\n != %f\n", (CeedInt)elem_offset + j * e_layout[0],
68*19605835SJeremy L Thompson                  (CeedScalar)read_array[elem_offset + j * e_layout[0]], (CeedScalar)i);
692b62239cSJeremy L Thompson           // LCOV_EXCL_STOP
700930e4e7SJeremy L Thompson         }
710930e4e7SJeremy L Thompson       }
720930e4e7SJeremy L Thompson     }
730930e4e7SJeremy L Thompson     CeedVectorRestoreArrayRead(y, &read_array);
740930e4e7SJeremy L Thompson   }
750930e4e7SJeremy L Thompson 
760930e4e7SJeremy L Thompson   CeedVectorDestroy(&x);
770930e4e7SJeremy L Thompson   CeedVectorDestroy(&y);
780930e4e7SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction);
790930e4e7SJeremy L Thompson   CeedDestroy(&ceed);
800930e4e7SJeremy L Thompson   return 0;
810930e4e7SJeremy L Thompson }
82