xref: /libCEED/tests/t409-qfunction.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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>
8*49aac155SJeremy L Thompson #include <stdio.h>
9441428dfSJeremy L Thompson 
10441428dfSJeremy L Thompson int main(int argc, char **argv) {
11441428dfSJeremy L Thompson   Ceed                 ceed;
12441428dfSJeremy L Thompson   CeedVector           in[16], out[16];
134fee36f0SJeremy L Thompson   CeedVector           u, v;
14441428dfSJeremy L Thompson   CeedQFunction        qf;
15441428dfSJeremy L Thompson   CeedQFunctionContext ctx;
164fee36f0SJeremy L Thompson   CeedInt              q           = 8;
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 
224fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &u);
234fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
244fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, q, &v);
254fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0.0);
264fee36f0SJeremy L Thompson 
27441428dfSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, scale, scale_loc, &qf);
28441428dfSJeremy L Thompson   CeedQFunctionAddInput(qf, "u", 1, CEED_EVAL_INTERP);
29441428dfSJeremy L Thompson   CeedQFunctionAddOutput(qf, "v", 1, CEED_EVAL_INTERP);
30441428dfSJeremy L Thompson 
31441428dfSJeremy L Thompson   CeedQFunctionContextCreate(ceed, &ctx);
322b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data);
33441428dfSJeremy L Thompson   CeedQFunctionSetContext(qf, ctx);
34441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
35441428dfSJeremy L Thompson   {
364fee36f0SJeremy L Thompson     in[0]  = u;
374fee36f0SJeremy L Thompson     out[0] = v;
384fee36f0SJeremy L Thompson     CeedQFunctionApply(qf, q, in, out);
39441428dfSJeremy L Thompson   }
40441428dfSJeremy L Thompson 
414fee36f0SJeremy L Thompson   {
424fee36f0SJeremy L Thompson     const CeedScalar *v_array;
434fee36f0SJeremy L Thompson 
444fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
454fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < q; i++)
464fee36f0SJeremy L Thompson       if (fabs(v_array[i] - ctx_data[1]) > 100. * CEED_EPSILON)
47441428dfSJeremy L Thompson         // LCOV_EXCL_START
484fee36f0SJeremy L Thompson         printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v_array[i]);
49441428dfSJeremy L Thompson     // LCOV_EXCL_STOP
504fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
514fee36f0SJeremy L Thompson   }
52441428dfSJeremy L Thompson 
53441428dfSJeremy L Thompson   // Check for written context data
54441428dfSJeremy L Thompson   CeedScalar *ctx_data_new;
55441428dfSJeremy L Thompson   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
56441428dfSJeremy L Thompson   if (ctx_data_new[0] != 42)
57441428dfSJeremy L Thompson     // LCOV_EXCL_START
58441428dfSJeremy L Thompson     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
59441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
60441428dfSJeremy L Thompson   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
61441428dfSJeremy L Thompson 
62441428dfSJeremy L Thompson   // Assert that context will not be written
63441428dfSJeremy L Thompson   // Note: The interface cannot enforce this in user code
64441428dfSJeremy L Thompson   //   so setting is_writable == false and then calling
65441428dfSJeremy L Thompson   //   CeedQFunctionApply to mutate the context would lead
662ae780f2SJeremy L Thompson   //   to inconsistent data on the GPU.
672ae780f2SJeremy L Thompson   //   Only the `/cpu/self/memcheck/*` backends verify that
682ae780f2SJeremy L Thompson   //   read-only access resulted in no changes to the context data
692ae780f2SJeremy L Thompson   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new);
702ae780f2SJeremy L Thompson   ctx_data_new[0] = 5;
712ae780f2SJeremy L Thompson   CeedQFunctionContextRestoreData(ctx, &ctx_data_new);
72441428dfSJeremy L Thompson   is_writable = false;
73441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
74441428dfSJeremy L Thompson   {
754fee36f0SJeremy L Thompson     in[0]  = u;
764fee36f0SJeremy L Thompson     out[0] = v;
772ae780f2SJeremy L Thompson     // Will only error in `/cpu/self/memcheck/*` backends
784fee36f0SJeremy L Thompson     CeedQFunctionApply(qf, q, in, out);
79441428dfSJeremy L Thompson   }
80441428dfSJeremy L Thompson 
814fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
824fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
83441428dfSJeremy L Thompson   CeedQFunctionDestroy(&qf);
84441428dfSJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
85441428dfSJeremy L Thompson   CeedDestroy(&ceed);
86441428dfSJeremy L Thompson   return 0;
87441428dfSJeremy L Thompson }
88