1182fbe45STzanio // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2182fbe45STzanio // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3182fbe45STzanio // reserved. See files LICENSE and NOTICE for details. 4182fbe45STzanio // 5182fbe45STzanio // This file is part of CEED, a collection of benchmarks, miniapps, software 6182fbe45STzanio // libraries and APIs for efficient high-order finite element and spectral 7182fbe45STzanio // element discretizations for exascale applications. For more information and 8182fbe45STzanio // source code availability see http://github.com/ceed. 9182fbe45STzanio // 10182fbe45STzanio // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11182fbe45STzanio // a collaborative effort of two U.S. Department of Energy organizations (Office 12182fbe45STzanio // of Science and the National Nuclear Security Administration) responsible for 13182fbe45STzanio // the planning and preparation of a capable exascale ecosystem, including 14182fbe45STzanio // software, applications, hardware, advanced system engineering and early 15182fbe45STzanio // testbed platforms, in support of the nation's exascale computing imperative. 16182fbe45STzanio 17182fbe45STzanio /// @file 185d6bafb2Sjeremylt /// Mass operator example using MFEM 19182fbe45STzanio #include <ceed.h> 20182fbe45STzanio #include <mfem.hpp> 21182fbe45STzanio 224d537eeaSYohann #include "bp1.h" 23182fbe45STzanio 24182fbe45STzanio /// Wrapper for a mass CeedOperator as an mfem::Operator 25182fbe45STzanio class CeedMassOperator : public mfem::Operator { 26182fbe45STzanio protected: 27182fbe45STzanio const mfem::FiniteElementSpace *fes; 28182fbe45STzanio CeedOperator build_oper, oper; 29182fbe45STzanio CeedBasis basis, mesh_basis; 30135a076eSjeremylt CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i; 31182fbe45STzanio CeedQFunction apply_qfunc, build_qfunc; 32*a2fa7910SValeria Barra CeedVector node_coords, qdata; 337ca8db16Sjeremylt CeedVector u, v; 34182fbe45STzanio 35182fbe45STzanio BuildContext build_ctx; 36182fbe45STzanio 37182fbe45STzanio static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, 38182fbe45STzanio const mfem::IntegrationRule &ir, 39182fbe45STzanio Ceed ceed, CeedBasis *basis, 40182fbe45STzanio CeedElemRestriction *restr) { 41182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 42182fbe45STzanio const mfem::FiniteElement *fe = fes->GetFE(0); 43182fbe45STzanio const int order = fes->GetOrder(0); 44182fbe45STzanio mfem::Array<int> dof_map; 45182fbe45STzanio switch (mesh->Dimension()) { 46182fbe45STzanio case 1: { 47182fbe45STzanio const mfem::H1_SegmentElement *h1_fe = 48182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement *>(fe); 49182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 50182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 51182fbe45STzanio break; 52182fbe45STzanio } 53182fbe45STzanio case 2: { 54182fbe45STzanio const mfem::H1_QuadrilateralElement *h1_fe = 55182fbe45STzanio dynamic_cast<const mfem::H1_QuadrilateralElement *>(fe); 56182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 57182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 58182fbe45STzanio break; 59182fbe45STzanio } 60182fbe45STzanio case 3: { 61182fbe45STzanio const mfem::H1_HexahedronElement *h1_fe = 62182fbe45STzanio dynamic_cast<const mfem::H1_HexahedronElement *>(fe); 63182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 64182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 65182fbe45STzanio break; 66182fbe45STzanio } 67182fbe45STzanio } 68182fbe45STzanio const mfem::FiniteElement *fe1d = 69182fbe45STzanio fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 70182fbe45STzanio mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 71182fbe45STzanio mfem::DenseMatrix grad1d(fe1d->GetDof(), ir.GetNPoints()); 72182fbe45STzanio mfem::Vector qref1d(ir.GetNPoints()), qweight1d(ir.GetNPoints()); 73182fbe45STzanio mfem::Vector shape_i(shape1d.Height()); 74182fbe45STzanio mfem::DenseMatrix grad_i(grad1d.Height(), 1); 75182fbe45STzanio const mfem::H1_SegmentElement *h1_fe1d = 76182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement *>(fe1d); 77182fbe45STzanio MFEM_VERIFY(h1_fe1d, "invalid FE"); 78182fbe45STzanio const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 79182fbe45STzanio for (int i = 0; i < ir.GetNPoints(); i++) { 80182fbe45STzanio const mfem::IntegrationPoint &ip = ir.IntPoint(i); 81182fbe45STzanio qref1d(i) = ip.x; 82182fbe45STzanio qweight1d(i) = ip.weight; 83182fbe45STzanio fe1d->CalcShape(ip, shape_i); 84182fbe45STzanio fe1d->CalcDShape(ip, grad_i); 85182fbe45STzanio for (int j = 0; j < shape1d.Height(); j++) { 86182fbe45STzanio shape1d(j,i) = shape_i(dof_map_1d[j]); 87182fbe45STzanio grad1d(j,i) = grad_i(dof_map_1d[j],0); 88182fbe45STzanio } 89182fbe45STzanio } 90182fbe45STzanio CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1, 91182fbe45STzanio ir.GetNPoints(), shape1d.GetData(), 92182fbe45STzanio grad1d.GetData(), qref1d.GetData(), 93182fbe45STzanio qweight1d.GetData(), basis); 94182fbe45STzanio 95182fbe45STzanio const mfem::Table &el_dof = fes->GetElementToDofTable(); 96182fbe45STzanio mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 97182fbe45STzanio for (int i = 0; i < mesh->GetNE(); i++) { 98182fbe45STzanio const int el_offset = fe->GetDof()*i; 99182fbe45STzanio for (int j = 0; j < fe->GetDof(); j++) { 100182fbe45STzanio tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 101182fbe45STzanio } 102182fbe45STzanio } 103182fbe45STzanio CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), 104288c0443SJeremy L Thompson fes->GetNDofs(), fes->GetVDim(), CEED_MEM_HOST, 105288c0443SJeremy L Thompson CEED_COPY_VALUES, tp_el_dof.GetData(), restr); 106182fbe45STzanio } 107182fbe45STzanio 108182fbe45STzanio public: 109182fbe45STzanio /// Constructor. Assumes @a fes is a scalar FE space. 110182fbe45STzanio CeedMassOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) 111182fbe45STzanio : Operator(fes->GetNDofs()), 112182fbe45STzanio fes(fes) { 113182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 114182fbe45STzanio const int order = fes->GetOrder(0); 115182fbe45STzanio const int ir_order = 2*(order + 2) - 1; // <----- 116182fbe45STzanio const mfem::IntegrationRule &ir = 117182fbe45STzanio mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 11834d77899SValeria Barra CeedInt nqpts, nelem = mesh->GetNE(), dim = mesh->SpaceDimension(), 11934d77899SValeria Barra ncompx = dim; 120182fbe45STzanio 121182fbe45STzanio FESpace2Ceed(fes, ir, ceed, &basis, &restr); 122182fbe45STzanio 123182fbe45STzanio const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 124182fbe45STzanio MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 125182fbe45STzanio FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 1267ca8db16Sjeremylt CeedBasisGetNumQuadraturePoints(basis, &nqpts); 127182fbe45STzanio 128135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 129135a076eSjeremylt nqpts*nelem, 1, &restr_i); 130135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 131135a076eSjeremylt nqpts*nelem, 1, &mesh_restr_i); 132135a076eSjeremylt 133182fbe45STzanio CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 134182fbe45STzanio CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, 135182fbe45STzanio mesh->GetNodes()->GetData()); 136182fbe45STzanio 137*a2fa7910SValeria Barra CeedVectorCreate(ceed, nelem*nqpts, &qdata); 1387ca8db16Sjeremylt 1397ca8db16Sjeremylt // Context data to be passed to the 'f_build_mass' Q-function. 140182fbe45STzanio build_ctx.dim = mesh->Dimension(); 1414d537eeaSYohann build_ctx.space_dim = dim; 142182fbe45STzanio 1437ca8db16Sjeremylt // Create the Q-function that builds the mass operator (i.e. computes its 1447ca8db16Sjeremylt // quadrature data) and set its context data. 14554251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_build_mass, 1464d537eeaSYohann f_build_mass_loc, &build_qfunc); 14734d77899SValeria Barra CeedQFunctionAddInput(build_qfunc, "dx", ncompx*dim, 1480f5de9e9Sjeremylt CEED_EVAL_GRAD); 1497ca8db16Sjeremylt CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 150*a2fa7910SValeria Barra CeedQFunctionAddOutput(build_qfunc, "qdata", 1, CEED_EVAL_NONE); 1517ca8db16Sjeremylt CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx)); 15254251743Sjeremylt 1537ca8db16Sjeremylt // Create the operator that builds the quadrature data for the mass operator. 15454251743Sjeremylt CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper); 1554dccadb6Sjeremylt CeedOperatorSetField(build_oper, "dx", mesh_restr, CEED_NOTRANSPOSE, 1564dccadb6Sjeremylt mesh_basis, CEED_VECTOR_ACTIVE); 1574dccadb6Sjeremylt CeedOperatorSetField(build_oper, "weights", mesh_restr_i, CEED_NOTRANSPOSE, 15854251743Sjeremylt mesh_basis, CEED_VECTOR_NONE); 159*a2fa7910SValeria Barra CeedOperatorSetField(build_oper, "qdata", restr_i, CEED_NOTRANSPOSE, 160783c99b3SValeria Barra CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 161182fbe45STzanio 1627ca8db16Sjeremylt // Compute the quadrature data for the mass operator. 163*a2fa7910SValeria Barra CeedOperatorApply(build_oper, node_coords, qdata, 1647ca8db16Sjeremylt CEED_REQUEST_IMMEDIATE); 1657ca8db16Sjeremylt 1667ca8db16Sjeremylt // Create the Q-function that defines the action of the mass operator. 16754251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_apply_mass, 1684d537eeaSYohann f_apply_mass_loc, &apply_qfunc); 16954251743Sjeremylt CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP); 170*a2fa7910SValeria Barra CeedQFunctionAddInput(apply_qfunc, "qdata", 1, CEED_EVAL_NONE); 17154251743Sjeremylt CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP); 17254251743Sjeremylt 1737ca8db16Sjeremylt // Create the mass operator. 17454251743Sjeremylt CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper); 1754dccadb6Sjeremylt CeedOperatorSetField(oper, "u", restr, CEED_NOTRANSPOSE, 1764dccadb6Sjeremylt basis, CEED_VECTOR_ACTIVE); 177*a2fa7910SValeria Barra CeedOperatorSetField(oper, "qdata", restr_i, CEED_NOTRANSPOSE, 178*a2fa7910SValeria Barra CEED_BASIS_COLLOCATED, qdata); 1794dccadb6Sjeremylt CeedOperatorSetField(oper, "v", restr, CEED_NOTRANSPOSE, 1804dccadb6Sjeremylt basis, CEED_VECTOR_ACTIVE); 181182fbe45STzanio 182182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &u); 183182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &v); 184182fbe45STzanio } 185182fbe45STzanio 186182fbe45STzanio /// Destructor 187182fbe45STzanio ~CeedMassOperator() { 188182fbe45STzanio CeedVectorDestroy(&u); 1897ca8db16Sjeremylt CeedVectorDestroy(&v); 190*a2fa7910SValeria Barra CeedVectorDestroy(&qdata); 1917ca8db16Sjeremylt CeedVectorDestroy(&node_coords); 192182fbe45STzanio CeedOperatorDestroy(&build_oper); 193182fbe45STzanio CeedQFunctionDestroy(&build_qfunc); 1947ca8db16Sjeremylt CeedOperatorDestroy(&oper); 1957ca8db16Sjeremylt CeedQFunctionDestroy(&apply_qfunc); 1967ca8db16Sjeremylt CeedBasisDestroy(&basis); 197182fbe45STzanio CeedBasisDestroy(&mesh_basis); 198182fbe45STzanio CeedElemRestrictionDestroy(&restr); 1997ca8db16Sjeremylt CeedElemRestrictionDestroy(&mesh_restr); 200135a076eSjeremylt CeedElemRestrictionDestroy(&restr_i); 201135a076eSjeremylt CeedElemRestrictionDestroy(&mesh_restr_i); 202182fbe45STzanio } 203182fbe45STzanio 204182fbe45STzanio /// Operator action 205182fbe45STzanio virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 206182fbe45STzanio CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 207182fbe45STzanio CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 20854251743Sjeremylt CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 20954540941SJeremy L Thompson CeedVectorSyncArray(v, CEED_MEM_HOST); 210182fbe45STzanio } 211182fbe45STzanio }; 212