xref: /libCEED/tests/t401-qfunction.c (revision 17b0d5c66e710b5b55588e66a2830e7df114beeb)
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 
9 int main(int argc, char **argv) {
10   Ceed                 ceed;
11   CeedVector           in[16], out[16];
12   CeedVector           q_data, w, u, v;
13   CeedQFunction        qf_setup, qf_mass;
14   CeedQFunctionContext ctx;
15   CeedInt              q = 8;
16   CeedScalar           v_true[q], ctx_data[5] = {1, 2, 3, 4, 5};
17 
18   CeedInit(argv[1], &ceed);
19 
20   CeedVectorCreate(ceed, q, &w);
21   CeedVectorCreate(ceed, q, &u);
22   {
23     CeedScalar w_array[q], u_array[q];
24 
25     for (CeedInt i = 0; i < q; i++) {
26       CeedScalar x = 2. * i / (q - 1) - 1;
27       w_array[i]   = 1 - x * x;
28       u_array[i]   = 2 + 3 * x + 5 * x * x;
29       v_true[i]    = w_array[i] * u_array[i];
30     }
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   CeedQFunctionContextCreate(ceed, &ctx);
55   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(ctx_data), &ctx_data);
56   CeedQFunctionSetContext(qf_mass, ctx);
57   {
58     in[0]  = w;
59     in[1]  = u;
60     out[0] = v;
61     CeedQFunctionApply(qf_mass, q, in, out);
62   }
63 
64   // Verify result
65   {
66     const CeedScalar *v_array;
67 
68     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
69     for (CeedInt i = 0; i < q; i++) {
70       if (fabs(ctx_data[4] * v_true[i] - v_array[i]) > 100. * CEED_EPSILON) {
71         // LCOV_EXCL_START
72         printf("[%" CeedInt_FMT "] v %f != v_true %f\n", i, v_array[i], ctx_data[4] * v_true[i]);
73         // LCOV_EXCL_STOP
74       }
75     }
76     CeedVectorRestoreArrayRead(v, &v_array);
77   }
78 
79   CeedVectorDestroy(&w);
80   CeedVectorDestroy(&u);
81   CeedVectorDestroy(&v);
82   CeedVectorDestroy(&q_data);
83   CeedQFunctionDestroy(&qf_setup);
84   CeedQFunctionDestroy(&qf_mass);
85   CeedQFunctionContextDestroy(&ctx);
86   CeedDestroy(&ceed);
87   return 0;
88 }
89