1d965c7a7SJeremy L Thompson /// @file 2d965c7a7SJeremy L Thompson /// Test assembly of mass matrix operator point block diagonal 3d965c7a7SJeremy L Thompson /// \test Test assembly of mass matrix operator point block diagonal 4d965c7a7SJeremy L Thompson #include "t537-operator.h" 5d965c7a7SJeremy L Thompson 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 11d965c7a7SJeremy L Thompson int main(int argc, char **argv) { 12d965c7a7SJeremy L Thompson Ceed ceed; 134fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data; 14d1d35e2fSjeremylt CeedBasis basis_x, basis_u; 15d965c7a7SJeremy L Thompson CeedQFunction qf_setup, qf_mass; 16d965c7a7SJeremy L Thompson CeedOperator op_setup, op_mass; 174fee36f0SJeremy L Thompson CeedVector q_data, x, assembled, u, v; 184fee36f0SJeremy L Thompson CeedInt num_elem = 6, p = 3, q = 4, dim = 2, num_comp = 2; 1908fdf5f2SJeremy L Thompson CeedInt n_x = 3, n_y = 2; 2008fdf5f2SJeremy L Thompson CeedInt num_dofs = (n_x * 2 + 1) * (n_y * 2 + 1), num_qpts = num_elem * q * q; 214fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p * p]; 22*01f0e615SJames Wright CeedInt *rows, *cols; 23*01f0e615SJames Wright CeedSize num_entries; 244fee36f0SJeremy L Thompson CeedScalar assembled_true[num_comp * num_comp * num_dofs]; 25*01f0e615SJames Wright CeedScalar assembled_full_true[num_comp * num_dofs][num_comp * num_dofs]; 26d965c7a7SJeremy L Thompson 27d965c7a7SJeremy L Thompson CeedInit(argv[1], &ceed); 28d965c7a7SJeremy L Thompson 294fee36f0SJeremy L Thompson // Vectors 304fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 314fee36f0SJeremy L Thompson { 324fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 334fee36f0SJeremy L Thompson 3408fdf5f2SJeremy L Thompson for (CeedInt i = 0; i < n_x * (p - 1) + 1; i++) { 3508fdf5f2SJeremy L Thompson for (CeedInt j = 0; j < n_y * (p - 1) + 1; j++) { 3608fdf5f2SJeremy L Thompson x_array[i + j * (n_x * (p - 1) + 1) + 0 * num_dofs] = (CeedScalar)i / ((p - 1) * n_x); 3708fdf5f2SJeremy L Thompson x_array[i + j * (n_x * (p - 1) + 1) + 1 * num_dofs] = (CeedScalar)j / ((p - 1) * n_y); 38d965c7a7SJeremy L Thompson } 392b730f8bSJeremy L Thompson } 404fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 414fee36f0SJeremy L Thompson } 42d1d35e2fSjeremylt CeedVectorCreate(ceed, num_qpts, &q_data); 43d965c7a7SJeremy L Thompson 444fee36f0SJeremy L Thompson // Restrictions 45d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 46d965c7a7SJeremy L Thompson CeedInt col, row, offset; 4708fdf5f2SJeremy L Thompson col = i % n_x; 4808fdf5f2SJeremy L Thompson row = i / n_x; 4908fdf5f2SJeremy L Thompson offset = col * (p - 1) + row * (n_x * 2 + 1) * (p - 1); 504fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 5108fdf5f2SJeremy L Thompson for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (n_x * 2 + 1) + j; 522b730f8bSJeremy L Thompson } 53d965c7a7SJeremy L Thompson } 544fee36f0SJeremy 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); 554fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p * p, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, 564fee36f0SJeremy L Thompson &elem_restriction_u); 57d965c7a7SJeremy L Thompson 584fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q * q, q * q}; 594fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 60d965c7a7SJeremy L Thompson 61d965c7a7SJeremy L Thompson // Bases 624fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x); 634fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, p, q, CEED_GAUSS, &basis_u); 64d965c7a7SJeremy L Thompson 65d965c7a7SJeremy L Thompson // QFunctions 66d965c7a7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 67a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 68d965c7a7SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD); 69d965c7a7SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 70d965c7a7SJeremy L Thompson 71d965c7a7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 72d965c7a7SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 73d1d35e2fSjeremylt CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP); 74d1d35e2fSjeremylt CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP); 75d965c7a7SJeremy L Thompson 76d965c7a7SJeremy L Thompson // Operators 772b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 782b730f8bSJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 794fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 80356036faSJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE); 81d965c7a7SJeremy L Thompson 822b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 83356036faSJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data); 844fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 854fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 86d965c7a7SJeremy L Thompson 87d965c7a7SJeremy L Thompson // Apply Setup Operator 884fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 89d965c7a7SJeremy L Thompson 90d965c7a7SJeremy L Thompson // Assemble diagonal 914fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_comp * num_dofs, &assembled); 924fee36f0SJeremy L Thompson CeedOperatorLinearAssemblePointBlockDiagonal(op_mass, assembled, CEED_REQUEST_IMMEDIATE); 93*01f0e615SJames Wright CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(op_mass, &num_entries, &rows, &cols); 94d965c7a7SJeremy L Thompson 95*01f0e615SJames Wright // Manually assemble point-block diagonal 964fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &u); 974fee36f0SJeremy L Thompson CeedVectorSetValue(u, 0.0); 984fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &v); 99*01f0e615SJames Wright for (CeedInt i = 0; i < num_comp * num_dofs; i++) { 100*01f0e615SJames Wright for (CeedInt j = 0; j < num_comp * num_dofs; j++) { 101*01f0e615SJames Wright assembled_full_true[i][j] = 0.; 102*01f0e615SJames Wright } 103*01f0e615SJames Wright } 1042b730f8bSJeremy L Thompson for (int i = 0; i < num_comp * num_comp * num_dofs; i++) assembled_true[i] = 0.0; 105d1d35e2fSjeremylt CeedInt ind_old = -1; 106d1d35e2fSjeremylt for (int i = 0; i < num_dofs; i++) { 107d1d35e2fSjeremylt for (int j = 0; j < num_comp; j++) { 1084fee36f0SJeremy L Thompson CeedScalar *u_array; 1094fee36f0SJeremy L Thompson const CeedScalar *v_array; 1104fee36f0SJeremy L Thompson 111d965c7a7SJeremy L Thompson // Set input 1124fee36f0SJeremy L Thompson CeedVectorGetArray(u, CEED_MEM_HOST, &u_array); 113d1d35e2fSjeremylt CeedInt ind = i + j * num_dofs; 1144fee36f0SJeremy L Thompson u_array[ind] = 1.0; 1154fee36f0SJeremy L Thompson if (ind > 0) u_array[ind_old] = 0.0; 116d1d35e2fSjeremylt ind_old = ind; 1174fee36f0SJeremy L Thompson CeedVectorRestoreArray(u, &u_array); 118d965c7a7SJeremy L Thompson 119d965c7a7SJeremy L Thompson // Compute effect of DoF i, comp j 1204fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 121d965c7a7SJeremy L Thompson 122d965c7a7SJeremy L Thompson // Retrieve entry 1234fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1244fee36f0SJeremy L Thompson for (int k = 0; k < num_comp; k++) assembled_true[i * num_comp * num_comp + k * num_comp + j] += v_array[i + k * num_dofs]; 125*01f0e615SJames Wright for (CeedInt k = 0; k < num_comp * num_dofs; k++) assembled_full_true[k][i + num_dofs * j] += v_array[k]; 126*01f0e615SJames Wright for (CeedInt k = 0; k < num_comp; k++) assembled_full_true[i * num_comp + k][i * num_comp + j] = v_array[i + k * num_dofs]; 1274fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 128d965c7a7SJeremy L Thompson } 129d965c7a7SJeremy L Thompson } 130d965c7a7SJeremy L Thompson 131d965c7a7SJeremy L Thompson // Check output 1324fee36f0SJeremy L Thompson { 1334fee36f0SJeremy L Thompson const CeedScalar *assembled_array; 1344fee36f0SJeremy L Thompson 1354fee36f0SJeremy L Thompson CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array); 1362b730f8bSJeremy L Thompson for (int i = 0; i < num_comp * num_comp * num_dofs; i++) { 1374fee36f0SJeremy L Thompson if (fabs(assembled_array[i] - assembled_true[i]) > 100. * CEED_EPSILON) { 1384fee36f0SJeremy L Thompson // LCOV_EXCL_START 1394fee36f0SJeremy L Thompson printf("[%" CeedInt_FMT "] Error in assembly: %f != %f\n", i, assembled_array[i], assembled_true[i]); 1404fee36f0SJeremy L Thompson // LCOV_EXCL_STOP 1412b730f8bSJeremy L Thompson } 1424fee36f0SJeremy L Thompson } 143*01f0e615SJames Wright 144*01f0e615SJames Wright for (CeedInt i = 0; i < num_entries; i++) { 145*01f0e615SJames Wright if (fabs(assembled_array[i] - assembled_full_true[rows[i]][cols[i]]) > 100. * CEED_EPSILON) { 146*01f0e615SJames Wright // LCOV_EXCL_START 147*01f0e615SJames Wright printf("[%" CeedInt_FMT "] Error in symbolic assembly: %f != %f for row,col indices (%" CeedInt_FMT ", %" CeedInt_FMT ")\n", i, 148*01f0e615SJames Wright assembled_array[i], assembled_full_true[rows[i]][cols[i]], rows[i], cols[i]); 149*01f0e615SJames Wright // LCOV_EXCL_STOP 150*01f0e615SJames Wright } 151*01f0e615SJames Wright } 1524fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(assembled, &assembled_array); 1534fee36f0SJeremy L Thompson } 154d965c7a7SJeremy L Thompson 155d965c7a7SJeremy L Thompson // Cleanup 1564fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1574fee36f0SJeremy L Thompson CeedVectorDestroy(&assembled); 1584fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 1594fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1604fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 1614fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 1624fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 1634fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 1644fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 1654fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 166d965c7a7SJeremy L Thompson CeedQFunctionDestroy(&qf_setup); 167d965c7a7SJeremy L Thompson CeedQFunctionDestroy(&qf_mass); 168d965c7a7SJeremy L Thompson CeedOperatorDestroy(&op_setup); 169d965c7a7SJeremy L Thompson CeedOperatorDestroy(&op_mass); 170d965c7a7SJeremy L Thompson CeedDestroy(&ceed); 171*01f0e615SJames Wright free(rows); 172*01f0e615SJames Wright free(cols); 173d965c7a7SJeremy L Thompson return 0; 174d965c7a7SJeremy L Thompson } 175