xref: /libCEED/tests/t353-basis.c (revision 743e43193999b7b492886ff8a7003d40e9db0cae)
12a94f45fSJeremy L Thompson /// @file
253ef2869SZach Atkins /// Test polynomial interpolation transpose from arbitrary points in 1D
32a94f45fSJeremy L Thompson /// \test Test polynomial interpolation transpose from arbitrary points in 1D
42a94f45fSJeremy L Thompson #include <ceed.h>
52a94f45fSJeremy L Thompson #include <math.h>
62a94f45fSJeremy L Thompson #include <stdio.h>
72a94f45fSJeremy L Thompson 
82a94f45fSJeremy L Thompson #define ALEN(a) (sizeof(a) / sizeof((a)[0]))
92a94f45fSJeremy L Thompson 
Eval(CeedScalar x,CeedInt n,const CeedScalar * c)102a94f45fSJeremy L Thompson static CeedScalar Eval(CeedScalar x, CeedInt n, const CeedScalar *c) {
112a94f45fSJeremy L Thompson   CeedScalar y = c[n - 1];
122a94f45fSJeremy L Thompson   for (CeedInt i = n - 2; i >= 0; i--) y = y * x + c[i];
132a94f45fSJeremy L Thompson   return y;
142a94f45fSJeremy L Thompson }
152a94f45fSJeremy L Thompson 
main(int argc,char ** argv)162a94f45fSJeremy L Thompson int main(int argc, char **argv) {
172a94f45fSJeremy L Thompson   Ceed             ceed;
182a94f45fSJeremy L Thompson   CeedVector       x, x_nodes, x_points, x_point, u, v, u_point, v_point;
192a94f45fSJeremy L Thompson   CeedBasis        basis_x, basis_u;
202a94f45fSJeremy L Thompson   const CeedInt    p = 5, q = 5, num_points = 4;
212a94f45fSJeremy L Thompson   const CeedScalar c[4] = {1, 2, 3, 4};  // 1 + 2x + 3x^2 + ...
222a94f45fSJeremy L Thompson 
232a94f45fSJeremy L Thompson   CeedInit(argv[1], &ceed);
242a94f45fSJeremy L Thompson 
252a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, 2, &x);
262a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, p, &x_nodes);
272a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, num_points, &x_points);
282a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, 1, &x_point);
292a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, p, &u);
302a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, num_points, &v);
312a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, p, &u_point);
322a94f45fSJeremy L Thompson   CeedVectorCreate(ceed, 1, &v_point);
332a94f45fSJeremy L Thompson   CeedVectorSetValue(v_point, 1.0);
342a94f45fSJeremy L Thompson 
352a94f45fSJeremy L Thompson   // Get nodal coordinates
362a94f45fSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, p, CEED_GAUSS_LOBATTO, &basis_x);
372a94f45fSJeremy L Thompson   {
382a94f45fSJeremy L Thompson     CeedScalar x_array[2];
392a94f45fSJeremy L Thompson 
402a94f45fSJeremy L Thompson     for (CeedInt i = 0; i < 2; i++) x_array[i] = CeedIntPow(-1, i + 1);
412a94f45fSJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
422a94f45fSJeremy L Thompson   }
432a94f45fSJeremy L Thompson   CeedBasisApply(basis_x, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_nodes);
442a94f45fSJeremy L Thompson 
452a94f45fSJeremy L Thompson   // Set values of u at nodes
462a94f45fSJeremy L Thompson   {
472a94f45fSJeremy L Thompson     const CeedScalar *x_array;
482a94f45fSJeremy L Thompson     CeedScalar        u_array[p];
492a94f45fSJeremy L Thompson 
502a94f45fSJeremy L Thompson     CeedVectorGetArrayRead(x_nodes, CEED_MEM_HOST, &x_array);
512a94f45fSJeremy L Thompson     for (CeedInt i = 0; i < p; i++) u_array[i] = Eval(x_array[i], ALEN(c), c);
522a94f45fSJeremy L Thompson     CeedVectorRestoreArrayRead(x_nodes, &x_array);
532a94f45fSJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
542a94f45fSJeremy L Thompson   }
552a94f45fSJeremy L Thompson 
562a94f45fSJeremy L Thompson   // Interpolate to arbitrary points
572a94f45fSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
582a94f45fSJeremy L Thompson   {
592a94f45fSJeremy L Thompson     CeedScalar x_array[4] = {-0.33, -0.65, 0.16, 0.99};
602a94f45fSJeremy L Thompson 
612a94f45fSJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
622a94f45fSJeremy L Thompson   }
63*fc0f7cc6SJeremy L Thompson   CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x_points, u, v);
642a94f45fSJeremy L Thompson 
652a94f45fSJeremy L Thompson   for (CeedInt i = 0; i < num_points; i++) {
66*fc0f7cc6SJeremy L Thompson     const CeedInt     num_point[1] = {1};
672a94f45fSJeremy L Thompson     CeedScalar        fx           = 0.0;
682a94f45fSJeremy L Thompson     const CeedScalar *x_array, *u_array, *v_array, *u_point_array;
692a94f45fSJeremy L Thompson 
702a94f45fSJeremy L Thompson     CeedVectorGetArrayRead(x_points, CEED_MEM_HOST, &x_array);
712a94f45fSJeremy L Thompson     CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array);
722a94f45fSJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
732a94f45fSJeremy L Thompson     CeedVectorSetValue(x_point, x_array[i]);
74*fc0f7cc6SJeremy L Thompson     CeedBasisApplyAtPoints(basis_u, 1, num_point, CEED_TRANSPOSE, CEED_EVAL_INTERP, x_point, v_point, u_point);
752a94f45fSJeremy L Thompson     CeedVectorGetArrayRead(u_point, CEED_MEM_HOST, &u_point_array);
762a94f45fSJeremy L Thompson     for (CeedInt j = 0; j < p; j++) fx += u_array[j] * u_point_array[j];
772a94f45fSJeremy L Thompson     if (fabs(v_array[i] - fx) > 100. * CEED_EPSILON) printf("%f != %f = f(%f)\n", v_array[i], fx, x_array[i]);
782a94f45fSJeremy L Thompson     CeedVectorRestoreArrayRead(u_point, &u_point_array);
792a94f45fSJeremy L Thompson     CeedVectorRestoreArrayRead(x_points, &x_array);
802a94f45fSJeremy L Thompson     CeedVectorRestoreArrayRead(u, &u_array);
812a94f45fSJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
822a94f45fSJeremy L Thompson   }
832a94f45fSJeremy L Thompson 
842a94f45fSJeremy L Thompson   CeedVectorDestroy(&x);
852a94f45fSJeremy L Thompson   CeedVectorDestroy(&x_nodes);
862a94f45fSJeremy L Thompson   CeedVectorDestroy(&x_points);
872a94f45fSJeremy L Thompson   CeedVectorDestroy(&x_point);
882a94f45fSJeremy L Thompson   CeedVectorDestroy(&u);
892a94f45fSJeremy L Thompson   CeedVectorDestroy(&v);
902a94f45fSJeremy L Thompson   CeedVectorDestroy(&u_point);
912a94f45fSJeremy L Thompson   CeedVectorDestroy(&v_point);
922a94f45fSJeremy L Thompson   CeedBasisDestroy(&basis_x);
932a94f45fSJeremy L Thompson   CeedBasisDestroy(&basis_u);
942a94f45fSJeremy L Thompson   CeedDestroy(&ceed);
952a94f45fSJeremy L Thompson   return 0;
962a94f45fSJeremy L Thompson }
97