xref: /libCEED/tests/t409-qfunction.c (revision 600b7929b98f3d8efad5f619bace308a359a46af)
1441428dfSJeremy L Thompson /// @file
2441428dfSJeremy L Thompson /// Test creation, evaluation, and destruction for QFunction
3441428dfSJeremy L Thompson /// \test Test creation, evaluation, and destruction for QFunction
4441428dfSJeremy L Thompson #include <ceed.h>
5441428dfSJeremy L Thompson #include <math.h>
6441428dfSJeremy L Thompson 
7441428dfSJeremy L Thompson #include "t409-qfunction.h"
8441428dfSJeremy L Thompson 
9441428dfSJeremy L Thompson int main(int argc, char **argv) {
10441428dfSJeremy L Thompson   Ceed ceed;
11441428dfSJeremy L Thompson   CeedVector in[16], out[16];
12441428dfSJeremy L Thompson   CeedVector U, V;
13441428dfSJeremy L Thompson   CeedQFunction qf;
14441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
15441428dfSJeremy L Thompson   CeedInt Q = 8;
16441428dfSJeremy L Thompson   const CeedScalar *v;
17441428dfSJeremy L Thompson   bool is_writable = true;
18441428dfSJeremy L Thompson   CeedScalar ctx_data[5] = {1, 2, 3, 4, 5};
19441428dfSJeremy L Thompson 
20441428dfSJeremy L Thompson   CeedInit(argv[1], &ceed);
21441428dfSJeremy L Thompson 
22441428dfSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf);
23441428dfSJeremy L Thompson   CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP);
24441428dfSJeremy L Thompson   CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP);
25441428dfSJeremy L Thompson 
26441428dfSJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
27441428dfSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES,
28441428dfSJeremy L Thompson                               sizeof(ctx_data), &ctx_data);
29441428dfSJeremy L Thompson   CeedQFunctionSetContext(qf, ctx);
30441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
31441428dfSJeremy L Thompson 
32441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &U);
33441428dfSJeremy L Thompson   CeedVectorSetValue(U, 1.0);
34441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &V);
35441428dfSJeremy L Thompson   CeedVectorSetValue(V, 0.0);
36441428dfSJeremy L Thompson 
37441428dfSJeremy L Thompson   {
38441428dfSJeremy L Thompson     in[0] = U;
39441428dfSJeremy L Thompson     out[0] = V;
40441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
41441428dfSJeremy L Thompson   }
42441428dfSJeremy L Thompson 
43441428dfSJeremy L Thompson   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
44441428dfSJeremy L Thompson   for (CeedInt i=0; i<Q; i++)
45441428dfSJeremy L Thompson     if (fabs(v[i] - ctx_data[1]) > 100.*CEED_EPSILON)
46441428dfSJeremy L Thompson       // LCOV_EXCL_START
47*600b7929SJeremy L Thompson       printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v[i]);
48441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
49441428dfSJeremy L Thompson   CeedVectorRestoreArrayRead(V, &v);
50441428dfSJeremy L Thompson 
51441428dfSJeremy L Thompson   // Check for written context data
52441428dfSJeremy L Thompson   CeedScalar *ctx_data_new;
53441428dfSJeremy L Thompson   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
54441428dfSJeremy L Thompson   if (ctx_data_new[0] != 42)
55441428dfSJeremy L Thompson     // LCOV_EXCL_START
56441428dfSJeremy L Thompson     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
57441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
58441428dfSJeremy L Thompson   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
59441428dfSJeremy L Thompson 
60441428dfSJeremy L Thompson   // Assert that context will not be written
61441428dfSJeremy L Thompson   // Note: The interface cannot enforce this in user code
62441428dfSJeremy L Thompson   //   so setting is_writable == false and then calling
63441428dfSJeremy L Thompson   //   CeedQFunctionApply to mutate the context would lead
64441428dfSJeremy L Thompson   //   to inconsistent data on the GPU
65441428dfSJeremy L Thompson   is_writable = false;
66441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
67441428dfSJeremy L Thompson   {
68441428dfSJeremy L Thompson     in[0] = U;
69441428dfSJeremy L Thompson     out[0] = V;
70441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
71441428dfSJeremy L Thompson   }
72441428dfSJeremy L Thompson 
73441428dfSJeremy L Thompson   CeedVectorDestroy(&U);
74441428dfSJeremy L Thompson   CeedVectorDestroy(&V);
75441428dfSJeremy L Thompson   CeedQFunctionDestroy(&qf);
76441428dfSJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
77441428dfSJeremy L Thompson   CeedDestroy(&ceed);
78441428dfSJeremy L Thompson   return 0;
79441428dfSJeremy L Thompson }
80