1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 /// @file 18 /// MFEM mass operator based on libCEED 19 20 #include <ceed.h> 21 #include <mfem.hpp> 22 23 /// A structure used to pass additional data to f_build_mass 24 struct BuildContext { CeedInt dim, space_dim; }; 25 26 /// libCEED Q-function for building quadrature data for a mass operator 27 static int f_build_mass(void *ctx, CeedInt Q, 28 const CeedScalar *const *in, CeedScalar *const *out) { 29 // in[0] is Jacobians, size (Q x nc x dim) with column-major layout 30 // in[1] is quadrature weights, size (Q) 31 BuildContext *bc = (BuildContext*)ctx; 32 const CeedScalar *J = in[0], *qw = in[1]; 33 CeedScalar *rho = out[0]; 34 switch (bc->dim + 10*bc->space_dim) { 35 case 11: 36 for (CeedInt i=0; i<Q; i++) { 37 rho[i] = J[i] * qw[i]; 38 } 39 break; 40 case 22: 41 for (CeedInt i=0; i<Q; i++) { 42 // 0 2 43 // 1 3 44 rho[i] = (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]) * qw[i]; 45 } 46 break; 47 case 33: 48 for (CeedInt i=0; i<Q; i++) { 49 // 0 3 6 50 // 1 4 7 51 // 2 5 8 52 rho[i] = (J[i+Q*0]*(J[i+Q*4]*J[i+Q*8] - J[i+Q*5]*J[i+Q*7]) - 53 J[i+Q*1]*(J[i+Q*3]*J[i+Q*8] - J[i+Q*5]*J[i+Q*6]) + 54 J[i+Q*2]*(J[i+Q*3]*J[i+Q*7] - J[i+Q*4]*J[i+Q*6])) * qw[i]; 55 } 56 break; 57 default: 58 return CeedError(NULL, 1, "dim=%d, space_dim=%d is not supported", 59 bc->dim, bc->space_dim); 60 } 61 return 0; 62 } 63 64 /// libCEED Q-function for applying a mass operator 65 static int f_apply_mass(void *ctx, CeedInt Q, 66 const CeedScalar *const *in, CeedScalar *const *out) { 67 const CeedScalar *u = in[0], *w = in[1]; 68 CeedScalar *v = out[0]; 69 for (CeedInt i=0; i<Q; i++) { 70 v[i] = w[i] * u[i]; 71 } 72 return 0; 73 } 74 75 /// Wrapper for a mass CeedOperator as an mfem::Operator 76 class CeedMassOperator : public mfem::Operator { 77 protected: 78 const mfem::FiniteElementSpace *fes; 79 CeedOperator build_oper, oper; 80 CeedBasis basis, mesh_basis; 81 CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i; 82 CeedQFunction apply_qfunc, build_qfunc; 83 CeedVector node_coords, rho; 84 CeedVector u, v; 85 86 BuildContext build_ctx; 87 88 static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, 89 const mfem::IntegrationRule &ir, 90 Ceed ceed, CeedBasis *basis, 91 CeedElemRestriction *restr) { 92 mfem::Mesh *mesh = fes->GetMesh(); 93 const mfem::FiniteElement *fe = fes->GetFE(0); 94 const int order = fes->GetOrder(0); 95 mfem::Array<int> dof_map; 96 switch (mesh->Dimension()) { 97 case 1: { 98 const mfem::H1_SegmentElement *h1_fe = 99 dynamic_cast<const mfem::H1_SegmentElement*>(fe); 100 MFEM_VERIFY(h1_fe, "invalid FE"); 101 h1_fe->GetDofMap().Copy(dof_map); 102 break; 103 } 104 case 2: { 105 const mfem::H1_QuadrilateralElement *h1_fe = 106 dynamic_cast<const mfem::H1_QuadrilateralElement*>(fe); 107 MFEM_VERIFY(h1_fe, "invalid FE"); 108 h1_fe->GetDofMap().Copy(dof_map); 109 break; 110 } 111 case 3: { 112 const mfem::H1_HexahedronElement *h1_fe = 113 dynamic_cast<const mfem::H1_HexahedronElement*>(fe); 114 MFEM_VERIFY(h1_fe, "invalid FE"); 115 h1_fe->GetDofMap().Copy(dof_map); 116 break; 117 } 118 } 119 const mfem::FiniteElement *fe1d = 120 fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 121 mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 122 mfem::DenseMatrix grad1d(fe1d->GetDof(), ir.GetNPoints()); 123 mfem::Vector qref1d(ir.GetNPoints()), qweight1d(ir.GetNPoints()); 124 mfem::Vector shape_i(shape1d.Height()); 125 mfem::DenseMatrix grad_i(grad1d.Height(), 1); 126 const mfem::H1_SegmentElement *h1_fe1d = 127 dynamic_cast<const mfem::H1_SegmentElement*>(fe1d); 128 MFEM_VERIFY(h1_fe1d, "invalid FE"); 129 const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 130 for (int i = 0; i < ir.GetNPoints(); i++) { 131 const mfem::IntegrationPoint &ip = ir.IntPoint(i); 132 qref1d(i) = ip.x; 133 qweight1d(i) = ip.weight; 134 fe1d->CalcShape(ip, shape_i); 135 fe1d->CalcDShape(ip, grad_i); 136 for (int j = 0; j < shape1d.Height(); j++) { 137 shape1d(j,i) = shape_i(dof_map_1d[j]); 138 grad1d(j,i) = grad_i(dof_map_1d[j],0); 139 } 140 } 141 CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1, 142 ir.GetNPoints(), shape1d.GetData(), 143 grad1d.GetData(), qref1d.GetData(), 144 qweight1d.GetData(), basis); 145 146 const mfem::Table &el_dof = fes->GetElementToDofTable(); 147 mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 148 for (int i = 0; i < mesh->GetNE(); i++) { 149 const int el_offset = fe->GetDof()*i; 150 for (int j = 0; j < fe->GetDof(); j++) { 151 tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 152 } 153 } 154 CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), 155 fes->GetNDofs(), fes->GetVDim(), CEED_MEM_HOST, CEED_COPY_VALUES, 156 tp_el_dof.GetData(), restr); 157 } 158 159 public: 160 /// Constructor. Assumes @a fes is a scalar FE space. 161 CeedMassOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) 162 : Operator(fes->GetNDofs()), 163 fes(fes) { 164 mfem::Mesh *mesh = fes->GetMesh(); 165 const int order = fes->GetOrder(0); 166 const int ir_order = 2*(order + 2) - 1; // <----- 167 const mfem::IntegrationRule &ir = 168 mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 169 CeedInt nqpts, nelem = mesh->GetNE(); 170 171 FESpace2Ceed(fes, ir, ceed, &basis, &restr); 172 173 const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 174 MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 175 FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 176 CeedBasisGetNumQuadraturePoints(basis, &nqpts); 177 178 CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 179 nqpts*nelem, 1, &restr_i); 180 CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 181 nqpts*nelem, 1, &mesh_restr_i); 182 183 CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 184 CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, 185 mesh->GetNodes()->GetData()); 186 187 CeedVectorCreate(ceed, nelem*nqpts, &rho); 188 189 // Context data to be passed to the 'f_build_mass' Q-function. 190 build_ctx.dim = mesh->Dimension(); 191 build_ctx.space_dim = mesh->SpaceDimension(); 192 193 // Create the Q-function that builds the mass operator (i.e. computes its 194 // quadrature data) and set its context data. 195 CeedQFunctionCreateInterior(ceed, 1, f_build_mass, 196 __FILE__":f_build_mass", &build_qfunc); 197 CeedQFunctionAddInput(build_qfunc, "dx", mesh->SpaceDimension(), 198 CEED_EVAL_GRAD); 199 CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 200 CeedQFunctionAddOutput(build_qfunc, "rho", 1, CEED_EVAL_NONE); 201 CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx)); 202 203 // Create the operator that builds the quadrature data for the mass operator. 204 CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper); 205 CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis, 206 CEED_VECTOR_ACTIVE); 207 CeedOperatorSetField(build_oper, "weights", mesh_restr_i, 208 mesh_basis, CEED_VECTOR_NONE); 209 CeedOperatorSetField(build_oper, "rho", restr_i, 210 CEED_BASIS_COLOCATED, CEED_VECTOR_ACTIVE); 211 212 // Compute the quadrature data for the mass operator. 213 CeedOperatorApply(build_oper, node_coords, rho, 214 CEED_REQUEST_IMMEDIATE); 215 216 // Create the Q-function that defines the action of the mass operator. 217 CeedQFunctionCreateInterior(ceed, 1, f_apply_mass, 218 __FILE__":f_apply_mass", &apply_qfunc); 219 CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP); 220 CeedQFunctionAddInput(apply_qfunc, "rho", 1, CEED_EVAL_NONE); 221 CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP); 222 223 // Create the mass operator. 224 CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper); 225 CeedOperatorSetField(oper, "u", restr, basis, CEED_VECTOR_ACTIVE); 226 CeedOperatorSetField(oper, "rho", restr_i, 227 CEED_BASIS_COLOCATED, rho); 228 CeedOperatorSetField(oper, "v", restr, basis, CEED_VECTOR_ACTIVE); 229 230 CeedVectorCreate(ceed, fes->GetNDofs(), &u); 231 CeedVectorCreate(ceed, fes->GetNDofs(), &v); 232 } 233 234 /// Destructor 235 ~CeedMassOperator() { 236 CeedVectorDestroy(&u); 237 CeedVectorDestroy(&v); 238 CeedVectorDestroy(&rho); 239 CeedVectorDestroy(&node_coords); 240 CeedOperatorDestroy(&build_oper); 241 CeedQFunctionDestroy(&build_qfunc); 242 CeedOperatorDestroy(&oper); 243 CeedQFunctionDestroy(&apply_qfunc); 244 CeedBasisDestroy(&basis); 245 CeedBasisDestroy(&mesh_basis); 246 CeedElemRestrictionDestroy(&restr); 247 CeedElemRestrictionDestroy(&mesh_restr); 248 CeedElemRestrictionDestroy(&restr_i); 249 CeedElemRestrictionDestroy(&mesh_restr_i); 250 } 251 252 /// Operator action 253 virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 254 CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 255 CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 256 CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 257 } 258 }; 259