xref: /libCEED/tests/t351-basis.c (revision 743e43193999b7b492886ff8a7003d40e9db0cae)
1c8c3fa7dSJeremy L Thompson /// @file
253ef2869SZach Atkins /// Test polynomial interpolation to arbitrary points in multiple dimensions
3c8c3fa7dSJeremy L Thompson /// \test Test polynomial interpolation to arbitrary points in multiple dimensions
4c8c3fa7dSJeremy L Thompson #include <ceed.h>
5c8c3fa7dSJeremy L Thompson #include <math.h>
6c8c3fa7dSJeremy L Thompson #include <stdio.h>
7c8c3fa7dSJeremy L Thompson 
Eval(CeedInt dim,const CeedScalar x[])8c8c3fa7dSJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
9c8c3fa7dSJeremy L Thompson   CeedScalar result = 1, center = 0.1;
10c8c3fa7dSJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
11c8c3fa7dSJeremy L Thompson     result *= tanh(x[d] - center);
12c8c3fa7dSJeremy L Thompson     center += 0.1;
13c8c3fa7dSJeremy L Thompson   }
14c8c3fa7dSJeremy L Thompson   return result;
15c8c3fa7dSJeremy L Thompson }
16c8c3fa7dSJeremy L Thompson 
main(int argc,char ** argv)17c8c3fa7dSJeremy L Thompson int main(int argc, char **argv) {
18c8c3fa7dSJeremy L Thompson   Ceed ceed;
19c8c3fa7dSJeremy L Thompson 
20c8c3fa7dSJeremy L Thompson   CeedInit(argv[1], &ceed);
21c8c3fa7dSJeremy L Thompson 
22c8c3fa7dSJeremy L Thompson   for (CeedInt dim = 1; dim <= 3; dim++) {
23c8c3fa7dSJeremy L Thompson     CeedVector    x, x_nodes, x_points, u, v;
24c8c3fa7dSJeremy L Thompson     CeedBasis     basis_x, basis_u;
2571a83b88SJeremy L Thompson     const CeedInt p = 9, q = 10, num_points = 4, x_dim = CeedIntPow(2, dim), p_dim = CeedIntPow(p, dim);
26c8c3fa7dSJeremy L Thompson 
27c8c3fa7dSJeremy L Thompson     CeedVectorCreate(ceed, x_dim * dim, &x);
28c8c3fa7dSJeremy L Thompson     CeedVectorCreate(ceed, p_dim * dim, &x_nodes);
29c8c3fa7dSJeremy L Thompson     CeedVectorCreate(ceed, num_points * dim, &x_points);
30c8c3fa7dSJeremy L Thompson     CeedVectorCreate(ceed, p_dim, &u);
31c8c3fa7dSJeremy L Thompson     CeedVectorCreate(ceed, num_points, &v);
32c8c3fa7dSJeremy L Thompson 
33c8c3fa7dSJeremy L Thompson     // Get nodal coordinates
34c8c3fa7dSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, p, CEED_GAUSS_LOBATTO, &basis_x);
35c8c3fa7dSJeremy L Thompson     {
36c8c3fa7dSJeremy L Thompson       CeedScalar x_array[x_dim * dim];
37c8c3fa7dSJeremy L Thompson 
38c8c3fa7dSJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
3953ef2869SZach 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;
40c8c3fa7dSJeremy L Thompson       }
41c8c3fa7dSJeremy L Thompson       CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
42c8c3fa7dSJeremy L Thompson     }
43c8c3fa7dSJeremy L Thompson     CeedBasisApply(basis_x, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_nodes);
44c8c3fa7dSJeremy L Thompson 
45c8c3fa7dSJeremy L Thompson     // Set values of u at nodes
46c8c3fa7dSJeremy L Thompson     {
47c8c3fa7dSJeremy L Thompson       const CeedScalar *x_array;
48c8c3fa7dSJeremy L Thompson       CeedScalar        u_array[p_dim];
49c8c3fa7dSJeremy L Thompson 
50c8c3fa7dSJeremy L Thompson       CeedVectorGetArrayRead(x_nodes, CEED_MEM_HOST, &x_array);
51c8c3fa7dSJeremy L Thompson       for (CeedInt i = 0; i < p_dim; i++) {
52c8c3fa7dSJeremy L Thompson         CeedScalar coord[dim];
53c8c3fa7dSJeremy L Thompson 
54c8c3fa7dSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) coord[d] = x_array[d * p_dim + i];
55c8c3fa7dSJeremy L Thompson         u_array[i] = Eval(dim, coord);
56c8c3fa7dSJeremy L Thompson       }
57c8c3fa7dSJeremy L Thompson       CeedVectorRestoreArrayRead(x_nodes, &x_array);
58c8c3fa7dSJeremy L Thompson       CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
59c8c3fa7dSJeremy L Thompson     }
60c8c3fa7dSJeremy L Thompson 
61c8c3fa7dSJeremy L Thompson     // Interpolate to arbitrary points
62c8c3fa7dSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
63c8c3fa7dSJeremy L Thompson     {
642a94f45fSJeremy 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};
65c8c3fa7dSJeremy L Thompson 
66c8c3fa7dSJeremy L Thompson       CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
67c8c3fa7dSJeremy L Thompson     }
68*fc0f7cc6SJeremy L Thompson     CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x_points, u, v);
69c8c3fa7dSJeremy L Thompson 
70c8c3fa7dSJeremy L Thompson     {
71c8c3fa7dSJeremy L Thompson       const CeedScalar *x_array, *v_array;
72c8c3fa7dSJeremy L Thompson 
73c8c3fa7dSJeremy L Thompson       CeedVectorGetArrayRead(x_points, CEED_MEM_HOST, &x_array);
74c8c3fa7dSJeremy L Thompson       CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
75c8c3fa7dSJeremy L Thompson       for (CeedInt i = 0; i < num_points; i++) {
76c8c3fa7dSJeremy L Thompson         CeedScalar coord[dim];
77c8c3fa7dSJeremy L Thompson 
789c34f28eSJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) coord[d] = x_array[d * num_points + i];
79c8c3fa7dSJeremy L Thompson         const CeedScalar fx = Eval(dim, coord);
80c8c3fa7dSJeremy L Thompson         if (fabs(v_array[i] - fx) > 1E-4) {
81c8c3fa7dSJeremy L Thompson           // LCOV_EXCL_START
82c8c3fa7dSJeremy L Thompson           printf("[%" CeedInt_FMT "] %f != %f = f(%f", dim, v_array[i], fx, coord[0]);
83c8c3fa7dSJeremy L Thompson           for (CeedInt d = 1; d < dim; d++) printf(", %f", coord[d]);
849c34f28eSJeremy L Thompson           printf(")\n");
85c8c3fa7dSJeremy L Thompson           // LCOV_EXCL_STOP
86c8c3fa7dSJeremy L Thompson         }
87c8c3fa7dSJeremy L Thompson       }
88c8c3fa7dSJeremy L Thompson       CeedVectorRestoreArrayRead(x_points, &x_array);
89c8c3fa7dSJeremy L Thompson       CeedVectorRestoreArrayRead(v, &v_array);
90c8c3fa7dSJeremy L Thompson     }
91c8c3fa7dSJeremy L Thompson 
92c8c3fa7dSJeremy L Thompson     CeedVectorDestroy(&x);
93c8c3fa7dSJeremy L Thompson     CeedVectorDestroy(&x_nodes);
94c8c3fa7dSJeremy L Thompson     CeedVectorDestroy(&x_points);
95c8c3fa7dSJeremy L Thompson     CeedVectorDestroy(&u);
96c8c3fa7dSJeremy L Thompson     CeedVectorDestroy(&v);
97c8c3fa7dSJeremy L Thompson     CeedBasisDestroy(&basis_x);
98c8c3fa7dSJeremy L Thompson     CeedBasisDestroy(&basis_u);
99c8c3fa7dSJeremy L Thompson   }
100c8c3fa7dSJeremy L Thompson 
101c8c3fa7dSJeremy L Thompson   CeedDestroy(&ceed);
102c8c3fa7dSJeremy L Thompson   return 0;
103c8c3fa7dSJeremy L Thompson }
104