xref: /libCEED/tests/t321-basis.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
152bfb9bbSJeremy L Thompson /// @file
252bfb9bbSJeremy L Thompson /// Test interpolation with a 2D Simplex non-tensor H1 basis
352bfb9bbSJeremy L Thompson /// \test Test interpolaton with a 2D Simplex non-tensor H1 basis
452bfb9bbSJeremy L Thompson #include <ceed.h>
552bfb9bbSJeremy L Thompson #include <math.h>
652bfb9bbSJeremy L Thompson #include "t320-basis.h"
752bfb9bbSJeremy L Thompson 
852bfb9bbSJeremy L Thompson double feval(double x1, double x2) {
952bfb9bbSJeremy L Thompson   return x1*x1 + x2*x2 + x1*x2 + 1;
1052bfb9bbSJeremy L Thompson }
1152bfb9bbSJeremy L Thompson 
1252bfb9bbSJeremy L Thompson int main(int argc, char **argv) {
1352bfb9bbSJeremy L Thompson   Ceed ceed;
1452bfb9bbSJeremy L Thompson   CeedVector In, Out;
1552bfb9bbSJeremy L Thompson   const CeedInt P = 6, Q = 4, dim = 2;
1652bfb9bbSJeremy L Thompson   CeedBasis b;
17*d1d35e2fSjeremylt   CeedScalar q_ref[dim*Q], q_weight[Q];
1852bfb9bbSJeremy L Thompson   CeedScalar interp[P*Q], grad[dim*P*Q];
1952bfb9bbSJeremy L Thompson   CeedScalar xq[] = {0.2, 0.6, 1./3., 0.2, 0.2, 0.2, 1./3., 0.6};
2052bfb9bbSJeremy L Thompson   CeedScalar xr[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
2152bfb9bbSJeremy L Thompson   const CeedScalar *out;
2252bfb9bbSJeremy L Thompson   CeedScalar in[P], value;
2352bfb9bbSJeremy L Thompson 
24*d1d35e2fSjeremylt   buildmats(q_ref, q_weight, interp, grad);
2552bfb9bbSJeremy L Thompson 
2652bfb9bbSJeremy L Thompson   CeedInit(argv[1], &ceed);
2752bfb9bbSJeremy L Thompson 
28*d1d35e2fSjeremylt   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, q_ref,
29*d1d35e2fSjeremylt                     q_weight, &b);
3052bfb9bbSJeremy L Thompson 
3152bfb9bbSJeremy L Thompson   // Interpolate function to quadrature points
3252bfb9bbSJeremy L Thompson   for (int i=0; i<P; i++)
3352bfb9bbSJeremy L Thompson     in[i] = feval(xr[0*P+i], xr[1*P+i]);
3452bfb9bbSJeremy L Thompson 
3552bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, P, &In);
3652bfb9bbSJeremy L Thompson   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
3752bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Out);
3852bfb9bbSJeremy L Thompson   CeedVectorSetValue(Out, 0);
3952bfb9bbSJeremy L Thompson 
4052bfb9bbSJeremy L Thompson   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, In, Out);
4152bfb9bbSJeremy L Thompson 
4252bfb9bbSJeremy L Thompson   // Check values at quadrature points
4352bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
4452bfb9bbSJeremy L Thompson   for (int i=0; i<Q; i++) {
4552bfb9bbSJeremy L Thompson     value = feval(xq[0*Q+i], xq[1*Q+i]);
46fc822deaSvaleriabarra     if (fabs(out[i] - value) > 1E-10)
4752bfb9bbSJeremy L Thompson       // LCOV_EXCL_START
4852bfb9bbSJeremy L Thompson       printf("[%d] %f != %f\n", i, out[i], value);
4952bfb9bbSJeremy L Thompson     // LCOV_EXCL_STOP
5052bfb9bbSJeremy L Thompson   }
5152bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Out, &out);
5252bfb9bbSJeremy L Thompson 
5352bfb9bbSJeremy L Thompson   CeedVectorDestroy(&In);
5452bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Out);
5552bfb9bbSJeremy L Thompson   CeedBasisDestroy(&b);
5652bfb9bbSJeremy L Thompson   CeedDestroy(&ceed);
5752bfb9bbSJeremy L Thompson   return 0;
5852bfb9bbSJeremy L Thompson }
59