xref: /libCEED/tests/t410-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
1288c0443SJeremy L Thompson /// @file
2d1d35e2fSjeremylt /// Test creation, evaluation, and destruction for QFunction by name
3d1d35e2fSjeremylt /// \test Test creation, evaluation, and destruction for QFunction by name
4288c0443SJeremy L Thompson #include <ceed.h>
5*49aac155SJeremy L Thompson #include <stdio.h>
6288c0443SJeremy L Thompson 
main(int argc,char ** argv)7288c0443SJeremy L Thompson int main(int argc, char **argv) {
8288c0443SJeremy L Thompson   Ceed          ceed;
9288c0443SJeremy L Thompson   CeedVector    in[16], out[16];
104fee36f0SJeremy L Thompson   CeedVector    q_data, dx, w, u, v;
11288c0443SJeremy L Thompson   CeedQFunction qf_setup, qf_mass;
124fee36f0SJeremy L Thompson   CeedInt       q = 8;
134fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
14288c0443SJeremy L Thompson 
15288c0443SJeremy L Thompson   CeedInit(argv[1], &ceed);
16288c0443SJeremy L Thompson 
174fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &dx);
184fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
194fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
204fee36f0SJeremy L Thompson   {
214fee36f0SJeremy L Thompson     CeedScalar dx_array[q], w_array[q], u_array[q];
224fee36f0SJeremy L Thompson 
234fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
244fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
254fee36f0SJeremy L Thompson       dx_array[i]  = 1;
264fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
274fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
284fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
294fee36f0SJeremy L Thompson     }
304fee36f0SJeremy L Thompson     CeedVectorSetArray(dx, CEED_MEM_HOST, CEED_COPY_VALUES, dx_array);
314fee36f0SJeremy L Thompson     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
324fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
334fee36f0SJeremy L Thompson   }
344fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
354fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
364fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
374fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
384fee36f0SJeremy L Thompson 
39288c0443SJeremy L Thompson   CeedQFunctionCreateInteriorByName(ceed, "Mass1DBuild", &qf_setup);
404fee36f0SJeremy L Thompson   {
414fee36f0SJeremy L Thompson     in[0]  = dx;
424fee36f0SJeremy L Thompson     in[1]  = w;
434fee36f0SJeremy L Thompson     out[0] = q_data;
444fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
454fee36f0SJeremy L Thompson   }
464fee36f0SJeremy L Thompson 
47288c0443SJeremy L Thompson   CeedQFunctionCreateInteriorByName(ceed, "MassApply", &qf_mass);
48288c0443SJeremy L Thompson   {
494fee36f0SJeremy L Thompson     in[0]  = w;
504fee36f0SJeremy L Thompson     in[1]  = u;
514fee36f0SJeremy L Thompson     out[0] = v;
524fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
53288c0443SJeremy L Thompson   }
544fee36f0SJeremy L Thompson 
554fee36f0SJeremy L Thompson   // Verify results
56288c0443SJeremy L Thompson   {
574fee36f0SJeremy L Thompson     const CeedScalar *v_array;
584fee36f0SJeremy L Thompson 
594fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
604fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
614fee36f0SJeremy L Thompson       if (v_true[i] != v_array[i]) printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, v_true[i], v_array[i]);
624fee36f0SJeremy L Thompson     }
634fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
64288c0443SJeremy L Thompson   }
65288c0443SJeremy L Thompson 
664fee36f0SJeremy L Thompson   CeedVectorDestroy(&dx);
674fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
684fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
694fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
704fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
71288c0443SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
72288c0443SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
73288c0443SJeremy L Thompson   CeedDestroy(&ceed);
74288c0443SJeremy L Thompson   return 0;
75288c0443SJeremy L Thompson }
76