xref: /libCEED/tests/t525-operator.c (revision 3668ca4b583f336c0c2dcc810e26e2ac50a514b8)
1d8dd9a91SJeremy L Thompson /// @file
2d8dd9a91SJeremy L Thompson /// Test setting QFunctionContext fields from Operator
3d8dd9a91SJeremy L Thompson /// \test Test setting QFunctionContext fields from Operator
4d8dd9a91SJeremy L Thompson #include <ceed.h>
5d8dd9a91SJeremy L Thompson #include <stddef.h>
6d8dd9a91SJeremy L Thompson #include "t500-operator.h"
7d8dd9a91SJeremy L Thompson 
8d8dd9a91SJeremy L Thompson typedef struct {
9d8dd9a91SJeremy L Thompson   int count;
10d8dd9a91SJeremy L Thompson   double other;
11d8dd9a91SJeremy L Thompson } TestContext1;
12d8dd9a91SJeremy L Thompson 
13d8dd9a91SJeremy L Thompson typedef struct {
14d8dd9a91SJeremy L Thompson   double time;
15d8dd9a91SJeremy L Thompson   double other;
16d8dd9a91SJeremy L Thompson } TestContext2;
17d8dd9a91SJeremy L Thompson 
18d8dd9a91SJeremy L Thompson int main(int argc, char **argv) {
19d8dd9a91SJeremy L Thompson   Ceed ceed;
20d8dd9a91SJeremy L Thompson   CeedQFunctionContext qf_ctx_sub_1, qf_ctx_sub_2;
21*3668ca4bSJeremy L Thompson   CeedContextFieldLabel count_label, other_label, time_label;
22d8dd9a91SJeremy L Thompson   CeedQFunction qf_sub_1, qf_sub_2;
23d8dd9a91SJeremy L Thompson   CeedOperator op_sub_1, op_sub_2, op_composite;
24*3668ca4bSJeremy L Thompson 
25d8dd9a91SJeremy L Thompson   TestContext1 ctx_data_1 = {
26d8dd9a91SJeremy L Thompson     .count = 42,
27d8dd9a91SJeremy L Thompson     .other = -3.0,
28d8dd9a91SJeremy L Thompson   };
29d8dd9a91SJeremy L Thompson   TestContext2 ctx_data_2 = {
30d8dd9a91SJeremy L Thompson     .time = 1.0,
31d8dd9a91SJeremy L Thompson     .other = -3.0,
32d8dd9a91SJeremy L Thompson   };
33d8dd9a91SJeremy L Thompson 
34d8dd9a91SJeremy L Thompson   CeedInit(argv[1], &ceed);
35d8dd9a91SJeremy L Thompson 
36d8dd9a91SJeremy L Thompson   // First sub-operator
37d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_1);
38d8dd9a91SJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_1, CEED_MEM_HOST, CEED_USE_POINTER,
39d8dd9a91SJeremy L Thompson                               sizeof(TestContext1), &ctx_data_1);
40d8dd9a91SJeremy L Thompson   CeedQFunctionContextRegisterInt32(qf_ctx_sub_1, "count", offsetof(TestContext1,
41d8dd9a91SJeremy L Thompson                                     count), "some sort of counter");
42d8dd9a91SJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_1, "other", offsetof(TestContext1,
43d8dd9a91SJeremy L Thompson                                      other), "some other value");
44d8dd9a91SJeremy L Thompson 
45d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_sub_1);
46d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_1, qf_ctx_sub_1);
47d8dd9a91SJeremy L Thompson 
48d8dd9a91SJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_1, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
49d8dd9a91SJeremy L Thompson                      &op_sub_1);
50d8dd9a91SJeremy L Thompson 
51d8dd9a91SJeremy L Thompson   // Check setting field in operator
52*3668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_sub_1, "count", &count_label);
53*3668ca4bSJeremy L Thompson   CeedOperatorContextSetInt32(op_sub_1, count_label, 43);
54d8dd9a91SJeremy L Thompson   if (ctx_data_1.count != 43)
55d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
56d8dd9a91SJeremy L Thompson     printf("Incorrect context data for count: %d != 43", ctx_data_1.count);
57d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
58d8dd9a91SJeremy L Thompson 
59d8dd9a91SJeremy L Thompson   // Second sub-operator
60d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
61d8dd9a91SJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER,
62d8dd9a91SJeremy L Thompson                               sizeof(TestContext2), &ctx_data_2);
63d8dd9a91SJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time", offsetof(TestContext2,
64d8dd9a91SJeremy L Thompson                                      time), "current time");
65d8dd9a91SJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other", offsetof(TestContext2,
66d8dd9a91SJeremy L Thompson                                      other), "some other value");
67d8dd9a91SJeremy L Thompson 
68d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
69d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
70d8dd9a91SJeremy L Thompson 
71d8dd9a91SJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
72d8dd9a91SJeremy L Thompson                      &op_sub_2);
73d8dd9a91SJeremy L Thompson 
74d8dd9a91SJeremy L Thompson   // Composite operator
75d8dd9a91SJeremy L Thompson   CeedCompositeOperatorCreate(ceed, &op_composite);
76d8dd9a91SJeremy L Thompson   CeedCompositeOperatorAddSub(op_composite, op_sub_1);
77d8dd9a91SJeremy L Thompson   CeedCompositeOperatorAddSub(op_composite, op_sub_2);
78d8dd9a91SJeremy L Thompson 
79d8dd9a91SJeremy L Thompson   // Check setting field in context of single sub-operator for composite operator
80*3668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "time", &time_label);
81*3668ca4bSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, time_label, 2.0);
82d8dd9a91SJeremy L Thompson   if (ctx_data_2.time != 2.0)
83d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
84d8dd9a91SJeremy L Thompson     printf("Incorrect context data for time: %f != 2.0", ctx_data_2.time);
85d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
86d8dd9a91SJeremy L Thompson 
87d8dd9a91SJeremy L Thompson   // Check setting field in context of multiple sub-operators for composite operator
88*3668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
89*3668ca4bSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, other_label, 9000.);
90d8dd9a91SJeremy L Thompson   if (ctx_data_1.other != 9000.0)
91d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
92d8dd9a91SJeremy L Thompson     printf("Incorrect context data for other: %f != 2.0", ctx_data_1.other);
93d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
94d8dd9a91SJeremy L Thompson   if (ctx_data_2.other != 9000.0)
95d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
96d8dd9a91SJeremy L Thompson     printf("Incorrect context data for other: %f != 2.0", ctx_data_2.other);
97d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
98d8dd9a91SJeremy L Thompson 
99d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
100d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
101d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_1);
102d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_2);
103d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_1);
104d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_2);
105d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_composite);
106d8dd9a91SJeremy L Thompson   CeedDestroy(&ceed);
107d8dd9a91SJeremy L Thompson   return 0;
108d8dd9a91SJeremy L Thompson }
109