xref: /libCEED/tests/t414-qfunction.c (revision 7ed3e4cde27d28430628eaa24b22da48dc51cc32)
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, J, W, U, V;
12   CeedQFunction qf_setup, qf_mass;
13   CeedInt Q = 8;
14   const CeedInt num_comp = 3;
15   const CeedScalar *vv;
16 
17   CeedInit(argv[1], &ceed);
18 
19   for (CeedInt dim = 2; dim <= 3; dim++) {
20     CeedInt num_qpts = CeedIntPow(Q, dim);
21     CeedScalar j[num_qpts * dim * dim], w[num_qpts], u[num_qpts * num_comp];
22 
23     char name[13] = "";
24     snprintf(name, sizeof name, "Mass%dDBuild", dim);
25     CeedQFunctionCreateInteriorByName(ceed, name, &qf_setup);
26     CeedQFunctionCreateInteriorByName(ceed, "Vector3MassApply", &qf_mass);
27 
28     for (CeedInt i=0; i<num_qpts; i++) {
29       w[i] = 1.0 / num_qpts;
30     }
31     for (CeedInt d=0; d<dim; d++) {
32       for (CeedInt g=0; g<dim; g++) {
33         for (CeedInt i=0; i<num_qpts; i++) {
34           j[i + (g * dim + d) * num_qpts] = d == g;
35         }
36       }
37     }
38     for (CeedInt c=0; c<num_comp; c++) {
39       for (CeedInt i=0; i<num_qpts; i++) {
40         u[i + c * num_qpts] = c + 1;
41       }
42     }
43 
44     CeedVectorCreate(ceed, num_qpts * dim * dim, &J);
45     CeedVectorSetArray(J, CEED_MEM_HOST, CEED_USE_POINTER, j);
46     CeedVectorCreate(ceed, num_qpts, &W);
47     CeedVectorSetArray(W, CEED_MEM_HOST, CEED_USE_POINTER, w);
48     CeedVectorCreate(ceed, num_qpts, &Q_data);
49     CeedVectorSetValue(Q_data, 0.0);
50     CeedVectorCreate(ceed, num_qpts * num_comp, &U);
51     CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u);
52     CeedVectorCreate(ceed, num_qpts * num_comp, &V);
53     CeedVectorSetValue(V, 0.0);
54 
55     {
56       in[0] = J;
57       in[1] = W;
58       out[0] = Q_data;
59       CeedQFunctionApply(qf_setup, num_qpts, in, out);
60     }
61     {
62       in[0] = U;
63       in[1] = Q_data;
64       out[0] = V;
65       CeedQFunctionApply(qf_mass, num_qpts, in, out);
66     }
67 
68     CeedVectorGetArrayRead(V, CEED_MEM_HOST, &vv);
69     for (CeedInt c=0; c<num_comp; c++) {
70       CeedScalar sum = 0;
71       for (CeedInt i=0; i<num_qpts; i++)
72         sum += vv[i + c * num_qpts];
73       if (fabs(sum - (c + 1)) > 10*CEED_EPSILON)
74         // LCOV_EXCL_START
75         printf("%dD volume error in component %d: %f != %f\n",
76                dim, c, sum, (c + 1.0));
77       // LCOV_EXCL_STOP
78     }
79     CeedVectorRestoreArrayRead(V, &vv);
80 
81     CeedVectorDestroy(&J);
82     CeedVectorDestroy(&W);
83     CeedVectorDestroy(&U);
84     CeedVectorDestroy(&V);
85     CeedVectorDestroy(&Q_data);
86     CeedQFunctionDestroy(&qf_setup);
87     CeedQFunctionDestroy(&qf_mass);
88   }
89 
90   CeedDestroy(&ceed);
91   return 0;
92 }
93