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