152bfb9bbSJeremy L Thompson /// @file 252bfb9bbSJeremy L Thompson /// Test underintegrated grad in multiple dimensions 352bfb9bbSJeremy L Thompson /// \test Test underintegraded grad in multiple dimensions 452bfb9bbSJeremy L Thompson #include <ceed.h> 552bfb9bbSJeremy L Thompson #include <math.h> 649aac155SJeremy L Thompson #include <stdio.h> 752bfb9bbSJeremy L Thompson 852bfb9bbSJeremy L Thompson static CeedScalar Eval(CeedInt dim, const CeedScalar x[]) { 952bfb9bbSJeremy L Thompson CeedScalar result = tanh(x[0] + 0.1); 1052bfb9bbSJeremy L Thompson if (dim > 1) result += atan(x[1] + 0.2); 1152bfb9bbSJeremy L Thompson if (dim > 2) result += exp(-(x[2] + 0.3) * (x[2] + 0.3)); 1252bfb9bbSJeremy L Thompson return result; 1352bfb9bbSJeremy L Thompson } 1452bfb9bbSJeremy L Thompson 1580a9ef05SNatalie Beams static CeedScalar GetTolerance(CeedScalarType scalar_type, int dim) { 1680a9ef05SNatalie Beams CeedScalar tol; 1780a9ef05SNatalie Beams if (scalar_type == CEED_SCALAR_FP32) { 182b730f8bSJeremy L Thompson if (dim == 3) tol = 0.005; 192b730f8bSJeremy L Thompson else tol = 1.e-4; 2080a9ef05SNatalie Beams } else { 2180a9ef05SNatalie Beams tol = 1.e-11; 2280a9ef05SNatalie Beams } 2380a9ef05SNatalie Beams return tol; 2480a9ef05SNatalie Beams } 2580a9ef05SNatalie Beams 2652bfb9bbSJeremy L Thompson int main(int argc, char **argv) { 2752bfb9bbSJeremy L Thompson Ceed ceed; 2852bfb9bbSJeremy L Thompson 2952bfb9bbSJeremy L Thompson CeedInit(argv[1], &ceed); 3052bfb9bbSJeremy L Thompson 3152bfb9bbSJeremy L Thompson for (CeedInt dim = 1; dim <= 3; dim++) { 324fee36f0SJeremy L Thompson CeedVector x, x_q, u, u_q, ones, v; 33d1d35e2fSjeremylt CeedBasis basis_x_lobatto, basis_u_gauss; 344fee36f0SJeremy L Thompson CeedInt p = 8, q = 7, p_dim = CeedIntPow(p, dim), q_dim = CeedIntPow(q, dim), x_dim = CeedIntPow(2, dim); 35d1d35e2fSjeremylt CeedScalar sum_1 = 0, sum_2 = 0; 3652bfb9bbSJeremy L Thompson 374fee36f0SJeremy L Thompson CeedVectorCreate(ceed, x_dim * dim, &x); 384fee36f0SJeremy L Thompson { 394fee36f0SJeremy L Thompson CeedScalar x_array[x_dim * dim]; 4052bfb9bbSJeremy L Thompson 414fee36f0SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) { 423c9d155aSZach Atkins for (CeedInt i = 0; i < x_dim; i++) x_array[d * x_dim + i] = (i % CeedIntPow(2, d + 1)) / CeedIntPow(2, d) ? 1 : -1; 434fee36f0SJeremy L Thompson } 444fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 454fee36f0SJeremy L Thompson } 464fee36f0SJeremy L Thompson CeedVectorCreate(ceed, p_dim * dim, &x_q); 474fee36f0SJeremy L Thompson CeedVectorSetValue(x_q, 0); 484fee36f0SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &u); 494fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q_dim * dim, &u_q); 504fee36f0SJeremy L Thompson CeedVectorSetValue(u_q, 0); 514fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q_dim * dim, &ones); 52d1d35e2fSjeremylt CeedVectorSetValue(ones, 1); 534fee36f0SJeremy L Thompson CeedVectorCreate(ceed, p_dim, &v); 544fee36f0SJeremy L Thompson CeedVectorSetValue(v, 0); 5552bfb9bbSJeremy L Thompson 5652bfb9bbSJeremy L Thompson // Get function values at quadrature points 574fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, p, CEED_GAUSS_LOBATTO, &basis_x_lobatto); 584fee36f0SJeremy L Thompson CeedBasisApply(basis_x_lobatto, 1, CEED_NOTRANSPOSE, CEED_EVAL_INTERP, x, x_q); 5952bfb9bbSJeremy L Thompson 604fee36f0SJeremy L Thompson { 614fee36f0SJeremy L Thompson const CeedScalar *x_q_array; 624fee36f0SJeremy L Thompson CeedScalar u_array[p_dim]; 634fee36f0SJeremy L Thompson 644fee36f0SJeremy L Thompson CeedVectorGetArrayRead(x_q, CEED_MEM_HOST, &x_q_array); 654fee36f0SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) { 664fee36f0SJeremy L Thompson CeedScalar coord[dim]; 67*99e754f0SJeremy L Thompson 684fee36f0SJeremy L Thompson for (CeedInt d = 0; d < dim; d++) coord[d] = x_q_array[d * p_dim + i]; 694fee36f0SJeremy L Thompson u_array[i] = Eval(dim, coord); 7052bfb9bbSJeremy L Thompson } 714fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(x_q, &x_q_array); 724fee36f0SJeremy L Thompson CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array); 734fee36f0SJeremy L Thompson } 7452bfb9bbSJeremy L Thompson 7552bfb9bbSJeremy L Thompson // Calculate G u at quadrature points, G' * 1 at dofs 764fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u_gauss); 774fee36f0SJeremy L Thompson CeedBasisApply(basis_u_gauss, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, u, u_q); 784fee36f0SJeremy L Thompson CeedBasisApply(basis_u_gauss, 1, CEED_TRANSPOSE, CEED_EVAL_GRAD, ones, v); 7952bfb9bbSJeremy L Thompson 8052bfb9bbSJeremy L Thompson // Check if 1' * G * u = u' * (G' * 1) 814fee36f0SJeremy L Thompson { 824fee36f0SJeremy L Thompson const CeedScalar *u_array, *v_array, *u_q_array; 834fee36f0SJeremy L Thompson 844fee36f0SJeremy L Thompson CeedVectorGetArrayRead(u, CEED_MEM_HOST, &u_array); 854fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 864fee36f0SJeremy L Thompson CeedVectorGetArrayRead(u_q, CEED_MEM_HOST, &u_q_array); 874fee36f0SJeremy L Thompson for (CeedInt i = 0; i < p_dim; i++) sum_1 += v_array[i] * u_array[i]; 884fee36f0SJeremy L Thompson for (CeedInt i = 0; i < dim * q_dim; i++) sum_2 += u_q_array[i]; 894fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(u, &u_array); 904fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 914fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(u_q, &u_q_array); 924fee36f0SJeremy L Thompson } 93*99e754f0SJeremy L Thompson { 94*99e754f0SJeremy L Thompson CeedScalarType scalar_type; 95*99e754f0SJeremy L Thompson 96*99e754f0SJeremy L Thompson CeedGetScalarType(&scalar_type); 97*99e754f0SJeremy L Thompson 98*99e754f0SJeremy L Thompson CeedScalar tol = GetTolerance(scalar_type, dim); 99*99e754f0SJeremy L Thompson 1002b730f8bSJeremy L Thompson if (fabs(sum_1 - sum_2) > tol) printf("[%" CeedInt_FMT "] %f != %f\n", dim, sum_1, sum_2); 101*99e754f0SJeremy L Thompson } 10252bfb9bbSJeremy L Thompson 1034fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1044fee36f0SJeremy L Thompson CeedVectorDestroy(&x_q); 1054fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1064fee36f0SJeremy L Thompson CeedVectorDestroy(&u_q); 107d1d35e2fSjeremylt CeedVectorDestroy(&ones); 1084fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 109d1d35e2fSjeremylt CeedBasisDestroy(&basis_x_lobatto); 110d1d35e2fSjeremylt CeedBasisDestroy(&basis_u_gauss); 11152bfb9bbSJeremy L Thompson } 11252bfb9bbSJeremy L Thompson CeedDestroy(&ceed); 11352bfb9bbSJeremy L Thompson return 0; 11452bfb9bbSJeremy L Thompson } 115