xref: /libCEED/tests/t331-basis.c (revision a2e5d304d0c7d96eecfcbbd32f1ea5194beb84ca)
1 /// @file
2 /// Test GetInterp and BasisApply for a 2D Quad non-tensor H(div) basis
3 /// \test Test GetInterp and BasisApply for a 2D Quad non-tensor H(div) basis
4 #include <ceed.h>
5 #include <math.h>
6 
7 #include "t330-basis.h"
8 
9 int main(int argc, char **argv) {
10   Ceed          ceed;
11   const CeedInt num_nodes = 4, q = 3, dim = 2, num_qpts = q * q;
12   CeedInt       num_comp = 1;                // one vector component
13   CeedInt       p        = dim * num_nodes;  // DoF per element
14   CeedBasis     basis;
15   CeedScalar    q_ref[dim * num_qpts], q_weights[num_qpts];
16   CeedScalar    div[p * num_qpts], interp[p * dim * num_qpts];
17   CeedVector    u, v;
18 
19   CeedInit(argv[1], &ceed);
20 
21   BuildHdivQuadrilateral(q, q_ref, q_weights, interp, div, CEED_GAUSS);
22   CeedBasisCreateHdiv(ceed, CEED_TOPOLOGY_QUAD, num_comp, p, num_qpts, interp, div, q_ref, q_weights, &basis);
23 
24   // Test GetInterp for H(div)
25   {
26     const CeedScalar *interp_2;
27     CeedBasisGetInterp(basis, &interp_2);
28     for (CeedInt i = 0; i < p * dim * num_qpts; i++) {
29       if (fabs(interp[i] - interp_2[i]) > 100. * CEED_EPSILON) {
30         // LCOV_EXCL_START
31         printf("%f != %f\n", interp[i], interp_2[i]);
32         // LCOV_EXCL_STOP
33       }
34     }
35   }
36 
37   CeedVectorCreate(ceed, p, &u);
38   CeedVectorSetValue(u, 1.0);
39   CeedVectorCreate(ceed, num_qpts * dim, &v);
40   CeedVectorSetValue(v, 0.);
41 
42   // BasisApply for H(div): CEED_EVAL_INTERP, NOTRANSPOSE case
43   CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, v);
44 
45   {
46     const CeedScalar *v_array;
47 
48     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
49     for (CeedInt i = 0; i < dim * num_qpts; i++) {
50       if (fabs(q_ref[i] - v_array[i]) > 100. * CEED_EPSILON) {
51         // LCOV_EXCL_START
52         printf("%f != %f\n", q_ref[i], v_array[i]);
53         // LCOV_EXCL_STOP
54       }
55     }
56     CeedVectorRestoreArrayRead(v, &v_array);
57   }
58 
59   CeedVectorSetValue(v, 1.0);
60   CeedVectorSetValue(u, 0.0);
61 
62   // BasisApply for Hdiv: CEED_EVAL_INTERP, TRANSPOSE case
63   CeedBasisApply(basis, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, v, u);
64 
65   {
66     const CeedScalar *u_array;
67 
68     CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array);
69     CeedScalar sum = 0.;
70     for (CeedInt i = 0; i < p; i++) {
71       sum += u_array[i];
72     }
73     if (fabs(sum) > 100. * CEED_EPSILON) printf("sum of array %f != %f\n", sum, 0.0);
74     CeedVectorRestoreArrayRead(u, &u_array);
75   }
76 
77   CeedBasisDestroy(&basis);
78   CeedVectorDestroy(&u);
79   CeedVectorDestroy(&v);
80   CeedDestroy(&ceed);
81   return 0;
82 }
83