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]; 224fee36f0SJeremy L Thompson CeedScalar assembled_true[num_comp * num_comp * num_dofs]; 23d965c7a7SJeremy L Thompson 24d965c7a7SJeremy L Thompson CeedInit(argv[1], &ceed); 25d965c7a7SJeremy L Thompson 264fee36f0SJeremy L Thompson // Vectors 274fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 284fee36f0SJeremy L Thompson { 294fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 304fee36f0SJeremy L Thompson 3108fdf5f2SJeremy L Thompson for (CeedInt i = 0; i < n_x * (p - 1) + 1; i++) { 3208fdf5f2SJeremy L Thompson for (CeedInt j = 0; j < n_y * (p - 1) + 1; j++) { 3308fdf5f2SJeremy L Thompson x_array[i + j * (n_x * (p - 1) + 1) + 0 * num_dofs] = (CeedScalar)i / ((p - 1) * n_x); 3408fdf5f2SJeremy L Thompson x_array[i + j * (n_x * (p - 1) + 1) + 1 * num_dofs] = (CeedScalar)j / ((p - 1) * n_y); 35d965c7a7SJeremy L Thompson } 362b730f8bSJeremy L Thompson } 374fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 384fee36f0SJeremy L Thompson } 39d1d35e2fSjeremylt CeedVectorCreate(ceed, num_qpts, &q_data); 40d965c7a7SJeremy L Thompson 414fee36f0SJeremy L Thompson // Restrictions 42d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 43d965c7a7SJeremy L Thompson CeedInt col, row, offset; 4408fdf5f2SJeremy L Thompson col = i % n_x; 4508fdf5f2SJeremy L Thompson row = i / n_x; 4608fdf5f2SJeremy L Thompson offset = col * (p - 1) + row * (n_x * 2 + 1) * (p - 1); 474fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 4808fdf5f2SJeremy L Thompson for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (n_x * 2 + 1) + j; 492b730f8bSJeremy L Thompson } 50d965c7a7SJeremy L Thompson } 514fee36f0SJeremy 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); 524fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p * p, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, 534fee36f0SJeremy L Thompson &elem_restriction_u); 54d965c7a7SJeremy L Thompson 554fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q * q, q * q}; 564fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 57d965c7a7SJeremy L Thompson 58d965c7a7SJeremy L Thompson // Bases 594fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x); 604fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, p, q, CEED_GAUSS, &basis_u); 61d965c7a7SJeremy L Thompson 62d965c7a7SJeremy L Thompson // QFunctions 63d965c7a7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 64a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 65d965c7a7SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD); 66d965c7a7SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 67d965c7a7SJeremy L Thompson 68d965c7a7SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 69d965c7a7SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 70d1d35e2fSjeremylt CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP); 71d1d35e2fSjeremylt CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP); 72d965c7a7SJeremy L Thompson 73d965c7a7SJeremy L Thompson // Operators 742b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 752b730f8bSJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 764fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 77*356036faSJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE); 78d965c7a7SJeremy L Thompson 792b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 80*356036faSJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data); 814fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 824fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 83d965c7a7SJeremy L Thompson 84d965c7a7SJeremy L Thompson // Apply Setup Operator 854fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 86d965c7a7SJeremy L Thompson 87d965c7a7SJeremy L Thompson // Assemble diagonal 884fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_comp * num_dofs, &assembled); 894fee36f0SJeremy L Thompson CeedOperatorLinearAssemblePointBlockDiagonal(op_mass, assembled, CEED_REQUEST_IMMEDIATE); 90d965c7a7SJeremy L Thompson 91d965c7a7SJeremy L Thompson // Manually assemble diagonal 924fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &u); 934fee36f0SJeremy L Thompson CeedVectorSetValue(u, 0.0); 944fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs, &v); 952b730f8bSJeremy L Thompson for (int i = 0; i < num_comp * num_comp * num_dofs; i++) assembled_true[i] = 0.0; 96d1d35e2fSjeremylt CeedInt ind_old = -1; 97d1d35e2fSjeremylt for (int i = 0; i < num_dofs; i++) { 98d1d35e2fSjeremylt for (int j = 0; j < num_comp; j++) { 994fee36f0SJeremy L Thompson CeedScalar *u_array; 1004fee36f0SJeremy L Thompson const CeedScalar *v_array; 1014fee36f0SJeremy L Thompson 102d965c7a7SJeremy L Thompson // Set input 1034fee36f0SJeremy L Thompson CeedVectorGetArray(u, CEED_MEM_HOST, &u_array); 104d1d35e2fSjeremylt CeedInt ind = i + j * num_dofs; 1054fee36f0SJeremy L Thompson u_array[ind] = 1.0; 1064fee36f0SJeremy L Thompson if (ind > 0) u_array[ind_old] = 0.0; 107d1d35e2fSjeremylt ind_old = ind; 1084fee36f0SJeremy L Thompson CeedVectorRestoreArray(u, &u_array); 109d965c7a7SJeremy L Thompson 110d965c7a7SJeremy L Thompson // Compute effect of DoF i, comp j 1114fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 112d965c7a7SJeremy L Thompson 113d965c7a7SJeremy L Thompson // Retrieve entry 1144fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1154fee36f0SJeremy 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]; 1164fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 117d965c7a7SJeremy L Thompson } 118d965c7a7SJeremy L Thompson } 119d965c7a7SJeremy L Thompson 120d965c7a7SJeremy L Thompson // Check output 1214fee36f0SJeremy L Thompson { 1224fee36f0SJeremy L Thompson const CeedScalar *assembled_array; 1234fee36f0SJeremy L Thompson 1244fee36f0SJeremy L Thompson CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array); 1252b730f8bSJeremy L Thompson for (int i = 0; i < num_comp * num_comp * num_dofs; i++) { 1264fee36f0SJeremy L Thompson if (fabs(assembled_array[i] - assembled_true[i]) > 100. * CEED_EPSILON) { 1274fee36f0SJeremy L Thompson // LCOV_EXCL_START 1284fee36f0SJeremy L Thompson printf("[%" CeedInt_FMT "] Error in assembly: %f != %f\n", i, assembled_array[i], assembled_true[i]); 1294fee36f0SJeremy L Thompson // LCOV_EXCL_STOP 1302b730f8bSJeremy L Thompson } 1314fee36f0SJeremy L Thompson } 1324fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(assembled, &assembled_array); 1334fee36f0SJeremy L Thompson } 134d965c7a7SJeremy L Thompson 135d965c7a7SJeremy L Thompson // Cleanup 1364fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1374fee36f0SJeremy L Thompson CeedVectorDestroy(&assembled); 1384fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 1394fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1404fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 1414fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 1424fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 1434fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 1444fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 1454fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 146d965c7a7SJeremy L Thompson CeedQFunctionDestroy(&qf_setup); 147d965c7a7SJeremy L Thompson CeedQFunctionDestroy(&qf_mass); 148d965c7a7SJeremy L Thompson CeedOperatorDestroy(&op_setup); 149d965c7a7SJeremy L Thompson CeedOperatorDestroy(&op_mass); 150d965c7a7SJeremy L Thompson CeedDestroy(&ceed); 151d965c7a7SJeremy L Thompson return 0; 152d965c7a7SJeremy L Thompson } 153