xref: /libCEED/tests/t409-qfunction.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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
4*2b730f8bSJeremy L Thompson #include "t409-qfunction.h"
5*2b730f8bSJeremy 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];
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);
27*2b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(ctx, CEED_MEM_HOST, CEED_COPY_VALUES, sizeof(ctx_data), &ctx_data);
28441428dfSJeremy L Thompson   CeedQFunctionSetContext(qf, ctx);
29441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
30441428dfSJeremy L Thompson 
31441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &U);
32441428dfSJeremy L Thompson   CeedVectorSetValue(U, 1.0);
33441428dfSJeremy L Thompson   CeedVectorCreate(ceed, Q, &V);
34441428dfSJeremy L Thompson   CeedVectorSetValue(V, 0.0);
35441428dfSJeremy L Thompson 
36441428dfSJeremy L Thompson   {
37441428dfSJeremy L Thompson     in[0]  = U;
38441428dfSJeremy L Thompson     out[0] = V;
39441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
40441428dfSJeremy L Thompson   }
41441428dfSJeremy L Thompson 
42441428dfSJeremy L Thompson   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
43441428dfSJeremy L Thompson   for (CeedInt i = 0; i < Q; i++)
44441428dfSJeremy L Thompson     if (fabs(v[i] - ctx_data[1]) > 100. * CEED_EPSILON)
45441428dfSJeremy L Thompson       // LCOV_EXCL_START
46600b7929SJeremy L Thompson       printf("v[%" CeedInt_FMT "] %f != 2.0\n", i, v[i]);
47441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
48441428dfSJeremy L Thompson   CeedVectorRestoreArrayRead(V, &v);
49441428dfSJeremy L Thompson 
50441428dfSJeremy L Thompson   // Check for written context data
51441428dfSJeremy L Thompson   CeedScalar *ctx_data_new;
52441428dfSJeremy L Thompson   CeedQFunctionContextGetDataRead(ctx, CEED_MEM_HOST, &ctx_data_new);
53441428dfSJeremy L Thompson   if (ctx_data_new[0] != 42)
54441428dfSJeremy L Thompson     // LCOV_EXCL_START
55441428dfSJeremy L Thompson     printf("Context data not written: %f != 42\n", ctx_data_new[0]);
56441428dfSJeremy L Thompson   // LCOV_EXCL_STOP
57441428dfSJeremy L Thompson   CeedQFunctionContextRestoreDataRead(ctx, &ctx_data_new);
58441428dfSJeremy L Thompson 
59441428dfSJeremy L Thompson   // Assert that context will not be written
60441428dfSJeremy L Thompson   // Note: The interface cannot enforce this in user code
61441428dfSJeremy L Thompson   //   so setting is_writable == false and then calling
62441428dfSJeremy L Thompson   //   CeedQFunctionApply to mutate the context would lead
632ae780f2SJeremy L Thompson   //   to inconsistent data on the GPU.
642ae780f2SJeremy L Thompson   //   Only the `/cpu/self/memcheck/*` backends verify that
652ae780f2SJeremy L Thompson   //   read-only access resulted in no changes to the context data
662ae780f2SJeremy L Thompson   CeedQFunctionContextGetData(ctx, CEED_MEM_HOST, &ctx_data_new);
672ae780f2SJeremy L Thompson   ctx_data_new[0] = 5;
682ae780f2SJeremy L Thompson   CeedQFunctionContextRestoreData(ctx, &ctx_data_new);
69441428dfSJeremy L Thompson   is_writable = false;
70441428dfSJeremy L Thompson   CeedQFunctionSetContextWritable(qf, is_writable);
71441428dfSJeremy L Thompson   {
72441428dfSJeremy L Thompson     in[0]  = U;
73441428dfSJeremy L Thompson     out[0] = V;
742ae780f2SJeremy L Thompson     // Will only error in `/cpu/self/memcheck/*` backends
75441428dfSJeremy L Thompson     CeedQFunctionApply(qf, Q, in, out);
76441428dfSJeremy L Thompson   }
77441428dfSJeremy L Thompson 
78441428dfSJeremy L Thompson   CeedVectorDestroy(&U);
79441428dfSJeremy L Thompson   CeedVectorDestroy(&V);
80441428dfSJeremy L Thompson   CeedQFunctionDestroy(&qf);
81441428dfSJeremy L Thompson   CeedQFunctionContextDestroy(&ctx);
82441428dfSJeremy L Thompson   CeedDestroy(&ceed);
83441428dfSJeremy L Thompson   return 0;
84441428dfSJeremy L Thompson }
85