xref: /libCEED/tests/t525-operator.c (revision 0126412d32e96658dcd899fe3d8b989fdf201e32)
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>
62b730f8bSJeremy L Thompson 
7d8dd9a91SJeremy L Thompson #include "t500-operator.h"
8d8dd9a91SJeremy L Thompson 
9d8dd9a91SJeremy L Thompson typedef struct {
10d8dd9a91SJeremy L Thompson   int    count;
11d8dd9a91SJeremy L Thompson   double other;
12d8dd9a91SJeremy L Thompson } TestContext1;
13d8dd9a91SJeremy L Thompson 
14d8dd9a91SJeremy L Thompson typedef struct {
15d8dd9a91SJeremy L Thompson   double time;
16d8dd9a91SJeremy L Thompson   double other;
17d8dd9a91SJeremy L Thompson } TestContext2;
18d8dd9a91SJeremy L Thompson 
19d8dd9a91SJeremy L Thompson int main(int argc, char **argv) {
20d8dd9a91SJeremy L Thompson   Ceed                  ceed;
21d8dd9a91SJeremy L Thompson   CeedQFunctionContext  qf_ctx_sub_1, qf_ctx_sub_2;
22f2adece3SJeremy L Thompson   CeedContextFieldLabel count_label, other_label, time_label, bad_label;
23d8dd9a91SJeremy L Thompson   CeedQFunction         qf_sub_1, qf_sub_2;
24d8dd9a91SJeremy L Thompson   CeedOperator          op_sub_1, op_sub_2, op_composite;
253668ca4bSJeremy L Thompson 
26d8dd9a91SJeremy L Thompson   TestContext1 ctx_data_1 = {
27d8dd9a91SJeremy L Thompson       .count = 42,
28d8dd9a91SJeremy L Thompson       .other = -3.0,
29d8dd9a91SJeremy L Thompson   };
30d8dd9a91SJeremy L Thompson   TestContext2 ctx_data_2 = {
31d8dd9a91SJeremy L Thompson       .time  = 1.0,
32d8dd9a91SJeremy L Thompson       .other = -3.0,
33d8dd9a91SJeremy L Thompson   };
34d8dd9a91SJeremy L Thompson 
35d8dd9a91SJeremy L Thompson   CeedInit(argv[1], &ceed);
36d8dd9a91SJeremy L Thompson 
37d8dd9a91SJeremy L Thompson   // First sub-operator
38d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_1);
392b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_1, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext1), &ctx_data_1);
402b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterInt32(qf_ctx_sub_1, "count", offsetof(TestContext1, count), 1, "some sort of counter");
412b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_1, "other", offsetof(TestContext1, other), 1, "some other value");
42d8dd9a91SJeremy L Thompson 
43d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_sub_1);
44d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_1, qf_ctx_sub_1);
45d8dd9a91SJeremy L Thompson 
462b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_1, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_sub_1);
47d8dd9a91SJeremy L Thompson 
48d8dd9a91SJeremy L Thompson   // Check setting field in operator
493668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_sub_1, "count", &count_label);
507bfe0f0eSJeremy L Thompson   int value_count = 43;
517bfe0f0eSJeremy L Thompson   CeedOperatorContextSetInt32(op_sub_1, count_label, &value_count);
522b730f8bSJeremy L Thompson   if (ctx_data_1.count != 43) printf("Incorrect context data for count: %" CeedInt_FMT " != 43", ctx_data_1.count);
532788fa27SJeremy L Thompson   {
542788fa27SJeremy L Thompson     const int *values;
552788fa27SJeremy L Thompson     size_t     num_values;
562788fa27SJeremy L Thompson 
572788fa27SJeremy L Thompson     CeedOperatorContextGetInt32Read(op_sub_1, count_label, &num_values, &values);
582788fa27SJeremy L Thompson     if (num_values != 1) printf("Incorrect number of count values, found %ld but expected 1", num_values);
592788fa27SJeremy L Thompson     if (values[0] != ctx_data_1.count) printf("Incorrect value found, found %d but expected %d", values[0], ctx_data_1.count);
602788fa27SJeremy L Thompson     CeedOperatorContextRestoreInt32Read(op_sub_1, count_label, &values);
612788fa27SJeremy L Thompson   }
62d8dd9a91SJeremy L Thompson 
63d8dd9a91SJeremy L Thompson   // Second sub-operator
64d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
652b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext2), &ctx_data_2);
662b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time", offsetof(TestContext2, time), 1, "current time");
672b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other", offsetof(TestContext2, other), 1, "some other value");
68d8dd9a91SJeremy L Thompson 
69d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
70d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
71d8dd9a91SJeremy L Thompson 
722b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &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
803668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "time", &time_label);
817bfe0f0eSJeremy L Thompson   double value_time = 2.0;
827bfe0f0eSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, time_label, &value_time);
832b730f8bSJeremy L Thompson   if (ctx_data_2.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data_2.time);
842788fa27SJeremy L Thompson   {
852788fa27SJeremy L Thompson     const double *values;
862788fa27SJeremy L Thompson     size_t        num_values;
872788fa27SJeremy L Thompson 
882788fa27SJeremy L Thompson     CeedOperatorContextGetDoubleRead(op_composite, time_label, &num_values, &values);
892788fa27SJeremy L Thompson     if (num_values != 1) printf("Incorrect number of time values, found %ld but expected 1", num_values);
902788fa27SJeremy L Thompson     if (values[0] != ctx_data_2.time) printf("Incorrect value found, found %f but expected %f", values[0], ctx_data_2.time);
912788fa27SJeremy L Thompson     CeedOperatorContextRestoreDoubleRead(op_composite, time_label, &values);
922788fa27SJeremy L Thompson   }
93d8dd9a91SJeremy L Thompson 
94d8dd9a91SJeremy L Thompson   // Check setting field in context of multiple sub-operators for composite operator
953668ca4bSJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
96dab60d25SJeremy L Thompson   // No issue requesting same label twice
97dab60d25SJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
987bfe0f0eSJeremy L Thompson   double value_other = 9000.;
997bfe0f0eSJeremy L Thompson   CeedOperatorContextSetDouble(op_composite, other_label, &value_other);
1002b730f8bSJeremy L Thompson   if (ctx_data_1.other != 9000.0) printf("Incorrect context data for other: %f != 2.0\n", ctx_data_1.other);
1012b730f8bSJeremy L Thompson   if (ctx_data_2.other != 9000.0) printf("Incorrect context data for other: %f != 2.0\n", ctx_data_2.other);
102d8dd9a91SJeremy L Thompson 
103f2adece3SJeremy L Thompson   // Check requesting label for field that doesn't exist returns NULL
104f2adece3SJeremy L Thompson   CeedOperatorContextGetFieldLabel(op_composite, "bad", &bad_label);
1052b730f8bSJeremy L Thompson   if (bad_label) printf("Incorrect context label returned\n");
106f2adece3SJeremy L Thompson 
107*0126412dSJeremy L Thompson   {
108*0126412dSJeremy L Thompson     // Check getting reference to QFunctionContext
109*0126412dSJeremy L Thompson     CeedQFunctionContext ctx_copy = NULL;
110*0126412dSJeremy L Thompson 
111*0126412dSJeremy L Thompson     CeedOperatorGetContext(op_sub_1, &ctx_copy);
112*0126412dSJeremy L Thompson     if (ctx_copy != qf_ctx_sub_1) printf("Incorrect QFunctionContext retrieved");
113*0126412dSJeremy L Thompson 
114*0126412dSJeremy L Thompson     CeedOperatorGetContext(op_sub_2, &ctx_copy);  // Destroys reference to qf_ctx_sub_1
115*0126412dSJeremy L Thompson     if (ctx_copy != qf_ctx_sub_2) printf("Incorrect QFunctionContext retrieved");
116*0126412dSJeremy L Thompson     CeedQFunctionContextDestroy(&ctx_copy);  // Cleanup to prevent leak
117*0126412dSJeremy L Thompson   }
118*0126412dSJeremy L Thompson 
119d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
120d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
121d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_1);
122d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_2);
123d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_1);
124d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_2);
125d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_composite);
126d8dd9a91SJeremy L Thompson   CeedDestroy(&ceed);
127d8dd9a91SJeremy L Thompson   return 0;
128d8dd9a91SJeremy L Thompson }
129