xref: /libCEED/tests/t312-basis.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1a8de75f0Sjeremylt /// @file
252bfb9bbSJeremy L Thompson /// Test polynomial interpolation in 1D
352bfb9bbSJeremy L Thompson /// \test Test polynomial interpolation in 1D
4a8de75f0Sjeremylt #include <ceed.h>
5a8de75f0Sjeremylt #include <math.h>
6a8de75f0Sjeremylt 
752bfb9bbSJeremy L Thompson #define ALEN(a) (sizeof(a) / sizeof((a)[0]))
852bfb9bbSJeremy L Thompson 
9*4fee36f0SJeremy L Thompson static CeedScalar Eval(CeedScalar x, CeedInt n, const CeedScalar *p) {
1052bfb9bbSJeremy L Thompson   CeedScalar y = p[n - 1];
1152bfb9bbSJeremy L Thompson   for (CeedInt i = n - 2; i >= 0; i--) y = y * x + p[i];
1252bfb9bbSJeremy L Thompson   return y;
13a8de75f0Sjeremylt }
14a8de75f0Sjeremylt 
15a8de75f0Sjeremylt int main(int argc, char **argv) {
16a8de75f0Sjeremylt   Ceed             ceed;
17*4fee36f0SJeremy L Thompson   CeedVector       x, x_q, u, u_q, w;
18d1d35e2fSjeremylt   CeedBasis        basis_x_lobatto, basis_x_gauss, basis_u_gauss;
19*4fee36f0SJeremy L Thompson   CeedInt          q    = 6;
2052bfb9bbSJeremy L Thompson   const CeedScalar p[6] = {1, 2, 3, 4, 5, 6};  // 1 + 2x + 3x^2 + ...
21*4fee36f0SJeremy L Thompson   CeedScalar       sum, error, pint[ALEN(p) + 1];
22a8de75f0Sjeremylt 
23a8de75f0Sjeremylt   CeedInit(argv[1], &ceed);
24aedaa0e5Sjeremylt 
25*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, 2, &x);
26*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &x_q);
27*4fee36f0SJeremy L Thompson   CeedVectorSetValue(x_q, 0);
28*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
29*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u_q);
30*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u_q, 0);
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
32*4fee36f0SJeremy L Thompson   CeedVectorSetValue(w, 0);
33a8de75f0Sjeremylt 
34*4fee36f0SJeremy L Thompson   {
35*4fee36f0SJeremy L Thompson     CeedScalar x_array[2];
36aedaa0e5Sjeremylt 
37*4fee36f0SJeremy L Thompson     for (int i = 0; i < 2; i++) x_array[i] = CeedIntPow(-1, i + 1);
38*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
39*4fee36f0SJeremy L Thompson   }
40aedaa0e5Sjeremylt 
41*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS_LOBATTO, &basis_x_lobatto);
42a8de75f0Sjeremylt 
43*4fee36f0SJeremy L Thompson   CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
44*4fee36f0SJeremy L Thompson   {
45*4fee36f0SJeremy L Thompson     const CeedScalar *x_q_array;
46*4fee36f0SJeremy L Thompson     CeedScalar        u_array[q];
4752bfb9bbSJeremy L Thompson 
48*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
49*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) u_array[i] = Eval(x_q_array[i], ALEN(p), p);
50*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(x_q, &x_q_array);
51*4fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
52*4fee36f0SJeremy L Thompson   }
5352bfb9bbSJeremy L Thompson 
54*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x_gauss);
55*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, q, q, CEED_GAUSS, &basis_u_gauss);
5652bfb9bbSJeremy L Thompson 
57*4fee36f0SJeremy L Thompson   CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
58*4fee36f0SJeremy L Thompson   CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, u_q);
59*4fee36f0SJeremy L Thompson   CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, w);
60*4fee36f0SJeremy L Thompson 
61*4fee36f0SJeremy L Thompson   {
62*4fee36f0SJeremy L Thompson     const CeedScalar *w_array, *u_q_array;
63*4fee36f0SJeremy L Thompson 
64*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(w, CEED_MEM_HOST, &w_array);
65*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_q_array);
66a8de75f0Sjeremylt     sum = 0;
67*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) sum += w_array[i] * u_q_array[i];
68*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(w, &w_array);
69*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(u_q, &u_q_array);
70*4fee36f0SJeremy L Thompson   }
71a8de75f0Sjeremylt 
7252bfb9bbSJeremy L Thompson   pint[0] = 0;
732b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < (int)ALEN(p); i++) pint[i + 1] = p[i] / (i + 1);
74*4fee36f0SJeremy L Thompson   error = sum - Eval(1, ALEN(pint), pint) + Eval(-1, ALEN(pint), pint);
752b730f8bSJeremy L Thompson   if (error > 100. * CEED_EPSILON) {
7652bfb9bbSJeremy L Thompson     // LCOV_EXCL_START
77*4fee36f0SJeremy L Thompson     printf("Error %e  sum %g  exact %g\n", error, sum, Eval(1, ALEN(pint), pint) - Eval(-1, ALEN(pint), pint));
7852bfb9bbSJeremy L Thompson     // LCOV_EXCL_STOP
792b730f8bSJeremy L Thompson   }
8052bfb9bbSJeremy L Thompson 
81*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
82*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x_q);
83*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
84*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u_q);
85*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
86d1d35e2fSjeremylt   CeedBasisDestroy(&basis_x_lobatto);
87d1d35e2fSjeremylt   CeedBasisDestroy(&basis_x_gauss);
88d1d35e2fSjeremylt   CeedBasisDestroy(&basis_u_gauss);
89a8de75f0Sjeremylt   CeedDestroy(&ceed);
90a8de75f0Sjeremylt   return 0;
91a8de75f0Sjeremylt }
92