xref: /libCEED/tests/t405-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
177841947SLeila Ghaffari /// @file
277841947SLeila Ghaffari /// Test QFunction helper macro
377841947SLeila Ghaffari /// \test Test QFunction helper macro
477841947SLeila Ghaffari #include "t405-qfunction.h"
577841947SLeila Ghaffari 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson 
877841947SLeila Ghaffari int main(int argc, char **argv) {
977841947SLeila Ghaffari   Ceed          ceed;
1077841947SLeila Ghaffari   CeedVector    in[16], out[16];
11*4fee36f0SJeremy L Thompson   CeedVector    q_data, w, u, v;
1277841947SLeila Ghaffari   CeedQFunction qf_setup, qf_mass;
13*4fee36f0SJeremy L Thompson   CeedInt       q = 8;
14*4fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
1577841947SLeila Ghaffari 
1677841947SLeila Ghaffari   CeedInit(argv[1], &ceed);
1777841947SLeila Ghaffari 
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 
3777841947SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
3877841947SLeila Ghaffari   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
3977841947SLeila Ghaffari   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   }
4577841947SLeila Ghaffari 
4677841947SLeila Ghaffari   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
4777841947SLeila Ghaffari   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
4877841947SLeila Ghaffari   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
4977841947SLeila Ghaffari   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
5077841947SLeila Ghaffari   {
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);
5577841947SLeila Ghaffari   }
56*4fee36f0SJeremy L Thompson 
57*4fee36f0SJeremy L Thompson   // Verify result
5877841947SLeila Ghaffari   {
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 (2 * v_true[i] != v_array[i]) printf("[%" CeedInt_FMT "] v %f != vv %f\n", i, 2 * v_true[i], v_array[i]);
64*4fee36f0SJeremy L Thompson     }
65*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
6677841947SLeila Ghaffari   }
6777841947SLeila Ghaffari 
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);
7277841947SLeila Ghaffari   CeedQFunctionDestroy(&qf_setup);
7377841947SLeila Ghaffari   CeedQFunctionDestroy(&qf_mass);
7477841947SLeila Ghaffari   CeedDestroy(&ceed);
7577841947SLeila Ghaffari   return 0;
7677841947SLeila Ghaffari }
77