xref: /libCEED/tests/t304-basis.c (revision f82d2baa7ca109f28fd503bf3902b2ceeba90030)
1 /// @file
2 /// Test grad in multiple dimensions
3 /// \test Test grad in multiple dimensions
4 #include <ceed.h>
5 #include <math.h>
6 
7 static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
8   CeedScalar result = tanh(x[0] + 0.1);
9   if (dim > 1) result += atan(x[1] + 0.2);
10   if (dim > 2) result += exp(-(x[2] + 0.3)*(x[2] + 0.3));
11   return result;
12 }
13 
14 int main(int argc, char **argv) {
15   Ceed ceed;
16 
17   CeedInit(argv[1], &ceed);
18   for (CeedInt dim=1; dim<=3; dim++) {
19     CeedBasis bxl, bug;
20     CeedInt P = 8, Q = 10, Pdim = CeedIntPow(P, dim), Qdim = CeedIntPow(Q, dim),
21             Xdim = CeedIntPow(2, dim);
22     CeedScalar x[Xdim*dim], ones[dim*Qdim], gtposeones[Pdim];
23     CeedScalar xq[Pdim*dim], uq[dim*Qdim], u[Pdim], sum1 = 0, sum2 = 0;
24 
25     for (CeedInt i=0; i<dim*Qdim; i++) ones[i] = 1;
26     for (CeedInt d=0; d<dim; d++) {
27       for (CeedInt i=0; i<Xdim; i++) {
28         x[d*Xdim + i] = (i % CeedIntPow(2, dim-d)) / CeedIntPow(2, dim-d-1) ? 1 : -1;
29       }
30     }
31 
32     // Get function values at quadrature points
33     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, P, CEED_GAUSS_LOBATTO, &bxl);
34     CeedBasisApply(bxl, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, xq);
35     for (CeedInt i=0; i<Pdim; i++) {
36       CeedScalar xx[dim];
37       for (CeedInt d=0; d<dim; d++) xx[d] = xq[d*Pdim + i];
38       u[i] = Eval(dim, xx);
39     }
40 
41     // Calculate G u at quadrature points, G' * 1 at dofs
42     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, P, Q, CEED_GAUSS, &bug);
43     CeedBasisApply(bug, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, u, uq);
44     CeedBasisApply(bug, 1, CEED_TRANSPOSE, CEED_EVAL_GRAD, ones, gtposeones);
45 
46     // Check if 1' * G * u = u' * (G' * 1)
47     for (CeedInt i=0; i<Pdim; i++) {
48       sum1 += gtposeones[i]*u[i];
49     }
50     for (CeedInt i=0; i<dim*Qdim; i++) {
51       sum2 += uq[i];
52     }
53     if (fabs(sum1 - sum2) > 1e-10) {
54       printf("[%d] %f != %f\n", dim, sum1, sum2);
55     }
56 
57     CeedBasisDestroy(&bxl);
58     CeedBasisDestroy(&bug);
59   }
60   CeedDestroy(&ceed);
61   return 0;
62 }
63