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 18182fbe45STzanio /// MFEM mass operator based on libCEED 19182fbe45STzanio 20182fbe45STzanio #include <ceed.h> 21182fbe45STzanio #include <mfem.hpp> 22182fbe45STzanio 23182fbe45STzanio /// A structure used to pass additional data to f_build_mass 24182fbe45STzanio struct BuildContext { CeedInt dim, space_dim; }; 25182fbe45STzanio 26182fbe45STzanio /// libCEED Q-function for building quadrature data for a mass operator 2754251743Sjeremylt static int f_build_mass(void *ctx, CeedInt Q, 2854251743Sjeremylt const CeedScalar *const *in, CeedScalar *const *out) { 29ecf6354eSJed Brown // in[0] is Jacobians with shape [dim, nc=dim, Q] 3054251743Sjeremylt // in[1] is quadrature weights, size (Q) 31182fbe45STzanio BuildContext *bc = (BuildContext*)ctx; 327ca8db16Sjeremylt const CeedScalar *J = in[0], *qw = in[1]; 337ca8db16Sjeremylt CeedScalar *rho = out[0]; 34182fbe45STzanio switch (bc->dim + 10*bc->space_dim) { 35182fbe45STzanio case 11: 36182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 377ca8db16Sjeremylt rho[i] = J[i] * qw[i]; 38182fbe45STzanio } 39182fbe45STzanio break; 40182fbe45STzanio case 22: 41182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 42182fbe45STzanio // 0 2 43182fbe45STzanio // 1 3 447ca8db16Sjeremylt rho[i] = (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]) * qw[i]; 45182fbe45STzanio } 46182fbe45STzanio break; 47182fbe45STzanio case 33: 48182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 49182fbe45STzanio // 0 3 6 50182fbe45STzanio // 1 4 7 51182fbe45STzanio // 2 5 8 527ca8db16Sjeremylt rho[i] = (J[i+Q*0]*(J[i+Q*4]*J[i+Q*8] - J[i+Q*5]*J[i+Q*7]) - 53182fbe45STzanio J[i+Q*1]*(J[i+Q*3]*J[i+Q*8] - J[i+Q*5]*J[i+Q*6]) + 54182fbe45STzanio J[i+Q*2]*(J[i+Q*3]*J[i+Q*7] - J[i+Q*4]*J[i+Q*6])) * qw[i]; 55182fbe45STzanio } 56182fbe45STzanio break; 57182fbe45STzanio default: 58182fbe45STzanio return CeedError(NULL, 1, "dim=%d, space_dim=%d is not supported", 59182fbe45STzanio bc->dim, bc->space_dim); 60182fbe45STzanio } 61182fbe45STzanio return 0; 62182fbe45STzanio } 63182fbe45STzanio 64182fbe45STzanio /// libCEED Q-function for applying a mass operator 6554251743Sjeremylt static int f_apply_mass(void *ctx, CeedInt Q, 6654251743Sjeremylt const CeedScalar *const *in, CeedScalar *const *out) { 677ca8db16Sjeremylt const CeedScalar *u = in[0], *w = in[1]; 687ca8db16Sjeremylt CeedScalar *v = out[0]; 69182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 7054251743Sjeremylt v[i] = w[i] * u[i]; 71182fbe45STzanio } 72182fbe45STzanio return 0; 73182fbe45STzanio } 74182fbe45STzanio 75182fbe45STzanio /// Wrapper for a mass CeedOperator as an mfem::Operator 76182fbe45STzanio class CeedMassOperator : public mfem::Operator { 77182fbe45STzanio protected: 78182fbe45STzanio const mfem::FiniteElementSpace *fes; 79182fbe45STzanio CeedOperator build_oper, oper; 80182fbe45STzanio CeedBasis basis, mesh_basis; 81135a076eSjeremylt CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i; 82182fbe45STzanio CeedQFunction apply_qfunc, build_qfunc; 837ca8db16Sjeremylt CeedVector node_coords, rho; 847ca8db16Sjeremylt CeedVector u, v; 85182fbe45STzanio 86182fbe45STzanio BuildContext build_ctx; 87182fbe45STzanio 88182fbe45STzanio static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, 89182fbe45STzanio const mfem::IntegrationRule &ir, 90182fbe45STzanio Ceed ceed, CeedBasis *basis, 91182fbe45STzanio CeedElemRestriction *restr) { 92182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 93182fbe45STzanio const mfem::FiniteElement *fe = fes->GetFE(0); 94182fbe45STzanio const int order = fes->GetOrder(0); 95182fbe45STzanio mfem::Array<int> dof_map; 96182fbe45STzanio switch (mesh->Dimension()) { 97182fbe45STzanio case 1: { 98182fbe45STzanio const mfem::H1_SegmentElement *h1_fe = 99182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement*>(fe); 100182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 101182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 102182fbe45STzanio break; 103182fbe45STzanio } 104182fbe45STzanio case 2: { 105182fbe45STzanio const mfem::H1_QuadrilateralElement *h1_fe = 106182fbe45STzanio dynamic_cast<const mfem::H1_QuadrilateralElement*>(fe); 107182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 108182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 109182fbe45STzanio break; 110182fbe45STzanio } 111182fbe45STzanio case 3: { 112182fbe45STzanio const mfem::H1_HexahedronElement *h1_fe = 113182fbe45STzanio dynamic_cast<const mfem::H1_HexahedronElement*>(fe); 114182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 115182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 116182fbe45STzanio break; 117182fbe45STzanio } 118182fbe45STzanio } 119182fbe45STzanio const mfem::FiniteElement *fe1d = 120182fbe45STzanio fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 121182fbe45STzanio mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 122182fbe45STzanio mfem::DenseMatrix grad1d(fe1d->GetDof(), ir.GetNPoints()); 123182fbe45STzanio mfem::Vector qref1d(ir.GetNPoints()), qweight1d(ir.GetNPoints()); 124182fbe45STzanio mfem::Vector shape_i(shape1d.Height()); 125182fbe45STzanio mfem::DenseMatrix grad_i(grad1d.Height(), 1); 126182fbe45STzanio const mfem::H1_SegmentElement *h1_fe1d = 127182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement*>(fe1d); 128182fbe45STzanio MFEM_VERIFY(h1_fe1d, "invalid FE"); 129182fbe45STzanio const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 130182fbe45STzanio for (int i = 0; i < ir.GetNPoints(); i++) { 131182fbe45STzanio const mfem::IntegrationPoint &ip = ir.IntPoint(i); 132182fbe45STzanio qref1d(i) = ip.x; 133182fbe45STzanio qweight1d(i) = ip.weight; 134182fbe45STzanio fe1d->CalcShape(ip, shape_i); 135182fbe45STzanio fe1d->CalcDShape(ip, grad_i); 136182fbe45STzanio for (int j = 0; j < shape1d.Height(); j++) { 137182fbe45STzanio shape1d(j,i) = shape_i(dof_map_1d[j]); 138182fbe45STzanio grad1d(j,i) = grad_i(dof_map_1d[j],0); 139182fbe45STzanio } 140182fbe45STzanio } 141182fbe45STzanio CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1, 142182fbe45STzanio ir.GetNPoints(), shape1d.GetData(), 143182fbe45STzanio grad1d.GetData(), qref1d.GetData(), 144182fbe45STzanio qweight1d.GetData(), basis); 145182fbe45STzanio 146182fbe45STzanio const mfem::Table &el_dof = fes->GetElementToDofTable(); 147182fbe45STzanio mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 148182fbe45STzanio for (int i = 0; i < mesh->GetNE(); i++) { 149182fbe45STzanio const int el_offset = fe->GetDof()*i; 150182fbe45STzanio for (int j = 0; j < fe->GetDof(); j++) { 151182fbe45STzanio tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 152182fbe45STzanio } 153182fbe45STzanio } 154182fbe45STzanio CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), 1557ca8db16Sjeremylt fes->GetNDofs(), fes->GetVDim(), CEED_MEM_HOST, CEED_COPY_VALUES, 156182fbe45STzanio tp_el_dof.GetData(), restr); 157182fbe45STzanio } 158182fbe45STzanio 159182fbe45STzanio public: 160182fbe45STzanio /// Constructor. Assumes @a fes is a scalar FE space. 161182fbe45STzanio CeedMassOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) 162182fbe45STzanio : Operator(fes->GetNDofs()), 163182fbe45STzanio fes(fes) { 164182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 165182fbe45STzanio const int order = fes->GetOrder(0); 166182fbe45STzanio const int ir_order = 2*(order + 2) - 1; // <----- 167182fbe45STzanio const mfem::IntegrationRule &ir = 168182fbe45STzanio mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 1697ca8db16Sjeremylt CeedInt nqpts, nelem = mesh->GetNE(); 170182fbe45STzanio 171182fbe45STzanio FESpace2Ceed(fes, ir, ceed, &basis, &restr); 172182fbe45STzanio 173182fbe45STzanio const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 174182fbe45STzanio MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 175182fbe45STzanio FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 1767ca8db16Sjeremylt CeedBasisGetNumQuadraturePoints(basis, &nqpts); 177182fbe45STzanio 178135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 179135a076eSjeremylt nqpts*nelem, 1, &restr_i); 180135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 181135a076eSjeremylt nqpts*nelem, 1, &mesh_restr_i); 182135a076eSjeremylt 183182fbe45STzanio CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 184182fbe45STzanio CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, 185182fbe45STzanio mesh->GetNodes()->GetData()); 186182fbe45STzanio 1877ca8db16Sjeremylt CeedVectorCreate(ceed, nelem*nqpts, &rho); 1887ca8db16Sjeremylt 1897ca8db16Sjeremylt // Context data to be passed to the 'f_build_mass' Q-function. 190182fbe45STzanio build_ctx.dim = mesh->Dimension(); 191182fbe45STzanio build_ctx.space_dim = mesh->SpaceDimension(); 192182fbe45STzanio 1937ca8db16Sjeremylt // Create the Q-function that builds the mass operator (i.e. computes its 1947ca8db16Sjeremylt // quadrature data) and set its context data. 19554251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_build_mass, 196182fbe45STzanio __FILE__":f_build_mass", &build_qfunc); 1970f5de9e9Sjeremylt CeedQFunctionAddInput(build_qfunc, "dx", mesh->SpaceDimension(), 1980f5de9e9Sjeremylt CEED_EVAL_GRAD); 1997ca8db16Sjeremylt CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 2007ca8db16Sjeremylt CeedQFunctionAddOutput(build_qfunc, "rho", 1, CEED_EVAL_NONE); 2017ca8db16Sjeremylt CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx)); 20254251743Sjeremylt 2037ca8db16Sjeremylt // Create the operator that builds the quadrature data for the mass operator. 20454251743Sjeremylt CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper); 2057ca8db16Sjeremylt CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis, 20654251743Sjeremylt CEED_VECTOR_ACTIVE); 207135a076eSjeremylt CeedOperatorSetField(build_oper, "weights", mesh_restr_i, 20854251743Sjeremylt mesh_basis, CEED_VECTOR_NONE); 209135a076eSjeremylt CeedOperatorSetField(build_oper, "rho", restr_i, 210*783c99b3SValeria Barra CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 211182fbe45STzanio 2127ca8db16Sjeremylt // Compute the quadrature data for the mass operator. 2137ca8db16Sjeremylt CeedOperatorApply(build_oper, node_coords, rho, 2147ca8db16Sjeremylt CEED_REQUEST_IMMEDIATE); 2157ca8db16Sjeremylt 2167ca8db16Sjeremylt // Create the Q-function that defines the action of the mass operator. 21754251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_apply_mass, 218182fbe45STzanio __FILE__":f_apply_mass", &apply_qfunc); 21954251743Sjeremylt CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP); 2207ca8db16Sjeremylt CeedQFunctionAddInput(apply_qfunc, "rho", 1, CEED_EVAL_NONE); 22154251743Sjeremylt CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP); 22254251743Sjeremylt 2237ca8db16Sjeremylt // Create the mass operator. 22454251743Sjeremylt CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper); 22554251743Sjeremylt CeedOperatorSetField(oper, "u", restr, basis, CEED_VECTOR_ACTIVE); 226135a076eSjeremylt CeedOperatorSetField(oper, "rho", restr_i, 227*783c99b3SValeria Barra CEED_BASIS_COLLOCATED, rho); 22854251743Sjeremylt CeedOperatorSetField(oper, "v", restr, basis, CEED_VECTOR_ACTIVE); 229182fbe45STzanio 230182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &u); 231182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &v); 232182fbe45STzanio } 233182fbe45STzanio 234182fbe45STzanio /// Destructor 235182fbe45STzanio ~CeedMassOperator() { 236182fbe45STzanio CeedVectorDestroy(&u); 2377ca8db16Sjeremylt CeedVectorDestroy(&v); 2387ca8db16Sjeremylt CeedVectorDestroy(&rho); 2397ca8db16Sjeremylt CeedVectorDestroy(&node_coords); 240182fbe45STzanio CeedOperatorDestroy(&build_oper); 241182fbe45STzanio CeedQFunctionDestroy(&build_qfunc); 2427ca8db16Sjeremylt CeedOperatorDestroy(&oper); 2437ca8db16Sjeremylt CeedQFunctionDestroy(&apply_qfunc); 2447ca8db16Sjeremylt CeedBasisDestroy(&basis); 245182fbe45STzanio CeedBasisDestroy(&mesh_basis); 246182fbe45STzanio CeedElemRestrictionDestroy(&restr); 2477ca8db16Sjeremylt CeedElemRestrictionDestroy(&mesh_restr); 248135a076eSjeremylt CeedElemRestrictionDestroy(&restr_i); 249135a076eSjeremylt CeedElemRestrictionDestroy(&mesh_restr_i); 250182fbe45STzanio } 251182fbe45STzanio 252182fbe45STzanio /// Operator action 253182fbe45STzanio virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 254182fbe45STzanio CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 255182fbe45STzanio CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 25654251743Sjeremylt CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 257182fbe45STzanio } 258182fbe45STzanio }; 259