xref: /libCEED/tests/t302-basis.c (revision b5cf12ee8b5ffe0ca173fbf1c0d42d7d14238b45)
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, bul, 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], uq[Q], u[Q];
22 
23   CeedInit(argv[1], &ceed);
24   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
25   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul);
26   CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, xq);
27   for (CeedInt i=0; i<Q; i++) uq[i] = PolyEval(xq[i], ALEN(p), p);
28 
29   // This operation is the identity because the quadrature is collocated
30   CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, uq, u);
31 
32   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg);
33   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug);
34   CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, xq);
35   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, uq);
36   for (CeedInt i=0; i<Q; i++) {
37     CeedScalar px = PolyEval(xq[i], ALEN(p), p);
38     if (!(fabs(uq[i] - px) < 1e-14)) {
39       printf("%f != %f=p(%f)\n", uq[i], px, xq[i]);
40     }
41   }
42 
43   CeedBasisDestroy(&bxl);
44   CeedBasisDestroy(&bul);
45   CeedBasisDestroy(&bxg);
46   CeedBasisDestroy(&bug);
47   CeedDestroy(&ceed);
48   return 0;
49 }
50