xref: /libCEED/tests/t566-operator.c (revision 506b1a0c535297a01950817d4491440b9ddb5287)
1b8edc0e7SJeremy L Thompson /// @file
2*506b1a0cSSebastian Grimberg /// Test full assembly of non-symmetric mass matrix operator (multi-component) (see t537)
3*506b1a0cSSebastian Grimberg /// \test Test full assembly of non-symmetric mass matrix operator (multi-component)
4ac415f71SSebastian Grimberg #include "t566-operator.h"
5ac415f71SSebastian Grimberg 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson #include <math.h>
849aac155SJeremy L Thompson #include <stdio.h>
92b730f8bSJeremy L Thompson #include <stdlib.h>
102b730f8bSJeremy L Thompson 
main(int argc,char ** argv)11b8edc0e7SJeremy L Thompson int main(int argc, char **argv) {
12b8edc0e7SJeremy L Thompson   Ceed                ceed;
134fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
14b8edc0e7SJeremy L Thompson   CeedBasis           basis_x, basis_u;
15b8edc0e7SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
16b8edc0e7SJeremy L Thompson   CeedOperator        op_setup, op_mass;
174fee36f0SJeremy L Thompson   CeedVector          q_data, x, u, v;
184fee36f0SJeremy L Thompson   CeedInt             p = 3, q = 3, dim = 2, num_comp = 2;
19b8edc0e7SJeremy L Thompson   CeedInt             n_x = 1, n_y = 1;
20b8edc0e7SJeremy L Thompson   CeedInt             num_elem = n_x * n_y;
214fee36f0SJeremy L Thompson   CeedInt             num_dofs = (n_x * (p - 1) + 1) * (n_y * (p - 1) + 1), num_qpts = num_elem * q * q;
224fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p * p];
234fee36f0SJeremy L Thompson   CeedScalar          assembled_values[num_comp * num_comp * num_dofs * num_dofs];
244fee36f0SJeremy L Thompson   CeedScalar          assembled_true[num_comp * num_comp * num_dofs * num_dofs];
25b8edc0e7SJeremy L Thompson 
26b8edc0e7SJeremy L Thompson   CeedInit(argv[1], &ceed);
27b8edc0e7SJeremy L Thompson 
284fee36f0SJeremy L Thompson   // Vectors
294fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
304fee36f0SJeremy L Thompson   {
314fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
32b8edc0e7SJeremy L Thompson 
334fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < n_x * (p - 1) + 1; i++) {
344fee36f0SJeremy L Thompson       for (CeedInt j = 0; j < n_y * (p - 1) + 1; j++) {
354fee36f0SJeremy L Thompson         x_array[i + j * (n_x * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (n_x * (p - 1));
364fee36f0SJeremy L Thompson         x_array[i + j * (n_x * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (n_y * (p - 1));
374fee36f0SJeremy L Thompson       }
384fee36f0SJeremy L Thompson     }
394fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
404fee36f0SJeremy L Thompson   }
414fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &u);
424fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &v);
43b8edc0e7SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts, &q_data);
44b8edc0e7SJeremy L Thompson 
454fee36f0SJeremy L Thompson   // Restrictions
46b8edc0e7SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) {
47b8edc0e7SJeremy L Thompson     CeedInt col, row, offset;
484fee36f0SJeremy L Thompson 
49b8edc0e7SJeremy L Thompson     col    = i % n_x;
50b8edc0e7SJeremy L Thompson     row    = i / n_x;
514fee36f0SJeremy L Thompson     offset = col * (p - 1) + row * (n_x * (p - 1) + 1) * (p - 1);
524fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
534fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * p + j;
542b730f8bSJeremy L Thompson     }
55b8edc0e7SJeremy L Thompson   }
564fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
574fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x,
584fee36f0SJeremy L Thompson                             &elem_restriction_u);
59b8edc0e7SJeremy L Thompson 
604fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q * q * num_elem, q * q};
614fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
62b8edc0e7SJeremy L Thompson 
63b8edc0e7SJeremy L Thompson   // Bases
644fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x);
654fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, p, q, CEED_GAUSS, &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
792b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
802b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
814fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
82356036faSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
83b8edc0e7SJeremy L Thompson 
842b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
85356036faSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
864fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
874fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
88b8edc0e7SJeremy L Thompson 
89b8edc0e7SJeremy L Thompson   // Apply Setup Operator
904fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
91b8edc0e7SJeremy L Thompson 
92b8edc0e7SJeremy L Thompson   // Fuly assemble operator
934fee36f0SJeremy L Thompson   CeedSize   num_entries;
94b8edc0e7SJeremy L Thompson   CeedInt   *rows;
95b8edc0e7SJeremy L Thompson   CeedInt   *cols;
964fee36f0SJeremy L Thompson   CeedVector assembled;
974fee36f0SJeremy L Thompson 
984fee36f0SJeremy L Thompson   for (CeedInt k = 0; k < num_comp * num_comp * num_dofs * num_dofs; k++) {
994fee36f0SJeremy L Thompson     assembled_values[k] = 0.0;
1004fee36f0SJeremy L Thompson     assembled_true[k]   = 0.0;
1014fee36f0SJeremy L Thompson   }
1024fee36f0SJeremy L Thompson   CeedOperatorLinearAssembleSymbolic(op_mass, &num_entries, &rows, &cols);
1034fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_entries, &assembled);
1044fee36f0SJeremy L Thompson   CeedOperatorLinearAssemble(op_mass, assembled);
1054fee36f0SJeremy L Thompson   {
1064fee36f0SJeremy L Thompson     const CeedScalar *assembled_array;
1074fee36f0SJeremy L Thompson 
1084fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array);
1094fee36f0SJeremy L Thompson     for (CeedInt k = 0; k < num_entries; k++) assembled_values[rows[k] * num_comp * num_dofs + cols[k]] += assembled_array[k];
1104fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(assembled, &assembled_array);
1114fee36f0SJeremy L Thompson   }
112b8edc0e7SJeremy L Thompson 
113b8edc0e7SJeremy L Thompson   // Manually assemble operator
1144fee36f0SJeremy L Thompson   CeedInt old_index = -1;
115b8edc0e7SJeremy L Thompson 
1164fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
117b8edc0e7SJeremy L Thompson   for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
118b8edc0e7SJeremy L Thompson     for (CeedInt node_in = 0; node_in < num_dofs; node_in++) {
1194fee36f0SJeremy L Thompson       CeedScalar       *u_array;
1204fee36f0SJeremy L Thompson       const CeedScalar *v_array;
1214fee36f0SJeremy L Thompson 
122b8edc0e7SJeremy L Thompson       // Set input
1234fee36f0SJeremy L Thompson       CeedVectorGetArray(u, CEED_MEM_HOST, &u_array);
124b8edc0e7SJeremy L Thompson       CeedInt ind  = node_in + comp_in * num_dofs;
1254fee36f0SJeremy L Thompson       u_array[ind] = 1.0;
1264fee36f0SJeremy L Thompson       if (ind > 0) u_array[old_index] = 0.0;
1274fee36f0SJeremy L Thompson       old_index = ind;
1284fee36f0SJeremy L Thompson       CeedVectorRestoreArray(u, &u_array);
129b8edc0e7SJeremy L Thompson 
130*506b1a0cSSebastian Grimberg       // Compute effect of DoF ind
1314fee36f0SJeremy L Thompson       CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
132b8edc0e7SJeremy L Thompson 
1334fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
134*506b1a0cSSebastian Grimberg       for (CeedInt i = 0; i < num_dofs * num_comp; i++) assembled_true[i * num_dofs * num_comp + ind] = v_array[i];
1354fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(v, &v_array);
136b8edc0e7SJeremy L Thompson     }
137b8edc0e7SJeremy L Thompson   }
138b8edc0e7SJeremy L Thompson 
139b8edc0e7SJeremy L Thompson   // Check output
140b8edc0e7SJeremy L Thompson   for (CeedInt node_in = 0; node_in < num_dofs; node_in++) {
141b8edc0e7SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
142b8edc0e7SJeremy L Thompson       for (CeedInt node_out = 0; node_out < num_dofs; node_out++) {
143b8edc0e7SJeremy L Thompson         for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
1442b730f8bSJeremy L Thompson           const CeedInt    index                = (node_out + comp_out * num_dofs) * num_comp + node_in + comp_in * num_dofs;
1454fee36f0SJeremy L Thompson           const CeedScalar assembled_value      = assembled_values[index];
146b8edc0e7SJeremy L Thompson           const CeedScalar assembled_true_value = assembled_true[index];
1472b730f8bSJeremy L Thompson           if (fabs(assembled_value - assembled_true_value) > 100. * CEED_EPSILON) {
148b8edc0e7SJeremy L Thompson             // LCOV_EXCL_START
1492b730f8bSJeremy L Thompson             printf("[(%" CeedInt_FMT ", %" CeedInt_FMT "), (%" CeedInt_FMT ", %" CeedInt_FMT ")] Error in assembly: %f != %f\n", node_out, comp_out,
1502b730f8bSJeremy L Thompson                    node_in, comp_in, assembled_value, assembled_true_value);
151b8edc0e7SJeremy L Thompson             // LCOV_EXCL_STOP
152b8edc0e7SJeremy L Thompson           }
153b8edc0e7SJeremy L Thompson         }
154b8edc0e7SJeremy L Thompson       }
155b8edc0e7SJeremy L Thompson     }
1562b730f8bSJeremy L Thompson   }
157b8edc0e7SJeremy L Thompson 
158b8edc0e7SJeremy L Thompson   // Cleanup
159b8edc0e7SJeremy L Thompson   free(rows);
160b8edc0e7SJeremy L Thompson   free(cols);
1614fee36f0SJeremy L Thompson   CeedVectorDestroy(&assembled);
1624fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
1634fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
1644fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
1654fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
1664fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
1674fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
1684fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
1694fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
1704fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
171b8edc0e7SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
172b8edc0e7SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
173b8edc0e7SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
174b8edc0e7SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
175b8edc0e7SJeremy L Thompson   CeedDestroy(&ceed);
176b8edc0e7SJeremy L Thompson   return 0;
177b8edc0e7SJeremy L Thompson }
178