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