xref: /libCEED/tests/t414-qfunction.c (revision 600b7929b98f3d8efad5f619bace308a359a46af)
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] = "";
24*600b7929SJeremy 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;
71b249f0c7SJeremy L Thompson       for (CeedInt i=0; i<num_qpts; i++)
72b249f0c7SJeremy L Thompson         sum += vv[i + c * num_qpts];
73b249f0c7SJeremy L Thompson       if (fabs(sum - (c + 1)) > 10*CEED_EPSILON)
74b249f0c7SJeremy L Thompson         // LCOV_EXCL_START
75*600b7929SJeremy L Thompson         printf("%" CeedInt_FMT "D volume error in component %" CeedInt_FMT
76*600b7929SJeremy L Thompson                ": %f != %f\n", dim, c, sum, (c + 1.0));
77b249f0c7SJeremy L Thompson       // LCOV_EXCL_STOP
78b249f0c7SJeremy L Thompson     }
79b249f0c7SJeremy L Thompson     CeedVectorRestoreArrayRead(V, &vv);
80b249f0c7SJeremy L Thompson 
81b249f0c7SJeremy L Thompson     CeedVectorDestroy(&J);
82b249f0c7SJeremy L Thompson     CeedVectorDestroy(&W);
83b249f0c7SJeremy L Thompson     CeedVectorDestroy(&U);
84b249f0c7SJeremy L Thompson     CeedVectorDestroy(&V);
85b249f0c7SJeremy L Thompson     CeedVectorDestroy(&Q_data);
86b249f0c7SJeremy L Thompson     CeedQFunctionDestroy(&qf_setup);
87b249f0c7SJeremy L Thompson     CeedQFunctionDestroy(&qf_mass);
88b249f0c7SJeremy L Thompson   }
89b249f0c7SJeremy L Thompson 
90b249f0c7SJeremy L Thompson   CeedDestroy(&ceed);
91b249f0c7SJeremy L Thompson   return 0;
92b249f0c7SJeremy L Thompson }
93