xref: /libCEED/tests/t415-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1b249f0c7SJeremy L Thompson /// @file
2b249f0c7SJeremy L Thompson /// Test creation, evaluation, and destruction for vector Poisson QFunction by name
3b249f0c7SJeremy L Thompson /// \test Test creation, evaluation, and destruction for vector Poisson 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];
11*4fee36f0SJeremy L Thompson   CeedVector    q_data, dx, w, du, dv;
12b249f0c7SJeremy L Thompson   CeedQFunction qf_setup, qf_diff;
13*4fee36f0SJeremy L Thompson   CeedInt       q        = 8;
14b249f0c7SJeremy L Thompson   const CeedInt num_comp = 3;
15b249f0c7SJeremy L Thompson 
16b249f0c7SJeremy L Thompson   CeedInit(argv[1], &ceed);
17b249f0c7SJeremy L Thompson 
18b249f0c7SJeremy L Thompson   for (CeedInt dim = 1; dim <= 3; dim++) {
19*4fee36f0SJeremy L Thompson     CeedInt num_qpts = CeedIntPow(q, dim);
20b249f0c7SJeremy L Thompson 
21*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, num_qpts * dim * dim, &dx);
22*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, num_qpts, &w);
23*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, num_qpts * dim * num_comp, &du);
24*4fee36f0SJeremy L Thompson     {
25*4fee36f0SJeremy L Thompson       CeedScalar dx_array[num_qpts * dim * dim], w_array[num_qpts], du_array[num_qpts * dim * num_comp];
26b249f0c7SJeremy L Thompson 
27b249f0c7SJeremy L Thompson       for (CeedInt i = 0; i < num_qpts; i++) {
28*4fee36f0SJeremy L Thompson         w_array[i] = 1.0 / num_qpts;
29b249f0c7SJeremy L Thompson       }
30b249f0c7SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
31b249f0c7SJeremy L Thompson         for (CeedInt g = 0; g < dim; g++) {
32b249f0c7SJeremy L Thompson           for (CeedInt i = 0; i < num_qpts; i++) {
33*4fee36f0SJeremy L Thompson             dx_array[i + (g * dim + d) * num_qpts] = d == g;
34b249f0c7SJeremy L Thompson           }
35b249f0c7SJeremy L Thompson         }
36b249f0c7SJeremy L Thompson       }
37b249f0c7SJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
38b249f0c7SJeremy L Thompson         for (CeedInt g = 0; g < dim; g++) {
39b249f0c7SJeremy L Thompson           for (CeedInt i = 0; i < num_qpts; i++) {
40*4fee36f0SJeremy L Thompson             du_array[i + (g * num_comp + c) * num_qpts] = c + 1;
41b249f0c7SJeremy L Thompson           }
42b249f0c7SJeremy L Thompson         }
43b249f0c7SJeremy L Thompson       }
44b249f0c7SJeremy L Thompson 
45*4fee36f0SJeremy L Thompson       CeedVectorSetArray(dx, CEED_MEM_HOST, CEED_COPY_VALUES, dx_array);
46*4fee36f0SJeremy L Thompson       CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
47*4fee36f0SJeremy L Thompson       CeedVectorSetArray(du, CEED_MEM_HOST, CEED_COPY_VALUES, du_array);
48*4fee36f0SJeremy L Thompson     }
49*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, num_qpts * dim * (dim + 1) / 2, &q_data);
50*4fee36f0SJeremy L Thompson     CeedVectorSetValue(q_data, 0.0);
51*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, num_qpts * dim * num_comp, &dv);
52*4fee36f0SJeremy L Thompson     CeedVectorSetValue(dv, 0.0);
53b249f0c7SJeremy L Thompson 
54*4fee36f0SJeremy L Thompson     char name_setup[26] = "", name_apply[26] = "";
55*4fee36f0SJeremy L Thompson     snprintf(name_setup, sizeof name_setup, "Poisson%" CeedInt_FMT "DBuild", dim);
56*4fee36f0SJeremy L Thompson     CeedQFunctionCreateInteriorByName(ceed, name_setup, &qf_setup);
57b249f0c7SJeremy L Thompson     {
58*4fee36f0SJeremy L Thompson       in[0]  = dx;
59*4fee36f0SJeremy L Thompson       in[1]  = w;
60*4fee36f0SJeremy L Thompson       out[0] = q_data;
61b249f0c7SJeremy L Thompson       CeedQFunctionApply(qf_setup, num_qpts, in, out);
62b249f0c7SJeremy L Thompson     }
63*4fee36f0SJeremy L Thompson 
64*4fee36f0SJeremy L Thompson     snprintf(name_apply, sizeof name_apply, "Vector3Poisson%" CeedInt_FMT "DApply", dim);
65*4fee36f0SJeremy L Thompson     CeedQFunctionCreateInteriorByName(ceed, name_apply, &qf_diff);
66b249f0c7SJeremy L Thompson     {
67*4fee36f0SJeremy L Thompson       in[0]  = du;
68*4fee36f0SJeremy L Thompson       in[1]  = q_data;
69*4fee36f0SJeremy L Thompson       out[0] = dv;
70b249f0c7SJeremy L Thompson       CeedQFunctionApply(qf_diff, num_qpts, in, out);
71b249f0c7SJeremy L Thompson     }
72b249f0c7SJeremy L Thompson 
73*4fee36f0SJeremy L Thompson     // Verify results
74*4fee36f0SJeremy L Thompson     {
75*4fee36f0SJeremy L Thompson       const CeedScalar *v_array;
76*4fee36f0SJeremy L Thompson 
77*4fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(dv, CEED_MEM_HOST, &v_array);
78b249f0c7SJeremy L Thompson       for (CeedInt c = 0; c < num_comp; c++) {
79b249f0c7SJeremy L Thompson         CeedScalar sum = 0;
802b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_qpts; i++) {
81*4fee36f0SJeremy L Thompson           for (CeedInt g = 0; g < dim; g++) sum += v_array[i + (g * num_comp + c) * num_qpts];
822b730f8bSJeremy L Thompson         }
832b730f8bSJeremy L Thompson         if (fabs(sum - dim * (c + 1)) > 10 * CEED_EPSILON) {
84b249f0c7SJeremy L Thompson           // LCOV_EXCL_START
852b730f8bSJeremy L Thompson           printf("%" CeedInt_FMT "D volume error in component %" CeedInt_FMT ": %f != %f\n", dim, c, sum, dim * (c + 1.0));
86b249f0c7SJeremy L Thompson           // LCOV_EXCL_STOP
87b249f0c7SJeremy L Thompson         }
882b730f8bSJeremy L Thompson       }
89*4fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(dv, &v_array);
90*4fee36f0SJeremy L Thompson     }
91b249f0c7SJeremy L Thompson 
92*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&dx);
93*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&w);
94*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&du);
95*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&dv);
96*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&q_data);
97b249f0c7SJeremy L Thompson     CeedQFunctionDestroy(&qf_setup);
98b249f0c7SJeremy L Thompson     CeedQFunctionDestroy(&qf_diff);
99b249f0c7SJeremy L Thompson   }
100b249f0c7SJeremy L Thompson 
101b249f0c7SJeremy L Thompson   CeedDestroy(&ceed);
102b249f0c7SJeremy L Thompson   return 0;
103b249f0c7SJeremy L Thompson }
104