xref: /libCEED/tests/t311-basis.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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;
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 
22   CeedInit(argv[1], &ceed);
23 
24   CeedVectorCreate(ceed, 2, &x);
25   CeedVectorCreate(ceed, q, &x_q);
26   CeedVectorSetValue(x_q, 0);
27   CeedVectorCreate(ceed, q, &u);
28   CeedVectorSetValue(u, 0);
29   CeedVectorCreate(ceed, q, &u_q);
30 
31   {
32     CeedScalar x_array[2];
33 
34     for (int i = 0; i < 2; i++) x_array[i] = CeedIntPow(-1, i + 1);
35     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
36   }
37 
38   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS_LOBATTO, &basis_x_lobatto);
39   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, q, q, CEED_GAUSS_LOBATTO, &basis_u_lobatto);
40 
41   CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
42   {
43     const CeedScalar *x_q_array;
44     CeedScalar        u_array[q];
45 
46     CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
47     for (CeedInt i = 0; i < q; i++) u_array[i] = Eval(x_q_array[i], ALEN(p), p);
48     CeedVectorRestoreArrayRead(x_q, &x_q_array);
49     CeedVectorSetArray(u_q, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
50   }
51 
52   // This operation is the identity because the quadrature is collocated
53   CeedBasisApply(basis_u_lobatto, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, u_q, u);
54 
55   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x_gauss);
56   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, q, q, CEED_GAUSS, &basis_u_gauss);
57 
58   CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
59   CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, u_q);
60 
61   {
62     const CeedScalar *x_q_array, *u_q_array;
63 
64     CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
65     CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_q_array);
66     for (CeedInt i = 0; i < q; i++) {
67       CeedScalar px = Eval(x_q_array[i], ALEN(p), p);
68       if (fabs(u_q_array[i] - px) > 100. * CEED_EPSILON) printf("%f != %f = p(%f)\n", u_q_array[i], px, x_q_array[i]);
69     }
70     CeedVectorRestoreArrayRead(x_q, &x_q_array);
71     CeedVectorRestoreArrayRead(u_q, &u_q_array);
72   }
73 
74   CeedVectorDestroy(&x);
75   CeedVectorDestroy(&x_q);
76   CeedVectorDestroy(&u);
77   CeedVectorDestroy(&u_q);
78   CeedBasisDestroy(&basis_x_lobatto);
79   CeedBasisDestroy(&basis_u_lobatto);
80   CeedBasisDestroy(&basis_x_gauss);
81   CeedBasisDestroy(&basis_u_gauss);
82   CeedDestroy(&ceed);
83   return 0;
84 }
85