152bfb9bbSJeremy L Thompson /// @file
2*b5404d5dSSebastian Grimberg /// Test grad with a 2D Simplex non-tensor H^1 basis
3*b5404d5dSSebastian Grimberg /// \test Test grad with a 2D Simplex non-tensor H^1 basis
452bfb9bbSJeremy L Thompson #include <ceed.h>
552bfb9bbSJeremy L Thompson #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson
852bfb9bbSJeremy L Thompson #include "t320-basis.h"
952bfb9bbSJeremy L Thompson
102b730f8bSJeremy L Thompson // polynomial eval helper
Eval(CeedScalar x1,CeedScalar x2)114fee36f0SJeremy L Thompson static CeedScalar Eval(CeedScalar x1, CeedScalar x2) { return x1 * x1 + x2 * x2 + x1 * x2 + 1; }
1252bfb9bbSJeremy L Thompson
132b730f8bSJeremy L Thompson // polynomial derivative helper
EvalGrad(CeedScalar x1,CeedScalar x2)144fee36f0SJeremy L Thompson CeedScalar EvalGrad(CeedScalar x1, CeedScalar x2) { return 2 * x1 + x2; }
1552bfb9bbSJeremy L Thompson
162b730f8bSJeremy L Thompson // main test
main(int argc,char ** argv)1752bfb9bbSJeremy L Thompson int main(int argc, char **argv) {
1852bfb9bbSJeremy L Thompson Ceed ceed;
194fee36f0SJeremy L Thompson CeedVector u, v;
204fee36f0SJeremy L Thompson const CeedInt p = 6, q = 4, dim = 2;
214fee36f0SJeremy L Thompson CeedBasis basis;
224fee36f0SJeremy L Thompson CeedScalar q_ref[dim * q], q_weight[q];
234fee36f0SJeremy L Thompson CeedScalar interp[p * q], grad[dim * p * q];
244fee36f0SJeremy L Thompson CeedScalar x_q[] = {0.2, 0.6, 1. / 3., 0.2, 0.2, 0.2, 1. / 3., 0.6};
254fee36f0SJeremy L Thompson CeedScalar x_ref[] = {0., 0.5, 1., 0., 0.5, 0., 0., 0., 0., 0.5, 0.5, 1.};
2652bfb9bbSJeremy L Thompson
2752bfb9bbSJeremy L Thompson CeedInit(argv[1], &ceed);
2852bfb9bbSJeremy L Thompson
294fee36f0SJeremy L Thompson CeedVectorCreate(ceed, p, &u);
304fee36f0SJeremy L Thompson {
314fee36f0SJeremy L Thompson CeedScalar u_array[p];
3252bfb9bbSJeremy L Thompson
3352bfb9bbSJeremy L Thompson // Interpolate function to quadrature points
344fee36f0SJeremy L Thompson for (int i = 0; i < p; i++) u_array[i] = Eval(x_ref[0 * p + i], x_ref[1 * p + i]);
354fee36f0SJeremy L Thompson CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
364fee36f0SJeremy L Thompson }
374fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q * dim, &v);
384fee36f0SJeremy L Thompson CeedVectorSetValue(v, 0);
3952bfb9bbSJeremy L Thompson
404fee36f0SJeremy L Thompson Build2DSimplex(q_ref, q_weight, interp, grad);
414fee36f0SJeremy L Thompson CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis);
4252bfb9bbSJeremy L Thompson
434fee36f0SJeremy L Thompson CeedBasisApply(basis, 1, CEED_NOTRANSPOSE, CEED_EVAL_GRAD, u, v);
4452bfb9bbSJeremy L Thompson
4552bfb9bbSJeremy L Thompson // Check values at quadrature points
464fee36f0SJeremy L Thompson {
474fee36f0SJeremy L Thompson const CeedScalar *v_array;
4852bfb9bbSJeremy L Thompson
494fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
504fee36f0SJeremy L Thompson for (int i = 0; i < q; i++) {
514fee36f0SJeremy L Thompson CeedScalar dfx = EvalGrad(x_q[0 * q + i], x_q[1 * q + i]);
524fee36f0SJeremy L Thompson if (fabs(v_array[0 * q + i] - dfx) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] %f != %f\n", i, v_array[0 * q + i], dfx);
534fee36f0SJeremy L Thompson dfx = EvalGrad(x_q[1 * q + i], x_q[0 * q + i]);
544fee36f0SJeremy L Thompson if (fabs(v_array[1 * q + i] - dfx) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] %f != %f\n", i, v_array[1 * q + i], dfx);
554fee36f0SJeremy L Thompson }
564fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
574fee36f0SJeremy L Thompson }
584fee36f0SJeremy L Thompson
594fee36f0SJeremy L Thompson CeedVectorDestroy(&u);
604fee36f0SJeremy L Thompson CeedVectorDestroy(&v);
614fee36f0SJeremy L Thompson CeedBasisDestroy(&basis);
6252bfb9bbSJeremy L Thompson CeedDestroy(&ceed);
6352bfb9bbSJeremy L Thompson return 0;
6452bfb9bbSJeremy L Thompson }
65