xref: /libCEED/tests/t566-operator.c (revision 49d98eadca5c128038c3bbc05f6f2f0bf7cf0252)
1b8edc0e7SJeremy L Thompson /// @file
2b8edc0e7SJeremy L Thompson /// Test assembly of non-symmetric mass matrix operator (multi-component) see t537
3b8edc0e7SJeremy L Thompson /// \test Test assembly of non-symmetric mass matrix operator (multi-component)
4b8edc0e7SJeremy L Thompson #include <ceed.h>
5b8edc0e7SJeremy L Thompson #include <stdlib.h>
6b8edc0e7SJeremy L Thompson #include <math.h>
7b8edc0e7SJeremy L Thompson #include "t566-operator.h"
8b8edc0e7SJeremy L Thompson 
9b8edc0e7SJeremy L Thompson int main(int argc, char **argv) {
10b8edc0e7SJeremy L Thompson   Ceed ceed;
11b8edc0e7SJeremy L Thompson   CeedElemRestriction elem_restr_x, elem_restr_u,
12b8edc0e7SJeremy L Thompson                       elem_restr_qd_i;
13b8edc0e7SJeremy L Thompson   CeedBasis basis_x, basis_u;
14b8edc0e7SJeremy L Thompson   CeedQFunction qf_setup, qf_mass;
15b8edc0e7SJeremy L Thompson   CeedOperator op_setup, op_mass;
16b8edc0e7SJeremy L Thompson   CeedVector q_data, X, U, V;
17b8edc0e7SJeremy L Thompson   CeedInt P = 3, Q = 3, dim = 2, num_comp = 2;
18b8edc0e7SJeremy L Thompson   CeedInt n_x = 1, n_y = 1;
19b8edc0e7SJeremy L Thompson   CeedInt num_elem = n_x * n_y;
20*49d98eadSJeremy L Thompson   CeedInt num_dofs = (n_x*(P-1)+1)*(n_y*(P-1)+1), num_qpts = num_elem*Q*Q;
21b8edc0e7SJeremy L Thompson   CeedInt ind_x[num_elem*P*P];
22b8edc0e7SJeremy L Thompson   CeedScalar assembled[num_comp*num_comp*num_dofs*num_dofs];
23b8edc0e7SJeremy L Thompson   CeedScalar x[dim*num_dofs], assembled_true[num_comp*num_comp*num_dofs*num_dofs];
24b8edc0e7SJeremy L Thompson   CeedScalar *u;
25b8edc0e7SJeremy L Thompson   const CeedScalar *v;
26b8edc0e7SJeremy L Thompson 
27b8edc0e7SJeremy L Thompson   CeedInit(argv[1], &ceed);
28b8edc0e7SJeremy L Thompson 
29b8edc0e7SJeremy L Thompson   // DoF Coordinates
30b8edc0e7SJeremy L Thompson   for (CeedInt i=0; i<n_x*2+1; i++)
31b8edc0e7SJeremy L Thompson     for (CeedInt j=0; j<n_y*2+1; j++) {
32b8edc0e7SJeremy L Thompson       x[i+j*(n_x*2+1)+0*num_dofs] = (CeedScalar) i / (2*n_x);
33b8edc0e7SJeremy L Thompson       x[i+j*(n_x*2+1)+1*num_dofs] = (CeedScalar) j / (2*n_y);
34b8edc0e7SJeremy L Thompson     }
35b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, dim*num_dofs, &X);
36b8edc0e7SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
37b8edc0e7SJeremy L Thompson 
38b8edc0e7SJeremy L Thompson   // Qdata Vector
39b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts, &q_data);
40b8edc0e7SJeremy L Thompson 
41b8edc0e7SJeremy L Thompson   // Element Setup
42b8edc0e7SJeremy L Thompson   for (CeedInt i=0; i<num_elem; i++) {
43b8edc0e7SJeremy L Thompson     CeedInt col, row, offset;
44b8edc0e7SJeremy L Thompson     col = i % n_x;
45b8edc0e7SJeremy L Thompson     row = i / n_x;
46b8edc0e7SJeremy L Thompson     offset = col*(P-1) + row*(n_x*2+1)*(P-1);
47b8edc0e7SJeremy L Thompson     for (CeedInt j=0; j<P; j++)
48b8edc0e7SJeremy L Thompson       for (CeedInt k=0; k<P; k++)
49b8edc0e7SJeremy L Thompson         ind_x[P*(P*i+k)+j] = offset + k*(n_x*2+1) + j;
50b8edc0e7SJeremy L Thompson   }
51b8edc0e7SJeremy L Thompson 
52b8edc0e7SJeremy L Thompson   // Restrictions
53b8edc0e7SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, P*P, dim, num_dofs, dim*num_dofs,
54b8edc0e7SJeremy L Thompson                             CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restr_x);
55b8edc0e7SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, P*P, num_comp, num_dofs,
56b8edc0e7SJeremy L Thompson                             num_comp*num_dofs,
57b8edc0e7SJeremy L Thompson                             CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restr_u);
58b8edc0e7SJeremy L Thompson   CeedInt strides_qd[3] = {1, Q*Q, Q*Q};
59b8edc0e7SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, Q*Q, 1, num_qpts, strides_qd,
60b8edc0e7SJeremy L Thompson                                    &elem_restr_qd_i);
61b8edc0e7SJeremy L Thompson 
62b8edc0e7SJeremy L Thompson   // Bases
63b8edc0e7SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &basis_x);
64b8edc0e7SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, P, Q, CEED_GAUSS,
65b8edc0e7SJeremy L Thompson                                   &basis_u);
66b8edc0e7SJeremy L Thompson 
67b8edc0e7SJeremy L Thompson   // QFunctions
68b8edc0e7SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
69b8edc0e7SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
70b8edc0e7SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD);
71b8edc0e7SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
72b8edc0e7SJeremy L Thompson 
73b8edc0e7SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
74b8edc0e7SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
75b8edc0e7SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP);
76b8edc0e7SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP);
77b8edc0e7SJeremy L Thompson 
78b8edc0e7SJeremy L Thompson   // Operators
79b8edc0e7SJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
80b8edc0e7SJeremy L Thompson                      &op_setup);
81b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x,
82b8edc0e7SJeremy L Thompson                        CEED_VECTOR_NONE);
83b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
84b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED,
85b8edc0e7SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
86b8edc0e7SJeremy L Thompson 
87b8edc0e7SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
88b8edc0e7SJeremy L Thompson                      &op_mass);
89b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED,
90b8edc0e7SJeremy L Thompson                        q_data);
91b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
92b8edc0e7SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
93b8edc0e7SJeremy L Thompson 
94b8edc0e7SJeremy L Thompson   // Apply Setup Operator
95b8edc0e7SJeremy L Thompson   CeedOperatorApply(op_setup, X, q_data, CEED_REQUEST_IMMEDIATE);
96b8edc0e7SJeremy L Thompson 
97b8edc0e7SJeremy L Thompson   // Fuly assemble operator
98b8edc0e7SJeremy L Thompson   for (CeedInt k=0; k<num_comp*num_comp*num_dofs*num_dofs; k++) {
99b8edc0e7SJeremy L Thompson     assembled[k] = 0.0;
100b8edc0e7SJeremy L Thompson     assembled_true[k] = 0.0;
101b8edc0e7SJeremy L Thompson   }
102b8edc0e7SJeremy L Thompson   CeedSize nentries;
103b8edc0e7SJeremy L Thompson   CeedInt *rows;
104b8edc0e7SJeremy L Thompson   CeedInt *cols;
105b8edc0e7SJeremy L Thompson   CeedVector values;
106b8edc0e7SJeremy L Thompson   CeedOperatorLinearAssembleSymbolic(op_mass, &nentries, &rows, &cols);
107b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, nentries, &values);
108b8edc0e7SJeremy L Thompson   CeedOperatorLinearAssemble(op_mass, values);
109b8edc0e7SJeremy L Thompson   const CeedScalar *vals;
110b8edc0e7SJeremy L Thompson   CeedVectorGetArrayRead(values, CEED_MEM_HOST, &vals);
111b8edc0e7SJeremy L Thompson   for (CeedInt k=0; k<nentries; k++) {
112b8edc0e7SJeremy L Thompson     assembled[rows[k]*num_comp*num_dofs + cols[k]] += vals[k];
113b8edc0e7SJeremy L Thompson   }
114b8edc0e7SJeremy L Thompson   CeedVectorRestoreArrayRead(values, &vals);
115b8edc0e7SJeremy L Thompson 
116b8edc0e7SJeremy L Thompson   // Manually assemble operator
117b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, num_comp*num_dofs, &U);
118b8edc0e7SJeremy L Thompson   CeedVectorSetValue(U, 0.0);
119b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, num_comp*num_dofs, &V);
120b8edc0e7SJeremy L Thompson   CeedInt indOld = -1;
121b8edc0e7SJeremy L Thompson 
122b8edc0e7SJeremy L Thompson   for (CeedInt comp_in=0; comp_in<num_comp; comp_in++) {
123b8edc0e7SJeremy L Thompson     for (CeedInt node_in=0; node_in<num_dofs; node_in++) {
124b8edc0e7SJeremy L Thompson       // Set input
125b8edc0e7SJeremy L Thompson       CeedVectorGetArray(U, CEED_MEM_HOST, &u);
126b8edc0e7SJeremy L Thompson       CeedInt ind = node_in + comp_in*num_dofs;
127b8edc0e7SJeremy L Thompson       u[ind] = 1.0;
128b8edc0e7SJeremy L Thompson       if (ind > 0)
129b8edc0e7SJeremy L Thompson         u[indOld] = 0.0;
130b8edc0e7SJeremy L Thompson       indOld = ind;
131b8edc0e7SJeremy L Thompson       CeedVectorRestoreArray(U, &u);
132b8edc0e7SJeremy L Thompson 
133b8edc0e7SJeremy L Thompson       // Compute effect of DoF j
134b8edc0e7SJeremy L Thompson       CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
135b8edc0e7SJeremy L Thompson 
136b8edc0e7SJeremy L Thompson       CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
137b8edc0e7SJeremy L Thompson       for (CeedInt k=0; k<num_dofs*num_comp; k++) {
138b8edc0e7SJeremy L Thompson         assembled_true[ind*num_dofs*num_comp + k] = v[k];
139b8edc0e7SJeremy L Thompson       }
140b8edc0e7SJeremy L Thompson       CeedVectorRestoreArrayRead(V, &v);
141b8edc0e7SJeremy L Thompson     }
142b8edc0e7SJeremy L Thompson   }
143b8edc0e7SJeremy L Thompson 
144b8edc0e7SJeremy L Thompson   // Check output
145b8edc0e7SJeremy L Thompson   for (CeedInt node_in=0; node_in<num_dofs; node_in++) {
146b8edc0e7SJeremy L Thompson     for (CeedInt comp_in=0; comp_in<num_comp; comp_in++) {
147b8edc0e7SJeremy L Thompson       for (CeedInt node_out=0; node_out<num_dofs; node_out++) {
148b8edc0e7SJeremy L Thompson         for (CeedInt comp_out=0; comp_out<num_comp; comp_out++) {
149b8edc0e7SJeremy L Thompson           const CeedInt index = (node_in + comp_in*num_dofs)*num_comp + node_out +
150b8edc0e7SJeremy L Thompson                                 comp_out*num_dofs;
151b8edc0e7SJeremy L Thompson           const CeedScalar assembled_value = assembled[index];
152b8edc0e7SJeremy L Thompson           const CeedScalar assembled_true_value = assembled_true[index];
153b8edc0e7SJeremy L Thompson           if (fabs(assembled_value - assembled_true_value) >
154b8edc0e7SJeremy L Thompson               100.*CEED_EPSILON)
155b8edc0e7SJeremy L Thompson             // LCOV_EXCL_START
156b8edc0e7SJeremy L Thompson             printf("[(%d, %d), (%d, %d)] Error in assembly: %f != %f\n",
157b8edc0e7SJeremy L Thompson                    node_in, comp_in, node_out, comp_out,
158b8edc0e7SJeremy L Thompson                    assembled_value, assembled_true_value);
159b8edc0e7SJeremy L Thompson           // LCOV_EXCL_STOP
160b8edc0e7SJeremy L Thompson         }
161b8edc0e7SJeremy L Thompson       }
162b8edc0e7SJeremy L Thompson     }
163b8edc0e7SJeremy L Thompson   }
164b8edc0e7SJeremy L Thompson 
165b8edc0e7SJeremy L Thompson   // Cleanup
166b8edc0e7SJeremy L Thompson   free(rows);
167b8edc0e7SJeremy L Thompson   free(cols);
168b8edc0e7SJeremy L Thompson   CeedVectorDestroy(&values);
169b8edc0e7SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
170b8edc0e7SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
171b8edc0e7SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
172b8edc0e7SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
173b8edc0e7SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_u);
174b8edc0e7SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_x);
175b8edc0e7SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_qd_i);
176b8edc0e7SJeremy L Thompson   CeedBasisDestroy(&basis_u);
177b8edc0e7SJeremy L Thompson   CeedBasisDestroy(&basis_x);
178b8edc0e7SJeremy L Thompson   CeedVectorDestroy(&X);
179b8edc0e7SJeremy L Thompson   CeedVectorDestroy(&q_data);
180b8edc0e7SJeremy L Thompson   CeedVectorDestroy(&U);
181b8edc0e7SJeremy L Thompson   CeedVectorDestroy(&V);
182b8edc0e7SJeremy L Thompson   CeedDestroy(&ceed);
183b8edc0e7SJeremy L Thompson   return 0;
184b8edc0e7SJeremy L Thompson }
185