xref: /libCEED/tests/t322-basis.c (revision caee03026e6576cbf7a399c2fc51bb918c77f451)
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 
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 // main test
13 int main(int argc, char **argv) {
14   Ceed              ceed;
15   CeedVector        In, Out, Weights;
16   const CeedInt     P = 6, Q = 4, dim = 2;
17   CeedBasis         b;
18   CeedScalar        q_ref[dim * Q], q_weight[Q];
19   CeedScalar        interp[P * Q], grad[dim * P * Q];
20   CeedScalar        xr[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
21   const CeedScalar *out, *weights;
22   CeedScalar        in[P], sum;
23 
24   buildmats(q_ref, q_weight, interp, grad);
25 
26   CeedInit(argv[1], &ceed);
27 
28   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, P, Q, interp, grad, q_ref, q_weight, &b);
29 
30   // Interpolate function to quadrature points
31   for (int i = 0; i < P; i++) in[i] = feval(xr[0 * P + i], xr[1 * P + i]);
32 
33   CeedVectorCreate(ceed, P, &In);
34   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
35   CeedVectorCreate(ceed, Q, &Out);
36   CeedVectorSetValue(Out, 0);
37   CeedVectorCreate(ceed, Q, &Weights);
38   CeedVectorSetValue(Weights, 0);
39 
40   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, In, Out);
41   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, Weights);
42 
43   // Check values at quadrature points
44   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
45   CeedVectorGetArrayRead(Weights, CEED_MEM_HOST, &weights);
46   sum = 0;
47   for (int i = 0; i < Q; i++) sum += out[i] * weights[i];
48   if (fabs(sum - 17. / 24.) > 100. * CEED_EPSILON) printf("%f != %f\n", sum, 17. / 24.);
49   CeedVectorRestoreArrayRead(Out, &out);
50   CeedVectorRestoreArrayRead(Weights, &weights);
51 
52   CeedVectorDestroy(&In);
53   CeedVectorDestroy(&Out);
54   CeedVectorDestroy(&Weights);
55   CeedBasisDestroy(&b);
56   CeedDestroy(&ceed);
57   return 0;
58 }
59