xref: /libCEED/tests/t400-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
14411cf47Sjeremylt /// @file
2d1d35e2fSjeremylt /// Test creation, evaluation, and destruction for QFunction
3d1d35e2fSjeremylt /// \test Test creation, evaluation, and destruction for QFunction
44d537eeaSYohann #include "t400-qfunction.h"
557c64913Sjeremylt 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson 
857c64913Sjeremylt int main(int argc, char **argv) {
957c64913Sjeremylt   Ceed          ceed;
10aedaa0e5Sjeremylt   CeedVector    in[16], out[16];
11*4fee36f0SJeremy L Thompson   CeedVector    q_data, w, u, v;
1257c64913Sjeremylt   CeedQFunction qf_setup, qf_mass;
13*4fee36f0SJeremy L Thompson   CeedInt       q = 8;
14*4fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
1557c64913Sjeremylt 
1657c64913Sjeremylt   CeedInit(argv[1], &ceed);
17aedaa0e5Sjeremylt 
18*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
19*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
20*4fee36f0SJeremy L Thompson   {
21*4fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
22*4fee36f0SJeremy L Thompson 
23*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
24*4fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
25*4fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
26*4fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
27*4fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
28*4fee36f0SJeremy L Thompson     }
29*4fee36f0SJeremy L Thompson     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
30*4fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
31*4fee36f0SJeremy L Thompson   }
32*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
33*4fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
34*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
35*4fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
36*4fee36f0SJeremy L Thompson 
374d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
3884e209c4Sjeremylt   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
3984e209c4Sjeremylt   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
40*4fee36f0SJeremy L Thompson   {
41*4fee36f0SJeremy L Thompson     in[0]  = w;
42*4fee36f0SJeremy L Thompson     out[0] = q_data;
43*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
44*4fee36f0SJeremy L Thompson   }
4557c64913Sjeremylt 
464d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
4784e209c4Sjeremylt   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
4857c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
4957c64913Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
5057c64913Sjeremylt   {
51*4fee36f0SJeremy L Thompson     in[0]  = w;
52*4fee36f0SJeremy L Thompson     in[1]  = u;
53*4fee36f0SJeremy L Thompson     out[0] = v;
54*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
5557c64913Sjeremylt   }
56*4fee36f0SJeremy L Thompson 
57*4fee36f0SJeremy L Thompson   // Verify result
5857c64913Sjeremylt   {
59*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
60*4fee36f0SJeremy L Thompson 
61*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
62*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
63*4fee36f0SJeremy L Thompson       if (v_array[i] != v_true[i]) printf("[%" CeedInt_FMT "] v %f != v_true %f\n", i, v_array[i], v_true[i]);
64*4fee36f0SJeremy L Thompson     }
65*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
6657c64913Sjeremylt   }
67aedaa0e5Sjeremylt 
68*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
69*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
70*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
71*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
7257c64913Sjeremylt   CeedQFunctionDestroy(&qf_setup);
7357c64913Sjeremylt   CeedQFunctionDestroy(&qf_mass);
7457c64913Sjeremylt   CeedDestroy(&ceed);
7557c64913Sjeremylt   return 0;
7657c64913Sjeremylt }
77