xref: /libCEED/tests/t525-operator.c (revision f2adece38a6bee62dc43f5afefd84530dab6dc2d)
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*f2adece3SJeremy L Thompson   CeedContextFieldLabel count_label, other_label, time_label, bad_label;
22d8dd9a91SJeremy L Thompson   CeedQFunction qf_sub_1, qf_sub_2;
23d8dd9a91SJeremy L Thompson   CeedOperator op_sub_1, op_sub_2, op_composite;
243668ca4bSJeremy 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);
407bfe0f0eSJeremy L Thompson   CeedQFunctionContextRegisterInt32(qf_ctx_sub_1, "count",
417bfe0f0eSJeremy L Thompson                                     offsetof(TestContext1, count),
427bfe0f0eSJeremy L Thompson                                     1, "some sort of counter");
437bfe0f0eSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_1, "other",
447bfe0f0eSJeremy L Thompson                                      offsetof(TestContext1, other),
457bfe0f0eSJeremy L Thompson                                      1, "some other value");
46d8dd9a91SJeremy L Thompson 
47d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_sub_1);
48d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_1, qf_ctx_sub_1);
49d8dd9a91SJeremy L Thompson 
50d8dd9a91SJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_1, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
51d8dd9a91SJeremy L Thompson                      &op_sub_1);
52d8dd9a91SJeremy L Thompson 
53d8dd9a91SJeremy L Thompson   // Check setting field in operator
543668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_sub_1, "count", &count_label);
557bfe0f0eSJeremy L Thompson   int value_count = 43;
567bfe0f0eSJeremy L Thompson   CeedOperatorContextSetInt32(op_sub_1, count_label, &value_count);
57d8dd9a91SJeremy L Thompson   if (ctx_data_1.count != 43)
58d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
59d8dd9a91SJeremy L Thompson     printf("Incorrect context data for count: %d != 43", ctx_data_1.count);
60d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
61d8dd9a91SJeremy L Thompson 
62d8dd9a91SJeremy L Thompson   // Second sub-operator
63d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
64d8dd9a91SJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER,
65d8dd9a91SJeremy L Thompson                               sizeof(TestContext2), &ctx_data_2);
667bfe0f0eSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time",
677bfe0f0eSJeremy L Thompson                                      offsetof(TestContext2, time),
687bfe0f0eSJeremy L Thompson                                      1, "current time");
697bfe0f0eSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other",
707bfe0f0eSJeremy L Thompson                                      offsetof(TestContext2, other),
717bfe0f0eSJeremy L Thompson                                      1, "some other value");
72d8dd9a91SJeremy L Thompson 
73d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
74d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
75d8dd9a91SJeremy L Thompson 
76d8dd9a91SJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
77d8dd9a91SJeremy L Thompson                      &op_sub_2);
78d8dd9a91SJeremy L Thompson 
79d8dd9a91SJeremy L Thompson   // Composite operator
80d8dd9a91SJeremy L Thompson   CeedCompositeOperatorCreate(ceed, &op_composite);
81d8dd9a91SJeremy L Thompson   CeedCompositeOperatorAddSub(op_composite, op_sub_1);
82d8dd9a91SJeremy L Thompson   CeedCompositeOperatorAddSub(op_composite, op_sub_2);
83d8dd9a91SJeremy L Thompson 
84d8dd9a91SJeremy L Thompson   // Check setting field in context of single sub-operator for composite operator
853668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "time", &time_label);
867bfe0f0eSJeremy L Thompson   double value_time = 2.0;
877bfe0f0eSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, time_label, &value_time);
88d8dd9a91SJeremy L Thompson   if (ctx_data_2.time != 2.0)
89d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
90d8dd9a91SJeremy L Thompson     printf("Incorrect context data for time: %f != 2.0", ctx_data_2.time);
91d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
92d8dd9a91SJeremy L Thompson 
93d8dd9a91SJeremy L Thompson   // Check setting field in context of multiple sub-operators for composite operator
943668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
95dab60d25SJeremy L Thompson   // No issue requesting same label twice
96dab60d25SJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
977bfe0f0eSJeremy L Thompson   double value_other = 9000.;
987bfe0f0eSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, other_label, &value_other);
99d8dd9a91SJeremy L Thompson   if (ctx_data_1.other != 9000.0)
100d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
101d8dd9a91SJeremy L Thompson     printf("Incorrect context data for other: %f != 2.0", ctx_data_1.other);
102d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
103d8dd9a91SJeremy L Thompson   if (ctx_data_2.other != 9000.0)
104d8dd9a91SJeremy L Thompson     // LCOV_EXCL_START
105d8dd9a91SJeremy L Thompson     printf("Incorrect context data for other: %f != 2.0", ctx_data_2.other);
106d8dd9a91SJeremy L Thompson   // LCOV_EXCL_STOP
107d8dd9a91SJeremy L Thompson 
108*f2adece3SJeremy L Thompson   // Check requesting label for field that doesn't exist returns NULL
109*f2adece3SJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "bad", &bad_label);
110*f2adece3SJeremy L Thompson   if (bad_label)
111*f2adece3SJeremy L Thompson     // LCOV_EXCL_START
112*f2adece3SJeremy L Thompson     printf("Incorrect context label returned");
113*f2adece3SJeremy L Thompson   // LCOV_EXCL_STOP
114*f2adece3SJeremy L Thompson 
115d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
116d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
117d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_1);
118d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_2);
119d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_1);
120d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_2);
121d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_composite);
122d8dd9a91SJeremy L Thompson   CeedDestroy(&ceed);
123d8dd9a91SJeremy L Thompson   return 0;
124d8dd9a91SJeremy L Thompson }
125