xref: /libCEED/tests/t532-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
11d102b48SJeremy L Thompson /// @file
21d102b48SJeremy L Thompson /// Test assembly of mass and Poisson operator QFunction
31d102b48SJeremy L Thompson /// \test Test assembly of mass and Poisson operator QFunction
41d102b48SJeremy L Thompson #include "t532-operator.h"
51d102b48SJeremy L Thompson 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson #include <math.h>
82b730f8bSJeremy L Thompson #include <stdlib.h>
92b730f8bSJeremy L Thompson 
101d102b48SJeremy L Thompson int main(int argc, char **argv) {
111d102b48SJeremy L Thompson   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data_mass, elem_restriction_q_data_diff, elem_restriction_assembled;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14*4fee36f0SJeremy L Thompson   CeedQFunction       qf_setup_mass, qf_setup_diff, qf_apply, qf_apply_assembled;
15*4fee36f0SJeremy L Thompson   CeedOperator        op_setup_mass, op_setup_diff, op_apply, op_apply_assembled;
16*4fee36f0SJeremy L Thompson   CeedVector          q_data_mass, q_data_diff, x, assembled, u, v;
17*4fee36f0SJeremy L Thompson   CeedInt             num_elem = 6, p = 3, q = 4, dim = 2;
181d102b48SJeremy L Thompson   CeedInt             nx = 3, ny = 2;
19*4fee36f0SJeremy L Thompson   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q;
20*4fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p * p];
211d102b48SJeremy L Thompson 
221d102b48SJeremy L Thompson   CeedInit(argv[1], &ceed);
231d102b48SJeremy L Thompson 
24*4fee36f0SJeremy L Thompson   // Vectors
25*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
26*4fee36f0SJeremy L Thompson   {
27*4fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
28*4fee36f0SJeremy L Thompson 
292b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < nx * 2 + 1; i++) {
301d102b48SJeremy L Thompson       for (CeedInt j = 0; j < ny * 2 + 1; j++) {
31*4fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx);
32*4fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny);
331d102b48SJeremy L Thompson       }
342b730f8bSJeremy L Thompson     }
35*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
36*4fee36f0SJeremy L Thompson   }
37*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
38*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
39d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts, &q_data_mass);
40d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts * dim * (dim + 1) / 2, &q_data_diff);
411d102b48SJeremy L Thompson 
42*4fee36f0SJeremy L Thompson   // Restrictions
43d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
441d102b48SJeremy L Thompson     CeedInt col, row, offset;
451d102b48SJeremy L Thompson     col    = i % nx;
461d102b48SJeremy L Thompson     row    = i / nx;
47*4fee36f0SJeremy L Thompson     offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1);
48*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
49*4fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j;
502b730f8bSJeremy L Thompson     }
511d102b48SJeremy L Thompson   }
52*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
53*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u);
541d102b48SJeremy L Thompson 
55*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_mass[3] = {1, q * q, q * q};
56*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data_mass, &elem_restriction_q_data_mass);
571d102b48SJeremy L Thompson 
58*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_diff[3] = {1, q * q, q * q * dim * (dim + 1) / 2};
59*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, dim * (dim + 1) / 2, dim * (dim + 1) / 2 * num_qpts, strides_q_data_diff,
60*4fee36f0SJeremy L Thompson                                    &elem_restriction_q_data_diff);
611d102b48SJeremy L Thompson 
621d102b48SJeremy L Thompson   // Bases
63*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x);
64*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
651d102b48SJeremy L Thompson 
661d102b48SJeremy L Thompson   // QFunction - setup mass
672b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_mass, setup_mass_loc, &qf_setup_mass);
681d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "dx", dim * dim, CEED_EVAL_GRAD);
69a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "weight", 1, CEED_EVAL_WEIGHT);
701d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_mass, "q data", 1, CEED_EVAL_NONE);
711d102b48SJeremy L Thompson 
721d102b48SJeremy L Thompson   // Operator - setup mass
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_mass);
74*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
752b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
76*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "q data", elem_restriction_q_data_mass, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
771d102b48SJeremy L Thompson 
781d102b48SJeremy L Thompson   // QFunction - setup diff
792b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_diff, setup_diff_loc, &qf_setup_diff);
801d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "dx", dim * dim, CEED_EVAL_GRAD);
81a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "weight", 1, CEED_EVAL_WEIGHT);
821d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_diff, "q data", dim * (dim + 1) / 2, CEED_EVAL_NONE);
831d102b48SJeremy L Thompson 
841d102b48SJeremy L Thompson   // Operator - setup diff
852b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_diff, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_diff);
86*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
872b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
88*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "q data", elem_restriction_q_data_diff, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
891d102b48SJeremy L Thompson 
901d102b48SJeremy L Thompson   // Apply Setup Operators
91*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_mass, x, q_data_mass, CEED_REQUEST_IMMEDIATE);
92*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_diff, x, q_data_diff, CEED_REQUEST_IMMEDIATE);
931d102b48SJeremy L Thompson 
941d102b48SJeremy L Thompson   // QFunction - apply
951d102b48SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, apply, apply_loc, &qf_apply);
961d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "du", dim, CEED_EVAL_GRAD);
97a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "mass q data", 1, CEED_EVAL_NONE);
98a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "diff q data", dim * (dim + 1) / 2, CEED_EVAL_NONE);
991d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "u", 1, CEED_EVAL_INTERP);
1001d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "v", 1, CEED_EVAL_INTERP);
1011d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "dv", dim, CEED_EVAL_GRAD);
1021d102b48SJeremy L Thompson 
1031d102b48SJeremy L Thompson   // Operator - apply
1042b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply);
105*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "du", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
106*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "mass q data", elem_restriction_q_data_mass, CEED_BASIS_COLLOCATED, q_data_mass);
107*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "diff q data", elem_restriction_q_data_diff, CEED_BASIS_COLLOCATED, q_data_diff);
108*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
109*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
110*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "dv", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1111d102b48SJeremy L Thompson 
1121d102b48SJeremy L Thompson   // Apply original operator
1131d102b48SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
1141d102b48SJeremy L Thompson   CeedOperatorApply(op_apply, u, v, CEED_REQUEST_IMMEDIATE);
1151d102b48SJeremy L Thompson 
1161d102b48SJeremy L Thompson   // Check output
117*4fee36f0SJeremy L Thompson   {
118*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1191d102b48SJeremy L Thompson     CeedScalar        area = 0.0;
120*4fee36f0SJeremy L Thompson 
121*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
122*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
123*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1242b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: True operator computed area = %f != 1.0\n", area);
125*4fee36f0SJeremy L Thompson   }
1261d102b48SJeremy L Thompson 
1271d102b48SJeremy L Thompson   // Assemble QFunction
128beecbf24SJeremy L Thompson   CeedOperatorSetQFunctionAssemblyReuse(op_apply, true);
129*4fee36f0SJeremy L Thompson   CeedOperatorLinearAssembleQFunction(op_apply, &assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE);
1308b919e6bSJeremy L Thompson   // Second call will be no-op since SetQFunctionUpdated was not called
131beecbf24SJeremy L Thompson   CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_apply, false);
132*4fee36f0SJeremy L Thompson   CeedOperatorLinearAssembleQFunction(op_apply, &assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE);
1331d102b48SJeremy L Thompson 
1341d102b48SJeremy L Thompson   // QFunction - apply assembled
135*4fee36f0SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, apply_lin, apply_lin_loc, &qf_apply_assembled);
136*4fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "du", dim, CEED_EVAL_GRAD);
137*4fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "q data", (dim + 1) * (dim + 1), CEED_EVAL_NONE);
138*4fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "u", 1, CEED_EVAL_INTERP);
139*4fee36f0SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply_assembled, "v", 1, CEED_EVAL_INTERP);
140*4fee36f0SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply_assembled, "dv", dim, CEED_EVAL_GRAD);
1411d102b48SJeremy L Thompson 
1421d102b48SJeremy L Thompson   // Operator - apply assembled
143*4fee36f0SJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply_assembled, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply_assembled);
144*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "du", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
145*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "q data", elem_restriction_assembled, CEED_BASIS_COLLOCATED, assembled);
146*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
147*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
148*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "dv", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1491d102b48SJeremy L Thompson 
1501d102b48SJeremy L Thompson   // Apply assembled QFunction operator
1511d102b48SJeremy L Thompson   CeedVectorSetValue(v, 0.0);
152*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_apply_assembled, u, v, CEED_REQUEST_IMMEDIATE);
1531d102b48SJeremy L Thompson 
1541d102b48SJeremy L Thompson   // Check output
155*4fee36f0SJeremy L Thompson   {
156*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
157*4fee36f0SJeremy L Thompson     CeedScalar        area = 0.0;
158*4fee36f0SJeremy L Thompson 
159*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
160*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
161*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1622b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: Assembled operator computed area = %f != 1.0\n", area);
163*4fee36f0SJeremy L Thompson   }
1641d102b48SJeremy L Thompson 
1651d102b48SJeremy L Thompson   // Cleanup
166*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
167*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&assembled);
168d1d35e2fSjeremylt   CeedVectorDestroy(&q_data_mass);
169d1d35e2fSjeremylt   CeedVectorDestroy(&q_data_diff);
1701d102b48SJeremy L Thompson   CeedVectorDestroy(&u);
1711d102b48SJeremy L Thompson   CeedVectorDestroy(&v);
172*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
173*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
174*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_mass);
175*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_diff);
176*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_assembled);
177*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
178*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
179*4fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_mass);
180*4fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_diff);
181*4fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_apply);
182*4fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_apply_assembled);
183*4fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_setup_mass);
184*4fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_setup_diff);
185*4fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_apply);
186*4fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_apply_assembled);
1871d102b48SJeremy L Thompson   CeedDestroy(&ceed);
1881d102b48SJeremy L Thompson   return 0;
1891d102b48SJeremy L Thompson }
190