MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ex11p.cpp
Go to the documentation of this file.
1 // MFEM Example 11 - Parallel Version
2 //
3 // Compile with: make ex11p
4 //
5 // Sample runs: mpirun -np 4 ex11p -m ../data/square-disc.mesh
6 // mpirun -np 4 ex11p -m ../data/star.mesh
7 // mpirun -np 4 ex11p -m ../data/star-mixed.mesh
8 // mpirun -np 4 ex11p -m ../data/escher.mesh
9 // mpirun -np 4 ex11p -m ../data/fichera.mesh
10 // mpirun -np 4 ex11p -m ../data/fichera-mixed.mesh
11 // mpirun -np 4 ex11p -m ../data/periodic-annulus-sector.msh
12 // mpirun -np 4 ex11p -m ../data/periodic-torus-sector.msh -rs 1
13 // mpirun -np 4 ex11p -m ../data/toroid-wedge.mesh -o 2
14 // mpirun -np 4 ex11p -m ../data/square-disc-p2.vtk -o 2
15 // mpirun -np 4 ex11p -m ../data/square-disc-p3.mesh -o 3
16 // mpirun -np 4 ex11p -m ../data/square-disc-nurbs.mesh -o -1
17 // mpirun -np 4 ex11p -m ../data/disc-nurbs.mesh -o -1 -n 20
18 // mpirun -np 4 ex11p -m ../data/pipe-nurbs.mesh -o -1
19 // mpirun -np 4 ex11p -m ../data/ball-nurbs.mesh -o 2
20 // mpirun -np 4 ex11p -m ../data/star-surf.mesh
21 // mpirun -np 4 ex11p -m ../data/square-disc-surf.mesh
22 // mpirun -np 4 ex11p -m ../data/inline-segment.mesh
23 // mpirun -np 4 ex11p -m ../data/inline-quad.mesh
24 // mpirun -np 4 ex11p -m ../data/inline-tri.mesh
25 // mpirun -np 4 ex11p -m ../data/inline-hex.mesh
26 // mpirun -np 4 ex11p -m ../data/inline-tet.mesh
27 // mpirun -np 4 ex11p -m ../data/inline-wedge.mesh -s 83
28 // mpirun -np 4 ex11p -m ../data/amr-quad.mesh
29 // mpirun -np 4 ex11p -m ../data/amr-hex.mesh
30 // mpirun -np 4 ex11p -m ../data/mobius-strip.mesh -n 8
31 // mpirun -np 4 ex11p -m ../data/klein-bottle.mesh -n 10
32 //
33 // Description: This example code demonstrates the use of MFEM to solve the
34 // eigenvalue problem -Delta u = lambda u with homogeneous
35 // Dirichlet boundary conditions.
36 //
37 // We compute a number of the lowest eigenmodes by discretizing
38 // the Laplacian and Mass operators using a FE space of the
39 // specified order, or an isoparametric/isogeometric space if
40 // order < 1 (quadratic for quadratic curvilinear mesh, NURBS for
41 // NURBS mesh, etc.)
42 //
43 // The example highlights the use of the LOBPCG eigenvalue solver
44 // together with the BoomerAMG preconditioner in HYPRE, as well as
45 // optionally the SuperLU or STRUMPACK parallel direct solvers.
46 // Reusing a single GLVis visualization window for multiple
47 // eigenfunctions is also illustrated.
48 //
49 // We recommend viewing Example 1 before viewing this example.
50 
51 #include "mfem.hpp"
52 #include <fstream>
53 #include <iostream>
54 
55 using namespace std;
56 using namespace mfem;
57 
58 int main(int argc, char *argv[])
59 {
60  // 1. Initialize MPI and HYPRE.
61  Mpi::Init(argc, argv);
62  int num_procs = Mpi::WorldSize();
63  int myid = Mpi::WorldRank();
64  Hypre::Init();
65 
66  // 2. Parse command-line options.
67  const char *mesh_file = "../data/star.mesh";
68  int ser_ref_levels = 2;
69  int par_ref_levels = 1;
70  int order = 1;
71  int nev = 5;
72  int seed = 75;
73  bool slu_solver = false;
74  bool sp_solver = false;
75  bool cpardiso_solver = false;
76  bool visualization = 1;
77 
78  OptionsParser args(argc, argv);
79  args.AddOption(&mesh_file, "-m", "--mesh",
80  "Mesh file to use.");
81  args.AddOption(&ser_ref_levels, "-rs", "--refine-serial",
82  "Number of times to refine the mesh uniformly in serial.");
83  args.AddOption(&par_ref_levels, "-rp", "--refine-parallel",
84  "Number of times to refine the mesh uniformly in parallel.");
85  args.AddOption(&order, "-o", "--order",
86  "Finite element order (polynomial degree) or -1 for"
87  " isoparametric space.");
88  args.AddOption(&nev, "-n", "--num-eigs",
89  "Number of desired eigenmodes.");
90  args.AddOption(&seed, "-s", "--seed",
91  "Random seed used to initialize LOBPCG.");
92 #ifdef MFEM_USE_SUPERLU
93  args.AddOption(&slu_solver, "-slu", "--superlu", "-no-slu",
94  "--no-superlu", "Use the SuperLU Solver.");
95 #endif
96 #ifdef MFEM_USE_STRUMPACK
97  args.AddOption(&sp_solver, "-sp", "--strumpack", "-no-sp",
98  "--no-strumpack", "Use the STRUMPACK Solver.");
99 #endif
100 #ifdef MFEM_USE_MKL_CPARDISO
101  args.AddOption(&cpardiso_solver, "-cpardiso", "--cpardiso", "-no-cpardiso",
102  "--no-cpardiso", "Use the MKL CPardiso Solver.");
103 #endif
104  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
105  "--no-visualization",
106  "Enable or disable GLVis visualization.");
107  args.Parse();
108  if (slu_solver && sp_solver)
109  {
110  if (myid == 0)
111  cout << "WARNING: Both SuperLU and STRUMPACK have been selected,"
112  << " please choose either one." << endl
113  << " Defaulting to SuperLU." << endl;
114  sp_solver = false;
115  }
116  // The command line options are also passed to the STRUMPACK
117  // solver. So do not exit if some options are not recognized.
118  if (!sp_solver)
119  {
120  if (!args.Good())
121  {
122  if (myid == 0)
123  {
124  args.PrintUsage(cout);
125  }
126  return 1;
127  }
128  }
129  if (myid == 0)
130  {
131  args.PrintOptions(cout);
132  }
133 
134  // 3. Read the (serial) mesh from the given mesh file on all processors. We
135  // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
136  // and volume meshes with the same code.
137  Mesh *mesh = new Mesh(mesh_file, 1, 1);
138  int dim = mesh->Dimension();
139 
140  // 4. Refine the serial mesh on all processors to increase the resolution. In
141  // this example we do 'ref_levels' of uniform refinement (2 by default, or
142  // specified on the command line with -rs).
143  for (int lev = 0; lev < ser_ref_levels; lev++)
144  {
145  mesh->UniformRefinement();
146  }
147 
148  // 5. Define a parallel mesh by a partitioning of the serial mesh. Refine
149  // this mesh further in parallel to increase the resolution (1 time by
150  // default, or specified on the command line with -rp). Once the parallel
151  // mesh is defined, the serial mesh can be deleted.
152  ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
153  delete mesh;
154  for (int lev = 0; lev < par_ref_levels; lev++)
155  {
156  pmesh->UniformRefinement();
157  }
158 
159  // 6. Define a parallel finite element space on the parallel mesh. Here we
160  // use continuous Lagrange finite elements of the specified order. If
161  // order < 1, we instead use an isoparametric/isogeometric space.
163  if (order > 0)
164  {
165  fec = new H1_FECollection(order, dim);
166  }
167  else if (pmesh->GetNodes())
168  {
169  fec = pmesh->GetNodes()->OwnFEC();
170  }
171  else
172  {
173  fec = new H1_FECollection(order = 1, dim);
174  }
175  ParFiniteElementSpace *fespace = new ParFiniteElementSpace(pmesh, fec);
176  HYPRE_BigInt size = fespace->GlobalTrueVSize();
177  if (myid == 0)
178  {
179  cout << "Number of unknowns: " << size << endl;
180  }
181 
182  // 7. Set up the parallel bilinear forms a(.,.) and m(.,.) on the finite
183  // element space. The first corresponds to the Laplacian operator -Delta,
184  // while the second is a simple mass matrix needed on the right hand side
185  // of the generalized eigenvalue problem below. The boundary conditions
186  // are implemented by elimination with special values on the diagonal to
187  // shift the Dirichlet eigenvalues out of the computational range. After
188  // serial and parallel assembly we extract the corresponding parallel
189  // matrices A and M.
190  ConstantCoefficient one(1.0);
191  Array<int> ess_bdr;
192  if (pmesh->bdr_attributes.Size())
193  {
194  ess_bdr.SetSize(pmesh->bdr_attributes.Max());
195  ess_bdr = 1;
196  }
197 
198  ParBilinearForm *a = new ParBilinearForm(fespace);
200  if (pmesh->bdr_attributes.Size() == 0)
201  {
202  // Add a mass term if the mesh has no boundary, e.g. periodic mesh or
203  // closed surface.
204  a->AddDomainIntegrator(new MassIntegrator(one));
205  }
206  a->Assemble();
207  a->EliminateEssentialBCDiag(ess_bdr, 1.0);
208  a->Finalize();
209 
210  ParBilinearForm *m = new ParBilinearForm(fespace);
211  m->AddDomainIntegrator(new MassIntegrator(one));
212  m->Assemble();
213  // shift the eigenvalue corresponding to eliminated dofs to a large value
214  m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<double>::min());
215  m->Finalize();
216 
219 
220 #if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
221  Operator * Arow = NULL;
222 #ifdef MFEM_USE_SUPERLU
223  if (slu_solver)
224  {
225  Arow = new SuperLURowLocMatrix(*A);
226  }
227 #endif
228 #ifdef MFEM_USE_STRUMPACK
229  if (sp_solver)
230  {
231  Arow = new STRUMPACKRowLocMatrix(*A);
232  }
233 #endif
234 #endif
235 
236  delete a;
237  delete m;
238 
239  // 8. Define and configure the LOBPCG eigensolver and the BoomerAMG
240  // preconditioner for A to be used within the solver. Set the matrices
241  // which define the generalized eigenproblem A x = lambda M x.
242  Solver * precond = NULL;
243  if (!slu_solver && !sp_solver && !cpardiso_solver)
244  {
245  HypreBoomerAMG * amg = new HypreBoomerAMG(*A);
246  amg->SetPrintLevel(0);
247  precond = amg;
248  }
249  else
250  {
251 #ifdef MFEM_USE_SUPERLU
252  if (slu_solver)
253  {
254  SuperLUSolver * superlu = new SuperLUSolver(MPI_COMM_WORLD);
255  superlu->SetPrintStatistics(false);
256  superlu->SetSymmetricPattern(true);
258  superlu->SetOperator(*Arow);
259  precond = superlu;
260  }
261 #endif
262 #ifdef MFEM_USE_STRUMPACK
263  if (sp_solver)
264  {
265  STRUMPACKSolver * strumpack = new STRUMPACKSolver(argc, argv, MPI_COMM_WORLD);
266  strumpack->SetPrintFactorStatistics(true);
267  strumpack->SetPrintSolveStatistics(false);
268  strumpack->SetKrylovSolver(strumpack::KrylovSolver::DIRECT);
269  strumpack->SetReorderingStrategy(strumpack::ReorderingStrategy::METIS);
270  strumpack->DisableMatching();
271  strumpack->SetOperator(*Arow);
272  strumpack->SetFromCommandLine();
273  precond = strumpack;
274  }
275 #endif
276 #ifdef MFEM_USE_MKL_CPARDISO
277  if (cpardiso_solver)
278  {
279  auto cpardiso = new CPardisoSolver(A->GetComm());
280  cpardiso->SetMatrixType(CPardisoSolver::MatType::REAL_STRUCTURE_SYMMETRIC);
281  cpardiso->SetPrintLevel(1);
282  cpardiso->SetOperator(*A);
283  precond = cpardiso;
284  }
285 #endif
286  }
287 
288  HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
289  lobpcg->SetNumModes(nev);
290  lobpcg->SetRandomSeed(seed);
291  lobpcg->SetPreconditioner(*precond);
292  lobpcg->SetMaxIter(200);
293  lobpcg->SetTol(1e-8);
294  lobpcg->SetPrecondUsageMode(1);
295  lobpcg->SetPrintLevel(1);
296  lobpcg->SetMassMatrix(*M);
297  lobpcg->SetOperator(*A);
298 
299  // 9. Compute the eigenmodes and extract the array of eigenvalues. Define a
300  // parallel grid function to represent each of the eigenmodes returned by
301  // the solver.
302  Array<double> eigenvalues;
303  lobpcg->Solve();
304  lobpcg->GetEigenvalues(eigenvalues);
305  ParGridFunction x(fespace);
306 
307  // 10. Save the refined mesh and the modes in parallel. This output can be
308  // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
309  {
310  ostringstream mesh_name, mode_name;
311  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
312 
313  ofstream mesh_ofs(mesh_name.str().c_str());
314  mesh_ofs.precision(8);
315  pmesh->Print(mesh_ofs);
316 
317  for (int i=0; i<nev; i++)
318  {
319  // convert eigenvector from HypreParVector to ParGridFunction
320  x = lobpcg->GetEigenvector(i);
321 
322  mode_name << "mode_" << setfill('0') << setw(2) << i << "."
323  << setfill('0') << setw(6) << myid;
324 
325  ofstream mode_ofs(mode_name.str().c_str());
326  mode_ofs.precision(8);
327  x.Save(mode_ofs);
328  mode_name.str("");
329  }
330  }
331 
332  // 11. Send the solution by socket to a GLVis server.
333  if (visualization)
334  {
335  char vishost[] = "localhost";
336  int visport = 19916;
337  socketstream mode_sock(vishost, visport);
338  mode_sock.precision(8);
339 
340  for (int i=0; i<nev; i++)
341  {
342  if ( myid == 0 )
343  {
344  cout << "Eigenmode " << i+1 << '/' << nev
345  << ", Lambda = " << eigenvalues[i] << endl;
346  }
347 
348  // convert eigenvector from HypreParVector to ParGridFunction
349  x = lobpcg->GetEigenvector(i);
350 
351  mode_sock << "parallel " << num_procs << " " << myid << "\n"
352  << "solution\n" << *pmesh << x << flush
353  << "window_title 'Eigenmode " << i+1 << '/' << nev
354  << ", Lambda = " << eigenvalues[i] << "'" << endl;
355 
356  char c;
357  if (myid == 0)
358  {
359  cout << "press (q)uit or (c)ontinue --> " << flush;
360  cin >> c;
361  }
362  MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
363 
364  if (c != 'c')
365  {
366  break;
367  }
368  }
369  mode_sock.close();
370  }
371 
372  // 12. Free the used memory.
373  delete lobpcg;
374  delete precond;
375  delete M;
376  delete A;
377 #if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
378  delete Arow;
379 #endif
380 
381  delete fespace;
382  if (order > 0)
383  {
384  delete fec;
385  }
386  delete pmesh;
387 
388  return 0;
389 }
int Size() const
Return the logical size of the array.
Definition: array.hpp:138
MPI_Comm GetComm() const
MPI communicator.
Definition: hypre.hpp:534
A coefficient that is constant across space and time.
Definition: coefficient.hpp:84
HYPRE_BigInt GlobalTrueVSize() const
Definition: pfespace.hpp:285
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:873
void SetPreconditioner(Solver &precond)
Definition: hypre.cpp:6163
void SetMassMatrix(Operator &M)
Definition: hypre.cpp:6219
Abstract parallel finite element space.
Definition: pfespace.hpp:28
void SetPrintLevel(int logging)
Definition: hypre.cpp:6148
void SetSymmetricPattern(bool sym)
Definition: superlu.cpp:425
void GetEigenvalues(Array< double > &eigenvalues) const
Collect the converged eigenvalues.
Definition: hypre.cpp:6229
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
void SetTol(double tol)
Definition: hypre.cpp:6126
int close()
Close the socketstream.
void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: superlu.cpp:591
The BoomerAMG solver in hypre.
Definition: hypre.hpp:1579
void SetPrintFactorStatistics(bool print_stat)
Definition: strumpack.cpp:136
void SetMaxIter(int max_iter)
Definition: hypre.cpp:6142
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:151
constexpr char vishost[]
void SetColumnPermutation(superlu::ColPerm col_perm)
Definition: superlu.cpp:345
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:9816
void SetPrintLevel(int print_level)
Definition: hypre.hpp:1659
constexpr int visport
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:68
void Assemble(int skip_zeros=1)
Assemble the local matrix.
void EliminateEssentialBCDiag(const Array< int > &bdr_attr_is_ess, double value)
Perform elimination and set the diagonal entry to the given value.
void SetPrintSolveStatistics(bool print_stat)
Definition: strumpack.cpp:141
void SetPrintStatistics(bool print_stat)
Definition: superlu.cpp:327
int Dimension() const
Definition: mesh.hpp:1006
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:454
void SetReorderingStrategy(strumpack::ReorderingStrategy method)
Definition: strumpack.cpp:151
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:270
Collection of finite elements from the same family in multiple dimensions. This class is used to matc...
Definition: fe_coll.hpp:26
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set &#39;var&#39; to receive the value. Enable/disable tags are used to set the bool...
Definition: optparser.hpp:82
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
Definition: array.hpp:679
HYPRE_Int HYPRE_BigInt
void SetRandomSeed(int s)
Definition: hypre.hpp:1993
double a
Definition: lissajous.cpp:41
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
int dim
Definition: ex24.cpp:53
void SetKrylovSolver(strumpack::KrylovSolver method)
Definition: strumpack.cpp:146
void SetOperator(Operator &A)
Definition: hypre.cpp:6172
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:324
Class for parallel bilinear form.
const HypreParVector & GetEigenvector(unsigned int i) const
Extract a single eigenvector.
Definition: hypre.cpp:6241
void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: strumpack.cpp:230
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:7908
void SetPrecondUsageMode(int pcg_mode)
Definition: hypre.cpp:6157
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:220
void SetMatrixType(MatType mat_type)
Set the matrix type.
Definition: cpardiso.cpp:192
void Print(std::ostream &out=mfem::out) const override
Definition: pmesh.cpp:4770
Base class for solvers.
Definition: operator.hpp:655
Class for parallel grid function.
Definition: pgridfunc.hpp:32
Abstract operator.
Definition: operator.hpp:24
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:343
void SetNumModes(int num_eigs)
Definition: hypre.hpp:1991
Class for parallel meshes.
Definition: pmesh.hpp:32
void Solve()
Solve the eigenproblem.
Definition: hypre.cpp:6286
MKL Parallel Direct Sparse Solver for Clusters.
Definition: cpardiso.hpp:30
int main()
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150