xref: /libCEED/tests/t406-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1ccec5866SJeremy L Thompson /// @file
2ccec5866SJeremy L Thompson /// Test QFunction helper macro
3ccec5866SJeremy L Thompson /// \test Test QFunction helper macro
42b730f8bSJeremy L Thompson #include "t406-qfunction.h"
52b730f8bSJeremy L Thompson 
6ccec5866SJeremy L Thompson #include <ceed.h>
7ccec5866SJeremy L Thompson #include <math.h>
8ccec5866SJeremy L Thompson #include <stdlib.h>
9ccec5866SJeremy L Thompson #include <string.h>
10ccec5866SJeremy L Thompson 
11ccec5866SJeremy L Thompson int main(int argc, char **argv) {
12ccec5866SJeremy L Thompson   Ceed          ceed;
13ccec5866SJeremy L Thompson   CeedVector    in[16], out[16];
14*4fee36f0SJeremy L Thompson   CeedVector    q_data, w, u, v;
15ccec5866SJeremy L Thompson   CeedQFunction qf_setup, qf_mass;
16*4fee36f0SJeremy L Thompson   CeedInt       q = 8;
17*4fee36f0SJeremy L Thompson   CeedScalar    v_true[q];
18ccec5866SJeremy L Thompson 
19ccec5866SJeremy L Thompson   CeedInit(argv[1], &ceed);
20ccec5866SJeremy L Thompson 
21*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &w);
22*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
23*4fee36f0SJeremy L Thompson   {
24*4fee36f0SJeremy L Thompson     CeedScalar w_array[q], u_array[q];
25*4fee36f0SJeremy L Thompson 
26*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
27*4fee36f0SJeremy L Thompson       CeedScalar x = 2. * i / (q - 1) - 1;
28*4fee36f0SJeremy L Thompson       w_array[i]   = 1 - x * x;
29*4fee36f0SJeremy L Thompson       u_array[i]   = 2 + 3 * x + 5 * x * x;
30*4fee36f0SJeremy L Thompson       v_true[i]    = w_array[i] * u_array[i];
31*4fee36f0SJeremy L Thompson     }
32*4fee36f0SJeremy L Thompson     CeedVectorSetArray(w, CEED_MEM_HOST, CEED_COPY_VALUES, w_array);
33*4fee36f0SJeremy L Thompson     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_COPY_VALUES, u_array);
34*4fee36f0SJeremy L Thompson   }
35*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
36*4fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0);
37*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &q_data);
38*4fee36f0SJeremy L Thompson   CeedVectorSetValue(q_data, 0);
39*4fee36f0SJeremy L Thompson 
40ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
41ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "w", 1, CEED_EVAL_WEIGHT);
42ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
43*4fee36f0SJeremy L Thompson   {
44*4fee36f0SJeremy L Thompson     in[0]  = w;
45*4fee36f0SJeremy L Thompson     out[0] = q_data;
46*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_setup, q, in, out);
47*4fee36f0SJeremy L Thompson   }
48ccec5866SJeremy L Thompson 
49ccec5866SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
50ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
51ccec5866SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
52ccec5866SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
53ccec5866SJeremy L Thompson   {
54*4fee36f0SJeremy L Thompson     in[0]  = w;
55*4fee36f0SJeremy L Thompson     in[1]  = u;
56*4fee36f0SJeremy L Thompson     out[0] = v;
57*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf_mass, q, in, out);
58ccec5866SJeremy L Thompson   }
59*4fee36f0SJeremy L Thompson 
60*4fee36f0SJeremy L Thompson   // Verify results
61ccec5866SJeremy L Thompson   {
62*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
63*4fee36f0SJeremy L Thompson 
64*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
65*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++) {
66*4fee36f0SJeremy L Thompson       if (fabs(5 * v_true[i] * sqrt(2.) - v_array[i]) > 1E3 * CEED_EPSILON)
67*4fee36f0SJeremy L Thompson         printf("[%" CeedInt_FMT "] v_true %f != v %f\n", i, 5 * v_true[i] * sqrt(2.), v_array[i]);
68*4fee36f0SJeremy L Thompson     }
69*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
70ccec5866SJeremy L Thompson   }
71ccec5866SJeremy L Thompson 
72*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&w);
73*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
74*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
75*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
76ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
77ccec5866SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
78ccec5866SJeremy L Thompson   CeedDestroy(&ceed);
79ccec5866SJeremy L Thompson   return 0;
80ccec5866SJeremy L Thompson }
81