xref: /libCEED/tests/t316-basis.c (revision bf4cb66493dbcc06b8d25c9c91cf89fe1cab7c9b)
1 /// @file
2 /// Test underintegrated grad in multiple dimensions
3 /// \test Test underintegraded 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 static CeedScalar GetTolerance(CeedScalarType scalar_type, int dim) {
15   CeedScalar tol;
16   if (scalar_type == CEED_SCALAR_FP32) {
17     if (dim == 3)
18       tol = 0.005;
19     else
20       tol = 1.e-4;
21   } else {
22     tol = 1.e-11;
23   }
24   return tol;
25 }
26 
27 int main(int argc, char **argv) {
28   Ceed ceed;
29 
30   CeedInit(argv[1], &ceed);
31 
32   for (CeedInt dim=1; dim<=3; dim++) {
33     CeedVector X, X_q, U, U_q, ones, grad_T_ones;
34     CeedBasis basis_x_lobatto, basis_u_gauss;
35     CeedInt P = 8, Q = 7, P_dim = CeedIntPow(P, dim), Q_dim = CeedIntPow(Q, dim),
36             X_dim = CeedIntPow(2, dim);
37     CeedScalar x[X_dim*dim], u[P_dim];
38     const CeedScalar *x_q, *u_q, *grad_t_ones_array;
39     CeedScalar sum_1 = 0, sum_2 = 0;
40 
41     for (CeedInt d=0; d<dim; d++)
42       for (CeedInt i=0; i<X_dim; i++)
43         x[d*X_dim + i] = (i % CeedIntPow(2, dim-d)) /
44                          CeedIntPow(2, dim-d-1) ? 1 : -1;
45 
46     CeedVectorCreate(ceed, X_dim*dim, &X);
47     CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
48     CeedVectorCreate(ceed, P_dim*dim, &X_q);
49     CeedVectorSetValue(X_q, 0);
50     CeedVectorCreate(ceed, P_dim, &U);
51     CeedVectorCreate(ceed, Q_dim*dim, &U_q);
52     CeedVectorSetValue(U_q, 0);
53     CeedVectorCreate(ceed, Q_dim*dim, &ones);
54     CeedVectorSetValue(ones, 1);
55     CeedVectorCreate(ceed, P_dim, &grad_T_ones);
56     CeedVectorSetValue(grad_T_ones, 0);
57 
58     // Get function values at quadrature points
59     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, P,
60                                     CEED_GAUSS_LOBATTO, &basis_x_lobatto);
61     CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, X, X_q);
62 
63     CeedVectorGetArrayRead(X_q, CEED_MEM_HOST, &x_q);
64     for (CeedInt i=0; i<P_dim; i++) {
65       CeedScalar xx[dim];
66       for (CeedInt d=0; d<dim; d++)
67         xx[d] = x_q[d*P_dim + i];
68       u[i] = Eval(dim, xx);
69     }
70     CeedVectorRestoreArrayRead(X_q, &x_q);
71     CeedVectorSetArray(U, CEED_MEM_HOST, CEED_USE_POINTER, u);
72 
73     // Calculate G u at quadrature points, G' * 1 at dofs
74     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, P, Q, CEED_GAUSS, &basis_u_gauss);
75     CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, U, U_q);
76     CeedBasisApply(basis_u_gauss, 1, CEED_TRANSPOSE, CEED_EVAL_GRAD, ones,
77                    grad_T_ones);
78 
79     // Check if 1' * G * u = u' * (G' * 1)
80     CeedVectorGetArrayRead(grad_T_ones, CEED_MEM_HOST, &grad_t_ones_array);
81     CeedVectorGetArrayRead(U_q, CEED_MEM_HOST, &u_q);
82     for (CeedInt i=0; i<P_dim; i++)
83       sum_1 += grad_t_ones_array[i]*u[i];
84     for (CeedInt i=0; i<dim*Q_dim; i++)
85       sum_2 += u_q[i];
86     CeedVectorRestoreArrayRead(grad_T_ones, &grad_t_ones_array);
87     CeedVectorRestoreArrayRead(U_q, &u_q);
88     CeedScalar tol = GetTolerance(CEED_SCALAR_TYPE, dim);
89     if (fabs(sum_1 - sum_2) > tol)
90       // LCOV_EXCL_START
91       printf("[%d] %f != %f\n", dim, sum_1, sum_2);
92     // LCOV_EXCL_STOP
93 
94     CeedVectorDestroy(&X);
95     CeedVectorDestroy(&X_q);
96     CeedVectorDestroy(&U);
97     CeedVectorDestroy(&U_q);
98     CeedVectorDestroy(&ones);
99     CeedVectorDestroy(&grad_T_ones);
100     CeedBasisDestroy(&basis_x_lobatto);
101     CeedBasisDestroy(&basis_u_gauss);
102   }
103   CeedDestroy(&ceed);
104   return 0;
105 }
106