xref: /libCEED/tests/t218-elemrestriction.c (revision b473a06276daa8a28b9126dca2fc365f20267013)
1 /// @file
2 /// Test creation, use, and destruction of a curl-conforming oriented element restriction with and without unsigned application
3 /// \test Test creation, use, and destruction of a curl-conforming oriented element restriction with and without unsigned application
4 #include <ceed.h>
5 #include <stdio.h>
6 
7 int main(int argc, char **argv) {
8   Ceed                ceed;
9   CeedVector          x, y, y_unsigned;
10   CeedInt             num_elem = 6, elem_size = 4;
11   CeedInt             ind[elem_size * num_elem];
12   CeedInt8            curl_orients[3 * elem_size * num_elem];
13   CeedScalar          x_array[3 * num_elem + 1];
14   CeedElemRestriction elem_restriction, elem_restriction_unsigned;
15 
16   CeedInit(argv[1], &ceed);
17 
18   CeedVectorCreate(ceed, 3 * num_elem + 1, &x);
19   for (CeedInt i = 0; i < 3 * num_elem + 1; i++) x_array[i] = 10 + i;
20   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
21   CeedVectorCreate(ceed, num_elem * elem_size, &y);
22   CeedVectorCreate(ceed, num_elem * elem_size, &y_unsigned);
23 
24   for (CeedInt i = 0; i < num_elem; i++) {
25     ind[4 * i + 0] = 3 * i + 0;
26     ind[4 * i + 1] = 3 * i + 1;
27     ind[4 * i + 2] = 3 * i + 2;
28     ind[4 * i + 3] = 3 * i + 3;
29     if (i % 2 > 0) {
30       // T = [ 1  0  0  0]
31       //     [ 0  1  0  0]
32       //     [ 0  0  0 -1]
33       //     [ 0  0 -1  0]
34       curl_orients[3 * 4 * i + 0]  = 0;
35       curl_orients[3 * 4 * i + 1]  = 1;
36       curl_orients[3 * 4 * i + 2]  = 0;
37       curl_orients[3 * 4 * i + 3]  = 0;
38       curl_orients[3 * 4 * i + 4]  = 1;
39       curl_orients[3 * 4 * i + 5]  = 0;
40       curl_orients[3 * 4 * i + 6]  = 0;
41       curl_orients[3 * 4 * i + 7]  = 0;
42       curl_orients[3 * 4 * i + 8]  = -1;
43       curl_orients[3 * 4 * i + 9]  = -1;
44       curl_orients[3 * 4 * i + 10] = 0;
45       curl_orients[3 * 4 * i + 11] = 0;
46     } else {
47       // T = I
48       curl_orients[3 * 4 * i + 0]  = 0;
49       curl_orients[3 * 4 * i + 1]  = 1;
50       curl_orients[3 * 4 * i + 2]  = 0;
51       curl_orients[3 * 4 * i + 3]  = 0;
52       curl_orients[3 * 4 * i + 4]  = 1;
53       curl_orients[3 * 4 * i + 5]  = 0;
54       curl_orients[3 * 4 * i + 6]  = 0;
55       curl_orients[3 * 4 * i + 7]  = 1;
56       curl_orients[3 * 4 * i + 8]  = 0;
57       curl_orients[3 * 4 * i + 9]  = 0;
58       curl_orients[3 * 4 * i + 10] = 1;
59       curl_orients[3 * 4 * i + 11] = 0;
60     }
61   }
62   CeedElemRestrictionCreateCurlOriented(ceed, num_elem, elem_size, 1, 1, 3 * num_elem + 1, CEED_MEM_HOST, CEED_USE_POINTER, ind, curl_orients,
63                                         &elem_restriction);
64   CeedElemRestrictionCreateUnsignedCopy(elem_restriction, &elem_restriction_unsigned);
65 
66   // NoTranspose
67   CeedElemRestrictionApply(elem_restriction, CEED_NOTRANSPOSE, x, y, CEED_REQUEST_IMMEDIATE);
68   CeedElemRestrictionApply(elem_restriction_unsigned, CEED_NOTRANSPOSE, x, y_unsigned, CEED_REQUEST_IMMEDIATE);
69   {
70     const CeedScalar *y_array, *y_unsigned_array;
71 
72     CeedVectorGetArrayRead(y, CEED_MEM_HOST, &y_array);
73     CeedVectorGetArrayRead(y_unsigned, CEED_MEM_HOST, &y_unsigned_array);
74     for (CeedInt i = 0; i < num_elem; i++) {
75       for (CeedInt j = 0; j < elem_size; j++) {
76         CeedInt k = j + elem_size * i;
77         if (i % 2 > 0 && j >= 2) {
78           if (j == 2 && 10 + 3 * i + j + 1 != -y_array[k]) {
79             // LCOV_EXCL_START
80             printf("Error in restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_array[k]);
81             // LCOV_EXCL_STOP
82           } else if (j == 3 && 10 + 3 * i + j - 1 != -y_array[k]) {
83             // LCOV_EXCL_START
84             printf("Error in restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_array[k]);
85             // LCOV_EXCL_STOP
86           }
87           if (j == 2 && 10 + 3 * i + j + 1 != y_unsigned_array[k]) {
88             // LCOV_EXCL_START
89             printf("Error in unsigned restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_unsigned_array[k]);
90             // LCOV_EXCL_STOP
91           } else if (j == 3 && 10 + 3 * i + j - 1 != y_unsigned_array[k]) {
92             // LCOV_EXCL_START
93             printf("Error in unsigned restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_unsigned_array[k]);
94             // LCOV_EXCL_STOP
95           }
96         } else {
97           if (10 + 3 * i + j != y_array[k] || 10 + 3 * i + j != y_unsigned_array[k]) {
98             // LCOV_EXCL_START
99             printf("Error in restricted array y[%" CeedInt_FMT "] = %f\n", k, (CeedScalar)y_array[k]);
100             // LCOV_EXCL_STOP
101           }
102         }
103       }
104     }
105     CeedVectorRestoreArrayRead(y, &y_array);
106     CeedVectorRestoreArrayRead(y_unsigned, &y_unsigned_array);
107   }
108 
109   // Transpose
110   CeedVectorSetValue(x, 0);
111   CeedElemRestrictionApply(elem_restriction, CEED_TRANSPOSE, y, x, CEED_REQUEST_IMMEDIATE);
112   {
113     const CeedScalar *x_array;
114 
115     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array);
116     for (CeedInt i = 0; i < 3 * num_elem + 1; i++) {
117       if (x_array[i] != (10 + i) * (i > 0 && i < 3 * num_elem && i % 3 == 0 ? 2.0 : 1.0)) {
118         // LCOV_EXCL_START
119         printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)x_array[i]);
120         // LCOV_EXCL_STOP
121       }
122     }
123     CeedVectorRestoreArrayRead(x, &x_array);
124   }
125 
126   // Transpose unsigned
127   CeedVectorSetValue(x, 0);
128   CeedElemRestrictionApply(elem_restriction_unsigned, CEED_TRANSPOSE, y_unsigned, x, CEED_REQUEST_IMMEDIATE);
129   {
130     const CeedScalar *x_array;
131 
132     CeedVectorGetArrayRead(x, CEED_MEM_HOST, &x_array);
133     for (CeedInt i = 0; i < 3 * num_elem + 1; i++) {
134       if (x_array[i] != (10 + i) * (i > 0 && i < 3 * num_elem && i % 3 == 0 ? 2.0 : 1.0)) {
135         // LCOV_EXCL_START
136         printf("Error in restricted array x[%" CeedInt_FMT "] = %f\n", i, (CeedScalar)x_array[i]);
137         // LCOV_EXCL_STOP
138       }
139     }
140     CeedVectorRestoreArrayRead(x, &x_array);
141   }
142 
143   CeedVectorDestroy(&x);
144   CeedVectorDestroy(&y);
145   CeedVectorDestroy(&y_unsigned);
146   CeedElemRestrictionDestroy(&elem_restriction);
147   CeedElemRestrictionDestroy(&elem_restriction_unsigned);
148   CeedDestroy(&ceed);
149   return 0;
150 }
151