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 62b730f8bSJeremy L Thompson #include <ceed.h> 72b730f8bSJeremy L Thompson #include <math.h> 82b730f8bSJeremy L Thompson #include <stdlib.h> 92b730f8bSJeremy L Thompson 10b8edc0e7SJeremy L Thompson int main(int argc, char **argv) { 11b8edc0e7SJeremy L Thompson Ceed ceed; 12*4fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data; 13b8edc0e7SJeremy L Thompson CeedBasis basis_x, basis_u; 14b8edc0e7SJeremy L Thompson CeedQFunction qf_setup, qf_mass; 15b8edc0e7SJeremy L Thompson CeedOperator op_setup, op_mass; 16*4fee36f0SJeremy L Thompson CeedVector q_data, x, u, v; 17*4fee36f0SJeremy 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*4fee36f0SJeremy L Thompson CeedInt num_dofs = (n_x * (p - 1) + 1) * (n_y * (p - 1) + 1), num_qpts = num_elem * q * q; 21*4fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p * p]; 22*4fee36f0SJeremy L Thompson CeedScalar assembled_values[num_comp * num_comp * num_dofs * num_dofs]; 23*4fee36f0SJeremy L Thompson CeedScalar assembled_true[num_comp * num_comp * num_dofs * num_dofs]; 24b8edc0e7SJeremy L Thompson 25b8edc0e7SJeremy L Thompson CeedInit(argv[1], &ceed); 26b8edc0e7SJeremy L Thompson 27*4fee36f0SJeremy L Thompson // Vectors 28*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 29*4fee36f0SJeremy L Thompson { 30*4fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 31b8edc0e7SJeremy L Thompson 32*4fee36f0SJeremy L Thompson for (CeedInt i = 0; i < n_x * (p - 1) + 1; i++) { 33*4fee36f0SJeremy L Thompson for (CeedInt j = 0; j < n_y * (p - 1) + 1; j++) { 34*4fee36f0SJeremy L Thompson x_array[i + j * (n_x * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (n_x * (p - 1)); 35*4fee36f0SJeremy L Thompson x_array[i + j * (n_x * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (n_y * (p - 1)); 36*4fee36f0SJeremy L Thompson } 37*4fee36f0SJeremy L Thompson } 38*4fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 39*4fee36f0SJeremy L Thompson } 40*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &u); 41*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &v); 42b8edc0e7SJeremy L Thompson CeedVectorCreate(ceed, num_qpts, &q_data); 43b8edc0e7SJeremy L Thompson 44*4fee36f0SJeremy L Thompson // Restrictions 45b8edc0e7SJeremy L Thompson for (CeedInt i = 0; i < num_elem; i++) { 46b8edc0e7SJeremy L Thompson CeedInt col, row, offset; 47*4fee36f0SJeremy L Thompson 48b8edc0e7SJeremy L Thompson col = i % n_x; 49b8edc0e7SJeremy L Thompson row = i / n_x; 50*4fee36f0SJeremy L Thompson offset = col * (p - 1) + row * (n_x * (p - 1) + 1) * (p - 1); 51*4fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 52*4fee36f0SJeremy L Thompson for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * p + j; 532b730f8bSJeremy L Thompson } 54b8edc0e7SJeremy L Thompson } 55*4fee36f0SJeremy 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); 56*4fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p * p, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, 57*4fee36f0SJeremy L Thompson &elem_restriction_u); 58b8edc0e7SJeremy L Thompson 59*4fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q * q * num_elem, q * q}; 60*4fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 61b8edc0e7SJeremy L Thompson 62b8edc0e7SJeremy L Thompson // Bases 63*4fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x); 64*4fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, p, q, CEED_GAUSS, &basis_u); 65b8edc0e7SJeremy L Thompson 66b8edc0e7SJeremy L Thompson // QFunctions 67b8edc0e7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 68b8edc0e7SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 69b8edc0e7SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD); 70b8edc0e7SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 71b8edc0e7SJeremy L Thompson 72b8edc0e7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 73b8edc0e7SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 74b8edc0e7SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP); 75b8edc0e7SJeremy L Thompson CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP); 76b8edc0e7SJeremy L Thompson 77b8edc0e7SJeremy L Thompson // Operators 782b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 792b730f8bSJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 80*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 81*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 82b8edc0e7SJeremy L Thompson 832b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 84*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data); 85*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 86*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 87b8edc0e7SJeremy L Thompson 88b8edc0e7SJeremy L Thompson // Apply Setup Operator 89*4fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 90b8edc0e7SJeremy L Thompson 91b8edc0e7SJeremy L Thompson // Fuly assemble operator 92*4fee36f0SJeremy L Thompson CeedSize num_entries; 93b8edc0e7SJeremy L Thompson CeedInt *rows; 94b8edc0e7SJeremy L Thompson CeedInt *cols; 95*4fee36f0SJeremy L Thompson CeedVector assembled; 96*4fee36f0SJeremy L Thompson 97*4fee36f0SJeremy L Thompson for (CeedInt k = 0; k < num_comp * num_comp * num_dofs * num_dofs; k++) { 98*4fee36f0SJeremy L Thompson assembled_values[k] = 0.0; 99*4fee36f0SJeremy L Thompson assembled_true[k] = 0.0; 100*4fee36f0SJeremy L Thompson } 101*4fee36f0SJeremy L Thompson CeedOperatorLinearAssembleSymbolic(op_mass, &num_entries, &rows, &cols); 102*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_entries, &assembled); 103*4fee36f0SJeremy L Thompson CeedOperatorLinearAssemble(op_mass, assembled); 104*4fee36f0SJeremy L Thompson { 105*4fee36f0SJeremy L Thompson const CeedScalar *assembled_array; 106*4fee36f0SJeremy L Thompson 107*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array); 108*4fee36f0SJeremy L Thompson for (CeedInt k = 0; k < num_entries; k++) assembled_values[rows[k] * num_comp * num_dofs + cols[k]] += assembled_array[k]; 109*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(assembled, &assembled_array); 110*4fee36f0SJeremy L Thompson } 111b8edc0e7SJeremy L Thompson 112b8edc0e7SJeremy L Thompson // Manually assemble operator 113*4fee36f0SJeremy L Thompson CeedInt old_index = -1; 114b8edc0e7SJeremy L Thompson 115*4fee36f0SJeremy L Thompson CeedVectorSetValue(u, 0.0); 116b8edc0e7SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 117b8edc0e7SJeremy L Thompson for (CeedInt node_in = 0; node_in < num_dofs; node_in++) { 118*4fee36f0SJeremy L Thompson CeedScalar *u_array; 119*4fee36f0SJeremy L Thompson const CeedScalar *v_array; 120*4fee36f0SJeremy L Thompson 121b8edc0e7SJeremy L Thompson // Set input 122*4fee36f0SJeremy L Thompson CeedVectorGetArray(u, CEED_MEM_HOST, &u_array); 123b8edc0e7SJeremy L Thompson CeedInt ind = node_in + comp_in * num_dofs; 124*4fee36f0SJeremy L Thompson u_array[ind] = 1.0; 125*4fee36f0SJeremy L Thompson if (ind > 0) u_array[old_index] = 0.0; 126*4fee36f0SJeremy L Thompson old_index = ind; 127*4fee36f0SJeremy L Thompson CeedVectorRestoreArray(u, &u_array); 128b8edc0e7SJeremy L Thompson 129b8edc0e7SJeremy L Thompson // Compute effect of DoF j 130*4fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 131b8edc0e7SJeremy L Thompson 132*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 133*4fee36f0SJeremy L Thompson for (CeedInt k = 0; k < num_dofs * num_comp; k++) assembled_true[k * num_dofs * num_comp + ind] = v_array[k]; 134*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 135b8edc0e7SJeremy L Thompson } 136b8edc0e7SJeremy L Thompson } 137b8edc0e7SJeremy L Thompson 138b8edc0e7SJeremy L Thompson // Check output 139b8edc0e7SJeremy L Thompson for (CeedInt node_in = 0; node_in < num_dofs; node_in++) { 140b8edc0e7SJeremy L Thompson for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) { 141b8edc0e7SJeremy L Thompson for (CeedInt node_out = 0; node_out < num_dofs; node_out++) { 142b8edc0e7SJeremy L Thompson for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) { 1432b730f8bSJeremy L Thompson const CeedInt index = (node_out + comp_out * num_dofs) * num_comp + node_in + comp_in * num_dofs; 144*4fee36f0SJeremy L Thompson const CeedScalar assembled_value = assembled_values[index]; 145b8edc0e7SJeremy L Thompson const CeedScalar assembled_true_value = assembled_true[index]; 1462b730f8bSJeremy L Thompson if (fabs(assembled_value - assembled_true_value) > 100. * CEED_EPSILON) { 147b8edc0e7SJeremy L Thompson // LCOV_EXCL_START 1482b730f8bSJeremy L Thompson printf("[(%" CeedInt_FMT ", %" CeedInt_FMT "), (%" CeedInt_FMT ", %" CeedInt_FMT ")] Error in assembly: %f != %f\n", node_out, comp_out, 1492b730f8bSJeremy L Thompson node_in, comp_in, assembled_value, assembled_true_value); 150b8edc0e7SJeremy L Thompson // LCOV_EXCL_STOP 151b8edc0e7SJeremy L Thompson } 152b8edc0e7SJeremy L Thompson } 153b8edc0e7SJeremy L Thompson } 154b8edc0e7SJeremy L Thompson } 1552b730f8bSJeremy L Thompson } 156b8edc0e7SJeremy L Thompson 157b8edc0e7SJeremy L Thompson // Cleanup 158b8edc0e7SJeremy L Thompson free(rows); 159b8edc0e7SJeremy L Thompson free(cols); 160*4fee36f0SJeremy L Thompson CeedVectorDestroy(&assembled); 161*4fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 162*4fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 163*4fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 164*4fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 165*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 166*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 167*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 168*4fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 169*4fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 170b8edc0e7SJeremy L Thompson CeedQFunctionDestroy(&qf_setup); 171b8edc0e7SJeremy L Thompson CeedQFunctionDestroy(&qf_mass); 172b8edc0e7SJeremy L Thompson CeedOperatorDestroy(&op_setup); 173b8edc0e7SJeremy L Thompson CeedOperatorDestroy(&op_mass); 174b8edc0e7SJeremy L Thompson CeedDestroy(&ceed); 175b8edc0e7SJeremy L Thompson return 0; 176b8edc0e7SJeremy L Thompson } 177