xref: /libCEED/tests/t305-basis.c (revision ecf6354e9a1bfdf9b8a51c1eb1e730eaa037defc)
1 /// @file
2 /// Test polynomial interpolation in 1D
3 /// \test Test polynomial interpolation in 1D
4 #include <ceed.h>
5 #include <math.h>
6 
7 #define ALEN(a) (sizeof(a) / sizeof((a)[0]))
8 
9 static CeedScalar PolyEval(CeedScalar x, CeedInt n, const CeedScalar *p) {
10   CeedScalar y = p[n-1];
11   for (CeedInt i=n-2; i>=0; i--) y = y*x + p[i];
12   return y;
13 }
14 
15 int main(int argc, char **argv) {
16   Ceed ceed;
17   CeedBasis bxl, bxg, bug;
18   CeedInt Q = 6;
19   const CeedScalar p[] = {1, 2, 3, 4, 5, 6}; // 1 + 2x + 3x^2 + ...
20   const CeedScalar x[] = {-1, 1};
21   CeedScalar xq[Q], u[Q], uq[Q], w[Q], sum, error, pint[ALEN(p)+1];
22 
23   CeedInit(argv[1], &ceed);
24   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
25   CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, xq);
26   for (CeedInt i=0; i<Q; i++) u[i] = PolyEval(xq[i], ALEN(p), p);
27 
28   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg);
29   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug);
30   CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, xq);
31   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, uq);
32   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, w);
33   sum = 0;
34   for (CeedInt i=0; i<Q; i++) {
35     sum += w[i] * uq[i];
36   }
37   pint[0] = 0;
38   for (CeedInt i=0; i<(CeedInt)ALEN(p); i++) pint[i+1] = p[i] / (i+1);
39   error = sum - PolyEval(1, ALEN(pint), pint) + PolyEval(-1, ALEN(pint), pint);
40   if (!(error < 1e-10))
41     printf("Error %e  sum %g  exact %g\n", error, sum,
42            PolyEval(1, ALEN(pint), pint) - PolyEval(-1, ALEN(pint), pint));
43 
44   CeedBasisDestroy(&bxl);
45   CeedBasisDestroy(&bxg);
46   CeedBasisDestroy(&bug);
47   CeedDestroy(&ceed);
48   return 0;
49 }
50