xref: /libCEED/tests/t525-operator.c (revision 1ad466088344f9c7a89b938b3cea7230e969fe10)
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>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson 
8d8dd9a91SJeremy L Thompson #include "t500-operator.h"
9d8dd9a91SJeremy L Thompson 
10d8dd9a91SJeremy L Thompson typedef struct {
11d8dd9a91SJeremy L Thompson   int    count;
12d8dd9a91SJeremy L Thompson   double other;
13d8dd9a91SJeremy L Thompson } TestContext1;
14d8dd9a91SJeremy L Thompson 
15d8dd9a91SJeremy L Thompson typedef struct {
16d8dd9a91SJeremy L Thompson   double time;
17d8dd9a91SJeremy L Thompson   double other;
18d8dd9a91SJeremy L Thompson } TestContext2;
19d8dd9a91SJeremy L Thompson 
main(int argc,char ** argv)20d8dd9a91SJeremy L Thompson int main(int argc, char **argv) {
21d8dd9a91SJeremy L Thompson   Ceed                  ceed;
22d8dd9a91SJeremy L Thompson   CeedQFunctionContext  qf_ctx_sub_1, qf_ctx_sub_2;
23f2adece3SJeremy L Thompson   CeedContextFieldLabel count_label, other_label, time_label, bad_label;
24d8dd9a91SJeremy L Thompson   CeedQFunction         qf_sub_1, qf_sub_2;
25d8dd9a91SJeremy L Thompson   CeedOperator          op_sub_1, op_sub_2, op_composite;
263668ca4bSJeremy L Thompson 
27d8dd9a91SJeremy L Thompson   TestContext1 ctx_data_1 = {
28d8dd9a91SJeremy L Thompson       .count = 42,
29d8dd9a91SJeremy L Thompson       .other = -3.0,
30d8dd9a91SJeremy L Thompson   };
31d8dd9a91SJeremy L Thompson   TestContext2 ctx_data_2 = {
32d8dd9a91SJeremy L Thompson       .time  = 1.0,
33d8dd9a91SJeremy L Thompson       .other = -3.0,
34d8dd9a91SJeremy L Thompson   };
35d8dd9a91SJeremy L Thompson 
36d8dd9a91SJeremy L Thompson   CeedInit(argv[1], &ceed);
37d8dd9a91SJeremy L Thompson 
38d8dd9a91SJeremy L Thompson   // First sub-operator
39d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_1);
402b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_1, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext1), &ctx_data_1);
412b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterInt32(qf_ctx_sub_1, "count", offsetof(TestContext1, count), 1, "some sort of counter");
422b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_1, "other", offsetof(TestContext1, other), 1, "some other value");
43d8dd9a91SJeremy L Thompson 
44d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_sub_1);
45d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_1, qf_ctx_sub_1);
46d8dd9a91SJeremy L Thompson 
472b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_1, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_sub_1);
48d8dd9a91SJeremy L Thompson 
49d8dd9a91SJeremy L Thompson   // Check setting field in operator
5017b0d5c6SJeremy L Thompson   CeedOperatorGetContextFieldLabel(op_sub_1, "count", &count_label);
517bfe0f0eSJeremy L Thompson   int value_count = 43;
5217b0d5c6SJeremy L Thompson   CeedOperatorSetContextInt32(op_sub_1, count_label, &value_count);
532b730f8bSJeremy L Thompson   if (ctx_data_1.count != 43) printf("Incorrect context data for count: %" CeedInt_FMT " != 43", ctx_data_1.count);
542788fa27SJeremy L Thompson   {
552788fa27SJeremy L Thompson     const int *values;
562788fa27SJeremy L Thompson     size_t     num_values;
572788fa27SJeremy L Thompson 
5817b0d5c6SJeremy L Thompson     CeedOperatorGetContextInt32Read(op_sub_1, count_label, &num_values, &values);
59249f8407SJeremy L Thompson     if (num_values != 1) printf("Incorrect number of count values, found %zu but expected 1", num_values);
602788fa27SJeremy L Thompson     if (values[0] != ctx_data_1.count) printf("Incorrect value found, found %d but expected %d", values[0], ctx_data_1.count);
6117b0d5c6SJeremy L Thompson     CeedOperatorRestoreContextInt32Read(op_sub_1, count_label, &values);
622788fa27SJeremy L Thompson   }
63d8dd9a91SJeremy L Thompson 
64d8dd9a91SJeremy L Thompson   // Second sub-operator
65d8dd9a91SJeremy L Thompson   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
662b730f8bSJeremy L Thompson   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(TestContext2), &ctx_data_2);
672b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time", offsetof(TestContext2, time), 1, "current time");
682b730f8bSJeremy L Thompson   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other", offsetof(TestContext2, other), 1, "some other value");
69d8dd9a91SJeremy L Thompson 
70d8dd9a91SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
71d8dd9a91SJeremy L Thompson   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
72d8dd9a91SJeremy L Thompson 
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_sub_2);
74d8dd9a91SJeremy L Thompson 
75d8dd9a91SJeremy L Thompson   // Composite operator
76*ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_composite);
77*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_composite, op_sub_1);
78*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_composite, op_sub_2);
79d8dd9a91SJeremy L Thompson 
80d8dd9a91SJeremy L Thompson   // Check setting field in context of single sub-operator for composite operator
8117b0d5c6SJeremy L Thompson   CeedOperatorGetContextFieldLabel(op_composite, "time", &time_label);
827bfe0f0eSJeremy L Thompson   double value_time = 2.0;
8317b0d5c6SJeremy L Thompson   CeedOperatorSetContextDouble(op_composite, time_label, &value_time);
842b730f8bSJeremy L Thompson   if (ctx_data_2.time != 2.0) printf("Incorrect context data for time: %f != 2.0\n", ctx_data_2.time);
852788fa27SJeremy L Thompson   {
862788fa27SJeremy L Thompson     const double *values;
872788fa27SJeremy L Thompson     size_t        num_values;
882788fa27SJeremy L Thompson 
8917b0d5c6SJeremy L Thompson     CeedOperatorGetContextDoubleRead(op_composite, time_label, &num_values, &values);
90249f8407SJeremy L Thompson     if (num_values != 1) printf("Incorrect number of time values, found %zu but expected 1", num_values);
912788fa27SJeremy L Thompson     if (values[0] != ctx_data_2.time) printf("Incorrect value found, found %f but expected %f", values[0], ctx_data_2.time);
9217b0d5c6SJeremy L Thompson     CeedOperatorRestoreContextDoubleRead(op_composite, time_label, &values);
932788fa27SJeremy L Thompson   }
94d8dd9a91SJeremy L Thompson 
95d8dd9a91SJeremy L Thompson   // Check setting field in context of multiple sub-operators for composite operator
9617b0d5c6SJeremy L Thompson   CeedOperatorGetContextFieldLabel(op_composite, "other", &other_label);
97dab60d25SJeremy L Thompson   // No issue requesting same label twice
9817b0d5c6SJeremy L Thompson   CeedOperatorGetContextFieldLabel(op_composite, "other", &other_label);
997bfe0f0eSJeremy L Thompson   double value_other = 9000.;
100249f8407SJeremy L Thompson 
10117b0d5c6SJeremy L Thompson   CeedOperatorSetContextDouble(op_composite, other_label, &value_other);
1022b730f8bSJeremy L Thompson   if (ctx_data_1.other != 9000.0) printf("Incorrect context data for other: %f != 2.0\n", ctx_data_1.other);
1032b730f8bSJeremy L Thompson   if (ctx_data_2.other != 9000.0) printf("Incorrect context data for other: %f != 2.0\n", ctx_data_2.other);
104d8dd9a91SJeremy L Thompson 
105f2adece3SJeremy L Thompson   // Check requesting label for field that doesn't exist returns NULL
10617b0d5c6SJeremy L Thompson   CeedOperatorGetContextFieldLabel(op_composite, "bad", &bad_label);
1072b730f8bSJeremy L Thompson   if (bad_label) printf("Incorrect context label returned\n");
108f2adece3SJeremy L Thompson 
1090126412dSJeremy L Thompson   {
1100126412dSJeremy L Thompson     // Check getting reference to QFunctionContext
1110126412dSJeremy L Thompson     CeedQFunctionContext ctx_copy = NULL;
1120126412dSJeremy L Thompson 
1130126412dSJeremy L Thompson     CeedOperatorGetContext(op_sub_1, &ctx_copy);
1140126412dSJeremy L Thompson     if (ctx_copy != qf_ctx_sub_1) printf("Incorrect QFunctionContext retrieved");
1151485364cSJeremy L Thompson     CeedQFunctionContextDestroy(&ctx_copy);
1160126412dSJeremy L Thompson 
1170126412dSJeremy L Thompson     CeedOperatorGetContext(op_sub_2, &ctx_copy);  // Destroys reference to qf_ctx_sub_1
1180126412dSJeremy L Thompson     if (ctx_copy != qf_ctx_sub_2) printf("Incorrect QFunctionContext retrieved");
1190126412dSJeremy L Thompson     CeedQFunctionContextDestroy(&ctx_copy);  // Cleanup to prevent leak
1200126412dSJeremy L Thompson   }
1210126412dSJeremy L Thompson 
122d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
123d8dd9a91SJeremy L Thompson   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
124d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_1);
125d8dd9a91SJeremy L Thompson   CeedQFunctionDestroy(&qf_sub_2);
126d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_1);
127d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_sub_2);
128d8dd9a91SJeremy L Thompson   CeedOperatorDestroy(&op_composite);
129d8dd9a91SJeremy L Thompson   CeedDestroy(&ceed);
130d8dd9a91SJeremy L Thompson   return 0;
131d8dd9a91SJeremy L Thompson }
132