xref: /libCEED/tests/t506-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
15b3ccac8Sjeremylt /// @file
25b3ccac8Sjeremylt /// Test creation reuse of the same QFunction for multiple operators
35b3ccac8Sjeremylt /// \test Test creation reuse of the same QFunction for multiple operators
45b3ccac8Sjeremylt #include <ceed.h>
55b3ccac8Sjeremylt #include <math.h>
62b730f8bSJeremy L Thompson #include <stdlib.h>
72b730f8bSJeremy L Thompson 
85b3ccac8Sjeremylt #include "t502-operator.h"
95b3ccac8Sjeremylt 
105b3ccac8Sjeremylt int main(int argc, char **argv) {
115b3ccac8Sjeremylt   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data_small, elem_restriction_q_data_large;
13d1d35e2fSjeremylt   CeedBasis           basis_x_small, basis_x_large, basis_u_small, basis_u_large;
145b3ccac8Sjeremylt   CeedQFunction       qf_setup, qf_mass;
152b730f8bSJeremy L Thompson   CeedOperator        op_setup_small, op_mass_small, op_setup_large, op_mass_large;
16*4fee36f0SJeremy L Thompson   CeedVector          q_data_small, q_data_large, x, u, v;
17*4fee36f0SJeremy L Thompson   CeedInt             num_elem = 15, p = 5, q = 8, scale = 3;
18*4fee36f0SJeremy L Thompson   CeedInt             num_nodes_x = num_elem + 1, num_nodes_u = num_elem * (p - 1) + 1;
19*4fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * 2], ind_u[num_elem * p];
205b3ccac8Sjeremylt 
215b3ccac8Sjeremylt   CeedInit(argv[1], &ceed);
22*4fee36f0SJeremy L Thompson 
23*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
24*4fee36f0SJeremy L Thompson   {
25*4fee36f0SJeremy L Thompson     CeedScalar x_array[num_nodes_x];
26*4fee36f0SJeremy L Thompson 
27*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
28*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
29*4fee36f0SJeremy L Thompson   }
30*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, 2 * num_nodes_u, &u);
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, 2 * num_nodes_u, &v);
32*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data_small);
33*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q * scale, &q_data_large);
34*4fee36f0SJeremy L Thompson 
35*4fee36f0SJeremy L Thompson   // Restrictions
36d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
37d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
38d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
395b3ccac8Sjeremylt   }
40*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
415b3ccac8Sjeremylt 
42d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
43*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
44*4fee36f0SJeremy L Thompson       ind_u[p * i + j] = 2 * (i * (p - 1) + j);
455b3ccac8Sjeremylt     }
465b3ccac8Sjeremylt   }
47*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 2, 1, 2 * num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
48*4fee36f0SJeremy L Thompson 
49*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_small[3] = {1, q, q};
50*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data_small, &elem_restriction_q_data_small);
51*4fee36f0SJeremy L Thompson 
52*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_large[3] = {1, q * scale, q * scale};
53*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * scale, 1, q * num_elem * scale, strides_q_data_large, &elem_restriction_q_data_large);
545b3ccac8Sjeremylt 
555b3ccac8Sjeremylt   // Bases
56*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x_small);
57*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, p, q, CEED_GAUSS, &basis_u_small);
58*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q * scale, CEED_GAUSS, &basis_x_large);
59*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, p, q * scale, CEED_GAUSS, &basis_u_large);
605b3ccac8Sjeremylt 
615b3ccac8Sjeremylt   // QFunctions
625b3ccac8Sjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
63a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
645b3ccac8Sjeremylt   CeedQFunctionAddInput(qf_setup, "x", 1, CEED_EVAL_GRAD);
655b3ccac8Sjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
665b3ccac8Sjeremylt 
675b3ccac8Sjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
685b3ccac8Sjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
695b3ccac8Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP);
705b3ccac8Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP);
715b3ccac8Sjeremylt 
725b3ccac8Sjeremylt   // 'Small' Operators
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_small);
742b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_small, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_small, CEED_VECTOR_NONE);
75*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_small, "x", elem_restriction_x, basis_x_small, CEED_VECTOR_ACTIVE);
76*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_small, "rho", elem_restriction_q_data_small, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
775b3ccac8Sjeremylt 
78*4fee36f0SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_small);
79*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_small, "rho", elem_restriction_q_data_small, CEED_BASIS_COLLOCATED, q_data_small);
80*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_small, "u", elem_restriction_u, basis_u_small, CEED_VECTOR_ACTIVE);
81*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_small, "v", elem_restriction_u, basis_u_small, CEED_VECTOR_ACTIVE);
825b3ccac8Sjeremylt 
835b3ccac8Sjeremylt   // 'Large' operators
842b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_large);
852b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_large, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_large, CEED_VECTOR_NONE);
86*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_large, "x", elem_restriction_x, basis_x_large, CEED_VECTOR_ACTIVE);
87*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_large, "rho", elem_restriction_q_data_large, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
885b3ccac8Sjeremylt 
89*4fee36f0SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_large);
90*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_large, "rho", elem_restriction_q_data_large, CEED_BASIS_COLLOCATED, q_data_large);
91*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_large, "u", elem_restriction_u, basis_u_large, CEED_VECTOR_ACTIVE);
92*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_large, "v", elem_restriction_u, basis_u_large, CEED_VECTOR_ACTIVE);
935b3ccac8Sjeremylt 
945b3ccac8Sjeremylt   // Setup
95*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_small, x, q_data_small, CEED_REQUEST_IMMEDIATE);
96*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_large, x, q_data_large, CEED_REQUEST_IMMEDIATE);
975b3ccac8Sjeremylt 
98*4fee36f0SJeremy L Thompson   {
99*4fee36f0SJeremy L Thompson     CeedScalar *u_array;
100*4fee36f0SJeremy L Thompson 
101*4fee36f0SJeremy L Thompson     CeedVectorGetArrayWrite(u, CEED_MEM_HOST, &u_array);
102d1d35e2fSjeremylt     for (int i = 0; i < num_nodes_u; i++) {
103*4fee36f0SJeremy L Thompson       u_array[2 * i]     = 1.0;
104*4fee36f0SJeremy L Thompson       u_array[2 * i + 1] = 2.0;
1055b3ccac8Sjeremylt     }
106*4fee36f0SJeremy L Thompson     CeedVectorRestoreArray(u, &u_array);
107*4fee36f0SJeremy L Thompson   }
1085b3ccac8Sjeremylt 
1095b3ccac8Sjeremylt   // 'Small' operator
110*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass_small, u, v, CEED_REQUEST_IMMEDIATE);
1115b3ccac8Sjeremylt 
1125b3ccac8Sjeremylt   // Check output
113*4fee36f0SJeremy L Thompson   {
114*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
115*4fee36f0SJeremy L Thompson     CeedScalar        sum_1 = 0., sum_2 = 0.;
116*4fee36f0SJeremy L Thompson 
117*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
118d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_nodes_u; i++) {
119*4fee36f0SJeremy L Thompson       sum_1 += v_array[2 * i];
120*4fee36f0SJeremy L Thompson       sum_2 += v_array[2 * i + 1];
1215b3ccac8Sjeremylt     }
122*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1232b730f8bSJeremy L Thompson     if (fabs(sum_1 - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum_1);
1242b730f8bSJeremy L Thompson     if (fabs(sum_2 - 2.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 2.0\n", sum_2);
125*4fee36f0SJeremy L Thompson   }
1265b3ccac8Sjeremylt 
1275b3ccac8Sjeremylt   // 'Large' operator
128*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass_large, u, v, CEED_REQUEST_IMMEDIATE);
1295b3ccac8Sjeremylt 
1305b3ccac8Sjeremylt   // Check output
131*4fee36f0SJeremy L Thompson   {
132*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
133*4fee36f0SJeremy L Thompson     CeedScalar        sum_1 = 0., sum_2 = 0.;
134*4fee36f0SJeremy L Thompson 
135*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
136d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_nodes_u; i++) {
137*4fee36f0SJeremy L Thompson       sum_1 += v_array[2 * i];
138*4fee36f0SJeremy L Thompson       sum_2 += v_array[2 * i + 1];
1395b3ccac8Sjeremylt     }
140*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
141*4fee36f0SJeremy L Thompson 
1422b730f8bSJeremy L Thompson     if (fabs(sum_1 - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum_1);
1432b730f8bSJeremy L Thompson     if (fabs(sum_2 - 2.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 2.0\n", sum_2);
144*4fee36f0SJeremy L Thompson   }
1455b3ccac8Sjeremylt 
146*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
147*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
148*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
149*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_small);
150*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_large);
151*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
152*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
153*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_small);
154*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_large);
155*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_small);
156*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_small);
157*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_large);
158*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_large);
1595b3ccac8Sjeremylt   CeedQFunctionDestroy(&qf_setup);
1605b3ccac8Sjeremylt   CeedQFunctionDestroy(&qf_mass);
1615b3ccac8Sjeremylt   CeedOperatorDestroy(&op_setup_small);
1625b3ccac8Sjeremylt   CeedOperatorDestroy(&op_mass_small);
1635b3ccac8Sjeremylt   CeedOperatorDestroy(&op_setup_large);
1645b3ccac8Sjeremylt   CeedOperatorDestroy(&op_mass_large);
1655b3ccac8Sjeremylt   CeedDestroy(&ceed);
1665b3ccac8Sjeremylt   return 0;
1675b3ccac8Sjeremylt }
168