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