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