xref: /libCEED/examples/mfem/bp1.hpp (revision 0f5de9e98068b12a59dcd312c9f1e18e27ffc25d)
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) {
2954251743Sjeremylt   // in[0] is Jacobians, size (Q x nc x dim) with column-major layout
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;
81182fbe45STzanio   CeedElemRestriction restr, mesh_restr;
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 
178182fbe45STzanio     CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords);
179182fbe45STzanio     CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER,
180182fbe45STzanio                        mesh->GetNodes()->GetData());
181182fbe45STzanio 
1827ca8db16Sjeremylt     CeedVectorCreate(ceed, nelem*nqpts, &rho);
1837ca8db16Sjeremylt 
1847ca8db16Sjeremylt     // Context data to be passed to the 'f_build_mass' Q-function.
185182fbe45STzanio     build_ctx.dim = mesh->Dimension();
186182fbe45STzanio     build_ctx.space_dim = mesh->SpaceDimension();
187182fbe45STzanio 
1887ca8db16Sjeremylt     // Create the Q-function that builds the mass operator (i.e. computes its
1897ca8db16Sjeremylt     // quadrature data) and set its context data.
19054251743Sjeremylt     CeedQFunctionCreateInterior(ceed, 1, f_build_mass,
191182fbe45STzanio                                 __FILE__":f_build_mass", &build_qfunc);
192*0f5de9e9Sjeremylt     CeedQFunctionAddInput(build_qfunc, "dx", mesh->SpaceDimension(),
193*0f5de9e9Sjeremylt                           CEED_EVAL_GRAD);
1947ca8db16Sjeremylt     CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT);
1957ca8db16Sjeremylt     CeedQFunctionAddOutput(build_qfunc, "rho", 1, CEED_EVAL_NONE);
1967ca8db16Sjeremylt     CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx));
19754251743Sjeremylt 
1987ca8db16Sjeremylt     // Create the operator that builds the quadrature data for the mass operator.
19954251743Sjeremylt     CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper);
2007ca8db16Sjeremylt     CeedOperatorSetField(build_oper, "dx", mesh_restr, mesh_basis,
20154251743Sjeremylt                          CEED_VECTOR_ACTIVE);
2027ca8db16Sjeremylt     CeedOperatorSetField(build_oper, "weights", CEED_RESTRICTION_IDENTITY,
20354251743Sjeremylt                          mesh_basis, CEED_VECTOR_NONE);
2047ca8db16Sjeremylt     CeedOperatorSetField(build_oper, "rho", CEED_RESTRICTION_IDENTITY,
20554251743Sjeremylt                          CEED_BASIS_COLOCATED, CEED_VECTOR_ACTIVE);
206182fbe45STzanio 
2077ca8db16Sjeremylt     // Compute the quadrature data for the mass operator.
2087ca8db16Sjeremylt     printf("Computing the quadrature data for the mass operator ...");
2097ca8db16Sjeremylt     fflush(stdout);
2107ca8db16Sjeremylt     CeedOperatorApply(build_oper, node_coords, rho,
2117ca8db16Sjeremylt                       CEED_REQUEST_IMMEDIATE);
2127ca8db16Sjeremylt     printf(" done.\n");
2137ca8db16Sjeremylt 
2147ca8db16Sjeremylt     // Create the Q-function that defines the action of the mass operator.
21554251743Sjeremylt     CeedQFunctionCreateInterior(ceed, 1, f_apply_mass,
216182fbe45STzanio                                 __FILE__":f_apply_mass", &apply_qfunc);
21754251743Sjeremylt     CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP);
2187ca8db16Sjeremylt     CeedQFunctionAddInput(apply_qfunc, "rho", 1, CEED_EVAL_NONE);
21954251743Sjeremylt     CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP);
22054251743Sjeremylt 
2217ca8db16Sjeremylt     // Create the mass operator.
22254251743Sjeremylt     CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper);
22354251743Sjeremylt     CeedOperatorSetField(oper, "u", restr, basis, CEED_VECTOR_ACTIVE);
2247ca8db16Sjeremylt     CeedOperatorSetField(oper, "rho", CEED_RESTRICTION_IDENTITY,
2257ca8db16Sjeremylt                          CEED_BASIS_COLOCATED, rho);
22654251743Sjeremylt     CeedOperatorSetField(oper, "v", restr, basis, CEED_VECTOR_ACTIVE);
227182fbe45STzanio 
228182fbe45STzanio     CeedVectorCreate(ceed, fes->GetNDofs(), &u);
229182fbe45STzanio     CeedVectorCreate(ceed, fes->GetNDofs(), &v);
230182fbe45STzanio   }
231182fbe45STzanio 
232182fbe45STzanio   /// Destructor
233182fbe45STzanio   ~CeedMassOperator() {
234182fbe45STzanio     CeedVectorDestroy(&u);
2357ca8db16Sjeremylt     CeedVectorDestroy(&v);
2367ca8db16Sjeremylt     CeedVectorDestroy(&rho);
2377ca8db16Sjeremylt     CeedVectorDestroy(&node_coords);
238182fbe45STzanio     CeedOperatorDestroy(&build_oper);
239182fbe45STzanio     CeedQFunctionDestroy(&build_qfunc);
2407ca8db16Sjeremylt     CeedOperatorDestroy(&oper);
2417ca8db16Sjeremylt     CeedQFunctionDestroy(&apply_qfunc);
2427ca8db16Sjeremylt     CeedBasisDestroy(&basis);
243182fbe45STzanio     CeedBasisDestroy(&mesh_basis);
244182fbe45STzanio     CeedElemRestrictionDestroy(&restr);
2457ca8db16Sjeremylt     CeedElemRestrictionDestroy(&mesh_restr);
246182fbe45STzanio   }
247182fbe45STzanio 
248182fbe45STzanio   /// Operator action
249182fbe45STzanio   virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const {
250182fbe45STzanio     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData());
251182fbe45STzanio     CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData());
25254251743Sjeremylt     CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE);
253182fbe45STzanio   }
254182fbe45STzanio };
255