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