xref: /libCEED/tests/t355-basis.c (revision 743e43193999b7b492886ff8a7003d40e9db0cae)
1edfbf3a6SJeremy L Thompson /// @file
253ef2869SZach Atkins /// Test polynomial gradient to arbitrary points in 1D
3edfbf3a6SJeremy L Thompson /// \test Test polynomial gradient to arbitrary points in 1D
4edfbf3a6SJeremy L Thompson #include <ceed.h>
5edfbf3a6SJeremy L Thompson #include <math.h>
6edfbf3a6SJeremy L Thompson #include <stdio.h>
7edfbf3a6SJeremy L Thompson 
8edfbf3a6SJeremy L Thompson #define ALEN(a) (sizeof(a) / sizeof((a)[0]))
9edfbf3a6SJeremy L Thompson 
Eval(CeedScalar x,CeedInt n,const CeedScalar * c)10edfbf3a6SJeremy L Thompson static CeedScalar Eval(CeedScalar x, CeedInt n, const CeedScalar *c) {
11edfbf3a6SJeremy L Thompson   CeedScalar y = c[n - 1];
12edfbf3a6SJeremy L Thompson   for (CeedInt i = n - 2; i >= 0; i--) y = y * x + c[i];
13edfbf3a6SJeremy L Thompson   return y;
14edfbf3a6SJeremy L Thompson }
15edfbf3a6SJeremy L Thompson 
EvalGrad(CeedScalar x,CeedInt n,const CeedScalar * c)16edfbf3a6SJeremy L Thompson static CeedScalar EvalGrad(CeedScalar x, CeedInt n, const CeedScalar *c) {
17edfbf3a6SJeremy L Thompson   CeedScalar y = (n - 1) * c[n - 1];
18edfbf3a6SJeremy L Thompson   for (CeedInt i = n - 2; i >= 1; i--) y = y * x + i * c[i];
19edfbf3a6SJeremy L Thompson   return y;
20edfbf3a6SJeremy L Thompson }
21edfbf3a6SJeremy L Thompson 
main(int argc,char ** argv)22edfbf3a6SJeremy L Thompson int main(int argc, char **argv) {
23edfbf3a6SJeremy L Thompson   Ceed             ceed;
24edfbf3a6SJeremy L Thompson   CeedVector       x, x_nodes, x_points, u, v;
25edfbf3a6SJeremy L Thompson   CeedBasis        basis_x, basis_u;
26edfbf3a6SJeremy L Thompson   const CeedInt    p = 5, q = 5, num_points = 4;
27edfbf3a6SJeremy L Thompson   const CeedScalar c[4] = {1, 2, 3, 4};  // f = 1 + 2x + 3x^2 + ..., df = 2 + 6x + 12x^2 + ...
28edfbf3a6SJeremy L Thompson 
29edfbf3a6SJeremy L Thompson   CeedInit(argv[1], &ceed);
30edfbf3a6SJeremy L Thompson 
31edfbf3a6SJeremy L Thompson   CeedVectorCreate(ceed, 2, &x);
32edfbf3a6SJeremy L Thompson   CeedVectorCreate(ceed, p, &x_nodes);
33edfbf3a6SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &x_points);
34edfbf3a6SJeremy L Thompson   CeedVectorCreate(ceed, p, &u);
35edfbf3a6SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &v);
36edfbf3a6SJeremy L Thompson 
37edfbf3a6SJeremy L Thompson   // Get nodal coordinates
38edfbf3a6SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, p, CEED_GAUSS_LOBATTO, &basis_x);
39edfbf3a6SJeremy L Thompson   {
40edfbf3a6SJeremy L Thompson     CeedScalar x_array[2];
41edfbf3a6SJeremy L Thompson 
42edfbf3a6SJeremy L Thompson     for (CeedInt i = 0; i < 2; i++) x_array[i] = CeedIntPow(-1, i + 1);
43edfbf3a6SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
44edfbf3a6SJeremy L Thompson   }
45edfbf3a6SJeremy L Thompson   CeedBasisApply(basis_x, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_nodes);
46edfbf3a6SJeremy L Thompson 
47edfbf3a6SJeremy L Thompson   // Set values of u at nodes
48edfbf3a6SJeremy L Thompson   {
49edfbf3a6SJeremy L Thompson     const CeedScalar *x_array;
50edfbf3a6SJeremy L Thompson     CeedScalar        u_array[p];
51edfbf3a6SJeremy L Thompson 
52edfbf3a6SJeremy L Thompson     CeedVectorGetArrayRead(x_nodes, CEED_MEM_HOST, &x_array);
53edfbf3a6SJeremy L Thompson     for (CeedInt i = 0; i < p; i++) u_array[i] = Eval(x_array[i], ALEN(c), c);
54edfbf3a6SJeremy L Thompson     CeedVectorRestoreArrayRead(x_nodes, &x_array);
55edfbf3a6SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
56edfbf3a6SJeremy L Thompson   }
57edfbf3a6SJeremy L Thompson 
58edfbf3a6SJeremy L Thompson   // Interpolate to arbitrary points
59edfbf3a6SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
60edfbf3a6SJeremy L Thompson   {
61edfbf3a6SJeremy L Thompson     CeedScalar x_array[4] = {-0.33, -0.65, 0.16, 0.99};
62edfbf3a6SJeremy L Thompson 
63edfbf3a6SJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
64edfbf3a6SJeremy L Thompson   }
65*fc0f7cc6SJeremy L Thompson   CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, x_points, u, v);
66edfbf3a6SJeremy L Thompson 
67edfbf3a6SJeremy L Thompson   {
68edfbf3a6SJeremy L Thompson     const CeedScalar *x_array, *v_array;
69edfbf3a6SJeremy L Thompson 
70edfbf3a6SJeremy L Thompson     CeedVectorGetArrayRead(x_points, CEED_MEM_HOST, &x_array);
71edfbf3a6SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
72edfbf3a6SJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) {
73edfbf3a6SJeremy L Thompson       CeedScalar dfx = EvalGrad(x_array[i], ALEN(c), c);
74edfbf3a6SJeremy L Thompson       if (fabs(v_array[i] - dfx) > 500. * CEED_EPSILON) printf("%f != %f = df(%f)\n", v_array[i], dfx, x_array[i]);
75edfbf3a6SJeremy L Thompson     }
76edfbf3a6SJeremy L Thompson     CeedVectorRestoreArrayRead(x_points, &x_array);
77edfbf3a6SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
78edfbf3a6SJeremy L Thompson   }
79edfbf3a6SJeremy L Thompson 
80edfbf3a6SJeremy L Thompson   CeedVectorDestroy(&x);
81edfbf3a6SJeremy L Thompson   CeedVectorDestroy(&x_nodes);
82edfbf3a6SJeremy L Thompson   CeedVectorDestroy(&x_points);
83edfbf3a6SJeremy L Thompson   CeedVectorDestroy(&u);
84edfbf3a6SJeremy L Thompson   CeedVectorDestroy(&v);
85edfbf3a6SJeremy L Thompson   CeedBasisDestroy(&basis_x);
86edfbf3a6SJeremy L Thompson   CeedBasisDestroy(&basis_u);
87edfbf3a6SJeremy L Thompson   CeedDestroy(&ceed);
88edfbf3a6SJeremy L Thompson   return 0;
89edfbf3a6SJeremy L Thompson }
90