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