xref: /libCEED/tests/t311-basis.c (revision 00d548f6cce3ebb77e7917d79b50a2a368cad12e)
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, &basis_x_lobatto);
34   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS_LOBATTO, &basis_u_lobatto);
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(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q);
40 
41   CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &xq);
42   for (CeedInt i = 0; i < Q; i++) uq[i] = PolyEval(xq[i], ALEN(p), p);
43   CeedVectorRestoreArrayRead(X_q, &xq);
44   CeedVectorSetArray(U_q, CEED_MEM_HOST, CEED_USE_POINTER, (CeedScalar *)&uq);
45 
46   // This operation is the identity because the quadrature is collocated
47   CeedBasisApply(basis_u_lobatto, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, U_q, U);
48 
49   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &basis_x_gauss);
50   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, Q, Q, CEED_GAUSS, &basis_u_gauss);
51 
52   CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q);
53   CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, U_q);
54 
55   CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &xq);
56   CeedVectorGetArrayRead(U_q, 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) > 100. * CEED_EPSILON) printf("%f != %f = p(%f)\n", uuq[i], px, xq[i]);
60   }
61   CeedVectorRestoreArrayRead(X_q, &xq);
62   CeedVectorRestoreArrayRead(U_q, &uuq);
63 
64   CeedVectorDestroy(&X);
65   CeedVectorDestroy(&X_q);
66   CeedVectorDestroy(&U);
67   CeedVectorDestroy(&U_q);
68   CeedBasisDestroy(&basis_x_lobatto);
69   CeedBasisDestroy(&basis_u_lobatto);
70   CeedBasisDestroy(&basis_x_gauss);
71   CeedBasisDestroy(&basis_u_gauss);
72   CeedDestroy(&ceed);
73   return 0;
74 }
75