xref: /libCEED/examples/mfem/bp1.hpp (revision 4dccadb61a9bb3ddd06b933b05f6f28773cf32d8)
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 /// Mass operator example using MFEM
19182fbe45STzanio #include <ceed.h>
20182fbe45STzanio #include <mfem.hpp>
21182fbe45STzanio 
22182fbe45STzanio /// A structure used to pass additional data to f_build_mass
23182fbe45STzanio struct BuildContext { CeedInt dim, space_dim; };
24182fbe45STzanio 
25182fbe45STzanio /// libCEED Q-function for building quadrature data for a mass operator
2654251743Sjeremylt static int f_build_mass(void *ctx, CeedInt Q,
2754251743Sjeremylt                         const CeedScalar *const *in, CeedScalar *const *out) {
28ecf6354eSJed Brown   // in[0] is Jacobians with shape [dim, nc=dim, Q]
2954251743Sjeremylt   // in[1] is quadrature weights, size (Q)
30182fbe45STzanio   BuildContext *bc = (BuildContext*)ctx;
317ca8db16Sjeremylt   const CeedScalar *J = in[0], *qw = in[1];
327ca8db16Sjeremylt   CeedScalar *rho = out[0];
33182fbe45STzanio   switch (bc->dim + 10*bc->space_dim) {
34182fbe45STzanio   case 11:
35182fbe45STzanio     for (CeedInt i=0; i<Q; i++) {
367ca8db16Sjeremylt       rho[i] = J[i] * qw[i];
37182fbe45STzanio     }
38182fbe45STzanio     break;
39182fbe45STzanio   case 22:
40182fbe45STzanio     for (CeedInt i=0; i<Q; i++) {
41182fbe45STzanio       // 0 2
42182fbe45STzanio       // 1 3
437ca8db16Sjeremylt       rho[i] = (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]) * qw[i];
44182fbe45STzanio     }
45182fbe45STzanio     break;
46182fbe45STzanio   case 33:
47182fbe45STzanio     for (CeedInt i=0; i<Q; i++) {
48182fbe45STzanio       // 0 3 6
49182fbe45STzanio       // 1 4 7
50182fbe45STzanio       // 2 5 8
517ca8db16Sjeremylt       rho[i] = (J[i+Q*0]*(J[i+Q*4]*J[i+Q*8] - J[i+Q*5]*J[i+Q*7]) -
52182fbe45STzanio                 J[i+Q*1]*(J[i+Q*3]*J[i+Q*8] - J[i+Q*5]*J[i+Q*6]) +
53182fbe45STzanio                 J[i+Q*2]*(J[i+Q*3]*J[i+Q*7] - J[i+Q*4]*J[i+Q*6])) * qw[i];
54182fbe45STzanio     }
55182fbe45STzanio     break;
56182fbe45STzanio   default:
57182fbe45STzanio     return CeedError(NULL, 1, "dim=%d, space_dim=%d is not supported",
58182fbe45STzanio                      bc->dim, bc->space_dim);
59182fbe45STzanio   }
60182fbe45STzanio   return 0;
61182fbe45STzanio }
62182fbe45STzanio 
63182fbe45STzanio /// libCEED Q-function for applying a mass operator
6454251743Sjeremylt static int f_apply_mass(void *ctx, CeedInt Q,
6554251743Sjeremylt                         const CeedScalar *const *in, CeedScalar *const *out) {
667ca8db16Sjeremylt   const CeedScalar *u = in[0], *w = in[1];
677ca8db16Sjeremylt   CeedScalar *v = out[0];
68182fbe45STzanio   for (CeedInt i=0; i<Q; i++) {
6954251743Sjeremylt     v[i] = w[i] * u[i];
70182fbe45STzanio   }
71182fbe45STzanio   return 0;
72182fbe45STzanio }
73182fbe45STzanio 
74182fbe45STzanio /// Wrapper for a mass CeedOperator as an mfem::Operator
75182fbe45STzanio class CeedMassOperator : public mfem::Operator {
76182fbe45STzanio  protected:
77182fbe45STzanio   const mfem::FiniteElementSpace *fes;
78182fbe45STzanio   CeedOperator build_oper, oper;
79182fbe45STzanio   CeedBasis basis, mesh_basis;
80135a076eSjeremylt   CeedElemRestriction restr, mesh_restr, restr_i, mesh_restr_i;
81182fbe45STzanio   CeedQFunction apply_qfunc, build_qfunc;
827ca8db16Sjeremylt   CeedVector node_coords, rho;
837ca8db16Sjeremylt   CeedVector u, v;
84182fbe45STzanio 
85182fbe45STzanio   BuildContext build_ctx;
86182fbe45STzanio 
87182fbe45STzanio   static void FESpace2Ceed(const mfem::FiniteElementSpace *fes,
88182fbe45STzanio                            const mfem::IntegrationRule &ir,
89182fbe45STzanio                            Ceed ceed, CeedBasis *basis,
90182fbe45STzanio                            CeedElemRestriction *restr) {
91182fbe45STzanio     mfem::Mesh *mesh = fes->GetMesh();
92182fbe45STzanio     const mfem::FiniteElement *fe = fes->GetFE(0);
93182fbe45STzanio     const int order = fes->GetOrder(0);
94182fbe45STzanio     mfem::Array<int> dof_map;
95182fbe45STzanio     switch (mesh->Dimension()) {
96182fbe45STzanio     case 1: {
97182fbe45STzanio       const mfem::H1_SegmentElement *h1_fe =
98182fbe45STzanio         dynamic_cast<const mfem::H1_SegmentElement*>(fe);
99182fbe45STzanio       MFEM_VERIFY(h1_fe, "invalid FE");
100182fbe45STzanio       h1_fe->GetDofMap().Copy(dof_map);
101182fbe45STzanio       break;
102182fbe45STzanio     }
103182fbe45STzanio     case 2: {
104182fbe45STzanio       const mfem::H1_QuadrilateralElement *h1_fe =
105182fbe45STzanio         dynamic_cast<const mfem::H1_QuadrilateralElement*>(fe);
106182fbe45STzanio       MFEM_VERIFY(h1_fe, "invalid FE");
107182fbe45STzanio       h1_fe->GetDofMap().Copy(dof_map);
108182fbe45STzanio       break;
109182fbe45STzanio     }
110182fbe45STzanio     case 3: {
111182fbe45STzanio       const mfem::H1_HexahedronElement *h1_fe =
112182fbe45STzanio         dynamic_cast<const mfem::H1_HexahedronElement*>(fe);
113182fbe45STzanio       MFEM_VERIFY(h1_fe, "invalid FE");
114182fbe45STzanio       h1_fe->GetDofMap().Copy(dof_map);
115182fbe45STzanio       break;
116182fbe45STzanio     }
117182fbe45STzanio     }
118182fbe45STzanio     const mfem::FiniteElement *fe1d =
119182fbe45STzanio       fes->FEColl()->FiniteElementForGeometry(mfem::Geometry::SEGMENT);
120182fbe45STzanio     mfem::DenseMatrix shape1d(fe1d->GetDof(), ir.GetNPoints());
121182fbe45STzanio     mfem::DenseMatrix grad1d(fe1d->GetDof(), ir.GetNPoints());
122182fbe45STzanio     mfem::Vector qref1d(ir.GetNPoints()), qweight1d(ir.GetNPoints());
123182fbe45STzanio     mfem::Vector shape_i(shape1d.Height());
124182fbe45STzanio     mfem::DenseMatrix grad_i(grad1d.Height(), 1);
125182fbe45STzanio     const mfem::H1_SegmentElement *h1_fe1d =
126182fbe45STzanio       dynamic_cast<const mfem::H1_SegmentElement*>(fe1d);
127182fbe45STzanio     MFEM_VERIFY(h1_fe1d, "invalid FE");
128182fbe45STzanio     const mfem::Array<int> &dof_map_1d = h1_fe1d->GetDofMap();
129182fbe45STzanio     for (int i = 0; i < ir.GetNPoints(); i++) {
130182fbe45STzanio       const mfem::IntegrationPoint &ip = ir.IntPoint(i);
131182fbe45STzanio       qref1d(i) = ip.x;
132182fbe45STzanio       qweight1d(i) = ip.weight;
133182fbe45STzanio       fe1d->CalcShape(ip, shape_i);
134182fbe45STzanio       fe1d->CalcDShape(ip, grad_i);
135182fbe45STzanio       for (int j = 0; j < shape1d.Height(); j++) {
136182fbe45STzanio         shape1d(j,i) = shape_i(dof_map_1d[j]);
137182fbe45STzanio         grad1d(j,i) = grad_i(dof_map_1d[j],0);
138182fbe45STzanio       }
139182fbe45STzanio     }
140182fbe45STzanio     CeedBasisCreateTensorH1(ceed, mesh->Dimension(), fes->GetVDim(), order+1,
141182fbe45STzanio                             ir.GetNPoints(), shape1d.GetData(),
142182fbe45STzanio                             grad1d.GetData(), qref1d.GetData(),
143182fbe45STzanio                             qweight1d.GetData(), basis);
144182fbe45STzanio 
145182fbe45STzanio     const mfem::Table &el_dof = fes->GetElementToDofTable();
146182fbe45STzanio     mfem::Array<int> tp_el_dof(el_dof.Size_of_connections());
147182fbe45STzanio     for (int i = 0; i < mesh->GetNE(); i++) {
148182fbe45STzanio       const int el_offset = fe->GetDof()*i;
149182fbe45STzanio       for (int j = 0; j < fe->GetDof(); j++) {
150182fbe45STzanio         tp_el_dof[j + el_offset] = el_dof.GetJ()[dof_map[j] + el_offset];
151182fbe45STzanio       }
152182fbe45STzanio     }
153182fbe45STzanio     CeedElemRestrictionCreate(ceed, mesh->GetNE(), fe->GetDof(),
1547ca8db16Sjeremylt                               fes->GetNDofs(), fes->GetVDim(), CEED_MEM_HOST, CEED_COPY_VALUES,
155182fbe45STzanio                               tp_el_dof.GetData(), restr);
156182fbe45STzanio   }
157182fbe45STzanio 
158182fbe45STzanio  public:
159182fbe45STzanio   /// Constructor. Assumes @a fes is a scalar FE space.
160182fbe45STzanio   CeedMassOperator(Ceed ceed, const mfem::FiniteElementSpace *fes)
161182fbe45STzanio     : Operator(fes->GetNDofs()),
162182fbe45STzanio       fes(fes) {
163182fbe45STzanio     mfem::Mesh *mesh = fes->GetMesh();
164182fbe45STzanio     const int order = fes->GetOrder(0);
165182fbe45STzanio     const int ir_order = 2*(order + 2) - 1; // <-----
166182fbe45STzanio     const mfem::IntegrationRule &ir =
167182fbe45STzanio       mfem::IntRules.Get(mfem::Geometry::SEGMENT, ir_order);
1687ca8db16Sjeremylt     CeedInt nqpts, nelem = mesh->GetNE();
169182fbe45STzanio 
170182fbe45STzanio     FESpace2Ceed(fes, ir, ceed, &basis, &restr);
171182fbe45STzanio 
172182fbe45STzanio     const mfem::FiniteElementSpace *mesh_fes = mesh->GetNodalFESpace();
173182fbe45STzanio     MFEM_VERIFY(mesh_fes, "the Mesh has no nodal FE space");
174182fbe45STzanio     FESpace2Ceed(mesh_fes, ir, ceed, &mesh_basis, &mesh_restr);
1757ca8db16Sjeremylt     CeedBasisGetNumQuadraturePoints(basis, &nqpts);
176182fbe45STzanio 
177135a076eSjeremylt     CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts,
178135a076eSjeremylt                                       nqpts*nelem, 1, &restr_i);
179135a076eSjeremylt     CeedElemRestrictionCreateIdentity(ceed, nelem, nqpts,
180135a076eSjeremylt                                       nqpts*nelem, 1, &mesh_restr_i);
181135a076eSjeremylt 
182182fbe45STzanio     CeedVectorCreate(ceed, mesh->GetNodes()->Size(), &node_coords);
183182fbe45STzanio     CeedVectorSetArray(node_coords, CEED_MEM_HOST, CEED_USE_POINTER,
184182fbe45STzanio                        mesh->GetNodes()->GetData());
185182fbe45STzanio 
1867ca8db16Sjeremylt     CeedVectorCreate(ceed, nelem*nqpts, &rho);
1877ca8db16Sjeremylt 
1887ca8db16Sjeremylt     // Context data to be passed to the 'f_build_mass' Q-function.
189182fbe45STzanio     build_ctx.dim = mesh->Dimension();
190182fbe45STzanio     build_ctx.space_dim = mesh->SpaceDimension();
191182fbe45STzanio 
1927ca8db16Sjeremylt     // Create the Q-function that builds the mass operator (i.e. computes its
1937ca8db16Sjeremylt     // quadrature data) and set its context data.
19454251743Sjeremylt     CeedQFunctionCreateInterior(ceed, 1, f_build_mass,
195182fbe45STzanio                                 __FILE__":f_build_mass", &build_qfunc);
1960f5de9e9Sjeremylt     CeedQFunctionAddInput(build_qfunc, "dx", mesh->SpaceDimension(),
1970f5de9e9Sjeremylt                           CEED_EVAL_GRAD);
1987ca8db16Sjeremylt     CeedQFunctionAddInput(build_qfunc, "weights", 1, CEED_EVAL_WEIGHT);
1997ca8db16Sjeremylt     CeedQFunctionAddOutput(build_qfunc, "rho", 1, CEED_EVAL_NONE);
2007ca8db16Sjeremylt     CeedQFunctionSetContext(build_qfunc, &build_ctx, sizeof(build_ctx));
20154251743Sjeremylt 
2027ca8db16Sjeremylt     // Create the operator that builds the quadrature data for the mass operator.
20354251743Sjeremylt     CeedOperatorCreate(ceed, build_qfunc, NULL, NULL, &build_oper);
204*4dccadb6Sjeremylt     CeedOperatorSetField(build_oper, "dx", mesh_restr, CEED_NOTRANSPOSE,
205*4dccadb6Sjeremylt                          mesh_basis, CEED_VECTOR_ACTIVE);
206*4dccadb6Sjeremylt     CeedOperatorSetField(build_oper, "weights", mesh_restr_i, CEED_NOTRANSPOSE,
20754251743Sjeremylt                          mesh_basis, CEED_VECTOR_NONE);
208*4dccadb6Sjeremylt     CeedOperatorSetField(build_oper, "rho", restr_i, CEED_NOTRANSPOSE,
209783c99b3SValeria Barra                          CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
210182fbe45STzanio 
2117ca8db16Sjeremylt     // Compute the quadrature data for the mass operator.
2127ca8db16Sjeremylt     CeedOperatorApply(build_oper, node_coords, rho,
2137ca8db16Sjeremylt                       CEED_REQUEST_IMMEDIATE);
2147ca8db16Sjeremylt 
2157ca8db16Sjeremylt     // Create the Q-function that defines the action of the mass operator.
21654251743Sjeremylt     CeedQFunctionCreateInterior(ceed, 1, f_apply_mass,
217182fbe45STzanio                                 __FILE__":f_apply_mass", &apply_qfunc);
21854251743Sjeremylt     CeedQFunctionAddInput(apply_qfunc, "u", 1, CEED_EVAL_INTERP);
2197ca8db16Sjeremylt     CeedQFunctionAddInput(apply_qfunc, "rho", 1, CEED_EVAL_NONE);
22054251743Sjeremylt     CeedQFunctionAddOutput(apply_qfunc, "v", 1, CEED_EVAL_INTERP);
22154251743Sjeremylt 
2227ca8db16Sjeremylt     // Create the mass operator.
22354251743Sjeremylt     CeedOperatorCreate(ceed, apply_qfunc, NULL, NULL, &oper);
224*4dccadb6Sjeremylt     CeedOperatorSetField(oper, "u", restr, CEED_NOTRANSPOSE,
225*4dccadb6Sjeremylt                          basis, CEED_VECTOR_ACTIVE);
226*4dccadb6Sjeremylt     CeedOperatorSetField(oper, "rho", restr_i, CEED_NOTRANSPOSE,
227783c99b3SValeria Barra                          CEED_BASIS_COLLOCATED, rho);
228*4dccadb6Sjeremylt     CeedOperatorSetField(oper, "v", restr, CEED_NOTRANSPOSE,
229*4dccadb6Sjeremylt                          basis, CEED_VECTOR_ACTIVE);
230182fbe45STzanio 
231182fbe45STzanio     CeedVectorCreate(ceed, fes->GetNDofs(), &u);
232182fbe45STzanio     CeedVectorCreate(ceed, fes->GetNDofs(), &v);
233182fbe45STzanio   }
234182fbe45STzanio 
235182fbe45STzanio   /// Destructor
236182fbe45STzanio   ~CeedMassOperator() {
237182fbe45STzanio     CeedVectorDestroy(&u);
2387ca8db16Sjeremylt     CeedVectorDestroy(&v);
2397ca8db16Sjeremylt     CeedVectorDestroy(&rho);
2407ca8db16Sjeremylt     CeedVectorDestroy(&node_coords);
241182fbe45STzanio     CeedOperatorDestroy(&build_oper);
242182fbe45STzanio     CeedQFunctionDestroy(&build_qfunc);
2437ca8db16Sjeremylt     CeedOperatorDestroy(&oper);
2447ca8db16Sjeremylt     CeedQFunctionDestroy(&apply_qfunc);
2457ca8db16Sjeremylt     CeedBasisDestroy(&basis);
246182fbe45STzanio     CeedBasisDestroy(&mesh_basis);
247182fbe45STzanio     CeedElemRestrictionDestroy(&restr);
2487ca8db16Sjeremylt     CeedElemRestrictionDestroy(&mesh_restr);
249135a076eSjeremylt     CeedElemRestrictionDestroy(&restr_i);
250135a076eSjeremylt     CeedElemRestrictionDestroy(&mesh_restr_i);
251182fbe45STzanio   }
252182fbe45STzanio 
253182fbe45STzanio   /// Operator action
254182fbe45STzanio   virtual void Mult(const mfem::Vector &x, mfem::Vector &y) const {
255182fbe45STzanio     CeedVectorSetArray(u, CEED_MEM_HOST, CEED_USE_POINTER, x.GetData());
256182fbe45STzanio     CeedVectorSetArray(v, CEED_MEM_HOST, CEED_USE_POINTER, y.GetData());
25754251743Sjeremylt     CeedOperatorApply(oper, u, v, CEED_REQUEST_IMMEDIATE);
258182fbe45STzanio   }
259182fbe45STzanio };
260