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 /// Diffusion operator example using MFEM 193d576824SJeremy L Thompson 20182fbe45STzanio #include <ceed.h> 21182fbe45STzanio #include <mfem.hpp> 224d537eeaSYohann #include "bp3.h" 23182fbe45STzanio 24182fbe45STzanio /// Wrapper for a diffusion CeedOperator as an mfem::Operator 25182fbe45STzanio class CeedDiffusionOperator : 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; 32777ff853SJeremy L Thompson CeedQFunctionContext build_ctx; 33a2fa7910SValeria Barra CeedVector node_coords, qdata; 34182fbe45STzanio 35777ff853SJeremy L Thompson BuildContext build_ctx_data; 36182fbe45STzanio 37182fbe45STzanio CeedVector u, v; 38182fbe45STzanio 39182fbe45STzanio static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, 40182fbe45STzanio const mfem::IntegrationRule &ir, 41182fbe45STzanio Ceed ceed, CeedBasis *basis, 42182fbe45STzanio CeedElemRestriction *restr) { 43182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 44182fbe45STzanio const mfem::FiniteElement *fe = fes->GetFE(0); 45182fbe45STzanio const int order = fes->GetOrder(0); 46182fbe45STzanio mfem::Array<int> dof_map; 47182fbe45STzanio switch (mesh->Dimension()) { 48182fbe45STzanio case 1: { 49182fbe45STzanio const mfem::H1_SegmentElement *h1_fe = 50182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement *>(fe); 51182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 52182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 53182fbe45STzanio break; 54182fbe45STzanio } 55182fbe45STzanio case 2: { 56182fbe45STzanio const mfem::H1_QuadrilateralElement *h1_fe = 57182fbe45STzanio dynamic_cast<const mfem::H1_QuadrilateralElement *>(fe); 58182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 59182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 60182fbe45STzanio break; 61182fbe45STzanio } 62182fbe45STzanio case 3: { 63182fbe45STzanio const mfem::H1_HexahedronElement *h1_fe = 64182fbe45STzanio dynamic_cast<const mfem::H1_HexahedronElement *>(fe); 65182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 66182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 67182fbe45STzanio break; 68182fbe45STzanio } 69182fbe45STzanio } 70182fbe45STzanio const mfem::FiniteElement *fe1d = 71182fbe45STzanio fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 72182fbe45STzanio mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 73*d1d35e2fSjeremylt mfem::DenseMatrix grad_1d(fe1d->GetDof(), ir.GetNPoints()); 74*d1d35e2fSjeremylt mfem::Vector q_ref_1d(ir.GetNPoints()), q_weight_1d(ir.GetNPoints()); 75182fbe45STzanio mfem::Vector shape_i(shape1d.Height()); 76*d1d35e2fSjeremylt mfem::DenseMatrix grad_i(grad_1d.Height(), 1); 77182fbe45STzanio const mfem::H1_SegmentElement *h1_fe1d = 78182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement *>(fe1d); 79182fbe45STzanio MFEM_VERIFY(h1_fe1d, "invalid FE"); 80182fbe45STzanio const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 81182fbe45STzanio for (int i = 0; i < ir.GetNPoints(); i++) { 82182fbe45STzanio const mfem::IntegrationPoint &ip = ir.IntPoint(i); 83*d1d35e2fSjeremylt q_ref_1d(i) = ip.x; 84*d1d35e2fSjeremylt q_weight_1d(i) = ip.weight; 85182fbe45STzanio fe1d->CalcShape(ip, shape_i); 86182fbe45STzanio fe1d->CalcDShape(ip, grad_i); 87182fbe45STzanio for (int j = 0; j < shape1d.Height(); j++) { 88182fbe45STzanio shape1d(j,i) = shape_i(dof_map_1d[j]); 89*d1d35e2fSjeremylt grad_1d(j,i) = grad_i(dof_map_1d[j],0); 90182fbe45STzanio } 91182fbe45STzanio } 92182fbe45STzanio CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1, 93182fbe45STzanio ir.GetNPoints(), shape1d.GetData(), 94*d1d35e2fSjeremylt grad_1d.GetData(), q_ref_1d.GetData(), 95*d1d35e2fSjeremylt q_weight_1d.GetData(), basis); 96182fbe45STzanio 97182fbe45STzanio const mfem::Table &el_dof = fes->GetElementToDofTable(); 98182fbe45STzanio mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 99182fbe45STzanio for (int i = 0; i < mesh->GetNE(); i++) { 100182fbe45STzanio const int el_offset = fe->GetDof()*i; 101182fbe45STzanio for (int j = 0; j < fe->GetDof(); j++) { 102182fbe45STzanio tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 103182fbe45STzanio } 104182fbe45STzanio } 105d979a051Sjeremylt CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), 106d979a051Sjeremylt fes->GetVDim(), fes->GetNDofs(), 107d979a051Sjeremylt (fes->GetVDim())*(fes->GetNDofs()), 108d979a051Sjeremylt CEED_MEM_HOST, CEED_COPY_VALUES, 109d979a051Sjeremylt tp_el_dof.GetData(), restr); 110182fbe45STzanio } 111182fbe45STzanio 112182fbe45STzanio public: 113182fbe45STzanio /// Constructor. Assumes @a fes is a scalar FE space. 114182fbe45STzanio CeedDiffusionOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) 115182fbe45STzanio : Operator(fes->GetNDofs()), 116182fbe45STzanio fes(fes) { 117182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 118182fbe45STzanio const int order = fes->GetOrder(0); 119182fbe45STzanio const int ir_order = 2*(order + 2) - 1; // <----- 120182fbe45STzanio const mfem::IntegrationRule &ir = 121182fbe45STzanio mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 122*d1d35e2fSjeremylt CeedInt num_elem = mesh->GetNE(), dim = mesh->SpaceDimension(), 123cb0b5415Sjeremylt ncompx = dim, nqpts; 124182fbe45STzanio 125182fbe45STzanio FESpace2Ceed(fes, ir, ceed, &basis, &restr); 126182fbe45STzanio 127182fbe45STzanio const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 128182fbe45STzanio MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 129182fbe45STzanio FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 130a48d94bfSjeremylt CeedBasisGetNumQuadraturePoints(basis, &nqpts); 131182fbe45STzanio 1327509a596Sjeremylt CeedInt strides[3] = {1, nqpts, nqpts *dim *(dim+1)/2}; 133*d1d35e2fSjeremylt CeedElemRestrictionCreateStrided(ceed, num_elem, nqpts, dim*(dim+1)/2, 134*d1d35e2fSjeremylt dim*(dim+1)/2*nqpts*num_elem, strides, 135d979a051Sjeremylt &restr_i); 136135a076eSjeremylt 137182fbe45STzanio CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 138182fbe45STzanio CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, 139182fbe45STzanio mesh->GetNodes()->GetData()); 140182fbe45STzanio 141*d1d35e2fSjeremylt CeedVectorCreate(ceed, num_elem*nqpts*dim*(dim+1)/2, &qdata); 1427ca8db16Sjeremylt 1437ca8db16Sjeremylt // Context data to be passed to the 'f_build_diff' Q-function. 144777ff853SJeremy L Thompson build_ctx_data.dim = mesh->Dimension(); 145777ff853SJeremy L Thompson build_ctx_data.space_dim = dim; 146777ff853SJeremy L Thompson CeedQFunctionContextCreate(ceed, &build_ctx); 147777ff853SJeremy L Thompson CeedQFunctionContextSetData(build_ctx, CEED_MEM_HOST, CEED_USE_POINTER, 148777ff853SJeremy L Thompson sizeof(build_ctx_data), &build_ctx_data); 1497ca8db16Sjeremylt 1507ca8db16Sjeremylt // Create the Q-function that builds the diff operator (i.e. computes its 1517ca8db16Sjeremylt // quadrature data) and set its context data. 15254251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_build_diff, 1534d537eeaSYohann f_build_diff_loc, &build_qfunc); 15434d77899SValeria Barra CeedQFunctionAddInput(build_qfunc, "dx", ncompx*dim, CEED_EVAL_GRAD); 1557ca8db16Sjeremylt CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 156a2fa7910SValeria Barra CeedQFunctionAddOutput(build_qfunc, "qdata", dim*(dim+1)/2, CEED_EVAL_NONE); 157777ff853SJeremy L Thompson CeedQFunctionSetContext(build_qfunc, build_ctx); 15854251743Sjeremylt 1597ca8db16Sjeremylt // Create the operator that builds the quadrature data for the diff operator. 160442e7f0bSjeremylt CeedOperatorCreate(ceed, build_qfunc, CEED_QFUNCTION_NONE, 161442e7f0bSjeremylt CEED_QFUNCTION_NONE, &build_oper); 162a8d32208Sjeremylt CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis, 163a8d32208Sjeremylt CEED_VECTOR_ACTIVE); 16415910d16Sjeremylt CeedOperatorSetField(build_oper, "weights", CEED_ELEMRESTRICTION_NONE, 16515910d16Sjeremylt mesh_basis, CEED_VECTOR_NONE); 166a8d32208Sjeremylt CeedOperatorSetField(build_oper, "qdata", restr_i, CEED_BASIS_COLLOCATED, 167a8d32208Sjeremylt CEED_VECTOR_ACTIVE); 16854251743Sjeremylt 169a48d94bfSjeremylt // Compute the quadrature data for the diff operator. 170a2fa7910SValeria Barra CeedOperatorApply(build_oper, node_coords, qdata, 171182fbe45STzanio CEED_REQUEST_IMMEDIATE); 172182fbe45STzanio 1737ca8db16Sjeremylt // Create the Q-function that defines the action of the diff operator. 17454251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_apply_diff, 1754d537eeaSYohann f_apply_diff_loc, &apply_qfunc); 1764d537eeaSYohann CeedQFunctionAddInput(apply_qfunc, "u", dim, CEED_EVAL_GRAD); 177a2fa7910SValeria Barra CeedQFunctionAddInput(apply_qfunc, "qdata", dim*(dim+1)/2, CEED_EVAL_NONE); 1784d537eeaSYohann CeedQFunctionAddOutput(apply_qfunc, "v", dim, CEED_EVAL_GRAD); 179777ff853SJeremy L Thompson CeedQFunctionSetContext(apply_qfunc, build_ctx); 18054251743Sjeremylt 181a48d94bfSjeremylt // Create the diff operator. 182442e7f0bSjeremylt CeedOperatorCreate(ceed, apply_qfunc, CEED_QFUNCTION_NONE, 183442e7f0bSjeremylt CEED_QFUNCTION_NONE, &oper); 184a8d32208Sjeremylt CeedOperatorSetField(oper, "u", restr, basis, CEED_VECTOR_ACTIVE); 185a8d32208Sjeremylt CeedOperatorSetField(oper, "qdata", restr_i, CEED_BASIS_COLLOCATED, qdata); 186a8d32208Sjeremylt CeedOperatorSetField(oper, "v", restr, basis, CEED_VECTOR_ACTIVE); 187182fbe45STzanio 188182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &u); 189182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &v); 190182fbe45STzanio } 191182fbe45STzanio 192182fbe45STzanio /// Destructor 193182fbe45STzanio ~CeedDiffusionOperator() { 194182fbe45STzanio CeedVectorDestroy(&u); 1957ca8db16Sjeremylt CeedVectorDestroy(&v); 196a2fa7910SValeria Barra CeedVectorDestroy(&qdata); 197182fbe45STzanio CeedVectorDestroy(&node_coords); 198182fbe45STzanio CeedElemRestrictionDestroy(&restr); 1997ca8db16Sjeremylt CeedElemRestrictionDestroy(&mesh_restr); 200135a076eSjeremylt CeedElemRestrictionDestroy(&restr_i); 201182fbe45STzanio CeedBasisDestroy(&basis); 2027ca8db16Sjeremylt CeedBasisDestroy(&mesh_basis); 2037ca8db16Sjeremylt CeedQFunctionDestroy(&build_qfunc); 204777ff853SJeremy L Thompson CeedQFunctionContextDestroy(&build_ctx); 2057ca8db16Sjeremylt CeedOperatorDestroy(&build_oper); 2067ca8db16Sjeremylt CeedQFunctionDestroy(&apply_qfunc); 2077ca8db16Sjeremylt CeedOperatorDestroy(&oper); 208182fbe45STzanio } 209182fbe45STzanio 210182fbe45STzanio /// Operator action 211182fbe45STzanio virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 212182fbe45STzanio CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 213182fbe45STzanio CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 214182fbe45STzanio 21554251743Sjeremylt CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 21654540941SJeremy L Thompson CeedVectorSyncArray(v, CEED_MEM_HOST); 217182fbe45STzanio } 218182fbe45STzanio }; 219