xref: /libCEED/tests/t318-basis.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1d3f518cbSJeremy L Thompson /// @file
2d3f518cbSJeremy L Thompson /// Test high order interpolation in multiple dimensions
3d3f518cbSJeremy L Thompson /// \test Test high order interpolation in multiple dimensions
4d3f518cbSJeremy L Thompson #include <ceed.h>
5d3f518cbSJeremy L Thompson #include <math.h>
6d3f518cbSJeremy L Thompson 
7d3f518cbSJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
8d3f518cbSJeremy L Thompson   CeedScalar result = 1, center = 0.1;
9d3f518cbSJeremy L Thompson   for (CeedInt d = 0; d < dim; d++) {
10d3f518cbSJeremy L Thompson     result *= tanh(x[d] - center);
11d3f518cbSJeremy L Thompson     center += 0.1;
12d3f518cbSJeremy L Thompson   }
13d3f518cbSJeremy L Thompson   return result;
14d3f518cbSJeremy L Thompson }
15d3f518cbSJeremy L Thompson 
16d3f518cbSJeremy L Thompson int main(int argc, char **argv) {
17d3f518cbSJeremy L Thompson   Ceed ceed;
18d3f518cbSJeremy L Thompson 
19d3f518cbSJeremy L Thompson   CeedInit(argv[1], &ceed);
20d3f518cbSJeremy L Thompson   for (CeedInt dim = 1; dim <= 3; dim++) {
21*4fee36f0SJeremy L Thompson     CeedVector x, x_q, u, u_q;
22d1d35e2fSjeremylt     CeedBasis  basis_x_lobatto, basis_u_lobatto, basis_x_gauss, basis_u_gauss;
23*4fee36f0SJeremy L Thompson     CeedInt    q = 11, q_dim = CeedIntPow(q, dim), x_dim = CeedIntPow(2, dim);
24d3f518cbSJeremy L Thompson 
25*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, x_dim * dim, &x);
26*4fee36f0SJeremy L Thompson     {
27*4fee36f0SJeremy L Thompson       CeedScalar x_array[x_dim * dim];
28d3f518cbSJeremy L Thompson 
29*4fee36f0SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
30*4fee36f0SJeremy L Thompson         for (CeedInt i = 0; i < x_dim; i++) x_array[d * x_dim + i] = (i % CeedIntPow(2, dim - d)) / CeedIntPow(2, dim - d - 1) ? 1 : -1;
31d3f518cbSJeremy L Thompson       }
32*4fee36f0SJeremy L Thompson       CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
33*4fee36f0SJeremy L Thompson     }
34*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, q_dim * dim, &x_q);
35*4fee36f0SJeremy L Thompson     CeedVectorSetValue(x_q, 0);
36*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, q_dim, &u);
37*4fee36f0SJeremy L Thompson     CeedVectorSetValue(u, 0);
38*4fee36f0SJeremy L Thompson     CeedVectorCreate(ceed, q_dim, &u_q);
39*4fee36f0SJeremy L Thompson 
40*4fee36f0SJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS_LOBATTO, &basis_x_lobatto);
41*4fee36f0SJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, q, q, CEED_GAUSS_LOBATTO, &basis_u_lobatto);
42*4fee36f0SJeremy L Thompson 
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     {
46*4fee36f0SJeremy L Thompson       const CeedScalar *x_q_array;
47*4fee36f0SJeremy L Thompson       CeedScalar        u_q_array[q_dim];
48*4fee36f0SJeremy L Thompson 
49*4fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
50*4fee36f0SJeremy L Thompson       for (CeedInt i = 0; i < q_dim; i++) {
51*4fee36f0SJeremy L Thompson         CeedScalar coord[dim];
52*4fee36f0SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * q_dim + i];
53*4fee36f0SJeremy L Thompson         u_q_array[i] = Eval(dim, coord);
54*4fee36f0SJeremy L Thompson       }
55*4fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(x_q, &x_q_array);
56*4fee36f0SJeremy L Thompson       CeedVectorSetArray(u_q, CEED_MEM_HOST, CEED_COPY_VALUES, u_q_array);
57*4fee36f0SJeremy L Thompson     }
58d3f518cbSJeremy L Thompson 
59d3f518cbSJeremy L Thompson     // This operation is the identity because the quadrature is collocated
60*4fee36f0SJeremy L Thompson     CeedBasisApply(basis_u_lobatto, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, u_q, u);
61d3f518cbSJeremy L Thompson 
62*4fee36f0SJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x_gauss);
63*4fee36f0SJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, q, q, CEED_GAUSS, &basis_u_gauss);
64d3f518cbSJeremy L Thompson 
65*4fee36f0SJeremy L Thompson     CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
66*4fee36f0SJeremy L Thompson     CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, u_q);
67d3f518cbSJeremy L Thompson 
68*4fee36f0SJeremy L Thompson     {
69*4fee36f0SJeremy L Thompson       const CeedScalar *x_q_array, *u_array;
70*4fee36f0SJeremy L Thompson 
71*4fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
72*4fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_array);
73*4fee36f0SJeremy L Thompson       for (CeedInt i = 0; i < q_dim; i++) {
74*4fee36f0SJeremy L Thompson         CeedScalar coord[dim];
75*4fee36f0SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * q_dim + i];
76*4fee36f0SJeremy L Thompson         CeedScalar fx = Eval(dim, coord);
77*4fee36f0SJeremy L Thompson         if (fabs(u_array[i] - fx) > 1E-4) {
78d3f518cbSJeremy L Thompson           // LCOV_EXCL_START
79*4fee36f0SJeremy L Thompson           printf("[%" CeedInt_FMT "] %f != %f = f(%f", dim, u_array[i], fx, coord[0]);
80*4fee36f0SJeremy L Thompson           for (CeedInt d = 1; d < dim; d++) printf(",%f", coord[d]);
81d3f518cbSJeremy L Thompson           puts(")");
82d3f518cbSJeremy L Thompson           // LCOV_EXCL_STOP
83d3f518cbSJeremy L Thompson         }
84d3f518cbSJeremy L Thompson       }
85*4fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(x_q, &x_q_array);
86*4fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(u_q, &u_array);
87*4fee36f0SJeremy L Thompson     }
88d3f518cbSJeremy L Thompson 
89*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&x);
90*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&x_q);
91*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&u);
92*4fee36f0SJeremy L Thompson     CeedVectorDestroy(&u_q);
93d1d35e2fSjeremylt     CeedBasisDestroy(&basis_x_lobatto);
94d1d35e2fSjeremylt     CeedBasisDestroy(&basis_u_lobatto);
95d1d35e2fSjeremylt     CeedBasisDestroy(&basis_x_gauss);
96d1d35e2fSjeremylt     CeedBasisDestroy(&basis_u_gauss);
97d3f518cbSJeremy L Thompson   }
98d3f518cbSJeremy L Thompson   CeedDestroy(&ceed);
99d3f518cbSJeremy L Thompson   return 0;
100d3f518cbSJeremy L Thompson }
101