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