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