xref: /libCEED/tests/t401-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
1 /// @file
2 /// Test creation, evaluation, and destruction for QFunction
3 /// \test Test creation, evaluation, and destruction for QFunction
4 #include "t401-qfunction.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 #include <stdio.h>
9 
10 int main(int argc, char **argv) {
11   Ceed                 ceed;
12   CeedVector           in[16], out[16];
13   CeedVector           q_data, w, u, v;
14   CeedQFunction        qf_setup, qf_mass;
15   CeedQFunctionContext ctx;
16   CeedInt              q = 8;
17   CeedScalar           v_true[q], ctx_data[5] = {1, 2, 3, 4, 5};
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 
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   CeedQFunctionContextCreate(ceed, &ctx);
56   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(ctx_data), &ctx_data);
57   CeedQFunctionSetContext(qf_mass, ctx);
58   {
59     in[0]  = w;
60     in[1]  = u;
61     out[0] = v;
62     CeedQFunctionApply(qf_mass, q, in, out);
63   }
64 
65   // Verify result
66   {
67     const CeedScalar *v_array;
68 
69     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
70     for (CeedInt i = 0; i < q; i++) {
71       if (fabs(ctx_data[4] * v_true[i] - v_array[i]) > 100. * CEED_EPSILON) {
72         // LCOV_EXCL_START
73         printf("[%" CeedInt_FMT "] v %f != v_true %f\n", i, v_array[i], ctx_data[4] * v_true[i]);
74         // LCOV_EXCL_STOP
75       }
76     }
77     CeedVectorRestoreArrayRead(v, &v_array);
78   }
79 
80   CeedVectorDestroy(&w);
81   CeedVectorDestroy(&u);
82   CeedVectorDestroy(&v);
83   CeedVectorDestroy(&q_data);
84   CeedQFunctionDestroy(&qf_setup);
85   CeedQFunctionDestroy(&qf_mass);
86   CeedQFunctionContextDestroy(&ctx);
87   CeedDestroy(&ceed);
88   return 0;
89 }
90