xref: /libCEED/tests/t525-operator.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
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: %d != 43", ctx_data_1.count);
60   // LCOV_EXCL_STOP
61 
62   // Second sub-operator
63   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
64   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER,
65                               sizeof(TestContext2), &ctx_data_2);
66   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time",
67                                      offsetof(TestContext2, time),
68                                      1, "current time");
69   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other",
70                                      offsetof(TestContext2, other),
71                                      1, "some other value");
72 
73   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
74   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
75 
76   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
77                      &op_sub_2);
78 
79   // Composite operator
80   CeedCompositeOperatorCreate(ceed, &op_composite);
81   CeedCompositeOperatorAddSub(op_composite, op_sub_1);
82   CeedCompositeOperatorAddSub(op_composite, op_sub_2);
83 
84   // Check setting field in context of single sub-operator for composite operator
85   CeedOperatorContextGetFieldLabel(op_composite, "time", &time_label);
86   double value_time = 2.0;
87   CeedOperatorContextSetDouble(op_composite, time_label, &value_time);
88   if (ctx_data_2.time != 2.0)
89     // LCOV_EXCL_START
90     printf("Incorrect context data for time: %f != 2.0", ctx_data_2.time);
91   // LCOV_EXCL_STOP
92 
93   // Check setting field in context of multiple sub-operators for composite operator
94   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
95   // No issue requesting same label twice
96   CeedOperatorContextGetFieldLabel(op_composite, "other", &other_label);
97   double value_other = 9000.;
98   CeedOperatorContextSetDouble(op_composite, other_label, &value_other);
99   if (ctx_data_1.other != 9000.0)
100     // LCOV_EXCL_START
101     printf("Incorrect context data for other: %f != 2.0", ctx_data_1.other);
102   // LCOV_EXCL_STOP
103   if (ctx_data_2.other != 9000.0)
104     // LCOV_EXCL_START
105     printf("Incorrect context data for other: %f != 2.0", ctx_data_2.other);
106   // LCOV_EXCL_STOP
107 
108   // Check requesting label for field that doesn't exist returns NULL
109   CeedOperatorContextGetFieldLabel(op_composite, "bad", &bad_label);
110   if (bad_label)
111     // LCOV_EXCL_START
112     printf("Incorrect context label returned");
113   // LCOV_EXCL_STOP
114 
115   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
116   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
117   CeedQFunctionDestroy(&qf_sub_1);
118   CeedQFunctionDestroy(&qf_sub_2);
119   CeedOperatorDestroy(&op_sub_1);
120   CeedOperatorDestroy(&op_sub_2);
121   CeedOperatorDestroy(&op_composite);
122   CeedDestroy(&ceed);
123   return 0;
124 }
125