xref: /libCEED/tests/t313-basis.c (revision ca567da4bd07bc8e9c3c34f09a40e154feb42909)
1 /// @file
2 /// Test interpolation in multiple dimensions
3 /// \test Test interpolation in multiple dimensions
4 #include <ceed.h>
5 #include <math.h>
6 
7 static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
8   CeedScalar result = 1, center = 0.1;
9   for (CeedInt d = 0; d < dim; d++) {
10     result *= tanh(x[d] - center);
11     center += 0.1;
12   }
13   return result;
14 }
15 
16 int main(int argc, char **argv) {
17   Ceed ceed;
18 
19   CeedInit(argv[1], &ceed);
20   for (CeedInt dim = 1; dim <= 3; dim++) {
21     CeedVector x, x_q, u, u_q;
22     CeedBasis  basis_x_lobatto, basis_u_lobatto, basis_x_gauss, basis_u_gauss;
23     CeedInt    q = 10, q_dim = CeedIntPow(q, dim), x_dim = CeedIntPow(2, dim);
24 
25     CeedVectorCreate(ceed, x_dim * dim, &x);
26     {
27       CeedScalar x_array[x_dim * dim];
28 
29       for (CeedInt d = 0; d < dim; d++) {
30         for (CeedInt i = 0; i < x_dim; i++) x_array[d * x_dim + i] = (i % CeedIntPow(2, dim - d)) / CeedIntPow(2, dim - d - 1) ? 1 : -1;
31       }
32       CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
33     }
34     CeedVectorCreate(ceed, q_dim * dim, &x_q);
35     CeedVectorSetValue(x_q, 0);
36     CeedVectorCreate(ceed, q_dim, &u);
37     CeedVectorSetValue(u, 0);
38     CeedVectorCreate(ceed, q_dim, &u_q);
39 
40     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS_LOBATTO, &basis_x_lobatto);
41     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, q, q, CEED_GAUSS_LOBATTO, &basis_u_lobatto);
42 
43     CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
44 
45     {
46       const CeedScalar *x_q_array;
47       CeedScalar        u_q_array[q_dim];
48 
49       CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
50       for (CeedInt i = 0; i < q_dim; i++) {
51         CeedScalar coord[dim];
52         for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * q_dim + i];
53         u_q_array[i] = Eval(dim, coord);
54       }
55       CeedVectorRestoreArrayRead(x_q, &x_q_array);
56       CeedVectorSetArray(u_q, CEED_MEM_HOST, CEED_COPY_VALUES, u_q_array);
57     }
58 
59     // This operation is the identity because the quadrature is collocated
60     CeedBasisApply(basis_u_lobatto, 1, CEED_TRANSPOSE, CEED_EVAL_INTERP, u_q, u);
61 
62     CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x_gauss);
63     CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, q, q, CEED_GAUSS, &basis_u_gauss);
64 
65     CeedBasisApply(basis_x_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
66     CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, u, u_q);
67 
68     {
69       const CeedScalar *x_q_array, *u_array;
70 
71       CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
72       CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_array);
73       for (CeedInt i = 0; i < q_dim; i++) {
74         CeedScalar coord[dim];
75         for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * q_dim + i];
76         CeedScalar fx = Eval(dim, coord);
77         if (fabs(u_array[i] - fx) > 1E-4) {
78           // LCOV_EXCL_START
79           printf("[%" CeedInt_FMT "] %f != %f=f(%f", dim, u_array[i], fx, coord[0]);
80           for (CeedInt d = 1; d < dim; d++) printf(",%f", coord[d]);
81           puts(")");
82           // LCOV_EXCL_STOP
83         }
84       }
85       CeedVectorRestoreArrayRead(x_q, &x_q_array);
86       CeedVectorRestoreArrayRead(u_q, &u_array);
87     }
88 
89     CeedVectorDestroy(&x);
90     CeedVectorDestroy(&x_q);
91     CeedVectorDestroy(&u);
92     CeedVectorDestroy(&u_q);
93     CeedBasisDestroy(&basis_x_lobatto);
94     CeedBasisDestroy(&basis_u_lobatto);
95     CeedBasisDestroy(&basis_x_gauss);
96     CeedBasisDestroy(&basis_u_gauss);
97   }
98   CeedDestroy(&ceed);
99   return 0;
100 }
101