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, X_q, U, U_q, W; 18 CeedBasis basis_x_lobatto, basis_x_gauss, basis_u_gauss; 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, &X_q); 28 CeedVectorSetValue(X_q, 0); 29 CeedVectorCreate(ceed, Q, &U); 30 CeedVectorCreate(ceed, Q, &U_q); 31 CeedVectorSetValue(U_q, 0); 32 CeedVectorCreate(ceed, Q, &W); 33 CeedVectorSetValue(W, 0); 34 35 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS_LOBATTO, &basis_x_lobatto); 36 37 for (int i = 0; i < 2; i++) x[i] = CeedIntPow(-1, i + 1); 38 CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x); 39 40 CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q); 41 42 CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &xq); 43 for (CeedInt i = 0; i < Q; i++) u[i] = PolyEval(xq[i], ALEN(p), p); 44 CeedVectorRestoreArrayRead(X_q, &xq); 45 CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u); 46 47 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &basis_x_gauss); 48 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &basis_u_gauss); 49 50 CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q); 51 CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, U_q); 52 CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, W); 53 54 CeedVectorGetArrayRead(W, CEED_MEM_HOST, &w); 55 CeedVectorGetArrayRead(U_q, CEED_MEM_HOST, &uq); 56 sum = 0; 57 for (CeedInt i = 0; i < Q; i++) sum += w[i] * uq[i]; 58 CeedVectorRestoreArrayRead(W, &w); 59 CeedVectorRestoreArrayRead(U_q, &uq); 60 61 pint[0] = 0; 62 for (CeedInt i = 0; i < (int)ALEN(p); i++) pint[i + 1] = p[i] / (i + 1); 63 error = sum - PolyEval(1, ALEN(pint), pint) + PolyEval(-1, ALEN(pint), pint); 64 if (error > 100. * CEED_EPSILON) { 65 // LCOV_EXCL_START 66 printf("Error %e sum %g exact %g\n", error, sum, PolyEval(1, ALEN(pint), pint) - PolyEval(-1, ALEN(pint), pint)); 67 // LCOV_EXCL_STOP 68 } 69 70 CeedVectorDestroy(&X); 71 CeedVectorDestroy(&X_q); 72 CeedVectorDestroy(&U); 73 CeedVectorDestroy(&U_q); 74 CeedVectorDestroy(&W); 75 CeedBasisDestroy(&basis_x_lobatto); 76 CeedBasisDestroy(&basis_x_gauss); 77 CeedBasisDestroy(&basis_u_gauss); 78 CeedDestroy(&ceed); 79 return 0; 80 } 81