xref: /libCEED/tests/t323-basis.c (revision c9c2c07970382857cc7b4a28d359710237b91a3e)
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 #include "t320-basis.h"
7 
8 CeedScalar feval(CeedScalar x1, CeedScalar x2) {
9   return x1*x1 + x2*x2 + x1*x2 + 1;
10 }
11 
12 CeedScalar dfeval(CeedScalar x1, CeedScalar x2) {
13   return 2*x1 + x2;
14 }
15 
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,
33                     q_weight, &b);
34 
35   // Interpolate function to quadrature points
36   for (int i=0; i<P; i++)
37     in[i] = feval(xr[0*P+i], xr[1*P+i]);
38 
39   CeedVectorCreate(ceed, P, &In);
40   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
41   CeedVectorCreate(ceed, Q*dim, &Out);
42   CeedVectorSetValue(Out, 0);
43 
44   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, In, Out);
45 
46   // Check values at quadrature points
47   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
48   for (int i=0; i<Q; i++) {
49     value = dfeval(xq[0*Q+i], xq[1*Q+i]);
50     if (fabs(out[0*Q+i] - value) > 100.*CEED_EPSILON)
51       // LCOV_EXCL_START
52       printf("[%" CeedInt_FMT "] %f != %f\n", i, out[0*Q+i], value);
53     // LCOV_EXCL_STOP
54     value = dfeval(xq[1*Q+i], xq[0*Q+i]);
55     if (fabs(out[1*Q+i] - value) > 100.*CEED_EPSILON)
56       // LCOV_EXCL_START
57       printf("[%" CeedInt_FMT "] %f != %f\n", i, out[1*Q+i], value);
58     // LCOV_EXCL_STOP
59   }
60   CeedVectorRestoreArrayRead(Out, &out);
61 
62   CeedVectorDestroy(&In);
63   CeedVectorDestroy(&Out);
64   CeedBasisDestroy(&b);
65   CeedDestroy(&ceed);
66   return 0;
67 }
68