1a8de75f0Sjeremylt /// @file 231e5b035Sjeremylt /// Test creation, action, and destruction for mass matrix operator 331e5b035Sjeremylt /// \test Test creation, action, and destruction for mass matrix operator 4a8de75f0Sjeremylt #include <ceed.h> 5a8de75f0Sjeremylt #include <math.h> 649aac155SJeremy L Thompson #include <stdio.h> 72b730f8bSJeremy L Thompson #include <stdlib.h> 82b730f8bSJeremy L Thompson 952bfb9bbSJeremy L Thompson #include "t320-basis.h" 10a05f9790Sjeremylt #include "t510-operator.h" 11a8de75f0Sjeremylt 12a8de75f0Sjeremylt int main(int argc, char **argv) { 13a8de75f0Sjeremylt Ceed ceed; 144fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data; 15d1d35e2fSjeremylt CeedBasis basis_x, basis_u; 16a8de75f0Sjeremylt CeedQFunction qf_setup, qf_mass; 17a8de75f0Sjeremylt CeedOperator op_setup, op_mass; 184fee36f0SJeremy L Thompson CeedVector q_data, x, u, v; 194fee36f0SJeremy L Thompson CeedInt num_elem = 12, dim = 2, p = 6, q = 4; 20a8de75f0Sjeremylt CeedInt nx = 3, ny = 2; 21a8de75f0Sjeremylt CeedInt row, col, offset; 224fee36f0SJeremy L Thompson CeedInt num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q; 234fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p]; 244fee36f0SJeremy L Thompson CeedScalar q_ref[dim * q], q_weight[q]; 254fee36f0SJeremy L Thompson CeedScalar interp[p * q], grad[dim * p * q]; 26a8de75f0Sjeremylt 27a8de75f0Sjeremylt CeedInit(argv[1], &ceed); 28a8de75f0Sjeremylt 294fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x); 304fee36f0SJeremy L Thompson { 314fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs]; 324fee36f0SJeremy L Thompson 33d1d35e2fSjeremylt for (CeedInt i = 0; i < num_dofs; i++) { 344fee36f0SJeremy L Thompson x_array[i] = (1. / (nx * 2)) * (CeedScalar)(i % (nx * 2 + 1)); 354fee36f0SJeremy L Thompson x_array[i + num_dofs] = (1. / (ny * 2)) * (CeedScalar)(i / (nx * 2 + 1)); 36a8de75f0Sjeremylt } 374fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array); 384fee36f0SJeremy L Thompson } 394fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_qpts, &q_data); 404fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &u); 414fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &v); 424fee36f0SJeremy L Thompson 434fee36f0SJeremy L Thompson // Restrictions 44d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem / 2; i++) { 45a8de75f0Sjeremylt col = i % nx; 46a8de75f0Sjeremylt row = i / nx; 47a8de75f0Sjeremylt offset = col * 2 + row * (nx * 2 + 1) * 2; 48a8de75f0Sjeremylt 494fee36f0SJeremy L Thompson ind_x[i * 2 * p + 0] = 2 + offset; 504fee36f0SJeremy L Thompson ind_x[i * 2 * p + 1] = 9 + offset; 514fee36f0SJeremy L Thompson ind_x[i * 2 * p + 2] = 16 + offset; 524fee36f0SJeremy L Thompson ind_x[i * 2 * p + 3] = 1 + offset; 534fee36f0SJeremy L Thompson ind_x[i * 2 * p + 4] = 8 + offset; 544fee36f0SJeremy L Thompson ind_x[i * 2 * p + 5] = 0 + offset; 55a8de75f0Sjeremylt 564fee36f0SJeremy L Thompson ind_x[i * 2 * p + 6] = 14 + offset; 574fee36f0SJeremy L Thompson ind_x[i * 2 * p + 7] = 7 + offset; 584fee36f0SJeremy L Thompson ind_x[i * 2 * p + 8] = 0 + offset; 594fee36f0SJeremy L Thompson ind_x[i * 2 * p + 9] = 15 + offset; 604fee36f0SJeremy L Thompson ind_x[i * 2 * p + 10] = 8 + offset; 614fee36f0SJeremy L Thompson ind_x[i * 2 * p + 11] = 16 + offset; 62a8de75f0Sjeremylt } 634fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x); 644fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u); 65a8de75f0Sjeremylt 664fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q, q}; 674fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, num_qpts, strides_q_data, &elem_restriction_q_data); 68a8de75f0Sjeremylt 69a8de75f0Sjeremylt // Bases 704fee36f0SJeremy L Thompson Build2DSimplex(q_ref, q_weight, interp, grad); 714fee36f0SJeremy L Thompson CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p, q, interp, grad, q_ref, q_weight, &basis_x); 72a8de75f0Sjeremylt 734fee36f0SJeremy L Thompson Build2DSimplex(q_ref, q_weight, interp, grad); 744fee36f0SJeremy L Thompson CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis_u); 75a8de75f0Sjeremylt 76a8de75f0Sjeremylt // QFunctions 774d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup); 78a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT); 794d537eeaSYohann CeedQFunctionAddInput(qf_setup, "x", dim * dim, CEED_EVAL_GRAD); 80a8de75f0Sjeremylt CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE); 81a8de75f0Sjeremylt 824d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass); 83a8de75f0Sjeremylt CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE); 84a8de75f0Sjeremylt CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP); 85a8de75f0Sjeremylt CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP); 86a8de75f0Sjeremylt 87a8de75f0Sjeremylt // Operators 882b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup); 894fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE); 904fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "x", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE); 91*356036faSJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE); 92a8de75f0Sjeremylt 932b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass); 94*356036faSJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data); 954fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 964fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE); 97a8de75f0Sjeremylt 984fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE); 99a8de75f0Sjeremylt 1004fee36f0SJeremy L Thompson CeedVectorSetValue(u, 1.0); 1014fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE); 102a8de75f0Sjeremylt 103a8de75f0Sjeremylt // Check output 1044fee36f0SJeremy L Thompson { 1054fee36f0SJeremy L Thompson const CeedScalar *v_array; 1064fee36f0SJeremy L Thompson CeedScalar sum = 0.; 107a8de75f0Sjeremylt 1084fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array); 1094fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) sum += v_array[i]; 1104fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array); 1114fee36f0SJeremy L Thompson if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum); 1124fee36f0SJeremy L Thompson } 1134fee36f0SJeremy L Thompson 1144fee36f0SJeremy L Thompson CeedVectorDestroy(&x); 1154fee36f0SJeremy L Thompson CeedVectorDestroy(&u); 1164fee36f0SJeremy L Thompson CeedVectorDestroy(&v); 1174fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data); 1184fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u); 1194fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x); 1204fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data); 1214fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u); 1224fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x); 123a8de75f0Sjeremylt CeedQFunctionDestroy(&qf_setup); 124a8de75f0Sjeremylt CeedQFunctionDestroy(&qf_mass); 125a8de75f0Sjeremylt CeedOperatorDestroy(&op_setup); 126a8de75f0Sjeremylt CeedOperatorDestroy(&op_mass); 127a8de75f0Sjeremylt CeedDestroy(&ceed); 128a8de75f0Sjeremylt return 0; 129a8de75f0Sjeremylt } 130