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