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