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