xref: /libCEED/tests/t317-basis.c (revision 44554ea01e90fce366fc2a203c44be15754a38d6)
1 /// @file
2 /// Test polynomial derivative interpolation in 1D
3 /// \test Test polynomial derivative 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   CeedVector X, Xq, U, Uq;
18   CeedBasis bxl, bul, bxg, bug;
19   CeedInt Q = 6;
20   const CeedScalar p[6] = {1, 2, 3, 4, 5, 6}; // 1 + 2x + 3x^2 + ...
21   const CeedScalar dp[5] = {2, 6, 12, 20, 30}; // 2 + 6x + 12x^2 + ...
22   const CeedScalar *xq, *uuq;
23   CeedScalar x[2], uq[Q];
24 
25   CeedInit(argv[1], &ceed);
26 
27   CeedVectorCreate(ceed, 2, &X);
28   CeedVectorCreate(ceed, Q, &Xq);
29   CeedVectorSetValue(Xq, 0);
30   CeedVectorCreate(ceed, Q, &U);
31   CeedVectorSetValue(U, 0);
32   CeedVectorCreate(ceed, Q, &Uq);
33 
34   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
35   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul);
36 
37   for (int i = 0; i < 2; i++)
38     x[i] = CeedIntPow(-1, i+1);
39   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x);
40 
41   CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
42 
43   CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
44   for (CeedInt i=0; i<Q; i++)
45     uq[i] = PolyEval(xq[i], ALEN(p), p);
46   CeedVectorRestoreArrayRead(Xq, &xq);
47   CeedVectorSetArray(Uq, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq);
48 
49   // This operation is the identity because the quadrature is collocated
50   CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, Uq, U);
51 
52   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg);
53   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug);
54 
55   CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
56   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, U, Uq);
57 
58   CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
59   CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uuq);
60   for (CeedInt i=0; i<Q; i++) {
61     CeedScalar px = PolyEval(xq[i], ALEN(dp), dp);
62     if (fabs(uuq[i] - px) > 1E-13)
63       // LCOV_EXCL_START
64       printf("%f != %f=p(%f)\n", uuq[i], px, xq[i]);
65     // LCOV_EXCL_STOP
66   }
67   CeedVectorRestoreArrayRead(Xq, &xq);
68   CeedVectorRestoreArrayRead(Uq, &uuq);
69 
70   CeedVectorDestroy(&X);
71   CeedVectorDestroy(&Xq);
72   CeedVectorDestroy(&U);
73   CeedVectorDestroy(&Uq);
74   CeedBasisDestroy(&bxl);
75   CeedBasisDestroy(&bul);
76   CeedBasisDestroy(&bxg);
77   CeedBasisDestroy(&bug);
78   CeedDestroy(&ceed);
79   return 0;
80 }
81