xref: /libCEED/tests/t406-qfunction.c (revision a491a57ee1186d4a57a443cd84e0df1f56768bf7)
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   }
28 
29   CeedVectorCreate(ceed, q, &w);
30   CeedVectorCreate(ceed, q, &u);
31   {
32     CeedScalar w_array[q], u_array[q];
33 
34     for (CeedInt i = 0; i < q; i++) {
35       CeedScalar x = 2. * i / (q - 1) - 1;
36       w_array[i]   = 1 - x * x;
37       u_array[i]   = 2 + 3 * x + 5 * x * x;
38       v_true[i]    = w_array[i] * u_array[i];
39     }
40     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
41     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
42   }
43   CeedVectorCreate(ceed, q, &v);
44   CeedVectorSetValue(v, 0);
45   CeedVectorCreate(ceed, q, &q_data);
46   CeedVectorSetValue(q_data, 0);
47 
48   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
49   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
50   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
51   {
52     in[0]  = w;
53     out[0] = q_data;
54     CeedQFunctionApply(qf_setup, q, in, out);
55   }
56 
57   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
58   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
59   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
60   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
61   {
62     in[0]  = w;
63     in[1]  = u;
64     out[0] = v;
65     CeedQFunctionApply(qf_mass, q, in, out);
66   }
67 
68   // Verify results
69   {
70     const CeedScalar *v_array;
71 
72     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
73     for (CeedInt i = 0; i < q; i++) {
74       if (fabs(5 * v_true[i] * sqrt(2.) - v_array[i]) > 1E3 * CEED_EPSILON) {
75         // LCOV_EXCL_START
76         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * v_true[i] * sqrt(2.), v_array[i]);
77         // LCOV_EXCL_STOP
78       }
79     }
80     CeedVectorRestoreArrayRead(v, &v_array);
81   }
82 
83   CeedVectorDestroy(&w);
84   CeedVectorDestroy(&u);
85   CeedVectorDestroy(&v);
86   CeedVectorDestroy(&q_data);
87   CeedQFunctionDestroy(&qf_setup);
88   CeedQFunctionDestroy(&qf_mass);
89   CeedDestroy(&ceed);
90   return 0;
91 }
92