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