MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
nurbs_ex11p.cpp
Go to the documentation of this file.
1 // MFEM Example 11 - Parallel NURBS Version
2 //
3 // Compile with: make nurbs_ex11p
4 //
5 // Sample runs: mpirun -np 4 nurbs_ex11p -m ../../data/square-disc.mesh
6 // mpirun -np 4 nurbs_ex11p -m ../../data/star.mesh
7 // mpirun -np 4 nurbs_ex11p -m ../../data/escher.mesh
8 // mpirun -np 4 nurbs_ex11p -m ../../data/fichera.mesh
9 // mpirun -np 4 nurbs_ex11p -m ../../data/square-disc-p2.vtk -o 2
10 // mpirun -np 4 nurbs_ex11p -m ../../data/square-disc-p3.mesh -o 3
11 // mpirun -np 4 nurbs_ex11p -m ../../data/square-disc-nurbs.mesh -o -1
12 // mpirun -np 4 nurbs_ex11p -m ../../data/disc-nurbs.mesh -o -1 -n 20
13 // mpirun -np 4 nurbs_ex11p -m ../../data/pipe-nurbs.mesh -o -1
14 // mpirun -np 4 nurbs_ex11p -m ../../data/ball-nurbs.mesh -o 2
15 // mpirun -np 4 nurbs_ex11p -m ../../data/star-surf.mesh
16 // mpirun -np 4 nurbs_ex11p -m ../../data/square-disc-surf.mesh
17 // mpirun -np 4 nurbs_ex11p -m ../../data/inline-segment.mesh
18 // mpirun -np 4 nurbs_ex11p -m ../../data/amr-quad.mesh
19 // mpirun -np 4 nurbs_ex11p -m ../../data/amr-hex.mesh
20 // mpirun -np 4 nurbs_ex11p -m ../../data/mobius-strip.mesh -n 8
21 // mpirun -np 4 nurbs_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->DisableMatching();
285  strumpack->SetOperator(*Arow);
286  strumpack->SetFromCommandLine();
287  precond = strumpack;
288  }
289 #endif
290  }
291 
292 
293  HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
294  lobpcg->SetNumModes(nev);
295  lobpcg->SetRandomSeed(seed);
296  lobpcg->SetPreconditioner(*precond);
297  lobpcg->SetMaxIter(200);
298  lobpcg->SetTol(1e-8);
299  lobpcg->SetPrecondUsageMode(1);
300  lobpcg->SetPrintLevel(1);
301  lobpcg->SetMassMatrix(*M);
302  lobpcg->SetOperator(*A);
303 
304  // 9. Compute the eigenmodes and extract the array of eigenvalues. Define a
305  // parallel grid function to represent each of the eigenmodes returned by
306  // the solver.
307  Array<double> eigenvalues;
308  lobpcg->Solve();
309  lobpcg->GetEigenvalues(eigenvalues);
310  ParGridFunction x(fespace);
311 
312  // 10. Save the refined mesh and the modes in parallel. This output can be
313  // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
314  {
315  ostringstream mesh_name, mode_name;
316  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
317 
318  ofstream mesh_ofs(mesh_name.str().c_str());
319  mesh_ofs.precision(8);
320  pmesh->Print(mesh_ofs);
321 
322  for (int i=0; i<nev; i++)
323  {
324  // convert eigenvector from HypreParVector to ParGridFunction
325  x = lobpcg->GetEigenvector(i);
326 
327  mode_name << "mode_" << setfill('0') << setw(2) << i << "."
328  << setfill('0') << setw(6) << myid;
329 
330  ofstream mode_ofs(mode_name.str().c_str());
331  mode_ofs.precision(8);
332  x.Save(mode_ofs);
333  mode_name.str("");
334  }
335  }
336 
337  // 11. Send the solution by socket to a GLVis server.
338  if (visualization)
339  {
340  char vishost[] = "localhost";
341  int visport = 19916;
342  socketstream mode_sock(vishost, visport);
343  mode_sock.precision(8);
344 
345  for (int i=0; i<nev; i++)
346  {
347  if ( myid == 0 )
348  {
349  cout << "Eigenmode " << i+1 << '/' << nev
350  << ", Lambda = " << eigenvalues[i] << endl;
351  }
352 
353  // convert eigenvector from HypreParVector to ParGridFunction
354  x = lobpcg->GetEigenvector(i);
355 
356  mode_sock << "parallel " << num_procs << " " << myid << "\n"
357  << "solution\n" << *pmesh << x << flush
358  << "window_title 'Eigenmode " << i+1 << '/' << nev
359  << ", Lambda = " << eigenvalues[i] << "'" << endl;
360 
361  char c;
362  if (myid == 0)
363  {
364  cout << "press (q)uit or (c)ontinue --> " << flush;
365  cin >> c;
366  }
367  MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
368 
369  if (c != 'c')
370  {
371  break;
372  }
373  }
374  mode_sock.close();
375  }
376 
377  // 12. Free the used memory.
378  delete lobpcg;
379  delete precond;
380  delete M;
381  delete A;
382 #if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
383  delete Arow;
384 #endif
385 
386  delete fespace;
387  if (own_fec)
388  {
389  delete fec;
390  }
391  delete pmesh;
392 
393  MPI_Finalize();
394 
395  return 0;
396 }
Arbitrary order non-uniform rational B-splines (NURBS) finite elements.
Definition: fe_coll.hpp:289
int Size() const
Logical size of the array.
Definition: array.hpp:124
HypreParVector & GetEigenvector(unsigned int i)
Extract a single eigenvector.
Definition: hypre.cpp:3763
Subclass constant coefficient.
Definition: coefficient.hpp:67
int GetNKV() const
Definition: nurbs.hpp:350
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:485
void SetPreconditioner(Solver &precond)
Definition: hypre.cpp:3686
void SetMassMatrix(Operator &M)
Definition: hypre.cpp:3741
Abstract parallel finite element space.
Definition: pfespace.hpp:28
void SetPrintLevel(int logging)
Definition: hypre.cpp:3671
int main(int argc, char *argv[])
Definition: ex1.cpp:62
void SetSymmetricPattern(bool sym)
Definition: superlu.cpp:404
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
void SetTol(double tol)
Definition: hypre.cpp:3649
void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: superlu.cpp:538
The BoomerAMG solver in hypre.
Definition: hypre.hpp:951
void SetPrintFactorStatistics(bool print_stat)
Definition: strumpack.cpp:127
void SetMaxIter(int max_iter)
Definition: hypre.cpp:3665
void SetColumnPermutation(superlu::ColPerm col_perm)
Definition: superlu.cpp:324
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:153
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:7982
void SetPrintLevel(int print_level)
Definition: hypre.hpp:992
T Max() const
Find the maximal element in the array, using the comparison operator &lt; for class T.
Definition: array.cpp:68
HYPRE_Int GlobalTrueVSize() const
Definition: pfespace.hpp:251
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:4102
void SetPrintSolveStatistics(bool print_stat)
Definition: strumpack.cpp:132
void SetPrintStatistics(bool print_stat)
Definition: superlu.cpp:306
int Dimension() const
Definition: mesh.hpp:744
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:189
void GetEigenvalues(Array< double > &eigenvalues)
Collect the converged eigenvalues.
Definition: hypre.cpp:3751
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:76
void SetSize(int nsize)
Change logical size of the array, keep existing entries.
Definition: array.hpp:635
void SetRandomSeed(int s)
Definition: hypre.hpp:1210
double a
Definition: lissajous.cpp:41
NURBSExtension * NURBSext
Optional NURBS mesh extension.
Definition: mesh.hpp:191
virtual const char * Name() const
Definition: fe_coll.hpp:53
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
int dim
Definition: ex24.cpp:43
void SetKrylovSolver(strumpack::KrylovSolver method)
Definition: strumpack.cpp:137
void SetOperator(Operator &A)
Definition: hypre.cpp:3695
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
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:221
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:6203
void SetPrecondUsageMode(int pcg_mode)
Definition: hypre.cpp:3680
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:83
Base class for solvers.
Definition: operator.hpp:500
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:187
void SetNumModes(int num_eigs)
Definition: hypre.hpp:1208
Class for parallel meshes.
Definition: pmesh.hpp:32
void Solve()
Solve the eigenproblem.
Definition: hypre.cpp:3808
bool Good() const
Definition: optparser.hpp:125