MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ex12p.cpp
Go to the documentation of this file.
1 // MFEM Example 12 - Parallel Version
2 //
3 // Compile with: make ex12p
4 //
5 // Sample runs:
6 // mpirun -np 4 ex12p -m ../data/beam-tri.mesh
7 // mpirun -np 4 ex12p -m ../data/beam-quad.mesh
8 // mpirun -np 4 ex12p -m ../data/beam-tet.mesh -s 462 -n 10 -o 2 -elast
9 // mpirun -np 4 ex12p -m ../data/beam-hex.mesh -s 3878
10 // mpirun -np 4 ex12p -m ../data/beam-wedge.mesh -s 81
11 // mpirun -np 4 ex12p -m ../data/beam-tri.mesh -s 3877 -o 2 -sys
12 // mpirun -np 4 ex12p -m ../data/beam-quad.mesh -s 4544 -n 6 -o 3 -elast
13 // mpirun -np 4 ex12p -m ../data/beam-quad-nurbs.mesh
14 // mpirun -np 4 ex12p -m ../data/beam-hex-nurbs.mesh
15 //
16 // Description: This example code solves the linear elasticity eigenvalue
17 // problem for a multi-material cantilever beam.
18 //
19 // Specifically, we compute a number of the lowest eigenmodes by
20 // approximating the weak form of -div(sigma(u)) = lambda u where
21 // sigma(u)=lambda*div(u)*I+mu*(grad*u+u*grad) is the stress
22 // tensor corresponding to displacement field u, and lambda and mu
23 // are the material Lame constants. The boundary conditions are
24 // u=0 on the fixed part of the boundary with attribute 1, and
25 // sigma(u).n=f on the remainder. The geometry of the domain is
26 // assumed to be as follows:
27 //
28 // +----------+----------+
29 // boundary --->| material | material |
30 // attribute 1 | 1 | 2 |
31 // (fixed) +----------+----------+
32 //
33 // The example highlights the use of the LOBPCG eigenvalue solver
34 // together with the BoomerAMG preconditioner in HYPRE. Reusing a
35 // single GLVis visualization window for multiple eigenfunctions
36 // and optional saving with ADIOS2 (adios2.readthedocs.io) streams
37 // are also illustrated.
38 //
39 // We recommend viewing examples 2 and 11 before viewing this
40 // example.
41 
42 #include "mfem.hpp"
43 #include <fstream>
44 #include <iostream>
45 
46 using namespace std;
47 using namespace mfem;
48 
49 int main(int argc, char *argv[])
50 {
51  // 1. Initialize MPI.
52  int num_procs, myid;
53  MPI_Init(&argc, &argv);
54  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
55  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
56 
57  // 2. Parse command-line options.
58  const char *mesh_file = "../data/beam-tri.mesh";
59  int order = 1;
60  int nev = 5;
61  int seed = 66;
62  bool visualization = 1;
63  bool amg_elast = 0;
64  bool adios2 = false;
65 
66  OptionsParser args(argc, argv);
67  args.AddOption(&mesh_file, "-m", "--mesh",
68  "Mesh file to use.");
69  args.AddOption(&order, "-o", "--order",
70  "Finite element order (polynomial degree).");
71  args.AddOption(&nev, "-n", "--num-eigs",
72  "Number of desired eigenmodes.");
73  args.AddOption(&seed, "-s", "--seed",
74  "Random seed used to initialize LOBPCG.");
75  args.AddOption(&amg_elast, "-elast", "--amg-for-elasticity", "-sys",
76  "--amg-for-systems",
77  "Use the special AMG elasticity solver (GM/LN approaches), "
78  "or standard AMG for systems (unknown approach).");
79  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
80  "--no-visualization",
81  "Enable or disable GLVis visualization.");
82  args.AddOption(&adios2, "-adios2", "--adios2-streams", "-no-adios2",
83  "--no-adios2-streams",
84  "Save data using adios2 streams.");
85  args.Parse();
86  if (!args.Good())
87  {
88  if (myid == 0)
89  {
90  args.PrintUsage(cout);
91  }
92  MPI_Finalize();
93  return 1;
94  }
95  if (myid == 0)
96  {
97  args.PrintOptions(cout);
98  }
99 
100  // 3. Read the (serial) mesh from the given mesh file on all processors. We
101  // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
102  // and volume meshes with the same code.
103  Mesh *mesh = new Mesh(mesh_file, 1, 1);
104  int dim = mesh->Dimension();
105 
106  if (mesh->attributes.Max() < 2)
107  {
108  if (myid == 0)
109  cerr << "\nInput mesh should have at least two materials!"
110  << " (See schematic in ex12p.cpp)\n"
111  << endl;
112  MPI_Finalize();
113  return 3;
114  }
115 
116  // 4. Select the order of the finite element discretization space. For NURBS
117  // meshes, we increase the order by degree elevation.
118  if (mesh->NURBSext)
119  {
120  mesh->DegreeElevate(order, order);
121  }
122 
123  // 5. Refine the serial mesh on all processors to increase the resolution. In
124  // this example we do 'ref_levels' of uniform refinement. We choose
125  // 'ref_levels' to be the largest number that gives a final mesh with no
126  // more than 1,000 elements.
127  {
128  int ref_levels =
129  (int)floor(log(1000./mesh->GetNE())/log(2.)/dim);
130  for (int l = 0; l < ref_levels; l++)
131  {
132  mesh->UniformRefinement();
133  }
134  }
135 
136  // 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
137  // this mesh further in parallel to increase the resolution. Once the
138  // parallel mesh is defined, the serial mesh can be deleted.
139  ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
140  delete mesh;
141  {
142  int par_ref_levels = 1;
143  for (int l = 0; l < par_ref_levels; l++)
144  {
145  pmesh->UniformRefinement();
146  }
147  }
148 
149  // 7. Define a parallel finite element space on the parallel mesh. Here we
150  // use vector finite elements, i.e. dim copies of a scalar finite element
151  // space. We use the ordering by vector dimension (the last argument of
152  // the FiniteElementSpace constructor) which is expected in the systems
153  // version of BoomerAMG preconditioner. For NURBS meshes, we use the
154  // (degree elevated) NURBS space associated with the mesh nodes.
156  ParFiniteElementSpace *fespace;
157  const bool use_nodal_fespace = pmesh->NURBSext && !amg_elast;
158  if (use_nodal_fespace)
159  {
160  fec = NULL;
161  fespace = (ParFiniteElementSpace *)pmesh->GetNodes()->FESpace();
162  }
163  else
164  {
165  fec = new H1_FECollection(order, dim);
166  fespace = new ParFiniteElementSpace(pmesh, fec, dim, Ordering::byVDIM);
167  }
168  HYPRE_BigInt size = fespace->GlobalTrueVSize();
169  if (myid == 0)
170  {
171  cout << "Number of unknowns: " << size << endl
172  << "Assembling: " << flush;
173  }
174 
175  // 8. Set up the parallel bilinear forms a(.,.) and m(.,.) on the finite
176  // element space corresponding to the linear elasticity integrator with
177  // piece-wise constants coefficient lambda and mu, a simple mass matrix
178  // needed on the right hand side of the generalized eigenvalue problem
179  // below. The boundary conditions are implemented by marking only boundary
180  // attribute 1 as essential. We use special values on the diagonal to
181  // shift the Dirichlet eigenvalues out of the computational range. After
182  // serial/parallel assembly we extract the corresponding parallel matrices
183  // A and M.
184  Vector lambda(pmesh->attributes.Max());
185  lambda = 1.0;
186  lambda(0) = lambda(1)*50;
187  PWConstCoefficient lambda_func(lambda);
188  Vector mu(pmesh->attributes.Max());
189  mu = 1.0;
190  mu(0) = mu(1)*50;
191  PWConstCoefficient mu_func(mu);
192 
193  Array<int> ess_bdr(pmesh->bdr_attributes.Max());
194  ess_bdr = 0;
195  ess_bdr[0] = 1;
196 
197  ParBilinearForm *a = new ParBilinearForm(fespace);
198  a->AddDomainIntegrator(new ElasticityIntegrator(lambda_func, mu_func));
199  if (myid == 0)
200  {
201  cout << "matrix ... " << flush;
202  }
203  a->Assemble();
204  a->EliminateEssentialBCDiag(ess_bdr, 1.0);
205  a->Finalize();
206 
207  ParBilinearForm *m = new ParBilinearForm(fespace);
209  m->Assemble();
210  // shift the eigenvalue corresponding to eliminated dofs to a large value
211  m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<double>::min());
212  m->Finalize();
213  if (myid == 0)
214  {
215  cout << "done." << endl;
216  }
217 
220 
221  delete a;
222  delete m;
223 
224  // 9. Define and configure the LOBPCG eigensolver and the BoomerAMG
225  // preconditioner for A to be used within the solver. Set the matrices
226  // which define the generalized eigenproblem A x = lambda M x.
227  HypreBoomerAMG * amg = new HypreBoomerAMG(*A);
228  amg->SetPrintLevel(0);
229  if (amg_elast)
230  {
231  amg->SetElasticityOptions(fespace);
232  }
233  else
234  {
235  amg->SetSystemsOptions(dim);
236  }
237 
238  HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
239  lobpcg->SetNumModes(nev);
240  lobpcg->SetRandomSeed(seed);
241  lobpcg->SetPreconditioner(*amg);
242  lobpcg->SetMaxIter(100);
243  lobpcg->SetTol(1e-8);
244  lobpcg->SetPrecondUsageMode(1);
245  lobpcg->SetPrintLevel(1);
246  lobpcg->SetMassMatrix(*M);
247  lobpcg->SetOperator(*A);
248 
249  // 10. Compute the eigenmodes and extract the array of eigenvalues. Define a
250  // parallel grid function to represent each of the eigenmodes returned by
251  // the solver.
252  Array<double> eigenvalues;
253  lobpcg->Solve();
254  lobpcg->GetEigenvalues(eigenvalues);
255  ParGridFunction x(fespace);
256 
257  // 11. For non-NURBS meshes, make the mesh curved based on the finite element
258  // space. This means that we define the mesh elements through a fespace
259  // based transformation of the reference element. This allows us to save
260  // the displaced mesh as a curved mesh when using high-order finite
261  // element displacement field. We assume that the initial mesh (read from
262  // the file) is not higher order curved mesh compared to the chosen FE
263  // space.
264  if (!use_nodal_fespace)
265  {
266  pmesh->SetNodalFESpace(fespace);
267  }
268 
269  // 12. Save the refined mesh and the modes in parallel. This output can be
270  // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
271  {
272  ostringstream mesh_name, mode_name;
273  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
274 
275  ofstream mesh_ofs(mesh_name.str().c_str());
276  mesh_ofs.precision(8);
277  pmesh->Print(mesh_ofs);
278 
279  for (int i=0; i<nev; i++)
280  {
281  // convert eigenvector from HypreParVector to ParGridFunction
282  x = lobpcg->GetEigenvector(i);
283 
284  mode_name << "mode_" << setfill('0') << setw(2) << i << "."
285  << setfill('0') << setw(6) << myid;
286 
287  ofstream mode_ofs(mode_name.str().c_str());
288  mode_ofs.precision(8);
289  x.Save(mode_ofs);
290  mode_name.str("");
291  }
292  }
293 
294  // 13. Optionally output a BP (binary pack) file using ADIOS2. This can be
295  // visualized with the ParaView VTX reader.
296 #ifdef MFEM_USE_ADIOS2
297  if (adios2)
298  {
299  std::string postfix(mesh_file);
300  postfix.erase(0, std::string("../data/").size() );
301  postfix += "_o" + std::to_string(order);
302 
303  adios2stream adios2output("ex12-p-" + postfix + ".bp",
304  adios2stream::openmode::out, MPI_COMM_WORLD);
305  pmesh->Print(adios2output);
306  for (int i=0; i<nev; i++)
307  {
308  x = lobpcg->GetEigenvector(i);
309  // x is a temporary that must be saved immediately
310  x.Save(adios2output, "mode_" + std::to_string(i));
311  }
312  }
313 #endif
314 
315  // 14. Send the above data by socket to a GLVis server. Use the "n" and "b"
316  // keys in GLVis to visualize the displacements.
317  if (visualization)
318  {
319  char vishost[] = "localhost";
320  int visport = 19916;
321  socketstream mode_sock(vishost, visport);
322 
323  for (int i=0; i<nev; i++)
324  {
325  if ( myid == 0 )
326  {
327  cout << "Eigenmode " << i+1 << '/' << nev
328  << ", Lambda = " << eigenvalues[i] << endl;
329  }
330 
331  // convert eigenvector from HypreParVector to ParGridFunction
332  x = lobpcg->GetEigenvector(i);
333 
334  mode_sock << "parallel " << num_procs << " " << myid << "\n"
335  << "solution\n" << *pmesh << x << flush
336  << "window_title 'Eigenmode " << i+1 << '/' << nev
337  << ", Lambda = " << eigenvalues[i] << "'" << endl;
338 
339  char c;
340  if (myid == 0)
341  {
342  cout << "press (q)uit or (c)ontinue --> " << flush;
343  cin >> c;
344  }
345  MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
346 
347  if (c != 'c')
348  {
349  break;
350  }
351  }
352  mode_sock.close();
353  }
354 
355  // 15. Free the used memory.
356  delete lobpcg;
357  delete amg;
358  delete M;
359  delete A;
360 
361  if (fec)
362  {
363  delete fespace;
364  delete fec;
365  }
366  delete pmesh;
367 
368  MPI_Finalize();
369 
370  return 0;
371 }
HYPRE_BigInt GlobalTrueVSize() const
Definition: pfespace.hpp:275
int GetNE() const
Returns number of elements.
Definition: mesh.hpp:846
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:841
void SetPreconditioner(Solver &precond)
Definition: hypre.cpp:5415
void SetMassMatrix(Operator &M)
Definition: hypre.cpp:5471
Abstract parallel finite element space.
Definition: pfespace.hpp:28
void SetPrintLevel(int logging)
Definition: hypre.cpp:5400
void GetEigenvalues(Array< double > &eigenvalues) const
Collect the converged eigenvalues.
Definition: hypre.cpp:5481
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
void SetTol(double tol)
Definition: hypre.cpp:5378
int close()
Close the socketstream.
The BoomerAMG solver in hypre.
Definition: hypre.hpp:1387
void SetMaxIter(int max_iter)
Definition: hypre.cpp:5394
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
Definition: optparser.cpp:150
constexpr char vishost[]
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:9143
void SetPrintLevel(int print_level)
Definition: hypre.hpp:1467
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.
virtual void Print(std::ostream &out=mfem::out) const
Definition: pmesh.cpp:4382
void SetElasticityOptions(ParFiniteElementSpace *fespace)
Definition: hypre.cpp:4569
void SetNodalFESpace(FiniteElementSpace *nfes)
Definition: mesh.cpp:4843
int Dimension() const
Definition: mesh.hpp:911
void PrintUsage(std::ostream &out) const
Print the usage message.
Definition: optparser.cpp:457
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition: mesh.hpp:204
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
HYPRE_Int HYPRE_BigInt
void SetRandomSeed(int s)
Definition: hypre.hpp:1736
double a
Definition: lissajous.cpp:41
NURBSExtension * NURBSext
Optional NURBS mesh extension.
Definition: mesh.hpp:206
A piecewise constant coefficient with the constants keyed off the element attribute numbers...
Definition: coefficient.hpp:94
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
double mu
Definition: ex25.cpp:139
void DegreeElevate(int rel_degree, int degree=16)
Definition: mesh.cpp:4665
int dim
Definition: ex24.cpp:53
void SetOperator(Operator &A)
Definition: hypre.cpp:5424
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
void PrintOptions(std::ostream &out) const
Print the options.
Definition: optparser.cpp:327
Class for parallel bilinear form.
const HypreParVector & GetEigenvector(unsigned int i) const
Extract a single eigenvector.
Definition: hypre.cpp:5493
Vector data type.
Definition: vector.hpp:60
void GetNodes(Vector &node_coord) const
Definition: mesh.cpp:7343
void SetPrecondUsageMode(int pcg_mode)
Definition: hypre.cpp:5409
void SetSystemsOptions(int dim, bool order_bynodes=false)
Definition: hypre.cpp:4466
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:216
Class for parallel grid function.
Definition: pgridfunc.hpp:32
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:277
void SetNumModes(int num_eigs)
Definition: hypre.hpp:1734
Class for parallel meshes.
Definition: pmesh.hpp:32
void Solve()
Solve the eigenproblem.
Definition: hypre.cpp:5538
int main()
Array< int > attributes
A list of all unique element attributes used by the Mesh.
Definition: mesh.hpp:202
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150