xref: /libCEED/tests/t401-qfunction.c (revision 8ec64e9ae9d5df169dba8c8ee61d8ec8907b8f80)
1 /// @file
2 /// Test creation, evaluation, and destruction for QFunction
3 /// \test Test creation, evaluation, and destruction for QFunction
4 #include "t401-qfunction.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 
9 int main(int argc, char **argv) {
10   Ceed                 ceed;
11   CeedVector           in[16], out[16];
12   CeedVector           Q_data, W, U, V;
13   CeedQFunction        qf_setup, qf_mass;
14   CeedQFunctionContext ctx;
15   CeedInt              Q = 8;
16   const CeedScalar    *vv;
17   CeedScalar           w[Q], u[Q], v[Q], ctx_data[5] = {1, 2, 3, 4, 5};
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   CeedQFunctionContextCreate(ceed, &ctx);
31   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(ctx_data), &ctx_data);
32   CeedQFunctionSetContext(qf_mass, ctx);
33 
34   for (CeedInt i = 0; i < Q; i++) {
35     CeedScalar x = 2. * i / (Q - 1) - 1;
36     w[i]         = 1 - x * x;
37     u[i]         = 2 + 3 * x + 5 * x * x;
38     v[i]         = w[i] * u[i];
39   }
40 
41   CeedVectorCreate(ceed, Q, &W);
42   CeedVectorSetArray(W, CEED_MEM_HOST, CEED_USE_POINTER, w);
43   CeedVectorCreate(ceed, Q, &U);
44   CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u);
45   CeedVectorCreate(ceed, Q, &V);
46   CeedVectorSetValue(V, 0);
47   CeedVectorCreate(ceed, Q, &Q_data);
48   CeedVectorSetValue(Q_data, 0);
49 
50   {
51     in[0]  = W;
52     out[0] = Q_data;
53     CeedQFunctionApply(qf_setup, Q, in, out);
54   }
55   {
56     in[0]  = W;
57     in[1]  = U;
58     out[0] = V;
59     CeedQFunctionApply(qf_mass, Q, in, out);
60   }
61 
62   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &vv);
63   for (CeedInt i = 0; i < Q; i++) {
64     if (fabs(ctx_data[4] * v[i] - vv[i]) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] v %f != vv %f\n", i, v[i], vv[i]);
65   }
66   CeedVectorRestoreArrayRead(V, &vv);
67 
68   CeedVectorDestroy(&W);
69   CeedVectorDestroy(&U);
70   CeedVectorDestroy(&V);
71   CeedVectorDestroy(&Q_data);
72   CeedQFunctionDestroy(&qf_setup);
73   CeedQFunctionDestroy(&qf_mass);
74   CeedQFunctionContextDestroy(&ctx);
75   CeedDestroy(&ceed);
76   return 0;
77 }
78