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