xref: /libCEED/tests/t414-qfunction.c (revision 530ad8c4fcac036810a1c230b0a577d30882de6e)
1 /// @file
2 /// Test creation, evaluation, and destruction for vector mass QFunction by name
3 /// \test Test creation, evaluation, and destruction for vector mass QFunction by name
4 #include <ceed.h>
5 #include <math.h>
6 #include <string.h>
7 
8 int main(int argc, char **argv) {
9   Ceed          ceed;
10   CeedVector    in[16], out[16];
11   CeedVector    q_data, dx, w, u, v;
12   CeedQFunction qf_setup, qf_mass;
13   CeedInt       q        = 8;
14   const CeedInt num_comp = 3;
15 
16   CeedInit(argv[1], &ceed);
17 
18   for (CeedInt dim = 2; dim <= 3; dim++) {
19     CeedInt num_qpts = CeedIntPow(q, dim);
20 
21     CeedVectorCreate(ceed, num_qpts * dim * dim, &dx);
22     CeedVectorCreate(ceed, num_qpts, &w);
23     CeedVectorCreate(ceed, num_qpts * num_comp, &u);
24     {
25       CeedScalar dx_array[num_qpts * dim * dim], w_array[num_qpts], u_array[num_qpts * num_comp];
26 
27       for (CeedInt i = 0; i < num_qpts; i++) {
28         w_array[i] = 1.0 / num_qpts;
29       }
30       for (CeedInt d = 0; d < dim; d++) {
31         for (CeedInt g = 0; g < dim; g++) {
32           for (CeedInt i = 0; i < num_qpts; i++) {
33             dx_array[i + (g * dim + d) * num_qpts] = d == g;
34           }
35         }
36       }
37       for (CeedInt c = 0; c < num_comp; c++) {
38         for (CeedInt i = 0; i < num_qpts; i++) {
39           u_array[i + c * num_qpts] = c + 1;
40         }
41       }
42       CeedVectorSetArray(dx, CEED_MEM_HOST, CEED_COPY_VALUES, dx_array);
43       CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
44       CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
45     }
46     CeedVectorCreate(ceed, num_qpts, &q_data);
47     CeedVectorSetValue(q_data, 0.0);
48     CeedVectorCreate(ceed, num_qpts * num_comp, &v);
49     CeedVectorSetValue(v, 0.0);
50 
51     char name[13] = "";
52     snprintf(name, sizeof name, "Mass%" CeedInt_FMT "DBuild", dim);
53     CeedQFunctionCreateInteriorByName(ceed, name, &qf_setup);
54     {
55       in[0]  = dx;
56       in[1]  = w;
57       out[0] = q_data;
58       CeedQFunctionApply(qf_setup, num_qpts, in, out);
59     }
60 
61     CeedQFunctionCreateInteriorByName(ceed, "Vector3MassApply", &qf_mass);
62     {
63       in[0]  = u;
64       in[1]  = q_data;
65       out[0] = v;
66       CeedQFunctionApply(qf_mass, num_qpts, in, out);
67     }
68 
69     // Verify results
70     {
71       const CeedScalar *v_array;
72 
73       CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
74       for (CeedInt c = 0; c < num_comp; c++) {
75         CeedScalar sum = 0;
76         for (CeedInt i = 0; i < num_qpts; i++) sum += v_array[i + c * num_qpts];
77         if (fabs(sum - (c + 1)) > 10 * CEED_EPSILON) {
78           // LCOV_EXCL_START
79           printf("%" CeedInt_FMT "D volume error in component %" CeedInt_FMT ": %f != %f\n", dim, c, sum, (c + 1.0));
80           // LCOV_EXCL_STOP
81         }
82       }
83       CeedVectorRestoreArrayRead(v, &v_array);
84     }
85 
86     CeedVectorDestroy(&dx);
87     CeedVectorDestroy(&w);
88     CeedVectorDestroy(&u);
89     CeedVectorDestroy(&v);
90     CeedVectorDestroy(&q_data);
91     CeedQFunctionDestroy(&qf_setup);
92     CeedQFunctionDestroy(&qf_mass);
93   }
94 
95   CeedDestroy(&ceed);
96   return 0;
97 }
98