xref: /libCEED/tests/t323-basis.c (revision b0d170e7bc2e5c930ee481a47eb73044935a48a4)
1 /// @file
2 /// Test grad with a 2D Simplex non-tensor H1 basis
3 /// \test Test grad with a 2D Simplex non-tensor H1 basis
4 #include <ceed.h>
5 #include <math.h>
6 
7 #include "t320-basis.h"
8 
9 // polynomial eval helper
10 static CeedScalar feval(CeedScalar x1, CeedScalar x2) { return x1 * x1 + x2 * x2 + x1 * x2 + 1; }
11 
12 // polynomial derivative helper
13 CeedScalar dfeval(CeedScalar x1, CeedScalar x2) { return 2 * x1 + x2; }
14 
15 // main test
16 int main(int argc, char **argv) {
17   Ceed              ceed;
18   CeedVector        In, Out;
19   const CeedInt     P = 6, Q = 4, dim = 2;
20   CeedBasis         b;
21   CeedScalar        q_ref[dim * Q], q_weight[Q];
22   CeedScalar        interp[P * Q], grad[dim * P * Q];
23   CeedScalar        xq[] = {0.2, 0.6, 1. / 3., 0.2, 0.2, 0.2, 1. / 3., 0.6};
24   CeedScalar        xr[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
25   const CeedScalar *out;
26   CeedScalar        in[P], value;
27 
28   buildmats(q_ref, q_weight, interp, grad);
29 
30   CeedInit(argv[1], &ceed);
31 
32   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, P, Q, interp, grad, q_ref, q_weight, &b);
33 
34   // Interpolate function to quadrature points
35   for (int i = 0; i < P; i++) in[i] = feval(xr[0 * P + i], xr[1 * P + i]);
36 
37   CeedVectorCreate(ceed, P, &In);
38   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
39   CeedVectorCreate(ceed, Q * dim, &Out);
40   CeedVectorSetValue(Out, 0);
41 
42   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, In, Out);
43 
44   // Check values at quadrature points
45   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
46   for (int i = 0; i < Q; i++) {
47     value = dfeval(xq[0 * Q + i], xq[1 * Q + i]);
48     if (fabs(out[0 * Q + i] - value) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] %f != %f\n", i, out[0 * Q + i], value);
49     value = dfeval(xq[1 * Q + i], xq[0 * Q + i]);
50     if (fabs(out[1 * Q + i] - value) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] %f != %f\n", i, out[1 * Q + i], value);
51   }
52   CeedVectorRestoreArrayRead(Out, &out);
53 
54   CeedVectorDestroy(&In);
55   CeedVectorDestroy(&Out);
56   CeedBasisDestroy(&b);
57   CeedDestroy(&ceed);
58   return 0;
59 }
60