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 Eval(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 CeedScalar sum, error, pint[ALEN(p) + 1]; 22 23 CeedInit(argv[1], &ceed); 24 25 CeedVectorCreate(ceed, 2, &x); 26 CeedVectorCreate(ceed, q, &x_q); 27 CeedVectorSetValue(x_q, 0); 28 CeedVectorCreate(ceed, q, &u); 29 CeedVectorCreate(ceed, q, &u_q); 30 CeedVectorSetValue(u_q, 0); 31 CeedVectorCreate(ceed, q, &w); 32 CeedVectorSetValue(w, 0); 33 34 { 35 CeedScalar x_array[2]; 36 37 for (int i = 0; i < 2; i++) x_array[i] = CeedIntPow(-1, i + 1); 38 CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 39 } 40 41 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS_LOBATTO, &basis_x_lobatto); 42 43 CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q); 44 { 45 const CeedScalar *x_q_array; 46 CeedScalar u_array[q]; 47 48 CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array); 49 for (CeedInt i = 0; i < q; i++) u_array[i] = Eval(x_q_array[i], ALEN(p), p); 50 CeedVectorRestoreArrayRead(x_q, &x_q_array); 51 CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array); 52 } 53 54 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x_gauss); 55 CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, q, q, CEED_GAUSS, &basis_u_gauss); 56 57 CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q); 58 CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, u_q); 59 CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, w); 60 61 { 62 const CeedScalar *w_array, *u_q_array; 63 64 CeedVectorGetArrayRead(w, CEED_MEM_HOST, &w_array); 65 CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_q_array); 66 sum = 0; 67 for (CeedInt i = 0; i < q; i++) sum += w_array[i] * u_q_array[i]; 68 CeedVectorRestoreArrayRead(w, &w_array); 69 CeedVectorRestoreArrayRead(u_q, &u_q_array); 70 } 71 72 pint[0] = 0; 73 for (CeedInt i = 0; i < (int)ALEN(p); i++) pint[i + 1] = p[i] / (i + 1); 74 error = sum - Eval(1, ALEN(pint), pint) + Eval(-1, ALEN(pint), pint); 75 if (error > 100. * CEED_EPSILON) { 76 // LCOV_EXCL_START 77 printf("Error %e sum %g exact %g\n", error, sum, Eval(1, ALEN(pint), pint) - Eval(-1, ALEN(pint), pint)); 78 // LCOV_EXCL_STOP 79 } 80 81 CeedVectorDestroy(&x); 82 CeedVectorDestroy(&x_q); 83 CeedVectorDestroy(&u); 84 CeedVectorDestroy(&u_q); 85 CeedVectorDestroy(&w); 86 CeedBasisDestroy(&basis_x_lobatto); 87 CeedBasisDestroy(&basis_x_gauss); 88 CeedBasisDestroy(&basis_u_gauss); 89 CeedDestroy(&ceed); 90 return 0; 91 } 92