xref: /libCEED/tests/t410-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1 /// @file
2 /// Test creation, evaluation, and destruction for QFunction by name
3 /// \test Test creation, evaluation, and destruction for QFunction by name
4 #include <ceed.h>
5 
6 int main(int argc, char **argv) {
7   Ceed          ceed;
8   CeedVector    in[16], out[16];
9   CeedVector    q_data, dx, w, u, v;
10   CeedQFunction qf_setup, qf_mass;
11   CeedInt       q = 8;
12   CeedScalar    v_true[q];
13 
14   CeedInit(argv[1], &ceed);
15 
16   CeedVectorCreate(ceed, q, &dx);
17   CeedVectorCreate(ceed, q, &w);
18   CeedVectorCreate(ceed, q, &u);
19   {
20     CeedScalar dx_array[q], w_array[q], u_array[q];
21 
22     for (CeedInt i = 0; i < q; i++) {
23       CeedScalar x = 2. * i / (q - 1) - 1;
24       dx_array[i]  = 1;
25       w_array[i]   = 1 - x * x;
26       u_array[i]   = 2 + 3 * x + 5 * x * x;
27       v_true[i]    = w_array[i] * u_array[i];
28     }
29     CeedVectorSetArray(dx, CEED_MEM_HOST, CEED_COPY_VALUES, dx_array);
30     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
31     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
32   }
33   CeedVectorCreate(ceed, q, &v);
34   CeedVectorSetValue(v, 0);
35   CeedVectorCreate(ceed, q, &q_data);
36   CeedVectorSetValue(q_data, 0);
37 
38   CeedQFunctionCreateInteriorByName(ceed, "Mass1DBuild", &qf_setup);
39   {
40     in[0]  = dx;
41     in[1]  = w;
42     out[0] = q_data;
43     CeedQFunctionApply(qf_setup, q, in, out);
44   }
45 
46   CeedQFunctionCreateInteriorByName(ceed, "MassApply", &qf_mass);
47   {
48     in[0]  = w;
49     in[1]  = u;
50     out[0] = v;
51     CeedQFunctionApply(qf_mass, q, in, out);
52   }
53 
54   // Verify results
55   {
56     const CeedScalar *v_array;
57 
58     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
59     for (CeedInt i = 0; i < q; i++) {
60       if (v_true[i] != v_array[i]) printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, v_true[i], v_array[i]);
61     }
62     CeedVectorRestoreArrayRead(v, &v_array);
63   }
64 
65   CeedVectorDestroy(&dx);
66   CeedVectorDestroy(&w);
67   CeedVectorDestroy(&u);
68   CeedVectorDestroy(&v);
69   CeedVectorDestroy(&q_data);
70   CeedQFunctionDestroy(&qf_setup);
71   CeedQFunctionDestroy(&qf_mass);
72   CeedDestroy(&ceed);
73   return 0;
74 }
75