xref: /libCEED/tests/t410-qfunction.c (revision b249f0c79f6b6ffa6ed292c8449d061ee15719cd)
1 /// @file
2 /// Test creation, evaluation, and destruction for QFunction by name
3 /// \test Test creation, evaluation, and destruction for QFunction by name
4 #include <ceed.h>
5 
6 int main(int argc, char **argv) {
7   Ceed ceed;
8   CeedVector in[16], out[16];
9   CeedVector Q_data, J, W, U, V;
10   CeedQFunction qf_setup, qf_mass;
11   CeedInt Q = 8;
12   const CeedScalar *vv;
13   CeedScalar j[Q], w[Q], u[Q], v[Q];
14 
15   CeedInit(argv[1], &ceed);
16 
17   CeedQFunctionCreateInteriorByName(ceed, "Mass1DBuild", &qf_setup);
18   CeedQFunctionCreateInteriorByName(ceed, "MassApply", &qf_mass);
19 
20   for (CeedInt i=0; i<Q; i++) {
21     CeedScalar x = 2.*i/(Q-1) - 1;
22     j[i] = 1;
23     w[i] = 1 - x*x;
24     u[i] = 2 + 3*x + 5*x*x;
25     v[i] = w[i] * u[i];
26   }
27 
28   CeedVectorCreate(ceed, Q, &J);
29   CeedVectorSetArray(J, CEED_MEM_HOST, CEED_USE_POINTER, j);
30   CeedVectorCreate(ceed, Q, &W);
31   CeedVectorSetArray(W, CEED_MEM_HOST, CEED_USE_POINTER, w);
32   CeedVectorCreate(ceed, Q, &U);
33   CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u);
34   CeedVectorCreate(ceed, Q, &V);
35   CeedVectorSetValue(V, 0);
36   CeedVectorCreate(ceed, Q, &Q_data);
37   CeedVectorSetValue(Q_data, 0);
38 
39   {
40     in[0] = J;
41     in[1] = W;
42     out[0] = Q_data;
43     CeedQFunctionApply(qf_setup, Q, in, out);
44   }
45   {
46     in[0] = W;
47     in[1] = U;
48     out[0] = V;
49     CeedQFunctionApply(qf_mass, Q, in, out);
50   }
51 
52   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &vv);
53   for (CeedInt i=0; i<Q; i++)
54     if (v[i] != vv[i])
55       // LCOV_EXCL_START
56       printf("[%d] v %f != vv %f\n",i, v[i], vv[i]);
57   // LCOV_EXCL_STOP
58   CeedVectorRestoreArrayRead(V, &vv);
59 
60   CeedVectorDestroy(&J);
61   CeedVectorDestroy(&W);
62   CeedVectorDestroy(&U);
63   CeedVectorDestroy(&V);
64   CeedVectorDestroy(&Q_data);
65   CeedQFunctionDestroy(&qf_setup);
66   CeedQFunctionDestroy(&qf_mass);
67   CeedDestroy(&ceed);
68   return 0;
69 }
70