xref: /libCEED/tests/t406-qfunction.c (revision e036be4077c6ca5247c17c8792418b6c84e1762f)
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 
main(int argc,char ** argv)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);
21255dad32SJeremy L Thompson   {
22255dad32SJeremy L Thompson     char  file_path[2056] = __FILE__;
23255dad32SJeremy L Thompson     char *last_slash      = strrchr(file_path, '/');
24255dad32SJeremy L Thompson 
25255dad32SJeremy L Thompson     memcpy(&file_path[last_slash - file_path], "/test-include/", 15);
26255dad32SJeremy L Thompson     CeedAddJitSourceRoot(ceed, file_path);
27*4753b775SJeremy L Thompson     CeedAddJitDefine(ceed, "COMPILER_DEFINED_SCALE=42");
28255dad32SJeremy L Thompson   }
29ccec5866SJeremy L Thompson 
304fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
314fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
324fee36f0SJeremy L Thompson   {
334fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
344fee36f0SJeremy L Thompson 
354fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
364fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
374fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
384fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
394fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
404fee36f0SJeremy L Thompson     }
414fee36f0SJeremy L Thompson     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
424fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
434fee36f0SJeremy L Thompson   }
444fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
454fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
464fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
474fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
484fee36f0SJeremy L Thompson 
49ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
50ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
51ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
524fee36f0SJeremy L Thompson   {
534fee36f0SJeremy L Thompson     in[0]  = w;
544fee36f0SJeremy L Thompson     out[0] = q_data;
554fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
564fee36f0SJeremy L Thompson   }
57ccec5866SJeremy L Thompson 
58ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
59ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
60ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
61ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
62ccec5866SJeremy L Thompson   {
634fee36f0SJeremy L Thompson     in[0]  = w;
644fee36f0SJeremy L Thompson     in[1]  = u;
654fee36f0SJeremy L Thompson     out[0] = v;
664fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
67ccec5866SJeremy L Thompson   }
684fee36f0SJeremy L Thompson 
694fee36f0SJeremy L Thompson   // Verify results
70ccec5866SJeremy L Thompson   {
714fee36f0SJeremy L Thompson     const CeedScalar *v_array;
724fee36f0SJeremy L Thompson 
734fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
744fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
75*4753b775SJeremy L Thompson       if (fabs(5 * COMPILER_DEFINED_SCALE * v_true[i] * sqrt(2.) - v_array[i]) > 5E3 * CEED_EPSILON) {
762b62239cSJeremy L Thompson         // LCOV_EXCL_START
77*4753b775SJeremy L Thompson         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * COMPILER_DEFINED_SCALE * v_true[i] * sqrt(2.), v_array[i]);
782b62239cSJeremy L Thompson         // LCOV_EXCL_STOP
792b62239cSJeremy L Thompson       }
804fee36f0SJeremy L Thompson     }
814fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
82ccec5866SJeremy L Thompson   }
83ccec5866SJeremy L Thompson 
844fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
854fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
864fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
874fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
88ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
89ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
90ccec5866SJeremy L Thompson   CeedDestroy(&ceed);
91ccec5866SJeremy L Thompson   return 0;
92ccec5866SJeremy L Thompson }
93