xref: /libCEED/tests/t525-operator.c (revision 59189cfaeff825e3813131e44fcd18f3d0aa7fc5)
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   CeedQFunction qf_sub_1, qf_sub_2;
22   CeedOperator op_sub_1, op_sub_2, op_composite;
23   TestContext1 ctx_data_1 = {
24     .count = 42,
25     .other = -3.0,
26   };
27   TestContext2 ctx_data_2 = {
28     .time = 1.0,
29     .other = -3.0,
30   };
31 
32   CeedInit(argv[1], &ceed);
33 
34   // First sub-operator
35   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_1);
36   CeedQFunctionContextSetData(qf_ctx_sub_1, CEED_MEM_HOST, CEED_USE_POINTER,
37                               sizeof(TestContext1), &ctx_data_1);
38   CeedQFunctionContextRegisterInt32(qf_ctx_sub_1, "count", offsetof(TestContext1,
39                                     count), "some sort of counter");
40   CeedQFunctionContextRegisterDouble(qf_ctx_sub_1, "other", offsetof(TestContext1,
41                                      other), "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,
47                      &op_sub_1);
48 
49   // Check setting field in operator
50   CeedOperatorContextSetInt32(op_sub_1, "count", 43);
51   if (ctx_data_1.count != 43)
52     // LCOV_EXCL_START
53     printf("Incorrect context data for count: %d != 43", ctx_data_1.count);
54   // LCOV_EXCL_STOP
55 
56   // Second sub-operator
57   CeedQFunctionContextCreate(ceed, &qf_ctx_sub_2);
58   CeedQFunctionContextSetData(qf_ctx_sub_2, CEED_MEM_HOST, CEED_USE_POINTER,
59                               sizeof(TestContext2), &ctx_data_2);
60   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "time", offsetof(TestContext2,
61                                      time), "current time");
62   CeedQFunctionContextRegisterDouble(qf_ctx_sub_2, "other", offsetof(TestContext2,
63                                      other), "some other value");
64 
65   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_sub_2);
66   CeedQFunctionSetContext(qf_sub_2, qf_ctx_sub_2);
67 
68   CeedOperatorCreate(ceed, qf_sub_2, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
69                      &op_sub_2);
70 
71   // Composite operator
72   CeedCompositeOperatorCreate(ceed, &op_composite);
73   CeedCompositeOperatorAddSub(op_composite, op_sub_1);
74   CeedCompositeOperatorAddSub(op_composite, op_sub_2);
75 
76   // Check setting field in context of single sub-operator for composite operator
77   CeedOperatorContextSetDouble(op_composite, "time", 2.0);
78   if (ctx_data_2.time != 2.0)
79     // LCOV_EXCL_START
80     printf("Incorrect context data for time: %f != 2.0", ctx_data_2.time);
81   // LCOV_EXCL_STOP
82 
83   // Check setting field in context of multiple sub-operators for composite operator
84   CeedOperatorContextSetDouble(op_composite, "other", 9000.);
85   if (ctx_data_1.other != 9000.0)
86     // LCOV_EXCL_START
87     printf("Incorrect context data for other: %f != 2.0", ctx_data_1.other);
88   // LCOV_EXCL_STOP
89   if (ctx_data_2.other != 9000.0)
90     // LCOV_EXCL_START
91     printf("Incorrect context data for other: %f != 2.0", ctx_data_2.other);
92   // LCOV_EXCL_STOP
93 
94   CeedQFunctionContextDestroy(&qf_ctx_sub_1);
95   CeedQFunctionContextDestroy(&qf_ctx_sub_2);
96   CeedQFunctionDestroy(&qf_sub_1);
97   CeedQFunctionDestroy(&qf_sub_2);
98   CeedOperatorDestroy(&op_sub_1);
99   CeedOperatorDestroy(&op_sub_2);
100   CeedOperatorDestroy(&op_composite);
101   CeedDestroy(&ceed);
102   return 0;
103 }
104