xref: /libCEED/tests/t406-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
1ccec5866SJeremy L Thompson /// @file
2ccec5866SJeremy L Thompson /// Test QFunction helper macro
3ccec5866SJeremy L Thompson /// \test Test QFunction helper macro
42b730f8bSJeremy L Thompson #include "t406-qfunction.h"
52b730f8bSJeremy L Thompson 
6ccec5866SJeremy L Thompson #include <ceed.h>
7ccec5866SJeremy L Thompson #include <math.h>
8*49aac155SJeremy L Thompson #include <stdio.h>
9ccec5866SJeremy L Thompson #include <stdlib.h>
10ccec5866SJeremy L Thompson #include <string.h>
11ccec5866SJeremy L Thompson 
12ccec5866SJeremy L Thompson int main(int argc, char **argv) {
13ccec5866SJeremy L Thompson   Ceed          ceed;
14ccec5866SJeremy L Thompson   CeedVector    in[16], out[16];
154fee36f0SJeremy L Thompson   CeedVector    q_data, w, u, v;
16ccec5866SJeremy L Thompson   CeedQFunction qf_setup, qf_mass;
174fee36f0SJeremy L Thompson   CeedInt       q = 8;
184fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
19ccec5866SJeremy L Thompson 
20ccec5866SJeremy L Thompson   CeedInit(argv[1], &ceed);
21ccec5866SJeremy L Thompson 
224fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
234fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
244fee36f0SJeremy L Thompson   {
254fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
264fee36f0SJeremy L Thompson 
274fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
284fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
294fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
304fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
314fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
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 
41ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
42ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
43ccec5866SJeremy L Thompson   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   }
49ccec5866SJeremy L Thompson 
50ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
51ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
52ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
53ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
54ccec5866SJeremy L Thompson   {
554fee36f0SJeremy L Thompson     in[0]  = w;
564fee36f0SJeremy L Thompson     in[1]  = u;
574fee36f0SJeremy L Thompson     out[0] = v;
584fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
59ccec5866SJeremy L Thompson   }
604fee36f0SJeremy L Thompson 
614fee36f0SJeremy L Thompson   // Verify results
62ccec5866SJeremy L Thompson   {
634fee36f0SJeremy L Thompson     const CeedScalar *v_array;
644fee36f0SJeremy L Thompson 
654fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
664fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
674fee36f0SJeremy L Thompson       if (fabs(5 * v_true[i] * sqrt(2.) - v_array[i]) > 1E3 * CEED_EPSILON)
684fee36f0SJeremy L Thompson         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * v_true[i] * sqrt(2.), v_array[i]);
694fee36f0SJeremy L Thompson     }
704fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
71ccec5866SJeremy L Thompson   }
72ccec5866SJeremy L Thompson 
734fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
744fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
754fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
764fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
77ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
78ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
79ccec5866SJeremy L Thompson   CeedDestroy(&ceed);
80ccec5866SJeremy L Thompson   return 0;
81ccec5866SJeremy L Thompson }
82