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