xref: /libCEED/tests/t526-operator.c (revision 1ad466088344f9c7a89b938b3cea7230e969fe10)
16e15d496SJeremy L Thompson /// @file
26e15d496SJeremy L Thompson /// Test FLOP estimation for composite mass matrix operator
36e15d496SJeremy L Thompson /// \test Test FLOP estimation for composite mass matrix operator
46e15d496SJeremy L Thompson #include <ceed.h>
56e15d496SJeremy L Thompson #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson #include <stdlib.h>
82b730f8bSJeremy L Thompson 
96e15d496SJeremy L Thompson #include "t320-basis.h"
106e15d496SJeremy L Thompson 
114fee36f0SJeremy L Thompson /* The mesh comprises of two rows of 3 quadrilaterals followed by one row
126e15d496SJeremy L Thompson      of 6 triangles:
136e15d496SJeremy L Thompson    _ _ _
146e15d496SJeremy L Thompson   |_|_|_|
156e15d496SJeremy L Thompson   |_|_|_|
166e15d496SJeremy L Thompson   |/|/|/|
176e15d496SJeremy L Thompson 
186e15d496SJeremy L Thompson */
196e15d496SJeremy L Thompson 
main(int argc,char ** argv)206e15d496SJeremy L Thompson int main(int argc, char **argv) {
216e15d496SJeremy L Thompson   Ceed                ceed;
229d36ca50SJeremy L Thompson   CeedSize            flop_estimate;
234fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x_tet, elem_restriction_u_tet, elem_restriction_q_data_tet, elem_restriction_x_hex, elem_restriction_u_hex,
244fee36f0SJeremy L Thompson       elem_restriction_q_data_hex;
252b730f8bSJeremy L Thompson   CeedBasis     basis_x_tet, basis_u_tet, basis_x_hex, basis_u_hex;
266e15d496SJeremy L Thompson   CeedQFunction qf_mass;
276e15d496SJeremy L Thompson   CeedOperator  op_mass_tet, op_mass_hex, op_mass;
286e15d496SJeremy L Thompson   CeedVector    q_data_tet, q_data_hex;
294fee36f0SJeremy L Thompson   CeedInt       num_elem_tet = 6, p_tet = 6, q_tet = 4, num_elem_hex = 6, p_hex = 3, q_hex = 4, dim = 2;
302b730f8bSJeremy L Thompson   CeedInt       n_x = 3, n_y = 3, n_x_tet = 3, n_y_tet = 1, n_x_hex = 3;
316e15d496SJeremy L Thompson   CeedInt       row, col, offset;
324fee36f0SJeremy L Thompson   CeedInt       num_dofs = (n_x * 2 + 1) * (n_y * 2 + 1), num_qpts_tet = num_elem_tet * q_tet, num_qpts_hex = num_elem_hex * q_hex * q_hex;
334fee36f0SJeremy L Thompson   CeedInt       ind_x_tet[num_elem_tet * p_tet], ind_x_hex[num_elem_hex * p_hex * p_hex];
344fee36f0SJeremy L Thompson   CeedScalar    q_ref[dim * q_tet], q_weight[q_tet];
354fee36f0SJeremy L Thompson   CeedScalar    interp[p_tet * q_tet], grad[dim * p_tet * q_tet];
366e15d496SJeremy L Thompson 
376e15d496SJeremy L Thompson   CeedInit(argv[1], &ceed);
386e15d496SJeremy L Thompson 
396e15d496SJeremy L Thompson   // Qdata Vectors
406e15d496SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts_tet, &q_data_tet);
416e15d496SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts_hex, &q_data_hex);
426e15d496SJeremy L Thompson 
436e15d496SJeremy L Thompson   // Set up Tet Elements
444fee36f0SJeremy L Thompson   // -- Restrictions
456e15d496SJeremy L Thompson   for (CeedInt i = 0; i < num_elem_tet / 2; i++) {
466e15d496SJeremy L Thompson     col    = i % n_x_tet;
476e15d496SJeremy L Thompson     row    = i / n_x_tet;
486e15d496SJeremy L Thompson     offset = col * 2 + row * (n_x_tet * 2 + 1) * 2;
496e15d496SJeremy L Thompson 
504fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 0] = 2 + offset;
514fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 1] = 9 + offset;
524fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 2] = 16 + offset;
534fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 3] = 1 + offset;
544fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 4] = 8 + offset;
554fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 5] = 0 + offset;
566e15d496SJeremy L Thompson 
574fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 6]  = 14 + offset;
584fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 7]  = 7 + offset;
594fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 8]  = 0 + offset;
604fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 9]  = 15 + offset;
614fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 10] = 8 + offset;
624fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 11] = 16 + offset;
636e15d496SJeremy L Thompson   }
644fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_tet, p_tet, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_tet,
654fee36f0SJeremy L Thompson                             &elem_restriction_x_tet);
664fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_tet, p_tet, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_tet, &elem_restriction_u_tet);
676e15d496SJeremy L Thompson 
684fee36f0SJeremy L Thompson   CeedInt strides_q_data_tet[3] = {1, q_tet, q_tet};
694fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_tet, q_tet, 1, num_qpts_tet, strides_q_data_tet, &elem_restriction_q_data_tet);
706e15d496SJeremy L Thompson 
716e15d496SJeremy L Thompson   // -- Bases
724fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
734fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_x_tet);
746e15d496SJeremy L Thompson 
754fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
764fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_u_tet);
776e15d496SJeremy L Thompson 
786e15d496SJeremy L Thompson   // -- QFunction
796e15d496SJeremy L Thompson   CeedQFunctionCreateInteriorByName(ceed, "MassApply", &qf_mass);
806e15d496SJeremy L Thompson 
816e15d496SJeremy L Thompson   // -- Operators
826e15d496SJeremy L Thompson   // ---- Mass Tet
832b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_tet);
844fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "u", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
85356036faSJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "qdata", elem_restriction_q_data_tet, CEED_BASIS_NONE, q_data_tet);
864fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "v", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
876e15d496SJeremy L Thompson 
886e15d496SJeremy L Thompson   // Set up Hex Elements
894fee36f0SJeremy L Thompson   // -- Restrictions
906e15d496SJeremy L Thompson   for (CeedInt i = 0; i < num_elem_hex; i++) {
916e15d496SJeremy L Thompson     col    = i % n_x_hex;
926e15d496SJeremy L Thompson     row    = i / n_x_hex;
936e15d496SJeremy L Thompson     offset = (n_x_tet * 2 + 1) * (n_y_tet * 2) * (1 + row) + col * 2;
944fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p_hex; j++) {
954fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p_hex; k++) ind_x_hex[p_hex * (p_hex * i + k) + j] = offset + k * (n_x_hex * 2 + 1) + j;
962b730f8bSJeremy L Thompson     }
976e15d496SJeremy L Thompson   }
984fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_hex, p_hex * p_hex, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_hex,
994fee36f0SJeremy L Thompson                             &elem_restriction_x_hex);
1004fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_hex, p_hex * p_hex, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_hex, &elem_restriction_u_hex);
1016e15d496SJeremy L Thompson 
1024fee36f0SJeremy L Thompson   CeedInt strides_q_data_hex[3] = {1, q_hex * q_hex, q_hex * q_hex};
1034fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_hex, q_hex * q_hex, 1, num_qpts_hex, strides_q_data_hex, &elem_restriction_q_data_hex);
1046e15d496SJeremy L Thompson 
1056e15d496SJeremy L Thompson   // -- Bases
1064fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p_hex, q_hex, CEED_GAUSS, &basis_x_hex);
1074fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p_hex, q_hex, CEED_GAUSS, &basis_u_hex);
1086e15d496SJeremy L Thompson 
1096e15d496SJeremy L Thompson   // -- Operators
1102b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_hex);
1114fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "u", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
112356036faSJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "qdata", elem_restriction_q_data_hex, CEED_BASIS_NONE, q_data_hex);
1134fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "v", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
1146e15d496SJeremy L Thompson 
1156e15d496SJeremy L Thompson   // Set up Composite Operator
1166e15d496SJeremy L Thompson   // -- Create
117*ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_mass);
1186e15d496SJeremy L Thompson   // -- Add SubOperators
119*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_tet);
120*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_hex);
1216e15d496SJeremy L Thompson 
1226e15d496SJeremy L Thompson   // Estimate FLOPs
1236e15d496SJeremy L Thompson   CeedQFunctionSetUserFlopsEstimate(qf_mass, 1);
1246e15d496SJeremy L Thompson   CeedOperatorGetFlopsEstimate(op_mass, &flop_estimate);
1256e15d496SJeremy L Thompson 
1266e15d496SJeremy L Thompson   // Check output
127249f8407SJeremy L Thompson   if (flop_estimate != 3042) printf("Incorrect FLOP estimate computed, %" CeedSize_FMT " != 3042\n", flop_estimate);
1286e15d496SJeremy L Thompson 
1296e15d496SJeremy L Thompson   // Cleanup
1304fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_tet);
1314fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_hex);
1324fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_tet);
1334fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_tet);
1344fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_tet);
1354fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_hex);
1364fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_hex);
1374fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_hex);
1386e15d496SJeremy L Thompson   CeedBasisDestroy(&basis_u_tet);
1396e15d496SJeremy L Thompson   CeedBasisDestroy(&basis_x_tet);
1406e15d496SJeremy L Thompson   CeedBasisDestroy(&basis_u_hex);
1416e15d496SJeremy L Thompson   CeedBasisDestroy(&basis_x_hex);
1424fee36f0SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
1434fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_mass_tet);
1444fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_mass_hex);
1454fee36f0SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
1466e15d496SJeremy L Thompson   CeedDestroy(&ceed);
1476e15d496SJeremy L Thompson   return 0;
1486e15d496SJeremy L Thompson }
149