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> 62b730f8bSJeremy L Thompson #include <stdlib.h> 72b730f8bSJeremy L Thompson 8a05f9790Sjeremylt #include "t510-operator.h" 91d102b48SJeremy L Thompson 101d102b48SJeremy L Thompson int main(int argc, char **argv) { 111d102b48SJeremy L Thompson Ceed ceed; 12*4fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data, elem_restriction_assembled; 13d1d35e2fSjeremylt CeedBasis basis_x, basis_u; 141d102b48SJeremy L Thompson CeedQFunction qf_setup, qf_mass; 151d102b48SJeremy L Thompson CeedOperator op_setup, op_mass; 16*4fee36f0SJeremy L Thompson CeedVector q_data, x, qf_assembled, u, v; 17*4fee36f0SJeremy L Thompson CeedInt num_elem = 6, p = 3, q = 4, dim = 2; 181d102b48SJeremy L Thompson CeedInt nx = 3, ny = 2; 19*4fee36f0SJeremy L Thompson CeedInt num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q; 20*4fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p * p]; 211d102b48SJeremy L Thompson 221d102b48SJeremy L Thompson CeedInit(argv[1], &ceed); 231d102b48SJeremy L Thompson 24*4fee36f0SJeremy L Thompson // Vectors 25*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 26*4fee36f0SJeremy L Thompson { 27*4fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 282b730f8bSJeremy L Thompson for (CeedInt i = 0; i < nx * 2 + 1; i++) { 291d102b48SJeremy L Thompson for (CeedInt j = 0; j < ny * 2 + 1; j++) { 30*4fee36f0SJeremy L Thompson x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx); 31*4fee36f0SJeremy L Thompson x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny); 321d102b48SJeremy L Thompson } 332b730f8bSJeremy L Thompson } 34*4fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 35*4fee36f0SJeremy L Thompson } 36*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &u); 37*4fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &v); 38d1d35e2fSjeremylt CeedVectorCreate(ceed, num_qpts, &q_data); 391d102b48SJeremy L Thompson 40*4fee36f0SJeremy L Thompson // Restrictions 41d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) { 421d102b48SJeremy L Thompson CeedInt col, row, offset; 431d102b48SJeremy L Thompson col = i % nx; 441d102b48SJeremy L Thompson row = i / nx; 45*4fee36f0SJeremy L Thompson offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1); 46*4fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) { 47*4fee36f0SJeremy L Thompson for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j; 482b730f8bSJeremy L Thompson } 491d102b48SJeremy L Thompson } 50*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); 51*4fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u); 521d102b48SJeremy L Thompson 53*4fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q * q, q * q}; 54*4fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 551d102b48SJeremy L Thompson 561d102b48SJeremy L Thompson // Bases 57*4fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x); 58*4fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u); 591d102b48SJeremy L Thompson 601d102b48SJeremy L Thompson // QFunctions 611d102b48SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 62a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 631d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD); 641d102b48SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 651d102b48SJeremy L Thompson 661d102b48SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 671d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 681d102b48SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 691d102b48SJeremy L Thompson CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 701d102b48SJeremy L Thompson 711d102b48SJeremy L Thompson // Operators 722b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 732b730f8bSJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 74*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 75*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 761d102b48SJeremy L Thompson 772b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 78*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data); 79*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 80*4fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 811d102b48SJeremy L Thompson 821d102b48SJeremy L Thompson // Apply Setup Operator 83*4fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 841d102b48SJeremy L Thompson 851d102b48SJeremy L Thompson // Assemble QFunction 86beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyReuse(op_mass, true); 87beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_mass, true); 88*4fee36f0SJeremy L Thompson CeedOperatorLinearAssembleQFunction(op_mass, &qf_assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE); 898b919e6bSJeremy L Thompson // Second call will be no-op since SetQFunctionUpdated was not called 90beecbf24SJeremy L Thompson CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_mass, false); 91*4fee36f0SJeremy L Thompson CeedOperatorLinearAssembleQFunction(op_mass, &qf_assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE); 921d102b48SJeremy L Thompson 931d102b48SJeremy L Thompson // Check output 94*4fee36f0SJeremy L Thompson { 95*4fee36f0SJeremy L Thompson const CeedScalar *assembled_array, *q_data_array; 96*4fee36f0SJeremy L Thompson 97*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array); 98*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(q_data, CEED_MEM_HOST, &q_data_array); 99d1d35e2fSjeremylt for (CeedInt i = 0; i < num_qpts; i++) 100*4fee36f0SJeremy L Thompson if (fabs(q_data_array[i] - assembled_array[i]) > 1e-9) { 101*4fee36f0SJeremy L Thompson // LCOV_EXCL_START 102*4fee36f0SJeremy L Thompson printf("Error: qf_assembled[%" CeedInt_FMT "] = %f != %f\n", i, assembled_array[i], q_data_array[i]); 103*4fee36f0SJeremy L Thompson // LCOV_EXCL_STOP 104*4fee36f0SJeremy L Thompson } 105*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(qf_assembled, &assembled_array); 106*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(q_data, &q_data_array); 107*4fee36f0SJeremy L Thompson } 1081d102b48SJeremy L Thompson 1091d102b48SJeremy L Thompson // Apply original Mass Operator 1101d102b48SJeremy L Thompson CeedVectorSetValue(u, 1.0); 1111d102b48SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 1121d102b48SJeremy L Thompson 1131d102b48SJeremy L Thompson // Check output 114*4fee36f0SJeremy L Thompson { 115*4fee36f0SJeremy L Thompson const CeedScalar *v_array; 1161d102b48SJeremy L Thompson CeedScalar area = 0.0; 117*4fee36f0SJeremy L Thompson 118*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 119*4fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i]; 1202b730f8bSJeremy L Thompson if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: True operator computed area = %f != 1.0\n", area); 121*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 122*4fee36f0SJeremy L Thompson } 1231d102b48SJeremy L Thompson 124d1d35e2fSjeremylt // Switch to new q_data 125*4fee36f0SJeremy L Thompson { 126*4fee36f0SJeremy L Thompson const CeedScalar *assembled_array; 127*4fee36f0SJeremy L Thompson 128*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array); 129*4fee36f0SJeremy L Thompson CeedVectorSetArray(q_data, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)assembled_array); 130*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(qf_assembled, &assembled_array); 131*4fee36f0SJeremy L Thompson } 1321d102b48SJeremy L Thompson 1331d102b48SJeremy L Thompson // Apply new Mass Operator 1341d102b48SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 1351d102b48SJeremy L Thompson 1361d102b48SJeremy L Thompson // Check output 137*4fee36f0SJeremy L Thompson { 138*4fee36f0SJeremy L Thompson const CeedScalar *v_array; 139*4fee36f0SJeremy L Thompson CeedScalar area = 0.0; 140*4fee36f0SJeremy L Thompson 141*4fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 142*4fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i]; 143*4fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 1442b730f8bSJeremy L Thompson if (fabs(area - 1.0) > 1000. * CEED_EPSILON) printf("Error: Linearized operator computed area = %f != 1.0\n", area); 145*4fee36f0SJeremy L Thompson } 1461d102b48SJeremy L Thompson 1471d102b48SJeremy L Thompson // Cleanup 148*4fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 149*4fee36f0SJeremy L Thompson CeedVectorDestroy(&qf_assembled); 150*4fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 151*4fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 152*4fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 153*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 154*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 155*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 156*4fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_assembled); 157*4fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 158*4fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 1591d102b48SJeremy L Thompson CeedQFunctionDestroy(&qf_setup); 1601d102b48SJeremy L Thompson CeedQFunctionDestroy(&qf_mass); 1611d102b48SJeremy L Thompson CeedOperatorDestroy(&op_setup); 1621d102b48SJeremy L Thompson CeedOperatorDestroy(&op_mass); 1631d102b48SJeremy L Thompson CeedDestroy(&ceed); 1641d102b48SJeremy L Thompson return 0; 1651d102b48SJeremy L Thompson } 166