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