xref: /libCEED/tests/t321-basis.c (revision ca567da4bd07bc8e9c3c34f09a40e154feb42909)
1 /// @file
2 /// Test interpolation with a 2D Simplex non-tensor H1 basis
3 /// \test Test interpolaton with a 2D Simplex non-tensor H1 basis
4 #include <ceed.h>
5 #include <math.h>
6 
7 #include "t320-basis.h"
8 
9 // polynomial eval helper
10 static CeedScalar Eval(CeedScalar x1, CeedScalar x2) { return x1 * x1 + x2 * x2 + x1 * x2 + 1; }
11 
12 // main test
13 int main(int argc, char **argv) {
14   Ceed          ceed;
15   CeedVector    u, v;
16   const CeedInt p = 6, q = 4, dim = 2;
17   CeedBasis     basis;
18   CeedScalar    q_ref[dim * q], q_weight[q];
19   CeedScalar    interp[p * q], grad[dim * p * q];
20   CeedScalar    x_q[]   = {0.2, 0.6, 1. / 3., 0.2, 0.2, 0.2, 1. / 3., 0.6};
21   CeedScalar    x_ref[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
22 
23   CeedInit(argv[1], &ceed);
24 
25   CeedVectorCreate(ceed, p, &u);
26   {
27     CeedScalar u_array[p];
28 
29     // Interpolate function to quadrature points
30     for (int i = 0; i < p; i++) u_array[i] = Eval(x_ref[0 * p + i], x_ref[1 * p + i]);
31     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
32   }
33   CeedVectorCreate(ceed, q, &v);
34   CeedVectorSetValue(v, 0);
35 
36   Build2DSimplex(q_ref, q_weight, interp, grad);
37   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis);
38 
39   CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, v);
40 
41   // Check values at quadrature points
42   {
43     const CeedScalar *v_array;
44 
45     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
46     for (int i = 0; i < q; i++) {
47       CeedScalar fx = Eval(x_q[0 * q + i], x_q[1 * q + i]);
48       if (fabs(v_array[i] - fx) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] %f != %f\n", i, v_array[i], fx);
49     }
50     CeedVectorRestoreArrayRead(v, &v_array);
51   }
52 
53   CeedVectorDestroy(&u);
54   CeedVectorDestroy(&v);
55   CeedBasisDestroy(&basis);
56   CeedDestroy(&ceed);
57   return 0;
58 }
59