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