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 19182fbe45STzanio #include <ceed.h> 20182fbe45STzanio #include <mfem.hpp> 21182fbe45STzanio 22182fbe45STzanio /// A structure used to pass additional data to f_build_diff and f_apply_diff 23a48d94bfSjeremylt struct BuildContext { CeedInt dim, space_dim; }; 24182fbe45STzanio 25182fbe45STzanio /// libCEED Q-function for building quadrature data for a diffusion operator 2654251743Sjeremylt static int f_build_diff(void *ctx, CeedInt Q, 2754251743Sjeremylt const CeedScalar *const *in, CeedScalar *const *out) { 28a48d94bfSjeremylt BuildContext *bc = (BuildContext*)ctx; 29ecf6354eSJed Brown // in[0] is Jacobians with shape [dim, nc=dim, Q] 3054251743Sjeremylt // in[1] is quadrature weights, size (Q) 31182fbe45STzanio // 32182fbe45STzanio // At every quadrature point, compute qw/det(J).adj(J).adj(J)^T and store 33182fbe45STzanio // the symmetric part of the result. 347ca8db16Sjeremylt const CeedScalar *J = in[0], *qw = in[1]; 357ca8db16Sjeremylt CeedScalar *qd = out[0]; 36a48d94bfSjeremylt switch (bc->dim + 10*bc->space_dim) { 37182fbe45STzanio case 11: 38182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 39182fbe45STzanio qd[i] = qw[i] / J[i]; 40182fbe45STzanio } 41182fbe45STzanio break; 42182fbe45STzanio case 22: 43182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 44182fbe45STzanio // J: 0 2 qd: 0 1 adj(J): J22 -J12 45182fbe45STzanio // 1 3 1 2 -J21 J11 46182fbe45STzanio const CeedScalar J11 = J[i+Q*0]; 47182fbe45STzanio const CeedScalar J21 = J[i+Q*1]; 48182fbe45STzanio const CeedScalar J12 = J[i+Q*2]; 49182fbe45STzanio const CeedScalar J22 = J[i+Q*3]; 50182fbe45STzanio const CeedScalar w = qw[i] / (J11*J22 - J21*J12); 51182fbe45STzanio qd[i+Q*0] = w * (J12*J12 + J22*J22); 52182fbe45STzanio qd[i+Q*1] = - w * (J11*J12 + J21*J22); 53182fbe45STzanio qd[i+Q*2] = w * (J11*J11 + J21*J21); 54182fbe45STzanio } 55182fbe45STzanio break; 56182fbe45STzanio case 33: 57182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 58182fbe45STzanio // J: 0 3 6 qd: 0 1 2 59182fbe45STzanio // 1 4 7 1 3 4 60182fbe45STzanio // 2 5 8 2 4 5 61182fbe45STzanio const CeedScalar J11 = J[i+Q*0]; 62182fbe45STzanio const CeedScalar J21 = J[i+Q*1]; 63182fbe45STzanio const CeedScalar J31 = J[i+Q*2]; 64182fbe45STzanio const CeedScalar J12 = J[i+Q*3]; 65182fbe45STzanio const CeedScalar J22 = J[i+Q*4]; 66182fbe45STzanio const CeedScalar J32 = J[i+Q*5]; 67182fbe45STzanio const CeedScalar J13 = J[i+Q*6]; 68182fbe45STzanio const CeedScalar J23 = J[i+Q*7]; 69182fbe45STzanio const CeedScalar J33 = J[i+Q*8]; 70182fbe45STzanio const CeedScalar A11 = J22*J33 - J23*J32; 71182fbe45STzanio const CeedScalar A12 = J13*J32 - J12*J33; 72182fbe45STzanio const CeedScalar A13 = J12*J23 - J13*J22; 73182fbe45STzanio const CeedScalar A21 = J23*J31 - J21*J33; 74182fbe45STzanio const CeedScalar A22 = J11*J33 - J13*J31; 75182fbe45STzanio const CeedScalar A23 = J13*J21 - J11*J23; 76182fbe45STzanio const CeedScalar A31 = J21*J32 - J22*J31; 77182fbe45STzanio const CeedScalar A32 = J12*J31 - J11*J32; 78182fbe45STzanio const CeedScalar A33 = J11*J22 - J12*J21; 79182fbe45STzanio const CeedScalar w = qw[i] / (J11*A11 + J21*A12 + J31*A13); 80182fbe45STzanio qd[i+Q*0] = w * (A11*A11 + A12*A12 + A13*A13); 81182fbe45STzanio qd[i+Q*1] = w * (A11*A21 + A12*A22 + A13*A23); 82182fbe45STzanio qd[i+Q*2] = w * (A11*A31 + A12*A32 + A13*A33); 83182fbe45STzanio qd[i+Q*3] = w * (A21*A21 + A22*A22 + A23*A23); 84182fbe45STzanio qd[i+Q*4] = w * (A21*A31 + A22*A32 + A23*A33); 85182fbe45STzanio qd[i+Q*5] = w * (A31*A31 + A32*A32 + A33*A33); 86182fbe45STzanio } 87182fbe45STzanio break; 88182fbe45STzanio default: 89182fbe45STzanio return CeedError(NULL, 1, "dim=%d, space_dim=%d is not supported", 90a48d94bfSjeremylt bc->dim, bc->space_dim); 91182fbe45STzanio } 92182fbe45STzanio return 0; 93182fbe45STzanio } 94182fbe45STzanio 95182fbe45STzanio /// libCEED Q-function for applying a diff operator 9654251743Sjeremylt static int f_apply_diff(void *ctx, CeedInt Q, 9754251743Sjeremylt const CeedScalar *const *in, CeedScalar *const *out) { 98a48d94bfSjeremylt BuildContext *bc = (BuildContext*)ctx; 99ecf6354eSJed Brown // in[0], out[0] have shape [dim, nc=1, Q] 1007ca8db16Sjeremylt const CeedScalar *ug = in[0], *qd = in[1]; 1017ca8db16Sjeremylt CeedScalar *vg = out[0]; 102a48d94bfSjeremylt switch (bc->dim) { 103182fbe45STzanio case 1: 104182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 105182fbe45STzanio vg[i] = ug[i] * qd[i]; 106182fbe45STzanio } 107182fbe45STzanio break; 108182fbe45STzanio case 2: 109182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 110182fbe45STzanio const CeedScalar ug0 = ug[i+Q*0]; 111182fbe45STzanio const CeedScalar ug1 = ug[i+Q*1]; 112182fbe45STzanio vg[i+Q*0] = qd[i+Q*0]*ug0 + qd[i+Q*1]*ug1; 113182fbe45STzanio vg[i+Q*1] = qd[i+Q*1]*ug0 + qd[i+Q*2]*ug1; 114182fbe45STzanio } 115182fbe45STzanio break; 116182fbe45STzanio case 3: 117182fbe45STzanio for (CeedInt i=0; i<Q; i++) { 118182fbe45STzanio const CeedScalar ug0 = ug[i+Q*0]; 119182fbe45STzanio const CeedScalar ug1 = ug[i+Q*1]; 120182fbe45STzanio const CeedScalar ug2 = ug[i+Q*2]; 121182fbe45STzanio vg[i+Q*0] = qd[i+Q*0]*ug0 + qd[i+Q*1]*ug1 + qd[i+Q*2]*ug2; 122182fbe45STzanio vg[i+Q*1] = qd[i+Q*1]*ug0 + qd[i+Q*3]*ug1 + qd[i+Q*4]*ug2; 123182fbe45STzanio vg[i+Q*2] = qd[i+Q*2]*ug0 + qd[i+Q*4]*ug1 + qd[i+Q*5]*ug2; 124182fbe45STzanio } 125182fbe45STzanio break; 126182fbe45STzanio default: 127a48d94bfSjeremylt return CeedError(NULL, 1, "topo_dim=%d is not supported", bc->dim); 128182fbe45STzanio } 129182fbe45STzanio return 0; 130182fbe45STzanio } 131182fbe45STzanio 132182fbe45STzanio /// Wrapper for a diffusion CeedOperator as an mfem::Operator 133182fbe45STzanio class CeedDiffusionOperator : public mfem::Operator { 134182fbe45STzanio protected: 135182fbe45STzanio const mfem::FiniteElementSpace *fes; 136182fbe45STzanio CeedOperator build_oper, oper; 137182fbe45STzanio CeedBasis basis, mesh_basis; 138135a076eSjeremylt CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i; 139182fbe45STzanio CeedQFunction apply_qfunc, build_qfunc; 1407ca8db16Sjeremylt CeedVector node_coords, rho; 141182fbe45STzanio 142a48d94bfSjeremylt BuildContext build_ctx; 143182fbe45STzanio 144182fbe45STzanio CeedVector u, v; 145182fbe45STzanio 146182fbe45STzanio static void FESpace2Ceed(const mfem::FiniteElementSpace *fes, 147182fbe45STzanio const mfem::IntegrationRule &ir, 148182fbe45STzanio Ceed ceed, CeedBasis *basis, 149182fbe45STzanio CeedElemRestriction *restr) { 150182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 151182fbe45STzanio const mfem::FiniteElement *fe = fes->GetFE(0); 152182fbe45STzanio const int order = fes->GetOrder(0); 153182fbe45STzanio mfem::Array<int> dof_map; 154182fbe45STzanio switch (mesh->Dimension()) { 155182fbe45STzanio case 1: { 156182fbe45STzanio const mfem::H1_SegmentElement *h1_fe = 157182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement*>(fe); 158182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 159182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 160182fbe45STzanio break; 161182fbe45STzanio } 162182fbe45STzanio case 2: { 163182fbe45STzanio const mfem::H1_QuadrilateralElement *h1_fe = 164182fbe45STzanio dynamic_cast<const mfem::H1_QuadrilateralElement*>(fe); 165182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 166182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 167182fbe45STzanio break; 168182fbe45STzanio } 169182fbe45STzanio case 3: { 170182fbe45STzanio const mfem::H1_HexahedronElement *h1_fe = 171182fbe45STzanio dynamic_cast<const mfem::H1_HexahedronElement*>(fe); 172182fbe45STzanio MFEM_VERIFY(h1_fe, "invalid FE"); 173182fbe45STzanio h1_fe->GetDofMap().Copy(dof_map); 174182fbe45STzanio break; 175182fbe45STzanio } 176182fbe45STzanio } 177182fbe45STzanio const mfem::FiniteElement *fe1d = 178182fbe45STzanio fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT); 179182fbe45STzanio mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints()); 180182fbe45STzanio mfem::DenseMatrix grad1d(fe1d->GetDof(), ir.GetNPoints()); 181182fbe45STzanio mfem::Vector qref1d(ir.GetNPoints()), qweight1d(ir.GetNPoints()); 182182fbe45STzanio mfem::Vector shape_i(shape1d.Height()); 183182fbe45STzanio mfem::DenseMatrix grad_i(grad1d.Height(), 1); 184182fbe45STzanio const mfem::H1_SegmentElement *h1_fe1d = 185182fbe45STzanio dynamic_cast<const mfem::H1_SegmentElement*>(fe1d); 186182fbe45STzanio MFEM_VERIFY(h1_fe1d, "invalid FE"); 187182fbe45STzanio const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap(); 188182fbe45STzanio for (int i = 0; i < ir.GetNPoints(); i++) { 189182fbe45STzanio const mfem::IntegrationPoint &ip = ir.IntPoint(i); 190182fbe45STzanio qref1d(i) = ip.x; 191182fbe45STzanio qweight1d(i) = ip.weight; 192182fbe45STzanio fe1d->CalcShape(ip, shape_i); 193182fbe45STzanio fe1d->CalcDShape(ip, grad_i); 194182fbe45STzanio for (int j = 0; j < shape1d.Height(); j++) { 195182fbe45STzanio shape1d(j,i) = shape_i(dof_map_1d[j]); 196182fbe45STzanio grad1d(j,i) = grad_i(dof_map_1d[j],0); 197182fbe45STzanio } 198182fbe45STzanio } 199182fbe45STzanio CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1, 200182fbe45STzanio ir.GetNPoints(), shape1d.GetData(), 201182fbe45STzanio grad1d.GetData(), qref1d.GetData(), 202182fbe45STzanio qweight1d.GetData(), basis); 203182fbe45STzanio 204182fbe45STzanio const mfem::Table &el_dof = fes->GetElementToDofTable(); 205182fbe45STzanio mfem::Array<int> tp_el_dof(el_dof.Size_of_connections()); 206182fbe45STzanio for (int i = 0; i < mesh->GetNE(); i++) { 207182fbe45STzanio const int el_offset = fe->GetDof()*i; 208182fbe45STzanio for (int j = 0; j < fe->GetDof(); j++) { 209182fbe45STzanio tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset]; 210182fbe45STzanio } 211182fbe45STzanio } 212182fbe45STzanio CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(), 2137ca8db16Sjeremylt fes->GetNDofs(), fes->GetVDim(), CEED_MEM_HOST, CEED_COPY_VALUES, 214182fbe45STzanio tp_el_dof.GetData(), restr); 215182fbe45STzanio } 216182fbe45STzanio 217182fbe45STzanio public: 218182fbe45STzanio /// Constructor. Assumes @a fes is a scalar FE space. 219182fbe45STzanio CeedDiffusionOperator(Ceed ceed, const mfem::FiniteElementSpace *fes) 220182fbe45STzanio : Operator(fes->GetNDofs()), 221182fbe45STzanio fes(fes) { 222182fbe45STzanio mfem::Mesh *mesh = fes->GetMesh(); 223182fbe45STzanio const int order = fes->GetOrder(0); 224182fbe45STzanio const int ir_order = 2*(order + 2) - 1; // <----- 225182fbe45STzanio const mfem::IntegrationRule &ir = 226182fbe45STzanio mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order); 227a48d94bfSjeremylt CeedInt nqpts, nelem = mesh->GetNE(), dim = mesh->SpaceDimension(); 228182fbe45STzanio 229182fbe45STzanio FESpace2Ceed(fes, ir, ceed, &basis, &restr); 230182fbe45STzanio 231182fbe45STzanio const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace(); 232182fbe45STzanio MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space"); 233182fbe45STzanio FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr); 234a48d94bfSjeremylt CeedBasisGetNumQuadraturePoints(basis, &nqpts); 235182fbe45STzanio 236135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts*dim*(dim+1)/2, 237135a076eSjeremylt nqpts*nelem*dim*(dim+1)/2, 1, &restr_i); 238135a076eSjeremylt CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts, 239135a076eSjeremylt nqpts*nelem, 1, &mesh_restr_i); 240135a076eSjeremylt 241182fbe45STzanio CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords); 242182fbe45STzanio CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER, 243182fbe45STzanio mesh->GetNodes()->GetData()); 244182fbe45STzanio 245a48d94bfSjeremylt CeedVectorCreate(ceed, nelem*nqpts*dim*(dim+1)/2, &rho); 2467ca8db16Sjeremylt 2477ca8db16Sjeremylt // Context data to be passed to the 'f_build_diff' Q-function. 2487ca8db16Sjeremylt build_ctx.dim = mesh->Dimension(); 2497ca8db16Sjeremylt build_ctx.space_dim = mesh->SpaceDimension(); 2507ca8db16Sjeremylt 2517ca8db16Sjeremylt // Create the Q-function that builds the diff operator (i.e. computes its 2527ca8db16Sjeremylt // quadrature data) and set its context data. 25354251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_build_diff, 254182fbe45STzanio __FILE__":f_build_diff", &build_qfunc); 255a48d94bfSjeremylt CeedQFunctionAddInput(build_qfunc, "dx", dim, CEED_EVAL_GRAD); 2567ca8db16Sjeremylt CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT); 257a48d94bfSjeremylt CeedQFunctionAddOutput(build_qfunc, "rho", dim*(dim+1)/2, CEED_EVAL_NONE); 2587ca8db16Sjeremylt CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx)); 25954251743Sjeremylt 2607ca8db16Sjeremylt // Create the operator that builds the quadrature data for the diff operator. 26154251743Sjeremylt CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper); 262*4dccadb6Sjeremylt CeedOperatorSetField(build_oper, "dx", mesh_restr, CEED_NOTRANSPOSE, 263*4dccadb6Sjeremylt mesh_basis, CEED_VECTOR_ACTIVE); 264*4dccadb6Sjeremylt CeedOperatorSetField(build_oper, "weights", mesh_restr_i, CEED_NOTRANSPOSE, 2657ca8db16Sjeremylt mesh_basis, CEED_VECTOR_NONE); 266*4dccadb6Sjeremylt CeedOperatorSetField(build_oper, "rho", restr_i, CEED_NOTRANSPOSE, 267783c99b3SValeria Barra CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE); 26854251743Sjeremylt 269a48d94bfSjeremylt // Compute the quadrature data for the diff operator. 2707ca8db16Sjeremylt CeedOperatorApply(build_oper, node_coords, rho, 271182fbe45STzanio CEED_REQUEST_IMMEDIATE); 272182fbe45STzanio 2737ca8db16Sjeremylt // Create the Q-function that defines the action of the diff operator. 27454251743Sjeremylt CeedQFunctionCreateInterior(ceed, 1, f_apply_diff, 275a48d94bfSjeremylt __FILE__":f_apply_diff", &apply_qfunc); 276a48d94bfSjeremylt CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_GRAD); 277a48d94bfSjeremylt CeedQFunctionAddInput(apply_qfunc, "rho", dim*(dim+1)/2, CEED_EVAL_NONE); 278a48d94bfSjeremylt CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_GRAD); 279a48d94bfSjeremylt CeedQFunctionSetContext(apply_qfunc, &build_ctx, sizeof(build_ctx)); 28054251743Sjeremylt 281a48d94bfSjeremylt // Create the diff operator. 28254251743Sjeremylt CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper); 283*4dccadb6Sjeremylt CeedOperatorSetField(oper, "u", restr, CEED_NOTRANSPOSE, 284*4dccadb6Sjeremylt basis, CEED_VECTOR_ACTIVE); 285*4dccadb6Sjeremylt CeedOperatorSetField(oper, "rho", restr_i, CEED_NOTRANSPOSE, 286783c99b3SValeria Barra CEED_BASIS_COLLOCATED, rho); 287*4dccadb6Sjeremylt CeedOperatorSetField(oper, "v", restr, CEED_NOTRANSPOSE, 288*4dccadb6Sjeremylt basis, CEED_VECTOR_ACTIVE); 289182fbe45STzanio 290182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &u); 291182fbe45STzanio CeedVectorCreate(ceed, fes->GetNDofs(), &v); 292182fbe45STzanio } 293182fbe45STzanio 294182fbe45STzanio /// Destructor 295182fbe45STzanio ~CeedDiffusionOperator() { 296182fbe45STzanio CeedVectorDestroy(&u); 2977ca8db16Sjeremylt CeedVectorDestroy(&v); 2987ca8db16Sjeremylt CeedVectorDestroy(&rho); 299182fbe45STzanio CeedVectorDestroy(&node_coords); 300182fbe45STzanio CeedElemRestrictionDestroy(&restr); 3017ca8db16Sjeremylt CeedElemRestrictionDestroy(&mesh_restr); 302135a076eSjeremylt CeedElemRestrictionDestroy(&restr_i); 303135a076eSjeremylt CeedElemRestrictionDestroy(&mesh_restr_i); 304182fbe45STzanio CeedBasisDestroy(&basis); 3057ca8db16Sjeremylt CeedBasisDestroy(&mesh_basis); 3067ca8db16Sjeremylt CeedQFunctionDestroy(&build_qfunc); 3077ca8db16Sjeremylt CeedOperatorDestroy(&build_oper); 3087ca8db16Sjeremylt CeedQFunctionDestroy(&apply_qfunc); 3097ca8db16Sjeremylt CeedOperatorDestroy(&oper); 310182fbe45STzanio } 311182fbe45STzanio 312182fbe45STzanio /// Operator action 313182fbe45STzanio virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const { 314182fbe45STzanio CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData()); 315182fbe45STzanio CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData()); 316182fbe45STzanio 31754251743Sjeremylt CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE); 318182fbe45STzanio } 319182fbe45STzanio }; 320