1a8de75f0Sjeremylt /// @file 2*52bfb9bbSJeremy L Thompson /// Test polynomial interpolation in 1D 3*52bfb9bbSJeremy L Thompson /// \test Test polynomial interpolation in 1D 4a8de75f0Sjeremylt #include <ceed.h> 5a8de75f0Sjeremylt #include <math.h> 6a8de75f0Sjeremylt 7*52bfb9bbSJeremy L Thompson #define ALEN(a) (sizeof(a) / sizeof((a)[0])) 8*52bfb9bbSJeremy L Thompson 9*52bfb9bbSJeremy L Thompson static CeedScalar PolyEval(CeedScalar x, CeedInt n, const CeedScalar *p) { 10*52bfb9bbSJeremy L Thompson CeedScalar y = p[n-1]; 11*52bfb9bbSJeremy L Thompson for (CeedInt i=n-2; i>=0; i--) y = y*x + p[i]; 12*52bfb9bbSJeremy L Thompson return y; 13a8de75f0Sjeremylt } 14a8de75f0Sjeremylt 15a8de75f0Sjeremylt int main(int argc, char **argv) { 16a8de75f0Sjeremylt Ceed ceed; 17*52bfb9bbSJeremy L Thompson CeedVector X, Xq, U, Uq; 18*52bfb9bbSJeremy L Thompson CeedBasis bxl, bul, bxg, bug; 19*52bfb9bbSJeremy L Thompson CeedInt Q = 6; 20*52bfb9bbSJeremy L Thompson const CeedScalar p[6] = {1, 2, 3, 4, 5, 6}; // 1 + 2x + 3x^2 + ... 21*52bfb9bbSJeremy L Thompson const CeedScalar *xq, *uuq; 22*52bfb9bbSJeremy L Thompson CeedScalar x[2], uq[Q]; 23a8de75f0Sjeremylt 24a8de75f0Sjeremylt CeedInit(argv[1], &ceed); 25aedaa0e5Sjeremylt 26*52bfb9bbSJeremy L Thompson CeedVectorCreate(ceed, 2, &X); 27*52bfb9bbSJeremy L Thompson CeedVectorCreate(ceed, Q, &Xq); 28*52bfb9bbSJeremy L Thompson CeedVectorSetValue(Xq, 0); 29*52bfb9bbSJeremy L Thompson CeedVectorCreate(ceed, Q, &U); 30*52bfb9bbSJeremy L Thompson CeedVectorSetValue(U, 0); 31*52bfb9bbSJeremy L Thompson CeedVectorCreate(ceed, Q, &Uq); 32a8de75f0Sjeremylt 33*52bfb9bbSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS_LOBATTO, &bxl); 34*52bfb9bbSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul); 35aedaa0e5Sjeremylt 36*52bfb9bbSJeremy L Thompson for (int i = 0; i < 2; i++) 37*52bfb9bbSJeremy L Thompson x[i] = CeedIntPow(-1, i+1); 38*52bfb9bbSJeremy L Thompson CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x); 39aedaa0e5Sjeremylt 40*52bfb9bbSJeremy L Thompson CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 41a8de75f0Sjeremylt 42*52bfb9bbSJeremy L Thompson CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq); 43*52bfb9bbSJeremy L Thompson for (CeedInt i=0; i<Q; i++) 44*52bfb9bbSJeremy L Thompson uq[i] = PolyEval(xq[i], ALEN(p), p); 45*52bfb9bbSJeremy L Thompson CeedVectorRestoreArrayRead(Xq, &xq); 46*52bfb9bbSJeremy L Thompson CeedVectorSetArray(Uq, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq); 47*52bfb9bbSJeremy L Thompson 48*52bfb9bbSJeremy L Thompson // This operation is the identity because the quadrature is collocated 49*52bfb9bbSJeremy L Thompson CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, Uq, U); 50*52bfb9bbSJeremy L Thompson 51*52bfb9bbSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg); 52*52bfb9bbSJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug); 53*52bfb9bbSJeremy L Thompson 54*52bfb9bbSJeremy L Thompson CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 55*52bfb9bbSJeremy L Thompson CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq); 56*52bfb9bbSJeremy L Thompson 57*52bfb9bbSJeremy L Thompson CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq); 58*52bfb9bbSJeremy L Thompson CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uuq); 59*52bfb9bbSJeremy L Thompson for (CeedInt i=0; i<Q; i++) { 60*52bfb9bbSJeremy L Thompson CeedScalar px = PolyEval(xq[i], ALEN(p), p); 61*52bfb9bbSJeremy L Thompson if (fabs(uuq[i] - px) > 1e-14) 62a2546046Sjeremylt // LCOV_EXCL_START 63*52bfb9bbSJeremy L Thompson printf("%f != %f=p(%f)\n", uuq[i], px, xq[i]); 64de996c55Sjeremylt // LCOV_EXCL_STOP 65a8de75f0Sjeremylt } 66*52bfb9bbSJeremy L Thompson CeedVectorRestoreArrayRead(Xq, &xq); 67*52bfb9bbSJeremy L Thompson CeedVectorRestoreArrayRead(Uq, &uuq); 68a8de75f0Sjeremylt 69*52bfb9bbSJeremy L Thompson CeedVectorDestroy(&X); 70*52bfb9bbSJeremy L Thompson CeedVectorDestroy(&Xq); 71*52bfb9bbSJeremy L Thompson CeedVectorDestroy(&U); 72*52bfb9bbSJeremy L Thompson CeedVectorDestroy(&Uq); 73*52bfb9bbSJeremy L Thompson CeedBasisDestroy(&bxl); 74*52bfb9bbSJeremy L Thompson CeedBasisDestroy(&bul); 75*52bfb9bbSJeremy L Thompson CeedBasisDestroy(&bxg); 76*52bfb9bbSJeremy L Thompson CeedBasisDestroy(&bug); 77a8de75f0Sjeremylt CeedDestroy(&ceed); 78a8de75f0Sjeremylt return 0; 79a8de75f0Sjeremylt } 80