xref: /libCEED/tests/t322-basis.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
152bfb9bbSJeremy L Thompson /// @file
252bfb9bbSJeremy L Thompson /// Test integration with a 2D Simplex non-tensor H1 basis
352bfb9bbSJeremy L Thompson /// \test Test integration 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, Weights;
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 xr[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
2052bfb9bbSJeremy L Thompson   const CeedScalar *out, *weights;
2152bfb9bbSJeremy L Thompson   CeedScalar in[P], sum;
2252bfb9bbSJeremy L Thompson 
23*d1d35e2fSjeremylt   buildmats(q_ref, q_weight, interp, grad);
2452bfb9bbSJeremy L Thompson 
2552bfb9bbSJeremy L Thompson   CeedInit(argv[1], &ceed);
2652bfb9bbSJeremy L Thompson 
27*d1d35e2fSjeremylt   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, q_ref,
28*d1d35e2fSjeremylt                     q_weight, &b);
2952bfb9bbSJeremy L Thompson 
3052bfb9bbSJeremy L Thompson   // Interpolate function to quadrature points
3152bfb9bbSJeremy L Thompson   for (int i=0; i<P; i++)
3252bfb9bbSJeremy L Thompson     in[i] = feval(xr[0*P+i], xr[1*P+i]);
3352bfb9bbSJeremy L Thompson 
3452bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, P, &In);
3552bfb9bbSJeremy L Thompson   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
3652bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Out);
3752bfb9bbSJeremy L Thompson   CeedVectorSetValue(Out, 0);
3852bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Weights);
3952bfb9bbSJeremy L Thompson   CeedVectorSetValue(Weights, 0);
4052bfb9bbSJeremy L Thompson 
4152bfb9bbSJeremy L Thompson   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, In, Out);
42a7b7f929Sjeremylt   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT,
43a7b7f929Sjeremylt                  CEED_VECTOR_NONE, Weights);
4452bfb9bbSJeremy L Thompson 
4552bfb9bbSJeremy L Thompson   // Check values at quadrature points
4652bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
4752bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Weights, CEED_MEM_HOST, &weights);
4852bfb9bbSJeremy L Thompson   sum = 0;
4952bfb9bbSJeremy L Thompson   for (int i=0; i<Q; i++)
5052bfb9bbSJeremy L Thompson     sum += out[i]*weights[i];
5195c11be2Svaleriabarra   if (fabs(sum - 17./24.) > 1E-10)
52d99fa3c5SJeremy L Thompson     // LCOV_EXCL_START
5352bfb9bbSJeremy L Thompson     printf("%f != %f\n", sum, 17./24.);
54d99fa3c5SJeremy L Thompson   // LCOV_EXCL_STOP
5552bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Out, &out);
5652bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Weights, &weights);
5752bfb9bbSJeremy L Thompson 
5852bfb9bbSJeremy L Thompson   CeedVectorDestroy(&In);
5952bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Out);
6052bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Weights);
6152bfb9bbSJeremy L Thompson   CeedBasisDestroy(&b);
6252bfb9bbSJeremy L Thompson   CeedDestroy(&ceed);
6352bfb9bbSJeremy L Thompson   return 0;
6452bfb9bbSJeremy L Thompson }
65