xref: /libCEED/tests/t354-basis.c (revision 290fc47b5baa6f633b0072e77241e8667e9a8675)
12a94f45fSJeremy L Thompson /// @file
29e1d4b82SJeremy L Thompson /// Test polynomial interpolation transpose to arbitrary points in multiple dimensions
39e1d4b82SJeremy L Thompson /// \test Test polynomial interpolation transpose to arbitrary points in multiple dimensions
42a94f45fSJeremy L Thompson #include <ceed.h>
52a94f45fSJeremy L Thompson #include <math.h>
62a94f45fSJeremy L Thompson #include <stdio.h>
72a94f45fSJeremy L Thompson 
Eval(CeedInt dim,const CeedScalar x[])82a94f45fSJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
92a94f45fSJeremy L Thompson   CeedScalar result = 1, center = 0.1;
102a94f45fSJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
112a94f45fSJeremy L Thompson     result *= tanh(x[d] - center);
122a94f45fSJeremy L Thompson     center += 0.1;
132a94f45fSJeremy L Thompson   }
142a94f45fSJeremy L Thompson   return result;
152a94f45fSJeremy L Thompson }
162a94f45fSJeremy L Thompson 
main(int argc,char ** argv)172a94f45fSJeremy L Thompson int main(int argc, char **argv) {
182a94f45fSJeremy L Thompson   Ceed ceed;
192a94f45fSJeremy L Thompson 
202a94f45fSJeremy L Thompson   CeedInit(argv[1], &ceed);
212a94f45fSJeremy L Thompson 
222a94f45fSJeremy L Thompson   for (CeedInt dim = 1; dim <= 3; dim++) {
232a94f45fSJeremy L Thompson     CeedVector    x, x_nodes, x_points, x_point, u, v, u_point, v_point;
242a94f45fSJeremy L Thompson     CeedBasis     basis_x, basis_u;
252a94f45fSJeremy L Thompson     const CeedInt p = 9, q = 9, num_points = 4, x_dim = CeedIntPow(2, dim), p_dim = CeedIntPow(p, dim);
262a94f45fSJeremy L Thompson 
272a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, x_dim * dim, &x);
282a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, p_dim * dim, &x_nodes);
292a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, num_points * dim, &x_points);
302a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, dim, &x_point);
312a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, p_dim, &u);
322a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, num_points, &v);
332a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, p_dim, &u_point);
342a94f45fSJeremy L Thompson     CeedVectorCreate(ceed, 1, &v_point);
352a94f45fSJeremy L Thompson     CeedVectorSetValue(v_point, 1.0);
362a94f45fSJeremy L Thompson 
372a94f45fSJeremy L Thompson     // Get nodal coordinates
382a94f45fSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, p, CEED_GAUSS_LOBATTO, &basis_x);
392a94f45fSJeremy L Thompson     {
402a94f45fSJeremy L Thompson       CeedScalar x_array[x_dim * dim];
412a94f45fSJeremy L Thompson 
422a94f45fSJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
4353ef2869SZach Atkins         for (CeedInt i = 0; i < x_dim; i++) x_array[d * x_dim + i] = (i % CeedIntPow(2, d + 1)) / CeedIntPow(2, d) ? 1 : -1;
442a94f45fSJeremy L Thompson       }
452a94f45fSJeremy L Thompson       CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
462a94f45fSJeremy L Thompson     }
472a94f45fSJeremy L Thompson     CeedBasisApply(basis_x, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_nodes);
482a94f45fSJeremy L Thompson 
492a94f45fSJeremy L Thompson     // Set values of u at nodes
502a94f45fSJeremy L Thompson     {
512a94f45fSJeremy L Thompson       const CeedScalar *x_array;
522a94f45fSJeremy L Thompson       CeedScalar        u_array[p_dim];
532a94f45fSJeremy L Thompson 
542a94f45fSJeremy L Thompson       CeedVectorGetArrayRead(x_nodes, CEED_MEM_HOST, &x_array);
552a94f45fSJeremy L Thompson       for (CeedInt i = 0; i < p_dim; i++) {
562a94f45fSJeremy L Thompson         CeedScalar coord[dim];
572a94f45fSJeremy L Thompson 
582a94f45fSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) coord[d] = x_array[d * p_dim + i];
592a94f45fSJeremy L Thompson         u_array[i] = Eval(dim, coord);
602a94f45fSJeremy L Thompson       }
612a94f45fSJeremy L Thompson       CeedVectorRestoreArrayRead(x_nodes, &x_array);
622a94f45fSJeremy L Thompson       CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
632a94f45fSJeremy L Thompson     }
642a94f45fSJeremy L Thompson 
652a94f45fSJeremy L Thompson     // Interpolate to arbitrary points
662a94f45fSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
672a94f45fSJeremy L Thompson     {
682a94f45fSJeremy L Thompson       CeedScalar x_array[12] = {-0.33, -0.65, 0.16, 0.99, -0.65, 0.16, 0.99, -0.33, 0.16, 0.99, -0.33, -0.65};
692a94f45fSJeremy L Thompson 
702a94f45fSJeremy L Thompson       CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
712a94f45fSJeremy L Thompson     }
72fc0f7cc6SJeremy L Thompson     CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x_points, u, v);
732a94f45fSJeremy L Thompson 
742a94f45fSJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) {
75fc0f7cc6SJeremy L Thompson       const CeedInt     num_point[1] = {1};
762a94f45fSJeremy L Thompson       CeedScalar        fx           = 0.0;
772a94f45fSJeremy L Thompson       CeedScalar        coord[dim];
782a94f45fSJeremy L Thompson       const CeedScalar *x_array, *u_array, *v_array, *u_point_array;
792a94f45fSJeremy L Thompson 
802a94f45fSJeremy L Thompson       CeedVectorGetArrayRead(x_points, CEED_MEM_HOST, &x_array);
812a94f45fSJeremy L Thompson       CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array);
822a94f45fSJeremy L Thompson       CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
839c34f28eSJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) coord[d] = x_array[d * num_points + i];
842a94f45fSJeremy L Thompson       CeedVectorSetArray(x_point, CEED_MEM_HOST, CEED_COPY_VALUES, coord);
85fc0f7cc6SJeremy L Thompson       CeedBasisApplyAtPoints(basis_u, 1, num_point, CEED_TRANSPOSE, CEED_EVAL_INTERP, x_point, v_point, u_point);
862a94f45fSJeremy L Thompson       CeedVectorGetArrayRead(u_point, CEED_MEM_HOST, &u_point_array);
872a94f45fSJeremy L Thompson       for (CeedInt j = 0; j < p_dim; j++) fx += u_array[j] * u_point_array[j];
88*1b3d9bd6SJeremy L Thompson       if (fabs(v_array[i] - fx) > 500. * CEED_EPSILON) {
892a94f45fSJeremy L Thompson         // LCOV_EXCL_START
902a94f45fSJeremy L Thompson         printf("[%" CeedInt_FMT "] %f != %f = f(%f", dim, v_array[i], fx, coord[0]);
912a94f45fSJeremy L Thompson         for (CeedInt d = 1; d < dim; d++) printf(", %f", coord[d]);
929c34f28eSJeremy L Thompson         printf(")\n");
932a94f45fSJeremy L Thompson         // LCOV_EXCL_STOP
942a94f45fSJeremy L Thompson       }
952a94f45fSJeremy L Thompson       CeedVectorRestoreArrayRead(u_point, &u_point_array);
962a94f45fSJeremy L Thompson       CeedVectorRestoreArrayRead(x_points, &x_array);
972a94f45fSJeremy L Thompson       CeedVectorRestoreArrayRead(u, &u_array);
982a94f45fSJeremy L Thompson       CeedVectorRestoreArrayRead(v, &v_array);
992a94f45fSJeremy L Thompson     }
1002a94f45fSJeremy L Thompson 
1012a94f45fSJeremy L Thompson     CeedVectorDestroy(&x);
1022a94f45fSJeremy L Thompson     CeedVectorDestroy(&x_nodes);
1032a94f45fSJeremy L Thompson     CeedVectorDestroy(&x_points);
1042a94f45fSJeremy L Thompson     CeedVectorDestroy(&x_point);
1052a94f45fSJeremy L Thompson     CeedVectorDestroy(&u);
1062a94f45fSJeremy L Thompson     CeedVectorDestroy(&v);
1072a94f45fSJeremy L Thompson     CeedVectorDestroy(&u_point);
1082a94f45fSJeremy L Thompson     CeedVectorDestroy(&v_point);
1092a94f45fSJeremy L Thompson     CeedBasisDestroy(&basis_x);
1102a94f45fSJeremy L Thompson     CeedBasisDestroy(&basis_u);
1112a94f45fSJeremy L Thompson   }
1122a94f45fSJeremy L Thompson 
1132a94f45fSJeremy L Thompson   CeedDestroy(&ceed);
1142a94f45fSJeremy L Thompson   return 0;
1152a94f45fSJeremy L Thompson }
116