1*db2becc9SJeremy L Thompson /// @file
2*db2becc9SJeremy L Thompson /// Test grad ApplyAdd in multiple dimensions
3*db2becc9SJeremy L Thompson /// \test Test grad ApplyAdd in multiple dimensions
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.05;
19*db2becc9SJeremy L Thompson else tol = 1.e-3;
20*db2becc9SJeremy L Thompson } else {
21*db2becc9SJeremy L Thompson tol = 1.e-10;
22*db2becc9SJeremy L Thompson }
23*db2becc9SJeremy L Thompson return 2.0 * 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_q, u, u_q, ones, v;
33*db2becc9SJeremy L Thompson CeedBasis basis_x_lobatto, basis_u_gauss;
34*db2becc9SJeremy L Thompson CeedInt p = 8, q = 10, p_dim = CeedIntPow(p, dim), q_dim = CeedIntPow(q, dim), x_dim = CeedIntPow(2, 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 {
39*db2becc9SJeremy L Thompson CeedScalar x_array[x_dim * dim];
40*db2becc9SJeremy L Thompson
41*db2becc9SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) {
42*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;
43*db2becc9SJeremy L Thompson }
44*db2becc9SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
45*db2becc9SJeremy L Thompson }
46*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim * dim, &x_q);
47*db2becc9SJeremy L Thompson CeedVectorSetValue(x_q, 0);
48*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &u);
49*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, q_dim * dim, &u_q);
50*db2becc9SJeremy L Thompson CeedVectorSetValue(u_q, 0);
51*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, q_dim * dim, &ones);
52*db2becc9SJeremy L Thompson CeedVectorSetValue(ones, 1);
53*db2becc9SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &v);
54*db2becc9SJeremy L Thompson CeedVectorSetValue(v, 0);
55*db2becc9SJeremy L Thompson
56*db2becc9SJeremy L Thompson // Get function values at quadrature points
57*db2becc9SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, p, CEED_GAUSS_LOBATTO, &basis_x_lobatto);
58*db2becc9SJeremy L Thompson CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q);
59*db2becc9SJeremy L Thompson
60*db2becc9SJeremy L Thompson {
61*db2becc9SJeremy L Thompson const CeedScalar *x_q_array;
62*db2becc9SJeremy L Thompson CeedScalar u_array[p_dim];
63*db2becc9SJeremy L Thompson
64*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array);
65*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) {
66*db2becc9SJeremy L Thompson CeedScalar coord[dim];
67*db2becc9SJeremy L Thompson
68*db2becc9SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * p_dim + i];
69*db2becc9SJeremy L Thompson u_array[i] = Eval(dim, coord);
70*db2becc9SJeremy L Thompson }
71*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(x_q, &x_q_array);
72*db2becc9SJeremy L Thompson CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
73*db2becc9SJeremy L Thompson }
74*db2becc9SJeremy L Thompson
75*db2becc9SJeremy L Thompson // Calculate G u at quadrature points, G' * 1 at dofs
76*db2becc9SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u_gauss);
77*db2becc9SJeremy L Thompson CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, u, u_q);
78*db2becc9SJeremy L Thompson CeedVectorScale(u_q, 2.0);
79*db2becc9SJeremy L Thompson CeedBasisApply(basis_u_gauss, 1, CEED_TRANSPOSE, CEED_EVAL_GRAD, ones, v);
80*db2becc9SJeremy L Thompson CeedBasisApplyAdd(basis_u_gauss, 1, CEED_TRANSPOSE, CEED_EVAL_GRAD, ones, v);
81*db2becc9SJeremy L Thompson
82*db2becc9SJeremy L Thompson // Check if 1' * G * u = u' * (G' * 1)
83*db2becc9SJeremy L Thompson {
84*db2becc9SJeremy L Thompson const CeedScalar *v_array, *u_array, *u_q_array;
85*db2becc9SJeremy L Thompson
86*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
87*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array);
88*db2becc9SJeremy L Thompson CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_q_array);
89*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) sum_1 += v_array[i] * u_array[i];
90*db2becc9SJeremy L Thompson for (CeedInt i = 0; i < dim * q_dim; i++) sum_2 += u_q_array[i];
91*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
92*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(u, &u_array);
93*db2becc9SJeremy L Thompson CeedVectorRestoreArrayRead(u_q, &u_q_array);
94*db2becc9SJeremy L Thompson }
95*db2becc9SJeremy L Thompson {
96*db2becc9SJeremy L Thompson CeedScalarType scalar_type;
97*db2becc9SJeremy L Thompson
98*db2becc9SJeremy L Thompson CeedGetScalarType(&scalar_type);
99*db2becc9SJeremy L Thompson
100*db2becc9SJeremy L Thompson CeedScalar tol = GetTolerance(scalar_type, dim);
101*db2becc9SJeremy L Thompson
102*db2becc9SJeremy L Thompson if (fabs(sum_1 - sum_2) > tol) printf("[%" CeedInt_FMT "] %0.12f != %0.12f\n", dim, sum_1, sum_2);
103*db2becc9SJeremy L Thompson }
104*db2becc9SJeremy L Thompson
105*db2becc9SJeremy L Thompson CeedVectorDestroy(&x);
106*db2becc9SJeremy L Thompson CeedVectorDestroy(&x_q);
107*db2becc9SJeremy L Thompson CeedVectorDestroy(&u);
108*db2becc9SJeremy L Thompson CeedVectorDestroy(&u_q);
109*db2becc9SJeremy L Thompson CeedVectorDestroy(&ones);
110*db2becc9SJeremy L Thompson CeedVectorDestroy(&v);
111*db2becc9SJeremy L Thompson CeedBasisDestroy(&basis_x_lobatto);
112*db2becc9SJeremy L Thompson CeedBasisDestroy(&basis_u_gauss);
113*db2becc9SJeremy L Thompson }
114*db2becc9SJeremy L Thompson CeedDestroy(&ceed);
115*db2becc9SJeremy L Thompson return 0;
116*db2becc9SJeremy L Thompson }
117