xref: /libCEED/tests/t313-basis.c (revision 52bfb9bbf17f17edbcd45876cdc8689a879bc683)
1a8de75f0Sjeremylt /// @file
2*52bfb9bbSJeremy L Thompson /// Test interpolation in multiple dimensions
3*52bfb9bbSJeremy L Thompson /// \test Test interpolation in multiple dimensions
4a8de75f0Sjeremylt #include <ceed.h>
5a8de75f0Sjeremylt #include <math.h>
6a8de75f0Sjeremylt 
7*52bfb9bbSJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
8*52bfb9bbSJeremy L Thompson   CeedScalar result = 1, center = 0.1;
9*52bfb9bbSJeremy L Thompson   for (CeedInt d=0; d<dim; d++) {
10*52bfb9bbSJeremy L Thompson     result *= tanh(x[d] - center);
11*52bfb9bbSJeremy L Thompson     center += 0.1;
12a8de75f0Sjeremylt   }
13*52bfb9bbSJeremy L Thompson   return result;
14a8de75f0Sjeremylt }
15a8de75f0Sjeremylt 
16a8de75f0Sjeremylt int main(int argc, char **argv) {
17a8de75f0Sjeremylt   Ceed ceed;
18a8de75f0Sjeremylt 
19a8de75f0Sjeremylt   CeedInit(argv[1], &ceed);
20*52bfb9bbSJeremy L Thompson   for (CeedInt dim=1; dim<=3; dim++) {
21*52bfb9bbSJeremy L Thompson     CeedVector X, Xq, U, Uq;
22*52bfb9bbSJeremy L Thompson     CeedBasis bxl, bul, bxg, bug;
23*52bfb9bbSJeremy L Thompson     CeedInt Q = 10, Qdim = CeedIntPow(Q, dim), Xdim = CeedIntPow(2, dim);
24*52bfb9bbSJeremy L Thompson     CeedScalar x[Xdim*dim];
25*52bfb9bbSJeremy L Thompson     const CeedScalar *xq, *u;
26*52bfb9bbSJeremy L Thompson     CeedScalar uq[Qdim];
27aedaa0e5Sjeremylt 
28*52bfb9bbSJeremy L Thompson     for (CeedInt d=0; d<dim; d++)
29*52bfb9bbSJeremy L Thompson       for (CeedInt i=0; i<Xdim; i++)
30*52bfb9bbSJeremy L Thompson         x[d*Xdim + i] = (i % CeedIntPow(2, dim-d)) / CeedIntPow(2, dim-d-1) ? 1 : -1;
31a8de75f0Sjeremylt 
32*52bfb9bbSJeremy L Thompson     CeedVectorCreate(ceed, Xdim*dim, &X);
33*52bfb9bbSJeremy L Thompson     CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
34*52bfb9bbSJeremy L Thompson     CeedVectorCreate(ceed, Qdim*dim, &Xq);
35*52bfb9bbSJeremy L Thompson     CeedVectorSetValue(Xq, 0);
36*52bfb9bbSJeremy L Thompson     CeedVectorCreate(ceed, Qdim, &U);
37*52bfb9bbSJeremy L Thompson     CeedVectorSetValue(U, 0);
38*52bfb9bbSJeremy L Thompson     CeedVectorCreate(ceed, Qdim, &Uq);
39aedaa0e5Sjeremylt 
40*52bfb9bbSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, Q, CEED_GAUSS_LOBATTO, &bxl);
41*52bfb9bbSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, Q, Q, CEED_GAUSS_LOBATTO, &bul);
42aedaa0e5Sjeremylt 
43*52bfb9bbSJeremy L Thompson     CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
44a8de75f0Sjeremylt 
45*52bfb9bbSJeremy L Thompson     CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
46*52bfb9bbSJeremy L Thompson     for (CeedInt i=0; i<Qdim; i++) {
47*52bfb9bbSJeremy L Thompson       CeedScalar xx[dim];
48*52bfb9bbSJeremy L Thompson       for (CeedInt d=0; d<dim; d++)
49*52bfb9bbSJeremy L Thompson         xx[d] = xq[d*Qdim + i];
50*52bfb9bbSJeremy L Thompson       uq[i] = Eval(dim, xx);
51*52bfb9bbSJeremy L Thompson     }
52*52bfb9bbSJeremy L Thompson     CeedVectorRestoreArrayRead(Xq, &xq);
53*52bfb9bbSJeremy L Thompson     CeedVectorSetArray(Uq, CEED_MEM_HOST, CEED_USE_POINTER, uq);
54*52bfb9bbSJeremy L Thompson 
55*52bfb9bbSJeremy L Thompson     // This operation is the identity because the quadrature is collocated
56*52bfb9bbSJeremy L Thompson     CeedBasisApply(bul, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, Uq, U);
57*52bfb9bbSJeremy L Thompson 
58*52bfb9bbSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, Q, CEED_GAUSS, &bxg);
59*52bfb9bbSJeremy L Thompson     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, Q, Q, CEED_GAUSS, &bug);
60*52bfb9bbSJeremy L Thompson 
61*52bfb9bbSJeremy L Thompson     CeedBasisApply(bxg, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, Xq);
62*52bfb9bbSJeremy L Thompson     CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, U, Uq);
63*52bfb9bbSJeremy L Thompson 
64*52bfb9bbSJeremy L Thompson     CeedVectorGetArrayRead(Xq, CEED_MEM_HOST, &xq);
65*52bfb9bbSJeremy L Thompson     CeedVectorGetArrayRead(Uq, CEED_MEM_HOST, &u);
66*52bfb9bbSJeremy L Thompson     for (CeedInt i=0; i<Qdim; i++) {
67*52bfb9bbSJeremy L Thompson       CeedScalar xx[dim];
68*52bfb9bbSJeremy L Thompson       for (CeedInt d=0; d<dim; d++)
69*52bfb9bbSJeremy L Thompson         xx[d] = xq[d*Qdim + i];
70*52bfb9bbSJeremy L Thompson       CeedScalar fx = Eval(dim, xx);
71*52bfb9bbSJeremy L Thompson       if (fabs(u[i] - fx) > 1e-4) {
72a2546046Sjeremylt         // LCOV_EXCL_START
73*52bfb9bbSJeremy L Thompson         printf("[%d] %f != %f=f(%f", dim, u[i], fx, xx[0]);
74*52bfb9bbSJeremy L Thompson         for (CeedInt d=1; d<dim; d++) printf(",%f", xx[d]);
75*52bfb9bbSJeremy L Thompson         puts(")");
76de996c55Sjeremylt         // LCOV_EXCL_STOP
77a8de75f0Sjeremylt       }
78*52bfb9bbSJeremy L Thompson     }
79*52bfb9bbSJeremy L Thompson     CeedVectorRestoreArrayRead(Xq, &xq);
80*52bfb9bbSJeremy L Thompson     CeedVectorRestoreArrayRead(Uq, &u);
81a8de75f0Sjeremylt 
82*52bfb9bbSJeremy L Thompson     CeedVectorDestroy(&X);
83*52bfb9bbSJeremy L Thompson     CeedVectorDestroy(&Xq);
84*52bfb9bbSJeremy L Thompson     CeedVectorDestroy(&U);
85*52bfb9bbSJeremy L Thompson     CeedVectorDestroy(&Uq);
86*52bfb9bbSJeremy L Thompson     CeedBasisDestroy(&bxl);
87*52bfb9bbSJeremy L Thompson     CeedBasisDestroy(&bul);
88*52bfb9bbSJeremy L Thompson     CeedBasisDestroy(&bxg);
89*52bfb9bbSJeremy L Thompson     CeedBasisDestroy(&bug);
90*52bfb9bbSJeremy L Thompson   }
91a8de75f0Sjeremylt   CeedDestroy(&ceed);
92a8de75f0Sjeremylt   return 0;
93a8de75f0Sjeremylt }
94