1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3182fbe45STzanio // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5182fbe45STzanio // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7182fbe45STzanio 8182fbe45STzanio /// @file 95d6bafb2Sjeremylt /// Mass operator example using MFEM 103d576824SJeremy L Thompson 11182fbe45STzanio #include <ceed.h> 122b730f8bSJeremy L Thompson 13182fbe45STzanio #include <mfem.hpp> 142b730f8bSJeremy L Thompson 154d537eeaSYohann #include "bp1.h" 16182fbe45STzanio 17182fbe45STzanio /// Wrapper for a mass CeedOperator as an mfem::Operator 18182fbe45STzanio class CeedMassOperator : public mfem::Operator { 19182fbe45STzanio protected: 20182fbe45STzanio const mfem::FiniteElementSpace *fes; 21182fbe45STzanio CeedOperator build_oper, oper; 22182fbe45STzanio CeedBasis basis, mesh_basis; 23135a076eSjeremylt CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i; 24182fbe45STzanio CeedQFunction apply_qfunc, build_qfunc; 25777ff853SJeremy L Thompson CeedQFunctionContext build_ctx; 26a2fa7910SValeria Barra CeedVector node_coords, qdata; 277ca8db16Sjeremylt CeedVector u, v; 28182fbe45STzanio 29777ff853SJeremy L Thompson BuildContext build_ctx_data; 30182fbe45STzanio FESpace2Ceed(const mfem::FiniteElementSpace * fes,const mfem::IntegrationRule & ir,Ceed ceed,CeedBasis * basis,CeedElemRestriction * restr)312b730f8bSJeremy L Thompson static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, const mfem::IntegrationRule &ir, Ceed ceed, CeedBasis *basis, 32182fbe45STzanio CeedElemRestriction *restr) { 33182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 34182fbe45STzanio const mfem::FiniteElement *fe = fes->GetFE(0); 35182fbe45STzanio const int order = fes->GetOrder(0); 36182fbe45STzanio mfem::Array<int> dof_map; 37182fbe45STzanio switch (mesh->Dimension()) { 38182fbe45STzanio case 1: { 392b730f8bSJeremy L Thompson const mfem::H1_SegmentElement *h1_fe = dynamic_cast<const mfem::H1_SegmentElement *>(fe); 40182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 41182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 42182fbe45STzanio break; 43182fbe45STzanio } 44182fbe45STzanio case 2: { 452b730f8bSJeremy L Thompson const mfem::H1_QuadrilateralElement *h1_fe = dynamic_cast<const mfem::H1_QuadrilateralElement *>(fe); 46182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 47182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 48182fbe45STzanio break; 49182fbe45STzanio } 50182fbe45STzanio case 3: { 512b730f8bSJeremy L Thompson const mfem::H1_HexahedronElement *h1_fe = dynamic_cast<const mfem::H1_HexahedronElement *>(fe); 52182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 53182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 54182fbe45STzanio break; 55182fbe45STzanio } 56182fbe45STzanio } 572b730f8bSJeremy L Thompson const mfem::FiniteElement *fe1d = fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 58182fbe45STzanio mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 59d1d35e2fSjeremylt mfem::DenseMatrix grad_1d(fe1d->GetDof(), ir.GetNPoints()); 60d1d35e2fSjeremylt mfem::Vector q_ref_1d(ir.GetNPoints()), q_weight_1d(ir.GetNPoints()); 61182fbe45STzanio mfem::Vector shape_i(shape1d.Height()); 62d1d35e2fSjeremylt mfem::DenseMatrix grad_i(grad_1d.Height(), 1); 632b730f8bSJeremy L Thompson const mfem::H1_SegmentElement *h1_fe1d = dynamic_cast<const mfem::H1_SegmentElement *>(fe1d); 64182fbe45STzanio MFEM_VERIFY(h1_fe1d, "invalid FE"); 65182fbe45STzanio const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 66182fbe45STzanio for (int i = 0; i < ir.GetNPoints(); i++) { 67182fbe45STzanio const mfem::IntegrationPoint &ip = ir.IntPoint(i); 68d1d35e2fSjeremylt q_ref_1d(i) = ip.x; 69d1d35e2fSjeremylt q_weight_1d(i) = ip.weight; 70182fbe45STzanio fe1d->CalcShape(ip, shape_i); 71182fbe45STzanio fe1d->CalcDShape(ip, grad_i); 72182fbe45STzanio for (int j = 0; j < shape1d.Height(); j++) { 73182fbe45STzanio shape1d(j, i) = shape_i(dof_map_1d[j]); 74d1d35e2fSjeremylt grad_1d(j, i) = grad_i(dof_map_1d[j], 0); 75182fbe45STzanio } 76182fbe45STzanio } 772b730f8bSJeremy L Thompson CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order + 1, ir.GetNPoints(), shape1d.GetData(), grad_1d.GetData(), 782b730f8bSJeremy L Thompson q_ref_1d.GetData(), q_weight_1d.GetData(), basis); 79182fbe45STzanio 80182fbe45STzanio const mfem::Table &el_dof = fes->GetElementToDofTable(); 81182fbe45STzanio mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 82182fbe45STzanio for (int i = 0; i < mesh->GetNE(); i++) { 83182fbe45STzanio const int el_offset = fe->GetDof() * i; 84182fbe45STzanio for (int j = 0; j < fe->GetDof(); j++) { 85182fbe45STzanio tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 86182fbe45STzanio } 87182fbe45STzanio } 882b730f8bSJeremy L Thompson CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), fes->GetVDim(), fes->GetNDofs(), (fes->GetVDim()) * (fes->GetNDofs()), CEED_MEM_HOST, 892b730f8bSJeremy L Thompson CEED_COPY_VALUES, tp_el_dof.GetData(), restr); 90182fbe45STzanio } 91182fbe45STzanio 92182fbe45STzanio public: 93182fbe45STzanio /// Constructor. Assumes @a fes is a scalar FE space. CeedMassOperator(Ceed ceed,const mfem::FiniteElementSpace * fes)942b730f8bSJeremy L Thompson CeedMassOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) : Operator(fes->GetNDofs()), fes(fes) { 95182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 96182fbe45STzanio const int order = fes->GetOrder(0); 97182fbe45STzanio const int ir_order = 2 * (order + 2) - 1; // <----- 982b730f8bSJeremy L Thompson const mfem::IntegrationRule &ir = mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 992b730f8bSJeremy L Thompson CeedInt num_elem = mesh->GetNE(), dim = mesh->SpaceDimension(), ncompx = dim, nqpts; 100182fbe45STzanio 101182fbe45STzanio FESpace2Ceed(fes, ir, ceed, &basis, &restr); 102182fbe45STzanio 103182fbe45STzanio const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 104182fbe45STzanio MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 105182fbe45STzanio FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 1067ca8db16Sjeremylt CeedBasisGetNumQuadraturePoints(basis, &nqpts); 107182fbe45STzanio 1087509a596Sjeremylt CeedInt strides[3] = {1, nqpts, nqpts}; 1092b730f8bSJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, nqpts, 1, nqpts * num_elem, strides, &restr_i); 110135a076eSjeremylt 111182fbe45STzanio CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 1122b730f8bSJeremy L Thompson CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, mesh->GetNodes()->GetData()); 113182fbe45STzanio 114d1d35e2fSjeremylt CeedVectorCreate(ceed, num_elem * nqpts, &qdata); 1157ca8db16Sjeremylt 1167ca8db16Sjeremylt // Context data to be passed to the 'f_build_mass' Q-function. 117777ff853SJeremy L Thompson build_ctx_data.dim = mesh->Dimension(); 118777ff853SJeremy L Thompson build_ctx_data.space_dim = dim; 119777ff853SJeremy L Thompson CeedQFunctionContextCreate(ceed, &build_ctx); 1202b730f8bSJeremy L Thompson CeedQFunctionContextSetData(build_ctx, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(build_ctx_data), &build_ctx_data); 121182fbe45STzanio 122ea61e9acSJeremy L Thompson // Create the Q-function that builds the mass operator (i.e. computes its quadrature data) and set its context data. 1232b730f8bSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, f_build_mass, f_build_mass_loc, &build_qfunc); 1242b730f8bSJeremy L Thompson CeedQFunctionAddInput(build_qfunc, "dx", ncompx * dim, CEED_EVAL_GRAD); 1257ca8db16Sjeremylt CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 126a2fa7910SValeria Barra CeedQFunctionAddOutput(build_qfunc, "qdata", 1, CEED_EVAL_NONE); 127777ff853SJeremy L Thompson CeedQFunctionSetContext(build_qfunc, build_ctx); 12854251743Sjeremylt 1297ca8db16Sjeremylt // Create the operator that builds the quadrature data for the mass operator. 1302b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, build_qfunc, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &build_oper); 1312b730f8bSJeremy L Thompson CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis, CEED_VECTOR_ACTIVE); 1322b730f8bSJeremy L Thompson CeedOperatorSetField(build_oper, "weights", CEED_ELEMRESTRICTION_NONE, mesh_basis, CEED_VECTOR_NONE); 133356036faSJeremy L Thompson CeedOperatorSetField(build_oper, "qdata", restr_i, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE); 134182fbe45STzanio 1357ca8db16Sjeremylt // Compute the quadrature data for the mass operator. 1362b730f8bSJeremy L Thompson CeedOperatorApply(build_oper, node_coords, qdata, CEED_REQUEST_IMMEDIATE); 1377ca8db16Sjeremylt 1387ca8db16Sjeremylt // Create the Q-function that defines the action of the mass operator. 1392b730f8bSJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, f_apply_mass, f_apply_mass_loc, &apply_qfunc); 14054251743Sjeremylt CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP); 141a2fa7910SValeria Barra CeedQFunctionAddInput(apply_qfunc, "qdata", 1, CEED_EVAL_NONE); 14254251743Sjeremylt CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP); 14354251743Sjeremylt 1447ca8db16Sjeremylt // Create the mass operator. 1452b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, apply_qfunc, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &oper); 146a8d32208Sjeremylt CeedOperatorSetField(oper, "u", restr, basis, CEED_VECTOR_ACTIVE); 147356036faSJeremy L Thompson CeedOperatorSetField(oper, "qdata", restr_i, CEED_BASIS_NONE, qdata); 148a8d32208Sjeremylt CeedOperatorSetField(oper, "v", restr, basis, CEED_VECTOR_ACTIVE); 149182fbe45STzanio 150182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &u); 151182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &v); 152182fbe45STzanio } 153182fbe45STzanio 154182fbe45STzanio /// Destructor ~CeedMassOperator()155182fbe45STzanio ~CeedMassOperator() { 156182fbe45STzanio CeedVectorDestroy(&u); 1577ca8db16Sjeremylt CeedVectorDestroy(&v); 158a2fa7910SValeria Barra CeedVectorDestroy(&qdata); 1597ca8db16Sjeremylt CeedVectorDestroy(&node_coords); 160182fbe45STzanio CeedOperatorDestroy(&build_oper); 161182fbe45STzanio CeedQFunctionDestroy(&build_qfunc); 1627ca8db16Sjeremylt CeedOperatorDestroy(&oper); 1637ca8db16Sjeremylt CeedQFunctionDestroy(&apply_qfunc); 164777ff853SJeremy L Thompson CeedQFunctionContextDestroy(&build_ctx); 1657ca8db16Sjeremylt CeedBasisDestroy(&basis); 166182fbe45STzanio CeedBasisDestroy(&mesh_basis); 167182fbe45STzanio CeedElemRestrictionDestroy(&restr); 1687ca8db16Sjeremylt CeedElemRestrictionDestroy(&mesh_restr); 169135a076eSjeremylt CeedElemRestrictionDestroy(&restr_i); 170182fbe45STzanio } 171182fbe45STzanio 172182fbe45STzanio /// Operator action Mult(const mfem::Vector & x,mfem::Vector & y) const173182fbe45STzanio virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 174182fbe45STzanio CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 175182fbe45STzanio CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 17654251743Sjeremylt CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 17754540941SJeremy L Thompson CeedVectorSyncArray(v, CEED_MEM_HOST); 178182fbe45STzanio } 179182fbe45STzanio }; 180