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