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