xref: /libCEED/tests/t332-basis.c (revision b0d170e7bc2e5c930ee481a47eb73044935a48a4) !
1 /// @file
2 /// Test GetDiv and BasisApply for a 2D Quad non-tensor H(div) basis
3 /// \test Test GetDiv 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 componenet
13   CeedInt           P        = dim * num_nodes;  // dof per element!
14   CeedBasis         b;
15   CeedScalar        q_ref[dim * num_qpts], q_weights[num_qpts];
16   CeedScalar        div[P * num_qpts], interp[P * dim * num_qpts];
17   CeedVector        X, Y;
18   const CeedScalar *y, *x;
19 
20   CeedInit(argv[1], &ceed);
21 
22   HdivBasisQuad(Q, q_ref, q_weights, interp, div, CEED_GAUSS);
23   CeedBasisCreateHdiv(ceed, CEED_TOPOLOGY_QUAD, num_comp, P, num_qpts, interp, div, q_ref, q_weights, &b);
24   // Test GetDiv
25   const CeedScalar *div2;
26   CeedBasisGetDiv(b, &div2);
27   for (CeedInt i = 0; i < P * num_qpts; i++) {
28     if (fabs(div[i] - div2[i]) > 100. * CEED_EPSILON) printf("%f != %f\n", div[i], div2[i]);
29   }
30   CeedVectorCreate(ceed, P, &X);
31   CeedVectorSetValue(X, 1);
32   CeedVectorCreate(ceed, num_qpts, &Y);
33   CeedVectorSetValue(Y, 0);
34   // BasisApply for H(div): CEED_EVAL_DIV, NOTRANSPOSE case
35   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_DIV, X, Y);
36 
37   CeedVectorGetArrayRead(Y, CEED_MEM_HOST, &y);
38   for (CeedInt i = 0; i < num_qpts; i++) {
39     if (fabs(P * 0.25 - y[i]) > 100. * CEED_EPSILON) printf("%f != %f\n", 2.0, y[i]);
40   }
41   CeedVectorRestoreArrayRead(Y, &y);
42 
43   CeedVectorSetValue(Y, 1.0);
44   CeedVectorSetValue(X, 0.0);
45   // BasisApply for H(div): CEED_EVAL_DIV, TRANSPOSE case
46   CeedBasisApply(b, 1, CEED_TRANSPOSE, CEED_EVAL_DIV, Y, X);
47 
48   CeedVectorGetArrayRead(X, CEED_MEM_HOST, &x);
49   for (CeedInt i = 0; i < P; i++) {
50     if (fabs(num_qpts * 0.25 - x[i]) > 100. * CEED_EPSILON) printf("%f != %f\n", 2.0, x[i]);
51   }
52   CeedVectorRestoreArrayRead(X, &x);
53 
54   CeedBasisDestroy(&b);
55   CeedVectorDestroy(&X);
56   CeedVectorDestroy(&Y);
57   CeedDestroy(&ceed);
58   return 0;
59 }
60