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