xref: /libCEED/tests/t532-operator.c (revision 5d1e906964b04cb5161d672bdc4191311737c811)
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>
849aac155SJeremy L Thompson #include <stdio.h>
92b730f8bSJeremy L Thompson #include <stdlib.h>
102b730f8bSJeremy L Thompson 
main(int argc,char ** argv)111d102b48SJeremy L Thompson int main(int argc, char **argv) {
121d102b48SJeremy L Thompson   Ceed                ceed;
134fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data_mass, elem_restriction_q_data_diff, elem_restriction_assembled;
14d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
154fee36f0SJeremy L Thompson   CeedQFunction       qf_setup_mass, qf_setup_diff, qf_apply, qf_apply_assembled;
164fee36f0SJeremy L Thompson   CeedOperator        op_setup_mass, op_setup_diff, op_apply, op_apply_assembled;
174fee36f0SJeremy L Thompson   CeedVector          q_data_mass, q_data_diff, x, assembled, u, v;
184fee36f0SJeremy L Thompson   CeedInt             num_elem = 6, p = 3, q = 4, dim = 2;
191d102b48SJeremy L Thompson   CeedInt             nx = 3, ny = 2;
204fee36f0SJeremy L Thompson   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q;
214fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p * p];
221d102b48SJeremy L Thompson 
231d102b48SJeremy L Thompson   CeedInit(argv[1], &ceed);
241d102b48SJeremy L Thompson 
254fee36f0SJeremy L Thompson   // Vectors
264fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
274fee36f0SJeremy L Thompson   {
284fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
294fee36f0SJeremy L Thompson 
302b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < nx * 2 + 1; i++) {
311d102b48SJeremy L Thompson       for (CeedInt j = 0; j < ny * 2 + 1; j++) {
324fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx);
334fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny);
341d102b48SJeremy L Thompson       }
352b730f8bSJeremy L Thompson     }
364fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
374fee36f0SJeremy L Thompson   }
384fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
394fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
40d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts, &q_data_mass);
41d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts * dim * (dim + 1) / 2, &q_data_diff);
421d102b48SJeremy L Thompson 
434fee36f0SJeremy L Thompson   // Restrictions
44d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
451d102b48SJeremy L Thompson     CeedInt col, row, offset;
461d102b48SJeremy L Thompson     col    = i % nx;
471d102b48SJeremy L Thompson     row    = i / nx;
484fee36f0SJeremy L Thompson     offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1);
494fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
504fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j;
512b730f8bSJeremy L Thompson     }
521d102b48SJeremy L Thompson   }
534fee36f0SJeremy 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);
544fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u);
551d102b48SJeremy L Thompson 
564fee36f0SJeremy L Thompson   CeedInt strides_q_data_mass[3] = {1, q * q, q * q};
574fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data_mass, &elem_restriction_q_data_mass);
581d102b48SJeremy L Thompson 
594fee36f0SJeremy L Thompson   CeedInt strides_q_data_diff[3] = {1, q * q, q * q * dim * (dim + 1) / 2};
604fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, dim * (dim + 1) / 2, dim * (dim + 1) / 2 * num_qpts, strides_q_data_diff,
614fee36f0SJeremy L Thompson                                    &elem_restriction_q_data_diff);
621d102b48SJeremy L Thompson 
631d102b48SJeremy L Thompson   // Bases
644fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x);
654fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
661d102b48SJeremy L Thompson 
671d102b48SJeremy L Thompson   // QFunction - setup mass
682b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_mass, setup_mass_loc, &qf_setup_mass);
691d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "dx", dim * dim, CEED_EVAL_GRAD);
70a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "weight", 1, CEED_EVAL_WEIGHT);
711d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_mass, "q data", 1, CEED_EVAL_NONE);
721d102b48SJeremy L Thompson 
731d102b48SJeremy L Thompson   // Operator - setup mass
742b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_mass);
754fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
762b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
77*356036faSJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "q data", elem_restriction_q_data_mass, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
781d102b48SJeremy L Thompson 
791d102b48SJeremy L Thompson   // QFunction - setup diff
802b730f8bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_diff, setup_diff_loc, &qf_setup_diff);
811d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "dx", dim * dim, CEED_EVAL_GRAD);
82a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "weight", 1, CEED_EVAL_WEIGHT);
831d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_diff, "q data", dim * (dim + 1) / 2, CEED_EVAL_NONE);
841d102b48SJeremy L Thompson 
851d102b48SJeremy L Thompson   // Operator - setup diff
862b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_diff, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_diff);
874fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
882b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
89*356036faSJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "q data", elem_restriction_q_data_diff, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
901d102b48SJeremy L Thompson 
911d102b48SJeremy L Thompson   // Apply Setup Operators
924fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_mass, x, q_data_mass, CEED_REQUEST_IMMEDIATE);
934fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup_diff, x, q_data_diff, CEED_REQUEST_IMMEDIATE);
941d102b48SJeremy L Thompson 
951d102b48SJeremy L Thompson   // QFunction - apply
961d102b48SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, apply, apply_loc, &qf_apply);
971d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "du", dim, CEED_EVAL_GRAD);
98a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "mass q data", 1, CEED_EVAL_NONE);
99a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "diff q data", dim * (dim + 1) / 2, CEED_EVAL_NONE);
1001d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "u", 1, CEED_EVAL_INTERP);
1011d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "v", 1, CEED_EVAL_INTERP);
1021d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "dv", dim, CEED_EVAL_GRAD);
1031d102b48SJeremy L Thompson 
1041d102b48SJeremy L Thompson   // Operator - apply
1052b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply);
1064fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "du", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
107*356036faSJeremy L Thompson   CeedOperatorSetField(op_apply, "mass q data", elem_restriction_q_data_mass, CEED_BASIS_NONE, q_data_mass);
108*356036faSJeremy L Thompson   CeedOperatorSetField(op_apply, "diff q data", elem_restriction_q_data_diff, CEED_BASIS_NONE, q_data_diff);
1094fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1104fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1114fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply, "dv", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1121d102b48SJeremy L Thompson 
1131d102b48SJeremy L Thompson   // Apply original operator
1141d102b48SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
1151d102b48SJeremy L Thompson   CeedOperatorApply(op_apply, u, v, CEED_REQUEST_IMMEDIATE);
1161d102b48SJeremy L Thompson 
1171d102b48SJeremy L Thompson   // Check output
1184fee36f0SJeremy L Thompson   {
1194fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1201d102b48SJeremy L Thompson     CeedScalar        area = 0.0;
1214fee36f0SJeremy L Thompson 
1224fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1234fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
1244fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1252b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: True operator computed area = %f != 1.0\n", area);
1264fee36f0SJeremy L Thompson   }
1271d102b48SJeremy L Thompson 
1281d102b48SJeremy L Thompson   // Assemble QFunction
129beecbf24SJeremy L Thompson   CeedOperatorSetQFunctionAssemblyReuse(op_apply, true);
1304fee36f0SJeremy L Thompson   CeedOperatorLinearAssembleQFunction(op_apply, &assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE);
1311d102b48SJeremy L Thompson 
1321d102b48SJeremy L Thompson   // QFunction - apply assembled
1334fee36f0SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, apply_lin, apply_lin_loc, &qf_apply_assembled);
1344fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "du", dim, CEED_EVAL_GRAD);
1354fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "q data", (dim + 1) * (dim + 1), CEED_EVAL_NONE);
1364fee36f0SJeremy L Thompson   CeedQFunctionAddInput(qf_apply_assembled, "u", 1, CEED_EVAL_INTERP);
1374fee36f0SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply_assembled, "v", 1, CEED_EVAL_INTERP);
1384fee36f0SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply_assembled, "dv", dim, CEED_EVAL_GRAD);
1391d102b48SJeremy L Thompson 
1401d102b48SJeremy L Thompson   // Operator - apply assembled
1414fee36f0SJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply_assembled, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_apply_assembled);
1424fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "du", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
143*356036faSJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "q data", elem_restriction_assembled, CEED_BASIS_NONE, assembled);
1444fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1454fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1464fee36f0SJeremy L Thompson   CeedOperatorSetField(op_apply_assembled, "dv", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
1471d102b48SJeremy L Thompson 
1481d102b48SJeremy L Thompson   // Apply assembled QFunction operator
1491d102b48SJeremy L Thompson   CeedVectorSetValue(v, 0.0);
1504fee36f0SJeremy L Thompson   CeedOperatorApply(op_apply_assembled, u, v, CEED_REQUEST_IMMEDIATE);
1511d102b48SJeremy L Thompson 
1521d102b48SJeremy L Thompson   // Check output
1534fee36f0SJeremy L Thompson   {
1544fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1554fee36f0SJeremy L Thompson     CeedScalar        area = 0.0;
1564fee36f0SJeremy L Thompson 
1574fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1584fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
1594fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1602b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: Assembled operator computed area = %f != 1.0\n", area);
1614fee36f0SJeremy L Thompson   }
1621d102b48SJeremy L Thompson 
1631d102b48SJeremy L Thompson   // Cleanup
1644fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
1654fee36f0SJeremy L Thompson   CeedVectorDestroy(&assembled);
166d1d35e2fSjeremylt   CeedVectorDestroy(&q_data_mass);
167d1d35e2fSjeremylt   CeedVectorDestroy(&q_data_diff);
1681d102b48SJeremy L Thompson   CeedVectorDestroy(&u);
1691d102b48SJeremy L Thompson   CeedVectorDestroy(&v);
1704fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
1714fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
1724fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_mass);
1734fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_diff);
1744fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_assembled);
1754fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
1764fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
1774fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_mass);
1784fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_diff);
1794fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_apply);
1804fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_apply_assembled);
1814fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_setup_mass);
1824fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_setup_diff);
1834fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_apply);
1844fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_apply_assembled);
1851d102b48SJeremy L Thompson   CeedDestroy(&ceed);
1861d102b48SJeremy L Thompson   return 0;
1871d102b48SJeremy L Thompson }
188