xref: /libCEED/tests/t311-basis.c (revision 166c82af0ccdd574f082349fdd3d8ddcd0ba3057)
1a8de75f0Sjeremylt /// @file
252bfb9bbSJeremy L Thompson /// Test polynomial interpolation in 1D
352bfb9bbSJeremy L Thompson /// \test Test polynomial interpolation in 1D
4a8de75f0Sjeremylt #include <ceed.h>
5a8de75f0Sjeremylt #include <math.h>
6a8de75f0Sjeremylt 
752bfb9bbSJeremy L Thompson #define ALEN(a) (sizeof(a) / sizeof((a)[0]))
852bfb9bbSJeremy L Thompson 
952bfb9bbSJeremy L Thompson static CeedScalar PolyEval(CeedScalar x, CeedInt n, const CeedScalar *p) {
1052bfb9bbSJeremy L Thompson   CeedScalar y = p[n-1];
1152bfb9bbSJeremy L Thompson   for (CeedInt i=n-2; i>=0; i--) y = y*x + p[i];
1252bfb9bbSJeremy L Thompson   return y;
13a8de75f0Sjeremylt }
14a8de75f0Sjeremylt 
15a8de75f0Sjeremylt int main(int argc, char **argv) {
16a8de75f0Sjeremylt   Ceed ceed;
1752bfb9bbSJeremy L Thompson   CeedVector X, Xq, U, Uq;
1852bfb9bbSJeremy L Thompson   CeedBasis bxl, bul, bxg, bug;
1952bfb9bbSJeremy L Thompson   CeedInt Q = 6;
2052bfb9bbSJeremy L Thompson   const CeedScalar p[6] = {1, 2, 3, 4, 5, 6}; // 1 + 2x + 3x^2 + ...
2152bfb9bbSJeremy L Thompson   const CeedScalar *xq, *uuq;
2252bfb9bbSJeremy L Thompson   CeedScalar x[2], uq[Q];
23a8de75f0Sjeremylt 
24a8de75f0Sjeremylt   CeedInit(argv[1], &ceed);
25aedaa0e5Sjeremylt 
2652bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, 2, &X);
2752bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Xq);
2852bfb9bbSJeremy L Thompson   CeedVectorSetValue(Xq, 0);
2952bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &U);
3052bfb9bbSJeremy L Thompson   CeedVectorSetValue(U, 0);
3152bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Uq);
32a8de75f0Sjeremylt 
3352bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
3452bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul);
35aedaa0e5Sjeremylt 
3652bfb9bbSJeremy L Thompson   for (int i = 0; i < 2; i++)
3752bfb9bbSJeremy L Thompson     x[i] = CeedIntPow(-1, i+1);
3852bfb9bbSJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x);
39aedaa0e5Sjeremylt 
4052bfb9bbSJeremy L Thompson   CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
41a8de75f0Sjeremylt 
4252bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
4352bfb9bbSJeremy L Thompson   for (CeedInt i=0; i<Q; i++)
4452bfb9bbSJeremy L Thompson     uq[i] = PolyEval(xq[i], ALEN(p), p);
4552bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Xq, &xq);
4652bfb9bbSJeremy L Thompson   CeedVectorSetArray(Uq, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq);
4752bfb9bbSJeremy L Thompson 
4852bfb9bbSJeremy L Thompson   // This operation is the identity because the quadrature is collocated
4952bfb9bbSJeremy L Thompson   CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, Uq, U);
5052bfb9bbSJeremy L Thompson 
5152bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg);
5252bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug);
5352bfb9bbSJeremy L Thompson 
5452bfb9bbSJeremy L Thompson   CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
5552bfb9bbSJeremy L Thompson   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq);
5652bfb9bbSJeremy L Thompson 
5752bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
5852bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uuq);
5952bfb9bbSJeremy L Thompson   for (CeedInt i=0; i<Q; i++) {
6052bfb9bbSJeremy L Thompson     CeedScalar px = PolyEval(xq[i], ALEN(p), p);
61*166c82afSvaleriabarra     if (fabs(uuq[i] - px) > 1E-14)
62a2546046Sjeremylt       // LCOV_EXCL_START
6352bfb9bbSJeremy L Thompson       printf("%f != %f=p(%f)\n", uuq[i], px, xq[i]);
64de996c55Sjeremylt     // LCOV_EXCL_STOP
65a8de75f0Sjeremylt   }
6652bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Xq, &xq);
6752bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Uq, &uuq);
68a8de75f0Sjeremylt 
6952bfb9bbSJeremy L Thompson   CeedVectorDestroy(&X);
7052bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Xq);
7152bfb9bbSJeremy L Thompson   CeedVectorDestroy(&U);
7252bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Uq);
7352bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bxl);
7452bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bul);
7552bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bxg);
7652bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bug);
77a8de75f0Sjeremylt   CeedDestroy(&ceed);
78a8de75f0Sjeremylt   return 0;
79a8de75f0Sjeremylt }
80