xref: /libCEED/tests/t523-operator.c (revision 57af422ce4fd2bed083e8288205852f8262d25d3)
12ebaca42Sjeremylt /// @file
231e5b035Sjeremylt /// Test creation, action, and destruction for mass matrix operator
331e5b035Sjeremylt /// \test Test creation, action, and destruction for mass matrix operator
42ebaca42Sjeremylt #include <ceed.h>
52ebaca42Sjeremylt #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson #include <stdlib.h>
82b730f8bSJeremy L Thompson 
92ebaca42Sjeremylt #include "t320-basis.h"
102ebaca42Sjeremylt #include "t510-operator.h"
112ebaca42Sjeremylt 
124fee36f0SJeremy L Thompson /* The mesh comprises of two rows of 3 quadrilaterals followed by one row
132ebaca42Sjeremylt      of 6 triangles:
142ebaca42Sjeremylt    _ _ _
152ebaca42Sjeremylt   |_|_|_|
162ebaca42Sjeremylt   |_|_|_|
172ebaca42Sjeremylt   |/|/|/|
182ebaca42Sjeremylt 
192ebaca42Sjeremylt */
202ebaca42Sjeremylt 
main(int argc,char ** argv)212ebaca42Sjeremylt int main(int argc, char **argv) {
222ebaca42Sjeremylt   Ceed                ceed;
23506b1a0cSSebastian Grimberg   CeedElemRestriction elem_restriction_x_tet, elem_restriction_u_tet, elem_restr_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;
262b730f8bSJeremy L Thompson   CeedQFunction qf_setup_tet, qf_mass_tet, qf_setup_hex, qf_mass_hex;
272b730f8bSJeremy L Thompson   CeedOperator  op_setup_tet, op_mass_tet, op_setup_hex, op_mass_hex, op_setup, op_mass;
284fee36f0SJeremy L Thompson   CeedVector    q_data_tet, q_data_hex, x;
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       nx = 3, ny = 3, nx_tet = 3, ny_tet = 1, nx_hex = 3;
312ebaca42Sjeremylt   CeedInt       row, col, offset;
324fee36f0SJeremy L Thompson   CeedInt       num_dofs = (nx * 2 + 1) * (ny * 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];
362ebaca42Sjeremylt 
372ebaca42Sjeremylt   CeedInit(argv[1], &ceed);
382ebaca42Sjeremylt 
392ebaca42Sjeremylt   // DoF Coordinates
404fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
412ebaca42Sjeremylt 
422ebaca42Sjeremylt   // Qdata Vectors
43d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_tet, &q_data_tet);
44d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_hex, &q_data_hex);
452ebaca42Sjeremylt 
46d1d35e2fSjeremylt   // Set up _tet Elements
474fee36f0SJeremy L Thompson   // -- Restrictions
484fee36f0SJeremy L Thompson   for (CeedInt i = 0; i < num_elem_tet / 2; i++) {
49d1d35e2fSjeremylt     col    = i % nx_tet;
50d1d35e2fSjeremylt     row    = i / nx_tet;
51d1d35e2fSjeremylt     offset = col * 2 + row * (nx_tet * 2 + 1) * 2;
522ebaca42Sjeremylt 
534fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 0] = 2 + offset;
544fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 1] = 9 + offset;
554fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 2] = 16 + offset;
564fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 3] = 1 + offset;
574fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 4] = 8 + offset;
584fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 5] = 0 + offset;
592ebaca42Sjeremylt 
604fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 6]  = 14 + offset;
614fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 7]  = 7 + offset;
624fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 8]  = 0 + offset;
634fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 9]  = 15 + offset;
644fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 10] = 8 + offset;
654fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 11] = 16 + offset;
662ebaca42Sjeremylt   }
674fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_tet, p_tet, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_tet,
684fee36f0SJeremy L Thompson                             &elem_restriction_x_tet);
694fee36f0SJeremy 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);
702ebaca42Sjeremylt 
714fee36f0SJeremy L Thompson   CeedInt strides_q_data_tet[3] = {1, q_tet, q_tet};
72506b1a0cSSebastian Grimberg   CeedElemRestrictionCreateStrided(ceed, num_elem_tet, q_tet, 1, num_qpts_tet, strides_q_data_tet, &elem_restr_q_data_tet);
732ebaca42Sjeremylt 
742ebaca42Sjeremylt   // -- Bases
754fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
764fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_x_tet);
772ebaca42Sjeremylt 
784fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
794fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_u_tet);
802ebaca42Sjeremylt 
812ebaca42Sjeremylt   // -- QFunctions
82d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_tet);
83a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_tet, "weight", 1, CEED_EVAL_WEIGHT);
84d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_tet, "dx", dim * dim, CEED_EVAL_GRAD);
85d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_tet, "rho", 1, CEED_EVAL_NONE);
862ebaca42Sjeremylt 
87d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_tet);
88d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "rho", 1, CEED_EVAL_NONE);
89d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "u", 1, CEED_EVAL_INTERP);
90d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_tet, "v", 1, CEED_EVAL_INTERP);
912ebaca42Sjeremylt 
922ebaca42Sjeremylt   // -- Operators
93d1d35e2fSjeremylt   // ---- Setup _tet
942b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_tet);
95ea6b5821SJeremy L Thompson   CeedOperatorSetName(op_setup_tet, "triangle elements");
962b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_tet, CEED_VECTOR_NONE);
974fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "dx", elem_restriction_x_tet, basis_x_tet, CEED_VECTOR_ACTIVE);
98506b1a0cSSebastian Grimberg   CeedOperatorSetField(op_setup_tet, "rho", elem_restr_q_data_tet, CEED_BASIS_NONE, q_data_tet);
99d1d35e2fSjeremylt   // ---- Mass _tet
1002b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_tet);
101ea6b5821SJeremy L Thompson   CeedOperatorSetName(op_mass_tet, "triangle elements");
102506b1a0cSSebastian Grimberg   CeedOperatorSetField(op_mass_tet, "rho", elem_restr_q_data_tet, CEED_BASIS_NONE, q_data_tet);
1034fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "u", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
1044fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "v", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
1052ebaca42Sjeremylt 
106d1d35e2fSjeremylt   // Set up _hex Elements
1074fee36f0SJeremy L Thompson   // -- Restrictions
1084fee36f0SJeremy L Thompson   for (CeedInt i = 0; i < num_elem_hex; i++) {
109d1d35e2fSjeremylt     col    = i % nx_hex;
110d1d35e2fSjeremylt     row    = i / nx_hex;
111d1d35e2fSjeremylt     offset = (nx_tet * 2 + 1) * (ny_tet * 2) * (1 + row) + col * 2;
1124fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p_hex; j++) {
1134fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p_hex; k++) ind_x_hex[p_hex * (p_hex * i + k) + j] = offset + k * (nx_hex * 2 + 1) + j;
1142b730f8bSJeremy L Thompson     }
1152ebaca42Sjeremylt   }
1164fee36f0SJeremy 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,
1174fee36f0SJeremy L Thompson                             &elem_restriction_x_hex);
1184fee36f0SJeremy 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);
1192ebaca42Sjeremylt 
1204fee36f0SJeremy L Thompson   CeedInt strides_q_data_hex[3] = {1, q_hex * q_hex, q_hex * q_hex};
1214fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_hex, q_hex * q_hex, 1, num_qpts_hex, strides_q_data_hex, &elem_restriction_q_data_hex);
1222ebaca42Sjeremylt 
1232ebaca42Sjeremylt   // -- Bases
1244fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p_hex, q_hex, CEED_GAUSS, &basis_x_hex);
1254fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p_hex, q_hex, CEED_GAUSS, &basis_u_hex);
1262ebaca42Sjeremylt 
1272ebaca42Sjeremylt   // -- QFunctions
128d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_hex);
129a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_hex, "weight", 1, CEED_EVAL_WEIGHT);
130d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_hex, "dx", dim * dim, CEED_EVAL_GRAD);
131d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_hex, "rho", 1, CEED_EVAL_NONE);
1322ebaca42Sjeremylt 
133d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_hex);
134d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "rho", 1, CEED_EVAL_NONE);
135d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "u", 1, CEED_EVAL_INTERP);
136d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_hex, "v", 1, CEED_EVAL_INTERP);
1372ebaca42Sjeremylt 
1382ebaca42Sjeremylt   // -- Operators
1392b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_hex);
1404fee36f0SJeremy L Thompson   CeedOperatorSetName(op_setup_hex, "quadrilateral elements");
1412b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_hex, CEED_VECTOR_NONE);
1424fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "dx", elem_restriction_x_hex, basis_x_hex, CEED_VECTOR_ACTIVE);
143356036faSJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_NONE, q_data_hex);
1442ebaca42Sjeremylt 
1452b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_hex);
1464fee36f0SJeremy L Thompson   CeedOperatorSetName(op_mass_hex, "quadrilateral elements");
147356036faSJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_NONE, q_data_hex);
1484fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "u", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
1494fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "v", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
1502ebaca42Sjeremylt 
1512ebaca42Sjeremylt   // Set up Composite Operators
1522ebaca42Sjeremylt   // -- Create
153ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_setup);
154ea6b5821SJeremy L Thompson   CeedOperatorSetName(op_setup, "setup");
1552ebaca42Sjeremylt   // -- Add SubOperators
156ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_setup, op_setup_tet);
157ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_setup, op_setup_hex);
1582ebaca42Sjeremylt 
1592ebaca42Sjeremylt   // -- Create
160ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_mass);
161ea6b5821SJeremy L Thompson   CeedOperatorSetName(op_mass, "mass");
1622ebaca42Sjeremylt   // -- Add SubOperators
163ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_tet);
164ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_hex);
1652ebaca42Sjeremylt 
1662ebaca42Sjeremylt   // View
167935f026aSJeremy L Thompson   CeedOperatorViewTerse(op_setup, stdout);
1682ebaca42Sjeremylt   CeedOperatorView(op_setup, stdout);
169*5a526491SJeremy L Thompson   CeedOperatorSetNumViewTabs(op_mass, 1);
170935f026aSJeremy L Thompson   CeedOperatorViewTerse(op_mass, stdout);
1712ebaca42Sjeremylt   CeedOperatorView(op_mass, stdout);
1722ebaca42Sjeremylt 
1732ebaca42Sjeremylt   // Cleanup
1744fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
1754fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_tet);
1764fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_hex);
1774fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_tet);
1784fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_tet);
179506b1a0cSSebastian Grimberg   CeedElemRestrictionDestroy(&elem_restr_q_data_tet);
1804fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_hex);
1814fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_hex);
1824fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_hex);
1834fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_tet);
1844fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_tet);
1854fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_hex);
1864fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_hex);
187d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_tet);
188d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_tet);
189d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_tet);
190d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_tet);
191d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_hex);
192d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_hex);
193d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_hex);
194d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_hex);
1952ebaca42Sjeremylt   CeedOperatorDestroy(&op_setup);
1962ebaca42Sjeremylt   CeedOperatorDestroy(&op_mass);
1972ebaca42Sjeremylt   CeedDestroy(&ceed);
1982ebaca42Sjeremylt   return 0;
1992ebaca42Sjeremylt }
200