xref: /libCEED/tests/t521-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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>
62b730f8bSJeremy L Thompson #include <stdlib.h>
72b730f8bSJeremy L Thompson 
852bfb9bbSJeremy L Thompson #include "t320-basis.h"
9a05f9790Sjeremylt #include "t510-operator.h"
104d537eeaSYohann 
11*4fee36f0SJeremy L Thompson /* The mesh comprises of two rows of 3 quadrilaterals followed by one row
1252d6035fSJeremy L Thompson      of 6 triangles:
1352d6035fSJeremy L Thompson    _ _ _
1452d6035fSJeremy L Thompson   |_|_|_|
1552d6035fSJeremy L Thompson   |_|_|_|
1652d6035fSJeremy L Thompson   |/|/|/|
1752d6035fSJeremy L Thompson 
1852d6035fSJeremy L Thompson */
1952d6035fSJeremy L Thompson 
2052d6035fSJeremy L Thompson int main(int argc, char **argv) {
2152d6035fSJeremy L Thompson   Ceed                ceed;
22*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x_tet, elem_restriction_u_tet, elem_restriction_q_data_tet, elem_restriction_x_hex, elem_restriction_u_hex,
23*4fee36f0SJeremy L Thompson       elem_restriction_q_data_hex;
242b730f8bSJeremy L Thompson   CeedBasis     basis_x_tet, basis_u_tet, basis_x_hex, basis_u_hex;
252b730f8bSJeremy L Thompson   CeedQFunction qf_setup_tet, qf_mass_tet, qf_setup_hex, qf_mass_hex;
262b730f8bSJeremy L Thompson   CeedOperator  op_setup_tet, op_mass_tet, op_setup_hex, op_mass_hex, op_setup, op_mass;
27*4fee36f0SJeremy L Thompson   CeedVector    q_data_tet, q_data_hex, x, u, v;
28*4fee36f0SJeremy 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;
292b730f8bSJeremy L Thompson   CeedInt       n_x = 3, n_y = 3, n_x_tet = 3, n_y_tet = 1, n_x_hex = 3;
3052d6035fSJeremy L Thompson   CeedInt       row, col, offset;
31*4fee36f0SJeremy 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;
32*4fee36f0SJeremy L Thompson   CeedInt       ind_x_tet[num_elem_tet * p_tet], ind_x_hex[num_elem_hex * p_hex * p_hex];
33*4fee36f0SJeremy L Thompson   CeedScalar    q_ref[dim * q_tet], q_weight[q_tet];
34*4fee36f0SJeremy L Thompson   CeedScalar    interp[p_tet * q_tet], grad[dim * p_tet * q_tet];
3552d6035fSJeremy L Thompson 
3652d6035fSJeremy L Thompson   CeedInit(argv[1], &ceed);
3752d6035fSJeremy L Thompson 
38*4fee36f0SJeremy L Thompson   // Vectors
39*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
40*4fee36f0SJeremy L Thompson   {
41*4fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
42*4fee36f0SJeremy L Thompson 
432b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < n_y * 2 + 1; i++) {
44d1d35e2fSjeremylt       for (CeedInt j = 0; j < n_x * 2 + 1; j++) {
45*4fee36f0SJeremy L Thompson         x_array[i + j * (n_y * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * n_y);
46*4fee36f0SJeremy L Thompson         x_array[i + j * (n_y * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * n_x);
4752d6035fSJeremy L Thompson       }
482b730f8bSJeremy L Thompson     }
49*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
50*4fee36f0SJeremy L Thompson   }
51*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
52*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
53d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_tet, &q_data_tet);
54d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts_hex, &q_data_hex);
5552d6035fSJeremy L Thompson 
5652d6035fSJeremy L Thompson   // Tet Elements
57*4fee36f0SJeremy L Thompson   // -- Restrictions
58d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem_tet / 2; i++) {
59d1d35e2fSjeremylt     col    = i % n_x_tet;
60d1d35e2fSjeremylt     row    = i / n_x_tet;
61d1d35e2fSjeremylt     offset = col * 2 + row * (n_x_tet * 2 + 1) * 2;
6252d6035fSJeremy L Thompson 
63*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 0] = 2 + offset;
64*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 1] = 9 + offset;
65*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 2] = 16 + offset;
66*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 3] = 1 + offset;
67*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 4] = 8 + offset;
68*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 5] = 0 + offset;
6952d6035fSJeremy L Thompson 
70*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 6]  = 14 + offset;
71*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 7]  = 7 + offset;
72*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 8]  = 0 + offset;
73*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 9]  = 15 + offset;
74*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 10] = 8 + offset;
75*4fee36f0SJeremy L Thompson     ind_x_tet[i * 2 * p_tet + 11] = 16 + offset;
7652d6035fSJeremy L Thompson   }
77*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem_tet, p_tet, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x_tet,
78*4fee36f0SJeremy L Thompson                             &elem_restriction_x_tet);
79*4fee36f0SJeremy 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);
8052d6035fSJeremy L Thompson 
81*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_tet[3] = {1, q_tet, q_tet};
82*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_tet, q_tet, 1, num_qpts_tet, strides_q_data_tet, &elem_restriction_q_data_tet);
8352d6035fSJeremy L Thompson 
8452d6035fSJeremy L Thompson   // -- Bases
85*4fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
86*4fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_x_tet);
8752d6035fSJeremy L Thompson 
88*4fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
89*4fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p_tet, q_tet, interp, grad, q_ref, q_weight, &basis_u_tet);
9052d6035fSJeremy L Thompson 
9152d6035fSJeremy L Thompson   // -- QFunctions
92d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_tet);
93a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_tet, "weight", 1, CEED_EVAL_WEIGHT);
94d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_tet, "dx", dim * dim, CEED_EVAL_GRAD);
95d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_tet, "rho", 1, CEED_EVAL_NONE);
9652d6035fSJeremy L Thompson 
97d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_tet);
98d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "rho", 1, CEED_EVAL_NONE);
99d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_tet, "u", 1, CEED_EVAL_INTERP);
100d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_tet, "v", 1, CEED_EVAL_INTERP);
10152d6035fSJeremy L Thompson 
10252d6035fSJeremy L Thompson   // -- Operators
10352d6035fSJeremy L Thompson   // ---- Setup Tet
1042b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_tet);
1052b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "weight", CEED_ELEMRESTRICTION_NONE, basis_x_tet, CEED_VECTOR_NONE);
106*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "dx", elem_restriction_x_tet, basis_x_tet, CEED_VECTOR_ACTIVE);
107*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_tet, "rho", elem_restriction_q_data_tet, CEED_BASIS_COLLOCATED, q_data_tet);
10852d6035fSJeremy L Thompson   // ---- Mass Tet
1092b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_tet, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_tet);
110*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "rho", elem_restriction_q_data_tet, CEED_BASIS_COLLOCATED, q_data_tet);
111*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "u", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
112*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_tet, "v", elem_restriction_u_tet, basis_u_tet, CEED_VECTOR_ACTIVE);
11352d6035fSJeremy L Thompson 
11452d6035fSJeremy L Thompson   // Hex Elements
115*4fee36f0SJeremy L Thompson   // -- Restrictions
116d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem_hex; i++) {
117d1d35e2fSjeremylt     col    = i % n_x_hex;
118d1d35e2fSjeremylt     row    = i / n_x_hex;
119d1d35e2fSjeremylt     offset = (n_x_tet * 2 + 1) * (n_y_tet * 2) * (1 + row) + col * 2;
120*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p_hex; j++) {
121*4fee36f0SJeremy 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;
1222b730f8bSJeremy L Thompson     }
12352d6035fSJeremy L Thompson   }
124*4fee36f0SJeremy 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,
125*4fee36f0SJeremy L Thompson                             &elem_restriction_x_hex);
126*4fee36f0SJeremy 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);
12752d6035fSJeremy L Thompson 
128*4fee36f0SJeremy L Thompson   CeedInt strides_q_data_hex[3] = {1, q_hex * q_hex, q_hex * q_hex};
129*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem_hex, q_hex * q_hex, 1, num_qpts_hex, strides_q_data_hex, &elem_restriction_q_data_hex);
13052d6035fSJeremy L Thompson 
13152d6035fSJeremy L Thompson   // -- Bases
132*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p_hex, q_hex, CEED_GAUSS, &basis_x_hex);
133*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p_hex, q_hex, CEED_GAUSS, &basis_u_hex);
13452d6035fSJeremy L Thompson 
13552d6035fSJeremy L Thompson   // -- QFunctions
136d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup_hex);
137d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_hex, "_weight", 1, CEED_EVAL_WEIGHT);
138d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_setup_hex, "dx", dim * dim, CEED_EVAL_GRAD);
139d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_setup_hex, "rho", 1, CEED_EVAL_NONE);
14052d6035fSJeremy L Thompson 
141d1d35e2fSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass_hex);
142d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "rho", 1, CEED_EVAL_NONE);
143d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass_hex, "u", 1, CEED_EVAL_INTERP);
144d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass_hex, "v", 1, CEED_EVAL_INTERP);
14552d6035fSJeremy L Thompson 
14652d6035fSJeremy L Thompson   // -- Operators
1472b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup_hex);
1482b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "_weight", CEED_ELEMRESTRICTION_NONE, basis_x_hex, CEED_VECTOR_NONE);
149*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "dx", elem_restriction_x_hex, basis_x_hex, CEED_VECTOR_ACTIVE);
150*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_COLLOCATED, q_data_hex);
15152d6035fSJeremy L Thompson 
1522b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass_hex, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass_hex);
153*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "rho", elem_restriction_q_data_hex, CEED_BASIS_COLLOCATED, q_data_hex);
154*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "u", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
155*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass_hex, "v", elem_restriction_u_hex, basis_u_hex, CEED_VECTOR_ACTIVE);
15652d6035fSJeremy L Thompson 
15752d6035fSJeremy L Thompson   // Composite Operators
15852d6035fSJeremy L Thompson   CeedCompositeOperatorCreate(ceed, &op_setup);
159d1d35e2fSjeremylt   CeedCompositeOperatorAddSub(op_setup, op_setup_tet);
160d1d35e2fSjeremylt   CeedCompositeOperatorAddSub(op_setup, op_setup_hex);
16152d6035fSJeremy L Thompson 
16252d6035fSJeremy L Thompson   CeedCompositeOperatorCreate(ceed, &op_mass);
163d1d35e2fSjeremylt   CeedCompositeOperatorAddSub(op_mass, op_mass_tet);
164d1d35e2fSjeremylt   CeedCompositeOperatorAddSub(op_mass, op_mass_hex);
16552d6035fSJeremy L Thompson 
16652d6035fSJeremy L Thompson   // Apply Setup Operator
167*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
16852d6035fSJeremy L Thompson 
16952d6035fSJeremy L Thompson   // Apply Mass Operator
170*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
171*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
17252d6035fSJeremy L Thompson 
17352d6035fSJeremy L Thompson   // Check output
174*4fee36f0SJeremy L Thompson   {
175*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
176*4fee36f0SJeremy L Thompson     CeedScalar        sum = 0.;
177*4fee36f0SJeremy L Thompson 
178*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
179*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) sum += v_array[i];
180*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1812b730f8bSJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
182*4fee36f0SJeremy L Thompson   }
18352d6035fSJeremy L Thompson 
18452d6035fSJeremy L Thompson   // Cleanup
185*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
186*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
187*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
188*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_tet);
189*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data_hex);
190*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_tet);
191*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_tet);
192*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_tet);
193*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u_hex);
194*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_hex);
195*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data_hex);
196*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_tet);
197*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_tet);
198*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u_hex);
199*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x_hex);
200d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_tet);
201d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_tet);
202d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_tet);
203d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_tet);
204d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_setup_hex);
205d1d35e2fSjeremylt   CeedQFunctionDestroy(&qf_mass_hex);
206d1d35e2fSjeremylt   CeedOperatorDestroy(&op_setup_hex);
207d1d35e2fSjeremylt   CeedOperatorDestroy(&op_mass_hex);
20852d6035fSJeremy L Thompson   CeedOperatorDestroy(&op_setup);
20952d6035fSJeremy L Thompson   CeedOperatorDestroy(&op_mass);
21052d6035fSJeremy L Thompson   CeedDestroy(&ceed);
21152d6035fSJeremy L Thompson   return 0;
21252d6035fSJeremy L Thompson }
213