MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
ex11p.cpp
Go to the documentation of this file.
1// MFEM Example 11 - Parallel 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/star-mixed.mesh
8// mpirun -np 4 ex11p -m ../data/escher.mesh
9// mpirun -np 4 ex11p -m ../data/fichera.mesh
10// mpirun -np 4 ex11p -m ../data/fichera-mixed.mesh
11// mpirun -np 4 ex11p -m ../data/periodic-annulus-sector.msh
12// mpirun -np 4 ex11p -m ../data/periodic-torus-sector.msh -rs 1
13// mpirun -np 4 ex11p -m ../data/toroid-wedge.mesh -o 2
14// mpirun -np 4 ex11p -m ../data/square-disc-p2.vtk -o 2
15// mpirun -np 4 ex11p -m ../data/square-disc-p3.mesh -o 3
16// mpirun -np 4 ex11p -m ../data/square-disc-nurbs.mesh -o -1
17// mpirun -np 4 ex11p -m ../data/disc-nurbs.mesh -o -1 -n 20
18// mpirun -np 4 ex11p -m ../data/pipe-nurbs.mesh -o -1
19// mpirun -np 4 ex11p -m ../data/ball-nurbs.mesh -o 2
20// mpirun -np 4 ex11p -m ../data/star-surf.mesh
21// mpirun -np 4 ex11p -m ../data/square-disc-surf.mesh
22// mpirun -np 4 ex11p -m ../data/inline-segment.mesh
23// mpirun -np 4 ex11p -m ../data/inline-quad.mesh
24// mpirun -np 4 ex11p -m ../data/inline-tri.mesh
25// mpirun -np 4 ex11p -m ../data/inline-hex.mesh
26// mpirun -np 4 ex11p -m ../data/inline-tet.mesh
27// mpirun -np 4 ex11p -m ../data/inline-wedge.mesh -s 83
28// mpirun -np 4 ex11p -m ../data/amr-quad.mesh
29// mpirun -np 4 ex11p -m ../data/amr-hex.mesh
30// mpirun -np 4 ex11p -m ../data/mobius-strip.mesh -n 8
31// mpirun -np 4 ex11p -m ../data/klein-bottle.mesh -n 10
32//
33// Description: This example code demonstrates the use of MFEM to solve the
34// eigenvalue problem -Delta u = lambda u with homogeneous
35// Dirichlet boundary conditions.
36//
37// We compute a number of the lowest eigenmodes by discretizing
38// the Laplacian and Mass operators using a FE space of the
39// specified order, or an isoparametric/isogeometric space if
40// order < 1 (quadratic for quadratic curvilinear mesh, NURBS for
41// NURBS mesh, etc.)
42//
43// The example highlights the use of the LOBPCG eigenvalue solver
44// together with the BoomerAMG preconditioner in HYPRE, as well as
45// optionally the SuperLU or STRUMPACK parallel direct solvers.
46// Reusing a single GLVis visualization window for multiple
47// eigenfunctions is also illustrated.
48//
49// We recommend viewing Example 1 before viewing this example.
50
51#include "mfem.hpp"
52#include <fstream>
53#include <iostream>
54
55using namespace std;
56using namespace mfem;
57
58int main(int argc, char *argv[])
59{
60 // 1. Initialize MPI and HYPRE.
61 Mpi::Init(argc, argv);
62 int num_procs = Mpi::WorldSize();
63 int myid = Mpi::WorldRank();
65
66 // 2. Parse command-line options.
67 const char *mesh_file = "../data/star.mesh";
68 int ser_ref_levels = 2;
69 int par_ref_levels = 1;
70 int order = 1;
71 int nev = 5;
72 int seed = 75;
73 bool slu_solver = false;
74 bool sp_solver = false;
75 bool cpardiso_solver = false;
76 bool visualization = 1;
77
78 OptionsParser args(argc, argv);
79 args.AddOption(&mesh_file, "-m", "--mesh",
80 "Mesh file to use.");
81 args.AddOption(&ser_ref_levels, "-rs", "--refine-serial",
82 "Number of times to refine the mesh uniformly in serial.");
83 args.AddOption(&par_ref_levels, "-rp", "--refine-parallel",
84 "Number of times to refine the mesh uniformly in parallel.");
85 args.AddOption(&order, "-o", "--order",
86 "Finite element order (polynomial degree) or -1 for"
87 " isoparametric space.");
88 args.AddOption(&nev, "-n", "--num-eigs",
89 "Number of desired eigenmodes.");
90 args.AddOption(&seed, "-s", "--seed",
91 "Random seed used to initialize LOBPCG.");
92#ifdef MFEM_USE_SUPERLU
93 args.AddOption(&slu_solver, "-slu", "--superlu", "-no-slu",
94 "--no-superlu", "Use the SuperLU Solver.");
95#endif
96#ifdef MFEM_USE_STRUMPACK
97 args.AddOption(&sp_solver, "-sp", "--strumpack", "-no-sp",
98 "--no-strumpack", "Use the STRUMPACK Solver.");
99#endif
100#ifdef MFEM_USE_MKL_CPARDISO
101 args.AddOption(&cpardiso_solver, "-cpardiso", "--cpardiso", "-no-cpardiso",
102 "--no-cpardiso", "Use the MKL CPardiso Solver.");
103#endif
104 args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
105 "--no-visualization",
106 "Enable or disable GLVis visualization.");
107 args.Parse();
108 if (slu_solver && sp_solver)
109 {
110 if (myid == 0)
111 cout << "WARNING: Both SuperLU and STRUMPACK have been selected,"
112 << " please choose either one." << endl
113 << " Defaulting to SuperLU." << endl;
114 sp_solver = false;
115 }
116 // The command line options are also passed to the STRUMPACK
117 // solver. So do not exit if some options are not recognized.
118 if (!sp_solver)
119 {
120 if (!args.Good())
121 {
122 if (myid == 0)
123 {
124 args.PrintUsage(cout);
125 }
126 return 1;
127 }
128 }
129 if (myid == 0)
130 {
131 args.PrintOptions(cout);
132 }
133
134 // 3. Read the (serial) mesh from the given mesh file on all processors. We
135 // can handle triangular, quadrilateral, tetrahedral, hexahedral, surface
136 // and volume meshes with the same code.
137 Mesh *mesh = new Mesh(mesh_file, 1, 1);
138 int dim = mesh->Dimension();
139
140 // 4. Refine the serial mesh on all processors to increase the resolution. In
141 // this example we do 'ref_levels' of uniform refinement (2 by default, or
142 // specified on the command line with -rs).
143 for (int lev = 0; lev < ser_ref_levels; lev++)
144 {
145 mesh->UniformRefinement();
146 }
147
148 // 5. Define a parallel mesh by a partitioning of the serial mesh. Refine
149 // this mesh further in parallel to increase the resolution (1 time by
150 // default, or specified on the command line with -rp). Once the parallel
151 // mesh is defined, the serial mesh can be deleted.
152 ParMesh *pmesh = new ParMesh(MPI_COMM_WORLD, *mesh);
153 delete mesh;
154 for (int lev = 0; lev < par_ref_levels; lev++)
155 {
156 pmesh->UniformRefinement();
157 }
158
159 // 6. Define a parallel finite element space on the parallel mesh. Here we
160 // use continuous Lagrange finite elements of the specified order. If
161 // order < 1, we instead use an isoparametric/isogeometric space.
163 if (order > 0)
164 {
165 fec = new H1_FECollection(order, dim);
166 }
167 else if (pmesh->GetNodes())
168 {
169 fec = pmesh->GetNodes()->OwnFEC();
170 }
171 else
172 {
173 fec = new H1_FECollection(order = 1, dim);
174 }
175 ParFiniteElementSpace *fespace = new ParFiniteElementSpace(pmesh, fec);
176 HYPRE_BigInt size = fespace->GlobalTrueVSize();
177 if (myid == 0)
178 {
179 cout << "Number of unknowns: " << size << endl;
180 }
181
182 // 7. Set up the parallel bilinear forms a(.,.) and m(.,.) on the finite
183 // element space. The first corresponds to the Laplacian operator -Delta,
184 // while the second is a simple mass matrix needed on the right hand side
185 // of the generalized eigenvalue problem below. The boundary conditions
186 // are implemented by elimination with special values on the diagonal to
187 // shift the Dirichlet eigenvalues out of the computational range. After
188 // serial and parallel assembly we extract the corresponding parallel
189 // matrices A and M.
190 ConstantCoefficient one(1.0);
191 Array<int> ess_bdr;
192 if (pmesh->bdr_attributes.Size())
193 {
194 ess_bdr.SetSize(pmesh->bdr_attributes.Max());
195 ess_bdr = 1;
196 }
197
198 ParBilinearForm *a = new ParBilinearForm(fespace);
199 a->AddDomainIntegrator(new DiffusionIntegrator(one));
200 if (pmesh->bdr_attributes.Size() == 0)
201 {
202 // Add a mass term if the mesh has no boundary, e.g. periodic mesh or
203 // closed surface.
204 a->AddDomainIntegrator(new MassIntegrator(one));
205 }
206 a->Assemble();
207 a->EliminateEssentialBCDiag(ess_bdr, 1.0);
208 a->Finalize();
209
210 ParBilinearForm *m = new ParBilinearForm(fespace);
212 m->Assemble();
213 // shift the eigenvalue corresponding to eliminated dofs to a large value
214 m->EliminateEssentialBCDiag(ess_bdr, numeric_limits<real_t>::min());
215 m->Finalize();
216
217 HypreParMatrix *A = a->ParallelAssemble();
219
220#if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
221 Operator * Arow = NULL;
222#ifdef MFEM_USE_SUPERLU
223 if (slu_solver)
224 {
225 Arow = new SuperLURowLocMatrix(*A);
226 }
227#endif
228#ifdef MFEM_USE_STRUMPACK
229 if (sp_solver)
230 {
231 Arow = new STRUMPACKRowLocMatrix(*A);
232 }
233#endif
234#endif
235
236 delete a;
237 delete m;
238
239 // 8. Define and configure the LOBPCG eigensolver and the BoomerAMG
240 // preconditioner for A to be used within the solver. Set the matrices
241 // which define the generalized eigenproblem A x = lambda M x.
242 Solver * precond = NULL;
243 if (!slu_solver && !sp_solver && !cpardiso_solver)
244 {
245 HypreBoomerAMG * amg = new HypreBoomerAMG(*A);
246 amg->SetPrintLevel(0);
247 precond = amg;
248 }
249 else
250 {
251#ifdef MFEM_USE_SUPERLU
252 if (slu_solver)
253 {
254 SuperLUSolver * superlu = new SuperLUSolver(MPI_COMM_WORLD);
255 superlu->SetPrintStatistics(false);
256 superlu->SetSymmetricPattern(true);
258 superlu->SetOperator(*Arow);
259 precond = superlu;
260 }
261#endif
262#ifdef MFEM_USE_STRUMPACK
263 if (sp_solver)
264 {
265 STRUMPACKSolver * strumpack = new STRUMPACKSolver(MPI_COMM_WORLD, argc, argv);
266 strumpack->SetPrintFactorStatistics(true);
267 strumpack->SetPrintSolveStatistics(false);
268 strumpack->SetKrylovSolver(strumpack::KrylovSolver::DIRECT);
269 strumpack->SetReorderingStrategy(strumpack::ReorderingStrategy::METIS);
270 strumpack->SetMatching(strumpack::MatchingJob::NONE);
271 strumpack->SetCompression(strumpack::CompressionType::NONE);
272 strumpack->SetOperator(*Arow);
273 strumpack->SetFromCommandLine();
274 precond = strumpack;
275 }
276#endif
277#ifdef MFEM_USE_MKL_CPARDISO
278 if (cpardiso_solver)
279 {
280 auto cpardiso = new CPardisoSolver(A->GetComm());
281 cpardiso->SetMatrixType(CPardisoSolver::MatType::REAL_STRUCTURE_SYMMETRIC);
282 cpardiso->SetPrintLevel(1);
283 cpardiso->SetOperator(*A);
284 precond = cpardiso;
285 }
286#endif
287 }
288
289 HypreLOBPCG * lobpcg = new HypreLOBPCG(MPI_COMM_WORLD);
290 lobpcg->SetNumModes(nev);
291 lobpcg->SetRandomSeed(seed);
292 lobpcg->SetPreconditioner(*precond);
293 lobpcg->SetMaxIter(200);
294 lobpcg->SetTol(1e-8);
295 lobpcg->SetPrecondUsageMode(1);
296 lobpcg->SetPrintLevel(1);
297 lobpcg->SetMassMatrix(*M);
298 lobpcg->SetOperator(*A);
299
300 // 9. Compute the eigenmodes and extract the array of eigenvalues. Define a
301 // parallel grid function to represent each of the eigenmodes returned by
302 // the solver.
303 Array<real_t> eigenvalues;
304 lobpcg->Solve();
305 lobpcg->GetEigenvalues(eigenvalues);
306 ParGridFunction x(fespace);
307
308 // 10. Save the refined mesh and the modes in parallel. This output can be
309 // viewed later using GLVis: "glvis -np <np> -m mesh -g mode".
310 {
311 ostringstream mesh_name, mode_name;
312 mesh_name << "mesh." << setfill('0') << setw(6) << myid;
313
314 ofstream mesh_ofs(mesh_name.str().c_str());
315 mesh_ofs.precision(8);
316 pmesh->Print(mesh_ofs);
317
318 for (int i=0; i<nev; i++)
319 {
320 // convert eigenvector from HypreParVector to ParGridFunction
321 x = lobpcg->GetEigenvector(i);
322
323 mode_name << "mode_" << setfill('0') << setw(2) << i << "."
324 << setfill('0') << setw(6) << myid;
325
326 ofstream mode_ofs(mode_name.str().c_str());
327 mode_ofs.precision(8);
328 x.Save(mode_ofs);
329 mode_name.str("");
330 }
331 }
332
333 // 11. Send the solution by socket to a GLVis server.
334 if (visualization)
335 {
336 char vishost[] = "localhost";
337 int visport = 19916;
338 socketstream mode_sock(vishost, visport);
339 mode_sock.precision(8);
340
341 for (int i=0; i<nev; i++)
342 {
343 if ( myid == 0 )
344 {
345 cout << "Eigenmode " << i+1 << '/' << nev
346 << ", Lambda = " << eigenvalues[i] << endl;
347 }
348
349 // convert eigenvector from HypreParVector to ParGridFunction
350 x = lobpcg->GetEigenvector(i);
351
352 mode_sock << "parallel " << num_procs << " " << myid << "\n"
353 << "solution\n" << *pmesh << x << flush
354 << "window_title 'Eigenmode " << i+1 << '/' << nev
355 << ", Lambda = " << eigenvalues[i] << "'" << endl;
356
357 char c;
358 if (myid == 0)
359 {
360 cout << "press (q)uit or (c)ontinue --> " << flush;
361 cin >> c;
362 }
363 MPI_Bcast(&c, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
364
365 if (c != 'c')
366 {
367 break;
368 }
369 }
370 mode_sock.close();
371 }
372
373 // 12. Free the used memory.
374 delete lobpcg;
375 delete precond;
376 delete M;
377 delete A;
378#if defined(MFEM_USE_SUPERLU) || defined(MFEM_USE_STRUMPACK)
379 delete Arow;
380#endif
381
382 delete fespace;
383 if (order > 0)
384 {
385 delete fec;
386 }
387 delete pmesh;
388
389 return 0;
390}
T Max() const
Find the maximal element in the array, using the comparison operator < for class T.
Definition array.cpp:68
void SetSize(int nsize)
Change the logical size of the array, keep existing entries.
Definition array.hpp:697
int Size() const
Return the logical size of the array.
Definition array.hpp:144
virtual void Finalize(int skip_zeros=1)
Finalizes the matrix initialization if the AssemblyLevel is AssemblyLevel::LEGACY....
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
void EliminateEssentialBCDiag(const Array< int > &bdr_attr_is_ess, real_t value)
Perform elimination and set the diagonal entry to the given value.
MKL Parallel Direct Sparse Solver for Clusters.
Definition cpardiso.hpp:31
A coefficient that is constant across space and time.
Collection of finite elements from the same family in multiple dimensions. This class is used to matc...
Definition fe_coll.hpp:27
Arbitrary order H1-conforming (continuous) finite elements.
Definition fe_coll.hpp:260
The BoomerAMG solver in hypre.
Definition hypre.hpp:1691
void SetPrintLevel(int print_level)
Definition hypre.hpp:1771
void SetMassMatrix(Operator &M)
Definition hypre.cpp:6343
void SetPrintLevel(int logging)
Definition hypre.cpp:6272
void SetPreconditioner(Solver &precond)
Definition hypre.cpp:6287
void GetEigenvalues(Array< real_t > &eigenvalues) const
Collect the converged eigenvalues.
Definition hypre.cpp:6353
void SetTol(real_t tol)
Definition hypre.cpp:6250
void SetOperator(Operator &A)
Definition hypre.cpp:6296
void SetNumModes(int num_eigs)
Definition hypre.hpp:2103
void Solve()
Solve the eigenproblem.
Definition hypre.cpp:6410
void SetPrecondUsageMode(int pcg_mode)
Definition hypre.cpp:6281
void SetRandomSeed(int s)
Definition hypre.hpp:2105
void SetMaxIter(int max_iter)
Definition hypre.cpp:6266
const HypreParVector & GetEigenvector(unsigned int i) const
Extract a single eigenvector.
Definition hypre.cpp:6365
Wrapper for hypre's ParCSR matrix class.
Definition hypre.hpp:388
MPI_Comm GetComm() const
MPI communicator.
Definition hypre.hpp:578
static void Init()
Initialize hypre by calling HYPRE_Init() and set default options. After calling Hypre::Init(),...
Definition hypre.hpp:74
Mesh data type.
Definition mesh.hpp:56
Array< int > bdr_attributes
A list of all unique boundary attributes used by the Mesh.
Definition mesh.hpp:282
int Dimension() const
Dimension of the reference space used within the elements.
Definition mesh.hpp:1160
void GetNodes(Vector &node_coord) const
Definition mesh.cpp:8973
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition mesh.cpp:10970
static int WorldRank()
Return the MPI rank in MPI_COMM_WORLD.
static int WorldSize()
Return the size of MPI_COMM_WORLD.
static void Init(int &argc, char **&argv, int required=default_thread_required, int *provided=nullptr)
Singleton creation with Mpi::Init(argc, argv).
Abstract operator.
Definition operator.hpp:25
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
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 'var' to receive the value. Enable/disable tags are used to set the bool...
Definition optparser.hpp:82
bool Good() const
Return true if the command line options were parsed successfully.
Class for parallel bilinear form.
HypreParMatrix * ParallelAssemble()
Returns the matrix assembled on the true dofs, i.e. P^t A P.
void Assemble(int skip_zeros=1)
Assemble the local matrix.
Abstract parallel finite element space.
Definition pfespace.hpp:29
HYPRE_BigInt GlobalTrueVSize() const
Definition pfespace.hpp:285
Class for parallel grid function.
Definition pgridfunc.hpp:33
void Save(std::ostream &out) const override
Class for parallel meshes.
Definition pmesh.hpp:34
void Print(std::ostream &out=mfem::out, const std::string &comments="") const override
Definition pmesh.cpp:4801
void SetOperator(const Operator &op)
Set the operator/matrix.
void SetMatching(strumpack::MatchingJob job)
Configure static pivoting for stability.
void SetCompression(strumpack::CompressionType type)
Select compression for sparse data types.
void SetPrintFactorStatistics(bool print_stat)
Set up verbose printing during the factor step.
void SetKrylovSolver(strumpack::KrylovSolver method)
Set the Krylov solver method to use.
void SetFromCommandLine()
Set options that were captured from the command line.
void SetPrintSolveStatistics(bool print_stat)
Set up verbose printing during the solve step.
void SetReorderingStrategy(strumpack::ReorderingStrategy method)
Set matrix reordering strategy.
Base class for solvers.
Definition operator.hpp:683
void SetColumnPermutation(superlu::ColPerm col_perm)
Specify how to permute the columns of the matrix.
Definition superlu.cpp:399
void SetSymmetricPattern(bool sym)
Specify whether the matrix has a symmetric pattern to avoid extra work (default false)
Definition superlu.cpp:454
void SetOperator(const Operator &op)
Set the operator/matrix.
Definition superlu.cpp:475
void SetPrintStatistics(bool print_stat)
Specify whether to print the solver statistics (default true)
Definition superlu.cpp:385
int close()
Close the socketstream.
int dim
Definition ex24.cpp:53
int main()
HYPRE_Int HYPRE_BigInt
real_t a
Definition lissajous.cpp:41
@ PARMETIS
Sequential ordering on structure of using the PARMETIS package.
Definition superlu.hpp:71
const int visport
const char vishost[]