1 /// @file 2 /// Test polynomial interpolation in 1D 3 /// \test Test polynomial 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, W; 18 CeedBasis bxl, bxg, bug; 19 CeedInt Q = 6; 20 const CeedScalar p[6] = {1, 2, 3, 4, 5, 6}; // 1 + 2x + 3x^2 + ... 21 const CeedScalar *xq, *uq, *w; 22 CeedScalar u[Q], x[2], sum, error, pint[ALEN(p)+1]; 23 24 CeedInit(argv[1], &ceed); 25 26 CeedVectorCreate(ceed, 2, &X); 27 CeedVectorCreate(ceed, Q, &Xq); 28 CeedVectorSetValue(Xq, 0); 29 CeedVectorCreate(ceed, Q, &U); 30 CeedVectorCreate(ceed, Q, &Uq); 31 CeedVectorSetValue(Uq, 0); 32 CeedVectorCreate(ceed, Q, &W); 33 CeedVectorSetValue(W, 0); 34 35 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS_LOBATTO, &bxl); 36 37 for (int i = 0; i < 2; i++) x[i] = CeedIntPow(-1, i+1); 38 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x); 39 40 CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 41 42 CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq); 43 for (CeedInt i=0; i<Q; i++) u[i] = PolyEval(xq[i], ALEN(p), p); 44 CeedVectorRestoreArrayRead(Xq, &xq); 45 CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&u); 46 47 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg); 48 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug); 49 50 CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 51 CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq); 52 CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, NULL, W); 53 54 CeedVectorGetArrayRead(W, CEED_MEM_HOST, &w); 55 CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uq); 56 sum = 0; 57 for (CeedInt i=0; i<Q; i++) { 58 sum += w[i] * uq[i]; 59 } 60 CeedVectorRestoreArrayRead(W, &w); 61 CeedVectorRestoreArrayRead(Uq, &uq); 62 63 pint[0] = 0; 64 for (CeedInt i=0; i<(int)ALEN(p); i++) pint[i+1] = p[i] / (i+1); 65 error = sum - PolyEval(1, ALEN(pint), pint) + PolyEval(-1, ALEN(pint), pint); 66 if (error > 1.e-10) 67 // LCOV_EXCL_START 68 printf("Error %e sum %g exact %g\n", error, sum, 69 PolyEval(1, ALEN(pint), pint) - PolyEval(-1, ALEN(pint), pint)); 70 // LCOV_EXCL_STOP 71 72 CeedVectorDestroy(&X); 73 CeedVectorDestroy(&Xq); 74 CeedVectorDestroy(&U); 75 CeedVectorDestroy(&Uq); 76 CeedVectorDestroy(&W); 77 CeedBasisDestroy(&bxl); 78 CeedBasisDestroy(&bxg); 79 CeedBasisDestroy(&bug); 80 CeedDestroy(&ceed); 81 return 0; 82 } 83