xref: /libCEED/tests/t525-operator.c (revision 7bfe0f0e497883534890a072c2a8b865352898b0)
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;
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   double value_other = 9000.;
96   CeedOperatorContextSetDouble(op_composite, other_label, &value_other);
97   if (ctx_data_1.other != 9000.0)
98     // LCOV_EXCL_START
99     printf("Incorrect context data for other: %f != 2.0", ctx_data_1.other);
100   // LCOV_EXCL_STOP
101   if (ctx_data_2.other != 9000.0)
102     // LCOV_EXCL_START
103     printf("Incorrect context data for other: %f != 2.0", ctx_data_2.other);
104   // LCOV_EXCL_STOP
105 
106   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
107   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
108   CeedQFunctionDestroy(&qf_sub_1);
109   CeedQFunctionDestroy(&qf_sub_2);
110   CeedOperatorDestroy(&op_sub_1);
111   CeedOperatorDestroy(&op_sub_2);
112   CeedOperatorDestroy(&op_composite);
113   CeedDestroy(&ceed);
114   return 0;
115 }
116