xref: /libCEED/tests/t321-basis.c (revision 52bfb9bbf17f17edbcd45876cdc8689a879bc683)
1*52bfb9bbSJeremy L Thompson /// @file
2*52bfb9bbSJeremy L Thompson /// Test interpolation with a 2D Simplex non-tensor H1 basis
3*52bfb9bbSJeremy L Thompson /// \test Test interpolaton 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;
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 xq[] = {0.2, 0.6, 1./3., 0.2, 0.2, 0.2, 1./3., 0.6};
20*52bfb9bbSJeremy L Thompson   CeedScalar xr[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
21*52bfb9bbSJeremy L Thompson   const CeedScalar *out;
22*52bfb9bbSJeremy L Thompson   CeedScalar in[P], value;
23*52bfb9bbSJeremy L Thompson 
24*52bfb9bbSJeremy L Thompson   buildmats(qref, qweight, interp, grad);
25*52bfb9bbSJeremy L Thompson 
26*52bfb9bbSJeremy L Thompson   CeedInit(argv[1], &ceed);
27*52bfb9bbSJeremy L Thompson 
28*52bfb9bbSJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
29*52bfb9bbSJeremy L Thompson                     qweight, &b);
30*52bfb9bbSJeremy L Thompson 
31*52bfb9bbSJeremy L Thompson   // Interpolate function to quadrature points
32*52bfb9bbSJeremy L Thompson   for (int i=0; i<P; i++)
33*52bfb9bbSJeremy L Thompson     in[i] = feval(xr[0*P+i], xr[1*P+i]);
34*52bfb9bbSJeremy L Thompson 
35*52bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, P, &In);
36*52bfb9bbSJeremy L Thompson   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
37*52bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Out);
38*52bfb9bbSJeremy L Thompson   CeedVectorSetValue(Out, 0);
39*52bfb9bbSJeremy L Thompson 
40*52bfb9bbSJeremy L Thompson   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, In, Out);
41*52bfb9bbSJeremy L Thompson 
42*52bfb9bbSJeremy L Thompson   // Check values at quadrature points
43*52bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
44*52bfb9bbSJeremy L Thompson   for (int i=0; i<Q; i++) {
45*52bfb9bbSJeremy L Thompson     value = feval(xq[0*Q+i], xq[1*Q+i]);
46*52bfb9bbSJeremy L Thompson     if (fabs(out[i] - value) > 1e-10)
47*52bfb9bbSJeremy L Thompson       // LCOV_EXCL_START
48*52bfb9bbSJeremy L Thompson       printf("[%d] %f != %f\n", i, out[i], value);
49*52bfb9bbSJeremy L Thompson     // LCOV_EXCL_STOP
50*52bfb9bbSJeremy L Thompson   }
51*52bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Out, &out);
52*52bfb9bbSJeremy L Thompson 
53*52bfb9bbSJeremy L Thompson   CeedVectorDestroy(&In);
54*52bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Out);
55*52bfb9bbSJeremy L Thompson   CeedBasisDestroy(&b);
56*52bfb9bbSJeremy L Thompson   CeedDestroy(&ceed);
57*52bfb9bbSJeremy L Thompson   return 0;
58*52bfb9bbSJeremy L Thompson }
59