xref: /libCEED/tests/t400-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
14411cf47Sjeremylt /// @file
2d1d35e2fSjeremylt /// Test creation, evaluation, and destruction for QFunction
3d1d35e2fSjeremylt /// \test Test creation, evaluation, and destruction for QFunction
44d537eeaSYohann #include "t400-qfunction.h"
557c64913Sjeremylt 
62b730f8bSJeremy L Thompson #include <ceed.h>
7*49aac155SJeremy L Thompson #include <stdio.h>
82b730f8bSJeremy L Thompson 
957c64913Sjeremylt int main(int argc, char **argv) {
1057c64913Sjeremylt   Ceed          ceed;
11aedaa0e5Sjeremylt   CeedVector    in[16], out[16];
124fee36f0SJeremy L Thompson   CeedVector    q_data, w, u, v;
1357c64913Sjeremylt   CeedQFunction qf_setup, qf_mass;
144fee36f0SJeremy L Thompson   CeedInt       q = 8;
154fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
1657c64913Sjeremylt 
1757c64913Sjeremylt   CeedInit(argv[1], &ceed);
18aedaa0e5Sjeremylt 
194fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
204fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
214fee36f0SJeremy L Thompson   {
224fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
234fee36f0SJeremy L Thompson 
244fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
254fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 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(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
314fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
324fee36f0SJeremy L Thompson   }
334fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
344fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
354fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
364fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
374fee36f0SJeremy L Thompson 
384d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
3984e209c4Sjeremylt   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
4084e209c4Sjeremylt   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
414fee36f0SJeremy L Thompson   {
424fee36f0SJeremy L Thompson     in[0]  = w;
434fee36f0SJeremy L Thompson     out[0] = q_data;
444fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
454fee36f0SJeremy L Thompson   }
4657c64913Sjeremylt 
474d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
4884e209c4Sjeremylt   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
4957c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
5057c64913Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
5157c64913Sjeremylt   {
524fee36f0SJeremy L Thompson     in[0]  = w;
534fee36f0SJeremy L Thompson     in[1]  = u;
544fee36f0SJeremy L Thompson     out[0] = v;
554fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
5657c64913Sjeremylt   }
574fee36f0SJeremy L Thompson 
584fee36f0SJeremy L Thompson   // Verify result
5957c64913Sjeremylt   {
604fee36f0SJeremy L Thompson     const CeedScalar *v_array;
614fee36f0SJeremy L Thompson 
624fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
634fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
644fee36f0SJeremy L Thompson       if (v_array[i] != v_true[i]) printf("[%" CeedInt_FMT "] v %f != v_true %f\n", i, v_array[i], v_true[i]);
654fee36f0SJeremy L Thompson     }
664fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
6757c64913Sjeremylt   }
68aedaa0e5Sjeremylt 
694fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
704fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
714fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
724fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
7357c64913Sjeremylt   CeedQFunctionDestroy(&qf_setup);
7457c64913Sjeremylt   CeedQFunctionDestroy(&qf_mass);
7557c64913Sjeremylt   CeedDestroy(&ceed);
7657c64913Sjeremylt   return 0;
7757c64913Sjeremylt }
78