xref: /libCEED/tests/t406-qfunction.c (revision ccec58666b8929dfb32358838fe858499801da8c)
1 /// @file
2 /// Test QFunction helper macro
3 /// \test Test QFunction helper macro
4 #include <ceed.h>
5 #include <math.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "t406-qfunction-helper.h"
9 #include "t406-qfunction.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)
62       // LCOV_EXCL_START
63       printf("[%d] v %f != vv %f\n",i, 5*v[i]*sqrt(2.), vv[i]);
64   // LCOV_EXCL_STOP
65   CeedVectorRestoreArrayRead(V, &vv);
66 
67   CeedVectorDestroy(&W);
68   CeedVectorDestroy(&U);
69   CeedVectorDestroy(&V);
70   CeedVectorDestroy(&Q_data);
71   CeedQFunctionDestroy(&qf_setup);
72   CeedQFunctionDestroy(&qf_mass);
73   CeedDestroy(&ceed);
74   return 0;
75 }
76