xref: /libCEED/tests/t322-basis.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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>
6*2b730f8bSJeremy L Thompson 
752bfb9bbSJeremy L Thompson #include "t320-basis.h"
852bfb9bbSJeremy L Thompson 
9*2b730f8bSJeremy L Thompson // polynomial eval helper
10*2b730f8bSJeremy L Thompson static CeedScalar feval(CeedScalar x1, CeedScalar x2) { return x1 * x1 + x2 * x2 + x1 * x2 + 1; }
1152bfb9bbSJeremy L Thompson 
12*2b730f8bSJeremy L Thompson // main test
1352bfb9bbSJeremy L Thompson int main(int argc, char **argv) {
1452bfb9bbSJeremy L Thompson   Ceed              ceed;
1552bfb9bbSJeremy L Thompson   CeedVector        In, Out, Weights;
1652bfb9bbSJeremy L Thompson   const CeedInt     P = 6, Q = 4, dim = 2;
1752bfb9bbSJeremy L Thompson   CeedBasis         b;
18d1d35e2fSjeremylt   CeedScalar        q_ref[dim * Q], q_weight[Q];
1952bfb9bbSJeremy L Thompson   CeedScalar        interp[P * Q], grad[dim * P * Q];
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, *weights;
2252bfb9bbSJeremy L Thompson   CeedScalar        in[P], sum;
2352bfb9bbSJeremy L Thompson 
24d1d35e2fSjeremylt   buildmats(q_ref, q_weight, interp, grad);
2552bfb9bbSJeremy L Thompson 
2652bfb9bbSJeremy L Thompson   CeedInit(argv[1], &ceed);
2752bfb9bbSJeremy L Thompson 
28*2b730f8bSJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, P, Q, interp, grad, q_ref, q_weight, &b);
2952bfb9bbSJeremy L Thompson 
3052bfb9bbSJeremy L Thompson   // Interpolate function to quadrature points
31*2b730f8bSJeremy L Thompson   for (int i = 0; i < P; i++) in[i] = feval(xr[0 * P + i], xr[1 * P + i]);
3252bfb9bbSJeremy L Thompson 
3352bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, P, &In);
3452bfb9bbSJeremy L Thompson   CeedVectorSetArray(In, CEED_MEM_HOST, CEED_USE_POINTER, in);
3552bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Out);
3652bfb9bbSJeremy L Thompson   CeedVectorSetValue(Out, 0);
3752bfb9bbSJeremy L Thompson   CeedVectorCreate(ceed, Q, &Weights);
3852bfb9bbSJeremy L Thompson   CeedVectorSetValue(Weights, 0);
3952bfb9bbSJeremy L Thompson 
4052bfb9bbSJeremy L Thompson   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, In, Out);
41*2b730f8bSJeremy L Thompson   CeedBasisApply(b, 1, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, Weights);
4252bfb9bbSJeremy L Thompson 
4352bfb9bbSJeremy L Thompson   // Check values at quadrature points
4452bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Out, CEED_MEM_HOST, &out);
4552bfb9bbSJeremy L Thompson   CeedVectorGetArrayRead(Weights, CEED_MEM_HOST, &weights);
4652bfb9bbSJeremy L Thompson   sum = 0;
47*2b730f8bSJeremy L Thompson   for (int i = 0; i < Q; i++) sum += out[i] * weights[i];
48*2b730f8bSJeremy L Thompson   if (fabs(sum - 17. / 24.) > 100. * CEED_EPSILON) printf("%f != %f\n", sum, 17. / 24.);
4952bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Out, &out);
5052bfb9bbSJeremy L Thompson   CeedVectorRestoreArrayRead(Weights, &weights);
5152bfb9bbSJeremy L Thompson 
5252bfb9bbSJeremy L Thompson   CeedVectorDestroy(&In);
5352bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Out);
5452bfb9bbSJeremy L Thompson   CeedVectorDestroy(&Weights);
5552bfb9bbSJeremy L Thompson   CeedBasisDestroy(&b);
5652bfb9bbSJeremy L Thompson   CeedDestroy(&ceed);
5752bfb9bbSJeremy L Thompson   return 0;
5852bfb9bbSJeremy L Thompson }
59