xref: /libCEED/tests/t312-basis.c (revision a7b7f929ab476a681f8a846f95c06992dbac3dd3)
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, W;
1852bfb9bbSJeremy L Thompson   CeedBasis bxl, 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, *uq, *w;
2252bfb9bbSJeremy L Thompson   CeedScalar u[Q], x[2], sum, error, pint[ALEN(p)+1];
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   CeedVectorCreate(ceed, Q, &Uq);
3152bfb9bbSJeremy L Thompson   CeedVectorSetValue(Uq, 0);
3252bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &W);
3352bfb9bbSJeremy L Thompson   CeedVectorSetValue(W, 0);
34a8de75f0Sjeremylt 
3552bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
36aedaa0e5Sjeremylt 
3752bfb9bbSJeremy L Thompson   for (int i = 0; i < 2; i++)
3852bfb9bbSJeremy L Thompson     x[i] = CeedIntPow(-1, i+1);
3952bfb9bbSJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
40aedaa0e5Sjeremylt 
4152bfb9bbSJeremy L Thompson   CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
42a8de75f0Sjeremylt 
4352bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
4452bfb9bbSJeremy L Thompson   for (CeedInt i=0; i<Q; i++)
4552bfb9bbSJeremy L Thompson     u[i] = PolyEval(xq[i], ALEN(p), p);
4652bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Xq, &xq);
4752bfb9bbSJeremy L Thompson   CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u);
4852bfb9bbSJeremy L Thompson 
4952bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg);
5052bfb9bbSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug);
5152bfb9bbSJeremy L Thompson 
5252bfb9bbSJeremy L Thompson   CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
5352bfb9bbSJeremy L Thompson   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq);
54*a7b7f929Sjeremylt   CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT,
55*a7b7f929Sjeremylt                  CEED_VECTOR_NONE, W);
5652bfb9bbSJeremy L Thompson 
5752bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(W, CEED_MEM_HOST, &w);
5852bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uq);
59a8de75f0Sjeremylt   sum = 0;
6052bfb9bbSJeremy L Thompson   for (CeedInt i=0; i<Q; i++)
6152bfb9bbSJeremy L Thompson     sum += w[i] * uq[i];
6252bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(W, &w);
6352bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Uq, &uq);
64a8de75f0Sjeremylt 
6552bfb9bbSJeremy L Thompson   pint[0] = 0;
6652bfb9bbSJeremy L Thompson   for (CeedInt i=0; i<(int)ALEN(p); i++)
6752bfb9bbSJeremy L Thompson     pint[i+1] = p[i] / (i+1);
6852bfb9bbSJeremy L Thompson   error = sum - PolyEval(1, ALEN(pint), pint) + PolyEval(-1, ALEN(pint), pint);
6952bfb9bbSJeremy L Thompson   if (error > 1.e-10)
7052bfb9bbSJeremy L Thompson     // LCOV_EXCL_START
7152bfb9bbSJeremy L Thompson     printf("Error %e  sum %g  exact %g\n", error, sum,
7252bfb9bbSJeremy L Thompson            PolyEval(1, ALEN(pint), pint) - PolyEval(-1, ALEN(pint), pint));
7352bfb9bbSJeremy L Thompson   // LCOV_EXCL_STOP
7452bfb9bbSJeremy L Thompson 
7552bfb9bbSJeremy L Thompson   CeedVectorDestroy(&X);
7652bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Xq);
7752bfb9bbSJeremy L Thompson   CeedVectorDestroy(&U);
7852bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Uq);
7952bfb9bbSJeremy L Thompson   CeedVectorDestroy(&W);
8052bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bxl);
8152bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bxg);
8252bfb9bbSJeremy L Thompson   CeedBasisDestroy(&bug);
83a8de75f0Sjeremylt   CeedDestroy(&ceed);
84a8de75f0Sjeremylt   return 0;
85a8de75f0Sjeremylt }
86