xref: /libCEED/tests/t406-qfunction.c (revision d275d636ccaa61e594421fac80252590e7a77ccf)
1 /// @file
2 /// Test QFunction helper macro
3 /// \test Test QFunction helper macro
4 #include "t406-qfunction.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
12 int main(int argc, char **argv) {
13   Ceed          ceed;
14   CeedVector    in[16], out[16];
15   CeedVector    q_data, w, u, v;
16   CeedQFunction qf_setup, qf_mass;
17   CeedInt       q = 8;
18   CeedScalar    v_true[q];
19 
20   CeedInit(argv[1], &ceed);
21   {
22     char  file_path[2056] = __FILE__;
23     char *last_slash      = strrchr(file_path, '/');
24 
25     memcpy(&file_path[last_slash - file_path], "/test-include/", 15);
26     CeedAddJitSourceRoot(ceed, file_path);
27     CeedAddJitDefine(ceed, "COMPILER_DEFINED_SCALE=42");
28   }
29 
30   CeedVectorCreate(ceed, q, &w);
31   CeedVectorCreate(ceed, q, &u);
32   {
33     CeedScalar w_array[q], u_array[q];
34 
35     for (CeedInt i = 0; i < q; i++) {
36       CeedScalar x = 2. * i / (q - 1) - 1;
37       w_array[i]   = 1 - x * x;
38       u_array[i]   = 2 + 3 * x + 5 * x * x;
39       v_true[i]    = w_array[i] * u_array[i];
40     }
41     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
42     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
43   }
44   CeedVectorCreate(ceed, q, &v);
45   CeedVectorSetValue(v, 0);
46   CeedVectorCreate(ceed, q, &q_data);
47   CeedVectorSetValue(q_data, 0);
48 
49   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
50   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
51   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
52   {
53     in[0]  = w;
54     out[0] = q_data;
55     CeedQFunctionApply(qf_setup, q, in, out);
56   }
57 
58   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
59   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
60   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
61   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
62   {
63     in[0]  = w;
64     in[1]  = u;
65     out[0] = v;
66     CeedQFunctionApply(qf_mass, q, in, out);
67   }
68 
69   // Verify results
70   {
71     const CeedScalar *v_array;
72 
73     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
74     for (CeedInt i = 0; i < q; i++) {
75       if (fabs(5 * COMPILER_DEFINED_SCALE * v_true[i] * sqrt(2.) - v_array[i]) > 5E3 * CEED_EPSILON) {
76         // LCOV_EXCL_START
77         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * COMPILER_DEFINED_SCALE * v_true[i] * sqrt(2.), v_array[i]);
78         // LCOV_EXCL_STOP
79       }
80     }
81     CeedVectorRestoreArrayRead(v, &v_array);
82   }
83 
84   CeedVectorDestroy(&w);
85   CeedVectorDestroy(&u);
86   CeedVectorDestroy(&v);
87   CeedVectorDestroy(&q_data);
88   CeedQFunctionDestroy(&qf_setup);
89   CeedQFunctionDestroy(&qf_mass);
90   CeedDestroy(&ceed);
91   return 0;
92 }
93