11e35832bSjeremylt /// @file
2d1d35e2fSjeremylt /// Test creation, evaluation, and destruction for QFunction
3d1d35e2fSjeremylt /// \test Test creation, evaluation, and destruction for QFunction
4*ac415f71SSebastian Grimberg #include "t401-qfunction.h"
5*ac415f71SSebastian Grimberg
61e35832bSjeremylt #include <ceed.h>
71e35832bSjeremylt #include <math.h>
849aac155SJeremy L Thompson #include <stdio.h>
91e35832bSjeremylt
main(int argc,char ** argv)101e35832bSjeremylt int main(int argc, char **argv) {
111e35832bSjeremylt Ceed ceed;
121e35832bSjeremylt CeedVector in[16], out[16];
134fee36f0SJeremy L Thompson CeedVector q_data, w, u, v;
141e35832bSjeremylt CeedQFunction qf_setup, qf_mass;
15777ff853SJeremy L Thompson CeedQFunctionContext ctx;
164fee36f0SJeremy L Thompson CeedInt q = 8;
174fee36f0SJeremy L Thompson CeedScalar v_true[q], ctx_data[5] = {1, 2, 3, 4, 5};
181e35832bSjeremylt
191e35832bSjeremylt CeedInit(argv[1], &ceed);
201e35832bSjeremylt
214fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &w);
224fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &u);
234fee36f0SJeremy L Thompson {
244fee36f0SJeremy L Thompson CeedScalar w_array[q], u_array[q];
254fee36f0SJeremy L Thompson
264fee36f0SJeremy L Thompson for (CeedInt i = 0; i < q; i++) {
274fee36f0SJeremy L Thompson CeedScalar x = 2. * i / (q - 1) - 1;
284fee36f0SJeremy L Thompson w_array[i] = 1 - x * x;
294fee36f0SJeremy L Thompson u_array[i] = 2 + 3 * x + 5 * x * x;
304fee36f0SJeremy L Thompson v_true[i] = w_array[i] * u_array[i];
314fee36f0SJeremy L Thompson }
324fee36f0SJeremy L Thompson
334fee36f0SJeremy L Thompson CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
344fee36f0SJeremy L Thompson CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
354fee36f0SJeremy L Thompson }
364fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &v);
374fee36f0SJeremy L Thompson CeedVectorSetValue(v, 0);
384fee36f0SJeremy L Thompson CeedVectorCreate(ceed, q, &q_data);
394fee36f0SJeremy L Thompson CeedVectorSetValue(q_data, 0);
404fee36f0SJeremy L Thompson
414d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
4284e209c4Sjeremylt CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
4384e209c4Sjeremylt CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
444fee36f0SJeremy L Thompson {
454fee36f0SJeremy L Thompson in[0] = w;
464fee36f0SJeremy L Thompson out[0] = q_data;
474fee36f0SJeremy L Thompson CeedQFunctionApply(qf_setup, q, in, out);
484fee36f0SJeremy L Thompson }
491e35832bSjeremylt
504d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
5184e209c4Sjeremylt CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
521e35832bSjeremylt CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
531e35832bSjeremylt CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
541e35832bSjeremylt
55777ff853SJeremy L Thompson CeedQFunctionContextCreate(ceed, &ctx);
562b730f8bSJeremy L Thompson CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(ctx_data), &ctx_data);
57777ff853SJeremy L Thompson CeedQFunctionSetContext(qf_mass, ctx);
581e35832bSjeremylt {
594fee36f0SJeremy L Thompson in[0] = w;
604fee36f0SJeremy L Thompson in[1] = u;
614fee36f0SJeremy L Thompson out[0] = v;
624fee36f0SJeremy L Thompson CeedQFunctionApply(qf_mass, q, in, out);
631e35832bSjeremylt }
644fee36f0SJeremy L Thompson
654fee36f0SJeremy L Thompson // Verify result
661e35832bSjeremylt {
674fee36f0SJeremy L Thompson const CeedScalar *v_array;
684fee36f0SJeremy L Thompson
694fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
704fee36f0SJeremy L Thompson for (CeedInt i = 0; i < q; i++) {
714fee36f0SJeremy L Thompson if (fabs(ctx_data[4] * v_true[i] - v_array[i]) > 100. * CEED_EPSILON) {
724fee36f0SJeremy L Thompson // LCOV_EXCL_START
734fee36f0SJeremy L Thompson printf("[%" CeedInt_FMT "] v %f != v_true %f\n", i, v_array[i], ctx_data[4] * v_true[i]);
744fee36f0SJeremy L Thompson // LCOV_EXCL_STOP
754fee36f0SJeremy L Thompson }
764fee36f0SJeremy L Thompson }
774fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
781e35832bSjeremylt }
791e35832bSjeremylt
804fee36f0SJeremy L Thompson CeedVectorDestroy(&w);
814fee36f0SJeremy L Thompson CeedVectorDestroy(&u);
824fee36f0SJeremy L Thompson CeedVectorDestroy(&v);
834fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data);
841e35832bSjeremylt CeedQFunctionDestroy(&qf_setup);
851e35832bSjeremylt CeedQFunctionDestroy(&qf_mass);
86777ff853SJeremy L Thompson CeedQFunctionContextDestroy(&ctx);
871e35832bSjeremylt CeedDestroy(&ceed);
881e35832bSjeremylt return 0;
891e35832bSjeremylt }
90