MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
ex13p.cpp
Go to the documentation of this file.
1 // MFEM Example 13 - Parallel Version
2 //
3 // Compile with: make ex13p
4 //
5 // Sample runs: mpirun -np 4 ex13p -m ../data/star.mesh
6 // mpirun -np 4 ex13p -m ../data/square-disc.mesh -o 2 -n 4
7 // mpirun -np 4 ex13p -m ../data/beam-tet.mesh
8 // mpirun -np 4 ex13p -m ../data/beam-hex.mesh
9 // mpirun -np 4 ex13p -m ../data/escher.mesh
10 // mpirun -np 4 ex13p -m ../data/fichera.mesh
11 // mpirun -np 4 ex13p -m ../data/fichera-q2.vtk
12 // mpirun -np 4 ex13p -m ../data/fichera-q3.mesh
13 // mpirun -np 4 ex13p -m ../data/square-disc-nurbs.mesh
14 // mpirun -np 4 ex13p -m ../data/beam-hex-nurbs.mesh
15 // mpirun -np 4 ex13p -m ../data/amr-quad.mesh -o 2
16 // mpirun -np 4 ex13p -m ../data/amr-hex.mesh
17 // mpirun -np 4 ex13p -m ../data/mobius-strip.mesh -n 8 -o 2
18 // mpirun -np 4 ex13p -m ../data/klein-bottle.mesh -n 10 -o 2
19 //
20 // Description: This example code solves the Maxwell (electromagnetic)
21 // eigenvalue problem curl curl E = lambda E with homogeneous
22 // Dirichlet boundary conditions E x n = 0.
23 //
24 // We compute a number of the lowest nonzero eigenmodes by
25 // discretizing the curl curl operator using a Nedelec FE space of
26 // the specified order in 2D or 3D.
27 //
28 // The example highlights the use of the AME subspace eigenvalue
29 // solver from HYPRE, which uses LOBPCG and AMS internally.
30 // Reusing a single GLVis visualization window for multiple
31 // eigenfunctions is also illustrated.
32 //
33 // We recommend viewing examples 3 and 11 before viewing this
34 // example.
35 
36 #include "mfem.hpp"
37 #include <fstream>
38 #include <iostream>
39 
40 using namespace std;
41 using namespace mfem;
42 
43 int main(int argc, char *argv[])
44 {
45  // 1. Initialize MPI.
46  int num_procs, myid;
47  MPI_Init(&argc, &argv);
48  MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
49  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
50 
51  // 2. Parse command-line options.
52  const char *mesh_file = "../data/beam-tet.mesh";
53  int ser_ref_levels = 2;
54  int par_ref_levels = 1;
55  int order = 1;
56  int nev = 5;
57  bool visualization = 1;
58  const char *device_config = "cpu";
59 
60  OptionsParser args(argc, argv);
61  args.AddOption(&mesh_file, "-m", "--mesh",
62  "Mesh file to use.");
63  args.AddOption(&ser_ref_levels, "-rs", "--refine-serial",
64  "Number of times to refine the mesh uniformly in serial.");
65  args.AddOption(&par_ref_levels, "-rp", "--refine-parallel",
66  "Number of times to refine the mesh uniformly in parallel.");
67  args.AddOption(&order, "-o", "--order",
68  "Finite element order (polynomial degree) or -1 for"
69  " isoparametric space.");
70  args.AddOption(&nev, "-n", "--num-eigs",
71  "Number of desired eigenmodes.");
72  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
73  "--no-visualization",
74  "Enable or disable GLVis visualization.");
75  args.AddOption(&device_config, "-d", "--device",
76  "Device configuration string, see Device::Configure().");
77  args.Parse();
78  if (!args.Good())
79  {
80  if (myid == 0)
81  {
82  args.PrintUsage(cout);
83  }
84  MPI_Finalize();
85  return 1;
86  }
87  if (myid == 0)
88  {
89  args.PrintOptions(cout);
90  }
91 
92  // 3. Enable hardware devices such as GPUs, and programming models such as
93  // CUDA, OCCA, RAJA and OpenMP based on command line options.
94  Device device(device_config);
95  if (myid == 0) { device.Print(); }
96 
97  // 4. Read the (serial) mesh from the given mesh file on all processors. We
98  // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
99  // and volume meshes with the same code.
100  Mesh *mesh = new Mesh(mesh_file, 1, 1);
101  int dim = mesh->Dimension();
102 
103  // 5. Refine the serial mesh on all processors to increase the resolution. In
104  // this example we do 'ref_levels' of uniform refinement (2 by default, or
105  // specified on the command line with -rs).
106  for (int lev = 0; lev < ser_ref_levels; lev++)
107  {
108  mesh->UniformRefinement();
109  }
110 
111  // 6. Define a parallel mesh by a partitioning of the serial mesh. Refine
112  // this mesh further in parallel to increase the resolution (1 time by
113  // default, or specified on the command line with -rp). Once the parallel
114  // mesh is defined, the serial mesh can be deleted.
115  ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
116  delete mesh;
117  for (int lev = 0; lev < par_ref_levels; lev++)
118  {
119  pmesh->UniformRefinement();
120  }
121  pmesh->ReorientTetMesh();
122 
123  // 7. Define a parallel finite element space on the parallel mesh. Here we
124  // use the Nedelec finite elements of the specified order.
125  FiniteElementCollection *fec = new ND_FECollection(order, dim);
126  ParFiniteElementSpace *fespace = new ParFiniteElementSpace(pmesh, fec);
127  HYPRE_BigInt size = fespace->GlobalTrueVSize();
128  if (myid == 0)
129  {
130  cout << "Number of unknowns: " << size << endl;
131  }
132 
133  // 8. Set up the parallel bilinear forms a(.,.) and m(.,.) on the finite
134  // element space. The first corresponds to the curl curl, while the second
135  // is a simple mass matrix needed on the right hand side of the
136  // generalized eigenvalue problem below. The boundary conditions are
137  // implemented by marking all the boundary attributes from the mesh as
138  // essential. The corresponding degrees of freedom are eliminated with
139  // special values on the diagonal to shift the Dirichlet eigenvalues out
140  // of the computational range. After serial and parallel assembly we
141  // extract the corresponding parallel matrices A and M.
142  ConstantCoefficient one(1.0);
143  Array<int> ess_bdr;
144  if (pmesh->bdr_attributes.Size())
145  {
146  ess_bdr.SetSize(pmesh->bdr_attributes.Max());
147  ess_bdr = 1;
148  }
149 
150  ParBilinearForm *a = new ParBilinearForm(fespace);
152  if (pmesh->bdr_attributes.Size() == 0)
153  {
154  // Add a mass term if the mesh has no boundary, e.g. periodic mesh or
155  // closed surface.
157  }
158  a->Assemble();
159  a->EliminateEssentialBCDiag(ess_bdr, 1.0);
160  a->Finalize();
161 
162  ParBilinearForm *m = new ParBilinearForm(fespace);
164  m->Assemble();
165  // shift the eigenvalue corresponding to eliminated dofs to a large value
166  m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<double>::min());
167  m->Finalize();
168 
171 
172  delete a;
173  delete m;
174 
175  // 9. Define and configure the AME eigensolver and the AMS preconditioner for
176  // A to be used within the solver. Set the matrices which define the
177  // generalized eigenproblem A x = lambda M x.
178  HypreAMS *ams = new HypreAMS(*A,fespace);
179  ams->SetPrintLevel(0);
180  ams->SetSingularProblem();
181 
182  HypreAME *ame = new HypreAME(MPI_COMM_WORLD);
183  ame->SetNumModes(nev);
184  ame->SetPreconditioner(*ams);
185  ame->SetMaxIter(100);
186  ame->SetTol(1e-8);
187  ame->SetPrintLevel(1);
188  ame->SetMassMatrix(*M);
189  ame->SetOperator(*A);
190 
191  // 10. Compute the eigenmodes and extract the array of eigenvalues. Define a
192  // parallel grid function to represent each of the eigenmodes returned by
193  // the solver.
194  Array<double> eigenvalues;
195  ame->Solve();
196  ame->GetEigenvalues(eigenvalues);
197  ParGridFunction x(fespace);
198 
199  // 11. Save the refined mesh and the modes in parallel. This output can be
200  // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
201  {
202  ostringstream mesh_name, mode_name;
203  mesh_name << "mesh." << setfill('0') << setw(6) << myid;
204 
205  ofstream mesh_ofs(mesh_name.str().c_str());
206  mesh_ofs.precision(8);
207  pmesh->Print(mesh_ofs);
208 
209  for (int i=0; i<nev; i++)
210  {
211  // convert eigenvector from HypreParVector to ParGridFunction
212  x = ame->GetEigenvector(i);
213 
214  mode_name << "mode_" << setfill('0') << setw(2) << i << "."
215  << setfill('0') << setw(6) << myid;
216 
217  ofstream mode_ofs(mode_name.str().c_str());
218  mode_ofs.precision(8);
219  x.Save(mode_ofs);
220  mode_name.str("");
221  }
222  }
223 
224  // 12. Send the solution by socket to a GLVis server.
225  if (visualization)
226  {
227  char vishost[] = "localhost";
228  int visport = 19916;
229  socketstream mode_sock(vishost, visport);
230  mode_sock.precision(8);
231 
232  for (int i=0; i<nev; i++)
233  {
234  if ( myid == 0 )
235  {
236  cout << "Eigenmode " << i+1 << '/' << nev
237  << ", Lambda = " << eigenvalues[i] << endl;
238  }
239 
240  // convert eigenvector from HypreParVector to ParGridFunction
241  x = ame->GetEigenvector(i);
242 
243  mode_sock << "parallel " << num_procs << " " << myid << "\n"
244  << "solution\n" << *pmesh << x << flush
245  << "window_title 'Eigenmode " << i+1 << '/' << nev
246  << ", Lambda = " << eigenvalues[i] << "'" << endl;
247 
248  char c;
249  if (myid == 0)
250  {
251  cout << "press (q)uit or (c)ontinue --> " << flush;
252  cin >> c;
253  }
254  MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
255 
256  if (c != 'c')
257  {
258  break;
259  }
260  }
261  mode_sock.close();
262  }
263 
264  // 13. Free the used memory.
265  delete ame;
266  delete ams;
267  delete M;
268  delete A;
269 
270  delete fespace;
271  delete fec;
272  delete pmesh;
273 
274  MPI_Finalize();
275 
276  return 0;
277 }
int Size() const
Return the logical size of the array.
Definition: array.hpp:134
The Auxiliary-space Maxwell Solver in hypre.
Definition: hypre.hpp:1540
A coefficient that is constant across space and time.
Definition: coefficient.hpp:78
void SetMassMatrix(const HypreParMatrix &M)
Definition: hypre.cpp:5747
virtual void ReorientTetMesh()
See the remarks for the serial version in mesh.hpp.
Definition: pmesh.cpp:2798
Integrator for (curl u, curl v) for Nedelec elements.
HYPRE_BigInt GlobalTrueVSize() const
Definition: pfespace.hpp:275
void SetPreconditioner(HypreSolver &precond)
Definition: hypre.cpp:5726
void SetTol(double tol)
Definition: hypre.cpp:5695
virtual void Save(std::ostream &out) const
Definition: pgridfunc.cpp:841
void Print(std::ostream &out=mfem::out)
Print the configuration of the MFEM virtual device object.
Definition: device.cpp:279
Abstract parallel finite element space.
Definition: pfespace.hpp:28
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
int close()
Close the socketstream.
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
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
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 SetPrintLevel(int print_lvl)
Definition: hypre.cpp:4981
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:674
void Solve()
Solve the eigenproblem.
Definition: hypre.cpp:5754
HYPRE_Int HYPRE_BigInt
void SetNumModes(int num_eigs)
Definition: hypre.cpp:5687
const HypreParVector & GetEigenvector(unsigned int i) const
Extract a single eigenvector.
Definition: hypre.cpp:5790
void SetMaxIter(int max_iter)
Definition: hypre.cpp:5711
double a
Definition: lissajous.cpp:41
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization.
int dim
Definition: ex24.cpp:53
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
void GetEigenvalues(Array< double > &eigenvalues) const
Collect the converged eigenvalues.
Definition: hypre.cpp:5766
void SetPrintLevel(int logging)
Definition: hypre.cpp:5717
Class for parallel bilinear form.
Arbitrary order H(curl)-conforming Nedelec finite elements.
Definition: fe_coll.hpp:416
void SetOperator(const HypreParMatrix &A)
Definition: hypre.cpp:5732
Class for parallel grid function.
Definition: pgridfunc.hpp:32
The MFEM Device class abstracts hardware devices such as GPUs, as well as programming models such as ...
Definition: device.hpp:121
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:277
Class for parallel meshes.
Definition: pmesh.hpp:32
void SetSingularProblem()
Set this option when solving a curl-curl problem with zero mass term.
Definition: hypre.hpp:1565
int main()
bool Good() const
Return true if the command line options were parsed successfully.
Definition: optparser.hpp:150