11d102b48SJeremy L Thompson /// @file 21d102b48SJeremy L Thompson /// Test assembly of mass matrix operator QFunction 31d102b48SJeremy L Thompson /// \test Test assembly of mass matrix operator QFunction 41d102b48SJeremy L Thompson #include <ceed.h> 51d102b48SJeremy L Thompson #include <math.h> 6*49aac155SJeremy L Thompson #include <stdio.h> 72b730f8bSJeremy L Thompson #include <stdlib.h> 82b730f8bSJeremy L Thompson 9a05f9790Sjeremylt #include "t510-operator.h" 101d102b48SJeremy L Thompson 111d102b48SJeremy L Thompson int main(int argc, char **argv) { 121d102b48SJeremy L Thompson Ceed ceed; 134fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data, elem_restriction_assembled; 14d1d35e2fSjeremylt CeedBasis basis_x, basis_u; 151d102b48SJeremy L Thompson CeedQFunction qf_setup, qf_mass; 161d102b48SJeremy L Thompson CeedOperator op_setup, op_mass; 174fee36f0SJeremy L Thompson CeedVector q_data, x, qf_assembled, u, v; 184fee36f0SJeremy L Thompson CeedInt num_elem = 6, p = 3, q = 4, dim = 2; 191d102b48SJeremy L Thompson CeedInt nx = 3, ny = 2; 204fee36f0SJeremy L Thompson CeedInt num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q; 214fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p * p]; 221d102b48SJeremy L Thompson 231d102b48SJeremy L Thompson CeedInit(argv[1], &ceed); 241d102b48SJeremy L Thompson 254fee36f0SJeremy L Thompson // Vectors 264fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 274fee36f0SJeremy L Thompson { 284fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 292b730f8bSJeremy L Thompson for (CeedInt i = 0; i < nx * 2 + 1; i++) { 301d102b48SJeremy L Thompson for (CeedInt j = 0; j < ny * 2 + 1; j++) { 314fee36f0SJeremy L Thompson x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx); 324fee36f0SJeremy L Thompson x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny); 331d102b48SJeremy L Thompson } 342b730f8bSJeremy L Thompson } 354fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 364fee36f0SJeremy L Thompson } 374fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &u); 384fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &v); 39d1d35e2fSjeremylt CeedVectorCreate(ceed, num_qpts, &q_data); 401d102b48SJeremy L Thompson 414fee36f0SJeremy L Thompson // Restrictions 42d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 431d102b48SJeremy L Thompson CeedInt col, row, offset; 441d102b48SJeremy L Thompson col = i % nx; 451d102b48SJeremy L Thompson row = i / nx; 464fee36f0SJeremy L Thompson offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1); 474fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 484fee36f0SJeremy L Thompson for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j; 492b730f8bSJeremy L Thompson } 501d102b48SJeremy 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, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u); 531d102b48SJeremy L Thompson 544fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q * q, q * q}; 554fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 561d102b48SJeremy L Thompson 571d102b48SJeremy L Thompson // Bases 584fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x); 594fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u); 601d102b48SJeremy L Thompson 611d102b48SJeremy L Thompson // QFunctions 621d102b48SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 63a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 641d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD); 651d102b48SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 661d102b48SJeremy L Thompson 671d102b48SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 681d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 691d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 701d102b48SJeremy L Thompson CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 711d102b48SJeremy L Thompson 721d102b48SJeremy L Thompson // Operators 732b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 742b730f8bSJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 754fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 764fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 771d102b48SJeremy L Thompson 782b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 794fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data); 804fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 814fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 821d102b48SJeremy L Thompson 831d102b48SJeremy L Thompson // Apply Setup Operator 844fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 851d102b48SJeremy L Thompson 861d102b48SJeremy L Thompson // Assemble QFunction 87beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyReuse(op_mass, true); 88beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_mass, true); 894fee36f0SJeremy L Thompson CeedOperatorLinearAssembleQFunction(op_mass, &qf_assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE); 908b919e6bSJeremy L Thompson // Second call will be no-op since SetQFunctionUpdated was not called 91beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_mass, false); 924fee36f0SJeremy L Thompson CeedOperatorLinearAssembleQFunction(op_mass, &qf_assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE); 931d102b48SJeremy L Thompson 941d102b48SJeremy L Thompson // Check output 954fee36f0SJeremy L Thompson { 964fee36f0SJeremy L Thompson const CeedScalar *assembled_array, *q_data_array; 974fee36f0SJeremy L Thompson 984fee36f0SJeremy L Thompson CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array); 994fee36f0SJeremy L Thompson CeedVectorGetArrayRead(q_data, CEED_MEM_HOST, &q_data_array); 100d1d35e2fSjeremylt for (CeedInt i = 0; i < num_qpts; i++) 1014fee36f0SJeremy L Thompson if (fabs(q_data_array[i] - assembled_array[i]) > 1e-9) { 1024fee36f0SJeremy L Thompson // LCOV_EXCL_START 1034fee36f0SJeremy L Thompson printf("Error: qf_assembled[%" CeedInt_FMT "] = %f != %f\n", i, assembled_array[i], q_data_array[i]); 1044fee36f0SJeremy L Thompson // LCOV_EXCL_STOP 1054fee36f0SJeremy L Thompson } 1064fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(qf_assembled, &assembled_array); 1074fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(q_data, &q_data_array); 1084fee36f0SJeremy L Thompson } 1091d102b48SJeremy L Thompson 1101d102b48SJeremy L Thompson // Apply original Mass Operator 1111d102b48SJeremy L Thompson CeedVectorSetValue(u, 1.0); 1121d102b48SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 1131d102b48SJeremy L Thompson 1141d102b48SJeremy L Thompson // Check output 1154fee36f0SJeremy L Thompson { 1164fee36f0SJeremy L Thompson const CeedScalar *v_array; 1171d102b48SJeremy L Thompson CeedScalar area = 0.0; 1184fee36f0SJeremy L Thompson 1194fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1204fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i]; 1212b730f8bSJeremy L Thompson if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: True operator computed area = %f != 1.0\n", area); 1224fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 1234fee36f0SJeremy L Thompson } 1241d102b48SJeremy L Thompson 125d1d35e2fSjeremylt // Switch to new q_data 1264fee36f0SJeremy L Thompson { 1274fee36f0SJeremy L Thompson const CeedScalar *assembled_array; 1284fee36f0SJeremy L Thompson 1294fee36f0SJeremy L Thompson CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array); 1304fee36f0SJeremy L Thompson CeedVectorSetArray(q_data, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)assembled_array); 1314fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(qf_assembled, &assembled_array); 1324fee36f0SJeremy L Thompson } 1331d102b48SJeremy L Thompson 1341d102b48SJeremy L Thompson // Apply new Mass Operator 1351d102b48SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 1361d102b48SJeremy L Thompson 1371d102b48SJeremy L Thompson // Check output 1384fee36f0SJeremy L Thompson { 1394fee36f0SJeremy L Thompson const CeedScalar *v_array; 1404fee36f0SJeremy L Thompson CeedScalar area = 0.0; 1414fee36f0SJeremy L Thompson 1424fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1434fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i]; 1444fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 1452b730f8bSJeremy L Thompson if (fabs(area - 1.0) > 1000. * CEED_EPSILON) printf("Error: Linearized operator computed area = %f != 1.0\n", area); 1464fee36f0SJeremy L Thompson } 1471d102b48SJeremy L Thompson 1481d102b48SJeremy L Thompson // Cleanup 1494fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1504fee36f0SJeremy L Thompson CeedVectorDestroy(&qf_assembled); 1514fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 1524fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1534fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 1544fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 1554fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 1564fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 1574fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_assembled); 1584fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 1594fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 1601d102b48SJeremy L Thompson CeedQFunctionDestroy(&qf_setup); 1611d102b48SJeremy L Thompson CeedQFunctionDestroy(&qf_mass); 1621d102b48SJeremy L Thompson CeedOperatorDestroy(&op_setup); 1631d102b48SJeremy L Thompson CeedOperatorDestroy(&op_mass); 1641d102b48SJeremy L Thompson CeedDestroy(&ceed); 1651d102b48SJeremy L Thompson return 0; 1661d102b48SJeremy L Thompson } 167