xref: /libCEED/examples/mfem/bp1.cpp (revision 9647a07ed4cccf5520ee61cfc49faed91542e405)
1182fbe45STzanio //                         libCEED + MFEM Example: BP1
2182fbe45STzanio //
3182fbe45STzanio // This example illustrates a simple usage of libCEED with the MFEM (mfem.org)
4182fbe45STzanio // finite element library.
5182fbe45STzanio //
6182fbe45STzanio // The example reads a mesh from a file and solves a simple linear system with a
7182fbe45STzanio // mass matrix (L2-projection of a given analytic function provided by
8182fbe45STzanio // 'solution'). The mass matrix required for performing the projection is
9182fbe45STzanio // expressed as a new class, CeedMassOperator, derived from mfem::Operator.
10182fbe45STzanio // Internally, CeedMassOperator uses a CeedOperator object constructed based on
11182fbe45STzanio // an mfem::FiniteElementSpace. All libCEED objects use a Ceed device object
12182fbe45STzanio // constructed based on a command line argument (-ceed).
13182fbe45STzanio //
14182fbe45STzanio // The mass matrix is inverted using a simple conjugate gradient algorithm
15182fbe45STzanio // corresponding to CEED BP1, see http://ceed.exascaleproject.org/bps. Arbitrary
16182fbe45STzanio // mesh and solution orders in 1D, 2D and 3D are supported from the same code.
17182fbe45STzanio //
18182fbe45STzanio // Build with:
19182fbe45STzanio //
20182fbe45STzanio //     make bp1 [MFEM_DIR=</path/to/mfem>] [CEED_DIR=</path/to/libceed>]
21182fbe45STzanio //
22182fbe45STzanio // Sample runs:
23182fbe45STzanio //
2466087c08SValeria Barra //     ./bp1
2566087c08SValeria Barra //     ./bp1 -ceed /cpu/self
2666087c08SValeria Barra //     ./bp1 -ceed /gpu/occa
2766087c08SValeria Barra //     ./bp1 -ceed /cpu/occa
2866087c08SValeria Barra //     ./bp1 -ceed /omp/occa
2966087c08SValeria Barra //     ./bp1 -ceed /ocl/occa
3066087c08SValeria Barra //     ./bp1 -m ../../../mfem/data/fichera.mesh
3166087c08SValeria Barra //     ./bp1 -m ../../../mfem/data/star.vtk -o 3
3266087c08SValeria Barra //     ./bp1 -m ../../../mfem/data/inline-segment.mesh -o 8
33182fbe45STzanio 
345d6bafb2Sjeremylt /// @file
355d6bafb2Sjeremylt /// MFEM mass operator based on libCEED
365d6bafb2Sjeremylt 
37182fbe45STzanio #include <ceed.h>
38182fbe45STzanio #include <mfem.hpp>
39c0c38e35SVeselin Dobrev #include "bp1.hpp"
40182fbe45STzanio 
41182fbe45STzanio /// Continuous function to project on the discrete FE space
42182fbe45STzanio double solution(const mfem::Vector &pt) {
43182fbe45STzanio   return pt.Norml2(); // distance to the origin
44182fbe45STzanio }
45182fbe45STzanio 
4643dae957SJeremy L Thompson //TESTARGS -ceed {ceed_resource} -t -no-vis --size 2000 --order 4
47182fbe45STzanio int main(int argc, char *argv[]) {
48182fbe45STzanio   // 1. Parse command-line options.
49182fbe45STzanio   const char *ceed_spec = "/cpu/self";
50c0c38e35SVeselin Dobrev   #ifndef MFEM_DIR
51182fbe45STzanio   const char *mesh_file = "../../../mfem/data/star.mesh";
52c0c38e35SVeselin Dobrev   #else
53c0c38e35SVeselin Dobrev   const char *mesh_file = MFEM_DIR "/data/star.mesh";
54c0c38e35SVeselin Dobrev   #endif
55182fbe45STzanio   int order = 1;
56182fbe45STzanio   bool visualization = true;
57dc00e230Sjeremylt   bool test = false;
58e2b2c771Svaleria   double max_nnodes = 50000;
59182fbe45STzanio 
60182fbe45STzanio   mfem::OptionsParser args(argc, argv);
61182fbe45STzanio   args.AddOption(&ceed_spec, "-c", "-ceed", "Ceed specification.");
62182fbe45STzanio   args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use.");
63182fbe45STzanio   args.AddOption(&order, "-o", "--order",
64182fbe45STzanio                  "Finite element order (polynomial degree).");
65e2b2c771Svaleria   args.AddOption(&max_nnodes, "-s", "--size", "Maximum size (number of DoFs)");
66182fbe45STzanio   args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
67182fbe45STzanio                  "--no-visualization",
68182fbe45STzanio                  "Enable or disable GLVis visualization.");
69dc00e230Sjeremylt   args.AddOption(&test, "-t", "--test", "-no-test",
70dc00e230Sjeremylt                  "--no-test",
71dc00e230Sjeremylt                  "Enable or disable test mode.");
72182fbe45STzanio   args.Parse();
73182fbe45STzanio   if (!args.Good()) {
74182fbe45STzanio     args.PrintUsage(std::cout);
75182fbe45STzanio     return 1;
76182fbe45STzanio   }
77dc00e230Sjeremylt   if (!test) {
78182fbe45STzanio     args.PrintOptions(std::cout);
79dc00e230Sjeremylt   }
80182fbe45STzanio 
81182fbe45STzanio   // 2. Initialize a Ceed device object using the given Ceed specification.
82182fbe45STzanio   Ceed ceed;
83182fbe45STzanio   CeedInit(ceed_spec, &ceed);
84182fbe45STzanio 
85182fbe45STzanio   // 3. Read the mesh from the given mesh file.
86182fbe45STzanio   mfem::Mesh *mesh = new mfem::Mesh(mesh_file, 1, 1);
87182fbe45STzanio   int dim = mesh->Dimension();
88182fbe45STzanio 
89182fbe45STzanio   // 4. Refine the mesh to increase the resolution. In this example we do
90182fbe45STzanio   //    'ref_levels' of uniform refinement. We choose 'ref_levels' to be the
91182fbe45STzanio   //    largest number that gives a final system with no more than 50,000
92182fbe45STzanio   //    unknowns, approximately.
93182fbe45STzanio   {
94182fbe45STzanio     int ref_levels =
95e2b2c771Svaleria       (int)floor((log(max_nnodes/mesh->GetNE())-dim*log(order))/log(2.)/dim);
96182fbe45STzanio     for (int l = 0; l < ref_levels; l++) {
97182fbe45STzanio       mesh->UniformRefinement();
98182fbe45STzanio     }
99182fbe45STzanio   }
100182fbe45STzanio   if (mesh->GetNodalFESpace() == NULL) {
101182fbe45STzanio     mesh->SetCurvature(1, false, -1, mfem::Ordering::byNODES);
102182fbe45STzanio   }
103182fbe45STzanio   if (mesh->NURBSext) {
104182fbe45STzanio     mesh->SetCurvature(order, false, -1, mfem::Ordering::byNODES);
105182fbe45STzanio   }
106182fbe45STzanio 
107182fbe45STzanio   // 5. Define a finite element space on the mesh. Here we use continuous
108182fbe45STzanio   //    Lagrange finite elements of the specified order.
109182fbe45STzanio   MFEM_VERIFY(order > 0, "invalid order");
110182fbe45STzanio   mfem::FiniteElementCollection *fec = new mfem::H1_FECollection(order, dim);
111182fbe45STzanio   mfem::FiniteElementSpace *fespace = new mfem::FiniteElementSpace(mesh, fec);
112dc00e230Sjeremylt   if (!test) {
113182fbe45STzanio     std::cout << "Number of finite element unknowns: "
114182fbe45STzanio               << fespace->GetTrueVSize() << std::endl;
115dc00e230Sjeremylt   }
116182fbe45STzanio 
117182fbe45STzanio   // 6. Construct a rhs vector using the linear form f(v) = (solution, v), where
118182fbe45STzanio   //    v is a test function.
119182fbe45STzanio   mfem::LinearForm b(fespace);
120182fbe45STzanio   mfem::FunctionCoefficient sol_coeff(solution);
121182fbe45STzanio   b.AddDomainIntegrator(new mfem::DomainLFIntegrator(sol_coeff));
122182fbe45STzanio   b.Assemble();
123182fbe45STzanio 
124182fbe45STzanio   // 7. Construct a CeedMassOperator utilizing the 'ceed' device and using the
125182fbe45STzanio   //    'fespace' object to extract data needed by the Ceed objects.
126182fbe45STzanio   CeedMassOperator mass(ceed, fespace);
127182fbe45STzanio 
128182fbe45STzanio   // 8. Solve the discrete system using the conjugate gradients (CG) method.
129182fbe45STzanio   mfem::CGSolver cg;
130182fbe45STzanio   cg.SetRelTol(1e-6);
131182fbe45STzanio   cg.SetMaxIter(100);
132dc00e230Sjeremylt   if (test) {
133dc00e230Sjeremylt     cg.SetPrintLevel(0);
134dc00e230Sjeremylt   } else {
135182fbe45STzanio     cg.SetPrintLevel(3);
136dc00e230Sjeremylt   }
137182fbe45STzanio   cg.SetOperator(mass);
138182fbe45STzanio 
139182fbe45STzanio   mfem::GridFunction sol(fespace);
140182fbe45STzanio   sol = 0.0;
141182fbe45STzanio   cg.Mult(b, sol);
142182fbe45STzanio 
143182fbe45STzanio   // 9. Compute and print the L2 projection error.
144*9647a07eSDavid Medina   double err_l2 = sol.ComputeL2Error(sol_coeff);
145dc00e230Sjeremylt   if (!test) {
146*9647a07eSDavid Medina     std::cout << "L2 projection error: " << err_l2
147182fbe45STzanio               << std::endl;
148dc00e230Sjeremylt   } else {
149f063656dSJed Brown     if (fabs(sol.ComputeL2Error(sol_coeff))>2e-4) {
150*9647a07eSDavid Medina       std::cout << "Error too large: " << err_l2 << std::endl;
151dc00e230Sjeremylt     }
1529b872752Sjeremylt   }
153182fbe45STzanio 
154182fbe45STzanio   // 10. Open a socket connection to GLVis and send the mesh and solution for
155182fbe45STzanio   //     visualization.
156182fbe45STzanio   if (visualization) {
157182fbe45STzanio     char vishost[] = "localhost";
158182fbe45STzanio     int  visport   = 19916;
159182fbe45STzanio     mfem::socketstream sol_sock(vishost, visport);
160182fbe45STzanio     sol_sock.precision(8);
161182fbe45STzanio     sol_sock << "solution\n" << *mesh << sol << std::flush;
162182fbe45STzanio   }
163182fbe45STzanio 
164182fbe45STzanio   // 11. Free memory and exit.
165182fbe45STzanio   delete fespace;
166182fbe45STzanio   delete fec;
167182fbe45STzanio   delete mesh;
168182fbe45STzanio   CeedDestroy(&ceed);
169182fbe45STzanio   return 0;
170182fbe45STzanio }
171