xref: /libCEED/tests/t520-operator.c (revision 1ad466088344f9c7a89b938b3cea7230e969fe10)
152d6035fSJeremy L Thompson /// @file
26227f746SJeremy L Thompson /// Test creation, action, and destruction for composite mass matrix operator
36227f746SJeremy L Thompson /// \test Test creation, action, and destruction for composite mass matrix operator
452d6035fSJeremy L Thompson #include <ceed.h>
552d6035fSJeremy L Thompson #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson #include <stdlib.h>
82b730f8bSJeremy L Thompson 
952bfb9bbSJeremy L Thompson #include "t320-basis.h"
10a05f9790Sjeremylt #include "t510-operator.h"
114d537eeaSYohann 
124fee36f0SJeremy L Thompson /* The mesh comprises of two rows of 3 quadrilaterals followed by one row
1352d6035fSJeremy L Thompson      of 6 triangles:
1452d6035fSJeremy L Thompson    _ _ _
1552d6035fSJeremy L Thompson   |_|_|_|
1652d6035fSJeremy L Thompson   |_|_|_|
1752d6035fSJeremy L Thompson   |/|/|/|
1852d6035fSJeremy L Thompson 
1952d6035fSJeremy L Thompson */
2052d6035fSJeremy L Thompson 
main(int argc,char ** argv)2152d6035fSJeremy L Thompson int main(int argc, char **argv) {
2252d6035fSJeremy L Thompson   Ceed                ceed;
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;
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, u, v;
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;
3152d6035fSJeremy 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];
3652d6035fSJeremy L Thompson 
3752d6035fSJeremy L Thompson   CeedInit(argv[1], &ceed);
3852d6035fSJeremy L Thompson 
394fee36f0SJeremy L Thompson   // Vectors
404fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
414fee36f0SJeremy L Thompson   {
424fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
434fee36f0SJeremy L Thompson 
442b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < n_y * 2 + 1; i++) {
45d1d35e2fSjeremylt       for (CeedInt j = 0; j < n_x * 2 + 1; j++) {
464fee36f0SJeremy L Thompson         x_array[i + j * (n_y * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * n_y);
474fee36f0SJeremy L Thompson         x_array[i + j * (n_y * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * n_x);
4852d6035fSJeremy L Thompson       }
492b730f8bSJeremy L Thompson     }
504fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
514fee36f0SJeremy L Thompson   }
524fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
534fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
54d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_tet, &q_data_tet);
55d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_hex, &q_data_hex);
5652d6035fSJeremy L Thompson 
5752d6035fSJeremy L Thompson   // Set up Tet Elements
584fee36f0SJeremy L Thompson   // -- Restrictions
59d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem_tet / 2; i++) {
60d1d35e2fSjeremylt     col    = i % n_x_tet;
61d1d35e2fSjeremylt     row    = i / n_x_tet;
62d1d35e2fSjeremylt     offset = col * 2 + row * (n_x_tet * 2 + 1) * 2;
6352d6035fSJeremy L Thompson 
644fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 0] = 2 + offset;
654fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 1] = 9 + offset;
664fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 2] = 16 + offset;
674fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 3] = 1 + offset;
684fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 4] = 8 + offset;
694fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 5] = 0 + offset;
7052d6035fSJeremy L Thompson 
714fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 6]  = 14 + offset;
724fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 7]  = 7 + offset;
734fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 8]  = 0 + offset;
744fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 9]  = 15 + offset;
754fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 10] = 8 + offset;
764fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 11] = 16 + offset;
7752d6035fSJeremy L Thompson   }
784fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_tet, p_tet, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_tet,
794fee36f0SJeremy L Thompson                             &elem_restriction_x_tet);
804fee36f0SJeremy 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);
8152d6035fSJeremy L Thompson 
824fee36f0SJeremy L Thompson   CeedInt strides_q_data_tet[3] = {1, q_tet, q_tet};
834fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_tet, q_tet, 1, num_qpts_tet, strides_q_data_tet, &elem_restriction_q_data_tet);
8452d6035fSJeremy L Thompson 
8552d6035fSJeremy L Thompson   // -- Bases
864fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
874fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_x_tet);
8852d6035fSJeremy L Thompson 
894fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
904fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_u_tet);
9152d6035fSJeremy L Thompson 
9252d6035fSJeremy L Thompson   // -- QFunctions
93d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_tet);
94d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_tet, "_weight", 1, CEED_EVAL_WEIGHT);
95d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_tet, "dx", dim * dim, CEED_EVAL_GRAD);
96d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_tet, "rho", 1, CEED_EVAL_NONE);
9752d6035fSJeremy L Thompson 
98d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_tet);
99d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "rho", 1, CEED_EVAL_NONE);
100d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "u", 1, CEED_EVAL_INTERP);
101d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_tet, "v", 1, CEED_EVAL_INTERP);
10252d6035fSJeremy L Thompson 
10352d6035fSJeremy L Thompson   // -- Operators
10452d6035fSJeremy L Thompson   // ---- Setup Tet
1052b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_tet);
1062b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "_weight", CEED_ELEMRESTRICTION_NONE, basis_x_tet, CEED_VECTOR_NONE);
1074fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "dx", elem_restriction_x_tet, basis_x_tet, CEED_VECTOR_ACTIVE);
108356036faSJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "rho", elem_restriction_q_data_tet, CEED_BASIS_NONE, q_data_tet);
10952d6035fSJeremy L Thompson   // ---- Mass Tet
1102b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_tet);
111356036faSJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "rho", elem_restriction_q_data_tet, CEED_BASIS_NONE, q_data_tet);
1124fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "u", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
1134fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "v", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
1148a297abdSJames Wright   CeedOperatorSetName(op_mass_tet, "mass tet");
11552d6035fSJeremy L Thompson 
11652d6035fSJeremy L Thompson   // Set up Hex Elements
1174fee36f0SJeremy L Thompson   // -- Restrictions
118d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem_hex; i++) {
119d1d35e2fSjeremylt     col    = i % n_x_hex;
120d1d35e2fSjeremylt     row    = i / n_x_hex;
121d1d35e2fSjeremylt     offset = (n_x_tet * 2 + 1) * (n_y_tet * 2) * (1 + row) + col * 2;
1224fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p_hex; j++) {
1234fee36f0SJeremy 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;
1242b730f8bSJeremy L Thompson     }
12552d6035fSJeremy L Thompson   }
1264fee36f0SJeremy 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,
1274fee36f0SJeremy L Thompson                             &elem_restriction_x_hex);
1284fee36f0SJeremy 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);
12952d6035fSJeremy L Thompson 
1304fee36f0SJeremy L Thompson   CeedInt strides_q_data_hex[3] = {1, q_hex * q_hex, q_hex * q_hex};
1314fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_hex, q_hex * q_hex, 1, num_qpts_hex, strides_q_data_hex, &elem_restriction_q_data_hex);
13252d6035fSJeremy L Thompson 
13352d6035fSJeremy L Thompson   // -- Bases
1344fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p_hex, q_hex, CEED_GAUSS, &basis_x_hex);
1354fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p_hex, q_hex, CEED_GAUSS, &basis_u_hex);
13652d6035fSJeremy L Thompson 
13752d6035fSJeremy L Thompson   // -- QFunctions
138d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_hex);
139a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_hex, "weight", 1, CEED_EVAL_WEIGHT);
140d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_hex, "dx", dim * dim, CEED_EVAL_GRAD);
141d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_hex, "rho", 1, CEED_EVAL_NONE);
14252d6035fSJeremy L Thompson 
143d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_hex);
144d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "rho", 1, CEED_EVAL_NONE);
145d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "u", 1, CEED_EVAL_INTERP);
146d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_hex, "v", 1, CEED_EVAL_INTERP);
14752d6035fSJeremy L Thompson 
14852d6035fSJeremy L Thompson   // -- Operators
1492b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_hex);
1502b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_hex, CEED_VECTOR_NONE);
1514fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "dx", elem_restriction_x_hex, basis_x_hex, CEED_VECTOR_ACTIVE);
152356036faSJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_NONE, q_data_hex);
15352d6035fSJeremy L Thompson 
1542b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_hex);
155356036faSJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_NONE, q_data_hex);
1564fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "u", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
1574fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "v", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
1588a297abdSJames Wright   CeedOperatorSetName(op_mass_hex, "mass hex");
15952d6035fSJeremy L Thompson 
16052d6035fSJeremy L Thompson   // Set up Composite Operators
16152d6035fSJeremy L Thompson   // -- Create
162*ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_setup);
16352d6035fSJeremy L Thompson   // -- Add SubOperators
164*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_setup, op_setup_tet);
165*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_setup, op_setup_hex);
16652d6035fSJeremy L Thompson 
16752d6035fSJeremy L Thompson   // -- Create
168*ed094490SJeremy L Thompson   CeedOperatorCreateComposite(ceed, &op_mass);
16952d6035fSJeremy L Thompson   // -- Add SubOperators
170*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_tet);
171*ed094490SJeremy L Thompson   CeedOperatorCompositeAddSub(op_mass, op_mass_hex);
17252d6035fSJeremy L Thompson 
173*ed094490SJeremy L Thompson   {  // Test CeedOperatorCompositeGetSubByName
1748a297abdSJames Wright     CeedOperator op_byname;
1758a297abdSJames Wright 
176*ed094490SJeremy L Thompson     CeedOperatorCompositeGetSubByName(op_mass, "mass hex", &op_byname);
177*ed094490SJeremy L Thompson     if (op_byname != op_mass_hex) printf("CeedOperatorCompositeGetSubByName returned incorrect Sub Operator");
1788a297abdSJames Wright 
179*ed094490SJeremy L Thompson     CeedOperatorCompositeGetSubByName(op_mass, "asdf", &op_byname);
180*ed094490SJeremy L Thompson     if (op_byname != NULL) printf("CeedOperatorCompositeGetSubByName returned non-NULL for non-existent Sub Operator");
1818a297abdSJames Wright   }
1828a297abdSJames Wright 
18352d6035fSJeremy L Thompson   // Apply Setup Operator
1844fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
18552d6035fSJeremy L Thompson 
18652d6035fSJeremy L Thompson   // Apply Mass Operator
1874fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
1884fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
18952d6035fSJeremy L Thompson 
19052d6035fSJeremy L Thompson   // Check output
1914fee36f0SJeremy L Thompson   {
1924fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1934fee36f0SJeremy L Thompson 
1944fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1952b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) {
1964fee36f0SJeremy L Thompson       if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
1972b730f8bSJeremy L Thompson     }
1984fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1994fee36f0SJeremy L Thompson   }
20052d6035fSJeremy L Thompson 
20152d6035fSJeremy L Thompson   // Cleanup
2024fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
2034fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
2044fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
2054fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_tet);
2064fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_hex);
2074fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_tet);
2084fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_tet);
2094fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_tet);
2104fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_hex);
2114fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_hex);
2124fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_hex);
2134fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_tet);
2144fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_tet);
2154fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_hex);
2164fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_hex);
217d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_tet);
218d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_tet);
219d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_tet);
220d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_tet);
221d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_hex);
222d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_hex);
223d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_hex);
224d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_hex);
22552d6035fSJeremy L Thompson   CeedOperatorDestroy(&op_setup);
22652d6035fSJeremy L Thompson   CeedOperatorDestroy(&op_mass);
22752d6035fSJeremy L Thompson   CeedDestroy(&ceed);
22852d6035fSJeremy L Thompson   return 0;
22952d6035fSJeremy L Thompson }
230