xref: /libCEED/tests/t311-basis.c (revision cdf95791513f7c35170bef3ba2e19f272fe04533)
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;
18   CeedBasis basis_x_lobatto, basis_u_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, *uuq;
22   CeedScalar x[2], uq[Q];
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   CeedVectorSetValue(U, 0);
31   CeedVectorCreate(ceed, Q, &U_q);
32 
33   CeedBasisCreateTensorH1Lagrange(ceed, 1,  1, 2, Q, CEED_GAUSS_LOBATTO,
34                                   &basis_x_lobatto);
35   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO,
36                                   &basis_u_lobatto);
37 
38   for (int i = 0; i < 2; i++)
39     x[i] = CeedIntPow(-1, i+1);
40   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&x);
41 
42   CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q);
43 
44   CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &xq);
45   for (CeedInt i=0; i<Q; i++)
46     uq[i] = PolyEval(xq[i], ALEN(p), p);
47   CeedVectorRestoreArrayRead(X_q, &xq);
48   CeedVectorSetArray(U_q, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq);
49 
50   // This operation is the identity because the quadrature is collocated
51   CeedBasisApply(basis_u_lobatto, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, U_q, U);
52 
53   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &basis_x_gauss);
54   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &basis_u_gauss);
55 
56   CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q);
57   CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, U_q);
58 
59   CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &xq);
60   CeedVectorGetArrayRead(U_q, CEED_MEM_HOST, &uuq);
61   for (CeedInt i=0; i<Q; i++) {
62     CeedScalar px = PolyEval(xq[i], ALEN(p), p);
63     if (fabs(uuq[i] - px) > 100.*CEED_EPSILON)
64       // LCOV_EXCL_START
65       printf("%f != %f=p(%f)\n", uuq[i], px, xq[i]);
66     // LCOV_EXCL_STOP
67   }
68   CeedVectorRestoreArrayRead(X_q, &xq);
69   CeedVectorRestoreArrayRead(U_q, &uuq);
70 
71   CeedVectorDestroy(&X);
72   CeedVectorDestroy(&X_q);
73   CeedVectorDestroy(&U);
74   CeedVectorDestroy(&U_q);
75   CeedBasisDestroy(&basis_x_lobatto);
76   CeedBasisDestroy(&basis_u_lobatto);
77   CeedBasisDestroy(&basis_x_gauss);
78   CeedBasisDestroy(&basis_u_gauss);
79   CeedDestroy(&ceed);
80   return 0;
81 }
82