xref: /libCEED/tests/t322-basis.c (revision 3f21f6b10abeb5d85d3454ea5cd38498737dc88a)
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 "t320-basis.h"
7 
8 CeedScalar feval(CeedScalar x1, CeedScalar 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 q_ref[dim*Q], q_weight[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(q_ref, q_weight, interp, grad);
24 
25   CeedInit(argv[1], &ceed);
26 
27   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, q_ref,
28                     q_weight, &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, 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,
43                  CEED_VECTOR_NONE, Weights);
44 
45   // Check values at quadrature points
46   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
47   CeedVectorGetArrayRead(Weights, CEED_MEM_HOST, &weights);
48   sum = 0;
49   for (int i=0; i<Q; i++)
50     sum += out[i]*weights[i];
51   if (fabs(sum - 17./24.) > 100.*CEED_EPSILON)
52     // LCOV_EXCL_START
53     printf("%f != %f\n", sum, 17./24.);
54   // LCOV_EXCL_STOP
55   CeedVectorRestoreArrayRead(Out, &out);
56   CeedVectorRestoreArrayRead(Weights, &weights);
57 
58   CeedVectorDestroy(&In);
59   CeedVectorDestroy(&Out);
60   CeedVectorDestroy(&Weights);
61   CeedBasisDestroy(&b);
62   CeedDestroy(&ceed);
63   return 0;
64 }
65