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