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; 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 *xq, *uuq; 22 CeedScalar x[2], uq[Q]; 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 CeedVectorSetValue(U, 0); 31 CeedVectorCreate(ceed, Q, &Uq); 32 33 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS_LOBATTO, &bxl); 34 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul); 35 36 for (int i = 0; i < 2; i++) x[i] = CeedIntPow(-1, i+1); 37 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x); 38 39 CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 40 41 CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq); 42 for (CeedInt i=0; i<Q; i++) uq[i] = PolyEval(xq[i], ALEN(p), p); 43 CeedVectorRestoreArrayRead(Xq, &xq); 44 CeedVectorSetArray(Uq, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq); 45 46 // This operation is the identity because the quadrature is collocated 47 CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, Uq, U); 48 49 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bxg); 50 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &bug); 51 52 CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq); 53 CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq); 54 55 CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq); 56 CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &uuq); 57 for (CeedInt i=0; i<Q; i++) { 58 CeedScalar px = PolyEval(xq[i], ALEN(p), p); 59 if ((fabs(uuq[i] - px) > 1e-14)) { 60 printf("%f != %f=p(%f)\n", uuq[i], px, xq[i]); 61 } 62 } 63 CeedVectorRestoreArrayRead(Xq, &xq); 64 CeedVectorRestoreArrayRead(Uq, &uuq); 65 66 CeedVectorDestroy(&X); 67 CeedVectorDestroy(&Xq); 68 CeedVectorDestroy(&U); 69 CeedVectorDestroy(&Uq); 70 CeedBasisDestroy(&bxl); 71 CeedBasisDestroy(&bul); 72 CeedBasisDestroy(&bxg); 73 CeedBasisDestroy(&bug); 74 CeedDestroy(&ceed); 75 return 0; 76 } 77