MFEM  v4.6.0
Finite element discretization library
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 and HYPRE.
51  Mpi::Init(argc, argv);
52  int num_procs = Mpi::WorldSize();
53  int myid = Mpi::WorldRank();
54  Hypre::Init();
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  return 1;
113  }
114  }
115  if (myid == 0)
116  {
117  args.PrintOptions(cout);
118  }
119 
120  // 3. Read the (serial) mesh from the given mesh file on all processors. We
121  // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
122  // and volume meshes with the same code.
123  Mesh *mesh = new Mesh(mesh_file, 1, 1);
124  int dim = mesh->Dimension();
125 
126  // 4. Refine the serial mesh on all processors to increase the resolution. In
127  // this example we do 'ref_levels' of uniform refinement (2 by default, or
128  // specified on the command line with -rs).
129  for (int lev = 0; lev < ser_ref_levels; lev++)
130  {
131  mesh->UniformRefinement();
132  }
133 
134  // 5. Define a parallel mesh by a partitioning of the serial mesh. Refine
135  // this mesh further in parallel to increase the resolution (1 time by
136  // default, or specified on the command line with -rp). Once the parallel
137  // mesh is defined, the serial mesh can be deleted.
138  ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
139  delete mesh;
140  for (int lev = 0; lev < par_ref_levels; lev++)
141  {
142  pmesh->UniformRefinement();
143  }
144 
145  // 6. Define a parallel finite element space on the parallel mesh. Here we
146  // use continuous Lagrange finite elements of the specified order. If
147  // order < 1, we instead use an isoparametric/isogeometric space.
149  NURBSExtension *NURBSext = NULL;
150  int own_fec = 0;
151 
152  if (order[0] == 0) // Isoparametric
153  {
154  if (pmesh->GetNodes())
155  {
156  fec = pmesh->GetNodes()->OwnFEC();
157  own_fec = 0;
158  if (myid == 0)
159  {
160  cout << "Using isoparametric FEs: " << fec->Name() << endl;
161  }
162  }
163  else
164  {
165  if (myid == 0)
166  {
167  cout <<"Mesh does not have FEs --> Assume order 1.\n";
168  }
169  fec = new H1_FECollection(1, dim);
170  own_fec = 1;
171  }
172  }
173  else if (pmesh->NURBSext && (order[0] > 0) ) // Subparametric NURBS
174  {
175  fec = new NURBSFECollection(order[0]);
176  own_fec = 1;
177  int nkv = pmesh->NURBSext->GetNKV();
178 
179  if (order.Size() == 1)
180  {
181  int tmp = order[0];
182  order.SetSize(nkv);
183  order = tmp;
184  }
185  if (order.Size() != nkv ) { mfem_error("Wrong number of orders set."); }
186  NURBSext = new NURBSExtension(pmesh->NURBSext, order);
187  }
188  else
189  {
190  if (order.Size() > 1) { cout <<"Wrong number of orders set, needs one.\n"; }
191  fec = new H1_FECollection(abs(order[0]), dim);
192  own_fec = 1;
193  }
194  ParFiniteElementSpace *fespace = new ParFiniteElementSpace(pmesh,NURBSext,fec);
195  HYPRE_BigInt size = fespace->GlobalTrueVSize();
196  if (myid == 0)
197  {
198  cout << "Number of unknowns: " << size << endl;
199  }
200 
201  // 7. Set up the parallel bilinear forms a(.,.) and m(.,.) on the finite
202  // element space. The first corresponds to the Laplacian operator -Delta,
203  // while the second is a simple mass matrix needed on the right hand side
204  // of the generalized eigenvalue problem below. The boundary conditions
205  // are implemented by elimination with special values on the diagonal to
206  // shift the Dirichlet eigenvalues out of the computational range. After
207  // serial and parallel assembly we extract the corresponding parallel
208  // matrices A and M.
209  ConstantCoefficient one(1.0);
210  Array<int> ess_bdr;
211  if (pmesh->bdr_attributes.Size())
212  {
213  ess_bdr.SetSize(pmesh->bdr_attributes.Max());
214  ess_bdr = 1;
215  }
216 
217  ParBilinearForm *a = new ParBilinearForm(fespace);
218  a->AddDomainIntegrator(new DiffusionIntegrator(one));
219  if (pmesh->bdr_attributes.Size() == 0)
220  {
221  // Add a mass term if the mesh has no boundary, e.g. periodic mesh or
222  // closed surface.
223  a->AddDomainIntegrator(new MassIntegrator(one));
224  }
225  a->Assemble();
226  a->EliminateEssentialBCDiag(ess_bdr, 1.0);
227  a->Finalize();
228 
229  ParBilinearForm *m = new ParBilinearForm(fespace);
230  m->AddDomainIntegrator(new MassIntegrator(one));
231  m->Assemble();
232  // shift the eigenvalue corresponding to eliminated dofs to a large value
233  m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<double>::min());
234  m->Finalize();
235 
236  HypreParMatrix *A = a->ParallelAssemble();
238 
239 #if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
240  Operator * Arow = NULL;
241 #ifdef MFEM_USE_SUPERLU
242  if (slu_solver)
243  {
244  Arow = new SuperLURowLocMatrix(*A);
245  }
246 #endif
247 #ifdef MFEM_USE_STRUMPACK
248  if (sp_solver)
249  {
250  Arow = new STRUMPACKRowLocMatrix(*A);
251  }
252 #endif
253 #endif
254 
255  delete a;
256  delete m;
257 
258  // 8. Define and configure the LOBPCG eigensolver and the BoomerAMG
259  // preconditioner for A to be used within the solver. Set the matrices
260  // which define the generalized eigenproblem A x = lambda M x.
261  Solver * precond = NULL;
262  if (!slu_solver && !sp_solver)
263  {
264  HypreBoomerAMG * amg = new HypreBoomerAMG(*A);
265  amg->SetPrintLevel(0);
266  precond = amg;
267  }
268  else
269  {
270 #ifdef MFEM_USE_SUPERLU
271  if (slu_solver)
272  {
273  SuperLUSolver * superlu = new SuperLUSolver(MPI_COMM_WORLD);
274  superlu->SetPrintStatistics(false);
275  superlu->SetSymmetricPattern(true);
277  superlu->SetOperator(*Arow);
278  precond = superlu;
279  }
280 #endif
281 #ifdef MFEM_USE_STRUMPACK
282  if (sp_solver)
283  {
284  STRUMPACKSolver * strumpack = new STRUMPACKSolver(argc, argv, MPI_COMM_WORLD);
285  strumpack->SetPrintFactorStatistics(true);
286  strumpack->SetPrintSolveStatistics(false);
287  strumpack->SetKrylovSolver(strumpack::KrylovSolver::DIRECT);
288  strumpack->SetReorderingStrategy(strumpack::ReorderingStrategy::METIS);
289  strumpack->DisableMatching();
290  strumpack->SetOperator(*Arow);
291  strumpack->SetFromCommandLine();
292  precond = strumpack;
293  }
294 #endif
295  }
296 
297 
298  HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
299  lobpcg->SetNumModes(nev);
300  lobpcg->SetRandomSeed(seed);
301  lobpcg->SetPreconditioner(*precond);
302  lobpcg->SetMaxIter(200);
303  lobpcg->SetTol(1e-8);
304  lobpcg->SetPrecondUsageMode(1);
305  lobpcg->SetPrintLevel(1);
306  lobpcg->SetMassMatrix(*M);
307  lobpcg->SetOperator(*A);
308 
309  // 9. Compute the eigenmodes and extract the array of eigenvalues. Define a
310  // parallel grid function to represent each of the eigenmodes returned by
311  // the solver.
312  Array<double> eigenvalues;
313  lobpcg->Solve();
314  lobpcg->GetEigenvalues(eigenvalues);
315  ParGridFunction x(fespace);
316 
317  // 10. Save the refined mesh and the modes in parallel. This output can be
318  // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
319  {
320  ostringstream mesh_name, mode_name;
321  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
322 
323  ofstream mesh_ofs(mesh_name.str().c_str());
324  mesh_ofs.precision(8);
325  pmesh->Print(mesh_ofs);
326 
327  for (int i=0; i<nev; i++)
328  {
329  // convert eigenvector from HypreParVector to ParGridFunction
330  x = lobpcg->GetEigenvector(i);
331 
332  mode_name << "mode_" << setfill('0') << setw(2) << i << "."
333  << setfill('0') << setw(6) << myid;
334 
335  ofstream mode_ofs(mode_name.str().c_str());
336  mode_ofs.precision(8);
337  x.Save(mode_ofs);
338  mode_name.str("");
339  }
340  }
341 
342  // 11. Send the solution by socket to a GLVis server.
343  if (visualization)
344  {
345  char vishost[] = "localhost";
346  int visport = 19916;
347  socketstream mode_sock(vishost, visport);
348  mode_sock.precision(8);
349 
350  for (int i=0; i<nev; i++)
351  {
352  if ( myid == 0 )
353  {
354  cout << "Eigenmode " << i+1 << '/' << nev
355  << ", Lambda = " << eigenvalues[i] << endl;
356  }
357 
358  // convert eigenvector from HypreParVector to ParGridFunction
359  x = lobpcg->GetEigenvector(i);
360 
361  mode_sock << "parallel " << num_procs << " " << myid << "\n"
362  << "solution\n" << *pmesh << x << flush
363  << "window_title 'Eigenmode " << i+1 << '/' << nev
364  << ", Lambda = " << eigenvalues[i] << "'" << endl;
365 
366  char c;
367  if (myid == 0)
368  {
369  cout << "press (q)uit or (c)ontinue --> " << flush;
370  cin >> c;
371  }
372  MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
373 
374  if (c != 'c')
375  {
376  break;
377  }
378  }
379  mode_sock.close();
380  }
381 
382  // 12. Free the used memory.
383  delete lobpcg;
384  delete precond;
385  delete M;
386  delete A;
387 #if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
388  delete Arow;
389 #endif
390 
391  delete fespace;
392  if (own_fec)
393  {
394  delete fec;
395  }
396  delete pmesh;
397 
398  return 0;
399 }
Arbitrary order non-uniform rational B-splines (NURBS) finite elements.
Definition: fe_coll.hpp:649
void GetEigenvalues(Array< double > &eigenvalues) const
Collect the converged eigenvalues.
Definition: hypre.cpp:6202
int visport
A coefficient that is constant across space and time.
Definition: coefficient.hpp:84
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:331
int Dimension() const
Dimension of the reference space used within the elements.
Definition: mesh.hpp:1020
const HypreParVector & GetEigenvector(unsigned int i) const
Extract a single eigenvector.
Definition: hypre.cpp:6214
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:462
int main(int argc, char *argv[])
Definition: nurbs_ex11p.cpp:48
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition: array.cpp:68
void SetPreconditioner(Solver &precond)
Definition: hypre.cpp:6136
void SetMassMatrix(Operator &M)
Definition: hypre.cpp:6192
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:159
Abstract parallel finite element space.
Definition: pfespace.hpp:28
void SetPrintLevel(int logging)
Definition: hypre.cpp:6121
STL namespace.
void SetSymmetricPattern(bool sym)
Definition: superlu.cpp:454
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
void SetTol(double tol)
Definition: hypre.cpp:6099
int close()
Close the socketstream.
void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: superlu.cpp:475
The BoomerAMG solver in hypre.
Definition: hypre.hpp:1590
void SetMaxIter(int max_iter)
Definition: hypre.cpp:6115
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:151
char vishost[]
void SetColumnPermutation(superlu::ColPerm col_perm)
Definition: superlu.cpp:399
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:154
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:10232
void SetPrintLevel(int print_level)
Definition: hypre.hpp:1670
void Assemble(int skip_zeros=1)
Assemble the local matrix.
virtual const char * Name() const
Definition: fe_coll.hpp:80
void EliminateEssentialBCDiag(const Array< int > &bdr_attr_is_ess, double value)
Perform elimination and set the diagonal entry to the given value.
void SetPrintStatistics(bool print_stat)
Definition: superlu.cpp:385
HYPRE_BigInt GlobalTrueVSize() const
Definition: pfespace.hpp:281
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:275
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:687
HYPRE_Int HYPRE_BigInt
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:909
void SetRandomSeed(int s)
Definition: hypre.hpp:2004
double a
Definition: lissajous.cpp:41
NURBSExtension * NURBSext
Optional NURBS mesh extension.
Definition: mesh.hpp:277
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
int dim
Definition: ex24.cpp:53
void SetOperator(Operator &A)
Definition: hypre.cpp:6145
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
int GetNKV() const
Definition: nurbs.hpp:415
Class for parallel bilinear form.
int Size() const
Return the logical size of the array.
Definition: array.hpp:141
void SetPrecondUsageMode(int pcg_mode)
Definition: hypre.cpp:6130
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:259
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:8302
void Print(std::ostream &out=mfem::out) const override
Definition: pmesh.cpp:4825
Base class for solvers.
Definition: operator.hpp:682
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:2002
Class for parallel meshes.
Definition: pmesh.hpp:32
void Solve()
Solve the eigenproblem.
Definition: hypre.cpp:6259