xref: /libCEED/tests/t410-qfunction.c (revision 2eb0be0b68ebb7cb25cc038250732c3239325ad2)
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]) printf("[%" CeedInt_FMT "] v %f != vv %f\n", i, v[i], vv[i]);
55   }
56   CeedVectorRestoreArrayRead(V, &vv);
57 
58   CeedVectorDestroy(&J);
59   CeedVectorDestroy(&W);
60   CeedVectorDestroy(&U);
61   CeedVectorDestroy(&V);
62   CeedVectorDestroy(&Q_data);
63   CeedQFunctionDestroy(&qf_setup);
64   CeedQFunctionDestroy(&qf_mass);
65   CeedDestroy(&ceed);
66   return 0;
67 }
68