xref: /libCEED/tests/t409-qfunction.c (revision 441428df2c2036d88c450263ab42e401d8838e73)
1*441428dfSJeremy L Thompson /// @file
2*441428dfSJeremy L Thompson /// Test creation, evaluation, and destruction for QFunction
3*441428dfSJeremy L Thompson /// \test Test creation, evaluation, and destruction for QFunction
4*441428dfSJeremy L Thompson #include <ceed.h>
5*441428dfSJeremy L Thompson #include <math.h>
6*441428dfSJeremy L Thompson 
7*441428dfSJeremy L Thompson #include "t409-qfunction.h"
8*441428dfSJeremy L Thompson 
9*441428dfSJeremy L Thompson int main(int argc, char **argv) {
10*441428dfSJeremy L Thompson   Ceed ceed;
11*441428dfSJeremy L Thompson   CeedVector in[16], out[16];
12*441428dfSJeremy L Thompson   CeedVector U, V;
13*441428dfSJeremy L Thompson   CeedQFunction qf;
14*441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
15*441428dfSJeremy L Thompson   CeedInt Q = 8;
16*441428dfSJeremy L Thompson   const CeedScalar *v;
17*441428dfSJeremy L Thompson   bool is_writable = true;
18*441428dfSJeremy L Thompson   CeedScalar ctx_data[5] = {1, 2, 3, 4, 5};
19*441428dfSJeremy L Thompson 
20*441428dfSJeremy L Thompson   CeedInit(argv[1], &ceed);
21*441428dfSJeremy L Thompson 
22*441428dfSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf);
23*441428dfSJeremy L Thompson   CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP);
24*441428dfSJeremy L Thompson   CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP);
25*441428dfSJeremy L Thompson 
26*441428dfSJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
27*441428dfSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES,
28*441428dfSJeremy L Thompson                               sizeof(ctx_data), &ctx_data);
29*441428dfSJeremy L Thompson   CeedQFunctionSetContext(qf, ctx);
30*441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
31*441428dfSJeremy L Thompson 
32*441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &U);
33*441428dfSJeremy L Thompson   CeedVectorSetValue(U, 1.0);
34*441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &V);
35*441428dfSJeremy L Thompson   CeedVectorSetValue(V, 0.0);
36*441428dfSJeremy L Thompson 
37*441428dfSJeremy L Thompson   {
38*441428dfSJeremy L Thompson     in[0] = U;
39*441428dfSJeremy L Thompson     out[0] = V;
40*441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
41*441428dfSJeremy L Thompson   }
42*441428dfSJeremy L Thompson 
43*441428dfSJeremy L Thompson   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
44*441428dfSJeremy L Thompson   for (CeedInt i=0; i<Q; i++)
45*441428dfSJeremy L Thompson     if (fabs(v[i] - ctx_data[1]) > 100.*CEED_EPSILON)
46*441428dfSJeremy L Thompson       // LCOV_EXCL_START
47*441428dfSJeremy L Thompson       printf("v[%d] %f != 2.0\n", i, v[i]);
48*441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
49*441428dfSJeremy L Thompson   CeedVectorRestoreArrayRead(V, &v);
50*441428dfSJeremy L Thompson 
51*441428dfSJeremy L Thompson   // Check for written context data
52*441428dfSJeremy L Thompson   CeedScalar *ctx_data_new;
53*441428dfSJeremy L Thompson   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
54*441428dfSJeremy L Thompson   if (ctx_data_new[0] != 42)
55*441428dfSJeremy L Thompson     // LCOV_EXCL_START
56*441428dfSJeremy L Thompson     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
57*441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
58*441428dfSJeremy L Thompson   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
59*441428dfSJeremy L Thompson 
60*441428dfSJeremy L Thompson   // Assert that context will not be written
61*441428dfSJeremy L Thompson   // Note: The interface cannot enforce this in user code
62*441428dfSJeremy L Thompson   //   so setting is_writable == false and then calling
63*441428dfSJeremy L Thompson   //   CeedQFunctionApply to mutate the context would lead
64*441428dfSJeremy L Thompson   //   to inconsistent data on the GPU
65*441428dfSJeremy L Thompson   is_writable = false;
66*441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
67*441428dfSJeremy L Thompson   {
68*441428dfSJeremy L Thompson     in[0] = U;
69*441428dfSJeremy L Thompson     out[0] = V;
70*441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
71*441428dfSJeremy L Thompson   }
72*441428dfSJeremy L Thompson 
73*441428dfSJeremy L Thompson   CeedVectorDestroy(&U);
74*441428dfSJeremy L Thompson   CeedVectorDestroy(&V);
75*441428dfSJeremy L Thompson   CeedQFunctionDestroy(&qf);
76*441428dfSJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
77*441428dfSJeremy L Thompson   CeedDestroy(&ceed);
78*441428dfSJeremy L Thompson   return 0;
79*441428dfSJeremy L Thompson }
80