xref: /libCEED/tests/t232-elemrestriction.c (revision ef9a992f4cf09f2be4ec72f649495c67ec03f813)
10930e4e7SJeremy L Thompson /// @file
2*0b63de31SJeremy L Thompson /// Test creation, use, and destruction of an element restriction at points
3*0b63de31SJeremy L Thompson /// \test Test creation, use, and destruction of an element restriction at points
40930e4e7SJeremy L Thompson #include <ceed.h>
5*0b63de31SJeremy L Thompson #include <ceed/backend.h>
60930e4e7SJeremy L Thompson #include <stdio.h>
70930e4e7SJeremy L Thompson 
main(int argc,char ** argv)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   {
180930e4e7SJeremy L Thompson     CeedInt offset      = num_elem + 1;
190930e4e7SJeremy L Thompson     CeedInt point_index = num_elem;
200930e4e7SJeremy L Thompson 
210930e4e7SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
220930e4e7SJeremy L Thompson       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
230930e4e7SJeremy L Thompson 
240930e4e7SJeremy L Thompson       ind[i] = offset;
250930e4e7SJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
260930e4e7SJeremy L Thompson         ind[offset + j] = point_index;
270930e4e7SJeremy L Thompson         point_index     = (point_index + 1) % num_points;
280930e4e7SJeremy L Thompson       }
290930e4e7SJeremy L Thompson       offset += num_points_in_elem;
300930e4e7SJeremy L Thompson     }
310930e4e7SJeremy L Thompson     ind[num_elem] = offset;
320930e4e7SJeremy L Thompson   }
330930e4e7SJeremy L Thompson   CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_USE_POINTER, ind, &elem_restriction);
340930e4e7SJeremy L Thompson 
35*0b63de31SJeremy L Thompson   CeedElemRestrictionCreateVector(elem_restriction, &x, &y);
36*0b63de31SJeremy L Thompson   CeedVectorSetValue(y, 0.0);
37f930fbbfSJeremy L Thompson   {
38f930fbbfSJeremy L Thompson     CeedInt    point_index = num_elem;
39f930fbbfSJeremy L Thompson     CeedScalar array[num_points];
40f930fbbfSJeremy L Thompson 
41f930fbbfSJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
42f930fbbfSJeremy L Thompson       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
43f930fbbfSJeremy L Thompson 
44f930fbbfSJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
45f930fbbfSJeremy L Thompson         array[point_index] = i;
46f930fbbfSJeremy L Thompson         point_index        = (point_index + 1) % num_points;
47f930fbbfSJeremy L Thompson       }
48f930fbbfSJeremy L Thompson     }
49f930fbbfSJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, array);
50f930fbbfSJeremy L Thompson   }
51f930fbbfSJeremy L Thompson 
52*0b63de31SJeremy L Thompson   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
53*0b63de31SJeremy L Thompson   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
540930e4e7SJeremy L Thompson   {
55*0b63de31SJeremy L Thompson     CeedInt           point_index = num_elem;
560930e4e7SJeremy L Thompson     const CeedScalar *read_array;
570930e4e7SJeremy L Thompson 
58*0b63de31SJeremy L Thompson     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &read_array);
59*0b63de31SJeremy L Thompson     for (CeedInt i = 0; i < num_elem; i++) {
60*0b63de31SJeremy L Thompson       CeedInt num_points_in_elem = (i + 1) % num_elem + 1;
610930e4e7SJeremy L Thompson 
620930e4e7SJeremy L Thompson       for (CeedInt j = 0; j < num_points_in_elem; j++) {
63*0b63de31SJeremy L Thompson         if (read_array[point_index] != 2 * i) {
642b62239cSJeremy L Thompson           // LCOV_EXCL_START
65*0b63de31SJeremy L Thompson           printf("Error in restricted array x[%" CeedInt_FMT "] = %f != %f\n", point_index, read_array[point_index], 2.0 * i);
662b62239cSJeremy L Thompson           // LCOV_EXCL_STOP
670930e4e7SJeremy L Thompson         }
68*0b63de31SJeremy L Thompson         point_index = (point_index + 1) % num_points;
690930e4e7SJeremy L Thompson       }
700930e4e7SJeremy L Thompson     }
71*0b63de31SJeremy L Thompson     CeedVectorRestoreArrayRead(x, &read_array);
720930e4e7SJeremy L Thompson   }
730930e4e7SJeremy L Thompson 
740930e4e7SJeremy L Thompson   CeedVectorDestroy(&x);
750930e4e7SJeremy L Thompson   CeedVectorDestroy(&y);
760930e4e7SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction);
770930e4e7SJeremy L Thompson   CeedDestroy(&ceed);
780930e4e7SJeremy L Thompson   return 0;
790930e4e7SJeremy L Thompson }
80