1*db2becc9SJeremy L Thompson /// @file
2*db2becc9SJeremy L Thompson /// Test gradient transpose ApplyAdd in multiple dimensions at arbitrary points
3*db2becc9SJeremy L Thompson /// \test Test gradient transpose ApplyAdd in multiple dimensions at arbitrary points
4*db2becc9SJeremy L Thompson #include <ceed.h>
5*db2becc9SJeremy L Thompson #include <math.h>
6*db2becc9SJeremy L Thompson #include <stdio.h>
7*db2becc9SJeremy L Thompson
Eval(CeedInt dim,const CeedScalar x[])8*db2becc9SJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) {
9*db2becc9SJeremy L Thompson CeedScalar result = tanh(x[0] + 0.1);
10*db2becc9SJeremy L Thompson if (dim > 1) result += atan(x[1] + 0.2);
11*db2becc9SJeremy L Thompson if (dim > 2) result += exp(-(x[2] + 0.3) * (x[2] + 0.3));
12*db2becc9SJeremy L Thompson return result;
13*db2becc9SJeremy L Thompson }
14*db2becc9SJeremy L Thompson
GetTolerance(CeedScalarType scalar_type,int dim)15*db2becc9SJeremy L Thompson static CeedScalar GetTolerance(CeedScalarType scalar_type, int dim) {
16*db2becc9SJeremy L Thompson CeedScalar tol;
17*db2becc9SJeremy L Thompson if (scalar_type == CEED_SCALAR_FP32) {
18*db2becc9SJeremy L Thompson if (dim == 3) tol = 0.005;
19*db2becc9SJeremy L Thompson else tol = 1.e-4;
20*db2becc9SJeremy L Thompson } else {
21*db2becc9SJeremy L Thompson tol = 1.e-11;
22*db2becc9SJeremy L Thompson }
23*db2becc9SJeremy L Thompson return tol;
24*db2becc9SJeremy L Thompson }
25*db2becc9SJeremy L Thompson
main(int argc,char ** argv)26*db2becc9SJeremy L Thompson int main(int argc, char **argv) {
27*db2becc9SJeremy L Thompson Ceed ceed;
28*db2becc9SJeremy L Thompson
29*db2becc9SJeremy L Thompson CeedInit(argv[1], &ceed);
30*db2becc9SJeremy L Thompson
31*db2becc9SJeremy L Thompson for (CeedInt dim = 1; dim <= 3; dim++) {
32*db2becc9SJeremy L Thompson CeedVector x, x_nodes, x_points, u, u_points, v, ones;
33*db2becc9SJeremy L Thompson CeedBasis basis_x, basis_u;
34*db2becc9SJeremy L Thompson const CeedInt p = 9, q = 9, num_points = 4, x_dim = CeedIntPow(2, dim), p_dim = CeedIntPow(p, dim);
35*db2becc9SJeremy L Thompson CeedScalar sum_1 = 0, sum_2 = 0;
36*db2becc9SJeremy L Thompson
37*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, x_dim * dim, &x);
38*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim * dim, &x_nodes);
39*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, num_points * dim, &x_points);
40*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &u);
41*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, num_points * dim, &u_points);
42*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &v);
43*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, num_points * dim, &ones);
44*db2becc9SJeremy L Thompson
45*db2becc9SJeremy L Thompson CeedVectorSetValue(ones, 1);
46*db2becc9SJeremy L Thompson CeedVectorSetValue(v, 0);
47*db2becc9SJeremy L Thompson
48*db2becc9SJeremy L Thompson // Get nodal coordinates
49*db2becc9SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, p, CEED_GAUSS_LOBATTO, &basis_x);
50*db2becc9SJeremy L Thompson {
51*db2becc9SJeremy L Thompson CeedScalar x_array[x_dim * dim];
52*db2becc9SJeremy L Thompson
53*db2becc9SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
54*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < x_dim; i++) x_array[d * x_dim + i] = (i % CeedIntPow(2, d + 1)) / CeedIntPow(2, d) ? 1 : -1;
55*db2becc9SJeremy L Thompson }
56*db2becc9SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
57*db2becc9SJeremy L Thompson }
58*db2becc9SJeremy L Thompson CeedBasisApply(basis_x, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_nodes);
59*db2becc9SJeremy L Thompson
60*db2becc9SJeremy L Thompson // Set values of u at nodes
61*db2becc9SJeremy L Thompson {
62*db2becc9SJeremy L Thompson const CeedScalar *x_array;
63*db2becc9SJeremy L Thompson CeedScalar u_array[p_dim];
64*db2becc9SJeremy L Thompson
65*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(x_nodes, CEED_MEM_HOST, &x_array);
66*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) {
67*db2becc9SJeremy L Thompson CeedScalar coord[dim];
68*db2becc9SJeremy L Thompson
69*db2becc9SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) coord[d] = x_array[d * p_dim + i];
70*db2becc9SJeremy L Thompson u_array[i] = Eval(dim, coord);
71*db2becc9SJeremy L Thompson }
72*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(x_nodes, &x_array);
73*db2becc9SJeremy L Thompson CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)&u_array);
74*db2becc9SJeremy L Thompson }
75*db2becc9SJeremy L Thompson
76*db2becc9SJeremy L Thompson // Interpolate to arbitrary points
77*db2becc9SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
78*db2becc9SJeremy L Thompson {
79*db2becc9SJeremy L Thompson CeedScalar x_array[12] = {-0.33, -0.65, 0.16, 0.99, -0.65, 0.16, 0.99, -0.33, 0.16, 0.99, -0.33, -0.65};
80*db2becc9SJeremy L Thompson
81*db2becc9SJeremy L Thompson CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
82*db2becc9SJeremy L Thompson }
83*db2becc9SJeremy L Thompson
84*db2becc9SJeremy L Thompson // Calculate G u at arbitrary points, G' * 1 at dofs
85*db2becc9SJeremy L Thompson CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, x_points, u, u_points);
86*db2becc9SJeremy L Thompson CeedBasisApplyAtPoints(basis_u, 1, &num_points, CEED_TRANSPOSE, CEED_EVAL_GRAD, x_points, ones, v);
87*db2becc9SJeremy L Thompson // Double it
88*db2becc9SJeremy L Thompson CeedBasisApplyAddAtPoints(basis_u, 1, &num_points, CEED_TRANSPOSE, CEED_EVAL_GRAD, x_points, ones, v);
89*db2becc9SJeremy L Thompson {
90*db2becc9SJeremy L Thompson const CeedScalar *u_array, *v_array, *u_points_array;
91*db2becc9SJeremy L Thompson
92*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array);
93*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
94*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(u_points, CEED_MEM_HOST, &u_points_array);
95*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) sum_1 += v_array[i] * u_array[i];
96*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < num_points * dim; i++) sum_2 += u_points_array[i];
97*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(u, &u_array);
98*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
99*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(u_points, &u_points_array);
100*db2becc9SJeremy L Thompson }
101*db2becc9SJeremy L Thompson {
102*db2becc9SJeremy L Thompson CeedScalarType scalar_type;
103*db2becc9SJeremy L Thompson
104*db2becc9SJeremy L Thompson CeedGetScalarType(&scalar_type);
105*db2becc9SJeremy L Thompson
106*db2becc9SJeremy L Thompson CeedScalar tol = GetTolerance(scalar_type, dim);
107*db2becc9SJeremy L Thompson
108*db2becc9SJeremy L Thompson if (fabs(sum_1 - 2.0 * sum_2) > tol) printf("[%" CeedInt_FMT "] %f != %f\n", dim, sum_1, 2.0 * sum_2);
109*db2becc9SJeremy L Thompson }
110*db2becc9SJeremy L Thompson
111*db2becc9SJeremy L Thompson CeedVectorDestroy(&x);
112*db2becc9SJeremy L Thompson CeedVectorDestroy(&x_nodes);
113*db2becc9SJeremy L Thompson CeedVectorDestroy(&x_points);
114*db2becc9SJeremy L Thompson CeedVectorDestroy(&u);
115*db2becc9SJeremy L Thompson CeedVectorDestroy(&u_points);
116*db2becc9SJeremy L Thompson CeedVectorDestroy(&ones);
117*db2becc9SJeremy L Thompson CeedVectorDestroy(&v);
118*db2becc9SJeremy L Thompson CeedBasisDestroy(&basis_x);
119*db2becc9SJeremy L Thompson CeedBasisDestroy(&basis_u);
120*db2becc9SJeremy L Thompson }
121*db2becc9SJeremy L Thompson CeedDestroy(&ceed);
122*db2becc9SJeremy L Thompson return 0;
123*db2becc9SJeremy L Thompson }
124