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