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