xref: /libCEED/tests/t409-qfunction.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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
42b730f8bSJeremy L Thompson #include "t409-qfunction.h"
52b730f8bSJeremy L Thompson 
6441428dfSJeremy L Thompson #include <ceed.h>
7441428dfSJeremy L Thompson #include <math.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];
12*4fee36f0SJeremy L Thompson   CeedVector           u, v;
13441428dfSJeremy L Thompson   CeedQFunction        qf;
14441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
15*4fee36f0SJeremy L Thompson   CeedInt              q           = 8;
16441428dfSJeremy L Thompson   bool                 is_writable = true;
17441428dfSJeremy L Thompson   CeedScalar           ctx_data[5] = {1, 2, 3, 4, 5};
18441428dfSJeremy L Thompson 
19441428dfSJeremy L Thompson   CeedInit(argv[1], &ceed);
20441428dfSJeremy L Thompson 
21*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
22*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
23*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
24*4fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0.0);
25*4fee36f0SJeremy L Thompson 
26441428dfSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf);
27441428dfSJeremy L Thompson   CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP);
28441428dfSJeremy L Thompson   CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP);
29441428dfSJeremy L Thompson 
30441428dfSJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
312b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data);
32441428dfSJeremy L Thompson   CeedQFunctionSetContext(qf, ctx);
33441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
34441428dfSJeremy L Thompson   {
35*4fee36f0SJeremy L Thompson     in[0]  = u;
36*4fee36f0SJeremy L Thompson     out[0] = v;
37*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf, q, in, out);
38441428dfSJeremy L Thompson   }
39441428dfSJeremy L Thompson 
40*4fee36f0SJeremy L Thompson   {
41*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
42*4fee36f0SJeremy L Thompson 
43*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
44*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++)
45*4fee36f0SJeremy L Thompson       if (fabs(v_array[i] - ctx_data[1]) > 100. * CEED_EPSILON)
46441428dfSJeremy L Thompson         // LCOV_EXCL_START
47*4fee36f0SJeremy L Thompson         printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v_array[i]);
48441428dfSJeremy L Thompson     // LCOV_EXCL_STOP
49*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
50*4fee36f0SJeremy L Thompson   }
51441428dfSJeremy L Thompson 
52441428dfSJeremy L Thompson   // Check for written context data
53441428dfSJeremy L Thompson   CeedScalar *ctx_data_new;
54441428dfSJeremy L Thompson   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
55441428dfSJeremy L Thompson   if (ctx_data_new[0] != 42)
56441428dfSJeremy L Thompson     // LCOV_EXCL_START
57441428dfSJeremy L Thompson     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
58441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
59441428dfSJeremy L Thompson   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
60441428dfSJeremy L Thompson 
61441428dfSJeremy L Thompson   // Assert that context will not be written
62441428dfSJeremy L Thompson   // Note: The interface cannot enforce this in user code
63441428dfSJeremy L Thompson   //   so setting is_writable == false and then calling
64441428dfSJeremy L Thompson   //   CeedQFunctionApply to mutate the context would lead
652ae780f2SJeremy L Thompson   //   to inconsistent data on the GPU.
662ae780f2SJeremy L Thompson   //   Only the `/cpu/self/memcheck/*` backends verify that
672ae780f2SJeremy L Thompson   //   read-only access resulted in no changes to the context data
682ae780f2SJeremy L Thompson   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new);
692ae780f2SJeremy L Thompson   ctx_data_new[0] = 5;
702ae780f2SJeremy L Thompson   CeedQFunctionContextRestoreData(ctx, &ctx_data_new);
71441428dfSJeremy L Thompson   is_writable = false;
72441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
73441428dfSJeremy L Thompson   {
74*4fee36f0SJeremy L Thompson     in[0]  = u;
75*4fee36f0SJeremy L Thompson     out[0] = v;
762ae780f2SJeremy L Thompson     // Will only error in `/cpu/self/memcheck/*` backends
77*4fee36f0SJeremy L Thompson     CeedQFunctionApply(qf, q, in, out);
78441428dfSJeremy L Thompson   }
79441428dfSJeremy L Thompson 
80*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
81*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
82441428dfSJeremy L Thompson   CeedQFunctionDestroy(&qf);
83441428dfSJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
84441428dfSJeremy L Thompson   CeedDestroy(&ceed);
85441428dfSJeremy L Thompson   return 0;
86441428dfSJeremy L Thompson }
87