xref: /libCEED/tests/t406-qfunction.c (revision 255dad3207f061d22701e91ddb8337d8c6809493)
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>
849aac155SJeremy 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);
21*255dad32SJeremy L Thompson   {
22*255dad32SJeremy L Thompson     char  file_path[2056] = __FILE__;
23*255dad32SJeremy L Thompson     char *last_slash      = strrchr(file_path, '/');
24*255dad32SJeremy L Thompson 
25*255dad32SJeremy L Thompson     memcpy(&file_path[last_slash - file_path], "/test-include/", 15);
26*255dad32SJeremy L Thompson     CeedAddJitSourceRoot(ceed, file_path);
27*255dad32SJeremy L Thompson   }
28ccec5866SJeremy L Thompson 
294fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
304fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
314fee36f0SJeremy L Thompson   {
324fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
334fee36f0SJeremy L Thompson 
344fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
354fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
364fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
374fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
384fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
394fee36f0SJeremy L Thompson     }
404fee36f0SJeremy L Thompson     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
414fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
424fee36f0SJeremy L Thompson   }
434fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
444fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
454fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
464fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
474fee36f0SJeremy L Thompson 
48ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
49ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
50ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
514fee36f0SJeremy L Thompson   {
524fee36f0SJeremy L Thompson     in[0]  = w;
534fee36f0SJeremy L Thompson     out[0] = q_data;
544fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
554fee36f0SJeremy L Thompson   }
56ccec5866SJeremy L Thompson 
57ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
58ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
59ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
60ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
61ccec5866SJeremy L Thompson   {
624fee36f0SJeremy L Thompson     in[0]  = w;
634fee36f0SJeremy L Thompson     in[1]  = u;
644fee36f0SJeremy L Thompson     out[0] = v;
654fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
66ccec5866SJeremy L Thompson   }
674fee36f0SJeremy L Thompson 
684fee36f0SJeremy L Thompson   // Verify results
69ccec5866SJeremy L Thompson   {
704fee36f0SJeremy L Thompson     const CeedScalar *v_array;
714fee36f0SJeremy L Thompson 
724fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
734fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
742b62239cSJeremy L Thompson       if (fabs(5 * v_true[i] * sqrt(2.) - v_array[i]) > 1E3 * CEED_EPSILON) {
752b62239cSJeremy L Thompson         // LCOV_EXCL_START
764fee36f0SJeremy L Thompson         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * v_true[i] * sqrt(2.), v_array[i]);
772b62239cSJeremy L Thompson         // LCOV_EXCL_STOP
782b62239cSJeremy L Thompson       }
794fee36f0SJeremy L Thompson     }
804fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
81ccec5866SJeremy L Thompson   }
82ccec5866SJeremy L Thompson 
834fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
844fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
854fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
864fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
87ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
88ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
89ccec5866SJeremy L Thompson   CeedDestroy(&ceed);
90ccec5866SJeremy L Thompson   return 0;
91ccec5866SJeremy L Thompson }
92